Android: preserve EGLContext across screen rotation

Viewed 550

I'm writing an Android OpenGL ES application that potentially will have many resources, textures, shaders etc. to load. Normally, the EGLContext is trashed when the view is destroyed, such as when the screen orientation changes. Seeing as it can take a substantial amount of time to load all the necessary resources into OpenGL, compile the shaders, generate mipmaps, etc., I'm looking for a way to preserve the EGL context across screen orientation changes so that this does not need to be done every time the device is rotated.

Further info: I am using GLSurfaceView. I tried implementing a custom EGLContextFactory which creates an EGLContext and then returns the same context to the new GLSurfaceView every time. However, when the device rotates, an error of EGL_BAD_CONTEXT is thrown, presumably because the old view no longer exists or because the new view does not match the one the EGLContext was created to draw on.

There must be some way to save the context. Unfortunately, the EGLContext API is very minimal and does not allow you to modify the context once created. Is there a way, for example, to share the surface which the context is drawing on across multiple views?

1 Answers
Related