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