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