diff --git a/packages/react-native/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.mm b/packages/react-native/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.mm index 4571f0effc9e..13b37854e583 100644 --- a/packages/react-native/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.mm +++ b/packages/react-native/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.mm @@ -7,6 +7,7 @@ #import +#import #import #import @@ -53,6 +54,7 @@ @implementation RCTNativeAnimatedNodesManager { NSMutableDictionary *> *_eventDrivers; NSMutableSet> *_activeAnimations; CADisplayLink *_displayLink; + RCTAdaptiveFrameRateController *_frameRateController; } - (instancetype)initWithSurfacePresenter:(id)surfacePresenter @@ -62,6 +64,7 @@ - (instancetype)initWithSurfacePresenter:(id)surfacePre _animationNodes = [NSMutableDictionary new]; _eventDrivers = [NSMutableDictionary new]; _activeAnimations = [NSMutableSet new]; + _frameRateController = [RCTAdaptiveFrameRateController new]; } return self; } @@ -455,6 +458,7 @@ - (void)stopAnimationLoop - (void)stepAnimations:(CADisplayLink *)displaylink { + CFTimeInterval workStart = CACurrentMediaTime(); NSTimeInterval time = displaylink.timestamp; for (id animationDriver in _activeAnimations) { [animationDriver stepAnimationWithTime:time]; @@ -469,6 +473,8 @@ - (void)stepAnimations:(CADisplayLink *)displaylink } } + [_frameRateController recordFrameWorkFrom:workStart to:CACurrentMediaTime() forDisplayLink:displaylink]; + [self stopAnimationLoopIfNeeded]; } diff --git a/packages/react-native/React/Base/RCTAdaptiveFrameRate.h b/packages/react-native/React/Base/RCTAdaptiveFrameRate.h new file mode 100644 index 000000000000..27af425f3441 --- /dev/null +++ b/packages/react-native/React/Base/RCTAdaptiveFrameRate.h @@ -0,0 +1,89 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +/** + * Chooses a frame rate range for a display link from how long its recent + * frames took to process. Frames run at the screen's maximum rate while the + * work fits the frame budget, and step down to a steady 60, 30 or 24 fps when + * it does not, rather than dropping frames unevenly. + * + * Not thread safe: use each instance from the thread its display link ticks on. + */ +@interface RCTAdaptiveFrameRate : NSObject + +/** + * Returns nil when maximumFramesPerSecond is 60 or less, so callers leave + * their display link at its default rate. + */ ++ (nullable instancetype)adaptiveFrameRateForMaximumFramesPerSecond:(NSInteger)maximumFramesPerSecond; + +/** + * Uses the main screen's maximum frame rate. Main thread only, because it + * reads UIScreen. + */ ++ (nullable instancetype)adaptiveFrameRateForMainScreen; + +- (instancetype)initWithMaximumFramesPerSecond:(NSInteger)maximumFramesPerSecond NS_DESIGNATED_INITIALIZER; +- (instancetype)init NS_UNAVAILABLE; ++ (instancetype)new NS_UNAVAILABLE; + +@property (nonatomic, readonly) CAFrameRateRange currentRange; + +/** + * Records how long one frame's work took. Returns YES when this changed + * currentRange, which is when the display link needs to be updated. + */ +- (BOOL)recordFrameWorkDuration:(NSTimeInterval)seconds; + +#if !TARGET_OS_OSX +- (void)applyToDisplayLink:(CADisplayLink *)displayLink; +#endif + +@end + +#if !TARGET_OS_OSX + +/** + * Adapts the frame rate of the display links that one target ticks for. Each + * new display link, and each one that ticks again after a pause of more than + * 250 ms, starts over at the screen's maximum rate with no samples. Work + * measured for one animation then does not slow down the next. + * + * Not thread safe: use it only from the display link's tick, on the main thread. + */ +@interface RCTAdaptiveFrameRateController : NSObject + +/** + * Follows the main screen's maximum frame rate, which it reads each time a + * display link starts over. + */ +- (instancetype)init; + +/** + * Uses maximumFramesPerSecond in place of the main screen's maximum. + */ +- (instancetype)initWithMaximumFramesPerSecond:(NSInteger)maximumFramesPerSecond; + +/** + * Records the work that one tick of displayLink did between two + * CACurrentMediaTime() readings, and sets the display link's + * preferredFrameRateRange when the chosen range changes. + */ +- (void)recordFrameWorkFrom:(CFTimeInterval)startTime + to:(CFTimeInterval)endTime + forDisplayLink:(CADisplayLink *)displayLink; + +@end + +#endif + +NS_ASSUME_NONNULL_END diff --git a/packages/react-native/React/Base/RCTAdaptiveFrameRate.m b/packages/react-native/React/Base/RCTAdaptiveFrameRate.m new file mode 100644 index 000000000000..00775d9712e4 --- /dev/null +++ b/packages/react-native/React/Base/RCTAdaptiveFrameRate.m @@ -0,0 +1,144 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import "RCTAdaptiveFrameRate.h" + +#if !TARGET_OS_OSX +#import + +#import "RCTAssert.h" +#endif + +#define RCTFrameWorkSampleCount 4 + +@implementation RCTAdaptiveFrameRate { + NSInteger _maximumFramesPerSecond; + NSTimeInterval _samples[RCTFrameWorkSampleCount]; + NSUInteger _recordedSampleCount; +} + ++ (nullable instancetype)adaptiveFrameRateForMaximumFramesPerSecond:(NSInteger)maximumFramesPerSecond +{ + return maximumFramesPerSecond > 60 ? [[self alloc] initWithMaximumFramesPerSecond:maximumFramesPerSecond] : nil; +} + ++ (nullable instancetype)adaptiveFrameRateForMainScreen +{ +#if TARGET_OS_OSX + return nil; +#else + RCTAssertMainQueue(); + return [self adaptiveFrameRateForMaximumFramesPerSecond:UIScreen.mainScreen.maximumFramesPerSecond]; +#endif +} + +- (instancetype)initWithMaximumFramesPerSecond:(NSInteger)maximumFramesPerSecond +{ + if ((self = [super init])) { + _maximumFramesPerSecond = MAX(maximumFramesPerSecond, 60); + _currentRange = [self rangeForFrameWorkDuration:0]; + } + return self; +} + +- (BOOL)recordFrameWorkDuration:(NSTimeInterval)seconds +{ + _samples[_recordedSampleCount % RCTFrameWorkSampleCount] = seconds; + _recordedSampleCount++; + if (_recordedSampleCount < RCTFrameWorkSampleCount) { + return NO; + } + + NSTimeInterval total = 0; + for (NSUInteger i = 0; i < RCTFrameWorkSampleCount; i++) { + total += _samples[i]; + } + CAFrameRateRange range = [self rangeForFrameWorkDuration:total / RCTFrameWorkSampleCount]; + if (CAFrameRateRangeIsEqualToRange(range, _currentRange)) { + return NO; + } + _currentRange = range; + return YES; +} + +- (CAFrameRateRange)rangeForFrameWorkDuration:(NSTimeInterval)seconds +{ + if (seconds < 1.0 / _maximumFramesPerSecond) { + return CAFrameRateRangeMake(60, _maximumFramesPerSecond, _maximumFramesPerSecond); + } + if (seconds < 1.0 / 60) { + return CAFrameRateRangeMake(30, 60, 60); + } + if (seconds < 1.0 / 30) { + return CAFrameRateRangeMake(24, 30, 30); + } + return CAFrameRateRangeMake(10, 24, 24); +} + +#if !TARGET_OS_OSX +- (void)applyToDisplayLink:(CADisplayLink *)displayLink +{ + displayLink.preferredFrameRateRange = _currentRange; +} +#endif + +@end + +#if !TARGET_OS_OSX + +// More than twice the 100 ms between frames at the slowest range's 10 fps +// minimum, so only a pause or a stalled main thread reaches it. +static const CFTimeInterval RCTFrameWorkPauseThreshold = 0.25; + +@implementation RCTAdaptiveFrameRateController { + RCTAdaptiveFrameRate *_Nullable (^_makeAdaptiveFrameRate)(void); + RCTAdaptiveFrameRate *_Nullable _adaptiveFrameRate; + __weak CADisplayLink *_displayLink; + CFTimeInterval _lastFrameWorkEnd; +} + +- (instancetype)init +{ + return [self initWithAdaptiveFrameRateFactory:^{ + return [RCTAdaptiveFrameRate adaptiveFrameRateForMainScreen]; + }]; +} + +- (instancetype)initWithMaximumFramesPerSecond:(NSInteger)maximumFramesPerSecond +{ + return [self initWithAdaptiveFrameRateFactory:^{ + return [RCTAdaptiveFrameRate adaptiveFrameRateForMaximumFramesPerSecond:maximumFramesPerSecond]; + }]; +} + +- (instancetype)initWithAdaptiveFrameRateFactory:(RCTAdaptiveFrameRate *_Nullable (^)(void))factory +{ + if ((self = [super init])) { + _makeAdaptiveFrameRate = factory; + } + return self; +} + +- (void)recordFrameWorkFrom:(CFTimeInterval)startTime + to:(CFTimeInterval)endTime + forDisplayLink:(CADisplayLink *)displayLink +{ + if (displayLink != _displayLink || startTime - _lastFrameWorkEnd > RCTFrameWorkPauseThreshold) { + _displayLink = displayLink; + _adaptiveFrameRate = _makeAdaptiveFrameRate(); + [_adaptiveFrameRate applyToDisplayLink:displayLink]; + } + _lastFrameWorkEnd = endTime; + + if ([_adaptiveFrameRate recordFrameWorkDuration:endTime - startTime]) { + [_adaptiveFrameRate applyToDisplayLink:displayLink]; + } +} + +@end + +#endif diff --git a/packages/react-native/React/Fabric/RCTScheduler.mm b/packages/react-native/React/Fabric/RCTScheduler.mm index 5130b2fdcddc..f12a7715461a 100644 --- a/packages/react-native/React/Fabric/RCTScheduler.mm +++ b/packages/react-native/React/Fabric/RCTScheduler.mm @@ -8,6 +8,7 @@ #import "RCTScheduler.h" #import +#import #import #import #import @@ -144,11 +145,24 @@ @interface RCTAnimationChoreographerDisplayLinkTarget : NSObject - (void)displayLinkTick:(CADisplayLink *)sender; @end -@implementation RCTAnimationChoreographerDisplayLinkTarget +@implementation RCTAnimationChoreographerDisplayLinkTarget { + RCTAdaptiveFrameRateController *_frameRateController; +} + +- (instancetype)init +{ + if (self = [super init]) { + _frameRateController = [RCTAdaptiveFrameRateController new]; + } + return self; +} + - (void)displayLinkTick:(CADisplayLink *)sender { if (_choreographer != nullptr) { + CFTimeInterval workStart = CACurrentMediaTime(); _choreographer->onAnimationFrame(std::chrono::duration(sender.targetTimestamp)); + [_frameRateController recordFrameWorkFrom:workStart to:CACurrentMediaTime() forDisplayLink:sender]; } } @end diff --git a/packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.mm b/packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.mm index 71a3c8d9e147..9f635d491d65 100644 --- a/packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.mm +++ b/packages/react-native/ReactApple/RCTAnimatedModuleProvider/RCTAnimatedModuleProvider.mm @@ -15,6 +15,7 @@ #import #else #import +#import #import #endif @@ -27,6 +28,7 @@ @implementation RCTAnimatedModuleProvider { RCTPlatformDisplayLink *_displayLink; #else CADisplayLink *_displayLink; + RCTAdaptiveFrameRateController *_frameRateController; #endif std::function _onRender; @@ -44,6 +46,9 @@ - (instancetype)initWithSkipFramesDuringForegroundTransition:(BOOL)skipFramesDur { if (self = [super init]) { _skipFramesDuringForegroundTransition = skipFramesDuringForegroundTransition; +#if !TARGET_OS_OSX + _frameRateController = [RCTAdaptiveFrameRateController new]; +#endif } return self; } @@ -90,7 +95,15 @@ - (void)_onDisplayLinkTick auto strongProvider = _nativeAnimatedNodesManagerProvider.lock(); if (strongProvider != nullptr && _displayLink != nullptr && _onRender != nullptr) { +#if TARGET_OS_OSX _onRender(); +#else + // _onRender can stop rendering synchronously, which clears _displayLink. + CADisplayLink *displayLink = _displayLink; + CFTimeInterval workStart = CACurrentMediaTime(); + _onRender(); + [_frameRateController recordFrameWorkFrom:workStart to:CACurrentMediaTime() forDisplayLink:displayLink]; +#endif } } diff --git a/packages/rn-tester/RNTester/Info.plist b/packages/rn-tester/RNTester/Info.plist index ce09c212a744..fbfbd1345932 100644 --- a/packages/rn-tester/RNTester/Info.plist +++ b/packages/rn-tester/RNTester/Info.plist @@ -2,6 +2,8 @@ + CADisableMinimumFrameDurationOnPhone + CFBundleDevelopmentRegion en CFBundleExecutable diff --git a/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj b/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj index f0a5f5305351..610aa7bb98ea 100644 --- a/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj +++ b/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ 79B29C2E2E607A99007612A5 /* SceneDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 79B29C2D2E607A99007612A5 /* SceneDelegate.mm */; }; 7F806D4D4D97AD6403558B35 /* RCTStatusBarAppearanceTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 67B0814814F7C07E196BC295 /* RCTStatusBarAppearanceTests.mm */; }; A8717727467541429C262C63 /* RCTTimingClockChangeTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = A8717727467541429C262C64 /* RCTTimingClockChangeTests.mm */; }; + A1F0C3D2E4B5968778695A4B /* RCTAdaptiveFrameRateTests.m in Sources */ = {isa = PBXBuildFile; fileRef = B2E1D4C3F5A6079889706B5C /* RCTAdaptiveFrameRateTests.m */; }; 8145AE06241172D900A3F8DA /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8145AE05241172D900A3F8DA /* LaunchScreen.storyboard */; }; 832F45BB2A8A6E1F0097B4E6 /* SwiftTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 832F45BA2A8A6E1F0097B4E6 /* SwiftTest.swift */; }; A975CA6C2C05EADF0043F72A /* RCTNetworkTaskTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A975CA6B2C05EADE0043F72A /* RCTNetworkTaskTests.m */; }; @@ -92,6 +93,7 @@ 5C60EB1B226440DB0018C04F /* AppDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = AppDelegate.mm; path = RNTester/AppDelegate.mm; sourceTree = ""; }; 67B0814814F7C07E196BC295 /* RCTStatusBarAppearanceTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RCTStatusBarAppearanceTests.mm; sourceTree = ""; }; A8717727467541429C262C64 /* RCTTimingClockChangeTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RCTTimingClockChangeTests.mm; sourceTree = ""; }; + B2E1D4C3F5A6079889706B5C /* RCTAdaptiveFrameRateTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTAdaptiveFrameRateTests.m; sourceTree = ""; }; 79B29C2C2E607A99007612A5 /* SceneDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SceneDelegate.h; path = RNTester/SceneDelegate.h; sourceTree = ""; }; 79B29C2D2E607A99007612A5 /* SceneDelegate.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = SceneDelegate.mm; path = RNTester/SceneDelegate.mm; sourceTree = ""; }; 8145AE05241172D900A3F8DA /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = LaunchScreen.storyboard; path = RNTester/LaunchScreen.storyboard; sourceTree = ""; }; @@ -324,6 +326,7 @@ E7DB20B322B2BAA4005AC45F /* RNTesterUnitTestsBundle.js */, 67B0814814F7C07E196BC295 /* RCTStatusBarAppearanceTests.mm */, A8717727467541429C262C64 /* RCTTimingClockChangeTests.mm */, + B2E1D4C3F5A6079889706B5C /* RCTAdaptiveFrameRateTests.m */, ); path = RNTesterUnitTests; sourceTree = ""; @@ -645,6 +648,7 @@ 602160B9478DAFF2BAB56595 /* RCTUtilsTests.m in Sources */, 7F806D4D4D97AD6403558B35 /* RCTStatusBarAppearanceTests.mm in Sources */, A8717727467541429C262C63 /* RCTTimingClockChangeTests.mm in Sources */, + A1F0C3D2E4B5968778695A4B /* RCTAdaptiveFrameRateTests.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/packages/rn-tester/RNTesterUnitTests/RCTAdaptiveFrameRateTests.m b/packages/rn-tester/RNTesterUnitTests/RCTAdaptiveFrameRateTests.m new file mode 100644 index 000000000000..89046d261da3 --- /dev/null +++ b/packages/rn-tester/RNTesterUnitTests/RCTAdaptiveFrameRateTests.m @@ -0,0 +1,315 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +#import + +#define RCTAssertRange(actual, expectedMinimum, expectedMaximum, expectedPreferred) \ + do { \ + CAFrameRateRange range = (actual); \ + XCTAssertEqual(range.minimum, (expectedMinimum)); \ + XCTAssertEqual(range.maximum, (expectedMaximum)); \ + XCTAssertEqual(range.preferred, (expectedPreferred)); \ + } while (0) + +// Binary fractions, so that frame times add up exactly at the 250 ms pause boundary. +static const NSTimeInterval RCTFastFrameWork = 1.0 / 256; +static const NSTimeInterval RCTSlowFrameWork = 1.0 / 16; + +@interface RCTSpyDisplayLink : CADisplayLink +@property (nonatomic, readonly) NSUInteger rangeWriteCount; +@end + +@implementation RCTSpyDisplayLink { + CAFrameRateRange _preferredFrameRateRange; +} + +- (CAFrameRateRange)preferredFrameRateRange +{ + return _preferredFrameRateRange; +} + +- (void)setPreferredFrameRateRange:(CAFrameRateRange)preferredFrameRateRange +{ + _preferredFrameRateRange = preferredFrameRateRange; + _rangeWriteCount++; +} + +@end + +@interface RCTAdaptiveFrameRateTests : XCTestCase +@end + +@implementation RCTAdaptiveFrameRateTests + +static void RCTRecordSamples(RCTAdaptiveFrameRate *frameRate, NSTimeInterval seconds, NSUInteger count) +{ + for (NSUInteger i = 0; i < count; i++) { + [frameRate recordFrameWorkDuration:seconds]; + } +} + +static CAFrameRateRange RCTRangeAfterAveraging(NSTimeInterval seconds) +{ + RCTAdaptiveFrameRate *frameRate = [[RCTAdaptiveFrameRate alloc] initWithMaximumFramesPerSecond:120]; + RCTRecordSamples(frameRate, seconds, 4); + return frameRate.currentRange; +} + +// Each frame starts as the previous one ends. Returns when the last one ended. +static CFTimeInterval RCTRecordFrames( + RCTAdaptiveFrameRateController *controller, + CADisplayLink *displayLink, + CFTimeInterval startTime, + NSTimeInterval workDuration, + NSUInteger count) +{ + CFTimeInterval time = startTime; + for (NSUInteger i = 0; i < count; i++) { + [controller recordFrameWorkFrom:time to:time + workDuration forDisplayLink:displayLink]; + time += workDuration; + } + return time; +} + +- (void)testStartsAtScreenMaximum +{ + RCTAdaptiveFrameRate *frameRate = [[RCTAdaptiveFrameRate alloc] initWithMaximumFramesPerSecond:120]; + + RCTAssertRange(frameRate.currentRange, 60, 120, 120); +} + +- (void)testFastWorkStaysAtScreenMaximum +{ + RCTAdaptiveFrameRate *frameRate = [[RCTAdaptiveFrameRate alloc] initWithMaximumFramesPerSecond:120]; + + RCTRecordSamples(frameRate, 0.004, 4); + + RCTAssertRange(frameRate.currentRange, 60, 120, 120); +} + +- (void)testWorkOverScreenBudgetDropsTo60 +{ + RCTAdaptiveFrameRate *frameRate = [[RCTAdaptiveFrameRate alloc] initWithMaximumFramesPerSecond:120]; + + RCTRecordSamples(frameRate, 0.010, 4); + + RCTAssertRange(frameRate.currentRange, 30, 60, 60); +} + +- (void)testWorkOver60BudgetDropsTo30 +{ + RCTAdaptiveFrameRate *frameRate = [[RCTAdaptiveFrameRate alloc] initWithMaximumFramesPerSecond:120]; + + RCTRecordSamples(frameRate, 0.020, 4); + + RCTAssertRange(frameRate.currentRange, 24, 30, 30); +} + +- (void)testWorkOver30BudgetDropsTo24 +{ + RCTAdaptiveFrameRate *frameRate = [[RCTAdaptiveFrameRate alloc] initWithMaximumFramesPerSecond:120]; + + RCTRecordSamples(frameRate, 0.050, 4); + + RCTAssertRange(frameRate.currentRange, 10, 24, 24); +} + +- (void)testWorkAtScreenBudgetDropsTo60 +{ + RCTAssertRange(RCTRangeAfterAveraging(1.0 / 120 - 1e-6), 60, 120, 120); + RCTAssertRange(RCTRangeAfterAveraging(1.0 / 120), 30, 60, 60); + RCTAssertRange(RCTRangeAfterAveraging(1.0 / 120 + 1e-6), 30, 60, 60); +} + +- (void)testWorkAt60BudgetDropsTo30 +{ + RCTAssertRange(RCTRangeAfterAveraging(1.0 / 60 - 1e-6), 30, 60, 60); + RCTAssertRange(RCTRangeAfterAveraging(1.0 / 60), 24, 30, 30); + RCTAssertRange(RCTRangeAfterAveraging(1.0 / 60 + 1e-6), 24, 30, 30); +} + +- (void)testWorkAt30BudgetDropsTo24 +{ + RCTAssertRange(RCTRangeAfterAveraging(1.0 / 30 - 1e-6), 24, 30, 30); + RCTAssertRange(RCTRangeAfterAveraging(1.0 / 30), 10, 24, 24); + RCTAssertRange(RCTRangeAfterAveraging(1.0 / 30 + 1e-6), 10, 24, 24); +} + +- (void)testFewerThanFourSamplesKeepRange +{ + RCTAdaptiveFrameRate *frameRate = [[RCTAdaptiveFrameRate alloc] initWithMaximumFramesPerSecond:120]; + + for (NSUInteger i = 0; i < 3; i++) { + XCTAssertFalse([frameRate recordFrameWorkDuration:0.050]); + } + + RCTAssertRange(frameRate.currentRange, 60, 120, 120); +} + +- (void)testAveragesTheLastFourSamples +{ + RCTAdaptiveFrameRate *frameRate = [[RCTAdaptiveFrameRate alloc] initWithMaximumFramesPerSecond:120]; + + RCTRecordSamples(frameRate, 0.004, 3); + [frameRate recordFrameWorkDuration:0.020]; + + // (3 * 0.004 + 0.020) / 4 = 0.008, inside the 1/120 s budget. + RCTAssertRange(frameRate.currentRange, 60, 120, 120); + + [frameRate recordFrameWorkDuration:0.020]; + + // (2 * 0.004 + 2 * 0.020) / 4 = 0.012, over the 1/120 s budget. + RCTAssertRange(frameRate.currentRange, 30, 60, 60); +} + +- (void)testRecoversWhenWorkGetsFast +{ + RCTAdaptiveFrameRate *frameRate = [[RCTAdaptiveFrameRate alloc] initWithMaximumFramesPerSecond:120]; + RCTRecordSamples(frameRate, 0.050, 4); + + RCTRecordSamples(frameRate, 0.004, 4); + + RCTAssertRange(frameRate.currentRange, 60, 120, 120); +} + +- (void)testReturnsYesOnlyWhenRangeChanges +{ + RCTAdaptiveFrameRate *frameRate = [[RCTAdaptiveFrameRate alloc] initWithMaximumFramesPerSecond:120]; + + RCTRecordSamples(frameRate, 0.010, 3); + XCTAssertTrue([frameRate recordFrameWorkDuration:0.010]); + XCTAssertFalse([frameRate recordFrameWorkDuration:0.010]); + + // (3 * 0.010 + 0.001) / 4 = 0.00775, back inside the 1/120 s budget. + XCTAssertTrue([frameRate recordFrameWorkDuration:0.001]); + XCTAssertFalse([frameRate recordFrameWorkDuration:0.001]); +} + +- (void)testTopTierFollowsScreenMaximum +{ + RCTAdaptiveFrameRate *frameRate = [[RCTAdaptiveFrameRate alloc] initWithMaximumFramesPerSecond:90]; + + RCTAssertRange(frameRate.currentRange, 60, 90, 90); +} + +- (void)testTopTierBudgetFollowsScreenMaximum +{ + RCTAdaptiveFrameRate *at90Hz = [[RCTAdaptiveFrameRate alloc] initWithMaximumFramesPerSecond:90]; + RCTAdaptiveFrameRate *at120Hz = [[RCTAdaptiveFrameRate alloc] initWithMaximumFramesPerSecond:120]; + + RCTRecordSamples(at90Hz, 0.010, 4); + RCTRecordSamples(at120Hz, 0.010, 4); + + RCTAssertRange(at90Hz.currentRange, 60, 90, 90); + RCTAssertRange(at120Hz.currentRange, 30, 60, 60); +} + +- (void)testMaximumBelow60IsTreatedAs60 +{ + RCTAdaptiveFrameRate *frameRate = [[RCTAdaptiveFrameRate alloc] initWithMaximumFramesPerSecond:0]; + RCTAssertRange(frameRate.currentRange, 60, 60, 60); +} + +- (void)testFactoryAdaptsOnlyAbove60Hz +{ + XCTAssertNil([RCTAdaptiveFrameRate adaptiveFrameRateForMaximumFramesPerSecond:30]); + XCTAssertNil([RCTAdaptiveFrameRate adaptiveFrameRateForMaximumFramesPerSecond:60]); + XCTAssertNotNil([RCTAdaptiveFrameRate adaptiveFrameRateForMaximumFramesPerSecond:61]); + RCTAssertRange([RCTAdaptiveFrameRate adaptiveFrameRateForMaximumFramesPerSecond:120].currentRange, 60, 120, 120); +} + +- (void)testControllerWritesRangeOnlyWhenItChanges +{ + RCTAdaptiveFrameRateController *controller = + [[RCTAdaptiveFrameRateController alloc] initWithMaximumFramesPerSecond:120]; + RCTSpyDisplayLink *displayLink = [RCTSpyDisplayLink new]; + + CFTimeInterval time = RCTRecordFrames(controller, displayLink, 0, RCTFastFrameWork, 8); + + RCTAssertRange(displayLink.preferredFrameRateRange, 60, 120, 120); + XCTAssertEqual(displayLink.rangeWriteCount, 1u); + + RCTRecordFrames(controller, displayLink, time, 0.010, 8); + + RCTAssertRange(displayLink.preferredFrameRateRange, 30, 60, 60); + XCTAssertEqual(displayLink.rangeWriteCount, 2u); +} + +- (void)testControllerLeavesDisplayLinkAloneAt60Hz +{ + RCTAdaptiveFrameRateController *controller = + [[RCTAdaptiveFrameRateController alloc] initWithMaximumFramesPerSecond:60]; + RCTSpyDisplayLink *displayLink = [RCTSpyDisplayLink new]; + + RCTRecordFrames(controller, displayLink, 0, RCTSlowFrameWork, 8); + + XCTAssertEqual(displayLink.rangeWriteCount, 0u); +} + +- (void)testControllerStartsOverForNewDisplayLink +{ + RCTAdaptiveFrameRateController *controller = + [[RCTAdaptiveFrameRateController alloc] initWithMaximumFramesPerSecond:120]; + RCTSpyDisplayLink *previousDisplayLink = [RCTSpyDisplayLink new]; + RCTSpyDisplayLink *displayLink = [RCTSpyDisplayLink new]; + CFTimeInterval time = RCTRecordFrames(controller, previousDisplayLink, 0, RCTSlowFrameWork, 4); + RCTAssertRange(previousDisplayLink.preferredFrameRateRange, 10, 24, 24); + + time = RCTRecordFrames(controller, displayLink, time, RCTFastFrameWork, 1); + + RCTAssertRange(displayLink.preferredFrameRateRange, 60, 120, 120); + XCTAssertEqual(displayLink.rangeWriteCount, 1u); + + RCTRecordFrames(controller, displayLink, time, RCTFastFrameWork, 3); + + XCTAssertEqual(displayLink.rangeWriteCount, 1u); +} + +- (void)testControllerStartsOverAfterPause +{ + RCTAdaptiveFrameRateController *controller = + [[RCTAdaptiveFrameRateController alloc] initWithMaximumFramesPerSecond:120]; + RCTSpyDisplayLink *displayLink = [RCTSpyDisplayLink new]; + CFTimeInterval pauseStart = RCTRecordFrames(controller, displayLink, 0, RCTSlowFrameWork, 4); + RCTAssertRange(displayLink.preferredFrameRateRange, 10, 24, 24); + + RCTRecordFrames(controller, displayLink, pauseStart + 0.25 + 1.0 / 1024, RCTFastFrameWork, 1); + + RCTAssertRange(displayLink.preferredFrameRateRange, 60, 120, 120); +} + +- (void)testControllerKeepsSamplesAcross250msPause +{ + RCTAdaptiveFrameRateController *controller = + [[RCTAdaptiveFrameRateController alloc] initWithMaximumFramesPerSecond:120]; + RCTSpyDisplayLink *displayLink = [RCTSpyDisplayLink new]; + CFTimeInterval pauseStart = RCTRecordFrames(controller, displayLink, 0, RCTSlowFrameWork, 4); + + RCTRecordFrames(controller, displayLink, pauseStart + 0.25, RCTFastFrameWork, 1); + + // (3 * 62.5 ms + 3.9 ms) / 4 = 47.8 ms, still over the 1/30 s budget. + RCTAssertRange(displayLink.preferredFrameRateRange, 10, 24, 24); +} + +- (void)testControllerDoesNotKeepDisplayLinkAlive +{ + RCTAdaptiveFrameRateController *controller = + [[RCTAdaptiveFrameRateController alloc] initWithMaximumFramesPerSecond:120]; + __weak RCTSpyDisplayLink *weakDisplayLink; + + @autoreleasepool { + RCTSpyDisplayLink *displayLink = [RCTSpyDisplayLink new]; + weakDisplayLink = displayLink; + RCTRecordFrames(controller, displayLink, 0, RCTFastFrameWork, 1); + } + + XCTAssertNil(weakDisplayLink); +} + +@end diff --git a/private/helloworld/ios/HelloWorld/Info.plist b/private/helloworld/ios/HelloWorld/Info.plist index 032a0c493ca0..f0717e4781a5 100644 --- a/private/helloworld/ios/HelloWorld/Info.plist +++ b/private/helloworld/ios/HelloWorld/Info.plist @@ -2,6 +2,8 @@ + CADisableMinimumFrameDurationOnPhone + CFBundleDevelopmentRegion en CFBundleDisplayName