Flutter Engine
 
Loading...
Searching...
No Matches
ui_dart_state.cc
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
6
7#include <utility>
8
14
15using tonic::ToDart;
16
17namespace flutter {
18
20 : task_runners(task_runners) {}
21
23 const TaskRunners& task_runners,
25 fml::WeakPtr<IOManager> io_manager,
29 image_generator_registry,
30 std::string advisory_script_uri,
31 std::string advisory_script_entrypoint,
32 bool deterministic_rendering_enabled,
33 std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner,
34 std::shared_future<impeller::RuntimeStageBackend> runtime_stage_backend,
35 bool enable_impeller,
36 bool enable_flutter_gpu)
37 : task_runners(task_runners),
38 snapshot_delegate(std::move(snapshot_delegate)),
39 io_manager(std::move(io_manager)),
40 unref_queue(std::move(unref_queue)),
41 image_decoder(std::move(image_decoder)),
42 image_generator_registry(std::move(image_generator_registry)),
43 advisory_script_uri(std::move(advisory_script_uri)),
44 advisory_script_entrypoint(std::move(advisory_script_entrypoint)),
45 deterministic_rendering_enabled(deterministic_rendering_enabled),
46 concurrent_task_runner(std::move(concurrent_task_runner)),
47 runtime_stage_backend(std::move(runtime_stage_backend)),
48 enable_impeller(enable_impeller),
49 enable_flutter_gpu(enable_flutter_gpu) {}
50
52 TaskObserverAdd add_callback,
53 TaskObserverRemove remove_callback,
54 std::string logger_prefix,
56 LogMessageCallback log_message_callback,
57 std::shared_ptr<IsolateNameServer> isolate_name_server,
58 bool is_root_isolate,
59 const UIDartState::Context& context)
60 : add_callback_(std::move(add_callback)),
61 remove_callback_(std::move(remove_callback)),
62 logger_prefix_(std::move(logger_prefix)),
63 is_root_isolate_(is_root_isolate),
64 unhandled_exception_callback_(std::move(unhandled_exception_callback)),
65 log_message_callback_(std::move(log_message_callback)),
66 isolate_name_server_(std::move(isolate_name_server)),
67 context_(context) {
68 AddOrRemoveTaskObserver(true /* add */);
69}
70
72 AddOrRemoveTaskObserver(false /* remove */);
73}
74
75const std::string& UIDartState::GetAdvisoryScriptURI() const {
76 return context_.advisory_script_uri;
77}
78
82
84 return context_.enable_impeller;
85}
86
88 return context_.enable_impeller && context_.enable_flutter_gpu;
89}
90
91void UIDartState::DidSetIsolate() {
92 main_port_ = Dart_GetMainPortId();
93 std::ostringstream debug_name;
94 // main.dart$main-1234
95 debug_name << context_.advisory_script_uri << "$"
96 << context_.advisory_script_entrypoint << "-" << main_port_;
98}
99
102 Dart_EnterScope();
103 Dart_ThrowException(
104 tonic::ToDart("UI actions are only available on root isolate."));
105 }
106}
107
108void UIDartState::SetDebugName(const std::string& debug_name) {
109 debug_name_ = debug_name;
110 if (platform_configuration_) {
111 platform_configuration_->client()->UpdateIsolateDescription(debug_name_,
112 main_port_);
113 }
114}
115
117 return static_cast<UIDartState*>(DartState::Current());
118}
119
121 std::unique_ptr<PlatformConfiguration> platform_configuration) {
123 platform_configuration_ = std::move(platform_configuration);
124 if (platform_configuration_) {
125 platform_configuration_->client()->UpdateIsolateDescription(debug_name_,
126 main_port_);
127 }
128}
129
131 std::weak_ptr<PlatformMessageHandler> handler) {
133 platform_message_handler_ = std::move(handler);
134}
135
137 return context_.task_runners;
138}
139
143
147
148std::shared_ptr<fml::ConcurrentTaskRunner>
152
153void UIDartState::ScheduleMicrotask(Dart_Handle closure) {
154 if (tonic::CheckAndHandleError(closure) || !Dart_IsClosure(closure)) {
155 return;
156 }
157
158 microtask_queue_.ScheduleMicrotask(closure);
159}
160
162 microtask_queue_.RunMicrotasks();
163}
164
166 return microtask_queue_.HasMicrotasks();
167}
168
169void UIDartState::AddOrRemoveTaskObserver(bool add) {
170 auto task_runner = context_.task_runners.GetUITaskRunner();
171 if (!task_runner) {
172 // This may happen in case the isolate has no thread affinity (for example,
173 // the service isolate).
174 return;
175 }
176 FML_DCHECK(add_callback_ && remove_callback_);
177 if (add) {
178 FML_DCHECK(!callback_queue_id_.has_value());
179 callback_queue_id_ =
180 add_callback_(reinterpret_cast<intptr_t>(this),
181 [this]() { this->FlushMicrotasksNow(); });
182 } else if (callback_queue_id_.has_value()) {
183 remove_callback_(callback_queue_id_.value(),
184 reinterpret_cast<intptr_t>(this));
185 callback_queue_id_.reset();
186 }
187}
188
191 return context_.snapshot_delegate;
192}
193
198
203
204std::shared_ptr<IsolateNameServer> UIDartState::GetIsolateNameServer() const {
205 return isolate_name_server_;
206}
207
215
216void UIDartState::LogMessage(const std::string& tag,
217 const std::string& message) const {
218 if (log_message_callback_) {
219 log_message_callback_(tag, message);
220 }
221}
222
224 std::unique_ptr<PlatformMessage> message) {
225 if (platform_configuration_) {
226 platform_configuration_->client()->HandlePlatformMessage(
227 std::move(message));
228 } else {
229 std::shared_ptr<PlatformMessageHandler> handler =
230 platform_message_handler_.lock();
231 if (handler) {
232 handler->HandlePlatformMessage(std::move(message));
233 } else {
234 return tonic::ToDart(
235 "No platform channel handler registered for background isolate.");
236 }
237 }
238
239 return Dart_Null();
240}
241
243 return IsRootIsolate() ? reinterpret_cast<int64_t>(this) : 0;
244}
245
246Dart_Isolate UIDartState::CreatePlatformIsolate(Dart_Handle entry_point,
247 char** error) {
249 return nullptr;
250}
251
252/// The runtime stage to use for fragment shaders.
256
257} // namespace flutter
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
fml::RefPtr< flutter::SkiaUnrefQueue > GetSkiaUnrefQueue() const
std::shared_ptr< fml::ConcurrentTaskRunner > GetConcurrentTaskRunner() const
fml::TaskRunnerAffineWeakPtr< ImageGeneratorRegistry > GetImageGeneratorRegistry() 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)
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
DartErrorHandleType isolate_last_error() const
void ScheduleMicrotask(Dart_Handle callback)
DartErrorHandleType GetLastError()
DartMessageHandler & message_handler()
Definition dart_state.h:61
const gchar FlBinaryMessengerMessageHandler handler
G_BEGIN_DECLS GBytes * message
const uint8_t uint32_t uint32_t GError ** error
#define FML_UNREACHABLE()
Definition logging.h:128
#define FML_DCHECK(condition)
Definition logging.h:122
std::function< void(fml::TaskQueueId, intptr_t)> TaskObserverRemove
Definition settings.h:77
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
Definition ref_ptr.h:261
Dart_Handle ToDart(const T &object)
DartErrorHandleType
Definition dart_error.h:67
@ kNoError
Definition dart_error.h:68
bool CheckAndHandleError(Dart_Handle handle)
Definition dart_error.cc:33
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.
Context(const TaskRunners &task_runners)
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