Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: When is OpenGL context destroyed?

On android, the GLSurfaceView documentation says this:

A GLSurfaceView must be notified when the activity is paused and resumed. GLSurfaceView clients are required to call onPause() when the activity pauses and onResume() when the activity resumes. These calls allow GLSurfaceView to pause and resume the rendering thread, and also allow GLSurfaceView to release and recreate the OpenGL display.

So I'm supposed to do something like this in my activity:

public void onPause() {
    myGlSurfaceView.onPause();
}

public void onResume() {
    myGlSurfaceView.onResume();
}

I'm observing in my code that if I don't call onPause() and onResume() then the context is not lost when I press the home button, so I can switch between applications and then go back to my game and everything is working. What I see is that if I close the game using the back button then the screen is black when I open it again, but I can change the back button behaviour to totally close the game and avoid this problem.

So my question is: when is the OpenGL context destroyed? If I don't call onPause() and onResume() can I assume that it will never be destroyed?

EDIT:

I'm targeting Android 2.2, so setPreserveEGLContextOnPause() is not an option to me.

like image 233
Damian Avatar asked Jun 16 '12 23:06

Damian


1 Answers

The OpenGL might be lost only after Actvity::onPause() is called, and only in this case. See the setPreserveEGLContextOnPause documentation :

Whether the EGL context is actually preserved or not depends upon whether the Android device that the program is running on can support an arbitrary number of EGL contexts or not. Devices that can only support a limited number of EGL contexts must release the EGL context in order to allow multiple applications to share the GPU. [...] the EGL context [can be] released when the GLSurfaceView is paused, and recreated when the GLSurfaceView is resumed.

EDIT : The situation described in the documentation is valid on all Android version. Not matter you have access to setPreserveEGLContextOnPause

In my opinion, this is one major drawback is Android OGLES implementation : you can't be certain.

The documentation itself is vague (EGL Context Lost note) :

There are situations where the EGL rendering context will be lost. This typically happens when device wakes up after going to sleep

I noticed the same behavior as you about the Home and Back button. Calls are not exactly the sames (but can't remember them precisely).

The only way to be sure that the OpenGL context is available is to create all OpenGL resources in onSurfaceCreated

Note about setPreserveEGLContextOnPause. Once again, this documentation comment demonstrates the "random" behavior of context destruction :

If set to true, then the EGL context may be preserved when the GLSurfaceView is paused. [...]

like image 110
rockeye Avatar answered Sep 18 '22 19:09

rockeye