1 // Copyright 2013-2022 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 "third_party/skia/include/gpu/GrContext.h" 9 10 #include <map> 11 #include <memory> 12 13 #include "flutter/common/task_runners.h" 14 #include "flutter/flow/skia_gpu_object.h" 15 #include "flutter/fml/memory/weak_ptr.h" 16 #include "flutter/fml/thread_local.h" 17 #include "flutter/lib/ui/io_manager.h" 18 #include "flutter/lib/ui/window/window.h" 19 20 namespace flutter { 21 22 class Window; 23 24 class __attribute__((visibility("default"))) UIDartState { 25 public: 26 ~UIDartState(); 27 static UIDartState* Current(); 28 29 class UIStateItem { 30 public: UIStateItem(fml::WeakPtr<IOManager> io_manager,TaskRunners task_runners)31 UIStateItem(fml::WeakPtr<IOManager> io_manager, TaskRunners task_runners) : io_manager_(std::move(io_manager)), 32 task_runners_(std::move(task_runners)) {} GetTaskRunners()33 const TaskRunners& GetTaskRunners() const { return task_runners_; } GetSkiaUnrefQueue()34 fml::RefPtr<SkiaUnrefQueue> GetSkiaUnrefQueue() const { 35 return io_manager_->GetSkiaUnrefQueue(); 36 } GetIOManager()37 fml::WeakPtr<IOManager> GetIOManager() const { 38 return io_manager_; 39 } 40 private: 41 fml::WeakPtr<IOManager> io_manager_; 42 const TaskRunners task_runners_; 43 }; 44 45 static void Init(int32_t instanceId, 46 fml::WeakPtr<IOManager> io_manager, 47 TaskRunners task_runners); 48 static void DeInit(int32_t instanceId); 49 50 fml::RefPtr<SkiaUnrefQueue> GetSkiaUnrefQueue() const; 51 fml::WeakPtr<IOManager> GetIOManager() const; 52 Window* window() const; 53 const TaskRunners& GetTaskRunners() const; 54 UIStateItem* GetStateById(int32_t id) const; 55 56 template <class T> CreateGPUObject(sk_sp<T> object)57 static SkiaGPUObject<T> CreateGPUObject(sk_sp<T> object) { 58 if (!object) { 59 return {}; 60 } 61 62 auto state = UIDartState::Current(); 63 auto queue = state->GetSkiaUnrefQueue(); 64 return {std::move(object), std::move(queue)}; 65 }; 66 SetCurInstance(int32_t id)67 void SetCurInstance(int32_t id) { 68 int32_t* ptr = new int32_t(id); 69 cur_instance_id_.reset(ptr); 70 } 71 72 void SetCurStateItem(int32_t id, std::unique_ptr<UIStateItem>); 73 void RemoveStateItem(int32_t id); 74 75 void AddPluginParentContainer(int64_t pluginId, int32_t pluginParentContainerId); 76 void RemovePluginParentContainer(int64_t pluginParentContainerId); 77 int64_t GetPluginParentContainerId(int64_t pluginId) const; 78 79 private: 80 UIDartState(); 81 fml::ThreadLocalUniquePtr<int32_t> cur_instance_id_; 82 std::map<int32_t, std::unique_ptr<UIStateItem>> state_map_; 83 mutable std::mutex mutex_; 84 std::unordered_map<int64_t, int32_t> parentContainerMap_; 85 mutable std::mutex parentContainerMutex_; 86 FML_DISALLOW_COPY_AND_ASSIGN(UIDartState); 87 }; 88 89 } // namespace blink 90 91 #endif // FLUTTER_LIB_UI_UI_DART_STATE_H_ 92