Sharing OpenGL context with Skia

1,789 views
Skip to first unread message

Ray Tawil

unread,
Jul 19, 2017, 12:13:30 PM7/19/17
to skia-discuss
Hi,

I'm trying to use Skia with a GPU accelerated backend. I figured that I have to implement GrGLInterface so that I can share the OpenGL context with Skia. Does anyone have an example/tutorial of a similar use case that they can share?

Thanks,
Ray

Jim Van Verth

unread,
Jul 19, 2017, 12:53:50 PM7/19/17
to skia-discuss
You might want to look at example/SkiaSDLExample.cpp, which integrates Skia with an SDL backend. Lines 186-213 show you how to set up an SkSurface that uses the currently bound framebuffer and then you can get an SkCanvas from that.

--
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 https://groups.google.com/group/skia-discuss.
For more options, visit https://groups.google.com/d/optout.

Brian Salomon

unread,
Jul 19, 2017, 12:55:17 PM7/19/17
to skia-discuss
Hi Ray, unless you're doing something complicated just pass (GrBackendContext)(nullptr) to GrContext::Create() when your GL context is bound and Skia will build its own GrGLInterface internally.

Brian

On Wed, Jul 19, 2017 at 12:13 PM 'Ray Tawil' via skia-discuss <skia-d...@googlegroups.com> wrote:
--

Jim Van Verth

unread,
Jul 19, 2017, 12:57:49 PM7/19/17
to skia-discuss
We should probably fix the example -- currently it's explicitly using GrGLCreateNativeInterface() to create a default interface and passing it into GrContext::Create().

Ray Tawil

unread,
Jul 20, 2017, 2:30:33 PM7/20/17
to skia-discuss
Thanks Brian & Jim. I have a follow up question in that case. It could be a stupid question but this is my first time doing a project involved with graphics and OpenGL. If I pass a nullptr and go for the default option of letting Skia build its own GrGLInterface internally, how is Skia going to figure out how to communicate with the GPU and what version of OpenGL is used, etc. 

And just for context, I want to integrate Skia with an internal framework that I can't discuss publicly unfortunately, for the purpose of supporting SVG rendering.

李昀陞

unread,
Jul 20, 2017, 4:19:23 PM7/20/17
to skia-discuss
Thank you for the Ray's question.

I also have the same problems for sharing the OpenGL context with Skia.

I really want to know the correct example code on SkiaSDLExample.cpp.

Thanks.

Ray Tawil於 2017年7月20日星期四 UTC+8上午12時13分30秒寫道:

Jim Van Verth

unread,
Jul 20, 2017, 4:41:33 PM7/20/17
to skia-discuss
I'm not sure what additional questions you may have. SkiaSDLExample.cpp is correct, except that you don't have to explicitly create the GrGLInterface. So instead of

// setup GrContext
sk_sp<const GrGLInterface> interface(GrGLCreateNativeInterface());

// setup contexts
sk_sp<GrContext> grContext(GrContext::Create(kOpenGL_GrBackend,
                                             (GrBackendContext)interface.get()));

you can simply do

// setup GrContext
sk_sp<GrContext> grContext(GrContext::Create(kOpenGL_GrBackend,
                                             (GrBackendContext) nullptr));

But both will work.

--

李昀陞

unread,
Jul 21, 2017, 1:48:54 PM7/21/17
to skia-discuss
Hi Jim, thank you for your reply. 

When I try to create the OpenGL context, it's failed for SkASSERT(grContext);

What;s the current error message? Thanks. Here is the code snippet:



// setup GrContext
    sk_sp
<const GrGLInterface> interface(GrGLCreateNativeInterface());


 
// setup contexts

    sk_sp
<GrContext> grContext(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) nullptr));
 
//sk_sp<GrContext> grContext(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)interface.get()));
   
SkASSERT(grContext);


 
// Wrap the frame buffer object attached to the screen in a Skia render target so Skia can
   
// render to it
   
GrGLint buffer;
    GR_GL_GetIntegerv
(interface.get(), GR_GL_FRAMEBUFFER_BINDING, &buffer);
   
GrGLFramebufferInfo info;
    info
.fFBOID = (GrGLuint) buffer;
   
GrBackendRenderTarget target(new_width, new_height, kMsaaSampleCount, kStencilBits, kSkia8888_GrPixelConfig, info);



Reply all
Reply to author
Forward
0 new messages