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 
10 namespace flutter {
11 
12 struct ShellArgs {
16  ShellArgs(Settings p_settings,
17  Shell::CreateCallback<PlatformView> p_on_create_platform_view,
18  Shell::CreateCallback<Rasterizer> p_on_create_rasterizer)
19  : settings(std::move(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  flutter::TaskRunners task_runners,
28  RunConfiguration run_configuration,
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>(std::move(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  if (!IsValid()) {
104  return false;
105  }
106 
107  auto platform_view = shell_->GetPlatformView();
108  if (!platform_view) {
109  return false;
110  }
111  platform_view->SetViewportMetrics(std::move(metrics));
112  return true;
113 }
114 
116  std::unique_ptr<flutter::PointerDataPacket> packet) {
117  if (!IsValid() || !packet) {
118  return false;
119  }
120 
121  auto platform_view = shell_->GetPlatformView();
122  if (!platform_view) {
123  return false;
124  }
125 
126  platform_view->DispatchPointerDataPacket(std::move(packet));
127  return true;
128 }
129 
131  std::unique_ptr<flutter::KeyDataPacket> packet,
133  if (!IsValid() || !packet) {
134  return false;
135  }
136 
137  auto platform_view = shell_->GetPlatformView();
138  if (!platform_view) {
139  return false;
140  }
141 
142  platform_view->DispatchKeyDataPacket(std::move(packet), std::move(callback));
143  return true;
144 }
145 
147  std::unique_ptr<PlatformMessage> message) {
148  if (!IsValid() || !message) {
149  return false;
150  }
151 
152  auto platform_view = shell_->GetPlatformView();
153  if (!platform_view) {
154  return false;
155  }
156 
157  platform_view->DispatchPlatformMessage(std::move(message));
158  return true;
159 }
160 
162  if (!IsValid()) {
163  return false;
164  }
165  shell_->GetPlatformView()->RegisterTexture(
166  external_texture_resolver_->ResolveExternalTexture(texture));
167  return true;
168 }
169 
171  if (!IsValid()) {
172  return false;
173  }
174  shell_->GetPlatformView()->UnregisterTexture(texture);
175  return true;
176 }
177 
179  if (!IsValid()) {
180  return false;
181  }
182  shell_->GetPlatformView()->MarkTextureFrameAvailable(texture);
183  return true;
184 }
185 
187  if (!IsValid()) {
188  return false;
189  }
190 
191  auto platform_view = shell_->GetPlatformView();
192  if (!platform_view) {
193  return false;
194  }
195  platform_view->SetSemanticsEnabled(enabled);
196  return true;
197 }
198 
200  if (!IsValid()) {
201  return false;
202  }
203  auto platform_view = shell_->GetPlatformView();
204  if (!platform_view) {
205  return false;
206  }
207  platform_view->SetAccessibilityFeatures(flags);
208  return true;
209 }
210 
214  if (!IsValid()) {
215  return false;
216  }
217  auto platform_view = shell_->GetPlatformView();
218  if (!platform_view) {
219  return false;
220  }
221  platform_view->DispatchSemanticsAction(id, action, std::move(args));
222  return true;
223 }
224 
225 bool EmbedderEngine::OnVsyncEvent(intptr_t baton,
226  fml::TimePoint frame_start_time,
227  fml::TimePoint frame_target_time) {
228  if (!IsValid()) {
229  return false;
230  }
231 
232  return VsyncWaiterEmbedder::OnEmbedderVsync(baton, frame_start_time,
233  frame_target_time);
234 }
235 
237  if (!IsValid()) {
238  return false;
239  }
240 
241  return shell_->ReloadSystemFonts();
242 }
243 
245  if (!IsValid()) {
246  return false;
247  }
248 
249  shell_->GetTaskRunners().GetRasterTaskRunner()->PostTask(task);
250  return true;
251 }
252 
254  // The shell doesn't need to be running or valid for access to the thread
255  // host. This is why there is no `IsValid` check here. This allows embedders
256  // to perform custom task runner interop before the shell is running.
257  if (task == nullptr) {
258  return false;
259  }
260  return thread_host_->PostTask(reinterpret_cast<int64_t>(task->runner),
261  task->task);
262 }
263 
266  if (!IsValid() || closure == nullptr) {
267  return false;
268  }
269 
270  const auto trampoline = [closure](FlutterNativeThreadType type,
272  runner->PostTask([closure, type] { closure(type); });
273  };
274 
275  // Post the task to all thread host threads.
276  const auto& task_runners = shell_->GetTaskRunners();
278  task_runners.GetRasterTaskRunner());
279  trampoline(kFlutterNativeThreadTypeWorker, task_runners.GetIOTaskRunner());
280  trampoline(kFlutterNativeThreadTypeUI, task_runners.GetUITaskRunner());
282  task_runners.GetPlatformTaskRunner());
283 
284  // Post the task to all worker threads.
285  auto vm = shell_->GetDartVM();
286  vm->GetConcurrentMessageLoop()->PostTaskToAllWorkers(
288 
289  return true;
290 }
291 
293  FML_DCHECK(shell_);
294  return *shell_.get();
295 }
296 
297 } // namespace flutter
G_BEGIN_DECLS FlValue * args
bool IsValid() const
A valid run configuration only guarantees that the engine should be able to find the assets and the i...
KeyCallType type
bool DispatchPointerDataPacket(std::unique_ptr< flutter::PointerDataPacket > packet)
G_BEGIN_DECLS FlTexture * texture
std::function< std::unique_ptr< T >(Shell &)> CreateCallback
Definition: shell.h:114
#define FML_DCHECK(condition)
Definition: logging.h:86
std::unique_ptr< flutter::PlatformViewIOS > platform_view
bool RunTask(const FlutterTask *task)
FlutterTaskRunner runner
Definition: embedder.h:898
Definition: ref_ptr.h:252
Dart_NativeFunction function
Definition: fuchsia.cc:51
ThreadHost thread_host_
uint64_t task
Definition: embedder.h:899
bool SetViewportMetrics(flutter::ViewportMetrics metrics)
FlKeyEvent FlKeyResponderAsyncCallback callback
bool OnVsyncEvent(intptr_t baton, fml::TimePoint frame_start_time, fml::TimePoint frame_target_time)
bool SetAccessibilityFeatures(int32_t flags)
bool PostTaskOnEngineManagedNativeThreads(std::function< void(FlutterNativeThreadType)> closure) const
std::function< void()> closure
Definition: closure.h:14
static bool OnEmbedderVsync(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...
SemanticsAction action
EmbedderEngine(std::unique_ptr< EmbedderThreadHost > thread_host, TaskRunners task_runners, Settings settings, RunConfiguration run_configuration, Shell::CreateCallback< PlatformView > on_create_platform_view, Shell::CreateCallback< Rasterizer > on_create_rasterizer, std::unique_ptr< EmbedderExternalTextureResolver > external_texture_resolver)
bool SetSemanticsEnabled(bool enabled)
static std::unique_ptr< Shell > Create(const PlatformData &platform_data, 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:126
Shell::CreateCallback< PlatformView > on_create_platform_view
bool SendPlatformMessage(std::unique_ptr< PlatformMessage > message)
bool RegisterTexture(int64_t texture)
Shell::CreateCallback< Rasterizer > on_create_rasterizer
A Mapping like NonOwnedMapping, but uses Free as its release proc.
Definition: mapping.h:129
ShellArgs(Settings p_settings, Shell::CreateCallback< PlatformView > p_on_create_platform_view, Shell::CreateCallback< Rasterizer > p_on_create_rasterizer)
bool UnregisterTexture(int64_t texture)
TaskRunners task_runners_
std::function< void(bool)> KeyDataResponse
FlutterNativeThreadType
Definition: embedder.h:1296
bool PostRenderThreadTask(const fml::closure &task)
#define FML_DLOG(severity)
Definition: logging.h:85
bool DispatchSemanticsAction(int id, flutter::SemanticsAction action, fml::MallocMapping args)
const TaskRunners & GetTaskRunners() const
bool DispatchKeyDataPacket(std::unique_ptr< flutter::KeyDataPacket > packet, KeyDataResponse callback)
Notifies the platform view that the embedder has sent it a key data packet. A key data packet contain...
DEF_SWITCHES_START snapshot asset Path to the directory containing the four files specified by VmSnapshotInstructions and IsolateSnapshotInstructions vm snapshot The VM instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present isolate snapshot The isolate instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present icu symbol Prefix for the symbols representing ICU data linked into the Flutter library dart flags
Definition: switches.h:66
bool MarkTextureFrameAvailable(int64_t texture)