Flutter Engine
The Flutter Engine
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
5#include "flutter/shell/platform/embedder/embedder_engine.h"
6
7#include "flutter/fml/make_copyable.h"
8#include "flutter/shell/platform/embedder/vsync_waiter_embedder.h"
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,
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 (!IsValid() || !run_configuration_.IsValid()) {
70 return false;
71 }
72 shell_->RunEngine(std::move(run_configuration_));
73 return true;
74}
75
77 return static_cast<bool>(shell_);
78}
79
81 return task_runners_;
82}
83
85 if (!IsValid()) {
86 return false;
87 }
88
89 shell_->GetPlatformView()->NotifyCreated();
90 return true;
91}
92
94 if (!IsValid()) {
95 return false;
96 }
97
98 shell_->GetPlatformView()->NotifyDestroyed();
99 return true;
100}
101
103 int64_t view_id,
104 const flutter::ViewportMetrics& metrics) {
105 if (!IsValid()) {
106 return false;
107 }
108
109 auto platform_view = shell_->GetPlatformView();
110 if (!platform_view) {
111 return false;
112 }
113 platform_view->SetViewportMetrics(view_id, metrics);
114 return true;
115}
116
118 std::unique_ptr<flutter::PointerDataPacket> packet) {
119 if (!IsValid() || !packet) {
120 return false;
121 }
122
123 auto platform_view = shell_->GetPlatformView();
124 if (!platform_view) {
125 return false;
126 }
127
128 platform_view->DispatchPointerDataPacket(std::move(packet));
129 return true;
130}
131
133 std::unique_ptr<PlatformMessage> message) {
134 if (!IsValid() || !message) {
135 return false;
136 }
137
138 auto platform_view = shell_->GetPlatformView();
139 if (!platform_view) {
140 return false;
141 }
142
143 platform_view->DispatchPlatformMessage(std::move(message));
144 return true;
145}
146
148 if (!IsValid()) {
149 return false;
150 }
151 shell_->GetPlatformView()->RegisterTexture(
152 external_texture_resolver_->ResolveExternalTexture(texture));
153 return true;
154}
155
157 if (!IsValid()) {
158 return false;
159 }
160 shell_->GetPlatformView()->UnregisterTexture(texture);
161 return true;
162}
163
165 if (!IsValid()) {
166 return false;
167 }
168 shell_->GetPlatformView()->MarkTextureFrameAvailable(texture);
169 return true;
170}
171
173 if (!IsValid()) {
174 return false;
175 }
176
177 auto platform_view = shell_->GetPlatformView();
178 if (!platform_view) {
179 return false;
180 }
181 platform_view->SetSemanticsEnabled(enabled);
182 return true;
183}
184
186 if (!IsValid()) {
187 return false;
188 }
189 auto platform_view = shell_->GetPlatformView();
190 if (!platform_view) {
191 return false;
192 }
193 platform_view->SetAccessibilityFeatures(flags);
194 return true;
195}
196
200 if (!IsValid()) {
201 return false;
202 }
203 auto platform_view = shell_->GetPlatformView();
204 if (!platform_view) {
205 return false;
206 }
207 platform_view->DispatchSemanticsAction(node_id, action, std::move(args));
208 return true;
209}
210
212 fml::TimePoint frame_start_time,
213 fml::TimePoint frame_target_time) {
214 if (!IsValid()) {
215 return false;
216 }
217
219 task_runners_, baton, frame_start_time, frame_target_time);
220}
221
223 if (!IsValid()) {
224 return false;
225 }
226
227 return shell_->ReloadSystemFonts();
228}
229
231 if (!IsValid()) {
232 return false;
233 }
234
235 shell_->GetTaskRunners().GetRasterTaskRunner()->PostTask(task);
236 return true;
237}
238
240 // The shell doesn't need to be running or valid for access to the thread
241 // host. This is why there is no `IsValid` check here. This allows embedders
242 // to perform custom task runner interop before the shell is running.
243 if (task == nullptr) {
244 return false;
245 }
246 return thread_host_->PostTask(reinterpret_cast<int64_t>(task->runner),
247 task->task);
248}
249
251 const std::function<void(FlutterNativeThreadType)>& closure) const {
252 if (!IsValid() || closure == nullptr) {
253 return false;
254 }
255
256 const auto trampoline = [closure](
258 const fml::RefPtr<fml::TaskRunner>& runner) {
259 runner->PostTask([closure, type] { closure(type); });
260 };
261
262 // Post the task to all thread host threads.
263 const auto& task_runners = shell_->GetTaskRunners();
265 task_runners.GetRasterTaskRunner());
266 trampoline(kFlutterNativeThreadTypeWorker, task_runners.GetIOTaskRunner());
267 trampoline(kFlutterNativeThreadTypeUI, task_runners.GetUITaskRunner());
269 task_runners.GetPlatformTaskRunner());
270
271 // Post the task to all worker threads.
272 auto vm = shell_->GetDartVM();
273 vm->GetConcurrentMessageLoop()->PostTaskToAllWorkers(
275
276 return true;
277}
278
280 if (!IsValid()) {
281 return false;
282 }
283
284 auto platform_view = shell_->GetPlatformView();
285 if (!platform_view) {
286 return false;
287 }
288 platform_view->ScheduleFrame();
289 return true;
290}
291
293 FML_DCHECK(shell_);
294 return *shell_.get();
295}
296
297} // 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 RunTask(const FlutterTask *task)
bool DispatchPointerDataPacket(std::unique_ptr< flutter::PointerDataPacket > packet)
const TaskRunners & GetTaskRunners() const
bool DispatchSemanticsAction(int node_id, flutter::SemanticsAction action, fml::MallocMapping args)
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:169
std::function< std::unique_ptr< T >(Shell &)> CreateCallback
Definition: shell.h:120
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
FlutterNativeThreadType
Definition: embedder.h:2086
@ kFlutterNativeThreadTypeWorker
Definition: embedder.h:2100
@ kFlutterNativeThreadTypeUI
Definition: embedder.h:2097
@ kFlutterNativeThreadTypePlatform
Definition: embedder.h:2090
@ kFlutterNativeThreadTypeRender
Definition: embedder.h:2094
TaskRunners task_runners_
ThreadHost thread_host_
FlutterSemanticsFlag flags
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
#define FML_DLOG(severity)
Definition: logging.h:102
#define FML_DCHECK(condition)
Definition: logging.h:103
Dart_NativeFunction function
Definition: fuchsia.cc:51
Win32Message message
FlTexture * texture
std::function< void()> closure
Definition: closure.h:14
Definition: ref_ptr.h:256
FlutterTaskRunner runner
Definition: embedder.h:1551
uint64_t task
Definition: embedder.h:1552
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
#define ERROR(message)
Definition: elf_loader.cc:260