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/hint_freed_delegate.h"
19 #include "flutter/lib/ui/io_manager.h"
20 #include "flutter/lib/ui/isolate_name_server/isolate_name_server.h"
21 #include "flutter/lib/ui/painting/image_decoder.h"
22 #include "flutter/lib/ui/snapshot_delegate.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 PlatformConfiguration;
32 
33 class UIDartState : public tonic::DartState {
34  public:
35  static UIDartState* Current();
36 
37  Dart_Port main_port() const { return main_port_; }
38  // Root isolate of the VM application
39  bool IsRootIsolate() const { return is_root_isolate_; }
40  static void ThrowIfUIOperationsProhibited();
41 
42  void SetDebugName(const std::string name);
43 
44  const std::string& debug_name() const { return debug_name_; }
45 
46  const std::string& logger_prefix() const { return logger_prefix_; }
47 
49  return platform_configuration_.get();
50  }
51 
52  const TaskRunners& GetTaskRunners() const;
53 
54  void ScheduleMicrotask(Dart_Handle handle);
55 
56  void FlushMicrotasksNow();
57 
59 
61 
63 
65 
67 
69 
70  std::shared_ptr<IsolateNameServer> GetIsolateNameServer() const;
71 
73 
74  void ReportUnhandledException(const std::string& error,
75  const std::string& stack_trace);
76 
77  template <class T>
78  static flutter::SkiaGPUObject<T> CreateGPUObject(sk_sp<T> object) {
79  if (!object) {
80  return {};
81  }
82  auto* state = UIDartState::Current();
83  FML_DCHECK(state);
84  auto queue = state->GetSkiaUnrefQueue();
85  return {std::move(object), std::move(queue)};
86  };
87 
88  protected:
89  UIDartState(TaskRunners task_runners,
90  TaskObserverAdd add_callback,
91  TaskObserverRemove remove_callback,
92  fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
93  fml::WeakPtr<HintFreedDelegate> hint_freed_delegate,
94  fml::WeakPtr<IOManager> io_manager,
95  fml::RefPtr<SkiaUnrefQueue> skia_unref_queue,
96  fml::WeakPtr<ImageDecoder> image_decoder,
97  std::string advisory_script_uri,
98  std::string advisory_script_entrypoint,
99  std::string logger_prefix,
100  UnhandledExceptionCallback unhandled_exception_callback,
101  std::shared_ptr<IsolateNameServer> isolate_name_server,
102  bool is_root_isolate_);
103 
104  ~UIDartState() override;
105 
107  std::unique_ptr<PlatformConfiguration> platform_configuration);
108 
109  const std::string& GetAdvisoryScriptURI() const;
110 
111  const std::string& GetAdvisoryScriptEntrypoint() const;
112 
113  private:
114  void DidSetIsolate() override;
115 
116  const TaskRunners task_runners_;
117  const TaskObserverAdd add_callback_;
118  const TaskObserverRemove remove_callback_;
119  fml::WeakPtr<SnapshotDelegate> snapshot_delegate_;
120  fml::WeakPtr<HintFreedDelegate> hint_freed_delegate_;
121  fml::WeakPtr<IOManager> io_manager_;
122  fml::RefPtr<SkiaUnrefQueue> skia_unref_queue_;
123  fml::WeakPtr<ImageDecoder> image_decoder_;
124  const std::string advisory_script_uri_;
125  const std::string advisory_script_entrypoint_;
126  const std::string logger_prefix_;
127  Dart_Port main_port_ = ILLEGAL_PORT;
128  const bool is_root_isolate_;
129  std::string debug_name_;
130  std::unique_ptr<PlatformConfiguration> platform_configuration_;
131  tonic::DartMicrotaskQueue microtask_queue_;
132  UnhandledExceptionCallback unhandled_exception_callback_;
133  const std::shared_ptr<IsolateNameServer> isolate_name_server_;
134 
135  void AddOrRemoveTaskObserver(bool add);
136 };
137 
138 } // namespace flutter
139 
140 #endif // FLUTTER_LIB_UI_UI_DART_STATE_H_
std::function< bool(const std::string &, const std::string &)> UnhandledExceptionCallback
Definition: settings.h:52
void ReportUnhandledException(const std::string &error, const std::string &stack_trace)
#define FML_DCHECK(condition)
Definition: logging.h:86
std::function< void(intptr_t)> TaskObserverRemove
Definition: settings.h:49
tonic::DartErrorHandleType GetLastError()
FlMethodResponse GError ** error
fml::WeakPtr< SnapshotDelegate > GetSnapshotDelegate() const
const std::string & logger_prefix() const
Definition: ui_dart_state.h:46
void SetDebugName(const std::string name)
fml::WeakPtr< GrDirectContext > GetResourceContext() const
UIDartState(TaskRunners task_runners, TaskObserverAdd add_callback, TaskObserverRemove remove_callback, fml::WeakPtr< SnapshotDelegate > snapshot_delegate, fml::WeakPtr< HintFreedDelegate > hint_freed_delegate, fml::WeakPtr< IOManager > io_manager, fml::RefPtr< SkiaUnrefQueue > skia_unref_queue, fml::WeakPtr< ImageDecoder > image_decoder, std::string advisory_script_uri, std::string advisory_script_entrypoint, std::string logger_prefix, UnhandledExceptionCallback unhandled_exception_callback, std::shared_ptr< IsolateNameServer > isolate_name_server, bool is_root_isolate_)
DartErrorHandleType
Definition: dart_error.h:18
bool IsRootIsolate() const
Definition: ui_dart_state.h:39
const std::string & GetAdvisoryScriptEntrypoint() const
std::function< void(intptr_t, fml::closure)> TaskObserverAdd
Definition: settings.h:48
const std::string & debug_name() const
Definition: ui_dart_state.h:44
static void ThrowIfUIOperationsProhibited()
PlatformConfiguration * platform_configuration() const
Definition: ui_dart_state.h:48
fml::RefPtr< flutter::SkiaUnrefQueue > GetSkiaUnrefQueue() const
const std::string & GetAdvisoryScriptURI() const
const char * name
Definition: fuchsia.cc:50
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
fml::WeakPtr< HintFreedDelegate > GetHintFreedDelegate() const
std::shared_ptr< IsolateNameServer > GetIsolateNameServer() const
const TaskRunners & GetTaskRunners() const
Dart_Port main_port() const
Definition: ui_dart_state.h:37
void ScheduleMicrotask(Dart_Handle handle)
void SetPlatformConfiguration(std::unique_ptr< PlatformConfiguration > platform_configuration)
static flutter::SkiaGPUObject< T > CreateGPUObject(sk_sp< T > object)
Definition: ui_dart_state.h:78
static UIDartState * Current()