Thanks for the answer and great sample.
Now speed is much higher than before and for example, for drawing 1000 of 100x100 texture it takes 50ms on my machine.
The remaining problem is the speed is lower than a simple OpenGL program, for example for 3000 of 100x100 textures it takes less than 20ms and near 200mb of memory but skia sample takes 500ms and 300mb of memory.
sk_sp<SkImage> *surfImage;
bool isFirst = true;
void draw2(SkCanvas* canvas) {
// This fiddle directly renders to an off-screen GPU surface
if (!canvas->getGrContext()) {
return;
}
if (isFirst) {
surfImage = new sk_sp<SkImage>[3000];
// sk_sp<SkImage> image is defined above, but could have been loaded from disk...
sk_sp<SkImage> image;
sk_sp<SkData> imageData(SkData::MakeFromFileName("test.png"));// 100x100 png file
image = SkImage::MakeFromEncoded(imageData);
for (size_t i = 0; i < 3000; i++)
{
SkImageInfo info = SkImageInfo::MakeN32(image->width(), image->height(), SkAlphaType::kPremul_SkAlphaType);
sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(canvas->getGrContext(), SkBudgeted::kYes, info);
SkCanvas* surfCanvas = surface->getCanvas();
surfCanvas->drawImage(image, 0, 0);
// No flush needed here - Skia handles that.
// We can draw on top of the image, if we like:
SkPaint p;
p.setColor(SK_ColorRED);
p.setStyle(SkPaint::kStroke_Style);
p.setStrokeWidth(15);
surfCanvas->drawLine(0, image->height(), image->width(), 0, p);
// Make a snapshot of this surface at this point in time. Because the surface was
// created on the GPU ('MakeRenderTarget'), the image is also on the GPU.
surfImage[i] = surface->makeImageSnapshot();
// Release the surface, too, if we're worried about that
surface = nullptr;
}
// Release the original image
image = nullptr;
// At this point, the only thing that exists is our image we created, with an extra line on it
// Draw a bunch of copies of it...
isFirst = false;
}
SkRandom random;
for (int i = 0; i < 3000; ++i) {
canvas->drawImage(surfImage[i], random.nextRangeF(0,1920) , random.nextRangeF(0, 1080));
}
}