@@ -18,10 +18,7 @@ namespace flutter {
18
18
19
19
RuntimeController::RuntimeController (RuntimeDelegate& client,
20
20
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) {}
25
22
26
23
RuntimeController::RuntimeController (
27
24
RuntimeDelegate& p_client,
@@ -55,81 +52,49 @@ RuntimeController::RuntimeController(
55
52
platform_data_(std::move(p_platform_data)),
56
53
isolate_create_callback_(p_isolate_create_callback),
57
54
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.
63
57
// It will be run at a later point when the engine provides a run
64
58
// 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 );
133
98
}
134
99
135
100
RuntimeController::~RuntimeController () {
@@ -145,8 +110,8 @@ RuntimeController::~RuntimeController() {
145
110
}
146
111
}
147
112
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 ();
150
115
if (root_isolate) {
151
116
return root_isolate->GetPhase () == DartIsolate::Phase::Running;
152
117
}
@@ -273,7 +238,7 @@ bool RuntimeController::ReportTimings(std::vector<int64_t> timings) {
273
238
}
274
239
275
240
bool 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 ();
277
242
if (!root_isolate) {
278
243
return false ;
279
244
}
@@ -333,7 +298,7 @@ bool RuntimeController::DispatchSemanticsAction(int32_t id,
333
298
334
299
PlatformConfiguration*
335
300
RuntimeController::GetPlatformConfigurationIfAvailable () {
336
- std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
301
+ std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
337
302
return root_isolate ? root_isolate->platform_configuration () : nullptr ;
338
303
}
339
304
@@ -395,17 +360,17 @@ RuntimeController::ComputePlatformResolvedLocale(
395
360
}
396
361
397
362
Dart_Port RuntimeController::GetMainPort () {
398
- std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
363
+ std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
399
364
return root_isolate ? root_isolate->main_port () : ILLEGAL_PORT;
400
365
}
401
366
402
367
std::string RuntimeController::GetIsolateName () {
403
- std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
368
+ std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
404
369
return root_isolate ? root_isolate->debug_name () : " " ;
405
370
}
406
371
407
372
bool RuntimeController::HasLivePorts () {
408
- std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
373
+ std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
409
374
if (!root_isolate) {
410
375
return false ;
411
376
}
@@ -414,20 +379,11 @@ bool RuntimeController::HasLivePorts() {
414
379
}
415
380
416
381
tonic::DartErrorHandleType RuntimeController::GetLastError () {
417
- std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate () .lock ();
382
+ std::shared_ptr<DartIsolate> root_isolate = root_isolate_ .lock ();
418
383
return root_isolate ? root_isolate->GetLastError () : tonic::kNoError ;
419
384
}
420
385
421
386
std::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
-
431
387
return root_isolate_;
432
388
}
433
389
0 commit comments