1 /*
2  * Copyright (c) 2021-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 #include "command/rs_base_node_command.h"
17 
18 #include "pipeline/rs_base_render_node.h"
19 #include "platform/common/rs_log.h"
20 
21 namespace OHOS {
22 namespace Rosen {
23 
Destroy(RSContext & context,NodeId nodeId)24 void BaseNodeCommandHelper::Destroy(RSContext& context, NodeId nodeId)
25 {
26     auto& nodeMap = context.GetMutableNodeMap();
27     auto node = nodeMap.GetRenderNode(nodeId);
28     if (node == nullptr) {
29         return;
30     }
31     auto parent = node->GetParent().lock();
32     node->ClearChildren();
33     node->RemoveFromTree();
34     nodeMap.UnregisterRenderNode(node->GetId());
35     if (parent) {
36         parent->RemoveChildFromFulllist(node->GetId());
37     }
38 }
39 
AddChild(RSContext & context,NodeId nodeId,NodeId childNodeId,int32_t index)40 void BaseNodeCommandHelper::AddChild(RSContext& context, NodeId nodeId, NodeId childNodeId, int32_t index)
41 {
42     auto& nodeMap = context.GetNodeMap();
43     auto node = nodeMap.GetRenderNode(nodeId);
44     auto child = nodeMap.GetRenderNode(childNodeId);
45     if (node && child) {
46         node->AddChild(child, index);
47     } else if (child == nullptr) {
48         RS_LOGE("BaseNodeCommandHelper::AddChild child:%{public}" PRIu64 " not found in map", childNodeId);
49     }
50 }
51 
MoveChild(RSContext & context,NodeId nodeId,NodeId childNodeId,int32_t index)52 void BaseNodeCommandHelper::MoveChild(RSContext& context, NodeId nodeId, NodeId childNodeId, int32_t index)
53 {
54     auto& nodeMap = context.GetNodeMap();
55     auto node = nodeMap.GetRenderNode(nodeId);
56     auto child = nodeMap.GetRenderNode(childNodeId);
57     if (node && child) {
58         node->MoveChild(child, index);
59     }
60 }
61 
RemoveChild(RSContext & context,NodeId nodeId,NodeId childNodeId)62 void BaseNodeCommandHelper::RemoveChild(RSContext& context, NodeId nodeId, NodeId childNodeId)
63 {
64     auto& nodeMap = context.GetNodeMap();
65     auto node = nodeMap.GetRenderNode(nodeId);
66     auto child = nodeMap.GetRenderNode(childNodeId);
67     if (node && child) {
68         node->RemoveChild(child);
69     }
70 }
71 
AddCrossParentChild(RSContext & context,NodeId id,NodeId childId,int32_t index)72 void BaseNodeCommandHelper::AddCrossParentChild(RSContext& context, NodeId id, NodeId childId, int32_t index)
73 {
74     auto& nodeMap = context.GetNodeMap();
75     auto node = nodeMap.GetRenderNode(id);
76     auto child = nodeMap.GetRenderNode(childId);
77     if (node && child) {
78         node->AddCrossParentChild(child, index);
79     }
80 }
81 
RemoveCrossParentChild(RSContext & context,NodeId nodeId,NodeId childNodeId,NodeId newParentId)82 void BaseNodeCommandHelper::RemoveCrossParentChild(RSContext& context, NodeId nodeId, NodeId childNodeId,
83     NodeId newParentId)
84 {
85     auto& nodeMap = context.GetNodeMap();
86     auto node = nodeMap.GetRenderNode(nodeId);
87     auto child = nodeMap.GetRenderNode(childNodeId);
88     auto newParent = nodeMap.GetRenderNode(newParentId);
89     if (node && child && newParent) {
90         node->RemoveCrossParentChild(child, newParent);
91     }
92 }
93 
RemoveFromTree(RSContext & context,NodeId nodeId)94 void BaseNodeCommandHelper::RemoveFromTree(RSContext& context, NodeId nodeId)
95 {
96     auto& nodeMap = context.GetNodeMap();
97     auto node = nodeMap.GetRenderNode(nodeId);
98     if (node == nullptr) {
99         return;
100     }
101     node->RemoveFromTree();
102 }
103 
ClearChildren(RSContext & context,NodeId nodeId)104 void BaseNodeCommandHelper::ClearChildren(RSContext& context, NodeId nodeId)
105 {
106     auto& nodeMap = context.GetNodeMap();
107     auto node = nodeMap.GetRenderNode(nodeId);
108     if (node) {
109         node->ClearChildren();
110     }
111 }
112 } // namespace Rosen
113 } // namespace OHOS
114