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

source code of flutter_engine/flutter/lib/ui/ui_dart_state.h