Documentation: Creating a GPU-backed canvas

441 views
Skip to first unread message

Evan Davis

unread,
Dec 14, 2022, 3:20:05 PM12/14/22
to skia-discuss
The example for creating an OpenGL-backed canvas mentions the function:

GrDirectContext::MakeGL(interface)

But my compiler complains:

no member named 'MakeGL' in 'GrDirectContext'

Evan Davis

unread,
Dec 14, 2022, 3:39:27 PM12/14/22
to skia-discuss
I found this function mentioned in the GrDirectCanvas.h file, and I also found "GrContext::MakeGL()" mentioned in the comments of GrGLInterface.h, but I couldn't find either function in the Doxygen documentation.

Is the documentation out of date?  I can't figure out how to get a canvas working with the GPU.

Evan Davis

unread,
Dec 14, 2022, 3:51:06 PM12/14/22
to skia-discuss
Nevermind, the function is defined in "GrDirectContext.h", but I needed to add the line:

#define SK_GL

before including that file.

On Wednesday, December 14, 2022 at 3:20:05 PM UTC-5 Evan Davis wrote:

Evan Davis

unread,
Dec 14, 2022, 10:40:52 PM12/14/22
to skia-discuss
I am trying to use SDL, OpenGL, and Skia to draw and display in a window, But all I get is a black window.  I know that the GPU rendering is working, becuase I am able to save the GPU-backed surface into a PNG file, where I see the correct drawing.

Here's a minimal example of what I'm doing:


void f(SDL_Window *window)
{
    SDL_GLContext sdl_gl_context = SDL_GL_CreateContext(window);
    sk_sp<GrDirectContext> skia_gl_context = GrDirectContext::MakeGL();
   
    int w, h;
    SDL_GetWindowSizeInPixels(window, &w, &h);
    SkImageInfo skia_gl_context_info = SkImageInfo::MakeN32Premul(w, h);
    sk_sp<SkSurface> surface(
        SkSurface::MakeRenderTarget(skia_gl_context.get(),
            SkBudgeted::kNo, skia_gl_context_info));
    SkCanvas *canvas = surface->getCanvas();

    draw(canvas, w, h);
    SDL_GL_SwapWindow(window);

    write_png(surface);

    SDL_GL_DeleteContext(sdl_gl_context);
}


What am I missing?

Brian Salomon

unread,
Dec 15, 2022, 11:49:30 AM12/15/22
to skia-d...@googlegroups.com
On Wed, Dec 14, 2022 at 10:40 PM Evan Davis <erda...@gmail.com> wrote:
I am trying to use SDL, OpenGL, and Skia to draw and display in a window, But all I get is a black window.  I know that the GPU rendering is working, becuase I am able to save the GPU-backed surface into a PNG file, where I see the correct drawing.

Here's a minimal example of what I'm doing:


void f(SDL_Window *window)
{
    SDL_GLContext sdl_gl_context = SDL_GL_CreateContext(window);
    sk_sp<GrDirectContext> skia_gl_context = GrDirectContext::MakeGL();
   
    int w, h;
    SDL_GetWindowSizeInPixels(window, &w, &h);
    SkImageInfo skia_gl_context_info = SkImageInfo::MakeN32Premul(w, h);
    sk_sp<SkSurface> surface(
        SkSurface::MakeRenderTarget(skia_gl_context.get(),
            SkBudgeted::kNo, skia_gl_context_info));
 ^^^ This makes a new texture backing the SkSurface and all drawing will go to that texture. You want SkSurface::MakeFromBackendRenderTarget() which lets you import OpenGL's default framebuffer into Skia as an SkSurface.
    SkCanvas *canvas = surface->getCanvas();

    draw(canvas, w, h);
    SDL_GL_SwapWindow(window);

    write_png(surface);

    SDL_GL_DeleteContext(sdl_gl_context);
}


What am I missing?

On Wednesday, December 14, 2022 at 3:51:06 PM UTC-5 Evan Davis wrote:
Nevermind, the function is defined in "GrDirectContext.h", but I needed to add the line:

#define SK_GL

before including that file.

On Wednesday, December 14, 2022 at 3:20:05 PM UTC-5 Evan Davis wrote:
The example for creating an OpenGL-backed canvas mentions the function:

GrDirectContext::MakeGL(interface)

But my compiler complains:

no member named 'MakeGL' in 'GrDirectContext'

--
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/4a97ce2e-4c5a-4018-b9c4-5541ea28a05fn%40googlegroups.com.


--

Brian Salomon | Office Hours: go/bsalomon-office | bsal...@google.com

Evan Davis

unread,
Dec 17, 2022, 9:55:11 AM12/17/22
to skia-discuss
Thanks!

Now I just need to figure out how to get GrRecordingContext and GrBackendRenderTarget from the GL context created by SDL..




Brian Salomon

unread,
Dec 18, 2022, 7:45:01 PM12/18/22
to skia-d...@googlegroups.com
GrRecordingContext is a base class of GrDirectContext so you already have that. The values you use to make the GrBackendRenderTarget should reflect how you set up the backing store for the window (the pixel format, the dimensions, number of stencil buffer bits, sample count) and use framebuffer ID 0, which is OpenGL's default framebuffer.

On Sat, Dec 17, 2022 at 9:55 AM Evan Davis <erda...@gmail.com> wrote:
Thanks!

Now I just need to figure out how to get GrRecordingContext and GrBackendRenderTarget from the GL context created by SDL..




--
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.

Evan Davis

unread,
Dec 18, 2022, 9:44:04 PM12/18/22
to skia-discuss
Thanks again!

Evan Davis

unread,
Dec 19, 2022, 10:02:32 AM12/19/22
to skia-discuss
I am having trouble finding documentation on this, but looking at the header files I found that the constructor for "GrBackendRenderTarget" takes "GrGLFramebufferInfo", which is a struct defined in the file "include/gpu/GrGLTypes.h".  It has a member "GrGLenum fFormat", which I assume is the place to set the pixel format, but where are its possible values defined or documented?

Evan Davis

unread,
Dec 21, 2022, 8:50:30 AM12/21/22
to skia-discuss
Answer: the "GrGLenum fFormat" appears to be an enum defined by OpenGL, as described on this page:

Brian Osman

unread,
Dec 21, 2022, 9:45:49 AM12/21/22
to skia-d...@googlegroups.com
Yes, precisely. It's defined by your system's GL headers (gl.h).

Evan Davis

unread,
Dec 21, 2022, 1:53:53 PM12/21/22
to skia-discuss
Thanks again for your help!

Happy Holidays.

Everdrone

unread,
Mar 22, 2023, 5:57:23 PM3/22/23
to skia-discuss
For some reason I'm getting the same error, even with #define SK_GL:

This is the beginning of my code:

#define SK_GL

#include <GLFW/glfw3.h>
#include "include/gpu/GrBackendSurface.h"
#include "include/gpu/GrDirectContext.h"

Reply all
Reply to author
Forward
0 new messages