show black when draw the skia rendering result in outside opengl environment

44 Views Asked by At

I'm trying to draw the skia result into an FBO instead of directly drawing into the window frame buffer.
steps like below:
(1) create texture with fbo
(2) create SkSurface with the created fbo using SkSurfaces::WrapBackendRenderTarget
(3) draw using canvas in skia
(4) draw the created texture(in step 2) in outside opengl environment to the screen. but show black

after step 3, I dump the createed texture. the texture has the right content

the snap code is below, anyone can figure out the problem? thanks. I've been stuck for a few days

//initialize in render thread:
glfwMakeContextCurrent(native_window_);
int width = 800, height = 600;
sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface();
GrDirectContext* grDirectContext_ = GrDirectContexts::MakeGL(GrGLMakeNativeInterface(), GrContextOptions()).release();
shared_ptr<TextureData> g_texture = CITextureUtils::createTextureWithFBO(CIGLContext::getInstance(), width, height);
sk_sp<SkSurface> g_surface = SkSurfaces::WrapBackendRenderTarget(
        grDirectContext_,
        GrBackendRenderTargets::MakeGL(width,
                                        height,
                                        1,
                                        8,
                                        GrGLFramebufferInfo{(GrGLuint)g_texture->fbo, GL_RGBA8}),
        kBottomLeft_GrSurfaceOrigin,
        kRGBA_8888_SkColorType,
        nullptr,
        nullptr);

//the draw loop in render thread:

SkCanvas *canvas = g_surface->getCanvas();
canvas->clear(kClearColor);
                canvas->save();
                canvas->translate(SkIntToScalar(128), SkIntToScalar(128));
                canvas->rotate(SkIntToScalar(45));
                SkRect rect = SkRect::MakeXYWH(-90.5f, -90.5f, 181.0f, 181.0f);
                SkPaint paint;
                paint.setColor(SK_ColorBLUE);
                canvas->drawRect(rect, paint);
                canvas->restore();
grDirectContext_->flush();
grDirectContext_->resetContext();
// here dump the created texture, the texture has right context
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &lOldFbo);
glBindFramebuffer(GL_FRAMEBUFFER, texture->fbo);
glReadPixels(0, 0, bitmap->width, bitmap->height, GL_RGBA, GL_UNSIGNED_BYTE, bitmap->plane[0]);
glBindFramebuffer(GL_FRAMEBUFFER, lOldFbo);

// draw in outside opengl environment in render thread:

glViewport(0, 0, width, height);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, g_texture->textureID);
GLint currProgram;
glGetIntegerv(GL_CURRENT_PROGRAM, &currProgram);
shader_.use();
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

glfwSwapBuffers((GLFWwindow*)window_);

I dump the created texture after the skia rendering, the texture has right content. but when rendering to the screen in outside opengl environment show black.

0

There are 0 best solutions below