Android: When is OpenGL context destroyed?

南楼画角 提交于 2019-12-18 12:57:11

问题


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.


回答1:


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. [...]




回答2:


setPreserveEGLContextOnPause is an option for you, you just have to implement the GlSurfaceView yourself.

See my answer here to a similar question: Prevent onPause from trashing OpenGL Context



来源:https://stackoverflow.com/questions/11067881/android-when-is-opengl-context-destroyed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!