3838NSString * const FlutterDefaultDartEntrypoint = nil ;
3939NSString * const FlutterDefaultInitialRoute = nil ;
4040static constexpr int kNumProfilerSamplesPerSec = 5 ;
41+ static size_t g_shellCount = 0 ;
4142
4243@interface FlutterEngineRegistrar : NSObject <FlutterPluginRegistrar>
4344@property (nonatomic , assign ) FlutterEngine* flutterEngine;
@@ -373,10 +374,11 @@ - (void)resetChannels {
373374 _keyEventChannel.reset ();
374375}
375376
376- - (void )startProfiler : (NSString *)threadLabel {
377+ - (void )startProfiler {
378+ FML_DCHECK (!_threadHost.name_prefix .empty ());
377379 _profiler_metrics = std::make_unique<flutter::ProfilerMetricsIOS>();
378380 _profiler = std::make_unique<flutter::SamplingProfiler>(
379- threadLabel. UTF8String , _threadHost.profiler_thread ->GetTaskRunner (),
381+ _threadHost. name_prefix . c_str () , _threadHost.profiler_thread ->GetTaskRunner (),
380382 [self ]() { return self->_profiler_metrics ->GenerateSample (); }, kNumProfilerSamplesPerSec );
381383 _profiler->Start ();
382384}
@@ -487,6 +489,45 @@ - (void)launchEngine:(NSString*)entrypoint libraryURI:(NSString*)libraryOrNil {
487489 libraryOrNil: libraryOrNil]);
488490}
489491
492+ - (void )setupShell : (std::unique_ptr<flutter::Shell>)shell
493+ withObservatoryPublication : (BOOL )doesObservatoryPublication {
494+ _shell = std::move (shell);
495+ [self setupChannels ];
496+ [self onLocaleUpdated: nil ];
497+ [self initializeDisplays ];
498+ _publisher.reset ([[FlutterObservatoryPublisher alloc ]
499+ initWithEnableObservatoryPublication: doesObservatoryPublication]);
500+ [self maybeSetupPlatformViewChannels ];
501+ _shell->GetIsGpuDisabledSyncSwitch ()->SetSwitch (_isGpuDisabled ? true : false );
502+ }
503+
504+ + (BOOL )isProfilerEnabled {
505+ bool profilerEnabled = false ;
506+ #if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG) || \
507+ (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE)
508+ profilerEnabled = true ;
509+ #endif
510+ return profilerEnabled;
511+ }
512+
513+ + (NSString *)generateThreadLabel : (NSString *)labelPrefix {
514+ return [NSString stringWithFormat: @" %@ .%zu " , labelPrefix, ++g_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