1 /* 2 * Copyright (c) 2023 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_NG_BASE_DISTRIBUTE_UI_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_DISTRIBUTE_UI_H 18 19 #include <cstdint> 20 #include <functional> 21 #include <set> 22 23 #include "interfaces/inner_api/ace/serializeable_object.h" 24 25 #include "base/json/node_object.h" 26 #include "core/components_ng/base/ui_node.h" 27 #include "core/event/touch_event.h" 28 29 #define CHECK_NULL_BREAK(ptr) \ 30 if (!(ptr)) { \ 31 LOGW(#ptr " is null, break on line %{public}d", __LINE__); \ 32 break; \ 33 } 34 35 namespace OHOS::Ace::NG { 36 class DistributedUI { 37 public: 38 enum UpdateType : uint8_t { 39 PAGE_UPDATE = 0, 40 PAGE_CHANGE = 1, 41 }; 42 43 enum StateMachine : uint8_t { 44 INIT = 0, 45 SOURCE_START = 1, 46 SINK_START = 2, 47 STOP = 3, 48 }; 49 50 enum OperationType : uint8_t { 51 OP_ADD = 0, 52 OP_MODIFY = 1, 53 OP_DELETE = 2, 54 }; 55 56 DistributedUI() = default; 57 ~DistributedUI() = default; 58 59 // for distribute UI source 60 SerializeableObjectArray DumpUITree(); 61 void SubscribeUpdate(const std::function<void(int32_t, SerializeableObjectArray&)>& onUpdate); 62 void UnSubscribeUpdate(); 63 void ProcessSerializeableInputEvent(const SerializeableObjectArray& array); 64 65 // for distribute UI sink 66 void RestoreUITree(const SerializeableObjectArray& array); 67 void UpdateUITree(const SerializeableObjectArray& array); 68 void SubscribeInputEventProcess(const std::function<void(SerializeableObjectArray&)>& onEvent); 69 void UnSubscribeInputEventProcess(); 70 71 // internal APIs for distribute UI source 72 void AddDeletedNode(int32_t nodeId); 73 void AddNewNode(int32_t nodeId); 74 void AddDirtyCustomNode(int32_t nodeId); 75 void AddDirtyRenderNode(int32_t nodeId); 76 void AddDirtyLayoutNode(int32_t nodeId); 77 void OnTreeUpdate(); 78 void OnPageChanged(int32_t pageId); 79 int32_t GetCurrentPageId(); 80 81 // internal APIs for distribute UI sink 82 void BypassEvent(const TouchEvent& point, bool isSubPipe); 83 bool IsSinkMode(); 84 void ApplyOneUpdate(); 85 86 private: 87 void DumpDirtyRenderNodes(SerializeableObjectArray& objectArray); 88 void DumpDirtyLayoutNodes(SerializeableObjectArray& objectArray); 89 void DumpNewNodes(SerializeableObjectArray& objectArray); 90 void DumpDelNodes(SerializeableObjectArray& objectArray); 91 SerializeableObjectArray DumpUpdate(); 92 void ResetDirtyNodes(); 93 bool IsNewNode(int32_t nodeId); 94 bool ReadyToDumpUpdate(); 95 96 void SetIdMapping(int32_t srcNodeId, int32_t sinkNodeId); 97 int32_t GetIdMapping(int32_t srcNodeId); 98 void AddNodeHash(int32_t nodeId, std::size_t hashValue); 99 void DelNodeHash(int32_t nodeId); 100 bool IsRecordHash(int32_t nodeId, std::size_t hashValue); 101 void DumpNode(const RefPtr<NG::UINode>& node, int depth, OperationType op, std::unique_ptr<NodeObject>& nodeObject); 102 void DumpTreeInner(const RefPtr<NG::UINode>& node, SerializeableObjectArray& objectArray, int depth); 103 RefPtr<UINode> RestoreNode(const std::unique_ptr<NodeObject>& nodeObject); 104 void AttachToTree(RefPtr<UINode> root, RefPtr<UINode> uiNode, const std::unique_ptr<NodeObject>& nodeObject); 105 void AddNode(const std::unique_ptr<NodeObject>& nodeObject, RefPtr<FrameNode> pageRootNode); 106 void ModNode(const std::unique_ptr<NodeObject>& nodeObject); 107 void DelNode(const std::unique_ptr<NodeObject>& nodeObject); 108 void UpdateUITreeInner(SerializeableObjectArray& nodeArray); 109 void RestoreUITreeInner(const SerializeableObjectArray& nodeArray); 110 bool IsInCurrentPage(RefPtr<NG::UINode> node, int32_t pageId); 111 112 std::set<int32_t, std::greater<int32_t>> deletedNodes_; 113 std::set<int32_t> newNodes_; 114 std::set<int32_t> dirtyCustomNodes_; 115 std::set<int32_t> dirtyLayoutNodes_; 116 std::set<int32_t> dirtyRenderNodes_; 117 bool pageChangeFlag_ = false; 118 int32_t currentPageId_ = 0; 119 std::function<void(UpdateType, SerializeableObjectArray&)> onUpdateCb_; 120 std::function<void(SerializeableObjectArray&)> onEventCb_; 121 StateMachine status_ = StateMachine::INIT; 122 std::list<SerializeableObjectArray> pendingUpdates_; 123 124 std::unordered_map<int32_t, int32_t> nodeIdMapping_; 125 std::unordered_map<int32_t, std::size_t> nodeHashs_; 126 std::list<RefPtr<NG::UINode>> sinkPageChildren_; 127 }; 128 } // namespace OHOS::Ace::NG 129 130 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_DISTRIBUTE_UI_H 131