From cca5e726db331704bc4155f44a3a17ec1b916ea4 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Thu, 15 Oct 2020 15:06:35 -0700 Subject: [PATCH 1/2] Refactored the FlutterEngine to make it easier to implement [FlutterEngine spawn*] methods. --- shell/common/thread_host.cc | 3 +- shell/common/thread_host.h | 1 + .../ios/framework/Source/FlutterEngine.mm | 96 +++++++++++-------- 3 files changed, 61 insertions(+), 39 deletions(-) diff --git a/shell/common/thread_host.cc b/shell/common/thread_host.cc index 7f76bbf0a846e..225a2271181c6 100644 --- a/shell/common/thread_host.cc +++ b/shell/common/thread_host.cc @@ -10,7 +10,8 @@ ThreadHost::ThreadHost() = default; ThreadHost::ThreadHost(ThreadHost&&) = default; -ThreadHost::ThreadHost(std::string name_prefix, uint64_t mask) { +ThreadHost::ThreadHost(std::string name_prefix_arg, uint64_t mask) + : name_prefix(name_prefix_arg) { if (mask & ThreadHost::Type::Platform) { platform_thread = std::make_unique(name_prefix + ".platform"); } diff --git a/shell/common/thread_host.h b/shell/common/thread_host.h index 679168ddaca53..fce933e44c3e9 100644 --- a/shell/common/thread_host.h +++ b/shell/common/thread_host.h @@ -22,6 +22,7 @@ struct ThreadHost { Profiler = 1 << 4, }; + std::string name_prefix; std::unique_ptr platform_thread; std::unique_ptr ui_thread; std::unique_ptr raster_thread; diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index acac834082fff..a3efcd61d2335 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -38,6 +38,7 @@ NSString* const FlutterDefaultDartEntrypoint = nil; NSString* const FlutterDefaultInitialRoute = nil; static constexpr int kNumProfilerSamplesPerSec = 5; +static size_t g_shellCount = 0; @interface FlutterEngineRegistrar : NSObject @property(nonatomic, assign) FlutterEngine* flutterEngine; @@ -373,10 +374,11 @@ - (void)resetChannels { _keyEventChannel.reset(); } -- (void)startProfiler:(NSString*)threadLabel { +- (void)startProfiler { + FML_DCHECK(!_threadHost.name_prefix.empty()); _profiler_metrics = std::make_unique(); _profiler = std::make_unique( - threadLabel.UTF8String, _threadHost.profiler_thread->GetTaskRunner(), + _threadHost.name_prefix.c_str(), _threadHost.profiler_thread->GetTaskRunner(), [self]() { return self->_profiler_metrics->GenerateSample(); }, kNumProfilerSamplesPerSec); _profiler->Start(); } @@ -487,6 +489,45 @@ - (void)launchEngine:(NSString*)entrypoint libraryURI:(NSString*)libraryOrNil { libraryOrNil:libraryOrNil]); } +- (void)setupShell:(std::unique_ptr)shell + withObservatoryPublication:(BOOL)doesObservatoryPublication { + _shell = std::move(shell); + [self setupChannels]; + [self onLocaleUpdated:nil]; + [self initializeDisplays]; + _publisher.reset([[FlutterObservatoryPublisher alloc] + initWithEnableObservatoryPublication:doesObservatoryPublication]); + [self maybeSetupPlatformViewChannels]; + _shell->GetIsGpuDisabledSyncSwitch()->SetSwitch(_isGpuDisabled ? true : false); +} + ++ (BOOL)isProfilerEnabled { + bool profilerEnabled = false; +#if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG) || \ + (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE) + profilerEnabled = true; +#endif + return profilerEnabled; +} + ++ (NSString*)generateThreadLabel:(NSString*)labelPrefix { + return [NSString stringWithFormat:@"%@.%zu", labelPrefix, ++g_shellCount]; +} + ++ (flutter::ThreadHost)makeThreadHost:(NSString*)threadLabel { + // The current thread will be used as the platform thread. Ensure that the message loop is + // initialized. + fml::MessageLoop::EnsureInitializedForCurrentThread(); + + uint32_t threadHostType = flutter::ThreadHost::Type::UI | flutter::ThreadHost::Type::GPU | + flutter::ThreadHost::Type::IO; + if ([FlutterEngine isProfilerEnabled]) { + threadHostType = threadHostType | flutter::ThreadHost::Type::Profiler; + } + return {threadLabel.UTF8String, // label + threadHostType}; +} + - (BOOL)createShell:(NSString*)entrypoint libraryURI:(NSString*)libraryURI initialRoute:(NSString*)initialRoute { @@ -495,7 +536,6 @@ - (BOOL)createShell:(NSString*)entrypoint return NO; } - static size_t shellCount = 1; self.initialRoute = initialRoute; auto settings = [_dartProject.get() settings]; @@ -515,24 +555,8 @@ - (BOOL)createShell:(NSString*)entrypoint settings.advisory_script_uri = std::string("main.dart"); } - const auto threadLabel = [NSString stringWithFormat:@"%@.%zu", _labelPrefix, shellCount++]; - - // The current thread will be used as the platform thread. Ensure that the message loop is - // initialized. - fml::MessageLoop::EnsureInitializedForCurrentThread(); - - uint32_t threadHostType = flutter::ThreadHost::Type::UI | flutter::ThreadHost::Type::GPU | - flutter::ThreadHost::Type::IO; - bool profilerEnabled = false; -#if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG) || \ - (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE) - profilerEnabled = true; -#endif - if (profilerEnabled) { - threadHostType = threadHostType | flutter::ThreadHost::Type::Profiler; - } - _threadHost = {threadLabel.UTF8String, // label - threadHostType}; + NSString* threadLabel = [FlutterEngine generateThreadLabel:_labelPrefix]; + _threadHost = [FlutterEngine makeThreadHost:threadLabel]; // Lambda captures by pointers to ObjC objects are fine here because the // create call is synchronous. @@ -554,26 +578,22 @@ - (BOOL)createShell:(NSString*)entrypoint ); // Create the shell. This is a blocking operation. - _shell = flutter::Shell::Create(std::move(task_runners), // task runners - std::move(platformData), // window data - std::move(settings), // settings - on_create_platform_view, // platform view creation - on_create_rasterizer // rasterzier creation - ); - - if (_shell == nullptr) { + std::unique_ptr shell = + flutter::Shell::Create(std::move(task_runners), // task runners + std::move(platformData), // window data + std::move(settings), // settings + on_create_platform_view, // platform view creation + on_create_rasterizer // rasterzier creation + ); + + if (shell == nullptr) { FML_LOG(ERROR) << "Could not start a shell FlutterEngine with entrypoint: " << entrypoint.UTF8String; } else { - [self setupChannels]; - [self onLocaleUpdated:nil]; - [self initializeDisplays]; - _publisher.reset([[FlutterObservatoryPublisher alloc] - initWithEnableObservatoryPublication:settings.enable_observatory_publication]); - [self maybeSetupPlatformViewChannels]; - _shell->GetIsGpuDisabledSyncSwitch()->SetSwitch(_isGpuDisabled ? true : false); - if (profilerEnabled) { - [self startProfiler:threadLabel]; + [self setupShell:std::move(shell) + withObservatoryPublication:settings.enable_observatory_publication]; + if ([FlutterEngine isProfilerEnabled]) { + [self startProfiler]; } } From 82633fdd7ee9c2d4f87c15f54e5e817912451d65 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Fri, 30 Oct 2020 10:56:08 -0700 Subject: [PATCH 2/2] moved file static back to function static --- shell/platform/darwin/ios/framework/Source/FlutterEngine.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index a3efcd61d2335..9c26b05081ccc 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -38,7 +38,6 @@ NSString* const FlutterDefaultDartEntrypoint = nil; NSString* const FlutterDefaultInitialRoute = nil; static constexpr int kNumProfilerSamplesPerSec = 5; -static size_t g_shellCount = 0; @interface FlutterEngineRegistrar : NSObject @property(nonatomic, assign) FlutterEngine* flutterEngine; @@ -511,7 +510,8 @@ + (BOOL)isProfilerEnabled { } + (NSString*)generateThreadLabel:(NSString*)labelPrefix { - return [NSString stringWithFormat:@"%@.%zu", labelPrefix, ++g_shellCount]; + static size_t s_shellCount = 0; + return [NSString stringWithFormat:@"%@.%zu", labelPrefix, ++s_shellCount]; } + (flutter::ThreadHost)makeThreadHost:(NSString*)threadLabel {