Flutter Windows Embedder
flutter_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_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_ENGINE_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_ENGINE_H_
7 
8 #include <flutter_windows.h>
9 
10 #include <chrono>
11 #include <memory>
12 #include <optional>
13 #include <string>
14 
15 #include "binary_messenger.h"
16 #include "dart_project.h"
17 #include "plugin_registrar.h"
18 #include "plugin_registry.h"
19 
20 namespace flutter {
21 
22 // An instance of a Flutter engine.
23 //
24 // In the future, this will be the API surface used for all interactions with
25 // the engine, rather than having them duplicated on FlutterViewController.
26 // For now it is only used in the rare case where you need a headless Flutter
27 // engine.
28 class FlutterEngine : public PluginRegistry {
29  public:
30  // Creates a new engine for running the given project.
31  explicit FlutterEngine(const DartProject& project);
32 
33  virtual ~FlutterEngine();
34 
35  // Prevent copying.
36  FlutterEngine(FlutterEngine const&) = delete;
38 
39  // Starts running the engine at the entrypoint function specified in the
40  // DartProject used to configure the engine, or main() by default.
41  bool Run();
42 
43  // Starts running the engine, with an optional entry point.
44  //
45  // If provided, entry_point must be the name of a top-level function from the
46  // same Dart library that contains the app's main() function, and must be
47  // decorated with `@pragma(vm:entry-point)` to ensure the method is not
48  // tree-shaken by the Dart compiler. If not provided, defaults to main().
49  bool Run(const char* entry_point);
50 
51  // Terminates the running engine.
52  void ShutDown();
53 
54  // Processes any pending events in the Flutter engine, and returns the
55  // nanosecond delay until the next scheduled event (or max, if none).
56  //
57  // This should be called on every run of the application-level runloop, and
58  // a wait for native events in the runloop should never be longer than the
59  // last return value from this function.
60  std::chrono::nanoseconds ProcessMessages();
61 
62  // Tells the engine that the system font list has changed. Should be called
63  // by clients when OS-level font changes happen (e.g., WM_FONTCHANGE in a
64  // Win32 application).
65  void ReloadSystemFonts();
66 
67  // Tells the engine that the platform brightness value has changed. Should be
68  // called by clients when OS-level theme changes happen (e.g.,
69  // WM_DWMCOLORIZATIONCOLORCHANGED in a Win32 application).
71 
72  // flutter::PluginRegistry:
74  const std::string& plugin_name) override;
75 
76  // Returns the messenger to use for creating channels to communicate with the
77  // Flutter engine.
78  //
79  // This pointer will remain valid for the lifetime of this instance.
80  BinaryMessenger* messenger() { return messenger_.get(); }
81 
82  // Schedule a callback to be called after the next frame is drawn.
83  //
84  // This must be called from the platform thread. The callback is executed only
85  // once on the platform thread.
86  void SetNextFrameCallback(std::function<void()> callback);
87 
88  // Returns true if the current thread is the platform thread.
89  bool IsPlatformThread() const;
90 
91  // Schedule a callback to be called on the platform thread.
92  //
93  // This can be called on any thread. The callback is executed only
94  // once on the platform thread.
95  void PostPlatformThreadTask(std::function<void()> callback);
96 
97  // Called to pass an external window message to the engine for lifecycle
98  // state updates. Non-Flutter windows must call this method in their WndProc
99  // in order to be included in the logic for application lifecycle state
100  // updates. Returns a result if the message should be consumed.
101  std::optional<LRESULT> ProcessExternalWindowMessage(HWND hwnd,
102  UINT message,
103  WPARAM wparam,
104  LPARAM lparam);
105 
106  // Retrieves the DXGI adapter used for rendering. Returns true if the adapter
107  // was successfully retrieved, or false if an error occured.
108  // The caller must provide a valid pointer to an IDXGIAdapter* and is
109  // responsible for releasing the adapter.
110  bool GetGraphicsAdapter(IDXGIAdapter** adapter_out) {
111  return FlutterDesktopEngineGetGraphicsAdapter(engine_, adapter_out);
112  }
113 
114  private:
115  // For access to the engine handle.
116  friend class FlutterViewController;
117 
118  // Gives up ownership of |engine_|, but keeps a weak reference to it.
119  //
120  // This is intended to be used by FlutterViewController, since the underlying
121  // C API for view controllers takes over engine ownership.
122  FlutterDesktopEngineRef RelinquishEngine();
123 
124  // Handle for interacting with the C API's engine reference.
125  FlutterDesktopEngineRef engine_ = nullptr;
126 
127  // Messenger for communicating with the engine.
128  std::unique_ptr<BinaryMessenger> messenger_;
129 
130  // Whether or not this wrapper owns |engine_|.
131  bool owns_engine_ = true;
132 
133  // Whether |Run| has been called successfully.
134  //
135  // This is used to improve error messages. This can be false while the engine
136  // is running if the engine was started by creating a view.
137  bool run_succeeded_ = false;
138 
139  // The callback to execute once the next frame is drawn.
140  std::function<void()> next_frame_callback_ = nullptr;
141 };
142 
143 } // namespace flutter
144 
145 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_ENGINE_H_
std::optional< LRESULT > ProcessExternalWindowMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
FlutterEngine(FlutterEngine const &)=delete
std::chrono::nanoseconds ProcessMessages()
FlutterDesktopPluginRegistrarRef GetRegistrarForPlugin(const std::string &plugin_name) override
void PostPlatformThreadTask(std::function< void()> callback)
bool IsPlatformThread() const
BinaryMessenger * messenger()
bool GetGraphicsAdapter(IDXGIAdapter **adapter_out)
FlutterEngine(const DartProject &project)
void SetNextFrameCallback(std::function< void()> callback)
FlutterEngine & operator=(FlutterEngine const &)=delete
bool FlutterDesktopEngineGetGraphicsAdapter(FlutterDesktopEngineRef engine, IDXGIAdapter **adapter_out)
struct FlutterDesktopEngine * FlutterDesktopEngineRef
FlutterDesktopBinaryReply callback
Win32Message message