Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
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

Closed

Description

@ljbade

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?:

How does the iOS version handle this?

Activity

  1. added
    Android
    on Nov 9, 2014
  2. ljbade commented on Nov 9, 2014

    @ljbade
    ContributorAuthor

    Related issues that are caused by problems with activity lifecycle:
    #568
    #561
    #568
    #570

  3. ljbade commented on Nov 9, 2014

    @ljbade
    ContributorAuthor

    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

  4. ljbade commented on Nov 9, 2014

    @ljbade
    ContributorAuthor

    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.

  5. ljbade commented on Nov 9, 2014

    @ljbade
    ContributorAuthor

    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)

  6. incanus commented on Nov 13, 2014

    @incanus
    Contributor

    How does the iOS version handle this?

    Handle what, more specifically? This is a lot to take in — what's the basics of what you're getting at?

  7. ljbade commented on Nov 13, 2014

    @ljbade
    ContributorAuthor

    @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)

  8. added this to the Android merge milestone on Nov 13, 2014
  9. incanus commented on Nov 17, 2014

    @incanus
    Contributor

    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:

    1. map->stop(), then use iOS-provided API to delete the (iOS-speak) view's underlying framebuffer.
    2. Use iOS-provided API to bind the framebuffer to OpenGL ES, then 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.

  10. ljbade commented on Nov 18, 2014

    @ljbade
    ContributorAuthor

    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.

  11. incanus commented on Nov 18, 2014

    @incanus
    Contributor

    My best guess is that iOS might take a snapshot of the map as you call deleteDrawable and then mock it with that.

  12. ljbade commented on Nov 20, 2014

    @ljbade
    ContributorAuthor

    @jfirebaugh prefers stdlib mutexes and stuff, will look to see if I can do what with pause/resume code

  13. ljbade commented on Nov 22, 2014

    @ljbade
    ContributorAuthor

    OK changed to stdlib in 78a6677

  14. ljbade commented on Nov 22, 2014

    @ljbade
    ContributorAuthor

    OK I think I am as good as I am going to get for the moment.

  15. ntop001 commented on Aug 13, 2018

    @ntop001

    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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Androidrefactor

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions