Skip to content

Instantly share code, notes, and snippets.

@rexguo
Created September 25, 2013 07:11
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save rexguo/6696123 to your computer and use it in GitHub Desktop.
Save rexguo/6696123 to your computer and use it in GitHub Desktop.
1. The texture target needs to be GLES20.GL_TEXTURE_EXTERNAL_OES instead of GL_TEXTURE_2D, e.g. in the glBindTexture calls and glTexParameteri calls.
2. In the fragment shader define a requirement to use the extension:
#extension GL_OES_EGL_image_external : require
3. For the texture sampler used in the fragment shader, use samplerExternalOES instead of sampler2D.
Everything below here is all in the C code, no more Java.
4. In the C code, use glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, eglImage) to specify where the data is, instead of using glTexImage2D family of functions.
5. Now, this is android specific, as GraphicBuffer.h is defined in the android native source code. new a GraphicBuffer object, and init with with the width, height, pixel format, etc... this is where we'll be writing the pixels to. Also, the android's GraphicBuffer object is the one that will allocate the memory for us i.e. call gralloc.
6. To write pixels to the GraphicBuffer, lock it via graphicBuffer->lock(GRALLOC_USAGE_SW_WRITE_RARELY, (void **) &pixels), lock() will give you the address to write the pixels to in the 2nd parameter. Once you have the address, now, you can freely write the data to the address pixels.
7. After you finish writing, unlock it, graphicBuffer->unlock().
8. Now, you need the eglImage object to pass into glEGLImageTargetTexture2DOES in step 4. To create the eglImage using createEGLImageKHR(). http://www.khronos.org/registry/egl/extensions/KHR/EGL_KHR_image_base.txt. 4th parameter to eglCreateImageKHR() takes in a EGLClientBuffer, use this (EGLClientBuffer) graphicBuffer->getNativeBuffer();
9. To clean up, use eglDestroyImageKHR().
I think that's about it. Everything is public API: glEGLImageTargetTexture2DOES(), eglCreateEGLImageKHR(), eglDestroyImageKHR(). gralloc is used, and the implementation of GraphicsBuffer object in the android native source code does that for us.
@sinojelly
Copy link

Good

@Noxalus
Copy link

Noxalus commented May 31, 2017

Thank you so much 👍

@geoabensur
Copy link

Thanks! Is there some sample code for this solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment