diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterDisplayLink.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterDisplayLink.mm index 31b74ec36d..ba07304292 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterDisplayLink.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterDisplayLink.mm @@ -262,8 +262,11 @@ static NSString* const kFlutterDisplayLinkViewDidMoveToWindow = - (void)invalidate { @synchronized(self) { FML_DCHECK([NSThread isMainThread]); - [_view removeFromSuperview]; + // Unregister observer before removing the view to ensure + // that the viewDidChangeWindow notification is not received + // while in @synchronized block. [[NSNotificationCenter defaultCenter] removeObserver:self]; + [_view removeFromSuperview]; _view = nil; _delegate = nil; } diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterVSyncWaiterTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterVSyncWaiterTest.mm index 062452c60e..0244854962 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterVSyncWaiterTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterVSyncWaiterTest.mm @@ -140,7 +140,14 @@ TEST(FlutterVSyncWaiterTest, VSyncWorks) { CFRunLoopStop(CFRunLoopGetCurrent()); }]; + __block CFTimeInterval expectedStartUntil; + // Warm up tick is scheduled immediately in a scheduled block. Schedule another + // block here to determine the maximum time when the warm up tick should be + // scheduled. [waiter waitForVSync:kWarmUpBaton]; + [[NSRunLoop currentRunLoop] performBlock:^{ + expectedStartUntil = CACurrentMediaTime(); + }]; // Reference vsync to setup phase. CFTimeInterval now = CACurrentMediaTime(); @@ -166,15 +173,22 @@ TEST(FlutterVSyncWaiterTest, VSyncWorks) { // Vsync without baton should pause the display link. [displayLink tickWithTimestamp:now + 3.5 * displayLink.nominalOutputRefreshPeriod targetTimestamp:now + 5 * displayLink.nominalOutputRefreshPeriod]; - // Make sure to run the timer scheduled in display link callback. - CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.02, NO); + + CFTimeInterval start = CACurrentMediaTime(); + while (!displayLink.paused) { + // Make sure to run the timer scheduled in display link callback. + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.02, NO); + if (CACurrentMediaTime() - start > 1.0) { + break; + } + } ASSERT_TRUE(displayLink.paused); EXPECT_EQ(entries.size(), size_t(4)); // Warm up frame should be presented as soon as possible. - EXPECT_TRUE(fabs(entries[0].timestamp - now) < 0.005); - EXPECT_TRUE(fabs(entries[0].targetTimestamp - now) < 0.005); + EXPECT_TRUE(entries[0].timestamp <= expectedStartUntil); + EXPECT_TRUE(entries[0].targetTimestamp <= expectedStartUntil); EXPECT_EQ(entries[0].baton, kWarmUpBaton); EXPECT_DOUBLE_EQ(entries[1].timestamp, now + displayLink.nominalOutputRefreshPeriod);