Friday, May 25, 2018

Workaround to losing the OpenGL context when Android pauses?


The Android documentation says:



There are situations where the EGL rendering context will be lost. This typically happens when device wakes up after going to sleep. When the EGL context is lost, all OpenGL resources (such as textures) that are associated with that context will be automatically deleted. In order to keep rendering correctly, a renderer must recreate any lost resources that it still needs. The onSurfaceCreated(GL10, EGLConfig) method is a convenient place to do this.




But having to reload all the textures in the OpenGL context is both a pain and hurts the game experience for the user when reentering the app after a pause. I know that "Angry Birds" somehow avoids this, I'm looking for suggestions on how to accomplish the same?


I'm working with the Android NDK r5 (CrystaX version.) I did find this possible hack to the problem but I'm trying to avoid building an entire custom SDK version.



Answer



Replica Island has a modified version of GLSurfaceView that deals with this issue (and works with earlier Android versions). According to Chris Pruett:



Basically, I hacked up the original GLSurfaceView to solve a very specific problem: I wanted to go to different Activities within my app without throwing all of my OpenGL state away. The major change was to separate the EGLSurface from the EGLContext, and to throw the former away onPause(), but preserve the latter until the context is explicitly lost. The default implementation of GLSurfaceView (which I didn't write, by the way), throws all GL state away when the activity is paused, and calls onSurfaceCreated() when it is resumed. That meant that, when a dialog box popped up in my game, closing it incurred a delay because all the textures had to be reloaded.


You should use the default GLSurfaceView. If you must have the same functionality that mine has, you can look at mine. But doing what I did exposed all sorts of awful driver bugs in some handsets (see the very long comments near the end of that file), and you can avoid all that mess by just using the default one.



Edit: I just realized you already posted the link to a similar hack. I don't think there is any built-in solution prior to honeycomb. Replica Island is a popular game working on many devices and you might find Chris's implementation and comments helpful.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...