Skip to content

Commit 3237222

Browse files
authored
[macOS] Implement merged UI and platform thread (#162883)
Original issue: flutter/flutter#150525 This PR lets the macOS embedder run both with and without UI and platform thread merged. Thread merging is controlled through `FLTEnableMergedPlatformUIThread` `info.plist` option similar to iOS embedder, though the default value is currently `false`. Changes in the resize / vsync synchronization: - Added `FlutterRunLoop` class to schedule Flutter tasks on main thread in a way where it is possible to only process these (Flutter posted) tasks while waiting for correct frame size during resizing. This significantly simplifies the resize synchronization and makes the same code work both with separate UI thread and with UI and platform thread merged. - `FlutterThreadSynchronizer` has been renamed to `FlutterResizeSynchronizer` vastly simplified, mutex and conditions are removed and the blocking is now done by only processing Flutter messages while waiting for resizing. It is now per view (instead of storing a viewId->Size map internally) and owned by the view itself, instead of engine. - This approach to resize synchronization will work for Windows and Linux as well. This will allow us to conceptually consolidate the way we do threading and resize synchronization on all three desktop embedders. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent f01d068 commit 3237222

27 files changed

Lines changed: 718 additions & 1094 deletions

engine/src/flutter/ci/licenses_golden/licenses_flutter

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52934,6 +52934,9 @@ ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPla
5293452934
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewControllerTest.mm + ../../../flutter/LICENSE
5293552935
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h + ../../../flutter/LICENSE
5293652936
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.mm + ../../../flutter/LICENSE
52937+
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h + ../../../flutter/LICENSE
52938+
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm + ../../../flutter/LICENSE
52939+
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizerTest.mm + ../../../flutter/LICENSE
5293752940
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.h + ../../../flutter/LICENSE
5293852941
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm + ../../../flutter/LICENSE
5293952942
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h + ../../../flutter/LICENSE
@@ -52947,9 +52950,6 @@ ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTex
5294752950
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputSemanticsObjectTest.mm + ../../../flutter/LICENSE
5294852951
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextureRegistrar.h + ../../../flutter/LICENSE
5294952952
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextureRegistrar.mm + ../../../flutter/LICENSE
52950-
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterThreadSynchronizer.h + ../../../flutter/LICENSE
52951-
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterThreadSynchronizer.mm + ../../../flutter/LICENSE
52952-
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterThreadSynchronizerTest.mm + ../../../flutter/LICENSE
5295352953
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTimeConverter.h + ../../../flutter/LICENSE
5295452954
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTimeConverter.mm + ../../../flutter/LICENSE
5295552955
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterUmbrellaImportTests.m + ../../../flutter/LICENSE
@@ -55943,6 +55943,11 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatf
5594355943
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewControllerTest.mm
5594455944
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h
5594555945
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.mm
55946+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h
55947+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm
55948+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizerTest.mm
55949+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRunLoop.h
55950+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRunLoop.mm
5594655951
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.h
5594755952
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm
5594855953
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h
@@ -55956,9 +55961,6 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextI
5595655961
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputSemanticsObjectTest.mm
5595755962
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextureRegistrar.h
5595855963
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextureRegistrar.mm
55959-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterThreadSynchronizer.h
55960-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterThreadSynchronizer.mm
55961-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterThreadSynchronizerTest.mm
5596255964
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTimeConverter.h
5596355965
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTimeConverter.mm
5596455966
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterUmbrellaImportTests.m

engine/src/flutter/shell/platform/darwin/macos/BUILD.gn

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ source_set("flutter_framework_source") {
9494
"framework/Source/FlutterPlatformViewController.mm",
9595
"framework/Source/FlutterRenderer.h",
9696
"framework/Source/FlutterRenderer.mm",
97+
"framework/Source/FlutterResizeSynchronizer.h",
98+
"framework/Source/FlutterResizeSynchronizer.mm",
99+
"framework/Source/FlutterRunLoop.h",
100+
"framework/Source/FlutterRunLoop.mm",
97101
"framework/Source/FlutterSurface.h",
98102
"framework/Source/FlutterSurface.mm",
99103
"framework/Source/FlutterSurfaceManager.h",
@@ -104,8 +108,6 @@ source_set("flutter_framework_source") {
104108
"framework/Source/FlutterTextInputSemanticsObject.mm",
105109
"framework/Source/FlutterTextureRegistrar.h",
106110
"framework/Source/FlutterTextureRegistrar.mm",
107-
"framework/Source/FlutterThreadSynchronizer.h",
108-
"framework/Source/FlutterThreadSynchronizer.mm",
109111
"framework/Source/FlutterTimeConverter.h",
110112
"framework/Source/FlutterTimeConverter.mm",
111113
"framework/Source/FlutterVSyncWaiter.h",
@@ -194,10 +196,10 @@ executable("flutter_desktop_darwin_unittests") {
194196
"framework/Source/FlutterMutatorViewTest.mm",
195197
"framework/Source/FlutterPlatformNodeDelegateMacTest.mm",
196198
"framework/Source/FlutterPlatformViewControllerTest.mm",
199+
"framework/Source/FlutterResizeSynchronizerTest.mm",
197200
"framework/Source/FlutterSurfaceManagerTest.mm",
198201
"framework/Source/FlutterTextInputPluginTest.mm",
199202
"framework/Source/FlutterTextInputSemanticsObjectTest.mm",
200-
"framework/Source/FlutterThreadSynchronizerTest.mm",
201203
"framework/Source/FlutterVSyncWaiterTest.mm",
202204
"framework/Source/FlutterViewControllerTest.mm",
203205
"framework/Source/FlutterViewControllerTestUtils.h",

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterDisplayLink.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#import <AppKit/AppKit.h>
55

66
@protocol FlutterDisplayLinkDelegate <NSObject>
7+
/// This will be called on main thread.
78
- (void)onDisplayLink:(CFTimeInterval)timestamp targetTimestamp:(CFTimeInterval)targetTimestamp;
89
@end
910

@@ -12,27 +13,28 @@
1213
/// Internally FlutterDisplayLink will use at most one CVDisplayLink per
1314
/// screen shared for all views belonging to that screen. This is necessary
1415
/// because each CVDisplayLink comes with its own thread.
16+
///
17+
/// All methods must be called on main thread.
1518
@interface FlutterDisplayLink : NSObject
1619

1720
/// Creates new instance tied to provided NSView. FlutterDisplayLink
1821
/// will track view display changes transparently to synchronize
1922
/// update with display refresh.
20-
/// This function must be called on the main thread.
2123
+ (instancetype)displayLinkWithView:(NSView*)view;
2224

23-
/// Delegate must be set on main thread. Delegate method will be called on
24-
/// on display link thread.
25+
/// Delegate must be set on main thread.
26+
/// Delegate method will be also called on main thread.
2527
@property(nonatomic, weak) id<FlutterDisplayLinkDelegate> delegate;
2628

27-
/// Pauses and resumes the display link. May be called from any thread.
29+
/// Pauses and resumes the display link.
2830
@property(readwrite) BOOL paused;
2931

3032
/// Returns the nominal refresh period of the display to which the view
3133
/// currently belongs (in seconds). If view does not belong to any display,
32-
/// returns 0. Can be called from any thread.
34+
/// returns 0.
3335
@property(readonly) CFTimeInterval nominalOutputRefreshPeriod;
3436

35-
/// Invalidates the display link. Must be called on the main thread.
37+
/// Invalidates the display link.
3638
- (void)invalidate;
3739

3840
@end

0 commit comments

Comments
 (0)