100% NDK version of setPreserveEGLContextOnPause?

891 views
Skip to first unread message

Alan Ide

unread,
Apr 17, 2015, 12:24:41 PM4/17/15
to andro...@googlegroups.com
I currently have a 100% NDK app that was originally targeted for v2.3. That API had no way to preserve an OpenGL context after an onPause or onStop was called, so we simply killed the app completely when either of those actions were performed. Which obviously makes for a pretty crappy user experience, but was far simpler than trying to recreate the entire OpenGL state. I am now in the process of bringing the entire application up to v4.x, and wanted to revisit this issue to see if there was now a proper solution. I see that the SDK has a method called "setPreserveEGLContextOnPause", but since I dont even have a GLSurfaceView in my app, I cant call that. Is there a solution to this issue in 100% NDK environments?

Philippe Simons

unread,
Apr 17, 2015, 5:59:03 PM4/17/15
to android-ndk
just do it manually (btw it was working on v2.3)

when you get the onSurfaceDestroyed event, unbind your EGLSurface and destroy it, but keep your EGLContext.

  eglMakeCurrent(sstDisplay.display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  eglASSERT();
  eglDestroySurface(sstDisplay.display, sstDisplay.surface);
  eglASSERT();


when you get the onSurfaceCreated event, create a new EGLSurface and rebind it to your EGLContext.

  sstDisplay.surface = eglCreateWindowSurface(sstDisplay.display, sstDisplay.config, window, NULL);
  eglASSERT();
  eglMakeCurrent(sstDisplay.display, sstDisplay.surface, sstDisplay.surface, sstDisplay.context);
  eglASSERT();


job done

On Fri, Apr 17, 2015 at 5:33 PM, Alan Ide <al...@digitalsynapses.com> wrote:
I currently have a 100% NDK app that was originally targeted for v2.3. That API had no way to preserve an OpenGL context after an onPause or onStop was called, so we simply killed the app completely when either of those actions were performed. Which obviously makes for a pretty crappy user experience, but was far simpler than trying to recreate the entire OpenGL state. I am now in the process of bringing the entire application up to v4.x, and wanted to revisit this issue to see if there was now a proper solution. I see that the SDK has a method called "setPreserveEGLContextOnPause", but since I dont even have a GLSurfaceView in my app, I cant call that. Is there a solution to this issue in 100% NDK environments?

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-ndk.
For more options, visit https://groups.google.com/d/optout.

Alan Ide

unread,
Apr 21, 2015, 10:43:19 AM4/21/15
to andro...@googlegroups.com
Thank you for pointing me in the right direction, but I seem to be a bit hung up on implementing what you wrote. Currently, I am not aware of anyway to receive the onSurface events, since this is a 100% NDK app. I probably should have mentioned in my original post that the "onPause/onStop" methods I am using are actually being created from the onAppCmd method with the APP_CMD_PAUSE & APP_CMD_STOP keys.  I was not able to find any APP_CMD_ keys for surface changes. However, what you suggested makes perfect sense, so I attempted to implement it in the APP_CMD_PAUSE & APP_CMD_RESUME keys, but that didnt work very well. Suggestions?

Philippe Simons

unread,
Apr 21, 2015, 2:59:39 PM4/21/15
to android-ndk
onSurfaceDestroyed and onSurfaceCreated match with APP_CMD_TERM_WINDOW and APP_CMD_INIT_WINDOW on NativeActivity.

Philippe

Hak Matsuda

unread,
Apr 21, 2015, 3:44:52 PM4/21/15
to andro...@googlegroups.com
EGLContext can be lost in some circumstances (e.g. max # of context reached while your app is in background).
So you need to check results of EGL calls and re-create context when it's lost.
I wrote small helper functions to do that.

thanks,
hak

Alan Ide

unread,
Apr 21, 2015, 6:02:06 PM4/21/15
to andro...@googlegroups.com
Thank you sir, that works!

Alan Ide

unread,
Apr 21, 2015, 6:02:06 PM4/21/15
to andro...@googlegroups.com
I appreciate your reply, but I am really trying to avoid re-creating the entire context because that requires also reloading all textures, shaders, etc.

sduclos

unread,
May 10, 2015, 7:38:28 PM5/10/15
to andro...@googlegroups.com
Dump,

On Mom's day my GPU is full. Thank to Philippe, I just need to recreate an
EGLSurface to get back a handle on it after a pause using engine->add->window.

This info is hard to find .. so I thought adding some more keyword to
this thread

Sylvain.
>>> email to android-ndk...@googlegroups.com <javascript:>.
>>> To post to this group, send email to andro...@googlegroups.com
>>> <javascript:>.

Philippe Simons

unread,
May 11, 2015, 3:38:27 AM5/11/15
to android-ndk
Yeah... actually, I found this myself with try and error
Reply all
Reply to author
Forward
0 new messages