• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_ELEMENT_REGISTER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_ELEMENT_REGISTER_H
18 
19 #include <functional>
20 #include <inttypes.h>
21 #include <list>
22 #include <mutex>
23 #include <unordered_map>
24 #include <unordered_set>
25 
26 #include "base/memory/referenced.h"
27 #include "frameworks/base/memory/ace_type.h"
28 #include "frameworks/core/components_ng/animation/geometry_transition.h"
29 
30 namespace OHOS::Ace::V2 {
31 class ElementProxy;
32 } // namespace OHOS::Ace::V2
33 
34 namespace OHOS::Ace::NG {
35 class UINode;
36 class FrameNode;
37 } // namespace OHOS::Ace::NG
38 
39 namespace OHOS::Ace {
40 using ElementIdType = int32_t;
41 class Element;
42 
43 using RemovedElementsType = std::unordered_set<ElementIdType>;
44 
45 class ACE_EXPORT ElementRegister {
46 public:
47     static constexpr ElementIdType UndefinedElementId = static_cast<ElementIdType>(-1);
48 
49     ACE_FORCE_EXPORT static ElementRegister* GetInstance();
50     RefPtr<Element> GetElementById(ElementIdType elementId);
51     RefPtr<V2::ElementProxy> GetElementProxyById(ElementIdType elementId);
52 
53     ACE_FORCE_EXPORT RefPtr<AceType> GetNodeById(ElementIdType elementId);
54     /**
55      * version of GetNodeById(elmtId) function to return an Element of
56      * given class. returns nullptr if Element with this elmtId baddest found
57      * or class mismatch
58      */
59     template<class E>
GetSpecificItemById(ElementIdType elmtId)60     RefPtr<E> GetSpecificItemById(ElementIdType elmtId)
61     {
62         return AceType::DynamicCast<E>(GetNodeById(elmtId));
63     }
64 
65     bool AddElementProxy(const WeakPtr<V2::ElementProxy>& element);
66     bool AddElement(const RefPtr<Element>& element);
67 
68     ACE_FORCE_EXPORT RefPtr<NG::UINode> GetUINodeById(ElementIdType elementId);
69     NG::FrameNode* GetFrameNodePtrById(ElementIdType elementId);
70 
71     ACE_FORCE_EXPORT bool AddUINode(const RefPtr<NG::UINode>& node);
72 
73     bool Exists(ElementIdType elementId);
74 
75     /**
76      * When a custom node is created from recycle, update its element id.
77      */
78     void UpdateRecycleElmtId(int32_t oldElmtId, int32_t newElmtId);
79 
80     /**
81      * remove Element with given elmtId from the Map
82      * means GetElementById on this elmtId no longer returns an Element
83      * method adds the elmtId to the removed Element Set
84      */
85     bool RemoveItem(ElementIdType elementId);
86 
87     /**
88      * remove Element with given elmtId from the Map
89      * means GetElementById on this elmtId no longer returns an Element
90      * method does NOT add the elmtId to the removed Element Set
91      * Use with caution: e.g. only use when knowing the Element will
92      * be added with new ElementId shortly
93      */
94     ACE_FORCE_EXPORT bool RemoveItemSilently(ElementIdType elementId);
95 
96     void MoveRemovedItems(RemovedElementsType& removedItems);
97 
98     /**
99      * does a complete reset
100      * clears the Map of Elements and Set of removed Elements
101      */
102     void Clear();
103 
104     ACE_FORCE_EXPORT ElementIdType MakeUniqueId();
105 
106     RefPtr<NG::GeometryTransition> GetOrCreateGeometryTransition(
107         const std::string& id, bool followWithoutTransition = false, bool doRegisterSharedTransition = true);
108     void DumpGeometryTransition();
109 
110     void ReSyncGeometryTransition(const WeakPtr<NG::FrameNode>& trigger = nullptr,
111                                   const AnimationOption& option = AnimationOption());
112 
113     void AddPendingRemoveNode(const RefPtr<NG::UINode>& node);
114     void ClearPendingRemoveNodes();
115 
RegisterJSCleanUpIdleTaskFunc(const std::function<void (int64_t)> & jsCallback)116     void RegisterJSCleanUpIdleTaskFunc(const std::function<void(int64_t)>& jsCallback) {
117         jsCleanUpIdleTaskCallback_ = std::move(jsCallback);
118     }
119 
CallJSCleanUpIdleTaskFunc(int64_t maxTimeInNs)120     void CallJSCleanUpIdleTaskFunc(int64_t maxTimeInNs) {
121         if (jsCleanUpIdleTaskCallback_) {
122             ACE_SCOPED_TRACE_COMMERCIAL("OnIdle CallJSCleanUpIdleTaskFunc:%" PRId64 "", maxTimeInNs);
123             jsCleanUpIdleTaskCallback_(maxTimeInNs);
124         }
125     }
126 
GetNodeNum()127     uint32_t GetNodeNum() const
128     {
129         return itemMap_.size();
130     }
131 
GetLastestElementId()132     ElementIdType GetLastestElementId() const
133     {
134         return lastestElementId_;
135     }
136 
137     RefPtr<NG::FrameNode> GetAttachedFrameNodeById(const std::string& key, bool willGetAll = false);
138 
139     void AddFrameNodeByInspectorId(const std::string& key, const WeakPtr<NG::FrameNode>& node);
140 
141     void RemoveFrameNodeByInspectorId(const std::string& key, int32_t nodeId);
142 
143     void RegisterEmbedNode(const uint64_t surfaceId, const WeakPtr<NG::FrameNode>& node);
144 
145     void UnregisterEmbedNode(const uint64_t surfaceId, const WeakPtr<NG::FrameNode>& node);
146 
147     WeakPtr<NG::FrameNode> GetEmbedNodeBySurfaceId(const uint64_t surfaceId);
148 
149     bool IsEmbedNode(NG::FrameNode* node);
150 
151     uint64_t GetSurfaceIdByEmbedNode(NG::FrameNode* node);
152 
153 private:
154     // private constructor
155     ElementRegister() = default;
156 
157     bool AddReferenced(ElementIdType elmtId, const WeakPtr<AceType>& referenced);
158 
159     //  Singleton instance
160     static thread_local ElementRegister* instance_;
161     static std::mutex mutex_;
162 
163     // ElementID assigned during initial render
164     // first to Component, then synced to Element
165     static std::atomic<ElementIdType> nextUniqueElementId_;
166 
167     ElementIdType lastestElementId_ = 0;
168 
169     // Map for created elements
170     std::unordered_map<ElementIdType, WeakPtr<AceType>> itemMap_;
171 
172     // Map for inspectorId
173     std::unordered_map<std::string, std::list<WeakPtr<NG::FrameNode>>> inspectorIdMap_;
174 
175     RemovedElementsType removedItems_;
176 
177     std::unordered_map<std::string, RefPtr<NG::GeometryTransition>> geometryTransitionMap_;
178 
179     std::list<RefPtr<NG::UINode>> pendingRemoveNodes_;
180 
181     std::function<void(int64_t)> jsCleanUpIdleTaskCallback_;
182 
183     ACE_DISALLOW_COPY_AND_MOVE(ElementRegister);
184 
185     std::unordered_map<uint64_t, WeakPtr<NG::FrameNode>> surfaceIdEmbedNodeMap_;
186 
187     std::unordered_map<NG::FrameNode*, uint64_t> embedNodeSurfaceIdMap_;
188 };
189 } // namespace OHOS::Ace
190 #endif
191