| Flutter Windows Embedder |

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
10 #include "binary_messenger_impl.h"
11 #include "flutter_windows.h"
12
13 namespace flutter {
14
15 FlutterEngine::FlutterEngine(const DartProject& project) {
16 FlutterDesktopEngineProperties c_engine_properties = {};
17 c_engine_properties.assets_path = project.assets_path().c_str();
18 c_engine_properties.icu_data_path = project.icu_data_path().c_str();
19 c_engine_properties.aot_library_path = project.aot_library_path().c_str();
20 c_engine_properties.dart_entrypoint = project.dart_entrypoint().c_str();
21 c_engine_properties.gpu_preference =
22 static_cast<FlutterDesktopGpuPreference>(project.gpu_preference());
23 c_engine_properties.ui_thread_policy =
24 static_cast<FlutterDesktopUIThreadPolicy>(project.ui_thread_policy());
25 c_engine_properties.accessibility_mode =
26 static_cast<FlutterDesktopAccessibilityMode>(
27 project.accessibility_mode());
28
29 const std::vector<std::string>& entrypoint_args =
30 project.dart_entrypoint_arguments();
31 std::vector<const char*> entrypoint_argv;
32 std::transform(
33 entrypoint_args.begin(), entrypoint_args.end(),
34 std::back_inserter(entrypoint_argv),
35 [](const std::string& arg) -> const char* { return arg.c_str(); });
36
37 c_engine_properties.dart_entrypoint_argc =
38 static_cast<int>(entrypoint_argv.size());
39 c_engine_properties.dart_entrypoint_argv =
40 entrypoint_argv.empty() ? nullptr : entrypoint_argv.data();
41
42 engine_ = FlutterDesktopEngineCreate(&c_engine_properties);
43
44 auto core_messenger = FlutterDesktopEngineGetMessenger(engine_);
45 messenger_ = std::make_unique<BinaryMessengerImpl>(core_messenger);
46 }
47
48 FlutterEngine::~FlutterEngine() {
49 ShutDown();
50 }
51
52 bool FlutterEngine::Run() {
53 return Run(nullptr);
54 }
55
56 bool FlutterEngine::Run(const char* entry_point) {
57 if (!engine_) {
58 std::cerr << "Cannot run an engine that failed creation." << std::endl;
59 return false;
60 }
61 if (run_succeeded_) {
62 std::cerr << "Cannot run an engine more than once." << std::endl;
63 return false;
64 }
65 bool run_succeeded = FlutterDesktopEngineRun(engine_, entry_point);
66 if (!run_succeeded) {
67 std::cerr << "Failed to start engine." << std::endl;
68 }
69 run_succeeded_ = true;
70 return run_succeeded;
71 }
72
73 void FlutterEngine::ShutDown() {
74 if (engine_ && owns_engine_) {
75 FlutterDesktopEngineDestroy(engine_);
76 }
77 engine_ = nullptr;
78 }
79
80 std::chrono::nanoseconds FlutterEngine::ProcessMessages() {
81 return std::chrono::nanoseconds(FlutterDesktopEngineProcessMessages(engine_));
82 }
83
84 void FlutterEngine::ReloadSystemFonts() {
85 FlutterDesktopEngineReloadSystemFonts(engine_);
86 }
87
88 FlutterDesktopPluginRegistrarRef FlutterEngine::GetRegistrarForPlugin(
89 const std::string& plugin_name) {
90 if (!engine_) {
91 std::cerr << "Cannot get plugin registrar on an engine that isn't running; "
92 "call Run first."
93 << std::endl;
94 return nullptr;
95 }
96 return FlutterDesktopEngineGetPluginRegistrar(engine_, plugin_name.c_str());
97 }
98
99 void FlutterEngine::SetNextFrameCallback(std::function<void()> callback) {
100 next_frame_callback_ = std::move(callback);
101 FlutterDesktopEngineSetNextFrameCallback(
102 engine_,
103 [](void* user_data) {
104 FlutterEngine* self = static_cast<FlutterEngine*>(user_data);
105 self->next_frame_callback_();
106 self->next_frame_callback_ = nullptr;
107 },
108 this);
109 }
110
111 std::optional<LRESULT> FlutterEngine::ProcessExternalWindowMessage(
112 HWND hwnd,
113 UINT message,
114 WPARAM wparam,
115 LPARAM lparam) {
116 LRESULT result;
117 if (FlutterDesktopEngineProcessExternalWindowMessage(
118 engine_, hwnd, message, wparam, lparam, &result)) {
119 return result;
120 }
121 return std::nullopt;
122 }
123
124 FlutterDesktopEngineRef FlutterEngine::RelinquishEngine() {
125 owns_engine_ = false;
126 return engine_;
127 }
128
129 } // namespace flutter
binary_messenger_impl.h
flutter::DartProject
Definition: dart_project.h:50
flutter::DartProject::accessibility_mode
AccessibilityMode accessibility_mode() const
Definition: dart_project.h:135
flutter::DartProject::ui_thread_policy
UIThreadPolicy ui_thread_policy() const
Definition: dart_project.h:126
flutter::DartProject::dart_entrypoint
const std::string & dart_entrypoint() const
Definition: dart_project.h:96
flutter::DartProject::dart_entrypoint_arguments
const std::vector< std::string > & dart_entrypoint_arguments() const
Definition: dart_project.h:106
flutter::DartProject::gpu_preference
GpuPreference gpu_preference() const
Definition: dart_project.h:117
flutter::FlutterEngine
Definition: flutter_engine.h:28
flutter::FlutterEngine::ProcessExternalWindowMessage
std::optional< LRESULT > ProcessExternalWindowMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: flutter_engine.cc:111
flutter::FlutterEngine::ProcessMessages
std::chrono::nanoseconds ProcessMessages()
Definition: flutter_engine.cc:80
flutter::FlutterEngine::GetRegistrarForPlugin
FlutterDesktopPluginRegistrarRef GetRegistrarForPlugin(const std::string &plugin_name) override
Definition: flutter_engine.cc:88
flutter::FlutterEngine::Run
bool Run()
Definition: flutter_engine.cc:52
flutter::FlutterEngine::ReloadSystemFonts
void ReloadSystemFonts()
Definition: flutter_engine.cc:84
flutter::FlutterEngine::FlutterEngine
FlutterEngine(const DartProject &project)
Definition: flutter_engine.cc:15
flutter::FlutterEngine::SetNextFrameCallback
void SetNextFrameCallback(std::function< void()> callback)
Definition: flutter_engine.cc:99
flutter::FlutterEngine::ShutDown
void ShutDown()
Definition: flutter_engine.cc:73
flutter::FlutterEngine::~FlutterEngine
virtual ~FlutterEngine()
Definition: flutter_engine.cc:48
flutter_engine.h
FlutterDesktopEngineCreate
FlutterDesktopEngineRef FlutterDesktopEngineCreate(const FlutterDesktopEngineProperties *engine_properties)
Definition: flutter_windows.cc:184
FlutterDesktopEngineReloadSystemFonts
void FlutterDesktopEngineReloadSystemFonts(FlutterDesktopEngineRef engine)
Definition: flutter_windows.cc:221
FlutterDesktopEngineProcessExternalWindowMessage
bool FlutterDesktopEngineProcessExternalWindowMessage(FlutterDesktopEngineRef engine, HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam, LRESULT *result)
Definition: flutter_windows.cc:279
FlutterDesktopEngineGetPluginRegistrar
FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar(FlutterDesktopEngineRef engine, const char *plugin_name)
Definition: flutter_windows.cc:225
FlutterDesktopEngineDestroy
bool FlutterDesktopEngineDestroy(FlutterDesktopEngineRef engine_ref)
Definition: flutter_windows.cc:191
FlutterDesktopEngineProcessMessages
uint64_t FlutterDesktopEngineProcessMessages(FlutterDesktopEngineRef engine)
Definition: flutter_windows.cc:217
FlutterDesktopEngineGetMessenger
FlutterDesktopMessengerRef FlutterDesktopEngineGetMessenger(FlutterDesktopEngineRef engine)
Definition: flutter_windows.cc:235
FlutterDesktopEngineSetNextFrameCallback
void FlutterDesktopEngineSetNextFrameCallback(FlutterDesktopEngineRef engine, VoidCallback callback, void *user_data)
Definition: flutter_windows.cc:246
FlutterDesktopEngineRun
bool FlutterDesktopEngineRun(FlutterDesktopEngineRef engine, const char *entry_point)
Definition: flutter_windows.cc:207
flutter_windows.h
FlutterDesktopEngineRef
struct FlutterDesktopEngine * FlutterDesktopEngineRef
Definition: flutter_windows.h:33
FlutterDesktopUIThreadPolicy
FlutterDesktopUIThreadPolicy
Definition: flutter_windows.h:53
FlutterDesktopGpuPreference
FlutterDesktopGpuPreference
Definition: flutter_windows.h:39
FlutterDesktopAccessibilityMode
FlutterDesktopAccessibilityMode
Definition: flutter_windows.h:64
user_data
void * user_data
Definition: flutter_windows_view_unittests.cc:53
callback
FlutterDesktopBinaryReply callback
Definition: flutter_windows_view_unittests.cc:52
message
Win32Message message
Definition: keyboard_unittests.cc:137
flutter
Definition: accessibility_bridge_windows.cc:11
FlutterDesktopEngineProperties
Definition: flutter_windows.h:75
FlutterDesktopEngineProperties::dart_entrypoint
const char * dart_entrypoint
Definition: flutter_windows.h:98
FlutterDesktopEngineProperties::accessibility_mode
FlutterDesktopAccessibilityMode accessibility_mode
Definition: flutter_windows.h:115
FlutterDesktopEngineProperties::icu_data_path
const wchar_t * icu_data_path
Definition: flutter_windows.h:84
FlutterDesktopEngineProperties::dart_entrypoint_argc
int dart_entrypoint_argc
Definition: flutter_windows.h:101
FlutterDesktopEngineProperties::assets_path
const wchar_t * assets_path
Definition: flutter_windows.h:79
FlutterDesktopEngineProperties::dart_entrypoint_argv
const char ** dart_entrypoint_argv
Definition: flutter_windows.h:105
FlutterDesktopEngineProperties::ui_thread_policy
FlutterDesktopUIThreadPolicy ui_thread_policy
Definition: flutter_windows.h:111
FlutterDesktopEngineProperties::aot_library_path
const wchar_t * aot_library_path
Definition: flutter_windows.h:90
FlutterDesktopEngineProperties::gpu_preference
FlutterDesktopGpuPreference gpu_preference
Definition: flutter_windows.h:108
FlutterDesktopPluginRegistrar
Definition: window_state.h:23