Losing OpenGL Textures in Android after a resume
Asked Answered
S

3

7

My game is working correctly except in the case where I press the HOME button then resume. 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).

Any ideas what I could be doing wrong?

Squab answered 11/5, 2011 at 14:28 Comment(2)
Could you plz post some code samples?Marni
@Marni I'm not sure what code is relevant. My Activity / View and Renderer? I am loading the textures using native code. What needs to be done to restore the textures? Do I need to reload from disk?Squab
B
6

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);
Bettencourt answered 11/5, 2011 at 14:42 Comment(2)
I had tried this, but for some reason the textureIds seemed to be mixed up (my font texture was applied in the wrong place for example). I will persevere and give it another goSquab
Thanks, got it working. Lack of sleep from a new baby caused a couple of mistakes. With a clear head it works well :)Squab
O
5

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.

You have to recreate every OpenGL-resources on resume (texture in you case, but also VBOs etc etc).

Since API 11, you can ask kindly Android to keep the context, but there is no guaranty it would.

Otherness answered 7/2, 2012 at 11:12 Comment(4)
I did reload the textures to fix the problem. I just had made a mistake in doing that :)Squab
How do you find out whether OpenGL has in fact preserved its context to save reloading unnecessarily?Moustache
Just found out about getPreserveEGLContextOnPause(). If this returns true, are you GUARANTEED to keep context?Moustache
I wish I had known about the existence of setPreserveEGLContextOnPause three years ago. Would have prevented plenty of rating hits on my game from players having to wait for intermittent texture loads during play, like after ad calls. Incredible the default on this is false.Casting
G
2

After trying:

  1. do not call GLSurfaceView#onPause/onResume in Activity's onPause/onResume
  2. call GLSurfaceView#onPause/onResume, but also set GLSurfaceView#setPreserveEGLContextOnPause(true)

Both of cases fix the HOME-resume-black-texture issue. Guess Android implementation failed to re-create the EGL context when resume. Since onPause/onResume are required to call, should always set setPreserveEGLContextOnPause to true.

Gaussmeter answered 8/7, 2015 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.