Repository navigation
[ios] Add preferred FPS setting; vary maximum FPS by device capability #12501
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,6 +79,21 @@ typedef NS_ENUM(NSUInteger, MGLUserTrackingMode) { | |
| MGLUserTrackingModeFollowWithCourse, | ||
| }; | ||
|
|
||
| /** Options for `MGLMapView.preferredFramesPerSecond`. */ | ||
| typedef NSInteger MGLMapViewPreferredFramesPerSecond NS_TYPED_EXTENSIBLE_ENUM; | ||
|
|
||
| /** | ||
| The default frame rate. This can be either 30 FPS or 60 FPS, depending on | ||
| device capabilities. | ||
| */ | ||
| FOUNDATION_EXTERN MGL_EXPORT const MGLMapViewPreferredFramesPerSecond MGLMapViewPreferredFramesPerSecondDefault; | ||
|
|
||
| /** A conservative frame rate; typically 30 FPS. */ | ||
| FOUNDATION_EXTERN MGL_EXPORT const MGLMapViewPreferredFramesPerSecond MGLMapViewPreferredFramesPerSecondLowPower; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m not entirely happy with |
||
|
|
||
| /** The maximum supported frame rate; typically 60 FPS. */ | ||
| FOUNDATION_EXTERN MGL_EXPORT const MGLMapViewPreferredFramesPerSecond MGLMapViewPreferredFramesPerSecondMaximum; | ||
|
|
||
| /** | ||
| An interactive, customizable map view with an interface similar to the one | ||
| provided by Apple’s MapKit. | ||
|
|
@@ -286,6 +301,21 @@ MGL_EXPORT IB_DESIGNABLE | |
| */ | ||
| - (IBAction)showAttribution:(id)sender; | ||
|
|
||
| /** | ||
| The preferred frame rate at which the map view is rendered. | ||
|
|
||
| The default value for this property is | ||
| `MGLMapViewPreferredFramesPerSecondDefault`, which will adaptively set the | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
does this means that it will be overridden by the sdk according to the device?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, |
||
| preferred frame rate based on the capability of the user’s device to maintain | ||
| a smooth experience. | ||
|
|
||
| In addition to the provided `MGLMapViewPreferredFramesPerSecond` options, this | ||
| property can be set to arbitrary integer values. | ||
|
|
||
| @see `CADisplayLink.preferredFramesPerSecond` | ||
| */ | ||
| @property (nonatomic, assign) MGLMapViewPreferredFramesPerSecond preferredFramesPerSecond; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose for letting devs set 0...60? does it makes a difference than restricting to -1/30/60?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Allowing developers to set arbitrary values means that we can allow:
The |
||
|
|
||
| @property (nonatomic) NSArray<NSString *> *styleClasses __attribute__((unavailable("Support for style classes has been removed."))); | ||
|
|
||
| - (BOOL)hasStyleClass:(NSString *)styleClass __attribute__((unavailable("Support for style classes has been removed."))); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,7 @@ | |
| #import "NSProcessInfo+MGLAdditions.h" | ||
| #import "NSString+MGLAdditions.h" | ||
| #import "NSURL+MGLAdditions.h" | ||
| #import "UIDevice+MGLAdditions.h" | ||
| #import "UIImage+MGLAdditions.h" | ||
| #import "UIViewController+MGLAdditions.h" | ||
|
|
||
|
|
@@ -87,6 +88,10 @@ | |
| const CGFloat MGLMapViewDecelerationRateFast = UIScrollViewDecelerationRateFast; | ||
| const CGFloat MGLMapViewDecelerationRateImmediate = 0.0; | ||
|
|
||
| const MGLMapViewPreferredFramesPerSecond MGLMapViewPreferredFramesPerSecondDefault = -1; | ||
| const MGLMapViewPreferredFramesPerSecond MGLMapViewPreferredFramesPerSecondLowPower = 30; | ||
| const MGLMapViewPreferredFramesPerSecond MGLMapViewPreferredFramesPerSecondMaximum = 60; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there’s room for that kind of functionality, yeah — testing on an iPhone 5, it does seem like 15 FPS may be more appropriate for that device. |
||
|
|
||
| /// Indicates the manner in which the map view is tracking the user location. | ||
| typedef NS_ENUM(NSUInteger, MGLUserTrackingState) { | ||
| /// The map view is not yet tracking the user location. | ||
|
|
@@ -118,8 +123,6 @@ typedef NS_ENUM(NSUInteger, MGLUserTrackingState) { | |
| /// Initial zoom level when entering user tracking mode from a low zoom level. | ||
| const double MGLDefaultZoomLevelForUserTracking = 14.0; | ||
|
|
||
| const NSUInteger MGLTargetFrameInterval = 1; // Target FPS will be 60 divided by this value | ||
|
|
||
| /// Tolerance for snapping to true north, measured in degrees in either direction. | ||
| const CLLocationDirection MGLToleranceForSnappingToNorth = 7; | ||
|
|
||
|
|
@@ -403,6 +406,9 @@ - (void)commonInit | |
| self.backgroundColor = [UIColor clearColor]; | ||
| self.clipsToBounds = YES; | ||
| if (@available(iOS 11.0, *)) { self.accessibilityIgnoresInvertColors = YES; } | ||
|
|
||
| self.preferredFramesPerSecond = MGLMapViewPreferredFramesPerSecondDefault; | ||
|
|
||
| // setup mbgl view | ||
| _mbglView = new MBGLView(self); | ||
|
|
||
|
|
@@ -1125,7 +1131,7 @@ - (void)validateDisplayLink | |
| } | ||
|
|
||
| _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateFromDisplayLink)]; | ||
| _displayLink.frameInterval = MGLTargetFrameInterval; | ||
| [self updateDisplayLinkPreferredFramesPerSecond]; | ||
| [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; | ||
| _needsDisplayRefresh = YES; | ||
| [self updateFromDisplayLink]; | ||
|
|
@@ -1137,6 +1143,49 @@ - (void)validateDisplayLink | |
| } | ||
| } | ||
|
|
||
| - (void)updateDisplayLinkPreferredFramesPerSecond | ||
| { | ||
| if (!_displayLink) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| NSInteger newFrameRate; | ||
| if (_preferredFramesPerSecond == MGLMapViewPreferredFramesPerSecondDefault) | ||
| { | ||
| // On legacy devices that cannot maintain a reasonable frame rate, set | ||
| // a lower limit to avoid jank. | ||
| newFrameRate = UIDevice.currentDevice.mgl_isLegacyDevice ? MGLMapViewPreferredFramesPerSecondLowPower : MGLMapViewPreferredFramesPerSecondMaximum; | ||
| } | ||
| else | ||
| { | ||
| newFrameRate = _preferredFramesPerSecond; | ||
| } | ||
|
|
||
| if (@available(iOS 10.0, *)) | ||
| { | ||
| _displayLink.preferredFramesPerSecond = newFrameRate; | ||
| } | ||
| else | ||
| { | ||
| // CADisplayLink.frameInterval does not support more than 60 FPS (and | ||
| // no device that supports >60 FPS ever supported iOS 9). | ||
| NSInteger maximumFrameRate = 60; | ||
| _displayLink.frameInterval = maximumFrameRate / MIN(newFrameRate, maximumFrameRate); | ||
| } | ||
| } | ||
|
|
||
| - (void)setPreferredFramesPerSecond:(MGLMapViewPreferredFramesPerSecond)preferredFramesPerSecond | ||
| { | ||
| if (_preferredFramesPerSecond == preferredFramesPerSecond) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _preferredFramesPerSecond = preferredFramesPerSecond; | ||
| [self updateDisplayLinkPreferredFramesPerSecond]; | ||
| } | ||
|
|
||
| - (void)didMoveToWindow | ||
| { | ||
| [self validateDisplayLink]; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #import <UIKit/UIKit.h> | ||
|
|
||
| @interface UIDevice (MGLAdditions) | ||
|
|
||
| @property (nonatomic, readonly) BOOL mgl_isLegacyDevice; | ||
|
|
||
| @end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #import "UIDevice+MGLAdditions.h" | ||
| #include <sys/sysctl.h> | ||
|
|
||
| @implementation UIDevice (MGLAdditions) | ||
|
|
||
| - (NSString *)modelString { | ||
| char *typeSpecifier = "hw.machine"; | ||
|
|
||
| size_t size; | ||
| sysctlbyname(typeSpecifier, NULL, &size, NULL, 0); | ||
|
|
||
| char *answer = malloc(size); | ||
| sysctlbyname(typeSpecifier, answer, &size, NULL, 0); | ||
|
|
||
| NSString *results = [NSString stringWithCString:answer encoding:NSUTF8StringEncoding]; | ||
|
|
||
| free(answer); | ||
| return results; | ||
| } | ||
|
|
||
| - (BOOL)mgl_isLegacyDevice { | ||
| // This is a list of supported devices that cannot maintain a reasonable frame | ||
| // rate under typical load. For brevity, unsupported devices are not included. | ||
| NSSet *blacklist = [NSSet setWithObjects: | ||
| @"iPhone4", // iPhone 4s | ||
| @"iPhone5", // iPhone 5, 5c | ||
| @"iPhone6", // iPhone 5s | ||
|
|
||
| @"iPad2", // iPad 2, Mini | ||
| @"iPad3", // iPad 3 | ||
| @"iPad4", // iPad Air, Mini 2, Mini 3 | ||
|
|
||
| @"iPod5", // iPod Touch 5 | ||
|
|
||
| nil | ||
| ]; | ||
|
|
||
| NSString *model = [self modelString]; | ||
|
|
||
| for (NSString *blacklistedModel in blacklist) { | ||
| if ([model hasPrefix:[blacklistedModel stringByAppendingString:@","]]) { | ||
| return YES; | ||
| } | ||
| } | ||
|
|
||
| // TODO: Also handle simulator using something like `ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"]`. | ||
|
|
||
| return NO; | ||
| } | ||
|
|
||
| @end |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is implemented with
NS_TYPED_EXTENSIBLE_ENUM(instead of usingNS_ENUMor the like) to enable Swift users to omit the full key/const name and use the favored type-aware dot syntax (e.g.,mapView.preferredFramesPerSecond = .lowPower).