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 "third_party/dart/runtime/include/dart_api.h" |
23 | #include "third_party/skia/include/gpu/GrDirectContext.h" |
24 | #include "third_party/tonic/dart_microtask_queue.h" |
25 | #include "third_party/tonic/dart_persistent_value.h" |
26 | #include "third_party/tonic/dart_state.h" |
27 | |
28 | namespace flutter { |
29 | class FontSelector; |
30 | class PlatformConfiguration; |
31 | |
32 | class UIDartState : public tonic::DartState { |
33 | public: |
34 | static UIDartState* Current(); |
35 | |
36 | Dart_Port main_port() const { return main_port_; } |
37 | // Root isolate of the VM application |
38 | bool IsRootIsolate() const { return is_root_isolate_; } |
39 | static void ThrowIfUIOperationsProhibited(); |
40 | |
41 | void SetDebugName(const std::string name); |
42 | |
43 | const std::string& debug_name() const { return debug_name_; } |
44 | |
45 | const std::string& logger_prefix() const { return logger_prefix_; } |
46 | |
47 | PlatformConfiguration* platform_configuration() const { |
48 | return platform_configuration_.get(); |
49 | } |
50 | |
51 | const TaskRunners& GetTaskRunners() const; |
52 | |
53 | void ScheduleMicrotask(Dart_Handle handle); |
54 | |
55 | void FlushMicrotasksNow(); |
56 | |
57 | fml::WeakPtr<IOManager> GetIOManager() const; |
58 | |
59 | fml::RefPtr<flutter::SkiaUnrefQueue> GetSkiaUnrefQueue() const; |
60 | |
61 | fml::WeakPtr<SnapshotDelegate> GetSnapshotDelegate() const; |
62 | |
63 | fml::WeakPtr<GrDirectContext> GetResourceContext() const; |
64 | |
65 | fml::WeakPtr<ImageDecoder> GetImageDecoder() const; |
66 | |
67 | std::shared_ptr<IsolateNameServer> GetIsolateNameServer() const; |
68 | |
69 | tonic::DartErrorHandleType GetLastError(); |
70 | |
71 | void ReportUnhandledException(const std::string& error, |
72 | const std::string& stack_trace); |
73 | |
74 | template <class T> |
75 | static flutter::SkiaGPUObject<T> CreateGPUObject(sk_sp<T> object) { |
76 | if (!object) { |
77 | return {}; |
78 | } |
79 | auto* state = UIDartState::Current(); |
80 | FML_DCHECK(state); |
81 | auto queue = state->GetSkiaUnrefQueue(); |
82 | return {std::move(object), std::move(queue)}; |
83 | }; |
84 | |
85 | protected: |
86 | UIDartState(TaskRunners task_runners, |
87 | TaskObserverAdd add_callback, |
88 | TaskObserverRemove remove_callback, |
89 | fml::WeakPtr<SnapshotDelegate> snapshot_delegate, |
90 | fml::WeakPtr<IOManager> io_manager, |
91 | fml::RefPtr<SkiaUnrefQueue> skia_unref_queue, |
92 | fml::WeakPtr<ImageDecoder> image_decoder, |
93 | std::string advisory_script_uri, |
94 | std::string advisory_script_entrypoint, |
95 | std::string logger_prefix, |
96 | UnhandledExceptionCallback unhandled_exception_callback, |
97 | std::shared_ptr<IsolateNameServer> isolate_name_server, |
98 | bool is_root_isolate_); |
99 | |
100 | ~UIDartState() override; |
101 | |
102 | void SetPlatformConfiguration( |
103 | std::unique_ptr<PlatformConfiguration> platform_configuration); |
104 | |
105 | const std::string& GetAdvisoryScriptURI() const; |
106 | |
107 | const std::string& GetAdvisoryScriptEntrypoint() const; |
108 | |
109 | private: |
110 | void DidSetIsolate() override; |
111 | |
112 | const TaskRunners task_runners_; |
113 | const TaskObserverAdd add_callback_; |
114 | const TaskObserverRemove remove_callback_; |
115 | fml::WeakPtr<SnapshotDelegate> snapshot_delegate_; |
116 | fml::WeakPtr<IOManager> io_manager_; |
117 | fml::RefPtr<SkiaUnrefQueue> skia_unref_queue_; |
118 | fml::WeakPtr<ImageDecoder> image_decoder_; |
119 | const std::string advisory_script_uri_; |
120 | const std::string advisory_script_entrypoint_; |
121 | const std::string logger_prefix_; |
122 | Dart_Port main_port_ = ILLEGAL_PORT; |
123 | const bool is_root_isolate_; |
124 | std::string debug_name_; |
125 | std::unique_ptr<PlatformConfiguration> platform_configuration_; |
126 | tonic::DartMicrotaskQueue microtask_queue_; |
127 | UnhandledExceptionCallback unhandled_exception_callback_; |
128 | const std::shared_ptr<IsolateNameServer> isolate_name_server_; |
129 | |
130 | void AddOrRemoveTaskObserver(bool add); |
131 | }; |
132 | |
133 | } // namespace flutter |
134 | |
135 | #endif // FLUTTER_LIB_UI_UI_DART_STATE_H_ |
136 | |