Skip to content

Commit e34e4dd

Browse files
[CP-beta][iPhone Duo] Use the correct UIScreen to report the display size and update when fold/unfold (#195690)
This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request) Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request. ### Issue Link: What is the link to the issue this cherry-pick is addressing? #195177 ### Impact Description: What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Wrong display size being sent to dart side on iPhone Duo's inner display. This can cause rendering issues and accessibility issues. Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping of production apps (the app crashes on launch). This information is for domain experts and release engineers to understand the consequences of saying yes or no to the cherry pick. Impact production apps that runs on iPhone Duo's inner display. ### Changelog Description: Explain this cherry pick: * In one line that is accessible to most Flutter developers. * That describes the state prior to the fix. * That includes which platforms are impacted. See [best practices](https://github.com/flutter/flutter/blob/main/docs/releases/Hotfix-Documentation-Best-Practices.md) for examples. [flutter/195177] When running on iPhone Duo, Report the correct display size to dart side when switching between inner and outer displays. ### Workaround: Is there a workaround for this issue? No ### Risk: What is the risk level of this cherry-pick? ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: What are the steps to validate that this fix works? Test on [this sample app](#195177 (comment)) using local engine on iPhone Duo simulator.
1 parent 6da77a5 commit e34e4dd

8 files changed

Lines changed: 255 additions & 5 deletions

File tree

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine+Test.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class ThreadHost;
3838
NS_SWIFT_UI_ACTOR;
3939
- (const flutter::ThreadHost&)threadHost;
4040
- (void)updateDisplays;
41+
- (void)sendDisplayWithScreenNativeSize:(CGSize)screenNativeSize scale:(CGFloat)scale;
4142
- (void)flutterTextInputView:(FlutterTextInputView*)textInputView
4243
performAction:(FlutterTextInputAction)action
4344
withClient:(int)client;

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ @implementation FlutterEngine {
205205

206206
FlutterFMLTaskRunner* _platformTaskRunnerWrapper;
207207
FlutterFMLTaskRunner* _rasterTaskRunnerWrapper;
208+
209+
// The display information last sent to the framework by `updateDisplays`.
210+
BOOL _hasSentDisplays;
211+
CGSize _lastSentScreenNativeSize;
212+
CGFloat _lastSentScreenScale;
208213
}
209214

