How to Save and Restore OpenGL Context OnPause

Skip to first unread message


Oct 8, 2021, 7:51:14 PM10/8/21
to android-ndk
I have two fairly high profile gaming Apps that I ported from iOS to Android with the Marmalade SDK so I could reuse my C++ and OpenGL 1.1 code. Unfortunately, Marmalade went down the toilet several years ago and I'm now in the process of moving these two games over to Android Studio using NDK. I used Google's "native-activity" example from their "native-samples" as a starting point and everything is going surprisingly smoothly. However, whenever put my app into the background, the OpenGL context gets destroyed and when the app is brought back into the foreground, all my textures have been destroyed too. You might be thinking that it would be a simple task for me to just re-create all the textures but my code is all over the place and creates textures in various places and it would also be fairly slow to load each asset and convert it to a texture every time the device changed orientation or was brought to the foreground. It looks like Marmalade wrote their own code to save each texture to the device OnPause and recreate it OnResume. That seems like a decent chunk of work so my first question is, does anyone know if there's some code already written somewhere that I can use to save the entire state of the context ? I don't need to recreate the exact state of OpenGL when the app is resumed, just need to recreate all the textures. In the meantime, I've modified the native-activity code so it doesn't destroy the context in the  "engine_term_display" function and then it's re-used when the app resumes. My second question is whether this is bad programming practice ? I feel like it is because I'm guessing all my textures would linger in VRAM and affect any other apps that are opened while mine is in the background. However, by some miracle, OpenGL or Android OS might be written in such a way that when VRAM runs out for another app, my textures would automatically be saved to memory or SD card if another App ran out of VRAM and restored when my app resumes. Is that wishful thinking ? Thanks !
Reply all
Reply to author
0 new messages