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

6 views
Skip to first unread message

xiaoming wang

unread,
Jul 20, 2024, 11:01:59 AM (2 days ago) Jul 20
to skia-discuss

below is my snip code, when I dump the skia rendering result to '/Users/wxm/t.rgba'(the below picture), the result is ok, but when draw the texture to the screen, shows black, anyone can help me, I am confused for a few days, thanks very much.

11.jpg

const GLuint WIDTH = 800, HEIGHT = 600;


const char* vertexShaderSource = R"glsl(

    #version 330 core

    layout (location = 0) in vec3 position;

    layout (location = 1) in vec3 color;

    layout (location = 2) in vec2 texCoord;


    out vec3 ourColor;

    out vec2 TexCoord;


    void main()

    {

        gl_Position = vec4(position, 1.0f);

        ourColor = color;

        TexCoord = texCoord;

    }

)glsl";


const char* fragmentShaderSource = R"glsl(

    #version 330 core

    in vec3 ourColor;

    in vec2 TexCoord;


    out vec4 color;

    uniform sampler2D ourTexture1;


    void main()

    {

        color = texture(ourTexture1, TexCoord);

    }

)glsl";


GLuint VBO, VAO, EBO;

int init_vao() {

    // Set up vertex data (and buffer(s)) and attribute pointers

    GLfloat vertices[] = {

            // Positions          // Colors           // Texture Coords

            0.5f0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // Top Right

            0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // Bottom Right

            -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f, // Bottom Left

            -0.5f0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f  // Top Left

    };

    GLuint indices[] = {  // Note that we start from 0!

            0, 1, 3, // First Triangle

            1, 2, 3  // Second Triangle

    };

    glGenVertexArrays(1, &VAO);

    glGenBuffers(1, &VBO);

    glGenBuffers(1, &EBO);


    glBindVertexArray(VAO);


    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);


    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);


    // Position attribute

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);

    glEnableVertexAttribArray(0);

    // Color attribute

    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));

    glEnableVertexAttribArray(1);

    // TexCoord attribute

    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));

    glEnableVertexAttribArray(2);

    glBindVertexArray(0); // Unbind VAO

    return 0;

}


int main() {

    glfwInit();

    // Set all the required options for GLFW

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X

    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);

    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {

        CILOGE("Failed to initialize GLAD");

        return 0;

    }

    glfwSetKeyCallback(window, key_callback);


    Shader ourShader;

    ourShader.Init(vertexShaderSource, fragmentShaderSource);

    init_vao();


    // 创建 Skia 的 OpenGL 上下文

    auto skiaInterface = GrGLMakeNativeInterface();

    GrContextOptions options;

    options.fDisableDistanceFieldPaths = true;

    auto skiaContext = GrDirectContexts::MakeGL(std::move(skiaInterface), options);


    GLuint textureID;

    glGenTextures(1, &textureID);

    glBindTexture(GL_TEXTURE_2D, textureID);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


    while (!glfwWindowShouldClose(window)) {

        glfwPollEvents();


        // 使用 Skia 渲染

        skiaContext->resetContext();

        SkImageInfo info = SkImageInfo::MakeN32Premul(800, 600);


        auto surface = SkSurfaces::RenderTarget(skiaContext.get(),

                                            skgpu::Budgeted::kNo,

                                            info,

                                            0,

                                            GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, nullptr);

        auto canvas = surface->getCanvas();

        canvas->clear(SK_ColorWHITE);

        SkPaint paint;

        paint.setColor(SK_ColorBLUE);

        canvas->drawCircle(400, 300, 100, paint);

//        surface->flushAndSubmit();

        skiaContext->flushAndSubmit();//(surface.get());

        GrBackendTexture backendTexture =

                SkSurfaces::GetBackendTexture(surface.get(), SkSurfaces::BackendHandleAccess::kFlushRead);

        GrGLTextureInfo textureInfo;

        if (GrBackendTextures::GetGLTextureInfo(backendTexture, &textureInfo)) {

            glBindTexture(GL_TEXTURE_2D, textureInfo.fID);

        }


        if (1) {

            glBindTexture(GL_TEXTURE_2D, textureInfo.fID);

            uint8_t* data = new uint8_t[800 * 600 * 4];

            glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

            FILE* wb = fopen("/Users/wxm/t.rgba", "wb");

            fwrite(data, 1, 800 * 600 * 4, wb);

            fclose(wb);

        }

        

        ourShader.use();

        glViewport(0, 0, WIDTH, HEIGHT);

        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

        glClear(GL_COLOR_BUFFER_BIT);

        glActiveTexture(GL_TEXTURE0);

        glBindTexture(GL_TEXTURE_2D, textureInfo.fID);

        ourShader.setInt("ourTexture1", 0);

        glBindVertexArray(VAO);

        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

        glBindVertexArray(0);

        glfwSwapBuffers(window);

    }


    glfwDestroyWindow(window);

    glDeleteVertexArrays(1, &VAO);

    glDeleteBuffers(1, &VBO);

    glDeleteBuffers(1, &EBO);

    glfwTerminate();

    return 0;

}

Reply all
Reply to author
Forward
0 new messages