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 #ifdef SHELL_ENABLE_GL
32  ,
34 #endif
35  )
36  : thread_host_(std::move(thread_host)),
37  task_runners_(task_runners),
38  run_configuration_(std::move(run_configuration)),
39  shell_args_(std::make_unique<ShellArgs>(std::move(settings),
40  on_create_platform_view,
42 #ifdef SHELL_ENABLE_GL
43  ,
44  external_texture_callback_(external_texture_callback)
45 #endif
46 {
47 }
48 
50 
52  if (!shell_args_) {
53  FML_DLOG(ERROR) << "Invalid shell arguments.";
54  return false;
55  }
56 
57  if (shell_) {
58  FML_DLOG(ERROR) << "Shell already initialized";
59  }
60 
61  shell_ = Shell::Create(task_runners_, shell_args_->settings,
62  shell_args_->on_create_platform_view,
63  shell_args_->on_create_rasterizer);
64 
65  // Reset the args no matter what. They will never be used to initialize a
66  // shell again.
67  shell_args_.reset();
68 
69  return IsValid();
70 }
71 
73  shell_.reset();
74  return IsValid();
75 }
76 
78  if (!IsValid() || !run_configuration_.IsValid()) {
79  return false;
80  }
81  shell_->RunEngine(std::move(run_configuration_));
82  return true;
83 }
84 
86  return static_cast<bool>(shell_);
87 }
88 
90  return task_runners_;
91 }
92 
94  if (!IsValid()) {
95  return false;
96  }
97 
98  shell_->GetPlatformView()->NotifyCreated();
99  return true;
100 }
101 
103  if (!IsValid()) {
104  return false;
105  }
106 
107  shell_->GetPlatformView()->NotifyDestroyed();
108  return true;
109 }
110 
112  if (!IsValid()) {
113  return false;
114  }
115 
116  auto platform_view = shell_->GetPlatformView();
117  if (!platform_view) {
118  return false;
119  }
120  platform_view->SetViewportMetrics(std::move(metrics));
121  return true;
122 }
123 
125  std::unique_ptr<flutter::PointerDataPacket> packet) {
126  if (!IsValid() || !packet) {
127  return false;
128  }
129 
130  auto platform_view = shell_->GetPlatformView();
131  if (!platform_view) {
132  return false;
133  }
134 
135  platform_view->DispatchPointerDataPacket(std::move(packet));
136  return true;
137 }
138 
141  if (!IsValid() || !message) {
142  return false;
143  }
144 
145  auto platform_view = shell_->GetPlatformView();
146  if (!platform_view) {
147  return false;
148  }
149 
150  platform_view->DispatchPlatformMessage(message);
151  return true;
152 }
153 
154 bool EmbedderEngine::RegisterTexture(int64_t texture) {
155 #ifdef SHELL_ENABLE_GL
156  if (!IsValid() || !external_texture_callback_) {
157  return false;
158  }
159  shell_->GetPlatformView()->RegisterTexture(
160  std::make_unique<EmbedderExternalTextureGL>(texture,
161  external_texture_callback_));
162 #endif
163 
164  return true;
165 }
166 
167 bool EmbedderEngine::UnregisterTexture(int64_t texture) {
168 #ifdef SHELL_ENABLE_GL
169  if (!IsValid() || !external_texture_callback_) {
170  return false;
171  }
172  shell_->GetPlatformView()->UnregisterTexture(texture);
173 #endif
174 
175  return true;
176 }
177 
179 #ifdef SHELL_ENABLE_GL
180  if (!IsValid() || !external_texture_callback_) {
181  return false;
182  }
183  shell_->GetPlatformView()->MarkTextureFrameAvailable(texture);
184 #endif
185 
186  return true;
187 }
188 
190  if (!IsValid()) {
191  return false;
192  }
193 
194  auto platform_view = shell_->GetPlatformView();
195  if (!platform_view) {
196  return false;
197  }
198  platform_view->SetSemanticsEnabled(enabled);
199  return true;
200 }
201 
203  if (!IsValid()) {
204  return false;
205  }
206  auto platform_view = shell_->GetPlatformView();
207  if (!platform_view) {
208  return false;
209  }
210  platform_view->SetAccessibilityFeatures(flags);
211  return true;
212 }
213 
216  std::vector<uint8_t> args) {
217  if (!IsValid()) {
218  return false;
219  }
220  auto platform_view = shell_->GetPlatformView();
221  if (!platform_view) {
222  return false;
223  }
224  platform_view->DispatchSemanticsAction(id, action, std::move(args));
225  return true;
226 }
227 
228 bool EmbedderEngine::OnVsyncEvent(intptr_t baton,
229  fml::TimePoint frame_start_time,
230  fml::TimePoint frame_target_time) {
231  if (!IsValid()) {
232  return false;
233  }
234 
235  return VsyncWaiterEmbedder::OnEmbedderVsync(baton, frame_start_time,
236  frame_target_time);
237 }
238 
240  if (!IsValid()) {
241  return false;
242  }
243 
244  return shell_->ReloadSystemFonts();
245 }
246 
248  if (!IsValid()) {
249  return false;
250  }
251 
252  shell_->GetTaskRunners().GetRasterTaskRunner()->PostTask(task);
253  return true;
254 }
255 
257  // The shell doesn't need to be running or valid for access to the thread
258  // host. This is why there is no `IsValid` check here. This allows embedders
259  // to perform custom task runner interop before the shell is running.
260  if (task == nullptr) {
261  return false;
262  }
263  return thread_host_->PostTask(reinterpret_cast<int64_t>(task->runner),
264  task->task);
265 }
266 
269  if (!IsValid() || closure == nullptr) {
270  return false;
271  }
272 
273  const auto trampoline = [closure](FlutterNativeThreadType type,
275  runner->PostTask([closure, type] { closure(type); });
276  };
277 
278  // Post the task to all thread host threads.
279  const auto& task_runners = shell_->GetTaskRunners();
281  task_runners.GetRasterTaskRunner());
282  trampoline(kFlutterNativeThreadTypeWorker, task_runners.GetIOTaskRunner());
283  trampoline(kFlutterNativeThreadTypeUI, task_runners.GetUITaskRunner());
285  task_runners.GetPlatformTaskRunner());
286 
287  // Post the task to all worker threads.
288  auto vm = shell_->GetDartVM();
289  vm->GetConcurrentMessageLoop()->PostTaskToAllWorkers(
291 
292  return true;
293 }
294 
296  FML_DCHECK(shell_);
297  return *shell_.get();
298 }
299 
300 } // 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...
bool DispatchPointerDataPacket(std::unique_ptr< flutter::PointerDataPacket > packet)
std::function< std::unique_ptr< T >(Shell &)> CreateCallback
Definition: shell.h:99
bool SendPlatformMessage(fml::RefPtr< flutter::PlatformMessage > message)
#define FML_DCHECK(condition)
Definition: logging.h:86
bool RunTask(const FlutterTask *task)
ThreadHost thread_host_
FlutterTaskRunner runner
Definition: embedder.h:707
bool DispatchSemanticsAction(int id, flutter::SemanticsAction action, std::vector< uint8_t > args)
Definition: ref_ptr.h:252
Dart_NativeFunction function
Definition: fuchsia.cc:51
uint64_t task
Definition: embedder.h:708
bool SetViewportMetrics(flutter::ViewportMetrics metrics)
bool OnVsyncEvent(intptr_t baton, fml::TimePoint frame_start_time, fml::TimePoint frame_target_time)
TaskRunners task_runners_
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
bool SetSemanticsEnabled(bool enabled)
Shell::CreateCallback< PlatformView > on_create_platform_view
bool RegisterTexture(int64_t texture)
Shell::CreateCallback< Rasterizer > on_create_rasterizer
ShellArgs(Settings p_settings, Shell::CreateCallback< PlatformView > p_on_create_platform_view, Shell::CreateCallback< Rasterizer > p_on_create_rasterizer)
bool UnregisterTexture(int64_t texture)
FlutterNativeThreadType
Definition: embedder.h:1096
bool PostRenderThreadTask(const fml::closure &task)
#define FML_DLOG(severity)
Definition: logging.h:85
const TaskRunners & GetTaskRunners() const
std::function< sk_sp< SkImage >(int64_t texture_identifier, GrDirectContext *, const SkISize &)> ExternalTextureCallback
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)
static std::unique_ptr< Shell > Create(TaskRunners task_runners, Settings settings, const CreateCallback< PlatformView > &on_create_platform_view, const CreateCallback< Rasterizer > &on_create_rasterizer)
Creates a shell instance using the provided settings. The callbacks to create the various shell subco...
Definition: shell.cc:239
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)