|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
| 5 | +#define FML_USED_ON_EMBEDDER |
| 6 | + |
5 | 7 | #include "flutter/runtime/runtime_controller.h" |
6 | 8 |
|
7 | 9 | #include "flutter/fml/message_loop.h" |
@@ -44,47 +46,62 @@ RuntimeController::RuntimeController( |
44 | 46 | window_data_(std::move(p_window_data)), |
45 | 47 | isolate_create_callback_(p_isolate_create_callback), |
46 | 48 | isolate_shutdown_callback_(p_isolate_shutdown_callback), |
47 | | - persistent_isolate_data_(std::move(p_persistent_isolate_data)) { |
48 | | - // 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. |
49 | 54 | // It will be run at a later point when the engine provides a run |
50 | 55 | // configuration and then runs the isolate. |
51 | | - auto strong_root_isolate = |
52 | | - DartIsolate::CreateRootIsolate(vm_->GetVMData()->GetSettings(), // |
53 | | - isolate_snapshot_, // |
54 | | - task_runners_, // |
55 | | - std::make_unique<Window>(this), // |
56 | | - snapshot_delegate_, // |
57 | | - io_manager_, // |
58 | | - unref_queue_, // |
59 | | - image_decoder_, // |
60 | | - p_advisory_script_uri, // |
61 | | - p_advisory_script_entrypoint, // |
62 | | - nullptr, // |
63 | | - isolate_create_callback_, // |
64 | | - isolate_shutdown_callback_ // |
65 | | - ) |
66 | | - .lock(); |
67 | | - |
68 | | - FML_CHECK(strong_root_isolate) << "Could not create root isolate."; |
69 | | - |
70 | | - // The root isolate ivar is weak. |
71 | | - root_isolate_ = strong_root_isolate; |
72 | | - |
73 | | - strong_root_isolate->SetReturnCodeCallback([this](uint32_t code) { |
74 | | - root_isolate_return_code_ = {true, code}; |
75 | | - }); |
76 | | - |
77 | | - if (auto* window = GetWindowIfAvailable()) { |
78 | | - tonic::DartState::Scope scope(strong_root_isolate); |
79 | | - window->DidCreateIsolate(); |
80 | | - if (!FlushRuntimeStateToIsolate()) { |
81 | | - FML_DLOG(ERROR) << "Could not setup initial isolate state."; |
82 | | - } |
83 | | - } else { |
84 | | - FML_DCHECK(false) << "RuntimeController created without window binding."; |
85 | | - } |
86 | | - |
87 | | - FML_DCHECK(Dart_CurrentIsolate() == nullptr); |
| 56 | + fml::MessageLoop::GetCurrent().GetTaskRunner()->PostTask( |
| 57 | + [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<Window>(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 | + FML_CHECK(strong_root_isolate) << "Could not create root isolate."; |
| 80 | + |
| 81 | + // The root isolate ivar is weak. |
| 82 | + self->root_isolate_ = strong_root_isolate; |
| 83 | + |
| 84 | + strong_root_isolate->SetReturnCodeCallback([self](uint32_t code) { |
| 85 | + if (!self) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + self->root_isolate_return_code_ = {true, code}; |
| 90 | + }); |
| 91 | + |
| 92 | + if (auto* window = self->GetWindowIfAvailable()) { |
| 93 | + tonic::DartState::Scope scope(strong_root_isolate); |
| 94 | + window->DidCreateIsolate(); |
| 95 | + if (!self->FlushRuntimeStateToIsolate()) { |
| 96 | + FML_DLOG(ERROR) << "Could not setup initial isolate state."; |
| 97 | + } |
| 98 | + } else { |
| 99 | + FML_DCHECK(false) |
| 100 | + << "RuntimeController created without window binding."; |
| 101 | + } |
| 102 | + |
| 103 | + FML_DCHECK(Dart_CurrentIsolate() == nullptr); |
| 104 | + }); |
88 | 105 | } |
89 | 106 |
|
90 | 107 | RuntimeController::~RuntimeController() { |
|
0 commit comments