@@ -18,10 +18,7 @@ namespace flutter {
1818
1919RuntimeController::RuntimeController (RuntimeDelegate& client,
2020 TaskRunners p_task_runners)
21- : client_(client),
22- vm_ (nullptr ),
23- task_runners_(p_task_runners),
24- weak_factory_(this ) {}
21+ : client_(client), vm_(nullptr ), task_runners_(p_task_runners) {}
2522
2623RuntimeController::RuntimeController (
2724 RuntimeDelegate& p_client,
@@ -55,81 +52,49 @@ RuntimeController::RuntimeController(
5552 platform_data_(std::move(p_platform_data)),
5653 isolate_create_callback_(p_isolate_create_callback),
5754 isolate_shutdown_callback_(p_isolate_shutdown_callback),
58- persistent_isolate_data_(std::move(p_persistent_isolate_data)),
59- weak_factory_(this ) {
60- // Create the root isolate as soon as the runtime controller is initialized,
61- // but not using a synchronous way to avoid blocking the platform thread a
62- // long time as it is waiting while creating `Shell` on that platform thread.
55+ persistent_isolate_data_(std::move(p_persistent_isolate_data)) {
56+ // Create the root isolate as soon as the runtime controller is initialized.
6357 // It will be run at a later point when the engine provides a run
6458 // configuration and then runs the isolate.
65- create_and_config_root_isolate_ =
66- std::async (std::launch::deferred, [self = weak_factory_.GetWeakPtr ()]() {
67- if (!self) {
68- return ;
69- }
70-
71- auto strong_root_isolate =
72- DartIsolate::CreateRootIsolate (
73- self->vm_ ->GetVMData ()->GetSettings (), //
74- self->isolate_snapshot_ , //
75- self->task_runners_ , //
76- std::make_unique<PlatformConfiguration>(self.get ()), //
77- self->snapshot_delegate_ , //
78- self->hint_freed_delegate_ , //
79- self->io_manager_ , //
80- self->unref_queue_ , //
81- self->image_decoder_ , //
82- self->advisory_script_uri_ , //
83- self->advisory_script_entrypoint_ , //
84- nullptr , //
85- self->isolate_create_callback_ , //
86- self->isolate_shutdown_callback_ //
87- )
88- .lock ();
89-
90- FML_CHECK (strong_root_isolate) << " Could not create root isolate." ;
91-
92- // The root isolate ivar is weak.
93- self->root_isolate_ = strong_root_isolate;
94-
95- strong_root_isolate->SetReturnCodeCallback ([self](uint32_t code) {
96- if (!self) {
97- return ;
98- }
99-
100- self->root_isolate_return_code_ = {true , code};
101- });
102-
103- if (auto * platform_configuration =
104- self->GetPlatformConfigurationIfAvailable ()) {
105- tonic::DartState::Scope scope (strong_root_isolate);
106- platform_configuration->DidCreateIsolate ();
107- if (!self->FlushRuntimeStateToIsolate ()) {
108- FML_DLOG (ERROR) << " Could not setup initial isolate state." ;
109- }
110- } else {
111- FML_DCHECK (false )
112- << " RuntimeController created without window binding." ;
113- }
114-
115- FML_DCHECK (Dart_CurrentIsolate () == nullptr );
116-
117- self->client_ .OnRootIsolateCreated ();
118- return ;
119- });
120-
121- // We're still trying to create the root isolate as soon as possible here on
122- // the UI thread although it's deferred a little bit by
123- // std::async(std::launch::deferred, ...). So the callers of `GetRootIsolate`
124- // should get a quick return after this UI thread task.
125- task_runners_.GetUITaskRunner ()->PostTask (
126- [self = weak_factory_.GetWeakPtr ()]() {
127- if (!self) {
128- return ;
129- }
130-
131- self->GetRootIsolate ();
132- });
59+ auto strong_root_isolate =
60+ DartIsolate::CreateRootIsolate (
61+ vm_->GetVMData ()->GetSettings (), //
62+ isolate_snapshot_, //
63+ task_runners_, //
64+ std::make_unique<PlatformConfiguration>(this ), //
65+ snapshot_delegate_, //
66+ hint_freed_delegate_, //
67+ io_manager_, //
68+ unref_queue_, //
69+ image_decoder_, //
70+ p_advisory_script_uri, //
71+ p_advisory_script_entrypoint, //
72+ nullptr , //
73+ isolate_create_callback_, //
74+ isolate_shutdown_callback_ //
75+ )
76+ .lock ();
77+
78+ FML_CHECK (strong_root_isolate) << " Could not create root isolate." ;
79+
80+ // The root isolate ivar is weak.
81+ root_isolate_ = strong_root_isolate;
82+
83+ strong_root_isolate->SetReturnCodeCallback ([this ](uint32_t code) {
84+ root_isolate_return_code_ = {true , code};
85+ });
86+
87+ if (auto * platform_configuration = GetPlatformConfigurationIfAvailable ()) {
88+ tonic::DartState::Scope scope (strong_root_isolate);
89+ platform_configuration->DidCreateIsolate ();
90+ if (!FlushRuntimeStateToIsolate ()) {
91+ FML_DLOG (ERROR) << " Could not setup initial isolate state." ;
92+ }
93+ } else {
94+ FML_DCHECK (false ) << " RuntimeController created without window binding." ;
95+ }
96+
97+ FML_DCHECK (Dart_CurrentIsolate () == nullptr );
13398}
13499
135100RuntimeController::~RuntimeController () {
@@ -145,8 +110,8 @@ RuntimeController::~RuntimeController() {
145110 }
146111}
147112
148- bool RuntimeController::IsRootIsolateRunning () {
149- std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
113+ bool RuntimeController::IsRootIsolateRunning () const {
114+ std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
150115 if (root_isolate) {
151116 return root_isolate->GetPhase () == DartIsolate::Phase::Running;
152117 }
@@ -273,7 +238,7 @@ bool RuntimeController::ReportTimings(std::vector<int64_t> timings) {
273238}
274239
275240bool RuntimeController::NotifyIdle (int64_t deadline, size_t freed_hint) {
276- std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
241+ std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
277242 if (!root_isolate) {
278243 return false ;
279244 }
@@ -333,7 +298,7 @@ bool RuntimeController::DispatchSemanticsAction(int32_t id,
333298
334299PlatformConfiguration*
335300RuntimeController::GetPlatformConfigurationIfAvailable () {
336- std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
301+ std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
337302 return root_isolate ? root_isolate->platform_configuration () : nullptr ;
338303}
339304
@@ -395,17 +360,17 @@ RuntimeController::ComputePlatformResolvedLocale(
395360}
396361
397362Dart_Port RuntimeController::GetMainPort () {
398- std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
363+ std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
399364 return root_isolate ? root_isolate->main_port () : ILLEGAL_PORT;
400365}
401366
402367std::string RuntimeController::GetIsolateName () {
403- std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
368+ std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
404369 return root_isolate ? root_isolate->debug_name () : " " ;
405370}
406371
407372bool RuntimeController::HasLivePorts () {
408- std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
373+ std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
409374 if (!root_isolate) {
410375 return false ;
411376 }
@@ -414,20 +379,11 @@ bool RuntimeController::HasLivePorts() {
414379}
415380
416381tonic::DartErrorHandleType RuntimeController::GetLastError () {
417- std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
382+ std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
418383 return root_isolate ? root_isolate->GetLastError () : tonic::kNoError ;
419384}
420385
421386std::weak_ptr<DartIsolate> RuntimeController::GetRootIsolate () {
422- std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock ();
423- if (root_isolate) {
424- return root_isolate_;
425- }
426-
427- // Root isolate is not yet created, get it and do some configuration.
428- FML_DCHECK (create_and_config_root_isolate_.valid ());
429- create_and_config_root_isolate_.get ();
430-
431387 return root_isolate_;
432388}
433389
0 commit comments