How to use GraphicBuffer in android ndk

Kevin K picture Kevin K · Apr 24, 2014 · Viewed 10.9k times · Source

I am asking this with reference to an answer for my question at How to improve opengl es display performance in android . I was trying to build the code which uses GraphicBuffer with ndk-r9d. but It is saying GraphicBuffer is not declared in this scope. The same comments for eglCreateImageKHR and glEGLImageTargetTexture2DOES.

I have added EGL/eglext.h and GLES2/gl2ext.h . I tried to include ui/GraphicBuffer.h but it is not taking it. Is there another header file to be added ?

The code given below I have added to avoid use of glTexSubImage2D().

  GraphicBuffer * pGraphicBuffer = new GraphicBuffer(frame_width, frame_height, PIXEL_FORMAT_RGB_565, GraphicBuffer::USAGE_SW_WRITE_OFTEN | GraphicBuffer::USAGE_HW_TEXTURE);

        // Lock the buffer to get a pointer
        unsigned char * pBitmap = NULL;
        pGraphicBuffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN,(void **)&pBitmap);

        // Write 2D image to pBitmap
        memcpy(pBitmap, frame_buffer, frame_width * frame_height * 3);

        // Unlock to allow OpenGL ES to use it
        pGraphicBuffer->unlock();

        EGLClientBuffer ClientBufferAddress = pGraphicBuffer->getNativeBuffer();
        EGLint SurfaceType = EGL_NATIVE_BUFFER_ANDROID;

        // Make an EGL Image at the same address of the native client buffer
        EGLDisplay eglDisplayHandle = eglGetDisplay(EGL_DEFAULT_DISPLAY);

        // Create an EGL Image with these attributes
        EGLint eglImageAttributes[] = {EGL_WIDTH, frame_width, EGL_HEIGHT, frame_height, EGL_MATCH_FORMAT_KHR,  EGL_FORMAT_RGB_565_KHR, EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE};

        EGLImageKHR eglImageHandle = eglCreateImageKHR(eglDisplayHandle, EGL_NO_CONTEXT, SurfaceType, ClientBufferAddress, eglImageAttributes);

        // Create a texture and bind it to GL_TEXTURE_2D
/*        EGLint TextureHandle;
        glGenTextures(1, &TextureHandle);
        glBindTexture(GL_TEXTURE_2D, TextureHandle);
*/
        // Attach the EGL Image to the same texture
        glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, eglImageHandle);

What can I do to get it run......

Thanks in advance..

Answer

Eric Fu picture Eric Fu · Jun 14, 2016

I am working on this problem these days, too.

Many blogs said a copy of Android source code is needed to link along with your project. I believe it's more elegant to get function from libui.so in runtime, which is the "alternative approach" mentioned by Aleksandar Stojiljkovic.

I have written a simple library to do that. Here is it.