@@ -373,10 +373,11 @@ - (void)resetChannels {
373373 _keyEventChannel.reset ();
374374}
375375
376- - (void )startProfiler : (NSString *)threadLabel {
376+ - (void )startProfiler {
377+ FML_DCHECK (!_threadHost.name_prefix .empty ());
377378 _profiler_metrics = std::make_unique<flutter::ProfilerMetricsIOS>();
378379 _profiler = std::make_unique<flutter::SamplingProfiler>(
379- threadLabel. UTF8String , _threadHost.profiler_thread ->GetTaskRunner (),
380+ _threadHost. name_prefix . c_str () , _threadHost.profiler_thread ->GetTaskRunner (),
380381 [self ]() { return self->_profiler_metrics ->GenerateSample (); }, kNumProfilerSamplesPerSec );
381382 _profiler->Start ();
382383}
@@ -487,6 +488,46 @@ - (void)launchEngine:(NSString*)entrypoint libraryURI:(NSString*)libraryOrNil {
487488 libraryOrNil: libraryOrNil]);
488489}
489490
491+ - (void )setupShell : (std::unique_ptr<flutter::Shell>)shell
492+ withObservatoryPublication : (BOOL )doesObservatoryPublication {
493+ _shell = std::move (shell);
494+ [self setupChannels ];
495+ [self onLocaleUpdated: nil ];
496+ [self initializeDisplays ];
497+ _publisher.reset ([[FlutterObservatoryPublisher alloc ]
498+ initWithEnableObservatoryPublication: doesObservatoryPublication]);
499+ [self maybeSetupPlatformViewChannels ];
500+ _shell->GetIsGpuDisabledSyncSwitch ()->SetSwitch (_isGpuDisabled ? true : false );
501+ }
502+
503+ + (BOOL )isProfilerEnabled {
504+ bool profilerEnabled = false ;
505+ #if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG) || \
506+ (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE)
507+ profilerEnabled = true ;
508+ #endif
509+ return profilerEnabled;
510+ }
511+
512+ + (NSString *)generateThreadLabel : (NSString *)labelPrefix {
513+ static size_t s_shellCount = 0 ;
514+ return [NSString stringWithFormat: @" %@ .%zu " , labelPrefix, ++s_shellCount];
515+ }
516+
517+ + (flutter::ThreadHost)makeThreadHost : (NSString *)threadLabel {
518+ // The current thread will be used as the platform thread. Ensure that the message loop is
519+ // initialized.
520+ fml::MessageLoop::EnsureInitializedForCurrentThread ();
521+
522+ uint32_t threadHostType = flutter::ThreadHost::Type::UI | flutter::ThreadHost::Type::GPU |
523+ flutter::ThreadHost::Type::IO;
524+ if ([FlutterEngine isProfilerEnabled ]) {
525+ threadHostType = threadHostType | flutter::ThreadHost::Type::Profiler;
526+ }
527+ return {threadLabel.UTF8String , // label
528+ threadHostType};
529+ }
530+
490531- (BOOL )createShell : (NSString *)entrypoint
491532 libraryURI : (NSString *)libraryURI
492533 initialRoute : (NSString *)initialRoute {
@@ -495,7 +536,6 @@ - (BOOL)createShell:(NSString*)entrypoint
495536 return NO ;
496537 }
497538
498- static size_t shellCount = 1 ;
499539 self.initialRoute = initialRoute;
500540
501541 auto settings = [_dartProject.get () settings ];
@@ -515,24 +555,8 @@ - (BOOL)createShell:(NSString*)entrypoint
515555 settings.advisory_script_uri = std::string (" main.dart" );
516556 }
517557
518- const auto threadLabel = [NSString stringWithFormat: @" %@ .%zu " , _labelPrefix, shellCount++];
519-
520- // The current thread will be used as the platform thread. Ensure that the message loop is
521- // initialized.
522- fml::MessageLoop::EnsureInitializedForCurrentThread ();
523-
524- uint32_t threadHostType = flutter::ThreadHost::Type::UI | flutter::ThreadHost::Type::GPU |
525- flutter::ThreadHost::Type::IO;
526- bool profilerEnabled = false ;
527- #if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG) || \
528- (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE)
529- profilerEnabled = true ;
530- #endif
531- if (profilerEnabled) {
532- threadHostType = threadHostType | flutter::ThreadHost::Type::Profiler;
533- }
534- _threadHost = {threadLabel.UTF8String , // label
535- threadHostType};
558+ NSString * threadLabel = [FlutterEngine generateThreadLabel: _labelPrefix];
559+ _threadHost = [FlutterEngine makeThreadHost: threadLabel];
536560
537561 // Lambda captures by pointers to ObjC objects are fine here because the
538562 // create call is synchronous.
@@ -554,26 +578,22 @@ - (BOOL)createShell:(NSString*)entrypoint
554578 );
555579
556580 // Create the shell. This is a blocking operation.
557- _shell = flutter::Shell::Create (std::move (task_runners), // task runners
558- std::move (platformData), // window data
559- std::move (settings), // settings
560- on_create_platform_view, // platform view creation
561- on_create_rasterizer // rasterzier creation
562- );
563-
564- if (_shell == nullptr ) {
581+ std::unique_ptr<flutter::Shell> shell =
582+ flutter::Shell::Create (std::move (task_runners), // task runners
583+ std::move (platformData), // window data
584+ std::move (settings), // settings
585+ on_create_platform_view, // platform view creation
586+ on_create_rasterizer // rasterzier creation
587+ );
588+
589+ if (shell == nullptr ) {
565590 FML_LOG (ERROR) << " Could not start a shell FlutterEngine with entrypoint: "
566591 << entrypoint.UTF8String ;
567592 } else {
568- [self setupChannels ];
569- [self onLocaleUpdated: nil ];
570- [self initializeDisplays ];
571- _publisher.reset ([[FlutterObservatoryPublisher alloc ]
572- initWithEnableObservatoryPublication: settings.enable_observatory_publication]);
573- [self maybeSetupPlatformViewChannels ];
574- _shell->GetIsGpuDisabledSyncSwitch ()->SetSwitch (_isGpuDisabled ? true : false );
575- if (profilerEnabled) {
576- [self startProfiler: threadLabel];
593+ [self setupShell: std: :move (shell)
594+ withObservatoryPublication: settings.enable_observatory_publication];
595+ if ([FlutterEngine isProfilerEnabled ]) {
596+ [self startProfiler ];
577597 }
578598 }
579599
0 commit comments