Search code examples
androidandroid-ndkopengl-es-2.0android-ndk-r7

Reinitialize OpenGL ES 2 window on NativeActivity resume


Solved! See update below.

We are porting a large C++ application to Android, and everything is running fine until it comes to pausing and resuming the application. We listen to the APP_CMD_INIT_WINDOW command, and when it comes we reinitialize all the egl-commands to get a surface, context and display. After this point the screen is black. However, if we set a glClearColor after it works like it should, which means that we at least have the window and it can be drawn to. This leads us to believe that there might be something else that isn't reinitialized.

My question is if there are buffers, shader programs or other cached stuff that needs to be cleared and reloaded after resume (or before pause), and suitable ways of testing this. We suspect that it might have to do with our application referencing to old buffers using the old EGL Context, but we have no way of knowing. The application uses plenty of textures and off-screen buffers.


UPDATE: As usual, after a week of trying out different things we finally solve it just after creating the post. Here is the solution:

All cached textures, buffers and shaders are connected to the context handle created by EGL. There are several things initialized to create the GL-context, the window surface being one of them. This is the only one that depends on the ANativeWindow object which is destroyed and recreated when pausing and resuming the application. Thus, this is the only one that needs to be recreated.

In short:

Run the eglCreateWindowSurface again after the resume with the new window object as argument.


Solution

  • All cached textures, buffers and shaders are connected to the context handle created by EGL. There are several things initialized to create the GL-context, the window surface being one of them. This is the only one that depends on the ANativeWindow object which is destroyed and recreated when pausing and resuming the application. Thus, this is the only one that needs to be recreated.

    In short:

    Run the eglCreateWindowSurface again after the resume with the new window object as argument.