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 // A set of Flutter and Dart assets used to initialize a Flutter engine.
50 class DartProject {
51  public:
52  // Creates a DartProject from a series of absolute paths.
53  // The three paths are:
54  // - assets_path: Path to the assets directory as built by the Flutter tool.
55  // - icu_data_path: Path to the icudtl.dat file.
56  // - aot_library_path: Path to the AOT snapshot file.
57  //
58  // The paths may either be absolute or relative to the directory containing
59  // the running executable.
60  explicit DartProject(const std::wstring& assets_path,
61  const std::wstring& icu_data_path,
62  const std::wstring& aot_library_path) {
63  assets_path_ = assets_path;
64  icu_data_path_ = icu_data_path;
65  aot_library_path_ = aot_library_path;
66  }
67 
68  // Creates a DartProject from a directory path. The directory should contain
69  // the following top-level items:
70  // - icudtl.dat (provided as a resource by the Flutter tool)
71  // - flutter_assets (as built by the Flutter tool)
72  // - app.so, for an AOT build (as built by the Flutter tool)
73  //
74  // The path can either be absolute, or relative to the directory containing
75  // the running executable.
76  explicit DartProject(const std::wstring& path) {
77  assets_path_ = path + L"\\flutter_assets";
78  icu_data_path_ = path + L"\\icudtl.dat";
79  aot_library_path_ = path + L"\\app.so";
80  }
81 
82  ~DartProject() = default;
83 
84  // Sets the Dart entrypoint to the specified value.
85  //
86  // If not set, the default entrypoint (main) is used. Custom Dart entrypoints
87  // must be decorated with `@pragma('vm:entry-point')`.
88  void set_dart_entrypoint(const std::string& entrypoint) {
89  if (entrypoint.empty()) {
90  return;
91  }
92  dart_entrypoint_ = entrypoint;
93  }
94 
95  // Returns the Dart entrypoint.
96  const std::string& dart_entrypoint() const { return dart_entrypoint_; }
97 
98  // Sets the command line arguments that should be passed to the Dart
99  // entrypoint.
100  void set_dart_entrypoint_arguments(std::vector<std::string> arguments) {
101  dart_entrypoint_arguments_ = std::move(arguments);
102  }
103 
104  // Returns any command line arguments that should be passed to the Dart
105  // entrypoint.
106  const std::vector<std::string>& dart_entrypoint_arguments() const {
107  return dart_entrypoint_arguments_;
108  }
109 
110  // Sets the GPU usage preference for flutter engine.
112  gpu_preference_ = gpu_preference;
113  }
114 
115  // Returns the project's GPU preference.
116  // Defaults to NoPreference.
117  GpuPreference gpu_preference() const { return gpu_preference_; }
118 
119  // Sets the thread policy for UI isolate.
121  ui_thread_policy_ = policy;
122  }
123 
124  // Returns the policy for UI isolate.
125  // Defaults to UIThreadPolicy::Default.
126  UIThreadPolicy ui_thread_policy() const { return ui_thread_policy_; }
127 
128  // Sets the accessibility implementation used by Flutter.
130  accessibility_mode_ = accessibility_mode;
131  }
132 
133  // Returns the accessibility implementation used by Flutter.
134  // Defaults to AccessibilityMode::Defaults.
135  AccessibilityMode accessibility_mode() const { return accessibility_mode_; }
136 
137  private:
138  // Accessors for internals are private, so that they can be changed if more
139  // flexible options for project structures are needed later without it
140  // being a breaking change. Provide access to internal classes that need
141  // them.
142  friend class FlutterEngine;
143  friend class FlutterViewController;
144  friend class DartProjectTest;
145 
146  const std::wstring& assets_path() const { return assets_path_; }
147  const std::wstring& icu_data_path() const { return icu_data_path_; }
148  const std::wstring& aot_library_path() const { return aot_library_path_; }
149 
150  // The path to the assets directory.
151  std::wstring assets_path_;
152  // The path to the ICU data.
153  std::wstring icu_data_path_;
154  // The path to the AOT library. This will always return a path, but non-AOT
155  // builds will not be expected to actually have a library at that path.
156  std::wstring aot_library_path_;
157  // The Dart entrypoint to launch.
158  std::string dart_entrypoint_;
159  // The list of arguments to pass through to the Dart entrypoint.
160  std::vector<std::string> dart_entrypoint_arguments_;
161  // The preference for GPU to be used by flutter engine.
162  GpuPreference gpu_preference_ = GpuPreference::NoPreference;
163  // Thread policy for UI isolate.
164  UIThreadPolicy ui_thread_policy_ = UIThreadPolicy::Default;
165  // The accessibility implementation used by Flutter.
166  AccessibilityMode accessibility_mode_ = AccessibilityMode::Default;
167 };
168 
169 } // namespace flutter
170 
171 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_CLIENT_WRAPPER_INCLUDE_FLUTTER_DART_PROJECT_H_
void set_dart_entrypoint(const std::string &entrypoint)
Definition: dart_project.h:88
void set_ui_thread_policy(UIThreadPolicy policy)
Definition: dart_project.h:120
void set_gpu_preference(GpuPreference gpu_preference)
Definition: dart_project.h:111
DartProject(const std::wstring &assets_path, const std::wstring &icu_data_path, const std::wstring &aot_library_path)
Definition: dart_project.h:60
DartProject(const std::wstring &path)
Definition: dart_project.h:76
AccessibilityMode accessibility_mode() const
Definition: dart_project.h:135
void set_dart_entrypoint_arguments(std::vector< std::string > arguments)
Definition: dart_project.h:100
UIThreadPolicy ui_thread_policy() const
Definition: dart_project.h:126
const std::string & dart_entrypoint() const
Definition: dart_project.h:96
const std::vector< std::string > & dart_entrypoint_arguments() const
Definition: dart_project.h:106
void set_accessibility_mode(AccessibilityMode accessibility_mode)
Definition: dart_project.h:129
GpuPreference gpu_preference() const
Definition: dart_project.h:117
AccessibilityMode
Definition: dart_project.h:39