Flutter Windows Embedder
flutter_windows.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_PUBLIC_FLUTTER_WINDOWS_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_PUBLIC_FLUTTER_WINDOWS_H_
7 
8 #include <dxgi.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <windows.h>
12 
13 #include "flutter_export.h"
14 #include "flutter_messenger.h"
16 
17 #if defined(__cplusplus)
18 extern "C" {
19 #endif
20 
21 typedef void (*VoidCallback)(void* /* user data */);
22 
23 // Opaque reference to a Flutter view controller.
24 struct FlutterDesktopViewController;
25 typedef struct FlutterDesktopViewController* FlutterDesktopViewControllerRef;
26 
27 // Opaque reference to a Flutter window.
28 struct FlutterDesktopView;
29 typedef struct FlutterDesktopView* FlutterDesktopViewRef;
30 
31 // Opaque reference to a Flutter engine instance.
32 struct FlutterDesktopEngine;
33 typedef struct FlutterDesktopEngine* FlutterDesktopEngineRef;
34 
35 // The unique identifier for a view.
36 typedef int64_t FlutterDesktopViewId;
37 
38 // Configures how the Flutter engine selects a GPU.
39 typedef enum {
40  // No preference.
42  // Prefer energy efficiency over performance, such as an integrated GPU.
43  // This falls back to a high performance GPU if no low power GPU is
44  // available.
46  // Prefer performance over energy efficiency, such as a discrete GPU or
47  // dedicated GPU.
48  // This falls back to a low power GPU if no high performance GPU is available.
51 
52 // Configures the thread policy for running the UI isolate.
53 typedef enum {
54  // Default value. Currently will run the UI isolate on separate thread,
55  // later will be changed to running the UI isolate on platform thread.
57  // Run the UI isolate on platform thread.
59  // Run the UI isolate on a separate thread.
62 
63 // Configures the accessibility implementation used by Flutter.
64 typedef enum {
65  // Default value. Flutter will automatically select the best available
66  // implementation.
68  // Use the IAccessible implementation.
70  // Use the experimental IAccessibleEx implementation.
73 
74 // Configures the Impeller enablement switch.
75 typedef enum {
76  // Use the default Impeller enablement behavior.
78  // Enable Impeller.
80  // Disable Impeller.
83 
84 // Properties for configuring a Flutter engine instance.
85 typedef struct {
86  // The path to the flutter_assets folder for the application to be run.
87  // This can either be an absolute path or a path relative to the directory
88  // containing the executable.
89  const wchar_t* assets_path;
90 
91  // The path to the icudtl.dat file for the version of Flutter you are using.
92  // This can either be an absolute path or a path relative to the directory
93  // containing the executable.
94  const wchar_t* icu_data_path;
95 
96  // The path to the AOT library file for your application, if any.
97  // This can either be an absolute path or a path relative to the directory
98  // containing the executable. This can be nullptr for a non-AOT build, as
99  // it will be ignored in that case.
100  const wchar_t* aot_library_path;
101 
102  // The name of the top-level Dart entrypoint function. If null or the empty
103  // string, 'main' is assumed. If a custom entrypoint is used, this parameter
104  // must specifiy the name of a top-level function in the same Dart library as
105  // the app's main() function. Custom entrypoint functions must be decorated
106  // with `@pragma('vm:entry-point')` to ensure the method is not tree-shaken
107  // by the Dart compiler.
108  const char* dart_entrypoint;
109 
110  // Number of elements in the array passed in as dart_entrypoint_argv.
112 
113  // Array of Dart entrypoint arguments. This is deep copied during the call
114  // to FlutterDesktopEngineCreate.
115  const char** dart_entrypoint_argv;
116 
117  // GPU choice preference
118  // If not set defaults to NoPreference;
120 
121  // Policy for the thread that runs UI isolate.
122  // If not set defaults to Default;
124 
125  // The accessibility mode.
126  // This can be used to enable the experimental IAccessibleEx implementation.
128 
129  // Policy for enabling the Impeller renderer.
131 
132  // Whether to enable the Flutter GPU API (package:flutter_gpu).
133  // Flutter GPU requires the Impeller renderer.
134  // If not set defaults to false.
137 
138 // ========== View Controller ==========
139 
140 // Creates a view that hosts and displays the given engine instance.
141 //
142 // This takes ownership of |engine|, so FlutterDesktopEngineDestroy should no
143 // longer be called on it, as it will be called internally when the view
144 // controller is destroyed. If creating the view controller fails, the engine
145 // will be destroyed immediately.
146 //
147 // If |engine| is not already running, the view controller will start running
148 // it automatically before displaying the window.
149 //
150 // The caller owns the returned reference, and is responsible for calling
151 // FlutterDesktopViewControllerDestroy. Returns a null pointer in the event of
152 // an error.
153 //
154 // The Win32 implementation accepts width, height with view hookup explicitly
155 // performed using the caller using HWND parenting.
158  int height,
159  FlutterDesktopEngineRef engine);
160 
161 // Shuts down the engine instance associated with |controller|, and cleans up
162 // associated state.
163 //
164 // |controller| is no longer valid after this call.
167 
168 // Returns the view controller's view ID.
170  FlutterDesktopViewControllerRef view_controller);
171 
172 // Returns the handle for the engine running in FlutterDesktopViewControllerRef.
173 //
174 // Its lifetime is the same as the |controller|'s.
177 
178 // Returns the view managed by the given controller.
181 
182 // Requests new frame from the engine and repaints the view.
185 
186 // Allows the Flutter engine and any interested plugins an opportunity to
187 // handle the given message.
188 //
189 // If the WindowProc was handled and further handling should stop, this returns
190 // true and |result| will be populated. |result| is not set if returning false.
193  HWND hwnd,
194  UINT message,
195  WPARAM wparam,
196  LPARAM lparam,
197  LRESULT* result);
198 
199 // ========== Engine ==========
200 
201 // Creates a Flutter engine with the given properties.
202 //
203 // The caller owns the returned reference, and is responsible for calling
204 // FlutterDesktopEngineDestroy. The lifetime of |engine_properties| is required
205 // to extend only until the end of this call.
207  const FlutterDesktopEngineProperties* engine_properties);
208 
209 // Shuts down and destroys the given engine instance. Returns true if the
210 // shutdown was successful, or if the engine was not running.
211 //
212 // |engine| is no longer valid after this call.
214 
215 // Starts running the given engine instance.
216 //
217 // The entry_point parameter is deprecated but preserved for
218 // backward-compatibility. If desired, a custom Dart entrypoint function can be
219 // set in the dart_entrypoint field of the FlutterDesktopEngineProperties
220 // struct passed to FlutterDesktopEngineCreate.
221 //
222 // If specified, entry_point must be the name of a top-level function from the
223 // same Dart library that contains the app's main() function, and must be
224 // decorated with `@pragma(vm:entry-point)` to ensure the method is not
225 // tree-shaken by the Dart compiler. If conflicting non-null values are passed
226 // to this function and via the FlutterDesktopEngineProperties struct, the run
227 // will fail.
228 //
229 // Returns false if running the engine failed.
231  const char* entry_point);
232 
233 // DEPRECATED: This is no longer necessary to call, Flutter will take care of
234 // processing engine messages transparently through DispatchMessage.
235 //
236 // Processes any pending events in the Flutter engine, and returns the
237 // number of nanoseconds until the next scheduled event (or max, if none).
238 //
239 // This should be called on every run of the application-level runloop, and
240 // a wait for native events in the runloop should never be longer than the
241 // last return value from this function.
242 FLUTTER_EXPORT uint64_t
244 
246  FlutterDesktopEngineRef engine);
247 
248 // Returns the plugin registrar handle for the plugin with the given name.
249 //
250 // The name must be unique across the application.
253  const char* plugin_name);
254 
255 // Returns the messenger associated with the engine.
256 //
257 // This does not provide an owning reference, so should *not* be balanced with a
258 // call to |FlutterDesktopMessengerRelease|.
259 //
260 // Callers should use |FlutterDesktopMessengerAddRef| if the returned pointer
261 // will potentially outlive 'engine', such as when passing it to another thread.
264 
265 // Returns the texture registrar associated with the engine.
268 
269 // Schedule a callback to be called after the next frame is drawn.
270 //
271 // This must be called from the platform thread. The callback is executed only
272 // once on the platform thread.
276  void* user_data);
277 
278 // Returns true if the current thread is the platform thread.
279 // This can be called on any thread.
281  FlutterDesktopEngineRef engine);
282 
283 // Schedule a callback to be called on the platform thread.
284 //
285 // This can be called on any thread. The callback is executed only
286 // once on the platform thread.
287 //
288 // If the task is discarded without being executed (e.g. during engine
289 // shutdown), |on_cancel| is called on the platform thread so the caller can
290 // cleanup allocations. |on_cancel| can be nullptr if no cleanup is needed.
294  VoidCallback on_cancel,
295  void* user_data);
296 
297 // ========== View ==========
298 
299 // Returns the backing HWND for manipulation in host application.
301 
302 // Returns the DXGI adapter used for rendering or nullptr in case of error.
303 // The caller must release the adapter.
304 // DEPRECATED: Use |FlutterDesktopEngineGetGraphicsAdapter| instead.
306  FlutterDesktopViewRef view);
307 
308 // Retrieves the DXGI adapter used for rendering. Returns true if the adapter
309 // was successfully retrieved, or false if an error occured.
310 // The caller must provide a valid pointer to an IDXGIAdapter* and is
311 // responsible for releasing the adapter.
314  IDXGIAdapter** adapter_out);
315 
316 // Called to pass an external window message to the engine for lifecycle
317 // state updates. Non-Flutter windows must call this method in their WndProc
318 // in order to be included in the logic for application lifecycle state
319 // updates. Returns a result if the message should be consumed.
322  HWND hwnd,
323  UINT message,
324  WPARAM wparam,
325  LPARAM lparam,
326  LRESULT* result);
327 
328 // ========== Plugin Registrar (extensions) ==========
329 // These are Windows-specific extensions to flutter_plugin_registrar.h
330 
331 // Function pointer type for top level WindowProc delegate registration.
332 //
333 // The user data will be whatever was passed to
334 // FlutterDesktopRegisterTopLevelWindowProcHandler.
335 //
336 // Implementations should populate |result| and return true if the WindowProc
337 // was handled and further handling should stop. |result| is ignored if the
338 // function returns false.
339 typedef bool (*FlutterDesktopWindowProcCallback)(HWND /* hwnd */,
340  UINT /* uMsg */,
341  WPARAM /*wParam*/,
342  LPARAM /* lParam*/,
343  void* /* user data */,
344  LRESULT* result);
345 
346 // Returns the implicit view associated with this registrar's engine instance,
347 // or null if there is no implicit view.
348 //
349 // See:
350 // https://api.flutter.dev/flutter/dart-ui/PlatformDispatcher/implicitView.html
351 //
352 // DEPRECATED: Use |FlutterDesktopPluginRegistrarGetViewById| instead.
355 
356 // Returns the view associated with the registrar's engine instance, or null if
357 // the view does not exist.
360  FlutterDesktopViewId view_id);
361 
362 FLUTTER_EXPORT void
366  void* user_data);
367 
368 FLUTTER_EXPORT void
372 
373 // Retrieves the DXGI adapter used for rendering. Returns true if the adapter
374 // was successfully retrieved, or false if an error occured.
375 // The caller must provide a valid pointer to an IDXGIAdapter* and is
376 // responsible for releasing the adapter.
379  IDXGIAdapter** adapter_out);
380 
381 // ========== Freestanding Utilities ==========
382 
383 // Gets the DPI for a given |hwnd|, depending on the supported APIs per
384 // windows version and DPI awareness mode. If nullptr is passed, returns the DPI
385 // of the primary monitor.
386 //
387 // This uses the same logic and fallback for older Windows versions that is used
388 // internally by Flutter to determine the DPI to use for displaying Flutter
389 // content, so should be used by any code (e.g., in plugins) that translates
390 // between Windows and Dart sizes/offsets.
392 
393 // Gets the DPI for a given |monitor|. If the API is not available, a default
394 // DPI of 96 is returned.
395 //
396 // See FlutterDesktopGetDpiForHWND for more information.
397 FLUTTER_EXPORT UINT FlutterDesktopGetDpiForMonitor(HMONITOR monitor);
398 
399 // Reopens stdout and stderr and resysncs the standard library output streams.
400 // Should be called if output is being directed somewhere in the runner process
401 // (e.g., after an AllocConsole call).
403 
404 #if defined(__cplusplus)
405 } // extern "C"
406 #endif
407 
408 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_PUBLIC_FLUTTER_WINDOWS_H_
#define FLUTTER_EXPORT
struct FlutterDesktopMessenger * FlutterDesktopMessengerRef
struct FlutterDesktopTextureRegistrar * FlutterDesktopTextureRegistrarRef
FLUTTER_EXPORT bool FlutterDesktopPluginRegistrarGetGraphicsAdapter(FlutterDesktopPluginRegistrarRef registrar, IDXGIAdapter **adapter_out)
FLUTTER_EXPORT void FlutterDesktopResyncOutputStreams()
FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopViewControllerGetView(FlutterDesktopViewControllerRef controller)
FLUTTER_EXPORT FlutterDesktopTextureRegistrarRef FlutterDesktopEngineGetTextureRegistrar(FlutterDesktopEngineRef engine)
struct FlutterDesktopViewController * FlutterDesktopViewControllerRef
FLUTTER_EXPORT void FlutterDesktopViewControllerDestroy(FlutterDesktopViewControllerRef controller)
bool(* FlutterDesktopWindowProcCallback)(HWND, UINT, WPARAM, LPARAM, void *, LRESULT *result)
struct FlutterDesktopEngine * FlutterDesktopEngineRef
FLUTTER_EXPORT bool FlutterDesktopEngineRun(FlutterDesktopEngineRef engine, const char *entry_point)
FLUTTER_EXPORT bool FlutterDesktopEngineIsPlatformThread(FlutterDesktopEngineRef engine)
FlutterDesktopUIThreadPolicy
@ RunOnPlatformThread
@ RunOnSeparateThread
@ Default
FLUTTER_EXPORT bool FlutterDesktopEngineProcessExternalWindowMessage(FlutterDesktopEngineRef engine, HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam, LRESULT *result)
int64_t FlutterDesktopViewId
FLUTTER_EXPORT HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view)
FLUTTER_EXPORT void FlutterDesktopViewControllerForceRedraw(FlutterDesktopViewControllerRef controller)
FLUTTER_EXPORT FlutterDesktopMessengerRef FlutterDesktopEngineGetMessenger(FlutterDesktopEngineRef engine)
struct FlutterDesktopView * FlutterDesktopViewRef
FlutterDesktopGpuPreference
@ LowPowerPreference
@ HighPerformancePreference
@ NoPreference
FLUTTER_EXPORT void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopWindowProcCallback delegate, void *user_data)
FLUTTER_EXPORT IDXGIAdapter * FlutterDesktopViewGetGraphicsAdapter(FlutterDesktopViewRef view)
void(* VoidCallback)(void *)
FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetViewById(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopViewId view_id)
FLUTTER_EXPORT bool FlutterDesktopEngineDestroy(FlutterDesktopEngineRef engine)
FLUTTER_EXPORT FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine(FlutterDesktopViewControllerRef controller)
FLUTTER_EXPORT UINT FlutterDesktopGetDpiForHWND(HWND hwnd)
FLUTTER_EXPORT void FlutterDesktopEnginePostPlatformThreadTask(FlutterDesktopEngineRef engine, VoidCallback callback, VoidCallback on_cancel, void *user_data)
FLUTTER_EXPORT FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar(FlutterDesktopEngineRef engine, const char *plugin_name)
FlutterDesktopImpellerSwitch
@ DefaultImpeller
@ EnabledImpeller
@ DisabledImpeller
FlutterDesktopAccessibilityMode
@ IAccessibleMode
@ IAccessibleExMode
@ DefaultAccessibilityMode
FLUTTER_EXPORT bool FlutterDesktopEngineGetGraphicsAdapter(FlutterDesktopEngineRef engine, IDXGIAdapter **adapter_out)
FLUTTER_EXPORT void FlutterDesktopPluginRegistrarUnregisterTopLevelWindowProcDelegate(FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopWindowProcCallback delegate)
FLUTTER_EXPORT UINT FlutterDesktopGetDpiForMonitor(HMONITOR monitor)
FLUTTER_EXPORT void FlutterDesktopEngineReloadSystemFonts(FlutterDesktopEngineRef engine)
FLUTTER_EXPORT FlutterDesktopViewId FlutterDesktopViewControllerGetViewId(FlutterDesktopViewControllerRef view_controller)
FLUTTER_EXPORT FlutterDesktopEngineRef FlutterDesktopEngineCreate(const FlutterDesktopEngineProperties *engine_properties)
FLUTTER_EXPORT FlutterDesktopViewControllerRef FlutterDesktopViewControllerCreate(int width, int height, FlutterDesktopEngineRef engine)
FLUTTER_EXPORT bool FlutterDesktopViewControllerHandleTopLevelWindowProc(FlutterDesktopViewControllerRef controller, HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam, LRESULT *result)
FLUTTER_EXPORT void FlutterDesktopEngineSetNextFrameCallback(FlutterDesktopEngineRef engine, VoidCallback callback, void *user_data)
FLUTTER_EXPORT uint64_t FlutterDesktopEngineProcessMessages(FlutterDesktopEngineRef engine)
FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(FlutterDesktopPluginRegistrarRef registrar)
FlutterDesktopBinaryReply callback
Win32Message message
FlutterDesktopAccessibilityMode accessibility_mode
FlutterDesktopImpellerSwitch impeller_switch
FlutterDesktopUIThreadPolicy ui_thread_policy
FlutterDesktopGpuPreference gpu_preference