Flutter Engine
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 <map>
9 #include <memory>
10 #include <optional>
11 #include <vector>
12 
13 #include "flutter/shell/platform/common/client_wrapper/binary_messenger_impl.h"
14 #include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h"
15 #include "flutter/shell/platform/common/incoming_message_dispatcher.h"
16 #include "flutter/shell/platform/embedder/embedder.h"
17 #include "flutter/shell/platform/windows/angle_surface_manager.h"
18 #include "flutter/shell/platform/windows/flutter_project_bundle.h"
19 #include "flutter/shell/platform/windows/flutter_windows_texture_registrar.h"
20 #include "flutter/shell/platform/windows/public/flutter_windows.h"
21 #include "flutter/shell/platform/windows/task_runner.h"
22 #include "flutter/shell/platform/windows/window_state.h"
23 #include "third_party/rapidjson/include/rapidjson/document.h"
24 
25 #ifndef WINUWP
26 #include "flutter/shell/platform/windows/window_proc_delegate_manager_win32.h" // nogncheck
27 #endif
28 
29 namespace flutter {
30 
31 class FlutterWindowsView;
32 
33 // Manages state associated with the underlying FlutterEngine that isn't
34 // related to its display.
35 //
36 // In most cases this will be associated with a FlutterView, but if not will
37 // run in headless mode.
39  public:
40  // Creates a new Flutter engine object configured to run |project|.
41  explicit FlutterWindowsEngine(const FlutterProjectBundle& project);
42 
43  virtual ~FlutterWindowsEngine();
44 
45  // Prevent copying.
48 
49  // Starts running the engine with the given entrypoint. If null, defaults to
50  // main().
51  //
52  // Returns false if the engine couldn't be started.
53  bool RunWithEntrypoint(const char* entrypoint);
54 
55  // Returns true if the engine is currently running.
56  bool running() { return engine_ != nullptr; }
57 
58  // Stops the engine. This invalidates the pointer returned by engine().
59  //
60  // Returns false if stopping the engine fails, or if it was not running.
61  bool Stop();
62 
63  // Sets the view that is displaying this engine's content.
65 
66  // The view displaying this engine's content, if any. This will be null for
67  // headless engines.
68  FlutterWindowsView* view() { return view_; }
69 
70  // Returns the currently configured Plugin Registrar.
72 
73  // Sets |callback| to be called when the plugin registrar is destroyed.
76 
77  // Sets switches member to the given switches.
78  void SetSwitches(const std::vector<std::string>& switches);
79 
80  FlutterDesktopMessengerRef messenger() { return messenger_.get(); }
81 
83  return message_dispatcher_.get();
84  }
85 
86  TaskRunner* task_runner() { return task_runner_.get(); }
87 
89  return texture_registrar_.get();
90  }
91 
92  // The ANGLE surface manager object. If this is nullptr, then we are
93  // rendering using software instead of OpenGL.
94  AngleSurfaceManager* surface_manager() { return surface_manager_.get(); }
95 
96 #ifndef WINUWP
98  return window_proc_delegate_manager_.get();
99  }
100 #endif
101 
102  // Informs the engine that the window metrics have changed.
104 
105  // Informs the engine of an incoming pointer event.
106  void SendPointerEvent(const FlutterPointerEvent& event);
107 
108  // Informs the engine of an incoming key event.
109  void SendKeyEvent(const FlutterKeyEvent& event,
110  FlutterKeyEventCallback callback,
111  void* user_data);
112 
113  // Sends the given message to the engine, calling |reply| with |user_data|
114  // when a response is received from the engine if they are non-null.
115  bool SendPlatformMessage(const char* channel,
116  const uint8_t* message,
117  const size_t message_size,
118  const FlutterDesktopBinaryReply reply,
119  void* user_data);
120 
121  // Sends the given data as the response to an earlier platform message.
124  const uint8_t* data,
125  size_t data_length);
126 
127  // Callback passed to Flutter engine for notifying window of platform
128  // messages.
130 
131  // Informs the engine that the system font list has changed.
132  void ReloadSystemFonts();
133 
134  // Informs the engine that the platform brightness has changed.
136 
137  // Attempts to register the texture with the given |texture_id|.
138  bool RegisterExternalTexture(int64_t texture_id);
139 
140  // Attempts to unregister the texture with the given |texture_id|.
141  bool UnregisterExternalTexture(int64_t texture_id);
142 
143  // Notifies the engine about a new frame being available for the
144  // given |texture_id|.
145  bool MarkExternalTextureFrameAvailable(int64_t texture_id);
146 
147  private:
148  // Allows swapping out embedder_api_ calls in tests.
149  friend class EngineModifier;
150 
151  // Sends system settings (e.g., locale) to the engine.
152  //
153  // Should be called just after the engine is run, and after any relevant
154  // system changes.
155  void SendSystemSettings();
156 
157  // The handle to the embedder.h engine instance.
158  FLUTTER_API_SYMBOL(FlutterEngine) engine_ = nullptr;
159 
160  FlutterEngineProcTable embedder_api_ = {};
161 
162  std::unique_ptr<FlutterProjectBundle> project_;
163 
164  // AOT data, if any.
165  UniqueAotDataPtr aot_data_;
166 
167  // The view displaying the content running in this engine, if any.
168  FlutterWindowsView* view_ = nullptr;
169 
170  // Task runner for tasks posted from the engine.
171  std::unique_ptr<TaskRunner> task_runner_;
172 
173  // The plugin messenger handle given to API clients.
174  std::unique_ptr<FlutterDesktopMessenger> messenger_;
175 
176  // A wrapper around messenger_ for interacting with client_wrapper-level APIs.
177  std::unique_ptr<BinaryMessengerImpl> messenger_wrapper_;
178 
179  // Message dispatch manager for messages from engine_.
180  std::unique_ptr<IncomingMessageDispatcher> message_dispatcher_;
181 
182  // The plugin registrar handle given to API clients.
183  std::unique_ptr<FlutterDesktopPluginRegistrar> plugin_registrar_;
184 
185  // The texture registrar.
186  std::unique_ptr<FlutterWindowsTextureRegistrar> texture_registrar_;
187 
188  // Resolved OpenGL functions used by external texture implementations.
189  GlProcs gl_procs_ = {};
190 
191  // An object used for intializing Angle and creating / destroying render
192  // surfaces. Surface creation functionality requires a valid render_target.
193  // May be nullptr if ANGLE failed to initialize.
194  std::unique_ptr<AngleSurfaceManager> surface_manager_;
195 
196  // The MethodChannel used for communication with the Flutter engine.
197  std::unique_ptr<BasicMessageChannel<rapidjson::Document>> settings_channel_;
198 
199  // A callback to be called when the engine (and thus the plugin registrar)
200  // is being destroyed.
202  plugin_registrar_destruction_callback_ = nullptr;
203 
204 #ifndef WINUWP
205  // The manager for WindowProc delegate registration and callbacks.
206  std::unique_ptr<WindowProcDelegateManagerWin32> window_proc_delegate_manager_;
207 #endif
208 };
209 
210 } // namespace flutter
211 
212 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WINDOWS_ENGINE_H_
void SetPluginRegistrarDestructionCallback(FlutterDesktopOnPluginRegistrarDestroyed callback)
struct _FlutterEngine * FLUTTER_API_SYMBOL(FlutterEngine)
Definition: embedder.h:228
bool RegisterExternalTexture(int64_t texture_id)
Function-pointer-based versions of the APIs above.
Definition: embedder.h:2372
bool SendPlatformMessage(const char *channel, const uint8_t *message, const size_t message_size, const FlutterDesktopBinaryReply reply, void *user_data)
void * user_data
int64_t texture_id
bool MarkExternalTextureFrameAvailable(int64_t texture_id)
FlutterWindowsEngine(const FlutterProjectBundle &project)
bool RunWithEntrypoint(const char *entrypoint)
void SendPointerEvent(const FlutterPointerEvent &event)
void SetSwitches(const std::vector< std::string > &switches)
FlKeyEvent FlKeyResponderAsyncCallback callback
FlutterDesktopPluginRegistrarRef GetRegistrar()
FlKeyEvent * event
bool UnregisterExternalTexture(int64_t texture_id)
FlutterWindowsTextureRegistrar * texture_registrar()
void SendWindowMetricsEvent(const FlutterWindowMetricsEvent &event)
WindowProcDelegateManagerWin32 * window_proc_delegate_manager()
IncomingMessageDispatcher * message_dispatcher()
FlutterDesktopMessengerRef messenger()
FlutterWindowsEngine & operator=(FlutterWindowsEngine const &)=delete
AngleSurfaceManager * surface_manager()
void SendKeyEvent(const FlutterKeyEvent &event, FlutterKeyEventCallback callback, void *user_data)
void SendPlatformMessageResponse(const FlutterDesktopMessageResponseHandle *handle, const uint8_t *data, size_t data_length)
void HandlePlatformMessage(const FlutterPlatformMessage *)
void(* FlutterDesktopBinaryReply)(const uint8_t *data, size_t data_size, void *user_data)
void(* FlutterDesktopOnPluginRegistrarDestroyed)(FlutterDesktopPluginRegistrarRef)
void(* FlutterKeyEventCallback)(bool, void *)
Definition: embedder.h:748
void SetView(FlutterWindowsView *view)
std::unique_ptr< _FlutterEngineAOTData, FlutterEngineCollectAOTDataFnPtr > UniqueAotDataPtr