Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Always drive rendering from the main thread - #1624

Merged
jfirebaugh merged 10 commits into
masterfrom
1624-drive-rendering-from-main-thread
Jun 26, 2015
Merged

jfirebaugh merged 10 commits into
masterfrom
1624-drive-rendering-from-main-thread

Conversation

@kkaefer

@kkaefer kkaefer commented May 22, 2015

Copy link
Copy Markdown
Member

In most cases, the main/UI thread triggers rendering, except for our GLFW sample app.

@kkaefer kkaefer self-assigned this May 22, 2015
@kkaefer

kkaefer commented May 22, 2015

Copy link
Copy Markdown
Member Author

@ljbade from the map thread, how can we signal to the main UI/Java thread that it must call map->renderSync()?

@kkaefer kkaefer changed the title Always drive rendering from the main thread [DO NOT MERGE] Always drive rendering from the main thread May 22, 2015
@kkaefer kkaefer added the ⚠️ DO NOT MERGE Work in progress, proof of concept, or on hold label May 22, 2015
@ljbade

ljbade commented May 24, 2015

Copy link
Copy Markdown
Contributor

@kkafer first issue is that you need to attach java runtime to the map thread then you can use JNI to invoke Java. An example is the event handling for the map class in the nativemapview.cpp file.

Next issue is that Android does not allow non UI thread to touch UI objects so likely you need to call an async handler that runs on the UI thread.

It may be simpler to implement async call in C++ to UI thread

@kkaefer

kkaefer commented May 26, 2015

Copy link
Copy Markdown
Member Author

@ljbade so the flow would be:

  • In the Map thread, attach the Java runtime (can we attach this in addition to the main thread or can only be one thread attached at one point?)
  • call a Java function from native code on the Map thread
  • the Java function dispatches to the Main thread in Java
  • the Main thread in Java triggers a rerender

We can't really do an async call in C++ land since we don't have an event loop running in the Main thread. Essentially, what I'm looking for is a way to trigger a frame update from the Map thread. Do you see any other options?

@ljbade

ljbade commented May 26, 2015

Copy link
Copy Markdown
Contributor

@kkaefer you can have many threads attached, you just have to remember to detach before deleting the thread

Isn't most the Map functions async? Perhaps this can all be done in C++ side by just calling Map.

@jfirebaugh

Copy link
Copy Markdown
Contributor

We should pick this up again, I think it's a dependency for #1026.

@kkaefer
kkaefer force-pushed the 1624-drive-rendering-from-main-thread branch from fedef30 to c531b1c Compare June 25, 2015 12:40
@kkaefer

kkaefer commented Jun 25, 2015

Copy link
Copy Markdown
Member Author

@jfirebaugh updated and fixed Android support. This is mostly a stopgap for Android to keep the app functional; we need a larger Android refactoring eventually.

@kkaefer
kkaefer force-pushed the 1624-drive-rendering-from-main-thread branch from c531b1c to 70a58c1 Compare June 25, 2015 13:00
@jfirebaugh
jfirebaugh force-pushed the 1624-drive-rendering-from-main-thread branch from 70a58c1 to 7734ca0 Compare June 25, 2015 18:30
Comment thread src/mbgl/map/map_context.cpp Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since render doesn't check mayRender, it seems like it's equivalent to "is a still render pending?". Can we use callback in update() instead, like:

if (callback) {
    render();
} else {
    view.invalidate();
}

Or is there something I'm missing?

@jfirebaugh

Copy link
Copy Markdown
Contributor

I made Transform live on the main thread only. As part of that, Transform::updateTransitions() is now called on the main thread, which allows it to dispatch MapChangeRegionIsChanging and MapChangeRegionDidChangeAnimated correctly.

@incanus, can you review these changes (particularly 8e9a7e9, 9dc0263 and 7549aaf) and test them with the view-syncing stuff you're working on?

The remaining fix for MapChangeRegion* events that I'm seeing is that Transform should suppress events if state.gestureInProgress is true. In this situation, MGLMapView will be dispatching its own set of MapChangeRegion* events as the gesture progresses. @incanus: agree?

@jfirebaugh jfirebaugh changed the title [DO NOT MERGE] Always drive rendering from the main thread Always drive rendering from the main thread Jun 25, 2015
@jfirebaugh jfirebaugh added this to the iOS Beta 3 milestone Jun 25, 2015
@jfirebaugh jfirebaugh assigned jfirebaugh and unassigned kkaefer Jun 25, 2015
@incanus

incanus commented Jun 25, 2015

Copy link
Copy Markdown
Contributor

I don't see this affecting what I'm working on, as I'm no longer driving annotation view updates from the notification indirectly, but rather in the same method that the core GL view is told to render a frame synchronously. I will test, though.

As for approach, it looks good and tests pass, but I'll take a closer look.

@kkaefer

kkaefer commented Jun 26, 2015

Copy link
Copy Markdown
Member Author

@incanus @jfirebaugh any objections to merging this?

@incanus

incanus commented Jun 26, 2015

Copy link
Copy Markdown
Contributor

Still got a bit more digging to do — will hit it this am.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use MGLAssertIsMainThread(), which provides more details in the assertion message.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, that won’t work because _cmd is only available inside Objective-C classes. NSCAssert() would still be preferable to assert() in Objective-C code, though.

@incanus

incanus commented Jun 26, 2015

Copy link
Copy Markdown
Contributor

Ok I am 👍 if you are @jfirebaugh — this doesn't interfere with anything I'm working on and it certainly looks cleaner from the will/am/did update notification POV.

@jfirebaugh
jfirebaugh force-pushed the 1624-drive-rendering-from-main-thread branch from 7549aaf to ea5c56d Compare June 26, 2015 17:32
@jfirebaugh
jfirebaugh merged commit ea5c56d into master Jun 26, 2015
@jfirebaugh
jfirebaugh deleted the 1624-drive-rendering-from-main-thread branch June 26, 2015 17:32
@ljbade

ljbade commented Jun 29, 2015

Copy link
Copy Markdown
Contributor

Great work. Thanks for fixing this.

@1ec5 1ec5 removed the ⚠️ DO NOT MERGE Work in progress, proof of concept, or on hold label Jul 8, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants