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/flow/skia_gpu_object.h"
15 #include "flutter/fml/build_config.h"
16 #include "flutter/fml/memory/weak_ptr.h"
17 #include "flutter/fml/synchronization/waitable_event.h"
18 #include "flutter/lib/ui/io_manager.h"
19 #include "flutter/lib/ui/isolate_name_server/isolate_name_server.h"
20 #include "flutter/lib/ui/painting/image_decoder.h"
21 #include "flutter/lib/ui/snapshot_delegate.h"
22 #include "flutter/lib/ui/volatile_path_tracker.h"
23 #include "third_party/dart/runtime/include/dart_api.h"
24 #include "third_party/skia/include/gpu/GrDirectContext.h"
28 
29 namespace flutter {
30 class FontSelector;
31 class ImageGeneratorRegistry;
32 class PlatformConfiguration;
33 
34 class UIDartState : public tonic::DartState {
35  public:
36  static UIDartState* Current();
37 
38  /// @brief The subset of state which is owned by the shell or engine
39  /// and passed through the RuntimeController into DartIsolates.
40  /// If a shell-owned resource needs to be exposed to the framework via
41  /// UIDartState, a pointer to the resource can be added to this
42  /// struct with appropriate default construction.
43  struct Context {
45 
46  Context(const TaskRunners& task_runners,
52  std::string advisory_script_uri,
53  std::string advisory_script_entrypoint,
54  std::shared_ptr<VolatilePathTracker> volatile_path_tracker);
55 
56  /// The task runners used by the shell hosting this runtime controller. This
57  /// may be used by the isolate to scheduled asynchronous texture uploads or
58  /// post tasks to the platform task runner.
60 
61  /// The snapshot delegate used by the
62  /// isolate to gather raster snapshots
63  /// of Flutter view hierarchies.
65 
66  /// The IO manager used by the isolate for asynchronous texture uploads.
68 
69  /// The unref queue used by the isolate to collect resources that may
70  /// reference resources on the GPU.
72 
73  /// The image decoder.
75 
76  /// Cascading registry of image generator builders. Given compressed image
77  /// bytes as input, this is used to find and create image generators, which
78  /// can then be used for image decoding.
80 
81  /// The advisory script URI (only used for debugging). This does not affect
82  /// the code being run in the isolate in any way.
83  std::string advisory_script_uri;
84 
85  /// The advisory script entrypoint (only used for debugging). This does not
86  /// affect the code being run in the isolate in any way. The isolate must be
87  /// transitioned to the running state explicitly by the caller.
89 
90  /// Cache for tracking path volatility.
91  std::shared_ptr<VolatilePathTracker> volatile_path_tracker;
92  };
93 
94  Dart_Port main_port() const { return main_port_; }
95  // Root isolate of the VM application
96  bool IsRootIsolate() const { return is_root_isolate_; }
97  static void ThrowIfUIOperationsProhibited();
98 
99  void SetDebugName(const std::string name);
100 
101  const std::string& debug_name() const { return debug_name_; }
102 
103  const std::string& logger_prefix() const { return logger_prefix_; }
104 
106  return platform_configuration_.get();
107  }
108 
109  const TaskRunners& GetTaskRunners() const;
110 
111  void ScheduleMicrotask(Dart_Handle handle);
112 
113  void FlushMicrotasksNow();
114 
116 
118 
119  std::shared_ptr<VolatilePathTracker> GetVolatilePathTracker() const;
120 
122 
124 
126 
128 
129  std::shared_ptr<IsolateNameServer> GetIsolateNameServer() const;
130 
132 
133  void ReportUnhandledException(const std::string& error,
134  const std::string& stack_trace);
135 
136  // Logs `print` messages from the application via an embedder-specified
137  // logging mechanism.
138  //
139  // @param[in] tag A component name or tag that identifies the logging
140  // application.
141  // @param[in] message The message to be logged.
142  void LogMessage(const std::string& tag, const std::string& message) const;
143 
144  bool enable_skparagraph() const;
145 
146  bool enable_display_list() const;
147 
148  template <class T>
149  static flutter::SkiaGPUObject<T> CreateGPUObject(sk_sp<T> object) {
150  if (!object) {
151  return {};
152  }
153  auto* state = UIDartState::Current();
154  FML_DCHECK(state);
155  auto queue = state->GetSkiaUnrefQueue();
156  return {std::move(object), std::move(queue)};
157  };
158 
159  protected:
160  UIDartState(TaskObserverAdd add_callback,
161  TaskObserverRemove remove_callback,
162  std::string logger_prefix,
163  UnhandledExceptionCallback unhandled_exception_callback,
164  LogMessageCallback log_message_callback,
165  std::shared_ptr<IsolateNameServer> isolate_name_server,
166  bool is_root_isolate_,
167  bool enable_skparagraph,
168  bool enable_display_list,
169  const UIDartState::Context& context);
170 
171  ~UIDartState() override;
172 
174  std::unique_ptr<PlatformConfiguration> platform_configuration);
175 
176  const std::string& GetAdvisoryScriptURI() const;
177 
178  const std::string& GetAdvisoryScriptEntrypoint() const;
179 
180  private:
181  void DidSetIsolate() override;
182 
183  const TaskObserverAdd add_callback_;
184  const TaskObserverRemove remove_callback_;
185  const std::string logger_prefix_;
186  Dart_Port main_port_ = ILLEGAL_PORT;
187  const bool is_root_isolate_;
188  std::string debug_name_;
189  std::unique_ptr<PlatformConfiguration> platform_configuration_;
190  tonic::DartMicrotaskQueue microtask_queue_;
191  UnhandledExceptionCallback unhandled_exception_callback_;
192  LogMessageCallback log_message_callback_;
193  const std::shared_ptr<IsolateNameServer> isolate_name_server_;
194  const bool enable_skparagraph_;
195  const bool enable_display_list_;
196  UIDartState::Context context_;
197 
198  void AddOrRemoveTaskObserver(bool add);
199 };
200 
201 } // namespace flutter
202 
203 #endif // FLUTTER_LIB_UI_UI_DART_STATE_H_
std::function< bool(const std::string &, const std::string &)> UnhandledExceptionCallback
Definition: settings.h:77
void ReportUnhandledException(const std::string &error, const std::string &stack_trace)
fml::RefPtr< SkiaUnrefQueue > unref_queue
Definition: ui_dart_state.h:71
const uint8_t uint32_t uint32_t GError ** error
fml::WeakPtr< SnapshotDelegate > snapshot_delegate
Definition: ui_dart_state.h:64
#define FML_DCHECK(condition)
Definition: logging.h:86
fml::WeakPtr< IOManager > io_manager
The IO manager used by the isolate for asynchronous texture uploads.
Definition: ui_dart_state.h:67
std::function< void(intptr_t)> TaskObserverRemove
Definition: settings.h:74
tonic::DartErrorHandleType GetLastError()
fml::WeakPtr< SnapshotDelegate > GetSnapshotDelegate() const
std::function< void(const std::string &, const std::string &)> LogMessageCallback
Definition: settings.h:80
const std::string & logger_prefix() const
void SetDebugName(const std::string name)
fml::WeakPtr< GrDirectContext > GetResourceContext() const
fml::WeakPtr< ImageGeneratorRegistry > GetImageGeneratorRegistry() const
DartErrorHandleType
Definition: dart_error.h:18
bool enable_display_list() const
bool enable_skparagraph() const
bool IsRootIsolate() const
Definition: ui_dart_state.h:96
const std::string & GetAdvisoryScriptEntrypoint() const
std::function< void(intptr_t, fml::closure)> TaskObserverAdd
Definition: settings.h:73
fml::WeakPtr< ImageGeneratorRegistry > image_generator_registry
Definition: ui_dart_state.h:79
fml::WeakPtr< ImageDecoder > image_decoder
The image decoder.
Definition: ui_dart_state.h:74
const std::string & debug_name() const
static void ThrowIfUIOperationsProhibited()
The subset of state which is owned by the shell or engine and passed through the RuntimeController in...
Definition: ui_dart_state.h:43
PlatformConfiguration * platform_configuration() const
std::shared_ptr< VolatilePathTracker > volatile_path_tracker
Cache for tracking path volatility.
Definition: ui_dart_state.h:91
std::string advisory_script_entrypoint
Definition: ui_dart_state.h:88
const TaskRunners task_runners
Definition: ui_dart_state.h:59
fml::RefPtr< flutter::SkiaUnrefQueue > GetSkiaUnrefQueue() const
const std::string & GetAdvisoryScriptURI() const
const char * name
Definition: fuchsia.cc:50
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_, bool enable_skparagraph, bool enable_display_list, const UIDartState::Context &context)
fml::WeakPtr< ImageDecoder > GetImageDecoder() const
A class for holding and distributing platform-level information to and from the Dart code in Flutter&#39;...
fml::WeakPtr< IOManager > GetIOManager() const
void LogMessage(const std::string &tag, const std::string &message) const
std::shared_ptr< IsolateNameServer > GetIsolateNameServer() const
const TaskRunners & GetTaskRunners() const
Dart_Port main_port() const
Definition: ui_dart_state.h:94
void ScheduleMicrotask(Dart_Handle handle)
Context(const TaskRunners &task_runners)
void SetPlatformConfiguration(std::unique_ptr< PlatformConfiguration > platform_configuration)
static flutter::SkiaGPUObject< T > CreateGPUObject(sk_sp< T > object)
AtkStateType state
static UIDartState * Current()
std::shared_ptr< VolatilePathTracker > GetVolatilePathTracker() const