Flutter Engine
The Flutter Engine
flutter_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 "include/flutter/flutter_engine.h"
6
7#include <algorithm>
8#include <iostream>
9
11
12namespace flutter {
13
15 FlutterDesktopEngineProperties c_engine_properties = {};
16 c_engine_properties.assets_path = project.assets_path().c_str();
17 c_engine_properties.icu_data_path = project.icu_data_path().c_str();
18 c_engine_properties.aot_library_path = project.aot_library_path().c_str();
19 c_engine_properties.dart_entrypoint = project.dart_entrypoint().c_str();
20
21 const std::vector<std::string>& entrypoint_args =
23 std::vector<const char*> entrypoint_argv;
25 entrypoint_args.begin(), entrypoint_args.end(),
26 std::back_inserter(entrypoint_argv),
27 [](const std::string& arg) -> const char* { return arg.c_str(); });
28
29 c_engine_properties.dart_entrypoint_argc =
30 static_cast<int>(entrypoint_argv.size());
31 c_engine_properties.dart_entrypoint_argv =
32 entrypoint_argv.empty() ? nullptr : entrypoint_argv.data();
33
34 engine_ = FlutterDesktopEngineCreate(&c_engine_properties);
35
36 auto core_messenger = FlutterDesktopEngineGetMessenger(engine_);
37 messenger_ = std::make_unique<BinaryMessengerImpl>(core_messenger);
38}
39
41 ShutDown();
42}
43
45 return Run(nullptr);
46}
47
48bool FlutterEngine::Run(const char* entry_point) {
49 if (!engine_) {
50 std::cerr << "Cannot run an engine that failed creation." << std::endl;
51 return false;
52 }
53 if (run_succeeded_) {
54 std::cerr << "Cannot run an engine more than once." << std::endl;
55 return false;
56 }
57 bool run_succeeded = FlutterDesktopEngineRun(engine_, entry_point);
58 if (!run_succeeded) {
59 std::cerr << "Failed to start engine." << std::endl;
60 }
61 run_succeeded_ = true;
62 return run_succeeded;
63}
64
66 if (engine_ && owns_engine_) {
68 }
69 engine_ = nullptr;
70}
71
72std::chrono::nanoseconds FlutterEngine::ProcessMessages() {
73 return std::chrono::nanoseconds(FlutterDesktopEngineProcessMessages(engine_));
74}
75
78}
79
81 const std::string& plugin_name) {
82 if (!engine_) {
83 std::cerr << "Cannot get plugin registrar on an engine that isn't running; "
84 "call Run first."
85 << std::endl;
86 return nullptr;
87 }
88 return FlutterDesktopEngineGetPluginRegistrar(engine_, plugin_name.c_str());
89}
90
92 next_frame_callback_ = std::move(callback);
94 engine_,
95 [](void* user_data) {
96 FlutterEngine* self = static_cast<FlutterEngine*>(user_data);
97 self->next_frame_callback_();
98 self->next_frame_callback_ = nullptr;
99 },
100 this);
101}
102
104 HWND hwnd,
106 WPARAM wparam,
107 LPARAM lparam) {
110 engine_, hwnd, message, wparam, lparam, &result)) {
111 return result;
112 }
113 return std::nullopt;
114}
115
116FlutterDesktopEngineRef FlutterEngine::RelinquishEngine() {
117 owns_engine_ = false;
118 return engine_;
119}
120
121} // namespace flutter
const std::vector< std::string > & dart_entrypoint_arguments() const
Definition: dart_project.h:70
const std::string & dart_entrypoint() const
Definition: dart_project.h:60
std::chrono::nanoseconds ProcessMessages()
std::optional< LRESULT > ProcessExternalWindowMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
void SetNextFrameCallback(std::function< void()> callback)
FlutterDesktopPluginRegistrarRef GetRegistrarForPlugin(const std::string &plugin_name) override
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
GAsyncResult * result
Dart_NativeFunction function
Definition: fuchsia.cc:51
Win32Message message
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition: p3.cpp:47
FlutterDesktopEngineRef FlutterDesktopEngineCreate(const FlutterDesktopEngineProperties *engine_properties)
void FlutterDesktopEngineReloadSystemFonts(FlutterDesktopEngineRef engine)
bool FlutterDesktopEngineProcessExternalWindowMessage(FlutterDesktopEngineRef engine, HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam, LRESULT *result)
FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar(FlutterDesktopEngineRef engine, const char *plugin_name)
bool FlutterDesktopEngineDestroy(FlutterDesktopEngineRef engine_ref)
uint64_t FlutterDesktopEngineProcessMessages(FlutterDesktopEngineRef engine)
FlutterDesktopMessengerRef FlutterDesktopEngineGetMessenger(FlutterDesktopEngineRef engine)
void FlutterDesktopEngineSetNextFrameCallback(FlutterDesktopEngineRef engine, VoidCallback callback, void *user_data)
bool FlutterDesktopEngineRun(FlutterDesktopEngineRef engine, const char *entry_point)
void * user_data
LONG_PTR LRESULT
Definition: windows_types.h:61
unsigned int UINT
Definition: windows_types.h:32
LONG_PTR LPARAM
Definition: windows_types.h:60
UINT_PTR WPARAM
Definition: windows_types.h:59