androidc++android-ndkjava-native-interfaceegl

Getting a ANativeWindowBuffer from ANativeWindow_Buffer


To get fast OpenGL ES 2.0 texture pixel access on Android NDK, I want to use the eglCreateImageKHR() extension.

According to the EGL_NATIVE_BUFFER_ANDROID docs:

This extension enables using an Android window buffer (struct ANativeWindowBuffer) as an EGLImage source.

ANativeWindowBuffer is an internal struct used by the native framework classes like GraphicBuffer. Unfortunately, since I am on NDK I do not have direct access to these classes.

The NDK native_window interface allows me to pass a Java Surface object through to the NDK. I can then use ANativeWindow_fromSurface() to get an opaque ANativeWindow* handle. With this pointer I can call ANativeWindow_lock() to fill a struct of type ANativeWindow_Buffer (Note the _).

If I try to use this &ANativeWindow_Buffer object with eglCreateImageKHR() it fails with EGL_BAD_NATIVE_WINDOW.

My question is: How can I use ANativeWindow_Buffer with eglCreateImageKHR() or alternatively how to get an ANativeWindowBuffer from ANativeWindow_Buffer or from ANativeWindow*.


Solution

  • From what I figured out while going down this road, ANativeWindow_Buffer and ANativeWindowBuffer are entirely different types. Well, they are somewhat similar, but definitely so different that they can't be used interchangeably.

    If you want to compare, here are the definitions:

    You will notice that they have a few fields in common (width, height, stride, format). The big difference is that ANativeWindow_Buffer contains a pointer to the actual data, while ANativeWindowBuffer contains an opaque handle of type buffer_handle_t.

    So if you found out how to get a ANativeWindow_Buffer, and were hoping that you were well on your way to a ANativeWindowBuffer, you're... probably not. At least that was my conclusion. I think the very similar names are just a tease.

    I did not find a way to create an ANativeWindowBuffer from NDK code. At least with using only supported APIs, I believe it's not possible. My research was with KitKat.