3434
3535NSString * const FlutterDefaultDartEntrypoint = nil ;
3636NSString * const FlutterDefaultInitialRoute = nil ;
37+ NSString * const FlutterEngineWillDealloc = @" FlutterEngineWillDealloc" ;
3738static constexpr int kNumProfilerSamplesPerSec = 5 ;
3839
3940@interface FlutterEngineRegistrar : NSObject <FlutterPluginRegistrar>
@@ -54,7 +55,7 @@ @interface FlutterEngine () <FlutterTextInputDelegate, FlutterBinaryMessenger>
5455
5556@implementation FlutterEngine {
5657 fml::scoped_nsobject<FlutterDartProject> _dartProject;
57- flutter::ThreadHost _threadHost;
58+ std::shared_ptr< flutter::ThreadHost> _threadHost;
5859 std::unique_ptr<flutter::Shell> _shell;
5960 NSString * _labelPrefix;
6061 std::unique_ptr<fml::WeakPtrFactory<FlutterEngine>> _weakFactory;
@@ -64,8 +65,8 @@ @implementation FlutterEngine {
6465
6566 std::shared_ptr<flutter::FlutterPlatformViewsController> _platformViewsController;
6667 flutter::IOSRenderingAPI _renderingApi;
67- std::unique_ptr <flutter::ProfilerMetricsIOS> _profiler_metrics;
68- std::unique_ptr <flutter::SamplingProfiler> _profiler;
68+ std::shared_ptr <flutter::ProfilerMetricsIOS> _profiler_metrics;
69+ std::shared_ptr <flutter::SamplingProfiler> _profiler;
6970
7071 // Channels
7172 fml::scoped_nsobject<FlutterPlatformPlugin> _platformPlugin;
@@ -181,6 +182,10 @@ - (void)dealloc {
181182 }
182183 }];
183184
185+ [[NSNotificationCenter defaultCenter ] postNotificationName: FlutterEngineWillDealloc
186+ object: self
187+ userInfo: nil ];
188+
184189 // / nil out weak references.
185190 [_registrars
186191 enumerateKeysAndObjectsUsingBlock: ^(id key, FlutterEngineRegistrar* registrar, BOOL * stop) {
@@ -306,7 +311,7 @@ - (void)destroyContext {
306311 self.isolateId = nil ;
307312 _shell.reset ();
308313 _profiler.reset ();
309- _threadHost.Reset ();
314+ _threadHost.reset ();
310315 _platformViewsController.reset ();
311316}
312317
@@ -368,10 +373,10 @@ - (void)resetChannels {
368373}
369374
370375- (void )startProfiler {
371- FML_DCHECK (!_threadHost. name_prefix .empty ());
372- _profiler_metrics = std::make_unique <flutter::ProfilerMetricsIOS>();
373- _profiler = std::make_unique <flutter::SamplingProfiler>(
374- _threadHost. name_prefix .c_str (), _threadHost. profiler_thread ->GetTaskRunner (),
376+ FML_DCHECK (!_threadHost-> name_prefix .empty ());
377+ _profiler_metrics = std::make_shared <flutter::ProfilerMetricsIOS>();
378+ _profiler = std::make_shared <flutter::SamplingProfiler>(
379+ _threadHost-> name_prefix .c_str (), _threadHost-> profiler_thread ->GetTaskRunner (),
375380 [self ]() { return self->_profiler_metrics ->GenerateSample (); }, kNumProfilerSamplesPerSec );
376381 _profiler->Start ();
377382}
@@ -508,7 +513,7 @@ + (NSString*)generateThreadLabel:(NSString*)labelPrefix {
508513 return [NSString stringWithFormat: @" %@ .%zu " , labelPrefix, ++s_shellCount];
509514}
510515
511- + (flutter::ThreadHost)makeThreadHost : (NSString *)threadLabel {
516+ + (std::shared_ptr< flutter::ThreadHost> )makeThreadHost : (NSString *)threadLabel {
512517 // The current thread will be used as the platform thread. Ensure that the message loop is
513518 // initialized.
514519 fml::MessageLoop::EnsureInitializedForCurrentThread ();
@@ -518,8 +523,8 @@ + (NSString*)generateThreadLabel:(NSString*)labelPrefix {
518523 if ([FlutterEngine isProfilerEnabled ]) {
519524 threadHostType = threadHostType | flutter::ThreadHost::Type::Profiler;
520525 }
521- return { threadLabel.UTF8String , // label
522- threadHostType} ;
526+ return std::make_shared<flutter::ThreadHost>( threadLabel.UTF8String , // label
527+ threadHostType) ;
523528}
524529
525530- (BOOL )createShell : (NSString *)entrypoint
@@ -566,9 +571,9 @@ - (BOOL)createShell:(NSString*)entrypoint
566571
567572 flutter::TaskRunners task_runners (threadLabel.UTF8String , // label
568573 fml::MessageLoop::GetCurrent ().GetTaskRunner (), // platform
569- _threadHost. raster_thread ->GetTaskRunner (), // raster
570- _threadHost. ui_thread ->GetTaskRunner (), // ui
571- _threadHost. io_thread ->GetTaskRunner () // io
574+ _threadHost-> raster_thread ->GetTaskRunner (), // raster
575+ _threadHost-> ui_thread ->GetTaskRunner (), // ui
576+ _threadHost-> io_thread ->GetTaskRunner () // io
572577 );
573578
574579 // Create the shell. This is a blocking operation.
@@ -921,6 +926,42 @@ - (void)waitForFirstFrame:(NSTimeInterval)timeout
921926 });
922927}
923928
929+ - (FlutterEngine*)spawnWithEntrypoint : (NSString *)entrypoint {
930+ assert (_shell);
931+ FlutterEngine* result =
932+ [[FlutterEngine alloc ] initWithName: [_labelPrefix stringByAppendingString: @" -spawn" ]
933+ project: _dartProject.get ()
934+ allowHeadlessExecution: _allowHeadlessExecution];
935+
936+ flutter::Settings settings = _shell->GetSettings ();
937+ if (entrypoint) {
938+ settings.advisory_script_entrypoint = entrypoint.UTF8String ;
939+ settings.advisory_script_uri = std::string (" main.dart" );
940+ } else {
941+ settings.advisory_script_entrypoint = std::string (" main" );
942+ settings.advisory_script_uri = std::string (" main.dart" );
943+ }
944+
945+ flutter::Shell::CreateCallback<flutter::PlatformView> on_create_platform_view =
946+ [self ](flutter::Shell& shell) {
947+ [self recreatePlatformViewController ];
948+ return std::make_unique<flutter::PlatformViewIOS>(
949+ shell, self->_renderingApi , self->_platformViewsController , shell.GetTaskRunners ());
950+ };
951+
952+ flutter::Shell::CreateCallback<flutter::Rasterizer> on_create_rasterizer =
953+ [](flutter::Shell& shell) { return std::make_unique<flutter::Rasterizer>(shell); };
954+
955+ std::unique_ptr<flutter::Shell> shell =
956+ _shell->Spawn (std::move (settings), on_create_platform_view, on_create_rasterizer);
957+
958+ result->_threadHost = _threadHost;
959+ result->_profiler = _profiler;
960+ result->_profiler_metrics = _profiler_metrics;
961+ [result setupShell: std: :move (shell) withObservatoryPublication: NO ];
962+ return result;
963+ }
964+
924965@end
925966
926967@implementation FlutterEngineRegistrar {
0 commit comments