Flutter Engine
 
Loading...
Searching...
No Matches
embedder_engine.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
9
10namespace flutter {
11
12struct ShellArgs {
16 ShellArgs(const Settings& p_settings,
17 Shell::CreateCallback<PlatformView> p_on_create_platform_view,
18 Shell::CreateCallback<Rasterizer> p_on_create_rasterizer)
19 : settings(p_settings),
20 on_create_platform_view(std::move(p_on_create_platform_view)),
21 on_create_rasterizer(std::move(p_on_create_rasterizer)) {}
22};
23
25 std::unique_ptr<EmbedderThreadHost> thread_host,
26 const flutter::TaskRunners& task_runners,
27 const flutter::Settings& settings,
28 RunConfiguration run_configuration,
29 const Shell::CreateCallback<PlatformView>& on_create_platform_view,
30 const Shell::CreateCallback<Rasterizer>& on_create_rasterizer,
31 std::unique_ptr<EmbedderExternalTextureResolver> external_texture_resolver)
32 : thread_host_(std::move(thread_host)),
33 task_runners_(task_runners),
34 run_configuration_(std::move(run_configuration)),
35 shell_args_(std::make_unique<ShellArgs>(settings,
36 on_create_platform_view,
37 on_create_rasterizer)),
38 external_texture_resolver_(std::move(external_texture_resolver)) {}
39
41
43 if (!shell_args_) {
44 FML_DLOG(ERROR) << "Invalid shell arguments.";
45 return false;
46 }
47
48 if (shell_) {
49 FML_DLOG(ERROR) << "Shell already initialized";
50 }
51
52 shell_ = Shell::Create(
53 flutter::PlatformData(), task_runners_, shell_args_->settings,
54 shell_args_->on_create_platform_view, shell_args_->on_create_rasterizer);
55
56 // Reset the args no matter what. They will never be used to initialize a
57 // shell again.
58 shell_args_.reset();
59
60 return IsValid();
61}
62
64 shell_.reset();
65 return IsValid();
66}
67
69 if (!thread_host_) {
70 return;
71 }
72
73 // Once the collected, EmbedderThreadHost::RunnerIsValid will return false for
74 // all runners belonging to this thread host. This must be done with UI task
75 // runner blocked to prevent possible raciness that could happen when
76 // destroying the thread host in the middle of UI task runner execution. This
77 // is not an issue for other runners, because raster task runner should not
78 // have anything scheduled after engine shutdown and platform task runner is
79 // where this method is called from.
80 if (thread_host_->GetTaskRunners().GetUITaskRunner() &&
81 !thread_host_->GetTaskRunners()
82 .GetUITaskRunner()
83 ->RunsTasksOnCurrentThread()) {
84 fml::AutoResetWaitableEvent ui_thread_running;
85 fml::AutoResetWaitableEvent ui_thread_block;
86 fml::AutoResetWaitableEvent ui_thread_finished;
87
88 thread_host_->GetTaskRunners().GetUITaskRunner()->PostTask([&] {
89 ui_thread_running.Signal();
90 ui_thread_block.Wait();
91 ui_thread_finished.Signal();
92 });
93
94 // Wait until the task is running on the UI thread.
95 ui_thread_running.Wait();
96 thread_host_->InvalidateActiveRunners();
97 ui_thread_block.Signal();
98
99 // Needed to keep ui_thread_block in scope until the UI thread execution
100 // finishes.
101 ui_thread_finished.Wait();
102 } else {
103 thread_host_->InvalidateActiveRunners();
104 }
105 thread_host_.reset();
106}
107
109 if (!IsValid() || !run_configuration_.IsValid()) {
110 return false;
111 }
112 shell_->RunEngine(std::move(run_configuration_));
113 return true;
114}
115
117 return static_cast<bool>(shell_);
118}
119
121 return task_runners_;
122}
123
125 if (!IsValid()) {
126 return false;
127 }
128
129 shell_->GetPlatformView()->NotifyCreated();
130 return true;
131}
132
134 if (!IsValid()) {
135 return false;
136 }
137
138 shell_->GetPlatformView()->NotifyDestroyed();
139
140 return true;
141}
142
144 int64_t view_id,
145 const flutter::ViewportMetrics& metrics) {
146 if (!IsValid()) {
147 return false;
148 }
149
150 auto platform_view = shell_->GetPlatformView();
151 if (!platform_view) {
152 return false;
153 }
154 platform_view->SetViewportMetrics(view_id, metrics);
155 return true;
156}
157
159 std::unique_ptr<flutter::PointerDataPacket> packet) {
160 if (!IsValid() || !packet) {
161 return false;
162 }
163
164 auto platform_view = shell_->GetPlatformView();
165 if (!platform_view) {
166 return false;
167 }
168
169 platform_view->DispatchPointerDataPacket(std::move(packet));
170 return true;
171}
172
174 std::unique_ptr<PlatformMessage> message) {
175 if (!IsValid() || !message) {
176 return false;
177 }
178
179 auto platform_view = shell_->GetPlatformView();
180 if (!platform_view) {
181 return false;
182 }
183
184 platform_view->DispatchPlatformMessage(std::move(message));
185 return true;
186}
187
189 if (!IsValid()) {
190 return false;
191 }
192 shell_->GetPlatformView()->RegisterTexture(
193 external_texture_resolver_->ResolveExternalTexture(texture));
194 return true;
195}
196
198 if (!IsValid()) {
199 return false;
200 }
201 shell_->GetPlatformView()->UnregisterTexture(texture);
202 return true;
203}
204
206 if (!IsValid()) {
207 return false;
208 }
209 shell_->GetPlatformView()->MarkTextureFrameAvailable(texture);
210 return true;
211}
212
214 if (!IsValid()) {
215 return false;
216 }
217
218 auto platform_view = shell_->GetPlatformView();
219 if (!platform_view) {
220 return false;
221 }
222 platform_view->SetSemanticsEnabled(enabled);
223 return true;
224}
225
227 if (!IsValid()) {
228 return false;
229 }
230 auto platform_view = shell_->GetPlatformView();
231 if (!platform_view) {
232 return false;
233 }
234 platform_view->SetAccessibilityFeatures(flags);
235 return true;
236}
237
239 int node_id,
242 if (!IsValid()) {
243 return false;
244 }
245 auto platform_view = shell_->GetPlatformView();
246 if (!platform_view) {
247 return false;
248 }
249 platform_view->DispatchSemanticsAction(view_id, node_id, action,
250 std::move(args));
251 return true;
252}
253
255 fml::TimePoint frame_start_time,
256 fml::TimePoint frame_target_time) {
257 if (!IsValid()) {
258 return false;
259 }
260
262 task_runners_, baton, frame_start_time, frame_target_time);
263}
264
266 if (!IsValid()) {
267 return false;
268 }
269
270 return shell_->ReloadSystemFonts();
271}
272
274 if (!IsValid()) {
275 return false;
276 }
277
278 shell_->GetTaskRunners().GetRasterTaskRunner()->PostTask(task);
279 return true;
280}
281
283 // The shell doesn't need to be running or valid for access to the thread
284 // host. This is why there is no `IsValid` check here. This allows embedders
285 // to perform custom task runner interop before the shell is running.
286 if (task == nullptr) {
287 return false;
288 }
289 auto result = thread_host_->PostTask(reinterpret_cast<intptr_t>(task->runner),
290 task->task);
291 // If the UI and platform threads are separate, the microtask queue is
292 // flushed through MessageLoopTaskQueues observer.
293 // If the UI and platform threads are merged, the UI task runner has no
294 // associated task queue, and microtasks need to be flushed manually
295 // after running the task.
296 if (result && shell_ && task_runners_.GetUITaskRunner() &&
297 task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread() &&
298 !task_runners_.GetUITaskRunner()->GetTaskQueueId().is_valid()) {
299 shell_->FlushMicrotaskQueue();
300 }
301
302 return result;
303}
304
306 const std::function<void(FlutterNativeThreadType)>& closure) const {
307 if (!IsValid() || closure == nullptr) {
308 return false;
309 }
310
311 const auto trampoline = [closure](
313 const fml::RefPtr<fml::TaskRunner>& runner) {
314 runner->PostTask([closure, type] { closure(type); });
315 };
316
317 // Post the task to all thread host threads.
318 const auto& task_runners = shell_->GetTaskRunners();
320 task_runners.GetRasterTaskRunner());
321 trampoline(kFlutterNativeThreadTypeWorker, task_runners.GetIOTaskRunner());
322 trampoline(kFlutterNativeThreadTypeUI, task_runners.GetUITaskRunner());
324 task_runners.GetPlatformTaskRunner());
325
326 // Post the task to all worker threads.
327 auto vm = shell_->GetDartVM();
328 vm->GetConcurrentMessageLoop()->PostTaskToAllWorkers(
329 [closure]() { closure(kFlutterNativeThreadTypeWorker); });
330
331 return true;
332}
333
335 if (!IsValid()) {
336 return false;
337 }
338
339 auto platform_view = shell_->GetPlatformView();
340 if (!platform_view) {
341 return false;
342 }
343 platform_view->ScheduleFrame();
344 return true;
345}
346
348 FML_DCHECK(shell_);
349 return *shell_.get();
350}
351
352} // namespace flutter
std::unique_ptr< flutter::PlatformViewIOS > platform_view
GLenum type
bool MarkTextureFrameAvailable(int64_t texture)
bool PostTaskOnEngineManagedNativeThreads(const std::function< void(FlutterNativeThreadType)> &closure) const
EmbedderEngine(std::unique_ptr< EmbedderThreadHost > thread_host, const TaskRunners &task_runners, const Settings &settings, RunConfiguration run_configuration, const Shell::CreateCallback< PlatformView > &on_create_platform_view, const Shell::CreateCallback< Rasterizer > &on_create_rasterizer, std::unique_ptr< EmbedderExternalTextureResolver > external_texture_resolver)
bool RegisterTexture(int64_t texture)
bool PostRenderThreadTask(const fml::closure &task)
bool SetAccessibilityFeatures(int32_t flags)
bool SetViewportMetrics(int64_t view_id, const flutter::ViewportMetrics &metrics)
bool DispatchSemanticsAction(int64_t view_id, int node_id, flutter::SemanticsAction action, fml::MallocMapping args)
bool RunTask(const FlutterTask *task)
bool DispatchPointerDataPacket(std::unique_ptr< flutter::PointerDataPacket > packet)
const TaskRunners & GetTaskRunners() const
bool UnregisterTexture(int64_t texture)
bool SendPlatformMessage(std::unique_ptr< PlatformMessage > message)
bool SetSemanticsEnabled(bool enabled)
bool OnVsyncEvent(intptr_t baton, fml::TimePoint frame_start_time, fml::TimePoint frame_target_time)
Specifies all the configuration required by the runtime library to launch the root isolate....
bool IsValid() const
A valid run configuration only guarantees that the engine should be able to find the assets and the i...
static std::unique_ptr< Shell > Create(const PlatformData &platform_data, const TaskRunners &task_runners, Settings settings, const CreateCallback< PlatformView > &on_create_platform_view, const CreateCallback< Rasterizer > &on_create_rasterizer, bool is_gpu_disabled=false)
Creates a shell instance using the provided settings. The callbacks to create the various shell subco...
Definition shell.cc:221
std::function< std::unique_ptr< T >(Shell &)> CreateCallback
Definition shell.h:121
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
static bool OnEmbedderVsync(const flutter::TaskRunners &task_runners, intptr_t baton, fml::TimePoint frame_start_time, fml::TimePoint frame_target_time)
A Mapping like NonOwnedMapping, but uses Free as its release proc.
Definition mapping.h:144
bool is_valid() const
virtual bool RunsTasksOnCurrentThread()
virtual TaskQueueId GetTaskQueueId()
FlutterNativeThreadType
Definition embedder.h:2404
@ kFlutterNativeThreadTypeWorker
Definition embedder.h:2418
@ kFlutterNativeThreadTypeUI
Definition embedder.h:2415
@ kFlutterNativeThreadTypePlatform
Definition embedder.h:2408
@ kFlutterNativeThreadTypeRender
Definition embedder.h:2412
TaskRunners task_runners_
ThreadHost thread_host_
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
G_BEGIN_DECLS GBytes * message
G_BEGIN_DECLS FlutterViewId view_id
#define FML_DLOG(severity)
Definition logging.h:121
#define FML_DCHECK(condition)
Definition logging.h:122
FlTexture * texture
std::function< void()> closure
Definition closure.h:14
Definition ref_ptr.h:261
FlutterTaskRunner runner
Definition embedder.h:1849
uint64_t task
Definition embedder.h:1850
Shell::CreateCallback< PlatformView > on_create_platform_view
ShellArgs(const Settings &p_settings, Shell::CreateCallback< PlatformView > p_on_create_platform_view, Shell::CreateCallback< Rasterizer > p_on_create_rasterizer)
Shell::CreateCallback< Rasterizer > on_create_rasterizer