210215
- (int64_t)engineIdentifier {
@@ -853,6 +858,8 @@ - (void)launchEngine:(NSString*)entrypoint
853858
- (void)adoptShell:(std::unique_ptr<flutter::Shell>)shell {
854859
FML_DCHECK(shell);
855860
_shell = std::move(shell);
861+
// A new shell has not received any display information yet.
862+
_hasSentDisplays = NO;
856863
_platformTaskRunnerWrapper = [[FlutterFMLTaskRunner alloc]
857864
initWithTaskRunner:_shell->GetTaskRunners().GetPlatformTaskRunner()];
858865
_rasterTaskRunnerWrapper = [[FlutterFMLTaskRunner alloc]
@@ -1046,13 +1053,37 @@ - (void)updateDisplays {
10461053
// Tests may do this.
10471054
return;
10481055
}
1056+
// Prefer the screen that the view is on. Fallback to main screen if not available (e.g. the
1057+
// Flutter view hasn't been added to any screen).
1058+
UIScreen* screen = self.viewController.viewIfLoaded.window.windowScene.screen;
1059+
if (!screen) {
1060+
if (_hasSentDisplays) {
1061+
// Keep the last reported display, rather than regressing to a guess.
1062+
return;
1063+
}
1064+
// The framework can read the display information as soon as the isolate launches, before any
1065+
// view is on screen (e.g. a headless or pre-warmed engine). Use `UIScreen.mainScreen` as a
1066+
// best-effort guess until the view is in a window.
1067+
screen = UIScreen.mainScreen;
1068+
}
1069+
CGSize screen_size = screen.nativeBounds.size;
1070+
CGFloat scale = screen.scale;
1071+
if (_hasSentDisplays && CGSizeEqualToSize(screen_size, _lastSentScreenNativeSize) &&
1072+
scale == _lastSentScreenScale) {
1073+
return;
1074+
}
1075+
_hasSentDisplays = YES;
1076+
_lastSentScreenNativeSize = screen_size;
1077+
_lastSentScreenScale = scale;
1078+
[self sendDisplayWithScreenNativeSize:screen_size scale:scale];
1079+
}
1080+
1081+
- (void)sendDisplayWithScreenNativeSize:(CGSize)screenNativeSize scale:(CGFloat)scale {
10491082
auto vsync_waiter = _shell->GetVsyncWaiter().lock();
10501083
auto vsync_waiter_ios = std::static_pointer_cast<flutter::VsyncWaiterIOS>(vsync_waiter);
10511084
std::vector<std::unique_ptr<flutter::Display>> displays;
1052-
auto screen_size = UIScreen.mainScreen.nativeBounds.size;
1053-
auto scale = UIScreen.mainScreen.scale;
10541085
displays.push_back(std::make_unique<flutter::VariableRefreshRateDisplay>(
1055-
0, vsync_waiter_ios, screen_size.width, screen_size.height, scale));
1086+
0, vsync_waiter_ios, screenNativeSize.width, screenNativeSize.height, scale));
10561087
_shell->OnDisplayUpdates(std::move(displays));
10571088
}
10581089

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@
4343
private:
4444
TestPlatformMessageResponse() = default;
4545
};
46+
47+
// Returns a mock view controller whose view is in a window on a screen with the given native size
48+
// and scale.
49+
id CreateMockViewControllerOnScreen(CGSize screenNativeSize, CGFloat screenScale) {
50+
UIScreen* screen = OCMClassMock([UIScreen class]);
51+
OCMStub([screen nativeBounds]).andDo(^(NSInvocation* invocation) {
52+
CGRect nativeBounds = CGRectMake(0, 0, screenNativeSize.width, screenNativeSize.height);
53+
[invocation setReturnValue:&nativeBounds];
54+
});
55+
OCMStub([screen scale]).andDo(^(NSInvocation* invocation) {
56+
CGFloat scale = screenScale;
57+
[invocation setReturnValue:&scale];
58+
});
59+
UIWindowScene* windowScene = OCMClassMock([UIWindowScene class]);
60+
OCMStub([windowScene screen]).andReturn(screen);
61+
UIWindow* window = OCMClassMock([UIWindow class]);
62+
OCMStub([window windowScene]).andReturn(windowScene);
63+
UIView* view = OCMClassMock([UIView class]);
64+
OCMStub([view window]).andReturn(window);
65+
FlutterViewController* viewController = OCMClassMock([FlutterViewController class]);
66+
OCMStub([viewController viewIfLoaded]).andReturn(view);
67+
return viewController;
68+
}
4669
} // namespace
4770

4871
/// A `FlutterTexture` that vends no frames. Used to obtain a registered texture id.
@@ -523,6 +546,109 @@ - (void)testFlutterEngineUpdatesDisplays {
523546
OCMVerify(times(2), [mockEngine updateDisplays]);
524547
}
525548

