Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -43720,9 +43720,9 @@ ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTex
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputSemanticsObjectTest.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextureRegistrar.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextureRegistrar.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterThreadSynchronizer.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterThreadSynchronizer.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterThreadSynchronizerTest.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizerTest.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTimeConverter.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTimeConverter.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterUmbrellaImportTests.m + ../../../flutter/LICENSE
Expand Down
12 changes: 12 additions & 0 deletions lib/ui/ui_dart_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ extern void syslog(int, const char*, ...);
}
#endif

namespace {
static std::set<flutter::UIDartState*> state;
}

void UglyHackFlushMicrotasks() {
for (auto s : state) {
s->FlushMicrotasksNow();
}
}

using tonic::ToDart;

namespace flutter {
Expand Down Expand Up @@ -73,10 +83,12 @@ UIDartState::UIDartState(
isolate_name_server_(std::move(isolate_name_server)),
context_(context) {
AddOrRemoveTaskObserver(true /* add */);
state.insert(this);
}

UIDartState::~UIDartState() {
AddOrRemoveTaskObserver(false /* remove */);
state.erase(this);
}

const std::string& UIDartState::GetAdvisoryScriptURI() const {
Expand Down
2 changes: 1 addition & 1 deletion runtime/dart_isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ bool DartIsolate::Initialize(Dart_Isolate dart_isolate) {
SetMessageHandlingTaskRunner(GetTaskRunners().GetPlatformTaskRunner(),
true);
} else {
SetMessageHandlingTaskRunner(GetTaskRunners().GetUITaskRunner(), false);
SetMessageHandlingTaskRunner(GetTaskRunners().GetUITaskRunner(), true);
}

if (tonic::CheckAndHandleError(
Expand Down
8 changes: 5 additions & 3 deletions shell/platform/darwin/macos/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ source_set("flutter_framework_source") {
"framework/Source/FlutterPlatformViewController.mm",
"framework/Source/FlutterRenderer.h",
"framework/Source/FlutterRenderer.mm",
"framework/Source/FlutterResizeSynchronizer.h",
"framework/Source/FlutterResizeSynchronizer.mm",
"framework/Source/FlutterRunLoop.h",
"framework/Source/FlutterRunLoop.mm",
"framework/Source/FlutterSurface.h",
"framework/Source/FlutterSurface.mm",
"framework/Source/FlutterSurfaceManager.h",
Expand All @@ -101,8 +105,6 @@ source_set("flutter_framework_source") {
"framework/Source/FlutterTextInputSemanticsObject.mm",
"framework/Source/FlutterTextureRegistrar.h",
"framework/Source/FlutterTextureRegistrar.mm",
"framework/Source/FlutterThreadSynchronizer.h",
"framework/Source/FlutterThreadSynchronizer.mm",
"framework/Source/FlutterTimeConverter.h",
"framework/Source/FlutterTimeConverter.mm",
"framework/Source/FlutterVSyncWaiter.h",
Expand Down Expand Up @@ -190,10 +192,10 @@ executable("flutter_desktop_darwin_unittests") {
"framework/Source/FlutterMutatorViewTest.mm",
"framework/Source/FlutterPlatformNodeDelegateMacTest.mm",
"framework/Source/FlutterPlatformViewControllerTest.mm",
"framework/Source/FlutterResizeSynchronizerTest.mm",
"framework/Source/FlutterSurfaceManagerTest.mm",
"framework/Source/FlutterTextInputPluginTest.mm",
"framework/Source/FlutterTextInputSemanticsObjectTest.mm",
"framework/Source/FlutterThreadSynchronizerTest.mm",
"framework/Source/FlutterVSyncWaiterTest.mm",
"framework/Source/FlutterViewControllerTest.mm",
"framework/Source/FlutterViewControllerTestUtils.h",
Expand Down
61 changes: 22 additions & 39 deletions shell/platform/darwin/macos/framework/Source/FlutterEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterRunLoop.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterTimeConverter.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterVSyncWaiter.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
Expand All @@ -37,6 +38,8 @@

using flutter::kFlutterImplicitViewId;

extern void UglyHackFlushMicrotasks();

/**
* Constructs and returns a FlutterLocale struct corresponding to |locale|, which must outlive
* the returned struct.
Expand Down Expand Up @@ -455,8 +458,6 @@ @implementation FlutterEngine {
// A method channel for miscellaneous platform functionality.
FlutterMethodChannel* _platformChannel;

FlutterThreadSynchronizer* _threadSynchronizer;

// Whether the application is currently the active application.
BOOL _active;

Expand Down Expand Up @@ -499,6 +500,9 @@ - (instancetype)initWithName:(NSString*)labelPrefix
allowHeadlessExecution:(BOOL)allowHeadlessExecution {
self = [super init];
NSAssert(self, @"Super init cannot be nil");

[FlutterRunLoop ensureMainLoopInitialized];

_pasteboard = [[FlutterPasteboard alloc] init];
_active = NO;
_visible = NO;
Expand Down Expand Up @@ -527,7 +531,6 @@ - (instancetype)initWithName:(NSString*)labelPrefix
object:nil];

_platformViewController = [[FlutterPlatformViewController alloc] init];
_threadSynchronizer = [[FlutterThreadSynchronizer alloc] init];
[self setUpPlatformViewChannel];
[self setUpAccessibilityChannel];
[self setUpNotificationCenterListeners];
Expand Down Expand Up @@ -653,7 +656,8 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
const FlutterCustomTaskRunners custom_task_runners = {
.struct_size = sizeof(FlutterCustomTaskRunners),
.platform_task_runner = &cocoa_task_runner_description,
.thread_priority_setter = SetThreadPriority};
.thread_priority_setter = SetThreadPriority,
.merged_ui_thread = 1};
flutterArguments.custom_task_runners = &custom_task_runners;

[self loadAOTData:_project.assetsPath];
Expand Down Expand Up @@ -739,9 +743,7 @@ - (void)registerViewController:(FlutterViewController*)controller
NSAssert([_viewControllers objectForKey:@(viewIdentifier)] == nil,
@"The requested view ID is occupied.");
[_viewControllers setObject:controller forKey:@(viewIdentifier)];
[controller setUpWithEngine:self
viewIdentifier:viewIdentifier
threadSynchronizer:_threadSynchronizer];
[controller setUpWithEngine:self viewIdentifier:viewIdentifier];
NSAssert(controller.viewIdentifier == viewIdentifier, @"Failed to assign view ID.");
// Verify that the controller's property are updated accordingly. Failing the
// assertions is likely because either the FlutterViewController or the
Expand Down Expand Up @@ -769,13 +771,7 @@ - (void)viewControllerViewDidLoad:(FlutterViewController*)viewController {
[timeConverter CAMediaTimeToEngineTime:targetTimestamp];
FlutterEngine* engine = weakSelf;
if (engine) {
// It is a bit unfortunate that embedder requires OnVSync call on
// platform thread just to immediately redispatch it to UI thread.
// We are already on UI thread right now, but have to do the
// extra hop to main thread.
[engine->_threadSynchronizer performOnPlatformThread:^{
engine->_embedderAPI.OnVsync(_engine, baton, timeNanos, targetTimeNanos);
}];
engine->_embedderAPI.OnVsync(_engine, baton, timeNanos, targetTimeNanos);
}
}];
FML_DCHECK([_vsyncWaiters objectForKey:@(viewController.viewIdentifier)] == nil);
Expand Down Expand Up @@ -1132,9 +1128,6 @@ - (void)shutDownEngine {
return;
}

[_threadSynchronizer shutdown];
_threadSynchronizer = nil;

FlutterEngineResult result = _embedderAPI.Deinitialize(_engine);
if (result != kSuccess) {
NSLog(@"Could not de-initialize the Flutter engine: error %d", result);
Expand Down Expand Up @@ -1331,10 +1324,6 @@ - (BOOL)clipboardHasStrings {
return flutter::GetSwitchesFromEnvironment();
}

- (FlutterThreadSynchronizer*)testThreadSynchronizer {
return _threadSynchronizer;
}

#pragma mark - FlutterAppLifecycleDelegate

- (void)setApplicationState:(flutter::AppLifecycleState)state {
Expand Down Expand Up @@ -1518,29 +1507,23 @@ - (BOOL)unregisterTextureWithID:(int64_t)textureID {

#pragma mark - Task runner integration

- (void)runTaskOnEmbedder:(FlutterTask)task {
if (_engine) {
auto result = _embedderAPI.RunTask(_engine, &task);
if (result != kSuccess) {
NSLog(@"Could not post a task to the Flutter engine.");
}
}
}

- (void)postMainThreadTask:(FlutterTask)task targetTimeInNanoseconds:(uint64_t)targetTime {
__weak FlutterEngine* weakSelf = self;
auto worker = ^{
[weakSelf runTaskOnEmbedder:task];
};

const auto engine_time = _embedderAPI.GetCurrentTime();
if (targetTime <= engine_time) {
dispatch_async(dispatch_get_main_queue(), worker);

} else {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, targetTime - engine_time),
dispatch_get_main_queue(), worker);
}
[FlutterRunLoop.mainRunLoop
performBlock:^{
FlutterEngine* self = weakSelf;
if (_engine) {
auto result = _embedderAPI.RunTask(_engine, &task);
if (result != kSuccess) {
NSLog(@"Could not post a task to the Flutter engine.");
}
}
UglyHackFlushMicrotasks();
}
afterDelay:(targetTime - (double)engine_time) / 1000000000.0];
}

// Getter used by test harness, only exposed through the FlutterEngine(Test) category
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
// CREATE_NATIVE_ENTRY and MOCK_ENGINE_PROC are leaky by design
// NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)

constexpr int64_t kImplicitViewId = 0ll;

@interface FlutterEngine (Test)
/**
* The FlutterCompositor object currently in use by the FlutterEngine.
Expand Down Expand Up @@ -526,7 +524,7 @@ @implementation MockableFlutterEngine
result:^(id result){
}];

[engine.testThreadSynchronizer blockUntilFrameAvailable];
// [engine.testThreadSynchronizer blockUntilFrameAvailable];

CALayer* rootLayer = viewController.flutterView.layer;

Expand Down Expand Up @@ -863,20 +861,6 @@ @implementation MockableFlutterEngine
}
}

TEST_F(FlutterEngineTest, ThreadSynchronizerNotBlockingRasterThreadAfterShutdown) {
FlutterThreadSynchronizer* threadSynchronizer = [[FlutterThreadSynchronizer alloc] init];
[threadSynchronizer shutdown];

std::thread rasterThread([&threadSynchronizer] {
[threadSynchronizer performCommitForView:kImplicitViewId
size:CGSizeMake(100, 100)
notify:^{
}];
});

rasterThread.join();
}

TEST_F(FlutterEngineTest, ManageControllersIfInitiatedByController) {
NSString* fixtures = @(flutter::testing::GetFixturesPath());
FlutterDartProject* project = [[FlutterDartProject alloc]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,6 @@ typedef NS_ENUM(NSInteger, FlutterAppExitResponse) {
- (NSArray<NSScreen*>*)screens;
@end

@interface FlutterEngine (Tests)
- (nonnull FlutterThreadSynchronizer*)testThreadSynchronizer;
@end

NS_ASSUME_NONNULL_END

#endif // FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERENGINE_INTERNAL_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERRESIZESYNCHRONIZER_H_
#define FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERRESIZESYNCHRONIZER_H_

#import <Cocoa/Cocoa.h>

#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterViewController.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterRunLoop.h"

@interface FlutterResizeSynchronizer : NSObject

/**
* Begins a resize operation for the given size. Block the thread until
* performCommitForSize: with the same size is called.
* While the thread is blocked Flutter messages are being pumped.
* See [FlutterRunLoop pollOnce].
*/
- (void)beginResizeForSize:(CGSize)size notify:(nonnull dispatch_block_t)notify;

/**
* Called from raster thread. Schedules the given block on platform thread
* at given delay and unblocks the platform thread if waiting for the surface
* during resize.
*/
- (void)performCommitForSize:(CGSize)size
notify:(nonnull dispatch_block_t)notify
delay:(NSTimeInterval)delay;

/**
* Called when the view is shut down. Unblocks platform thread if blocked
* during resize.
*/
- (void)shutDown;

@end

#endif // FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERRESIZESYNCHRONIZER_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h"

#import "flutter/fml/logging.h"

@implementation FlutterResizeSynchronizer {
std::atomic_bool _inResize;
bool _shuttingDown;
bool _hasFrame;
CGSize _contentSize;
}

- (void)beginResizeForSize:(CGSize)size notify:(nonnull dispatch_block_t)notify {
if (!_hasFrame || _shuttingDown) {
notify();
return;
}

_inResize = true;
_contentSize = CGSizeMake(-1, -1);
notify();
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
while (true) {
if (CGSizeEqualToSize(_contentSize, size) || _shuttingDown) {
break;
}
if (CFAbsoluteTimeGetCurrent() - start > 1.0) {
FML_LOG(ERROR) << "Resize timed out.";
break;
}
[FlutterRunLoop.mainRunLoop pollOnce];
}
_inResize = false;
}

- (void)performCommitForSize:(CGSize)size
notify:(nonnull dispatch_block_t)notify
delay:(NSTimeInterval)delay {
if (_inResize) {
delay = 0;
}
[FlutterRunLoop.mainRunLoop
performBlock:^{
_hasFrame = YES;
_contentSize = size;
notify();
}
afterDelay:delay];
}

- (void)shutDown {
[FlutterRunLoop.mainRunLoop performBlock:^{
_shuttingDown = YES;
}];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#if 0

#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterThreadSynchronizer.h"

#import "flutter/fml/synchronization/waitable_event.h"
Expand Down Expand Up @@ -374,3 +376,5 @@ - (void)joinRender {
[scaffold joinMain];
EXPECT_FALSE([synchronizer isWaitingWhenMutexIsAvailable]);
}

#endif
Loading