Flutter Engine
 
Loading...
Searching...
No Matches
dart_isolate_runner.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <utility>
8
10
11namespace flutter::testing {
12
13AutoIsolateShutdown::AutoIsolateShutdown(std::shared_ptr<DartIsolate> isolate,
15 : isolate_(std::move(isolate)), runner_(std::move(runner)) {}
16
18 if (!isolate_->IsShuttingDown()) {
19 Shutdown();
20 }
22 fml::TaskRunner::RunNowOrPostTask(runner_, [this, &latch]() {
23 // Delete isolate on thread.
24 isolate_.reset();
25 latch.Signal();
26 });
27 latch.Wait();
28}
29
31 if (!IsValid()) {
32 return;
33 }
36 runner_, [isolate = isolate_.get(), &latch]() {
37 if (!isolate->Shutdown()) {
38 FML_LOG(ERROR) << "Could not shutdown isolate.";
39 FML_CHECK(false);
40 }
41 latch.Signal();
42 });
43 latch.Wait();
44}
45
46[[nodiscard]] bool AutoIsolateShutdown::RunInIsolateScope(
47 const std::function<bool(void)>& closure) {
48 if (!IsValid()) {
49 return false;
50 }
51
52 bool result = false;
55 runner_, [this, &result, &latch, closure]() {
56 tonic::DartIsolateScope scope(isolate_->isolate());
57 tonic::DartApiScope api_scope;
58 if (closure) {
59 result = closure();
60 }
61 latch.Signal();
62 });
63 latch.Wait();
64 return result;
65}
66
67std::unique_ptr<AutoIsolateShutdown> RunDartCodeInIsolateOnUITaskRunner(
68 DartVMRef& vm_ref,
69 const Settings& p_settings,
70 const TaskRunners& task_runners,
71 std::string entrypoint,
72 const std::vector<std::string>& args,
73 const std::string& kernel_file_path,
74 fml::WeakPtr<IOManager> io_manager,
75 std::unique_ptr<PlatformConfiguration> platform_configuration) {
77
78 if (!vm_ref) {
79 return nullptr;
80 }
81
82 auto vm_data = vm_ref.GetVMData();
83
84 if (!vm_data) {
85 return nullptr;
86 }
87
88 auto settings = p_settings;
89
91 if (!fml::IsFile(kernel_file_path)) {
92 FML_LOG(ERROR) << "Could not locate kernel file.";
93 return nullptr;
94 }
95
96 auto kernel_file = fml::OpenFile(kernel_file_path.c_str(), false,
98
99 if (!kernel_file.is_valid()) {
100 FML_LOG(ERROR) << "Kernel file descriptor was invalid.";
101 return nullptr;
102 }
103
104 auto kernel_mapping = std::make_unique<fml::FileMapping>(kernel_file);
105
106 if (kernel_mapping->GetMapping() == nullptr) {
107 FML_LOG(ERROR) << "Could not set up kernel mapping.";
108 return nullptr;
109 }
110
111 settings.application_kernels = fml::MakeCopyable(
112 [kernel_mapping = std::move(kernel_mapping)]() mutable -> Mappings {
113 Mappings mappings;
114 mappings.emplace_back(std::move(kernel_mapping));
115 return mappings;
116 });
117 }
118
119 auto isolate_configuration =
121
122 UIDartState::Context context(task_runners);
123 context.io_manager = std::move(io_manager);
124 context.advisory_script_uri = "main.dart";
125 context.advisory_script_entrypoint = entrypoint.c_str();
126 context.enable_impeller = p_settings.enable_impeller;
127
128 auto isolate =
130 settings, // settings
131 vm_data->GetIsolateSnapshot(), // isolate snapshot
132 std::move(platform_configuration), // platform configuration
133 DartIsolate::Flags{}, // flags
134 nullptr, // root isolate create callback
135 settings.isolate_create_callback, // isolate create callback
136 settings.isolate_shutdown_callback, // isolate shutdown callback
137 entrypoint, // entrypoint
138 std::nullopt, // library
139 args, // args
140 std::move(isolate_configuration), // isolate configuration
141 context // engine context
142 )
143 .lock();
144
145 if (!isolate) {
146 FML_LOG(ERROR) << "Could not create running isolate.";
147 return nullptr;
148 }
149
150 return std::make_unique<AutoIsolateShutdown>(
151 isolate, context.task_runners.GetUITaskRunner());
152}
153
154std::unique_ptr<AutoIsolateShutdown> RunDartCodeInIsolate(
155 DartVMRef& vm_ref,
156 const Settings& settings,
157 const TaskRunners& task_runners,
158 std::string entrypoint,
159 const std::vector<std::string>& args,
160 const std::string& kernel_file_path,
161 fml::WeakPtr<IOManager> io_manager,
162 std::unique_ptr<PlatformConfiguration> platform_configuration) {
163 std::unique_ptr<AutoIsolateShutdown> result;
166 task_runners.GetUITaskRunner(), fml::MakeCopyable([&]() mutable {
167 result = RunDartCodeInIsolateOnUITaskRunner(
168 vm_ref, settings, task_runners, entrypoint, args, kernel_file_path,
169 io_manager, std::move(platform_configuration));
170 latch.Signal();
171 }));
172 latch.Wait();
173 return result;
174}
175
176} // namespace flutter::testing
static std::weak_ptr< DartIsolate > CreateRunningRootIsolate(const Settings &settings, const fml::RefPtr< const DartSnapshot > &isolate_snapshot, std::unique_ptr< PlatformConfiguration > platform_configuration, Flags flags, const fml::closure &root_isolate_create_callback, const fml::closure &isolate_create_callback, const fml::closure &isolate_shutdown_callback, std::optional< std::string > dart_entrypoint, std::optional< std::string > dart_entrypoint_library, const std::vector< std::string > &dart_entrypoint_args, std::unique_ptr< IsolateConfiguration > isolate_configuration, const UIDartState::Context &context, const DartIsolate *spawning_isolate=nullptr, std::shared_ptr< NativeAssetsManager > native_assets_manager=nullptr)
Creates an instance of a root isolate and returns a weak pointer to the same. The isolate instance ma...
static bool IsRunningPrecompiledCode()
Checks if VM instances in the process can run precompiled code. This call can be made at any time and...
Definition dart_vm.cc:176
static std::shared_ptr< const DartVMData > GetVMData()
static std::unique_ptr< IsolateConfiguration > InferFromSettings(const Settings &settings, const std::shared_ptr< AssetManager > &asset_manager=nullptr, const fml::RefPtr< fml::TaskRunner > &io_worker=nullptr, IsolateLaunchType launch_type=IsolateLaunchType::kNewGroup)
Attempts to infer the isolate configuration from the Settings object. If the VM is configured for AOT...
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
virtual bool RunsTasksOnCurrentThread()
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
#define FML_LOG(severity)
Definition logging.h:101
#define FML_CHECK(condition)
Definition logging.h:104
std::unique_ptr< AutoIsolateShutdown > RunDartCodeInIsolate(DartVMRef &vm_ref, const Settings &settings, const TaskRunners &task_runners, std::string entrypoint, const std::vector< std::string > &args, const std::string &kernel_file_path, fml::WeakPtr< IOManager > io_manager, std::unique_ptr< PlatformConfiguration > platform_configuration)
std::unique_ptr< AutoIsolateShutdown > RunDartCodeInIsolateOnUITaskRunner(DartVMRef &vm_ref, const Settings &p_settings, const TaskRunners &task_runners, std::string entrypoint, const std::vector< std::string > &args, const std::string &kernel_file_path, fml::WeakPtr< IOManager > io_manager, std::unique_ptr< PlatformConfiguration > platform_configuration)
std::vector< std::unique_ptr< const fml::Mapping > > Mappings
Definition settings.h:88
internal::CopyableLambda< T > MakeCopyable(T lambda)
bool IsFile(const std::string &path)
fml::UniqueFD OpenFile(const char *path, bool create_if_necessary, FilePermission permission)
This can open a directory on POSIX, but not on Windows.
Definition file_posix.cc:66
Definition ref_ptr.h:261
The subset of state which is owned by the shell or engine and passed through the RuntimeController in...
fml::WeakPtr< IOManager > io_manager
The IO manager used by the isolate for asynchronous texture uploads.
const TaskRunners task_runners
bool enable_impeller
Whether Impeller is enabled or not.