简体   繁体   English

简历后在Android中丢失OpenGL纹理

[英]Losing OpenGL Textures in Android after a resume

My game is working correctly except in the case where I press the HOME button then resume. 我的游戏工作正常,除非我按HOME按钮然后恢复。 What needs to be done to use the textures again? 需要做什么才能再次使用纹理? I have tried calling onPause and onResume on the GLSurfaceView (when the activity's onPause and onResume are called). 我试过在GLSurfaceView上调用onPause和onResume(当调用activity的onPause和onResume时)。

Any ideas what I could be doing wrong? 我有什么想法可能做错了吗?

If all else fails, reload the textures: 如果所有其他方法都失败,请重新加载纹理:

Pseudocode 伪代码

for tex in textures:
    if glIsTexture(tex.opengl_name) == false:
        glGenTextures(1, &tex.opengl_name)

    glBindTexture(tex.texture_target);
    glTexImage(..., texture.image);

Even if you fixed your problem, just to give a bit of explanation that might help others. 即使你解决了问题,也只是给出一些可能对其他人有帮助的解释。

Android does not guaranty to keep the OpenGL context alive when the activity is paused. 当活动暂停时,Android无法保证OpenGL上下文保持活动状态。

You have to recreate every OpenGL-resources on resume (texture in you case, but also VBOs etc etc). 你必须在简历上重新创建每个OpenGL资源(在你的情况下是纹理,还有VBO等)。

Since API 11, you can ask kindly Android to keep the context, but there is no guaranty it would. 从API 11开始,你可以请求 Android保持上下文,但它没有保证。

After trying: 尝试后:

  1. do not call GLSurfaceView#onPause/onResume in Activity's onPause/onResume 不要在Activity的onPause / onResume中调用GLSurfaceView#onPause / onResume
  2. call GLSurfaceView#onPause/onResume, but also set GLSurfaceView#setPreserveEGLContextOnPause(true) 调用GLSurfaceView#onPause / onResume,还可以设置GLSurfaceView#setPreserveEGLContextOnPause(true)

Both of cases fix the HOME-resume-black-texture issue. 这两种情况都解决了HOME-resume-black-texture问题。 Guess Android implementation failed to re-create the EGL context when resume. 猜测Android实现无法在恢复时重新创建EGL上下文。 Since onPause/onResume are required to call, should always set setPreserveEGLContextOnPause to true. 由于onPause / onResume需要调用,因此应始终将setPreserveEGLContextOnPause设置为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM