From 4e9459e0070ad85ed7cfbee0b3b51ea80d9802e4 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Fri, 30 Oct 2020 15:29:10 -0700 Subject: [PATCH] Refactored the FlutterEngine to make it easier to implement spawn functionality (#21890) --- 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 7f76bbf0a8..225a227118 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 679168ddac..fce933e44c 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 acac834082..9c26b05081 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -373,10 +373,11 @@ static constexpr int kNumProfilerSamplesPerSec = 5; _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 +488,46 @@ static constexpr int kNumProfilerSamplesPerSec = 5; 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 { + static size_t s_shellCount = 0; + return [NSString stringWithFormat:@"%@.%zu", labelPrefix, ++s_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 @@ static constexpr int kNumProfilerSamplesPerSec = 5; return NO; } - static size_t shellCount = 1; self.initialRoute = initialRoute; auto settings = [_dartProject.get() settings]; @@ -515,24 +555,8 @@ static constexpr int kNumProfilerSamplesPerSec = 5; 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 @@ static constexpr int kNumProfilerSamplesPerSec = 5; ); // 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]; } } -- GitLab