Offscreen rendering in skia with Metal in iOS

299 views
Skip to first unread message

sAsuKe

unread,
May 26, 2021, 9:08:55 AM5/26/21
to skia-discuss
HI, I'm using metal to draw some contents on my screen. So I'm using the below code to draw the contents offscreen and i'll use it whenever needed

SkSurfaceCharacterization characterization;

if(!surface->characterize(&characterization)) {

return

}

SkDeferredDisplayListRecorder recorder(characterization);

SkCanvas* subcanvas = recorder.getCanvas();

//some drawings

sk_sp<SkDeferredDisplayList> displayList = recorder.detach();

I'm storing this displayList and reusing it later. But this displayList canvas has beed cleared after a single use. But i need to persist it and use it later for multiple occurrences.

Also is it possible to draw contents in multiple thread using skia in metal?

Regards,

Praveenkumar R

Jim Van Verth

unread,
May 27, 2021, 9:23:22 AM5/27/21
to skia-discuss
Unless I'm missing something, it looks like you're creating a display list but not actually rendering it. The recorder SkCanvas only acts as an interface for recording -- it doesn't draw anything to a surface.

And currently we assume that all rendering occurs in a single thread.

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/5fa7eeaa-30a9-4609-acb1-7d1c1fa9df85n%40googlegroups.com.


--

Jim Van Verth | Software Engineer | jvan...@google.com | 919-210-7664

sAsuKe

unread,
May 28, 2021, 3:03:29 AM5/28/21
to skia-discuss
Sorry i forgot to add some code.

this is my use case ----> My drawable viewport size is 1000*1000, So its not drawing the contents in 60fps(frame rate).

here are the few thing i tried to make the frame rate 60fps,

1. I tried creating a multiple DeferredDisplayList for single surface and tries to draw in multiple thread.(Note: Creating is happening in main thread, only the rendering is happening in different threads). But doing this crashes the app. So is there any way that i can achieve this case? here is the code that i tried

///

void ZSSample::drawInMultipleThread(SkCanvas* canvas) {

    SkImageInfo firstInfo = SkImageInfo::MakeN32(500, 500, SkAlphaType::kPremul_SkAlphaType);

    sk_sp<SkSurface> firstSurface = SkSurface::MakeRenderTarget(canvas->recordingContext(), SkBudgeted::kYes, firstInfo);

    SkSurfaceCharacterization firstCharacterization;

    if(!firstSurface->characterize(&firstCharacterization)) {

        return;

    }

    SkDeferredDisplayListRecorder firstRecorder(firstCharacterization);

    SkCanvas *firstCanvas = firstRecorder.getCanvas();


    SkImageInfo secondInfo = SkImageInfo::MakeN32(500, 500, SkAlphaType::kPremul_SkAlphaType);

    sk_sp<SkSurface> secondSurface = SkSurface::MakeRenderTarget(canvas->recordingContext(), SkBudgeted::kYes, secondInfo);

    SkSurfaceCharacterization secondCharacterization;

    if(!secondSurface->characterize(&secondCharacterization)) {

        return;

    }

    SkDeferredDisplayListRecorder secondRecorder(secondCharacterization);

    SkCanvas *secondCanvas = secondRecorder.getCanvas();

    

    std::thread firstThread(drawInSurface, firstCanvas);

    std::thread secondThread(drawInSurface, secondCanvas);

    firstThread.join();

    secondThread.join();

    sk_sp<SkDeferredDisplayList> firstDisplayList = firstRecorder.detach();

    firstSurface->draw(firstDisplayList);

    

    sk_sp<SkDeferredDisplayList> secondDisplayList = secondRecorder.detach();

    secondSurface->draw(secondDisplayList);

    

    sk_sp<SkImage> firstImage = firstSurface->makeImageSnapshot();

    sk_sp<SkImage> secondImage = secondSurface->makeImageSnapshot();

    canvas->drawImage(firstImage, 0, 0);

    canvas->drawImage(secondImage, 0, 500);

}

///

2.So i tried creating a multiple surface using MakeRenderTarget and draw the contents in multiple thread, But even in this case my app crashes. Please check the below sample

///

void ZSSample::drawInMultipleThread(SkCanvas* canvas) {

    SkImageInfo firstInfo = SkImageInfo::MakeN32(500, 500, SkAlphaType::kPremul_SkAlphaType);

    sk_sp<SkSurface> firstSurface = SkSurface::MakeRenderTarget(canvas->recordingContext(), SkBudgeted::kYes, firstInfo);

    SkCanvas *firstCanvas = firstSurface->getCanvas();

   

    SkImageInfo secondInfo = SkImageInfo::MakeN32(500, 500, SkAlphaType::kPremul_SkAlphaType);

    sk_sp<SkSurface> secondSurface = SkSurface::MakeRenderTarget(canvas->recordingContext(), SkBudgeted::kNo, secondInfo);

    SkCanvas* secondCanvas = secondSurface->getCanvas();


    std::thread firstThread(drawInSurface, firstCanvas);

    std::thread secondThread(drawInSurface, secondCanvas);

    firstThread.join();

    secondThread.join();

   sk_sp<SkImage> image = firstSurface->makeImageSnapshot();

    sk_sp<SkImage> image1 = secondSurface->makeImageSnapshot();

    canvas->drawImage(image, 0, 0);

    canvas->drawImage(image1, 0, 500);

}

///

In both the approaches the app crashes, is there any way that i can achieve the above case.

Reply all
Reply to author
Forward
0 new messages