Flutter Engine
The Flutter Engine
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 <memory>
9#include <string>
10#include <utility>
11
12#include "flutter/common/settings.h"
13#include "flutter/common/task_runners.h"
14#include "flutter/fml/build_config.h"
15#include "flutter/fml/memory/weak_ptr.h"
16#include "flutter/fml/synchronization/waitable_event.h"
17#include "flutter/lib/ui/io_manager.h"
18#include "flutter/lib/ui/isolate_name_server/isolate_name_server.h"
19#include "flutter/lib/ui/painting/image_decoder.h"
20#include "flutter/lib/ui/snapshot_delegate.h"
21#include "flutter/lib/ui/volatile_path_tracker.h"
22#include "flutter/shell/common/platform_message_handler.h"
24#include "third_party/dart/runtime/include/dart_api.h"
29
30namespace flutter {
31class FontSelector;
32class ImageGeneratorRegistry;
33class PlatformConfiguration;
34class PlatformMessage;
35
37 public:
38 static UIDartState* Current();
39
40 /// @brief The subset of state which is owned by the shell or engine
41 /// and passed through the RuntimeController into DartIsolates.
42 /// If a shell-owned resource needs to be exposed to the framework via
43 /// UIDartState, a pointer to the resource can be added to this
44 /// struct with appropriate default construction.
45 struct Context {
46 explicit Context(const TaskRunners& task_runners);
47
54 std::string advisory_script_uri,
56 std::shared_ptr<VolatilePathTracker> volatile_path_tracker,
57 std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner,
58 bool enable_impeller,
60
61 /// The task runners used by the shell hosting this runtime controller. This
62 /// may be used by the isolate to scheduled asynchronous texture uploads or
63 /// post tasks to the platform task runner.
65
66 /// The snapshot delegate used by the
67 /// isolate to gather raster snapshots
68 /// of Flutter view hierarchies.
70
71 /// The IO manager used by the isolate for asynchronous texture uploads.
73
74 /// The unref queue used by the isolate to collect resources that may
75 /// reference resources on the GPU.
77
78 /// The image decoder.
80
81 /// Cascading registry of image generator builders. Given compressed image
82 /// bytes as input, this is used to find and create image generators, which
83 /// can then be used for image decoding.
85
86 /// The advisory script URI (only used for debugging). This does not affect
87 /// the code being run in the isolate in any way.
89
90 /// The advisory script entrypoint (only used for debugging). This does not
91 /// affect the code being run in the isolate in any way. The isolate must be
92 /// transitioned to the running state explicitly by the caller.
94
95 /// Cache for tracking path volatility.
96 std::shared_ptr<VolatilePathTracker> volatile_path_tracker;
97
98 /// The task runner whose tasks may be executed concurrently on a pool
99 /// of shared worker threads.
100 std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner;
101
102 /// Whether Impeller is enabled or not.
103 bool enable_impeller = false;
104
105 /// The expected backend for runtime stage shaders.
107 };
108
109 Dart_Port main_port() const { return main_port_; }
110 // Root isolate of the VM application
111 bool IsRootIsolate() const { return is_root_isolate_; }
112 static void ThrowIfUIOperationsProhibited();
113
114 void SetDebugName(const std::string& name);
115
116 const std::string& debug_name() const { return debug_name_; }
117
118 const std::string& logger_prefix() const { return logger_prefix_; }
119
121 return platform_configuration_.get();
122 }
123
124 void SetPlatformMessageHandler(std::weak_ptr<PlatformMessageHandler> handler);
125
126 Dart_Handle HandlePlatformMessage(std::unique_ptr<PlatformMessage> message);
127
128 const TaskRunners& GetTaskRunners() const;
129
130 void ScheduleMicrotask(Dart_Handle handle);
131
132 void FlushMicrotasksNow();
133
135
137
138 std::shared_ptr<VolatilePathTracker> GetVolatilePathTracker() const;
139
140 std::shared_ptr<fml::ConcurrentTaskRunner> GetConcurrentTaskRunner() const;
141
143
145
147
148 std::shared_ptr<IsolateNameServer> GetIsolateNameServer() const;
149
151
152 // Logs `print` messages from the application via an embedder-specified
153 // logging mechanism.
154 //
155 // @param[in] tag A component name or tag that identifies the logging
156 // application.
157 // @param[in] message The message to be logged.
158 void LogMessage(const std::string& tag, const std::string& message) const;
159
161 return unhandled_exception_callback_;
162 }
163
164 /// Returns a enumeration that uniquely represents this root isolate.
165 /// Returns `0` if called from a non-root isolate.
166 int64_t GetRootIsolateToken() const;
167
168 /// Whether Impeller is enabled for this application.
169 bool IsImpellerEnabled() const;
170
171 /// The expected type for runtime stage shaders.
173
175 char** error);
176
177 protected:
178 UIDartState(TaskObserverAdd add_callback,
179 TaskObserverRemove remove_callback,
180 std::string logger_prefix,
182 LogMessageCallback log_message_callback,
183 std::shared_ptr<IsolateNameServer> isolate_name_server,
184 bool is_root_isolate_,
185 const UIDartState::Context& context);
186
187 ~UIDartState() override;
188
190 std::unique_ptr<PlatformConfiguration> platform_configuration);
191
192 const std::string& GetAdvisoryScriptURI() const;
193
194 private:
195 void DidSetIsolate() override;
196
197 const TaskObserverAdd add_callback_;
198 const TaskObserverRemove remove_callback_;
199 const std::string logger_prefix_;
200 Dart_Port main_port_ = ILLEGAL_PORT;
201 const bool is_root_isolate_;
202 std::string debug_name_;
203 std::unique_ptr<PlatformConfiguration> platform_configuration_;
204 std::weak_ptr<PlatformMessageHandler> platform_message_handler_;
205 tonic::DartMicrotaskQueue microtask_queue_;
206 UnhandledExceptionCallback unhandled_exception_callback_;
207 LogMessageCallback log_message_callback_;
208 const std::shared_ptr<IsolateNameServer> isolate_name_server_;
209 UIDartState::Context context_;
210
211 void AddOrRemoveTaskObserver(bool add);
212};
213
214} // namespace flutter
215
216#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
Dart_Port main_port() const
PlatformConfiguration * platform_configuration() const
fml::WeakPtr< ImageDecoder > GetImageDecoder() const
UnhandledExceptionCallback unhandled_exception_callback() const
bool IsRootIsolate() const
void SetPlatformConfiguration(std::unique_ptr< PlatformConfiguration > platform_configuration)
std::shared_ptr< VolatilePathTracker > GetVolatilePathTracker() const
const std::string & logger_prefix() const
tonic::DartErrorHandleType GetLastError()
fml::WeakPtr< IOManager > GetIOManager() const
const TaskRunners & GetTaskRunners() const
fml::WeakPtr< ImageGeneratorRegistry > GetImageGeneratorRegistry() const
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 expected type for runtime stage 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)
UIDartState(TaskObserverAdd add_callback, TaskObserverRemove remove_callback, std::string logger_prefix, UnhandledExceptionCallback unhandled_exception_callback, LogMessageCallback log_message_callback, std::shared_ptr< IsolateNameServer > isolate_name_server, bool is_root_isolate_, const UIDartState::Context &context)
const std::string & debug_name() const
#define ILLEGAL_PORT
Definition: dart_api.h:1535
int64_t Dart_Port
Definition: dart_api.h:1525
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
struct _Dart_Isolate * Dart_Isolate
Definition: dart_api.h:88
const uint8_t uint32_t uint32_t GError ** error
Win32Message message
std::function< void(intptr_t)> TaskObserverRemove
Definition: settings.h:83
std::function< void(intptr_t, fml::closure)> TaskObserverAdd
Definition: settings.h:82
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
std::function< bool(const std::string &, const std::string &)> UnhandledExceptionCallback
Definition: settings.h:86
std::function< void(const std::string &, const std::string &)> LogMessageCallback
Definition: settings.h:89
DartErrorHandleType
Definition: dart_error.h:67
The subset of state which is owned by the shell or engine and passed through the RuntimeController in...
Definition: ui_dart_state.h:45
fml::RefPtr< SkiaUnrefQueue > unref_queue
Definition: ui_dart_state.h:76
fml::WeakPtr< ImageGeneratorRegistry > image_generator_registry
Definition: ui_dart_state.h:84
fml::WeakPtr< IOManager > io_manager
The IO manager used by the isolate for asynchronous texture uploads.
Definition: ui_dart_state.h:72
Context(const TaskRunners &task_runners)
const TaskRunners task_runners
Definition: ui_dart_state.h:64
std::shared_ptr< VolatilePathTracker > volatile_path_tracker
Cache for tracking path volatility.
Definition: ui_dart_state.h:96
fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > snapshot_delegate
Definition: ui_dart_state.h:69
bool enable_impeller
Whether Impeller is enabled or not.
impeller::RuntimeStageBackend runtime_stage_backend
The expected backend for runtime stage shaders.
fml::WeakPtr< ImageDecoder > image_decoder
The image decoder.
Definition: ui_dart_state.h:79
std::shared_ptr< fml::ConcurrentTaskRunner > concurrent_task_runner
std::string advisory_script_entrypoint
Definition: ui_dart_state.h:93