Flutter Engine
 
Loading...
Searching...
No Matches
ui_dart_state.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_LIB_UI_UI_DART_STATE_H_
6#define FLUTTER_LIB_UI_UI_DART_STATE_H_
7
8#include <future>
9#include <memory>
10#include <string>
11#include <utility>
12
25#include "third_party/dart/runtime/include/dart_api.h"
26#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
30
31namespace flutter {
32class FontSelector;
33class ImageGeneratorRegistry;
34class PlatformConfiguration;
35class PlatformMessage;
36
38 public:
39 static UIDartState* Current();
40
41 /// @brief The subset of state which is owned by the shell or engine
42 /// and passed through the RuntimeController into DartIsolates.
43 /// If a shell-owned resource needs to be exposed to the framework via
44 /// UIDartState, a pointer to the resource can be added to this
45 /// struct with appropriate default construction.
46 struct Context {
47 explicit Context(const TaskRunners& task_runners);
48
49 Context(
57 std::string advisory_script_uri,
60 std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner,
61 std::shared_future<impeller::RuntimeStageBackend> runtime_stage_backend,
62 bool enable_impeller,
64
65 /// The task runners used by the shell hosting this runtime controller. This
66 /// may be used by the isolate to scheduled asynchronous texture uploads or
67 /// post tasks to the platform task runner.
69
70 /// The snapshot delegate used by the
71 /// isolate to gather raster snapshots
72 /// of Flutter view hierarchies.
74
75 /// The IO manager used by the isolate for asynchronous texture uploads.
77
78 /// The unref queue used by the isolate to collect resources that may
79 /// reference resources on the GPU.
81
82 /// The image decoder.
84
85 /// Cascading registry of image generator builders. Given compressed image
86 /// bytes as input, this is used to find and create image generators, which
87 /// can then be used for image decoding.
90
91 /// The advisory script URI (only used for debugging). This does not affect
92 /// the code being run in the isolate in any way.
94
95 /// The advisory script entrypoint (only used for debugging). This does not
96 /// affect the code being run in the isolate in any way. The isolate must be
97 /// transitioned to the running state explicitly by the caller.
99
100 /// Whether deterministic rendering practices should be used.
102
103 /// The task runner whose tasks may be executed concurrently on a pool
104 /// of shared worker threads.
105 std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner;
106
107 /// The runtime stage backend for fragment shaders.
108 std::shared_future<impeller::RuntimeStageBackend> runtime_stage_backend;
109
110 /// Whether Impeller is enabled or not.
111 bool enable_impeller = false;
112
113 /// Whether flutter_gpu is enabled or not.
114 bool enable_flutter_gpu = false;
115 };
116
117 Dart_Port main_port() const { return main_port_; }
118 // Root isolate of the VM application
119 bool IsRootIsolate() const { return is_root_isolate_; }
120 static void ThrowIfUIOperationsProhibited();
121
122 void SetDebugName(const std::string& name);
123
124 const std::string& debug_name() const { return debug_name_; }
125
126 const std::string& logger_prefix() const { return logger_prefix_; }
127
129 return platform_configuration_.get();
130 }
131
132 void SetPlatformMessageHandler(std::weak_ptr<PlatformMessageHandler> handler);
133
134 Dart_Handle HandlePlatformMessage(std::unique_ptr<PlatformMessage> message);
135
136 const TaskRunners& GetTaskRunners() const;
137
138 void ScheduleMicrotask(Dart_Handle handle);
139
140 void FlushMicrotasksNow();
141
143
145
147
148 std::shared_ptr<fml::ConcurrentTaskRunner> GetConcurrentTaskRunner() const;
149
151
153
156
157 std::shared_ptr<IsolateNameServer> GetIsolateNameServer() const;
158
160
161 // Logs `print` messages from the application via an embedder-specified
162 // logging mechanism.
163 //
164 // @param[in] tag A component name or tag that identifies the logging
165 // application.
166 // @param[in] message The message to be logged.
167 void LogMessage(const std::string& tag, const std::string& message) const;
168
170 return unhandled_exception_callback_;
171 }
172
173 /// Returns a enumeration that uniquely represents this root isolate.
174 /// Returns `0` if called from a non-root isolate.
175 int64_t GetRootIsolateToken() const;
176
177 /// Whether deterministic rendering practices are enabled for this application
179
180 /// Whether Impeller is enabled for this application.
181 bool IsImpellerEnabled() const;
182
183 /// Whether Flutter GPU is enabled for this application.
184 bool IsFlutterGPUEnabled() const;
185
186 /// The runtime stage to use for fragment shaders.
188
189 virtual Dart_Isolate CreatePlatformIsolate(Dart_Handle entry_point,
190 char** error);
191
192 protected:
193 UIDartState(TaskObserverAdd add_callback,
194 TaskObserverRemove remove_callback,
195 std::string logger_prefix,
197 LogMessageCallback log_message_callback,
198 std::shared_ptr<IsolateNameServer> isolate_name_server,
199 bool is_root_isolate_,
200 const UIDartState::Context& context);
201
202 ~UIDartState() override;
203
205 std::unique_ptr<PlatformConfiguration> platform_configuration);
206
207 const std::string& GetAdvisoryScriptURI() const;
208
209 private:
210 void DidSetIsolate() override;
211
212 const TaskObserverAdd add_callback_;
213 const TaskObserverRemove remove_callback_;
214 std::optional<fml::TaskQueueId> callback_queue_id_;
215 const std::string logger_prefix_;
216 Dart_Port main_port_ = ILLEGAL_PORT;
217 const bool is_root_isolate_;
218 std::string debug_name_;
219 std::unique_ptr<PlatformConfiguration> platform_configuration_;
220 std::weak_ptr<PlatformMessageHandler> platform_message_handler_;
221 tonic::DartMicrotaskQueue microtask_queue_;
222 UnhandledExceptionCallback unhandled_exception_callback_;
223 LogMessageCallback log_message_callback_;
224 const std::shared_ptr<IsolateNameServer> isolate_name_server_;
225 UIDartState::Context context_;
226
227 void AddOrRemoveTaskObserver(bool add);
228};
229
230} // namespace flutter
231
232#endif // FLUTTER_LIB_UI_UI_DART_STATE_H_
A class for holding and distributing platform-level information to and from the Dart code in Flutter'...
fml::RefPtr< flutter::SkiaUnrefQueue > GetSkiaUnrefQueue() const
std::shared_ptr< fml::ConcurrentTaskRunner > GetConcurrentTaskRunner() const
fml::TaskRunnerAffineWeakPtr< ImageGeneratorRegistry > GetImageGeneratorRegistry() const
Dart_Port main_port() const
PlatformConfiguration * platform_configuration() const
fml::TaskRunnerAffineWeakPtr< ImageDecoder > GetImageDecoder() const
UnhandledExceptionCallback unhandled_exception_callback() const
bool IsRootIsolate() const
bool IsFlutterGPUEnabled() const
Whether Flutter GPU is enabled for this application.
void SetPlatformConfiguration(std::unique_ptr< PlatformConfiguration > platform_configuration)
const std::string & logger_prefix() const
tonic::DartErrorHandleType GetLastError()
fml::WeakPtr< IOManager > GetIOManager() const
const TaskRunners & GetTaskRunners() const
bool IsDeterministicRenderingEnabled() const
Whether deterministic rendering practices are enabled for this application.
virtual Dart_Isolate CreatePlatformIsolate(Dart_Handle entry_point, char **error)
bool IsImpellerEnabled() const
Whether Impeller is enabled for this application.
std::shared_ptr< IsolateNameServer > GetIsolateNameServer() const
void SetDebugName(const std::string &name)
int64_t GetRootIsolateToken() const
void ScheduleMicrotask(Dart_Handle handle)
fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > GetSnapshotDelegate() const
const std::string & GetAdvisoryScriptURI() const
impeller::RuntimeStageBackend GetRuntimeStageBackend() const
The runtime stage to use for fragment shaders.
void LogMessage(const std::string &tag, const std::string &message) const
void SetPlatformMessageHandler(std::weak_ptr< PlatformMessageHandler > handler)
static UIDartState * Current()
static void ThrowIfUIOperationsProhibited()
Dart_Handle HandlePlatformMessage(std::unique_ptr< PlatformMessage > message)
const std::string & debug_name() const
const gchar FlBinaryMessengerMessageHandler handler
G_BEGIN_DECLS GBytes * message
const uint8_t uint32_t uint32_t GError ** error
std::function< void(fml::TaskQueueId, intptr_t)> TaskObserverRemove
Definition settings.h:77
DEF_SWITCHES_START aot vmservice shared library name
Definition switch_defs.h:27
std::function< fml::TaskQueueId(intptr_t, fml::closure)> TaskObserverAdd
Definition settings.h:75
std::function< bool(const std::string &, const std::string &)> UnhandledExceptionCallback
Definition settings.h:80
std::function< void(const std::string &, const std::string &)> LogMessageCallback
Definition settings.h:83
DartErrorHandleType
Definition dart_error.h:67
The subset of state which is owned by the shell or engine and passed through the RuntimeController in...
fml::RefPtr< SkiaUnrefQueue > unref_queue
bool enable_flutter_gpu
Whether flutter_gpu is enabled or not.
fml::WeakPtr< IOManager > io_manager
The IO manager used by the isolate for asynchronous texture uploads.
std::shared_future< impeller::RuntimeStageBackend > runtime_stage_backend
The runtime stage backend for fragment shaders.
const TaskRunners task_runners
fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > snapshot_delegate
bool enable_impeller
Whether Impeller is enabled or not.
fml::TaskRunnerAffineWeakPtr< ImageDecoder > image_decoder
The image decoder.
std::shared_ptr< fml::ConcurrentTaskRunner > concurrent_task_runner
bool deterministic_rendering_enabled
Whether deterministic rendering practices should be used.
fml::TaskRunnerAffineWeakPtr< ImageGeneratorRegistry > image_generator_registry