From 3b22bd4442c5e10c26e40c7b6a8bd9d851e471e4 Mon Sep 17 00:00:00 2001 From: Callum Moffat Date: Tue, 25 Jul 2023 17:29:13 -0400 Subject: [PATCH] Uncap framerate for `iOSAppOnMac` (flutter/engine#43840) Right now framerate when platform views are on screen is capped to 80 fps on iOS devices. I tried uncapping the framerate and ran the test app in https://github.com/flutter/flutter/issues/116640 on my M1 Max MBP and didn't notice any of the same slowdowns. `iOSAppOnMac` is only available on Apple Silicon Macs, so the performance baseline is really high. I think it's safe to remove the cap here. ### 120 Hz / ProMotion minimum specs | Category | Device | SoC | CPU | GPU | |--|--|--|--|--| iOS | iPhone 13 Pro | A15 | 2P+4e | 5 EU | Mac with built-in ProMotion | MacBook Pro 2021 | M1 Pro | 6P+2e | 14 EU | Mac with external 120Hz display | MacBook Air 2020 | M1 | 4P+4e | 7 EU | [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../framework/Source/VsyncWaiterIosTest.mm | 31 +++++++++++++++++++ .../ios/framework/Source/vsync_waiter_ios.mm | 14 ++++++--- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/VsyncWaiterIosTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/VsyncWaiterIosTest.mm index dda84f9ac1..eace658519 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/VsyncWaiterIosTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/VsyncWaiterIosTest.mm @@ -187,6 +187,37 @@ fml::RefPtr CreateNewThread(std::string name) { } else { XCTAssertEqualWithAccuracy(link.preferredFramesPerSecond, maxFrameRate, 0.1); } + + if (@available(iOS 14.0, *)) { + // Fake response that we are running on Mac. + id processInfo = [NSProcessInfo processInfo]; + id processInfoPartialMock = OCMPartialMock(processInfo); + bool iOSAppOnMac = true; + [OCMStub([processInfoPartialMock isiOSAppOnMac]) andReturnValue:OCMOCK_VALUE(iOSAppOnMac)]; + + merger->MergeWithLease(5); + vsync_waiter.AwaitVSync(); + + // On Mac, framerate should be uncapped. + if (@available(iOS 15.0, *)) { + XCTAssertEqualWithAccuracy(link.preferredFrameRateRange.maximum, maxFrameRate, 0.1); + XCTAssertEqualWithAccuracy(link.preferredFrameRateRange.preferred, maxFrameRate, 0.1); + XCTAssertEqualWithAccuracy(link.preferredFrameRateRange.minimum, maxFrameRate / 2, 0.1); + } else { + XCTAssertEqualWithAccuracy(link.preferredFramesPerSecond, 80, 0.1); + } + + merger->UnMergeNowIfLastOne(); + vsync_waiter.AwaitVSync(); + + if (@available(iOS 15.0, *)) { + XCTAssertEqualWithAccuracy(link.preferredFrameRateRange.maximum, maxFrameRate, 0.1); + XCTAssertEqualWithAccuracy(link.preferredFrameRateRange.preferred, maxFrameRate, 0.1); + XCTAssertEqualWithAccuracy(link.preferredFrameRateRange.minimum, maxFrameRate / 2, 0.1); + } else { + XCTAssertEqualWithAccuracy(link.preferredFramesPerSecond, maxFrameRate, 0.1); + } + } } @end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.mm index 5ed238704c..3396d1fc70 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.mm @@ -44,10 +44,16 @@ void VsyncWaiterIOS::AwaitVSync() { if (fml::TaskRunnerChecker::RunsOnTheSameThread( task_runners_.GetRasterTaskRunner()->GetTaskQueueId(), task_runners_.GetPlatformTaskRunner()->GetTaskQueueId())) { - // Pressure tested on iPhone 13 pro, the oldest iPhone that supports refresh rate greater than - // 60fps. A flutter app can handle fast scrolling on 80 fps with 6 PlatformViews in the scene at - // the same time. - new_max_refresh_rate = 80; + BOOL isRunningOnMac = NO; + if (@available(iOS 14.0, *)) { + isRunningOnMac = [NSProcessInfo processInfo].iOSAppOnMac; + } + if (!isRunningOnMac) { + // Pressure tested on iPhone 13 pro, the oldest iPhone that supports refresh rate greater than + // 60fps. A flutter app can handle fast scrolling on 80 fps with 6 PlatformViews in the scene + // at the same time. + new_max_refresh_rate = 80; + } } if (fabs(new_max_refresh_rate - max_refresh_rate_) > kRefreshRateDiffToIgnore) { max_refresh_rate_ = new_max_refresh_rate;