@@ -46,48 +46,76 @@ RuntimeController::RuntimeController(
4646 platform_data_(std::move(p_platform_data)),
4747 isolate_create_callback_(p_isolate_create_callback),
4848 isolate_shutdown_callback_(p_isolate_shutdown_callback),
49- persistent_isolate_data_(std::move(p_persistent_isolate_data)) {
50- // Create the root isolate as soon as the runtime controller is initialized.
49+ persistent_isolate_data_(std::move(p_persistent_isolate_data)),
50+ weak_factory_(this ) {
51+ // Create the root isolate as soon as the runtime controller is initialized,
52+ // but not using a synchronous way to avoid blocking the platform thread a
53+ // long time as it is waiting while creating `Shell` on that platform thread.
5154 // It will be run at a later point when the engine provides a run
5255 // configuration and then runs the isolate.
53- auto strong_root_isolate =
54- DartIsolate::CreateRootIsolate (
55- vm_->GetVMData ()->GetSettings (), //
56- isolate_snapshot_, //
57- task_runners_, //
58- std::make_unique<PlatformConfiguration>(this ), //
59- snapshot_delegate_, //
60- io_manager_, //
61- unref_queue_, //
62- image_decoder_, //
63- p_advisory_script_uri, //
64- p_advisory_script_entrypoint, //
65- nullptr , //
66- isolate_create_callback_, //
67- isolate_shutdown_callback_ //
68- )
69- .lock ();
70-
71- FML_CHECK (strong_root_isolate) << " Could not create root isolate." ;
72-
73- // The root isolate ivar is weak.
74- root_isolate_ = strong_root_isolate;
75-
76- strong_root_isolate->SetReturnCodeCallback ([this ](uint32_t code) {
77- root_isolate_return_code_ = {true , code};
78- });
79-
80- if (auto * platform_configuration = GetPlatformConfigurationIfAvailable ()) {
81- tonic::DartState::Scope scope (strong_root_isolate);
82- platform_configuration->DidCreateIsolate ();
83- if (!FlushRuntimeStateToIsolate ()) {
84- FML_DLOG (ERROR) << " Could not setup initial isolate state." ;
85- }
86- } else {
87- FML_DCHECK (false ) << " RuntimeController created without window binding." ;
88- }
89-
90- FML_DCHECK (Dart_CurrentIsolate () == nullptr );
56+ create_and_config_root_isolate_ =
57+ std::async (std::launch::deferred, [self = weak_factory_.GetWeakPtr ()]() {
58+ if (!self) {
59+ return ;
60+ }
61+
62+ auto strong_root_isolate =
63+ DartIsolate::CreateRootIsolate (
64+ self->vm_ ->GetVMData ()->GetSettings (), //
65+ self->isolate_snapshot_ , //
66+ self->task_runners_ , //
67+ std::make_unique<PlatformConfiguration>(self.get ()), //
68+ self->snapshot_delegate_ , //
69+ self->io_manager_ , //
70+ self->unref_queue_ , //
71+ self->image_decoder_ , //
72+ self->advisory_script_uri_ , //
73+ self->advisory_script_entrypoint_ , //
74+ nullptr , //
75+ self->isolate_create_callback_ , //
76+ self->isolate_shutdown_callback_ //
77+ )
78+ .lock ();
79+
80+ FML_CHECK (strong_root_isolate) << " Could not create root isolate." ;
81+
82+ // The root isolate ivar is weak.
83+ self->root_isolate_ = strong_root_isolate;
84+
85+ strong_root_isolate->SetReturnCodeCallback ([self](uint32_t code) {
86+ if (!self) {
87+ return ;
88+ }
89+
90+ self->root_isolate_return_code_ = {true , code};
91+ });
92+
93+ if (auto * platform_configuration =
94+ self->GetPlatformConfigurationIfAvailable ()) {
95+ tonic::DartState::Scope scope (strong_root_isolate);
96+ platform_configuration->DidCreateIsolate ();
97+ if (!self->FlushRuntimeStateToIsolate ()) {
98+ FML_DLOG (ERROR) << " Could not setup initial isolate state." ;
99+ }
100+ } else {
101+ FML_DCHECK (false )
102+ << " RuntimeController created without window binding." ;
103+ }
104+
105+ FML_DCHECK (Dart_CurrentIsolate () == nullptr );
106+
107+ self->client_ .OnRootIsolateCreated ();
108+ return ;
109+ });
110+
111+ task_runners_.GetUITaskRunner ()->PostTask (
112+ [self = weak_factory_.GetWeakPtr ()]() {
113+ if (!self) {
114+ return ;
115+ }
116+
117+ self->GetRootIsolate ();
118+ });
91119}
92120
93121RuntimeController::~RuntimeController () {
@@ -103,8 +131,8 @@ RuntimeController::~RuntimeController() {
103131 }
104132}
105133
106- bool RuntimeController::IsRootIsolateRunning () const {
107- std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
134+ bool RuntimeController::IsRootIsolateRunning () {
135+ std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
108136 if (root_isolate) {
109137 return root_isolate->GetPhase () == DartIsolate::Phase::Running;
110138 }
@@ -230,7 +258,7 @@ bool RuntimeController::ReportTimings(std::vector<int64_t> timings) {
230258}
231259
232260bool RuntimeController::NotifyIdle (int64_t deadline) {
233- std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
261+ std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
234262 if (!root_isolate) {
235263 return false ;
236264 }
@@ -287,7 +315,7 @@ bool RuntimeController::DispatchSemanticsAction(int32_t id,
287315
288316PlatformConfiguration*
289317RuntimeController::GetPlatformConfigurationIfAvailable () {
290- std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
318+ std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
291319 return root_isolate ? root_isolate->platform_configuration () : nullptr ;
292320}
293321
@@ -349,17 +377,17 @@ RuntimeController::ComputePlatformResolvedLocale(
349377}
350378
351379Dart_Port RuntimeController::GetMainPort () {
352- std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
380+ std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
353381 return root_isolate ? root_isolate->main_port () : ILLEGAL_PORT;
354382}
355383
356384std::string RuntimeController::GetIsolateName () {
357- std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
385+ std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
358386 return root_isolate ? root_isolate->debug_name () : " " ;
359387}
360388
361389bool RuntimeController::HasLivePorts () {
362- std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
390+ std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
363391 if (!root_isolate) {
364392 return false ;
365393 }
@@ -368,11 +396,20 @@ bool RuntimeController::HasLivePorts() {
368396}
369397
370398tonic::DartErrorHandleType RuntimeController::GetLastError () {
371- std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
399+ std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
372400 return root_isolate ? root_isolate->GetLastError () : tonic::kNoError ;
373401}
374402
375403std::weak_ptr<DartIsolate> RuntimeController::GetRootIsolate () {
404+ std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock ();
405+ if (root_isolate) {
406+ return root_isolate_;
407+ }
408+
409+ // Root isolate is not yet created, get it and do some configuration.
410+ FML_DCHECK (create_and_config_root_isolate_.valid ());
411+ create_and_config_root_isolate_.get ();
412+
376413 return root_isolate_;
377414}
378415
0 commit comments