How to draw GPU backed SkImage, ignoring its alpha channel.

166 views
Skip to first unread message

ahujab...@gmail.com

unread,
Jul 1, 2021, 3:12:52 AM7/1/21
to skia-discuss

Hi,

I have a texture that I wanted to draw on a GPU surface but I want to ignore its alpha channel while drawing. I created a skimage using SkImage::MakeFromTexture and passed kOpaque_SkAlphaType while creation. The BackEnd texture was created using GL_RGBA8_OES format but it seems like it's not working as expected. I am trying to implement an API similar to Direct2D's createsharedbitmap.

 (https://docs.microsoft.com/en-us/windows/win32/api/d2d1/nf-d2d1-id2d1rendertarget-createsharedbitmap)

Backend Texture Creation:

GrGLTextureInfo info;
info.fTarget = OGL2::GL_TEXTURE_TYPE;
info.fID = textureID;
info.fFormat = GL_RGBA8_OES;

GrBackendTexture backEndTexture( width,
heigh, GrMipMapped::kNo, info );

SkImageCreation:

sk_sp< SkImage > pImage = SkImage::MakeFromTexture( pCtx,
backEndTexture, kBottomLeft_GrSurfaceOrigin, kRGBx_888_SkColorType/*( tried kRGBA_8888_SkColorType too)*/
kOpaque_SkAlphaType, nullptr /*colorSpacePtr*/, textureReleaseProc,
reinterpret_cast< SkImage::ReleaseContext >(mycontext) );

Thanks

Bharat

Greg Daniel

unread,
Jul 1, 2021, 7:37:39 AM7/1/21
to skia-discuss
Did you try not passing on kOpaque?

--
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/300307d3-7b60-4006-a57e-6cabe8aff431n%40googlegroups.com.

ahujab...@gmail.com

unread,
Jul 1, 2021, 11:44:09 AM7/1/21
to skia-discuss
I tried kPremul_SkAlphaType and  kOpaque_SkAlphaType  both but the output doesn't change. It seems like passing skcolortype and alpha-type has no effect and rendered content is using the alpha channel of the backend texture. 

I can write a SkRunTimeEffect and create skimage out of it but I believe that is too much to do and will involve an extra pass and skia should have some trivial API to make it work.

Brian Osman

unread,
Jul 1, 2021, 11:53:42 AM7/1/21
to skia-d...@googlegroups.com
I think you can get the effect you want with a matrix color filter, if nothing else:

SkColorMatrix cm;
cm.postTranslate(0, 0, 0, 1);  // a += 1
paint.setColorFilter(SkColorFilters::Matrix(cm));

Greg Daniel

unread,
Jul 1, 2021, 12:05:28 PM7/1/21
to skia-discuss
Oh yes sorry, what Brian said. I misread your first post and thought you didn't want the alpha to be ignored.

Bharat Ahuja

unread,
Jul 1, 2021, 1:04:04 PM7/1/21
to skia-d...@googlegroups.com
Yes i can use this.

BTW , I use this skimage to do various stuff, I use drawImageRect, create skshader for tiling and I also create SkImageFilter from skimage to render some effects so was looking something which can create SkImage to unify all the stuff.

Btw curious what is the use of params color type and alpha type if makefromtexture doesn't respect them. Ideally, these parameters can be calculated from the backend texture format if they have to match with input texture format.

Thanks
Bharat

Greg Daniel

unread,
Jul 1, 2021, 1:12:43 PM7/1/21
to skia-discuss
There is not always a 1 to 1 correspondence between the backed texture format and the SkColorType. For example, your backend format could be R8, but you could use that with kAlpha_8_SkColorType or kGray_8_SkColorType.

Bharat Ahuja

unread,
Jul 1, 2021, 1:20:40 PM7/1/21
to skia-d...@googlegroups.com
I see. I guess I'll do this using ColorFilter then. Thanks for the help guys.


Thanks
Bharat

Reply all
Reply to author
Forward
0 new messages