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.
Map view lags behind annotation views when panning #5489
annotationsAnnotations on iOS and macOS or markers on AndroidiOSMapbox Maps SDK for iOSperformanceSpeed, stability, CPU usage, memory usage, or power usage
I found the MGLAnnotationView scrolling like jello when I use the new iOS SDK. The MGLAnnotationView can't follow the MapView smoothly. I have tested at iPhone 5s and iPhone 6.
I suspect #5245 may yield some improvement, specifically disabling animations while updating annotations and coalescing the frame updates into one Core Animation transaction. I’ll need to pull those changes out into a separate PR and see if they improve things on their own, without the other matrix-related changes.
The changes in #5245 don’t address this issue after all.
As far as I can tell, the map is lagging behind the annotation views by one frame every other frame or so. In the screen recording below, keep an eye on the annotation view that initially covers up the word “Washington”. When I scroll up, you can occasionally see “Washington” appear to the south of the annotation view; when I scroll down, you can occasionally see “Washington” appear to the north of the annotation view:
One potential way to eliminate the "tearing" effect might be to put the annotation updating phase into a custom style layer drawing callback. This would ensure that the Core Graphics and Core Animation drawing commands occur closer to the OpenGL drawing commands and in the same run loop iteration. Ironically, the annotation views would be less performant, but the perceived performance would improve.
#1813 in some respects addresses the opposite issue: the map was leading the UIView because the map was given the opportunity to advance one step (for a transition) before the UIView got a chance to update. The annotation view implementation addresses this by updating the frame on the "map did finish rendering frame" notification.
I think what we're seeing is that Core Animation / Core Graphics manages to maintain 60fps drawing of the annotation views even as MGLMapView struggles to do the same with the underlying map. We mitigated that for the user dot in #3683 by disabling implicit animation, but the root cause can still be reexposed by adding multiple views onto the map.
@mb12, thanks for the reminder. We previously looked into presentsWithTransaction in #1125 (comment), but there was no effect. In hindsight, that was because of an implicit animation, which we disabled in #3683. Having exhausted that lead for this issue, we should look into presentsWithTransaction at some point.
The good news: I have figured out the problem and have a local version working nearly perfectly.
The bad news: We need to get a higher frame rate on our core GL draws to solve it. This is essentially what @1ec5 has been hypothesizing since #5489 (comment), but I think I have confirmed it.
I tried a number of approaches to get the annotations & Core Animation to slow down and/or draw in sync with the OpenGL frame updates:
Calling -updateAnnotationViews only every other GL render (i.e. every other CADisplayLink update, i.e. at essentially half its current frame rate)
Experimenting with -[CATransaction flush] and -[CATransaction lock]
Bracketing both the call to -updateAnnotationViewsand_mbglMap->render() in a CATransaction with varying options (-setDisableActions:YES, varying animation durations including 0)
Checking out ((CAEAGLLayer *)_glView.layer).presentsWithTransaction = YES; after watching the WWDC session (again), despite poor specifics in both that and the docs around how to use it (but which is what led me to experimenting with the above bracketing around the GL render)
None of these reduced jitter since what we perceive as jitter is the position relative to the map, and the map isn't moving as smoothly as it could.
Also, just to be sure, I experimented with:
Rounding annotation latitude & longitude to varying levels (2-5 decimal places)
Rounding annotation position values to whole numbers instead of their default fractional CGPoint values
Experimenting with calling updateAnnotationViews from the drawingHandler of a custom layer
Ensuring that a map-less view with annotations still looked smooth
Ensuring that an annotation-less map view still looked smooth
It was that last one that still looked suspect to me, especially when using Apple Maps to compare, and profiling confirmed that I was only ever seeing ~30fps for a default Streets style (on a couple years old but respectable retina iPad mini). I pared back a style in Studio until I could get one that was consistently 55-60fps:
Portland
This mostly involved getting rid of all labels, symbols, and roads and just leaving fills like water and parks.
But: once I did this, and got the OpenGL frame rate to about 60fps, I got some good-looking sync:
'
You can still see this jitter a tiny bit when we dip into the ~55fps range, which is to be expected.
The test style URL in question is mapbox://styles/justin/cis6rzo41000egpnt2julnz92 if you'd like to try this yourself.
So, again I think we punt on this for now as there is no magic Core Animation bullet to help us. We need to get our frame rate up with arbitrary styles, which is a more difficult proposition than what Apple Maps does with its one or two default styles.
To be complete: I also changed the MGLTargetFrameInterval to trigger 30fps max GL draws, in combination with the CATransaction experimentation in case that would be enough to cause Core Animation to slow down and sync, but no dice. Our map looks the same (since we're at about 30fps already) and Core Animation was still out of sync since it's faster.
You can also see the map lagging behind the view in the GIF (!) in #5489 (comment) – that effect wouldn't be possible if not for the map rendering at far less than 60 fps.
We added CADisplayLink in order to throttle map redraws (originally to 30 fps, later to 60 fps, nominally). Should we revisit our use of that API? It has its own performance downsides: #2985.
As a clarification, when I say Core Animation in the context of this problem, I mean the mechanism by which native views are composited to the frame buffer on the device. Even though we no longer actually animate our annotation views, the mere movement of them pixel-by-pixel is governed by internal OpenGL rendering on Core Animation's part and that is a solid 60fps, thus highlighting weaknesses in our own frame draws.
I can't see why the user dot would behave differently.
I’ve split out #12740 to track a separate synchronization issue that only affects the user dot but not other annotation views. That issue includes a possible explanation as to why the user dot behaves differently than other annotation views.
annotationsAnnotations on iOS and macOS or markers on AndroidiOSMapbox Maps SDK for iOSperformanceSpeed, stability, CPU usage, memory usage, or power usage
Activity
friedbunny commentedon Jun 27, 2016
Please post more information about the view you’re using — code and a gif would be ideal. Thanks.
Paladinfeng commentedon Jun 27, 2016
ok, I'll save something as a gif video tomorrow.
it just like rolling shutter in the field of photography.
Paladinfeng commentedon Jun 28, 2016
can u see it? The MGLAnnotationView can't follow the mapView totally.
friedbunny commentedon Jun 28, 2016
Thanks for the image, @Paladinfeng. Syncing the movement of native views with the map is an ongoing challenge, certainly.
/cc @boundsj @1ec5
1ec5 commentedon Jun 28, 2016
I suspect #5245 may yield some improvement, specifically disabling animations while updating annotations and coalescing the frame updates into one Core Animation transaction. I’ll need to pull those changes out into a separate PR and see if they improve things on their own, without the other matrix-related changes.
[-]AnnotationView scrolling like jello[/-][+]Annotation views lag behind map view when panning[/+][-]Annotation views lag behind map view when panning[/-][+]Map view lags behind annotation views when panning[/+]1ec5 commentedon Jul 3, 2016
The changes in #5245 don’t address this issue after all.
As far as I can tell, the map is lagging behind the annotation views by one frame every other frame or so. In the screen recording below, keep an eye on the annotation view that initially covers up the word “Washington”. When I scroll up, you can occasionally see “Washington” appear to the south of the annotation view; when I scroll down, you can occasionally see “Washington” appear to the north of the annotation view:
1ec5 commentedon Jul 7, 2016
One potential way to eliminate the "tearing" effect might be to put the annotation updating phase into a custom style layer drawing callback. This would ensure that the Core Graphics and Core Animation drawing commands occur closer to the OpenGL drawing commands and in the same run loop iteration. Ironically, the annotation views would be less performant, but the perceived performance would improve.
incanus commentedon Jul 19, 2016
Naive post to related issue, before further investigation: #1813
1ec5 commentedon Jul 19, 2016
#1813 in some respects addresses the opposite issue: the map was leading the UIView because the map was given the opportunity to advance one step (for a transition) before the UIView got a chance to update. The annotation view implementation addresses this by updating the frame on the "map did finish rendering frame" notification.
I think what we're seeing is that Core Animation / Core Graphics manages to maintain 60fps drawing of the annotation views even as MGLMapView struggles to do the same with the underlying map. We mitigated that for the user dot in #3683 by disabling implicit animation, but the root cause can still be reexposed by adding multiple views onto the map.
34 remaining items
incanus commentedon Aug 22, 2016
Will continue to investigate this, but for a lot of use cases (particularly small view annotations) current performance is acceptable.
1ec5 commentedon Aug 22, 2016
@mb12, thanks for the reminder. We previously looked into
presentsWithTransactionin #1125 (comment), but there was no effect. In hindsight, that was because of an implicit animation, which we disabled in #3683. Having exhausted that lead for this issue, we should look intopresentsWithTransactionat some point.incanus commentedon Aug 23, 2016
The good news: I have figured out the problem and have a local version working nearly perfectly.
The bad news: We need to get a higher frame rate on our core GL draws to solve it. This is essentially what @1ec5 has been hypothesizing since #5489 (comment), but I think I have confirmed it.
I tried a number of approaches to get the annotations & Core Animation to slow down and/or draw in sync with the OpenGL frame updates:
-updateAnnotationViewsonly every other GL render (i.e. every otherCADisplayLinkupdate, i.e. at essentially half its current frame rate)-[CATransaction flush]and-[CATransaction lock]-updateAnnotationViewsand_mbglMap->render()in aCATransactionwith varying options (-setDisableActions:YES, varying animation durations including0)((CAEAGLLayer *)_glView.layer).presentsWithTransaction = YES;after watching the WWDC session (again), despite poor specifics in both that and the docs around how to use it (but which is what led me to experimenting with the above bracketing around the GL render)None of these reduced jitter since what we perceive as jitter is the position relative to the map, and the map isn't moving as smoothly as it could.
Also, just to be sure, I experimented with:
positionvalues to whole numbers instead of their default fractionalCGPointvaluesupdateAnnotationViewsfrom thedrawingHandlerof a custom layerIt was that last one that still looked suspect to me, especially when using Apple Maps to compare, and profiling confirmed that I was only ever seeing ~30fps for a default Streets style (on a couple years old but respectable retina iPad mini). I pared back a style in Studio until I could get one that was consistently 55-60fps:
Portland
This mostly involved getting rid of all labels, symbols, and roads and just leaving fills like water and parks.
But: once I did this, and got the OpenGL frame rate to about 60fps, I got some good-looking sync:
You can still see this jitter a tiny bit when we dip into the ~55fps range, which is to be expected.
The test style URL in question is
mapbox://styles/justin/cis6rzo41000egpnt2julnz92if you'd like to try this yourself.So, again I think we punt on this for now as there is no magic Core Animation bullet to help us. We need to get our frame rate up with arbitrary styles, which is a more difficult proposition than what Apple Maps does with its one or two default styles.
/cc @jfirebaugh @kkaefer
incanus commentedon Aug 23, 2016
To be complete: I also changed the
MGLTargetFrameIntervalto trigger 30fps max GL draws, in combination with theCATransactionexperimentation in case that would be enough to cause Core Animation to slow down and sync, but no dice. Our map looks the same (since we're at about 30fps already) and Core Animation was still out of sync since it's faster.1ec5 commentedon Aug 23, 2016
You can also see the map lagging behind the view in the GIF (!) in #5489 (comment) – that effect wouldn't be possible if not for the map rendering at far less than 60 fps.
We added CADisplayLink in order to throttle map redraws (originally to 30 fps, later to 60 fps, nominally). Should we revisit our use of that API? It has its own performance downsides: #2985.
incanus commentedon Aug 23, 2016
Will read up on
CADisplayLinkfurther.As a clarification, when I say Core Animation in the context of this problem, I mean the mechanism by which native views are composited to the frame buffer on the device. Even though we no longer actually animate our annotation views, the mere movement of them pixel-by-pixel is governed by internal OpenGL rendering on Core Animation's part and that is a solid 60fps, thus highlighting weaknesses in our own frame draws.
1ec5 commentedon Nov 1, 2016
@Paladinfeng, FYI, the equivalent issue on Android is being tracked in #6139. mapbox/mapbox-gl-js#3273 is a conceptually similar issue in GL JS.
tabsong commentedon Jun 30, 2017
@incanus in the end, can you resolve the sync problem?
1ec5 commentedon Aug 25, 2018
I’ve split out #12740 to track a separate synchronization issue that only affects the user dot but not other annotation views. That issue includes a possible explanation as to why the user dot behaves differently than other annotation views.
1ec5 commentedon Dec 10, 2018
Fixed in #12895.