Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

opengl es - what is wrong when i use Eglimage replace glreadpixels in NDK program?

//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
", 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
", 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
", 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!
");
        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
", 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();

i do not know what is wrong with it , the memory of vaddr is wrong data,it is show nothing on the screen. but i use glTexImage2D and glReadPixels the result is right

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need a glFinish() call to tell the driver to do the actual drawing. The glReadPixels() call is causing things to work because it forces the rendering to happen -- you've told the driver that you want to read the pixels back, so it pauses until rendering is complete.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...