Flutter Engine
 
Loading...
Searching...
No Matches
embedder_config_builder.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
11#include "third_party/skia/include/core/SkImage.h"
12
13namespace flutter::testing {
14
16 EmbedderTestContext& context,
17 InitializationPreference preference)
18 : context_(context) {
19 project_args_.struct_size = sizeof(project_args_);
20 project_args_.shutdown_dart_vm_when_done = true;
21 project_args_.platform_message_callback =
22 [](const FlutterPlatformMessage* message, void* context) {
23 reinterpret_cast<EmbedderTestContext*>(context)
24 ->PlatformMessageCallback(message);
25 };
26
27 custom_task_runners_.struct_size = sizeof(FlutterCustomTaskRunners);
28
29 // The first argument is always the executable name. Don't make tests have to
30 // do this manually.
31 AddCommandLineArgument("embedder_unittest");
32
41 AddCommandLineArgument("--disable-vm-service");
42
46 }
50 }
51 }
52}
53
55
59
61 project_args_.assets_path = context_.GetAssetsPath().c_str();
62}
63
65 if (auto mapping = context_.GetVMSnapshotData()) {
66 project_args_.vm_snapshot_data = mapping->GetMapping();
67 project_args_.vm_snapshot_data_size = mapping->GetSize();
68 }
69
70 if (auto mapping = context_.GetVMSnapshotInstructions()) {
71 project_args_.vm_snapshot_instructions = mapping->GetMapping();
72 project_args_.vm_snapshot_instructions_size = mapping->GetSize();
73 }
74
75 if (auto mapping = context_.GetIsolateSnapshotData()) {
76 project_args_.isolate_snapshot_data = mapping->GetMapping();
77 project_args_.isolate_snapshot_data_size = mapping->GetSize();
78 }
79
80 if (auto mapping = context_.GetIsolateSnapshotInstructions()) {
81 project_args_.isolate_snapshot_instructions = mapping->GetMapping();
82 project_args_.isolate_snapshot_instructions_size = mapping->GetSize();
83 }
84}
85
87 project_args_.aot_data = context_.GetAOTData();
88}
89
94
105
110
115
120
121void EmbedderConfigBuilder::SetLogTag(std::string tag) {
122 log_tag_ = std::move(tag);
123 project_args_.log_tag = log_tag_.c_str();
124}
125
130
131void EmbedderConfigBuilder::SetExecutableName(std::string executable_name) {
132 if (executable_name.empty()) {
133 return;
134 }
135 command_line_arguments_[0] = std::move(executable_name);
136}
137
138void EmbedderConfigBuilder::SetDartEntrypoint(std::string entrypoint) {
139 if (entrypoint.empty()) {
140 return;
141 }
142
143 dart_entrypoint_ = std::move(entrypoint);
144 project_args_.custom_dart_entrypoint = dart_entrypoint_.c_str();
145}
146
148 if (arg.empty()) {
149 return;
150 }
151
152 command_line_arguments_.emplace_back(std::move(arg));
153}
154
156 if (arg.empty()) {
157 return;
158 }
159
160 dart_entrypoint_arguments_.emplace_back(std::move(arg));
161}
162
164 const FlutterTaskRunnerDescription* runner) {
165 if (runner == nullptr) {
166 return;
167 }
168 custom_task_runners_.platform_task_runner = runner;
169 project_args_.custom_task_runners = &custom_task_runners_;
170}
171
173 const FlutterTaskRunnerDescription* runner) {
174 if (runner == nullptr) {
175 return;
176 }
177 custom_task_runners_.ui_task_runner = runner;
178 project_args_.custom_task_runners = &custom_task_runners_;
179}
180
182 project_args_.vsync_callback = [](void* user_data, intptr_t baton) {
183 auto context = reinterpret_cast<EmbedderTestContext*>(user_data);
184 context->RunVsyncCallback(baton);
185 };
186}
187
189 const FlutterTaskRunnerDescription* runner) {
190 if (runner == nullptr) {
191 return;
192 }
193
194 custom_task_runners_.render_task_runner = runner;
195 project_args_.custom_task_runners = &custom_task_runners_;
196}
197
202
207
208void EmbedderConfigBuilder::SetCompositor(bool avoid_backing_store_cache,
209 bool use_present_layers_callback) {
210 context_.SetupCompositor();
211 auto& compositor = context_.GetCompositor();
212 compositor_.struct_size = sizeof(compositor_);
213 compositor_.user_data = &compositor;
214 compositor_.create_backing_store_callback =
215 [](const FlutterBackingStoreConfig* config, //
216 FlutterBackingStore* backing_store_out, //
217 void* user_data //
218 ) {
219 return reinterpret_cast<EmbedderTestCompositor*>(user_data)
220 ->CreateBackingStore(config, backing_store_out);
221 };
222 compositor_.collect_backing_store_callback =
223 [](const FlutterBackingStore* backing_store, //
224 void* user_data //
225 ) {
226 return reinterpret_cast<EmbedderTestCompositor*>(user_data)
227 ->CollectBackingStore(backing_store);
228 };
229 if (use_present_layers_callback) {
230 compositor_.present_layers_callback = [](const FlutterLayer** layers,
231 size_t layers_count,
232 void* user_data) {
233 auto compositor = reinterpret_cast<EmbedderTestCompositor*>(user_data);
234
235 // The present layers callback is incompatible with multiple views;
236 // it can only be used to render the implicit view.
238 };
239 } else {
240 compositor_.present_view_callback = [](const FlutterPresentViewInfo* info) {
241 auto compositor =
242 reinterpret_cast<EmbedderTestCompositor*>(info->user_data);
243
244 return compositor->Present(info->view_id, info->layers,
245 info->layers_count);
246 };
247 }
248 compositor_.avoid_backing_store_cache = avoid_backing_store_cache;
249 project_args_.compositor = &compositor_;
250}
251
255
261
263 return SetupEngine(true);
264}
265
267 return SetupEngine(false);
268}
269
270UniqueEngine EmbedderConfigBuilder::SetupEngine(bool run) const {
271 FlutterEngine engine = nullptr;
272 FlutterProjectArgs project_args = project_args_;
273
274 std::vector<const char*> args;
275 args.reserve(command_line_arguments_.size());
276
277 for (const auto& arg : command_line_arguments_) {
278 args.push_back(arg.c_str());
279 }
280
281 if (!args.empty()) {
282 project_args.command_line_argv = args.data();
283 project_args.command_line_argc = args.size();
284 } else {
285 // Clear it out in case this is not the first engine launch from the
286 // embedder config builder.
287 project_args.command_line_argv = nullptr;
288 project_args.command_line_argc = 0;
289 }
290
291 std::vector<const char*> dart_args;
292 dart_args.reserve(dart_entrypoint_arguments_.size());
293
294 for (const auto& arg : dart_entrypoint_arguments_) {
295 dart_args.push_back(arg.c_str());
296 }
297
298 if (!dart_args.empty()) {
299 project_args.dart_entrypoint_argv = dart_args.data();
300 project_args.dart_entrypoint_argc = dart_args.size();
301 } else {
302 // Clear it out in case this is not the first engine launch from the
303 // embedder config builder.
304 project_args.dart_entrypoint_argv = nullptr;
305 project_args.dart_entrypoint_argc = 0;
306 }
307
308 auto result = run ? FlutterEngineRun(FLUTTER_ENGINE_VERSION,
309 &context_.GetRendererConfig(),
310 &project_args, &context_, &engine)
312 FLUTTER_ENGINE_VERSION, &context_.GetRendererConfig(),
313 &project_args, &context_, &engine);
314
315 if (result != kSuccess) {
316 return {};
317 }
318
319 return UniqueEngine{engine};
320}
321
322} // namespace flutter::testing
GLenum type
void SetPlatformTaskRunner(const FlutterTaskRunnerDescription *runner)
void SetExecutableName(std::string executable_name)
void SetRenderTargetType(EmbedderTestBackingStoreProducer::RenderTargetType type, FlutterSoftwarePixelFormat software_pixfmt=kFlutterSoftwarePixelFormatNative32)
void SetViewFocusChangeRequestCallback(const std::function< void(const FlutterViewFocusChangeRequest *)> &callback)
void SetRenderTaskRunner(const FlutterTaskRunnerDescription *runner)
EmbedderConfigBuilder(EmbedderTestContext &context, InitializationPreference preference=InitializationPreference::kSnapshotsInitialize)
void SetUITaskRunner(const FlutterTaskRunnerDescription *runner)
void SetCompositor(bool avoid_backing_store_cache=false, bool use_present_layers_callback=false)
void SetPlatformMessageCallback(const std::function< void(const FlutterPlatformMessage *)> &callback)
virtual void SetRenderTargetType(EmbedderTestBackingStoreProducer::RenderTargetType type, FlutterSoftwarePixelFormat software_pixfmt)=0
bool Present(FlutterViewId view_id, const FlutterLayer **layers, size_t layers_count)
const fml::Mapping * GetIsolateSnapshotData() const
const fml::Mapping * GetVMSnapshotInstructions() const
FlutterUpdateSemanticsNodeCallback GetUpdateSemanticsNodeCallbackHook()
void SetViewFocusChangeRequestCallback(const ViewFocusChangeRequestCallback &callback)
const fml::Mapping * GetIsolateSnapshotInstructions() const
static FlutterLogMessageCallback GetLogMessageCallbackHook()
void SetPlatformMessageCallback(const std::function< void(const FlutterPlatformMessage *)> &callback)
static FlutterComputePlatformResolvedLocaleCallback GetComputePlatformResolvedLocaleCallbackHook()
FlutterViewFocusChangeRequestCallback GetViewFocusChangeRequestCallbackHook()
FlutterUpdateSemanticsCustomActionCallback GetUpdateSemanticsCustomActionCallbackHook()
FlutterUpdateSemanticsCallback2 GetUpdateSemanticsCallback2Hook()
FlutterUpdateSemanticsCallback GetUpdateSemanticsCallbackHook()
FlutterChannelUpdateCallback GetChannelUpdateCallbackHook()
const fml::Mapping * GetVMSnapshotData() const
FlutterEngineResult FlutterEngineRun(size_t version, const FlutterRendererConfig *config, const FlutterProjectArgs *args, void *user_data, FLUTTER_API_SYMBOL(FlutterEngine) *engine_out)
Initialize and run a Flutter engine instance and return a handle to it. This is a convenience method ...
Definition embedder.cc:1971
FlutterEngineResult FlutterEngineInitialize(size_t version, const FlutterRendererConfig *config, const FlutterProjectArgs *args, void *user_data, FLUTTER_API_SYMBOL(FlutterEngine) *engine_out)
Initialize a Flutter engine instance. This does not run the Flutter application code till the Flutter...
Definition embedder.cc:1987
FlutterSoftwarePixelFormat
Definition embedder.h:450
#define FLUTTER_ENGINE_VERSION
Definition embedder.h:70
FlutterEngine engine
Definition main.cc:84
const FlutterLayer size_t layers_count
const FlutterLayer ** layers
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
G_BEGIN_DECLS GBytes * message
FlutterDesktopBinaryReply callback
fml::UniqueObject< FlutterEngine, UniqueEngineTraits > UniqueEngine
constexpr int64_t kFlutterImplicitViewId
Definition constants.h:35
const FlutterTaskRunnerDescription * ui_task_runner
Definition embedder.h:1908
const FlutterTaskRunnerDescription * render_task_runner
Definition embedder.h:1901
const FlutterTaskRunnerDescription * platform_task_runner
Definition embedder.h:1896
size_t struct_size
The size of this struct. Must be sizeof(FlutterCustomTaskRunners).
Definition embedder.h:1891
FlutterPlatformMessageCallback platform_message_callback
Definition embedder.h:2510
FlutterComputePlatformResolvedLocaleCallback compute_platform_resolved_locale_callback
Definition embedder.h:2682
FlutterLogMessageCallback log_message_callback
Definition embedder.h:2702
FlutterViewFocusChangeRequestCallback view_focus_change_request_callback
Definition embedder.h:2760
VsyncCallback vsync_callback
Definition embedder.h:2607
const char * assets_path
Definition embedder.h:2462
const uint8_t * isolate_snapshot_data
Definition embedder.h:2531
FlutterEngineAOTData aot_data
Definition embedder.h:2671
FlutterUpdateSemanticsCallback update_semantics_callback
Definition embedder.h:2737
const uint8_t * vm_snapshot_data
Definition embedder.h:2515
size_t isolate_snapshot_instructions_size
Definition embedder.h:2542
const char *const * dart_entrypoint_argv
Definition embedder.h:2694
size_t struct_size
The size of this struct. Must be sizeof(FlutterProjectArgs).
Definition embedder.h:2458
FlutterUpdateSemanticsCallback2 update_semantics_callback2
Definition embedder.h:2749
const uint8_t * vm_snapshot_instructions
Definition embedder.h:2523
size_t isolate_snapshot_data_size
Definition embedder.h:2534
const char *const * command_line_argv
Definition embedder.h:2504
FlutterChannelUpdateCallback channel_update_callback
Definition embedder.h:2754
size_t vm_snapshot_instructions_size
Definition embedder.h:2526
bool shutdown_dart_vm_when_done
Definition embedder.h:2640
const char * custom_dart_entrypoint
Definition embedder.h:2616
FlutterUpdateSemanticsCustomActionCallback update_semantics_custom_action_callback
Definition embedder.h:2585
FlutterUpdateSemanticsNodeCallback update_semantics_node_callback
Definition embedder.h:2564
const FlutterCustomTaskRunners * custom_task_runners
Definition embedder.h:2621
size_t vm_snapshot_data_size
Definition embedder.h:2518
const char * log_tag
Definition embedder.h:2709
int command_line_argc
The command line argument count used to initialize the project.
Definition embedder.h:2488
VoidCallback root_isolate_create_callback
Definition embedder.h:2545
const uint8_t * isolate_snapshot_instructions
Definition embedder.h:2539
const FlutterCompositor * compositor
Definition embedder.h:2656