Flutter Windows Embedder
dart_project.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_DART_PROJECT_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_CLIENT_WRAPPER_INCLUDE_FLUTTER_DART_PROJECT_H_
7 
8 #include <string>
9 #include <vector>
10 
11 namespace flutter {
12 
13 // Configures how the Flutter engine selects a GPU.
14 enum class GpuPreference {
15  // No preference.
17  // Prefer energy efficiency over performance, such as an integrated GPU.
18  // This falls back to a high performance GPU if no low power GPU is
19  // available.
21  // Prefer performance over energy efficiency, such as a discrete GPU or
22  // dedicated GPU.
23  // This falls back to a low power GPU if no high performance GPU is available.
25 };
26 
27 // Configures the thread policy for running the UI isolate.
28 enum class UIThreadPolicy {
29  // Default value. Currently will run the UI isolate on separate thread,
30  // later will be changed to running the UI isolate on platform thread.
31  Default,
32  // Run the UI isolate on platform thread.
34  // Run the UI isolate on a separate thread.
36 };
37 
38 // Configures the accessibility implementation used by Flutter.
39 enum class AccessibilityMode {
40  // Default value. Flutter will automatically select the best available
41  // implementation.
42  Default,
43  // Use the IAccessible implementation.
45  // Use the experimental IAccessibleEx implementation.
47 };
48 
49 // Configures the Impeller enablement switch.
50 enum class ImpellerSwitch {
51  // Use the default Impeller enablement behavior.
52  Default,
53  // Enable Impeller.
54  Enabled,
55  // Disable Impeller.
56  Disabled,
57 };
58 
59 // A set of Flutter and Dart assets used to initialize a Flutter engine.
60 class DartProject {
61  public:
62  // Creates a DartProject from a series of absolute paths.
63  // The three paths are:
64  // - assets_path: Path to the assets directory as built by the Flutter tool.
65  // - icu_data_path: Path to the icudtl.dat file.
66  // - aot_library_path: Path to the AOT snapshot file.
67  //
68  // The paths may either be absolute or relative to the directory containing
69  // the running executable.
70  explicit DartProject(const std::wstring& assets_path,
71  const std::wstring& icu_data_path,
72  const std::wstring& aot_library_path) {
73  assets_path_ = assets_path;
74  icu_data_path_ = icu_data_path;
75  aot_library_path_ = aot_library_path;
76  }
77 
78  // Creates a DartProject from a directory path. The directory should contain
79  // the following top-level items:
80  // - icudtl.dat (provided as a resource by the Flutter tool)
81  // - flutter_assets (as built by the Flutter tool)
82  // - app.so, for an AOT build (as built by the Flutter tool)
83  //
84  // The path can either be absolute, or relative to the directory containing
85  // the running executable.
86  explicit DartProject(const std::wstring& path) {
87  assets_path_ = path + L"\\flutter_assets";
88  icu_data_path_ = path + L"\\icudtl.dat";
89  aot_library_path_ = path + L"\\app.so";
90  }
91 
92  ~DartProject() = default;
93 
94  // Sets the Dart entrypoint to the specified value.
95  //
96  // If not set, the default entrypoint (main) is used. Custom Dart entrypoints
97  // must be decorated with `@pragma('vm:entry-point')`.
98  void set_dart_entrypoint(const std::string& entrypoint) {
99  if (entrypoint.empty()) {
100  return;
101  }
102  dart_entrypoint_ = entrypoint;
103  }
104 
105  // Returns the Dart entrypoint.
106  const std::string& dart_entrypoint() const { return dart_entrypoint_; }
107 
108  // Sets the command line arguments that should be passed to the Dart
109  // entrypoint.
110  void set_dart_entrypoint_arguments(std::vector<std::string> arguments) {
111  dart_entrypoint_arguments_ = std::move(arguments);
112  }
113 
114  // Returns any command line arguments that should be passed to the Dart
115  // entrypoint.
116  const std::vector<std::string>& dart_entrypoint_arguments() const {
117  return dart_entrypoint_arguments_;
118  }
119 
120  // Sets the GPU usage preference for flutter engine.
122  gpu_preference_ = gpu_preference;
123  }
124 
125  // Returns the project's GPU preference.
126  // Defaults to NoPreference.
127  GpuPreference gpu_preference() const { return gpu_preference_; }
128 
129  // Sets the thread policy for UI isolate.
131  ui_thread_policy_ = policy;
132  }
133 
134  // Returns the policy for UI isolate.
135  // Defaults to UIThreadPolicy::Default.
136  UIThreadPolicy ui_thread_policy() const { return ui_thread_policy_; }
137 
138  // Sets the accessibility implementation used by Flutter.
140  accessibility_mode_ = accessibility_mode;
141  }
142 
143  // Returns the accessibility implementation used by Flutter.
144  // Defaults to AccessibilityMode::Defaults.
145  AccessibilityMode accessibility_mode() const { return accessibility_mode_; }
146 
147  // Sets the Impeller enablement switch.
149  impeller_switch_ = impeller_switch;
150  }
151 
152  // Returns the Impeller enablement switch.
153  // Defaults to ImpellerSwitch::Default.
154  ImpellerSwitch impeller_switch() const { return impeller_switch_; }
155 
156  // Sets whether the Flutter GPU API (package:flutter_gpu) is enabled.
157  // Flutter GPU requires the Impeller renderer.
159  enable_flutter_gpu_ = enable_flutter_gpu;
160  }
161 
162  // Returns whether the Flutter GPU API is enabled.
163  // Defaults to false.
164  bool enable_flutter_gpu() const { return enable_flutter_gpu_; }
165 
166  private:
167  // Accessors for internals are private, so that they can be changed if more
168  // flexible options for project structures are needed later without it
169  // being a breaking change. Provide access to internal classes that need
170  // them.
171  friend class FlutterEngine;
172  friend class FlutterViewController;
173  friend class DartProjectTest;
174 
175  const std::wstring& assets_path() const { return assets_path_; }
176  const std::wstring& icu_data_path() const { return icu_data_path_; }
177  const std::wstring& aot_library_path() const { return aot_library_path_; }
178 
179  // The path to the assets directory.
180  std::wstring assets_path_;
181  // The path to the ICU data.
182  std::wstring icu_data_path_;
183  // The path to the AOT library. This will always return a path, but non-AOT
184  // builds will not be expected to actually have a library at that path.
185  std::wstring aot_library_path_;
186  // The Dart entrypoint to launch.
187  std::string dart_entrypoint_;
188  // The list of arguments to pass through to the Dart entrypoint.
189  std::vector<std::string> dart_entrypoint_arguments_;
190  // The preference for GPU to be used by flutter engine.
191  GpuPreference gpu_preference_ = GpuPreference::NoPreference;
192  // Thread policy for UI isolate.
193  UIThreadPolicy ui_thread_policy_ = UIThreadPolicy::Default;
194  // The accessibility implementation used by Flutter.
195  AccessibilityMode accessibility_mode_ = AccessibilityMode::Default;
196  // The Impeller enablement switch.
197  ImpellerSwitch impeller_switch_ = ImpellerSwitch::Default;
198  // Whether the Flutter GPU API is enabled.
199  bool enable_flutter_gpu_ = false;
200 };
201 
202 } // namespace flutter
203 
204 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_CLIENT_WRAPPER_INCLUDE_FLUTTER_DART_PROJECT_H_
void set_dart_entrypoint(const std::string &entrypoint)
Definition: dart_project.h:98
void set_enable_flutter_gpu(bool enable_flutter_gpu)
Definition: dart_project.h:158
bool enable_flutter_gpu() const
Definition: dart_project.h:164
void set_ui_thread_policy(UIThreadPolicy policy)
Definition: dart_project.h:130
void set_gpu_preference(GpuPreference gpu_preference)
Definition: dart_project.h:121
DartProject(const std::wstring &assets_path, const std::wstring &icu_data_path, const std::wstring &aot_library_path)
Definition: dart_project.h:70
DartProject(const std::wstring &path)
Definition: dart_project.h:86
AccessibilityMode accessibility_mode() const
Definition: dart_project.h:145
void set_dart_entrypoint_arguments(std::vector< std::string > arguments)
Definition: dart_project.h:110
UIThreadPolicy ui_thread_policy() const
Definition: dart_project.h:136
const std::string & dart_entrypoint() const
Definition: dart_project.h:106
void set_impeller_switch(ImpellerSwitch impeller_switch)
Definition: dart_project.h:148
const std::vector< std::string > & dart_entrypoint_arguments() const
Definition: dart_project.h:116
void set_accessibility_mode(AccessibilityMode accessibility_mode)
Definition: dart_project.h:139
GpuPreference gpu_preference() const
Definition: dart_project.h:127
ImpellerSwitch impeller_switch() const
Definition: dart_project.h:154
AccessibilityMode
Definition: dart_project.h:39