Flutter Windows Embedder
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
flutter_windows_engine.h
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 #ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WINDOWS_ENGINE_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WINDOWS_ENGINE_H_
7 
8 #include <chrono>
9 #include <map>
10 #include <memory>
11 #include <optional>
12 #include <shared_mutex>
13 #include <string>
14 #include <string_view>
15 #include <unordered_map>
16 #include <vector>
17 
18 #include "flutter/fml/closure.h"
19 #include "flutter/fml/macros.h"
25 #include "flutter/shell/platform/embedder/embedder.h"
47 #include "third_party/rapidjson/include/rapidjson/document.h"
48 
49 namespace flutter {
50 
51 // The implicit view's ID.
52 //
53 // See:
54 // https://api.flutter-io.cn/flutter/dart-ui/PlatformDispatcher/implicitView.html
56 
57 class FlutterWindowsView;
58 
59 // Update the thread priority for the Windows engine.
61  FlutterThreadPriority priority) {
62  // TODO(99502): Add support for tracing to the windows embedding so we can
63  // mark thread priorities and success/failure.
64  switch (priority) {
65  case FlutterThreadPriority::kBackground: {
66  SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
67  break;
68  }
69  case FlutterThreadPriority::kDisplay: {
70  SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
71  break;
72  }
73  case FlutterThreadPriority::kRaster: {
74  SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
75  break;
76  }
77  case FlutterThreadPriority::kNormal: {
78  // For normal or default priority we do not need to set the priority
79  // class.
80  break;
81  }
82  }
83 }
84 
85 // Manages state associated with the underlying FlutterEngine that isn't
86 // related to its display.
87 //
88 // In most cases this will be associated with a FlutterView, but if not will
89 // run in headless mode.
91  public:
92  // Creates a new Flutter engine object configured to run |project|.
94  const FlutterProjectBundle& project,
95  std::shared_ptr<WindowsProcTable> windows_proc_table = nullptr);
96 
97  virtual ~FlutterWindowsEngine();
98 
99  // Starts running the entrypoint function specifed in the project bundle. If
100  // unspecified, defaults to main().
101  //
102  // Returns false if the engine couldn't be started.
103  bool Run();
104 
105  // Starts running the engine with the given entrypoint. If the empty string
106  // is specified, defaults to the entrypoint function specified in the project
107  // bundle, or main() if both are unspecified.
108  //
109  // Returns false if the engine couldn't be started or if conflicting,
110  // non-default values are passed here and in the project bundle..
111  //
112  // DEPRECATED: Prefer setting the entrypoint in the FlutterProjectBundle
113  // passed to the constructor and calling the no-parameter overload.
114  bool Run(std::string_view entrypoint);
115 
116  // Returns true if the engine is currently running.
117  virtual bool running() const { return engine_ != nullptr; }
118 
119  // Stops the engine. This invalidates the pointer returned by engine().
120  //
121  // Returns false if stopping the engine fails, or if it was not running.
122  virtual bool Stop();
123 
124  // Create a view that can display this engine's content.
125  //
126  // Returns null on failure.
127  std::unique_ptr<FlutterWindowsView> CreateView(
128  std::unique_ptr<WindowBindingHandler> window);
129 
130  // Remove a view. The engine will no longer render into it.
131  virtual void RemoveView(FlutterViewId view_id);
132 
133  // Get a view that displays this engine's content.
134  //
135  // Returns null if the view does not exist.
136  FlutterWindowsView* view(FlutterViewId view_id) const;
137 
138  // Returns the currently configured Plugin Registrar.
140 
141  // Registers |callback| to be called when the plugin registrar is destroyed.
145 
146  // Sets switches member to the given switches.
147  void SetSwitches(const std::vector<std::string>& switches);
148 
149  FlutterDesktopMessengerRef messenger() { return messenger_->ToRef(); }
150 
152  return message_dispatcher_.get();
153  }
154 
155  TaskRunner* task_runner() { return task_runner_.get(); }
156 
157  BinaryMessenger* messenger_wrapper() { return messenger_wrapper_.get(); }
158 
160  return texture_registrar_.get();
161  }
162 
163  // The EGL manager object. If this is nullptr, then we are
164  // rendering using software instead of OpenGL.
165  egl::Manager* egl_manager() const { return egl_manager_.get(); }
166 
168  return window_proc_delegate_manager_.get();
169  }
170 
171  // Informs the engine that the window metrics have changed.
172  void SendWindowMetricsEvent(const FlutterWindowMetricsEvent& event);
173 
174  // Informs the engine of an incoming pointer event.
175  void SendPointerEvent(const FlutterPointerEvent& event);
176 
177  // Informs the engine of an incoming key event.
178  void SendKeyEvent(const FlutterKeyEvent& event,
179  FlutterKeyEventCallback callback,
180  void* user_data);
181 
183  return keyboard_key_handler_.get();
184  }
185  TextInputPlugin* text_input_plugin() { return text_input_plugin_.get(); }
186 
187  // Sends the given message to the engine, calling |reply| with |user_data|
188  // when a response is received from the engine if they are non-null.
189  bool SendPlatformMessage(const char* channel,
190  const uint8_t* message,
191  const size_t message_size,
192  const FlutterDesktopBinaryReply reply,
193  void* user_data);
194 
195  // Sends the given data as the response to an earlier platform message.
198  const uint8_t* data,
199  size_t data_length);
200 
201  // Callback passed to Flutter engine for notifying window of platform
202  // messages.
203  void HandlePlatformMessage(const FlutterPlatformMessage*);
204 
205  // Informs the engine that the system font list has changed.
206  void ReloadSystemFonts();
207 
208  // Informs the engine that a new frame is needed to redraw the content.
209  void ScheduleFrame();
210 
211  // Set the callback that is called when the next frame is drawn.
212  void SetNextFrameCallback(fml::closure callback);
213 
214  // Attempts to register the texture with the given |texture_id|.
215  bool RegisterExternalTexture(int64_t texture_id);
216 
217  // Attempts to unregister the texture with the given |texture_id|.
219 
220  // Notifies the engine about a new frame being available for the
221  // given |texture_id|.
223 
224  // Posts the given callback onto the raster thread.
225  virtual bool PostRasterThreadTask(fml::closure callback) const;
226 
227  // Invoke on the embedder's vsync callback to schedule a frame.
228  void OnVsync(intptr_t baton);
229 
230  // Dispatches a semantics action to the specified semantics node.
231  bool DispatchSemanticsAction(uint64_t id,
232  FlutterSemanticsAction action,
233  fml::MallocMapping data);
234 
235  // Informs the engine that the semantics enabled state has changed.
236  void UpdateSemanticsEnabled(bool enabled);
237 
238  // Returns true if the semantics tree is enabled.
239  bool semantics_enabled() const { return semantics_enabled_; }
240 
241  // Refresh accessibility features and send them to the engine.
243 
244  // Refresh high contrast accessibility mode and notify the engine.
245  void UpdateHighContrastMode();
246 
247  // Returns true if the high contrast feature is enabled.
248  bool high_contrast_enabled() const { return high_contrast_enabled_; }
249 
250  // Register a root isolate create callback.
251  //
252  // The root isolate create callback is invoked at creation of the root Dart
253  // isolate in the app. This may be used to be notified that execution of the
254  // main Dart entrypoint is about to begin, and is used by test infrastructure
255  // to register a native function resolver that can register and resolve
256  // functions marked as native in the Dart code.
257  //
258  // This must be called before calling |Run|.
259  void SetRootIsolateCreateCallback(const fml::closure& callback) {
260  root_isolate_create_callback_ = callback;
261  }
262 
263  // Returns the executable name for this process or "Flutter" if unknown.
264  std::string GetExecutableName() const;
265 
266  // Called when the application quits in response to a quit request.
267  void OnQuit(std::optional<HWND> hwnd,
268  std::optional<WPARAM> wparam,
269  std::optional<LPARAM> lparam,
270  UINT exit_code);
271 
272  // Called when a WM_CLOSE message is received.
273  void RequestApplicationQuit(HWND hwnd,
274  WPARAM wparam,
275  LPARAM lparam,
276  AppExitType exit_type);
277 
278  // Called when a WM_DWMCOMPOSITIONCHANGED message is received.
280 
281  // Called when a Window receives an event that may alter the application
282  // lifecycle state.
283  void OnWindowStateEvent(HWND hwnd, WindowStateEvent event);
284 
285  // Handle a message from a non-Flutter window in the same application.
286  // Returns a result when the message is consumed and should not be processed
287  // further.
288  std::optional<LRESULT> ProcessExternalWindowMessage(HWND hwnd,
289  UINT message,
290  WPARAM wparam,
291  LPARAM lparam);
292 
294  return lifecycle_manager_.get();
295  }
296 
297  std::shared_ptr<WindowsProcTable> windows_proc_table() {
298  return windows_proc_table_;
299  }
300 
301  protected:
302  // Creates the keyboard key handler.
303  //
304  // Exposing this method allows unit tests to override in order to
305  // capture information.
306  virtual std::unique_ptr<KeyboardHandlerBase> CreateKeyboardKeyHandler(
310 
311  // Creates the text input plugin.
312  //
313  // Exposing this method allows unit tests to override in order to
314  // capture information.
315  virtual std::unique_ptr<TextInputPlugin> CreateTextInputPlugin(
317 
318  // Invoked by the engine right before the engine is restarted.
319  //
320  // This should reset necessary states to as if the engine has just been
321  // created. This is typically caused by a hot restart (Shift-R in CLI.)
322  void OnPreEngineRestart();
323 
324  // Invoked by the engine when a listener is set or cleared on a platform
325  // channel.
326  virtual void OnChannelUpdate(std::string name, bool listening);
327 
328  private:
329  // Allows swapping out embedder_api_ calls in tests.
330  friend class EngineModifier;
331 
332  // Sends system locales to the engine.
333  //
334  // Should be called just after the engine is run, and after any relevant
335  // system changes.
336  void SendSystemLocales();
337 
338  // Sends the current lifecycle state to the framework.
339  void SetLifecycleState(flutter::AppLifecycleState state);
340 
341  // Create the keyboard & text input sub-systems.
342  //
343  // This requires that a view is attached to the engine.
344  // Calling this method again resets the keyboard state.
345  void InitializeKeyboard();
346 
347  // Send the currently enabled accessibility features to the engine.
348  void SendAccessibilityFeatures();
349 
350  // Present content to a view. Returns true if the content was presented.
351  //
352  // This is invoked on the raster thread.
353  bool Present(const FlutterPresentViewInfo* info);
354 
355  // The handle to the embedder.h engine instance.
356  FLUTTER_API_SYMBOL(FlutterEngine) engine_ = nullptr;
357 
358  FlutterEngineProcTable embedder_api_ = {};
359 
360  std::unique_ptr<FlutterProjectBundle> project_;
361 
362  // AOT data, if any.
363  UniqueAotDataPtr aot_data_;
364 
365  // The ID that the next view will have.
366  FlutterViewId next_view_id_ = kImplicitViewId;
367 
368  // The views displaying the content running in this engine, if any.
369  //
370  // This is read and mutated by the platform thread. This is read by the raster
371  // thread to present content to a view.
372  //
373  // Reads to this object on non-platform threads must be protected
374  // by acquiring a shared lock on |views_mutex_|.
375  //
376  // Writes to this object must only happen on the platform thread
377  // and must be protected by acquiring an exclusive lock on |views_mutex_|.
378  std::unordered_map<FlutterViewId, FlutterWindowsView*> views_;
379 
380  // The mutex that protects the |views_| map.
381  //
382  // The raster thread acquires a shared lock to present to a view.
383  //
384  // The platform thread acquires a shared lock to access the view.
385  // The platform thread acquires an exclusive lock before adding
386  // a view to the engine or after removing a view from the engine.
387  mutable std::shared_mutex views_mutex_;
388 
389  // Task runner for tasks posted from the engine.
390  std::unique_ptr<TaskRunner> task_runner_;
391 
392  // The plugin messenger handle given to API clients.
393  fml::RefPtr<flutter::FlutterDesktopMessenger> messenger_;
394 
395  // A wrapper around messenger_ for interacting with client_wrapper-level APIs.
396  std::unique_ptr<BinaryMessengerImpl> messenger_wrapper_;
397 
398  // Message dispatch manager for messages from engine_.
399  std::unique_ptr<IncomingMessageDispatcher> message_dispatcher_;
400 
401  // The plugin registrar handle given to API clients.
402  std::unique_ptr<FlutterDesktopPluginRegistrar> plugin_registrar_;
403 
404  // The texture registrar.
405  std::unique_ptr<FlutterWindowsTextureRegistrar> texture_registrar_;
406 
407  // An object used for intializing ANGLE and creating / destroying render
408  // surfaces. If nullptr, ANGLE failed to initialize and software rendering
409  // should be used instead.
410  std::unique_ptr<egl::Manager> egl_manager_;
411 
412  // The compositor that creates backing stores for the engine to render into
413  // and then presents them onto views.
414  std::unique_ptr<Compositor> compositor_;
415 
416  // The plugin registrar managing internal plugins.
417  std::unique_ptr<PluginRegistrar> internal_plugin_registrar_;
418 
419  // Handler for accessibility events.
420  std::unique_ptr<AccessibilityPlugin> accessibility_plugin_;
421 
422  // Handler for cursor events.
423  std::unique_ptr<CursorHandler> cursor_handler_;
424 
425  // Handler for the flutter/platform channel.
426  std::unique_ptr<PlatformHandler> platform_handler_;
427 
428  // Handlers for keyboard events from Windows.
429  std::unique_ptr<KeyboardHandlerBase> keyboard_key_handler_;
430 
431  // Handlers for text events from Windows.
432  std::unique_ptr<TextInputPlugin> text_input_plugin_;
433 
434  // The settings plugin.
435  std::unique_ptr<SettingsPlugin> settings_plugin_;
436 
437  // Callbacks to be called when the engine (and thus the plugin registrar) is
438  // being destroyed.
441  plugin_registrar_destruction_callbacks_;
442 
443  // The approximate time between vblank events.
444  std::chrono::nanoseconds FrameInterval();
445 
446  // The start time used to align frames.
447  std::chrono::nanoseconds start_time_ = std::chrono::nanoseconds::zero();
448 
449  // An override of the frame interval used by EngineModifier for testing.
450  std::optional<std::chrono::nanoseconds> frame_interval_override_ =
451  std::nullopt;
452 
453  bool semantics_enabled_ = false;
454 
455  bool high_contrast_enabled_ = false;
456 
457  bool enable_impeller_ = false;
458 
459  // The manager for WindowProc delegate registration and callbacks.
460  std::unique_ptr<WindowProcDelegateManager> window_proc_delegate_manager_;
461 
462  // The root isolate creation callback.
463  fml::closure root_isolate_create_callback_;
464 
465  // The on frame drawn callback.
466  fml::closure next_frame_callback_;
467 
468  // Handler for top level window messages.
469  std::unique_ptr<WindowsLifecycleManager> lifecycle_manager_;
470 
471  std::shared_ptr<WindowsProcTable> windows_proc_table_;
472 
473  std::shared_ptr<egl::ProcTable> gl_;
474 
475  std::unique_ptr<PlatformViewPlugin> platform_view_plugin_;
476 
477  FML_DISALLOW_COPY_AND_ASSIGN(FlutterWindowsEngine);
478 };
479 
480 } // namespace flutter
481 
482 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WINDOWS_ENGINE_H_
FlutterWindowsTextureRegistrar * texture_registrar()
std::shared_ptr< WindowsProcTable > windows_proc_table()
FlutterWindowsEngine(const FlutterProjectBundle &project, std::shared_ptr< WindowsProcTable > windows_proc_table=nullptr)
FlutterDesktopMessengerRef messenger()
WindowsLifecycleManager * lifecycle_manager()
bool DispatchSemanticsAction(uint64_t id, FlutterSemanticsAction action, fml::MallocMapping data)
void OnWindowStateEvent(HWND hwnd, WindowStateEvent event)
void RequestApplicationQuit(HWND hwnd, WPARAM wparam, LPARAM lparam, AppExitType exit_type)
WindowProcDelegateManager * window_proc_delegate_manager()
FlutterWindowsView * view(FlutterViewId view_id) const
bool SendPlatformMessage(const char *channel, const uint8_t *message, const size_t message_size, const FlutterDesktopBinaryReply reply, void *user_data)
std::optional< LRESULT > ProcessExternalWindowMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
void AddPluginRegistrarDestructionCallback(FlutterDesktopOnPluginRegistrarDestroyed callback, FlutterDesktopPluginRegistrarRef registrar)
bool RegisterExternalTexture(int64_t texture_id)
void SendPlatformMessageResponse(const FlutterDesktopMessageResponseHandle *handle, const uint8_t *data, size_t data_length)
void SetRootIsolateCreateCallback(const fml::closure &callback)
virtual void OnChannelUpdate(std::string name, bool listening)
virtual std::unique_ptr< KeyboardHandlerBase > CreateKeyboardKeyHandler(BinaryMessenger *messenger, KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state, KeyboardKeyEmbedderHandler::MapVirtualKeyToScanCode map_vk_to_scan)
bool MarkExternalTextureFrameAvailable(int64_t texture_id)
KeyboardHandlerBase * keyboard_key_handler()
std::unique_ptr< FlutterWindowsView > CreateView(std::unique_ptr< WindowBindingHandler > window)
virtual bool PostRasterThreadTask(fml::closure callback) const
IncomingMessageDispatcher * message_dispatcher()
bool UnregisterExternalTexture(int64_t texture_id)
void SetNextFrameCallback(fml::closure callback)
void HandlePlatformMessage(const FlutterPlatformMessage *)
virtual void RemoveView(FlutterViewId view_id)
FlutterDesktopPluginRegistrarRef GetRegistrar()
virtual std::unique_ptr< TextInputPlugin > CreateTextInputPlugin(BinaryMessenger *messenger)
void SendPointerEvent(const FlutterPointerEvent &event)
void SendWindowMetricsEvent(const FlutterWindowMetricsEvent &event)
void SetSwitches(const std::vector< std::string > &switches)
void SendKeyEvent(const FlutterKeyEvent &event, FlutterKeyEventCallback callback, void *user_data)
void OnQuit(std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, UINT exit_code)
std::function< SHORT(UINT, bool)> MapVirtualKeyToScanCode
uint32_t texture_id
void(* FlutterDesktopBinaryReply)(const uint8_t *data, size_t data_size, void *user_data)
struct FlutterDesktopMessenger * FlutterDesktopMessengerRef
struct _FlutterPlatformMessageResponseHandle FlutterDesktopMessageResponseHandle
void(* FlutterDesktopOnPluginRegistrarDestroyed)(FlutterDesktopPluginRegistrarRef)
FlutterDesktopBinaryReply callback
Win32Message message
WindowStateEvent
An event representing a change in window state that may update the.
int64_t FlutterViewId
Definition: flutter_view.h:13
static void WindowsPlatformThreadPrioritySetter(FlutterThreadPriority priority)
constexpr FlutterViewId kImplicitViewId
std::unique_ptr< _FlutterEngineAOTData, FlutterEngineCollectAOTDataFnPtr > UniqueAotDataPtr