I can't figure out how to render to a new GL texture, or existing texture

694 views
Skip to first unread message

edA-qa mort-ora-y

unread,
Jan 10, 2017, 10:21:34 AM1/10/17
to skia-discuss
I'm trying to integrate Skia into a framework that does it's own GL drawing. For this I need Skia to render into a texture, either one it creates or one provide to it. I've tried both approached and don't seem to get any results.

The first approach is to render to a texture I've already created and bound to the GL context. I then do this with skia:

const GrGLInterface* interface = nullptr;
GrContext* context = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)interface);

GrBackendRenderTargetDesc desc;
desc
.fWidth = 400;
desc
.fHeight = 300;
desc
.fConfig = kSkia8888_GrPixelConfig;
desc
.fOrigin = kBottomLeft_GrSurfaceOrigin;
desc
.fSampleCnt = 1;
desc
.fStencilBits = 0;
desc
.fRenderTargetHandle = 0;
sk_sp
<SkSurface> surface = SkSurface::MakeFromBackendRenderTarget(context, desc, NULL);
SkCanvas* canvas = surface->getCanvas();
canvas
->clear(SK_ColorWHITE);
canvas
->flush();
context
->flush();

I'm assuming this is enough to color the surface white (I've cleared it to green). But it doesn't appear to do anything to the buffer.  (Note, I've also tried drawing the sample star in my code, but it doesn't do anything either).

The second approach is letting Skia create the texture and then get the handle to that.

const GrGLInterface* interface = nullptr;
GrContext* context = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)interface);
SkImageInfo info = SkImageInfo::MakeN32Premul(400,300);
sk_sp
<SkSurface> surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
SkCanvas* canvas = surface->getCanvas();

canvas
->clear(SK_ColorWHITE);
canvas
->flush();
context
->flush();
sk_sp
<SkImage> image = surface->makeImageSnapshot();
GrBackendObject handle = image->getTextureHandle(true);
SkImage* rawImage = image.release();
surface
.release(); //temporary for now, to prevent texture release

return handle;

I then bind the returned handle to GL and copy it to my output buffer. The result is always just black.

What am I doing wrong?  I'd prefer for the first approach to work, to output to my own buffers, but if I can see anything n the second one I'd use that at least for now.

Brian Salomon

unread,
Jan 10, 2017, 12:56:29 PM1/10/17
to skia-d...@googlegroups.com
Your first code snippet would write to FBO 0, typically the window back buffer. Is that what you're trying to do?

I would expect that the second one would work (modulo leaking the surface and image). I'm not sure why it wouldn't. Maybe try compiling with SK_DEBUG defined and see if you see any error spew to stderr. In debug mode we will make glGetError calls after each GL call and print if there is a GL error.


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

edA-qa mort-ora-y

unread,
Jan 11, 2017, 4:41:57 AM1/11/17
to skia-discuss
In the first one I'm hoping it writes to the current framebuffer, as set by `glBindFramebuffer`.

By debug I assume you mean build the library (static in my case) with `--args="is_debug=true"`. With that the console output doesn't show anything -- shoudl there at least be some kind of output?  I tested that `SkDebugf` is outputting something when I write it in my code at least.

In case it's relevant, I built the static library as `gn gen out/StaticDebug --args="is_debug=true skia_use_libpng=false"`.  I removed libpng since there's a symbol conflict with the libpng I'm already linking. Since my test code here doesn't use png I'm assuming it's okay.

Brian Osman

unread,
Jan 11, 2017, 9:19:21 AM1/11/17
to skia-d...@googlegroups.com
In the first example, fRenderTargetHandle needs to be set to the ID of the framebuffer you want to bind to the SkSurface. Assuming that you have your FB bound:

GLint buffer;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &buffer);
desc.fRenderTargetHandle = buffer;


To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss+unsubscribe@googlegroups.com.

edA-qa mort-ora-y

unread,
Jan 12, 2017, 3:20:00 AM1/12/17
to skia-discuss
Changing that parameter doesn't seem to to anything.  I get output now (not sure what I changed), but it's on the "main" drawing surface.  It doesn't go to the `fRenderTargetHandle` I provide, nor does it go to the current GL_FRAMEBUFFER_BINDING. It's like it's jusy bypassing my current GL contex and creating it's own somehow.

I'm on OSX. There are no errors written to the console.

edA-qa mort-ora-y

unread,
Jan 12, 2017, 3:39:26 AM1/12/17
to skia-discuss
I think some of  my IDs may have been wrong, or I skipped some setup line, but I have it drawing to my own texture now. In the below `glBuffer` is my FBO ID where I want to draw.


const GrGLInterface* interface = nullptr;
GrContext* context = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)interface);

GrBackendRenderTargetDesc desc;

desc
.fRenderTargetHandle = glBuffer;            
desc
.fWidth = 400;
desc
.fHeight = 300;
desc
.fConfig = kSkia8888_GrPixelConfig;
desc
.fOrigin = kBottomLeft_GrSurfaceOrigin;
desc
.fSampleCnt = 1;
desc
.fStencilBits = 0;
sk_sp
<SkSurface> surface = SkSurface::MakeFromBackendRenderTarget(context, desc, NULL);
SkCanvas* canvas = surface->getCanvas();

const SkScalar scale = 256.0f;
const SkScalar R = 0.45f * scale;
const SkScalar TAU = 6.2831853f;
SkPath path;
path
.moveTo(R, 0.0f);
for (int i = 1; i < 7; ++i) {
   
SkScalar theta = 3 * i * TAU / 7;
    path
.lineTo(R * cos(theta), R * sin(theta));
}
path
.close();
SkPaint p;
p
.setAntiAlias(true);
p
.setColor(SK_ColorBLUE);
canvas
->clear(0);
canvas
->translate(0.5f * scale, 0.5f * scale);
canvas
->drawPath(path, p);
canvas
->flush();
context
->flush();

Brian Salomon

unread,
Jan 12, 2017, 8:47:03 AM1/12/17
to skia-discuss
Great, glad you got it working!

--
Reply all
Reply to author
Forward
0 new messages