@@ -46,48 +46,76 @@ RuntimeController::RuntimeController(
46
46
platform_data_(std::move(p_platform_data)),
47
47
isolate_create_callback_(p_isolate_create_callback),
48
48
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.
51
54
// It will be run at a later point when the engine provides a run
52
55
// 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
+ });
91
119
}
92
120
93
121
RuntimeController::~RuntimeController () {
@@ -103,8 +131,8 @@ RuntimeController::~RuntimeController() {
103
131
}
104
132
}
105
133
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 ();
108
136
if (root_isolate) {
109
137
return root_isolate->GetPhase () == DartIsolate::Phase::Running;
110
138
}
@@ -230,7 +258,7 @@ bool RuntimeController::ReportTimings(std::vector<int64_t> timings) {
230
258
}
231
259
232
260
bool RuntimeController::NotifyIdle (int64_t deadline) {
233
- std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
261
+ std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
234
262
if (!root_isolate) {
235
263
return false ;
236
264
}
@@ -287,7 +315,7 @@ bool RuntimeController::DispatchSemanticsAction(int32_t id,
287
315
288
316
PlatformConfiguration*
289
317
RuntimeController::GetPlatformConfigurationIfAvailable () {
290
- std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
318
+ std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
291
319
return root_isolate ? root_isolate->platform_configuration () : nullptr ;
292
320
}
293
321
@@ -349,17 +377,17 @@ RuntimeController::ComputePlatformResolvedLocale(
349
377
}
350
378
351
379
Dart_Port RuntimeController::GetMainPort () {
352
- std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
380
+ std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
353
381
return root_isolate ? root_isolate->main_port () : ILLEGAL_PORT;
354
382
}
355
383
356
384
std::string RuntimeController::GetIsolateName () {
357
- std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
385
+ std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
358
386
return root_isolate ? root_isolate->debug_name () : " " ;
359
387
}
360
388
361
389
bool RuntimeController::HasLivePorts () {
362
- std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
390
+ std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
363
391
if (!root_isolate) {
364
392
return false ;
365
393
}
@@ -368,11 +396,20 @@ bool RuntimeController::HasLivePorts() {
368
396
}
369
397
370
398
tonic::DartErrorHandleType RuntimeController::GetLastError () {
371
- std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
399
+ std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
372
400
return root_isolate ? root_isolate->GetLastError () : tonic::kNoError ;
373
401
}
374
402
375
403
std::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
+
376
413
return root_isolate_;
377
414
}
378
415
0 commit comments