You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Rethink how JNI and Map interact in lifecycle #578
A careful analysis needs to be made of the interaction between the Android activity lifecycle, GL lifecycle and the way Map handles it.
My code is currently very complex to handle every possible order of calls since it seems to be somewhat chaotic as to what order Android calls things. I also have to work around many problems in getting Map to survive context deletion.
@kkaefer@incanus do you mind reading these to see if you can think of a way to handle it better?:
Perhaps we need to pick through how GLSurfaceView does stuff? You can find the code here
Also NativeActivity might be a useful guide from the NDK/C++ perspective
I have discovered the secret of setPreserveEGLContextOnPause:
As long as we have GLES 2.0 we are allowed to have multiple EGL contexts!
Thus I no longer need to delete (and recreate later) the GL context and state when switching between apps. It should be possible to preserve Map's state by simply pausing the render thread without worrying about the GL objects it is holding becoming invalid.
I have decided to add a Map::pause/resume function to allow pausing the map without resetting the map state. You can find it in map.cpp in 28d66bb
Please ignore all the other mess - I need to get better at keeping commits simple and readable. I removed the logging and stuff in a later commit.
The basic idea is to pause the render thread when our app loses focus (to prevent GL errors due to not having a valid GL surface bound), and to prevent CPU use.
It also means we can start back where we were without needing to reload state etc.
I have used a combination of mutex's and condition variables to implement it. It works in my testing but someone with better knowledge of threading issues should check my work.
In later commits I also allow Map::start to start paused (so we can wait for GL context init before continuing: due to activity lifecycle order complications we cannot be sure it is ready yet)
So unfortunately that doesn't get into too much detail about the how, but it seems that iOS provides a pretty high-level API that just does the right thing.
Of course iOS make it a lot easier. Sometimes I don't think the Android devs think some of their stuff through before they publish it.
If you are using map->start/stop, howcome the map's state does not get reset? It was doing that when I tried it. That was why I added functions to directly pause the rendering thread.
OK from analysing GLSurfaceView
I have discovered the secret of setPreserveEGLContextOnPause:
As long as we have GLES 2.0 we are allowed to have multiple EGL contexts!
Thus I no longer need to delete (and recreate later) the GL context and state when switching between apps. It should be possible to preserve Map's state by simply pausing the render thread without worrying about the GL objects it is holding becoming invalid.
Very old post, but I just come cross the same question. How did you solve the problem? In the old way, Surface and eglContext are created and destroyed at the same time, so, the glcontext lost. After some research, I found that, I can only destroy Surface when application paused, then create new surface and use the old context when resumed, is that right?
Activity
ljbade commentedon Nov 9, 2014
Related issues that are caused by problems with activity lifecycle:
#568
#561
#568
#570
ljbade commentedon Nov 9, 2014
I just discovered this interesting function of GLSurfaceView:
setPreserveEGLContextOnPause
Perhaps we need to pick through how GLSurfaceView does stuff? You can find the code here
Also NativeActivity might be a useful guide from the NDK/C++ perspective
ljbade commentedon Nov 9, 2014
OK from analysing GLSurfaceView
I have discovered the secret of setPreserveEGLContextOnPause:
As long as we have GLES 2.0 we are allowed to have multiple EGL contexts!
Thus I no longer need to delete (and recreate later) the GL context and state when switching between apps. It should be possible to preserve Map's state by simply pausing the render thread without worrying about the GL objects it is holding becoming invalid.
ljbade commentedon Nov 9, 2014
I have decided to add a Map::pause/resume function to allow pausing the map without resetting the map state. You can find it in map.cpp in 28d66bb
Please ignore all the other mess - I need to get better at keeping commits simple and readable. I removed the logging and stuff in a later commit.
The basic idea is to pause the render thread when our app loses focus (to prevent GL errors due to not having a valid GL surface bound), and to prevent CPU use.
It also means we can start back where we were without needing to reload state etc.
I have used a combination of mutex's and condition variables to implement it. It works in my testing but someone with better knowledge of threading issues should check my work.
In later commits I also allow Map::start to start paused (so we can wait for GL context init before continuing: due to activity lifecycle order complications we cannot be sure it is ready yet)
incanus commentedon Nov 13, 2014
Handle what, more specifically? This is a lot to take in — what's the basics of what you're getting at?
ljbade commentedon Nov 13, 2014
@incanus, sorry I meant how does iOS handle switching away from the app and losing the GL context without losing the map's state (e.g. position)
incanus commentedon Nov 17, 2014
You can see how background/foreground lifecycle happens here:
https://github.com/mapbox/mapbox-gl-cocoa/blob/10363d21122530e5021c14b383d7e74e6ba7a313/mapbox-gl-cocoa/MGLMapView.mm#L438-L450
But basically, it's:
map->stop(), then use iOS-provided API to delete the (iOS-speak) view's underlying framebuffer.map->start().The associated iOS API for deleting/binding looks like:
So unfortunately that doesn't get into too much detail about the how, but it seems that iOS provides a pretty high-level API that just does the right thing.
ljbade commentedon Nov 18, 2014
Of course iOS make it a lot easier. Sometimes I don't think the Android devs think some of their stuff through before they publish it.
If you are using
map->start/stop, howcome the map's state does not get reset? It was doing that when I tried it. That was why I added functions to directly pause the rendering thread.incanus commentedon Nov 18, 2014
My best guess is that iOS might take a snapshot of the map as you call
deleteDrawableand then mock it with that.ljbade commentedon Nov 20, 2014
@jfirebaugh prefers stdlib mutexes and stuff, will look to see if I can do what with pause/resume code
ljbade commentedon Nov 22, 2014
Hmm these will work http://www.cplusplus.com/reference/mutex/mutex/ and http://www.cplusplus.com/reference/condition_variable/
ljbade commentedon Nov 22, 2014
OK changed to stdlib in 78a6677
ljbade commentedon Nov 22, 2014
OK I think I am as good as I am going to get for the moment.
ntop001 commentedon Aug 13, 2018
Very old post, but I just come cross the same question. How did you solve the problem? In the old way, Surface and eglContext are created and destroyed at the same time, so, the glcontext lost. After some research, I found that, I can only destroy Surface when application paused, then create new surface and use the old context when resumed, is that right?