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 
6 
7 #include <algorithm>
8 #include <iostream>
9 
10 #include "binary_messenger_impl.h"
11 
12 namespace 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 =
22  project.dart_entrypoint_arguments();
23  std::vector<const char*> entrypoint_argv;
24  std::transform(
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 
48 bool 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 
72 std::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 
91 void FlutterEngine::SetNextFrameCallback(std::function<void()> callback) {
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,
105  UINT message,
106  WPARAM wparam,
107  LPARAM lparam) {
108  LRESULT result;
110  engine_, hwnd, message, wparam, lparam, &result)) {
111  return result;
112  }
113  return std::nullopt;
114 }
115 
116 FlutterDesktopEngineRef FlutterEngine::RelinquishEngine() {
117  owns_engine_ = false;
118  return engine_;
119 }
120 
121 } // namespace flutter
FlutterDesktopEngineProperties::aot_library_path
const wchar_t * aot_library_path
Definition: flutter_windows.h:54
FlutterDesktopEngineGetMessenger
FlutterDesktopMessengerRef FlutterDesktopEngineGetMessenger(FlutterDesktopEngineRef engine)
Definition: flutter_windows.cc:228
FlutterDesktopEngineProperties
Definition: flutter_windows.h:39
flutter::FlutterEngine
Definition: flutter_engine.h:28
flutter::DartProject
Definition: dart_project.h:14
flutter::FlutterEngine::SetNextFrameCallback
void SetNextFrameCallback(std::function< void()> callback)
Definition: flutter_engine.cc:91
user_data
void * user_data
Definition: flutter_windows_view_unittests.cc:53
FlutterDesktopEngineProperties::dart_entrypoint_argv
const char ** dart_entrypoint_argv
Definition: flutter_windows.h:69
FlutterDesktopEngineProperties::icu_data_path
const wchar_t * icu_data_path
Definition: flutter_windows.h:48
FlutterDesktopEngineProcessExternalWindowMessage
bool FlutterDesktopEngineProcessExternalWindowMessage(FlutterDesktopEngineRef engine, HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam, LRESULT *result)
Definition: flutter_windows.cc:266
FlutterDesktopEngineGetPluginRegistrar
FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar(FlutterDesktopEngineRef engine, const char *plugin_name)
Definition: flutter_windows.cc:218
flutter_engine.h
FlutterDesktopEngineProperties::dart_entrypoint
const char * dart_entrypoint
Definition: flutter_windows.h:62
flutter::FlutterEngine::GetRegistrarForPlugin
FlutterDesktopPluginRegistrarRef GetRegistrarForPlugin(const std::string &plugin_name) override
Definition: flutter_engine.cc:80
flutter::FlutterEngine::Run
bool Run()
Definition: flutter_engine.cc:44
FlutterDesktopEngineRef
struct FlutterDesktopEngine * FlutterDesktopEngineRef
Definition: flutter_windows.h:33
flutter::FlutterEngine::ShutDown
void ShutDown()
Definition: flutter_engine.cc:65
flutter::DartProject::dart_entrypoint_arguments
const std::vector< std::string > & dart_entrypoint_arguments() const
Definition: dart_project.h:70
flutter::FlutterEngine::ProcessExternalWindowMessage
std::optional< LRESULT > ProcessExternalWindowMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: flutter_engine.cc:103
FlutterDesktopEngineProcessMessages
uint64_t FlutterDesktopEngineProcessMessages(FlutterDesktopEngineRef engine)
Definition: flutter_windows.cc:210
flutter::FlutterEngine::~FlutterEngine
virtual ~FlutterEngine()
Definition: flutter_engine.cc:40
binary_messenger_impl.h
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::FlutterEngine::ReloadSystemFonts
void ReloadSystemFonts()
Definition: flutter_engine.cc:76
FlutterDesktopEngineDestroy
bool FlutterDesktopEngineDestroy(FlutterDesktopEngineRef engine_ref)
Definition: flutter_windows.cc:190
FlutterDesktopEngineCreate
FlutterDesktopEngineRef FlutterDesktopEngineCreate(const FlutterDesktopEngineProperties *engine_properties)
Definition: flutter_windows.cc:183
FlutterDesktopEngineProperties::dart_entrypoint_argc
int dart_entrypoint_argc
Definition: flutter_windows.h:65
message
Win32Message message
Definition: keyboard_unittests.cc:137
flutter::DartProject::dart_entrypoint
const std::string & dart_entrypoint() const
Definition: dart_project.h:60
FlutterDesktopEngineSetNextFrameCallback
void FlutterDesktopEngineSetNextFrameCallback(FlutterDesktopEngineRef engine, VoidCallback callback, void *user_data)
Definition: flutter_windows.cc:239
flutter::FlutterEngine::ProcessMessages
std::chrono::nanoseconds ProcessMessages()
Definition: flutter_engine.cc:72
FlutterDesktopEngineProperties::assets_path
const wchar_t * assets_path
Definition: flutter_windows.h:43
FlutterDesktopPluginRegistrar
Definition: window_state.h:23
FlutterDesktopEngineRun
bool FlutterDesktopEngineRun(FlutterDesktopEngineRef engine, const char *entry_point)
Definition: flutter_windows.cc:200
flutter::FlutterEngine::FlutterEngine
FlutterEngine(const DartProject &project)
Definition: flutter_engine.cc:14
FlutterDesktopEngineReloadSystemFonts
void FlutterDesktopEngineReloadSystemFonts(FlutterDesktopEngineRef engine)
Definition: flutter_windows.cc:214
callback
FlutterDesktopBinaryReply callback
Definition: flutter_windows_view_unittests.cc:52