Android resets OpenGL context after reopening

Quick question. I’m creating an OpenGL context with SDL 2.0.14. Everything’s working on the Android Studio emulator running Android 11, but when I run this engine on my Pixel 3 (also Android 11), after I go home or switch to another application, when I reenter my application, SDL always sends SDL_RENDER_DEVICE_RESET. I’ve checked Android’s developer settings and I don’t see anything there that would force SDL to reset the OpenGL context. Am I doing something wrong? Again, the emulator doesn’t have this problem at all.

I’m ok with uploading everything back to the GPU every time, but I wanted to check first if anybody else encountered this or has suggestions to avoid Android resetting the OpenGL context.

Thanks!

1 Like

In principle setPreserveEGLContextOnPause(true) is supposed to solve this problem, but it’s a GLSurfaceView method and SDL2 doesn’t use GLSurfaceView, I have no idea if it’s possible to somehow incorporate this in SDLActivity.java but it would be great if it is! There is some possibly relevant information here.

Reading that leaves me puzzled as to why SDL2 uses a SurfaceView rather than a GLSurfaceView because it implies the latter makes OpenGL rendering easier, and that’s surely what SDL2 wants?

Yeah, I read that after you posted it. Thanks for the link. I tried naively switching from SurfaceView to GLSurfaceView but it didn’t render anything to the screen, and then it’s still invalidating the context after bringing it into the foreground again. It looks like it’d take some work to switch it over to GLSurfaceView, but I can look into it if that’s why my phone keeps resetting the context.

I’ve read this before, but I’m mostly surprised that my phone is doing it every single time the application reenters the foreground, especially when the emulator doesn’t do that. It looks like, though, that this is (historically) how Android functions.

This answer from that StackExchange thread seems to indicate that this is basically how Android handles OpenGL context. He’s suggesting to use a plain SurfaceView and then handle EGL manually. When I have some time, I might look into that, but even when I was programming enterprise Android applications, I never used SurfaceView or EGL. Still, he’s probably right that SDL should remove the context when it enters the background and then I can just reload the resources.

You may be able to do that, but it isn’t always possible. My app uses a target texture, the contents of which are built up in an arbitrary number of steps over an arbitrary period of time, and there is no way of rebuilding it if it is destroyed.

Switching to a GLSurfaceView, if possible, and calling setPreserveEGLContextOnPause(true) would bring Android into line with all the other SDL2 targets and is the only solution that would work for me.