549+
- (void)testUpdateDisplaysFallsBackToMainScreenBeforeViewIsInWindow {
550+
FlutterEngine* engine = [[FlutterEngine alloc] init];
551+
id mockEngine = OCMPartialMock(engine);
552+
553+
// The framework can read the display as soon as the isolate launches, before any view controller
554+
// is attached, so `run` must send a display even though there is no screen to ask yet.
555+
[engine run];
556+
557+
CGSize mainScreenNativeSize = UIScreen.mainScreen.nativeBounds.size;
558+
CGFloat mainScreenScale = UIScreen.mainScreen.scale;
559+
OCMVerify(times(1), [mockEngine sendDisplayWithScreenNativeSize:mainScreenNativeSize
560+
scale:mainScreenScale]);
561+
}
562+
563+
- (void)testUpdateDisplaysUsesScreenOfViewWindow {
564+
FlutterEngine* engine = [[FlutterEngine alloc] init];
565+
id mockEngine = OCMPartialMock(engine);
566+
__block id viewController = nil;
567+
OCMStub([mockEngine viewController]).andDo(^(NSInvocation* invocation) {
568+
__unsafe_unretained id value = viewController;
569+
[invocation setReturnValue:&value];
570+
});
571+
[engine run];
572+
573+
viewController = CreateMockViewControllerOnScreen(CGSizeMake(1001, 2002), 3);
574+
[engine updateDisplays];
575+
576+
OCMVerify(times(1), [mockEngine sendDisplayWithScreenNativeSize:CGSizeMake(1001, 2002) scale:3]);
577+
}
578+
579+
- (void)testUpdateDisplaysDoesNotResendUnchangedDisplay {
580+
FlutterEngine* engine = [[FlutterEngine alloc] init];
581+
id mockEngine = OCMPartialMock(engine);
582+
__block id viewController = nil;
583+
OCMStub([mockEngine viewController]).andDo(^(NSInvocation* invocation) {
584+
__unsafe_unretained id value = viewController;
585+
[invocation setReturnValue:&value];
586+
});
587+
[engine run];
588+
589+
viewController = CreateMockViewControllerOnScreen(CGSizeMake(1001, 2002), 3);
590+
[engine updateDisplays];
591+
[engine updateDisplays];
592+
// A different view controller on a screen with the same size and scale.
593+
viewController = CreateMockViewControllerOnScreen(CGSizeMake(1001, 2002), 3);
594+
[engine updateDisplays];
595+
596+
OCMVerify(times(1), [mockEngine sendDisplayWithScreenNativeSize:CGSizeMake(1001, 2002) scale:3]);
597+
}
598+
599+
- (void)testUpdateDisplaysResendsWhenScreenChanges {
600+
FlutterEngine* engine = [[FlutterEngine alloc] init];
601+
id mockEngine = OCMPartialMock(engine);
602+
__block id viewController = nil;
603+
OCMStub([mockEngine viewController]).andDo(^(NSInvocation* invocation) {
604+
__unsafe_unretained id value = viewController;
605+
[invocation setReturnValue:&value];
606+
});
607+
[engine run];
608+
609+
// E.g. moving between the outer and inner displays of a device with multiple built-in displays.
610+
viewController = CreateMockViewControllerOnScreen(CGSizeMake(1001, 2002), 3);
611+
[engine updateDisplays];
612+
viewController = CreateMockViewControllerOnScreen(CGSizeMake(3003, 4004), 3);
613+
[engine updateDisplays];
614+
viewController = CreateMockViewControllerOnScreen(CGSizeMake(1001, 2002), 3);
615+
[engine updateDisplays];
616+
// Same size, different scale.
617+
viewController = CreateMockViewControllerOnScreen(CGSizeMake(1001, 2002), 2);
618+
[engine updateDisplays];
619+
620+
OCMVerify(times(2), [mockEngine sendDisplayWithScreenNativeSize:CGSizeMake(1001, 2002) scale:3]);
621+
OCMVerify(times(1), [mockEngine sendDisplayWithScreenNativeSize:CGSizeMake(3003, 4004) scale:3]);
622+
OCMVerify(times(1), [mockEngine sendDisplayWithScreenNativeSize:CGSizeMake(1001, 2002) scale:2]);
623+
}
624+
625+
- (void)testUpdateDisplaysKeepsLastDisplayWhenViewIsNotInWindow {
626+
FlutterEngine* engine = [[FlutterEngine alloc] init];
627+
id mockEngine = OCMPartialMock(engine);
628+
__block id viewController = nil;
629+
OCMStub([mockEngine viewController]).andDo(^(NSInvocation* invocation) {
630+
__unsafe_unretained id value = viewController;
631+
[invocation setReturnValue:&value];
632+
});
633+
[engine run];
634+
635+
viewController = CreateMockViewControllerOnScreen(CGSizeMake(1001, 2002), 3);
636+
[engine updateDisplays];
637+
// A view controller whose view is not loaded (e.g. a newly attached view controller).
638+
viewController = OCMClassMock([FlutterViewController class]);
639+
[engine updateDisplays];
640+
// No view controller.
641+
viewController = nil;
642+
[engine updateDisplays];
643+
644+
// Only the initial display (from `run`) used the `UIScreen.mainScreen` fallback.
645+
CGSize mainScreenNativeSize = UIScreen.mainScreen.nativeBounds.size;
646+
CGFloat mainScreenScale = UIScreen.mainScreen.scale;
647+
OCMVerify(times(1), [mockEngine sendDisplayWithScreenNativeSize:mainScreenNativeSize
648+
scale:mainScreenScale]);
649+
OCMVerify(times(1), [mockEngine sendDisplayWithScreenNativeSize:CGSizeMake(1001, 2002) scale:3]);
650+
}
651+
526652
- (void)testLifeCycleNotificationDidEnterBackgroundForApplication {
527653
FlutterDartProject* project = [[FlutterDartProject alloc] init];
528654
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar" project:project];

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ NS_ASSUME_NONNULL_BEGIN
132132
- (void)sendDeepLinkToFramework:(NSURL*)url completionHandler:(void (^)(BOOL success))completion;
133133

134134
- (void)onStatusBarTap;
135+
136+
/**
137+
* Sends display information for the screen that the view controller's view is on, if it differs
138+
* from the display last sent to the framework.
139+
*
140+
* If the view isn't in a window, keeps the last display sent. If no display has been sent yet,
141+
* sends `UIScreen.mainScreen` as a best-effort initial display.
142+
*/
143+
- (void)updateDisplays;
135144
@end
136145

137146
@interface FlutterImplicitEngineBridgeImpl : NSObject <FlutterImplicitEngineBridge>

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,7 @@ - (void)viewDidLayoutSubviews {
13441344
[self checkAndUpdateAutoResizeConstraints];
13451345
[self setViewportMetricsPaddings];
13461346
[self updateViewportMetricsIfNeeded];
1347+
[self updateEngineDisplays];
13471348

13481349
// There is no guarantee that UIKit will layout subviews when the application/scene is in the
13491350
// foreground. Creating the surface when backgrounded will cause app termination, so in that
@@ -1355,6 +1356,16 @@ - (void)viewDidLayoutSubviews {
13551356
}
13561357
}
13571358

1359+
// Asks the engine to send updated display information to the framework, e.g. when the view is first
1360+
// placed in a window, or moves between the inner and outer displays of a device with multiple
1361+
// built-in displays. The engine skips the update if the display information has not changed.
1362+
- (void)updateEngineDisplays {
1363+
if (self.engine.viewController != self) {
1364+
return;
1365+
}
1366+
[self.engine updateDisplays];
1367+
}
1368+
13581369
- (BOOL)isAutoResizable {
13591370
return self.flutterView.autoResizable;
13601371
}

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,33 @@ - (void)testUpdatedViewportMetricsDoesNotResizeFlutterViewWhenNotAutoResizable {
14151415
OCMVerifyAll(mockVC);
14161416
}
14171417

1418+
- (void)testViewDidLayoutSubviewsUpdatesEngineDisplays {
1419+
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
1420+
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
1421+
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
1422+
nibName:nil
1423+
bundle:nil];
1424+
1425+
// Layout is when the view may have moved to a different screen (e.g. between the inner and outer
1426+
// displays of a device with multiple built-in displays).
1427+
OCMExpect([mockEngine updateDisplays]);
1428+
[viewController viewDidLayoutSubviews];
1429+
OCMVerifyAll((id)mockEngine);
1430+
}
1431+
1432+
- (void)testViewDidLayoutSubviewsDoesNotUpdateEngineDisplaysWhenNotEngineViewController {
1433+
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
1434+
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
1435+
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
1436+
nibName:nil
1437+
bundle:nil];
1438+
mockEngine.viewController = nil;
1439+
1440+
OCMReject([mockEngine updateDisplays]);
1441+
[viewController viewDidLayoutSubviews];
1442+
OCMVerifyAll((id)mockEngine);
1443+
}
1444+
14181445
- (void)testUpdateViewportMetricsIfNeeded_DoesNotInvokeEngineWhenShouldBeIgnoredDuringRotation {
14191446
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
14201447
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,8 +1026,11 @@ - (CGRect)accessibilityFrame {
10261026
// Even if it's marked accessible, VoiceControl labels will not appear if it's too
10271027
// spatially distant. Set the frame to be the max screen size so all children are guaraenteed
10281028
// to be contained.
1029-
1030-
return UIScreen.mainScreen.bounds;
1029+
//
1030+
// Prefer the screen that the view is on. Fallback to main screen if not available (e.g. the
1031+
// Flutter view hasn't been added to any screen).
1032+
UIScreen* screen = self.semanticsObject.bridgeView.window.windowScene.screen;
1033+
return (screen ?: UIScreen.mainScreen).bounds;
10311034
}
10321035

10331036
- (id)accessibilityContainer {

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,48 @@ - (void)testSemanticsObjectContainerAccessibilityFrameCoversEntireScreen {
799799
XCTAssertTrue(CGRectEqualToRect(container.accessibilityFrame, UIScreen.mainScreen.bounds));
800800
}
801801

802+
- (void)testSemanticsObjectContainerAccessibilityFrameUsesScreenOfBridgeViewWindow {
803+
flutter::testing::MockAccessibilityBridge* mock = new flutter::testing::MockAccessibilityBridge();
804+
mock->isVoiceOverRunningValue = true;
805+
fml::WeakPtrFactory<flutter::AccessibilityBridgeIos> factory(mock);
806+
fml::WeakPtr<flutter::AccessibilityBridgeIos> bridge = factory.GetWeakPtr();
807+
808+
flutter::SemanticsNode parent;
809+
parent.id = 0;
810+
811+
flutter::SemanticsNode child;
812+
child.id = 1;
813+
child.rect = SkRect::MakeXYWH(0, 0, 100, 100);
814+
parent.childrenInTraversalOrder.push_back(1);
815+
816+
FlutterSemanticsObject* parentObject = [[FlutterSemanticsObject alloc] initWithBridge:bridge
817+
uid:0];
818+
[parentObject setSemanticsNode:&parent];
819+
FlutterSemanticsObject* childObject = [[FlutterSemanticsObject alloc] initWithBridge:bridge
820+
uid:1];
821+
[childObject setSemanticsNode:&child];
822+
parentObject.children = @[ childObject ];
823+
824+
// Put the bridge view's window on a screen that is not `UIScreen.mainScreen` (e.g. the inner
825+
// display of a device with multiple built-in displays).
826+
CGRect screenBounds = CGRectMake(0, 0, 951, 669);
827+
UIScreen* mockScreen = OCMClassMock([UIScreen class]);
828+
OCMStub([mockScreen bounds]).andDo(^(NSInvocation* invocation) {
829+
CGRect bounds = screenBounds;
830+
[invocation setReturnValue:&bounds];
831+
});
832+
UIWindowScene* mockWindowScene = OCMClassMock([UIWindowScene class]);
833+
OCMStub([mockWindowScene screen]).andReturn(mockScreen);
834+
UIWindow* mockWindow = OCMPartialMock(mock->view().window);
835+
OCMStub([mockWindow windowScene]).andReturn(mockWindowScene);
836+
837+
SemanticsObjectContainer* container =
838+
static_cast<SemanticsObjectContainer*>(parentObject.accessibilityContainer);
839+
XCTAssertTrue(CGRectEqualToRect(container.accessibilityFrame, screenBounds));
840+
841+
[(id)mockWindow stopMocking];
842+
}
843+
802844
- (void)testRootSemanticsObjectContainerAccessibilityContainerIsBridgeView {
803845
flutter::testing::MockAccessibilityBridge* mock = new flutter::testing::MockAccessibilityBridge();
804846
mock->isVoiceOverRunningValue = true;

0 commit comments

Comments
 (0)