Skia render to OpenGLES texture on Android

750 views
Skip to first unread message

Dustin Forster

unread,
Jul 21, 2014, 3:12:54 PM7/21/14
to skia-d...@googlegroups.com
I am trying to use Skia to draw some 2D geometry to a texture that will then be composited into my 3D scene later during the draw pass. However no matter what I do my draw calls are always drawn in the top left corner of the screen and the entire screen blinks black at a high rate. It seems like Skia is only rendering to the main frame buffer instead of the texture that I'm asking it to. What am I doing wrong?

Here is a snippet of what I've tried:

    //Setup EGL context, display, etc.

   
SkGraphics::Init();
   
const GrGLInterface* fInterface = GrGLCreateNativeInterface();
    mSkContext
= GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fInterface);

   
// Do other operations in OpenGL

    mSkContext
->resetContext();

   
GrTextureDesc texDesc;
    texDesc
.fFlags = kRenderTarget_GrTextureFlagBit;
    texDesc
.fWidth = aWidth;
    texDesc
.fHeight = aHeight;
    texDesc
.fConfig = kRGBA_8888_GrPixelConfig;

   
static uint64_t cacheNum = 0;
   
GrCacheID::Key cacheKey;
    cacheKey
.fData64[0] = cacheNum;
    cacheKey
.fData64[1] = 0;
   
GrCacheID cacheId(mSkDomain, cacheKey);
    cacheNum
++;
   
GrTexture* skTexture = mSkContext->createTexture(NULL, texDesc, cacheId, 0, 0);
   
GLuint texHandle = skTexture->getTextureHandle();

   
SkSurface* skSurface = SkSurface::NewRenderTargetDirect(skTexture->asRenderTarget());
   
SkCanvas* skCanvas = skSurface->getCanvas();

    mSkContext
->resetContext();
   
GrContext::AutoRenderTarget autoTarget(mSkContext, itr->second.skRenderTarget);

   
SkRect rect = SkRect::MakeWH(aWidth, aHeight);
    itr
->second.sk2DCanvas->clear(SK_ColorRED); // Red for debugging purposes
   
   
SkPaint paint;
    paint
.setStyle(SkPaint::kStroke_Style);
    paint
.setStrokeWidth(2);
    paint
.setColor(SK_ColorBLUE);
    itr
->second.sk2DCanvas->drawRect(rect, paint);

    mSkContext
->flush();


   
// Do other rendering in OpenGL


    glBindTexture
( GL_TEXTURE_2D, texHandle );
   
// Use texture on rendered triangles

Thanks,

Dustin Forster



Brian Salomon

unread,
Jul 21, 2014, 3:27:38 PM7/21/14
to skia-d...@googlegroups.com
Hey Dustin,

You shouldn't be setting the render target on GrContext with AutoRenderTarget. When you draw to a GPU-backed skcanvas the correct render target is automatically bound. Also, use createUncachedTexture() to create your texture rather than using a GrCacheID. I'm not sure if those changes will fix the issue you're seeing.

Brian


--
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 post to this group, send email to skia-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/skia-discuss.
For more options, visit https://groups.google.com/d/optout.

Dustin Forster

unread,
Jul 21, 2014, 4:25:09 PM7/21/14
to skia-d...@googlegroups.com
Thanks for the info. I thought that the render target would be set automatically and was originally not setting it but threw that in to try to fix my issue. I've removed the AutoRenderTarget and created my texture uncached but I'm still getting the same result. I've also tried creating the texture manually using GL calls and then wrapping it as a render target but that also produces the same result. Does it look like I'm generally setting things up correctly? When I create the texture, my display starts blinking continuously, even if I comment out the rest of the code that uses the texture. If I create the texture manually using direct GL calls, the screen starts blinking when I call NewRenderTargetDirect to get a surface. Fixing this issue would be my first hurdle. Here is the code I use to create the texture:

    glGenTextures( 1, &(texHandle) );
    glBindTexture
(GL_TEXTURE_2D, texHandle);
    glTexImage2D
(
        GL_TEXTURE_2D
,
       
0,
        GL_RGBA
,
        aWidth
,
        aHeight
,
       
0,
        GL_RGBA
,
        GL_UNSIGNED_BYTE
,
       
0 );



Thanks,
Dustin Forster

Brian Salomon

unread,
Jul 21, 2014, 4:30:53 PM7/21/14
to skia-d...@googlegroups.com
Your setup looks ok to me. Is your non-Skia GL code expecting Skia to change the GL state? I'm wondering if Skia is changing the currently bound FBO in ways your surrounding code doesn't expect. After making Skia calls you kind of have to assume that the GL state is unknown since Skia is whacking away at your GL context.

Brian
Reply all
Reply to author
Forward
Message has been deleted
0 new messages