当我使用Eglimage替换NDK程序中的glreadpixels时出了什么问题?

时间:2014-08-29 08:12:34

标签: opengl-es android-ndk opengl-es-2.0 egl

//step 2. Create the Android Graphic Buffer
GraphicBuffer* buffer = new GraphicBuffer(w, h,
   HAL_PIXEL_FORMAT_RGBA_8888,
   GraphicBuffer::USAGE_HW_TEXTURE |
   GraphicBuffer::USAGE_HW_2D |
   GRALLOC_USAGE_SW_READ_OFTEN |
   GRALLOC_USAGE_SW_WRITE_OFTEN);
// Init the buffer
status_t err = buffer->initCheck();
if (err != NO_ERROR)
{
  LOGE("Error: %s\n", strerror(-err));
  return ;
}

// Retrieve andorid native buffer
android_native_buffer_t* anb = buffer->getNativeBuffer();

//step 3. Create the EGLImage
const EGLint attrs[] = {
   EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
   EGL_NONE, EGL_NONE
};
EGLImageKHR pEGLImage = _eglCreateImageKHR(eglGetCurrentDisplay(), EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, (EGLClientBuffer)anb, attrs);
if (pEGLImage == EGL_NO_IMAGE_KHR) {
  EGLint error = eglGetError();
  LOGE("Error (%#x): Creating EGLImageKHR at %s:%i\n", error, __FILE__, __LINE__);
}

// Create GL texture, bind to GL_TEXTURE_2D, etc.
GLuint texture, renderBuffer, frameBuffer;
    printGLString("Version", GL_VERSION);
    printGLString("Vendor", GL_VENDOR);
    printGLString("Renderer", GL_RENDERER);

    //lglActiveTexture(GL_TEXTURE0);
    glGenTextures(1, &texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    LOG_INFO("Input Image Texture id %d\n", texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

// Attach the EGLImage to whatever texture is bound to GL_TEXTURE_2D
    _glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, pEGLImage);
    LOGE("glEGLImageTargetTexture2DOES");



    glGenFramebuffers(1, &frameBuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,texture, 0);
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
        glDeleteTextures(1, &texture);
        glDeleteFramebuffers(1, &frameBuffer);
        glDeleteRenderbuffers(1, &renderBuffer);
        LOG_ERROR("Image Handler initImageFBO failed!\n");
        return;
    }
    glViewport(0, 0, w, h);

    ////////////////////////////////////
    GLuint vsh, fsh, program;
    vsh = glCreateShader(GL_VERTEX_SHADER);
    fsh = glCreateShader(GL_FRAGMENT_SHADER);
    program = glCreateProgram();
    glShaderSource(vsh, 1, (const GLchar**)&g_defaultVertexShaderString, NULL);
    glShaderSource(fsh, 1, (const GLchar**)&g_defaultFragmentShaderString, NULL);
    glCompileShader(vsh);
    glCompileShader(fsh);
    glAttachShader(program, vsh);
    glAttachShader(program, fsh);
    glLinkProgram(program);
    glDeleteShader(vsh);
    glDeleteShader(fsh);
    glUseProgram(program);
    GLuint vPosition = glGetAttribLocation(program, "vPosition");
    checkGLError("glGetAttribLocation");
    LOG_INFO("glGetAttribLocation(\"vPosition\") = %d\n", vPosition);
    glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, g_vertices);
    glEnableVertexAttribArray(vPosition);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);


//glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, row);



void* vaddr = NULL;
buffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &vaddr);
if(vaddr)
{
    memcpy(row, vaddr, w * h * 4);
    LOGE("copy memory");
}
buffer->unlock();

我不知道它有什么问题,vaddr的内存是错误的数据,它在屏幕上什么也没显示。 但我使用glTexImage2D和glReadPixels,结果是正确的

1 个答案:

答案 0 :(得分:1)

您需要glFinish()来电告诉司机进行实际绘图。 glReadPixels()调用导致事情起作用,因为它会强制渲染发生 - 你告诉驱动程序你想要读回像素,所以它会暂停直到渲染完成。