SkImage from SkCanvas

265 views
Skip to first unread message

harish jangra

unread,
Mar 22, 2021, 1:41:46 PM3/22/21
to skia-d...@googlegroups.com
Hi all

I need to get SkImage of a rectangular region of current framebuffer texture. SkImage should be backed by a GPU texture.

What is the best way to do this?

Regards
Harish

Mike Reed

unread,
Mar 22, 2021, 2:12:20 PM3/22/21
to skia-d...@googlegroups.com
surface.makeImageSnapshot(rectangle)

--
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/CAAe2eYWeoVnEQw6O-DV6pTD2ux7VQdATREy-m%3D6Ocyt1gYcoXA%40mail.gmail.com.

harish jangra

unread,
Mar 30, 2021, 1:34:18 AM3/30/21
to skia-discuss
Hi Mike,

Thanks for the response. I found another way of doing something similar using SaveLayer/Restore calls. Calling savelayer redirects my rendering to new render texture and original (before call to SaveLayer) frame buffer texture is available for use in restre call via SaveLayerRec. Which of the two should be preferable from performance perspective - MakeImageSnapshot Vs SaveLayer/Restore?

Mike Reed

unread,
Mar 31, 2021, 10:41:08 AM3/31/21
to skia-d...@googlegroups.com
save/restore will be as fast as possible, assuming you don't want to re-use the snapshot multiple times.

harish jangra

unread,
Apr 1, 2021, 9:45:00 AM4/1/21
to skia-d...@googlegroups.com
Thanks Mike.

One more question- what is the way to ensure synchronization across 2 consecutive draw calls.

For example - Draw1 draws some color on canvas. Fragment shader of Draw2 needs to read the color from texture that Draw1's fragment shader would have written. With gl, we achieve this using glTextureBarrier. I was looking for something similar in skia to ensure this? Please suggest.

Regards
Harish


You received this message because you are subscribed to a topic in the Google Groups "skia-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/skia-discuss/Io4b6eKeMzo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/CAERTzqxBPaVM025G_qfdWEVCp%3DUyG4_pFRCa8HNHwK-OR9emOQ%40mail.gmail.com.

Mike Reed

unread,
Apr 1, 2021, 11:18:39 AM4/1/21
to skia-d...@googlegroups.com
If you're drawing through the same SkCanvas object, it will all works as intended -- the 'writes' from draw A will be visible (for blending) for draw B.

harish jangra

unread,
Apr 5, 2021, 8:55:40 AM4/5/21
to skia-discuss
Thanks Mike.

harish jangra

unread,
Apr 10, 2021, 5:11:36 AM4/10/21
to skia-discuss
Hi Mike,

Thanks for the responses. Another follow up question on MakeImageSnapshot,

I am getting Alpha Channel in output image of MakeImageSnapshot as 0xFF whereas i expect it to be same as it was in render texture, Is this expectation correct?

Following is brief code snippet to show the issue:

canvas->clear(SK_ColorTransparent); // Clears  RGBA to zeros on full render texture

SkPaint clearPaint;
clearPaint.setColor(SK_ColorRED);
canvas->drawRect(bounds(), clearPaint); // Draws partial section of rendertexture with red color (R- 0xFF, A- 0xFF

sk_sp<SkImage> image = canvas->getSurface()->makeImageSnapshot();
sk_sp<SkShader> imageShader = image->makeShader(SkSamplingOptions(SkFilterMode::kNearest, SkMipmapMode::kNone));
const char* src =
"uniform shader input;"
"half4 main(float2 coord) {"
"half4 red = float4(1.0, 0.0, 0.0, 1.0);"
"half4 green = float4(0.0, 1.0, 0.0, 1.0);"
"half4 color = sample(input).rgba;"
"if(color.r < 0.5)"                 // Comparing R channel values
" return red;"
"else"
" return green;"
"}";
           
           Above shader correctly renders partial area (where red was drawn with drawrect) 'Green' and remaining Red. But If i change the highlighted line  (in Bold) above to following:
                      "if(color.a < 0.5)"                 // Comparing R channel values
          Then shader renders full Green as if full image has Alpha channel > 0.5.

Please assist if there is any way to change some image color type/alpha type so that Alpha channels in output of makeImageSnapshot() are same as in render texture (canvas->getSurface()).

Regards
Harish

Mike Reed

unread,
Apr 10, 2021, 8:36:36 AM4/10/21
to skia-d...@googlegroups.com
https://fiddle.skia.org/c/e54ca8ac88233d374824688c7a034609

I'm not sure what draw call is invoking your shader. The above link just draws the snapped image, and since you can see green, that means the snapped image had alpha==0 outside of the red rectangle.

harish jangra

unread,
Apr 14, 2021, 6:15:56 AM4/14/21
to skia-d...@googlegroups.com
Thanks Mike. There was some bug with my locally compiled skia leading to issue. Worked well with binaries from latest skia code.

Regards
Harish

Reply all
Reply to author
Forward
0 new messages