Search code examples
androidopengl-esopengl-es-2.0egl

OpenGL textures into AHardwareBuffer


I came across AHardwareBuffer in Android. I wanted to make use of AHardwareBuffer to store textures so that I can use them on different threads where I don't have an OpenGL context. Currently, I'm doing the following:

  • Generate a texture and bind it to GL_TEXTURE_2D.
  • Create EGLClientBuffer and EGLImageKHR from it. Attach the EGLImage as texture target.
  • Generate an FBO and bind it to the texture using glFramebufferTexture2D.
  • To draw the texture (say tex), I'm rendering it onto the AHardwareBuffer using shaders

However, I wanted away so that I don't need to rerender it onto hardwarebuffer but instead directly store data of the texture onto hardwarebuffer.

I was thinking of using glCopyTexImage2d for doing this. Is this fine and would it work?

Also (a dumb question but I cannot get over it) if I attach my EGLImage which is from the Hardwarebuffer to GL_TEXTURE_2D and define the texture using glTexImage2D, would it not store the data of the texture which is a parameter of glTexImage2D into the hardwarebuffer?


Solution

  • I solved this issue using glSubTexImage2D.

    First create a opengl texture and bind it to GL_TEXTURE_2D. Then use glEGLImageTargetTexture2DOES to bind texture to EGLImageKHR created from EGLClientBuffer. This is similar to glTexImage2D. Any subsequent call to glTexImage2D will break the relationship between the texture and EGLClientBuffer.

    refer: https://www.khronos.org/registry/OpenGL/extensions/OES/OES_EGL_image_external.txt

    However glSubTexImage2D preserves the relationship. Hence we can load data with this API and store it in AHardwareBuffer.

    PS: This might me one way and if there are other ways i'll be accepting the answer.