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 #include "ui/rs_proxy_node.h"
17 
18 #include "command/rs_base_node_command.h"
19 #include "command/rs_proxy_node_command.h"
20 #include "pipeline/rs_node_map.h"
21 #include "platform/common/rs_log.h"
22 #include "transaction/rs_transaction_proxy.h"
23 
24 namespace OHOS {
25 namespace Rosen {
Create(NodeId targetNodeId,std::string name)26 RSProxyNode::SharedPtr RSProxyNode::Create(NodeId targetNodeId, std::string name)
27 {
28     if (auto prevNode = RSNodeMap::Instance().GetNode(targetNodeId)) {
29         // if the node id is already in the map, we should not create a new node.
30         return prevNode->ReinterpretCastTo<RSProxyNode>();
31     }
32 
33     SharedPtr node(new RSProxyNode(targetNodeId, std::move(name)));
34     RSNodeMap::MutableInstance().RegisterNode(node);
35 
36     auto transactionProxy = RSTransactionProxy::GetInstance();
37     if (transactionProxy == nullptr) {
38         return node;
39     }
40     NodeId proxyNodeId = node->GetHierarchyCommandNodeId();
41     std::unique_ptr<RSCommand> command = std::make_unique<RSProxyNodeCreate>(proxyNodeId, targetNodeId);
42     transactionProxy->AddCommand(command, node->IsUniRenderEnabled());
43 
44     // create proxy node in RS even if uni render not enabled.
45     if (!node->IsUniRenderEnabled()) {
46         std::unique_ptr<RSCommand> command = std::make_unique<RSProxyNodeCreate>(proxyNodeId, targetNodeId);
47         transactionProxy->AddCommand(command, true);
48     }
49 
50     ROSEN_LOGD("RSProxyNode::Create, target node id:%{public}" PRIu64 ", name %s proxy node id %{public}" PRIu64,
51         node->GetId(), node->GetName().c_str(), proxyNodeId);
52 
53     return node;
54 }
55 
~RSProxyNode()56 RSProxyNode::~RSProxyNode()
57 {
58     ROSEN_LOGD("RSProxyNode::~RSProxyNode, proxy id:%{public}" PRIu64 " target:%{public}" PRIu64,
59         proxyNodeId_, GetId());
60 
61     auto transactionProxy = RSTransactionProxy::GetInstance();
62     if (transactionProxy == nullptr) {
63         return;
64     }
65 
66     // destroy remote RSProxyRenderNode, NOT the target node.
67     std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeDestroy>(proxyNodeId_);
68     transactionProxy->AddCommand(command, IsUniRenderEnabled());
69 
70     // destroy corresponding RSProxyRenderNode in RS even if uni render not enabled.
71     if (!IsUniRenderEnabled()) {
72         command = std::make_unique<RSBaseNodeDestroy>(proxyNodeId_);
73         transactionProxy->AddCommand(command, true);
74     }
75 
76     ROSEN_LOGD("RSProxyNode::~RSProxyNode, id:%{public}" PRIu64, GetId());
77 }
78 
RSProxyNode(NodeId targetNodeId,std::string name)79 RSProxyNode::RSProxyNode(NodeId targetNodeId, std::string name) : RSNode(true), name_(std::move(name))
80 {
81     // hacky trick: replace self node id with provided targetNodeId, use self generated id as proxyNodeId
82     proxyNodeId_ = GetId();
83     SetId(targetNodeId);
84     // disable base node destructor, we will destroy the proxy node in the destructor of this class.
85     skipDestroyCommandInDestructor_ = true;
86 }
87 
ResetContextVariableCache() const88 void RSProxyNode::ResetContextVariableCache() const
89 {
90     // reset context variable cache in RSProxyRenderNode, make sure next visit will flush correct context variables.
91     auto transactionProxy = RSTransactionProxy::GetInstance();
92     if (transactionProxy == nullptr) {
93         return;
94     }
95     // send command to proxy node, not the target node
96     std::unique_ptr<RSCommand> command = std::make_unique<RSProxyNodeResetContextVariableCache>(proxyNodeId_);
97     transactionProxy->AddCommand(command, IsUniRenderEnabled());
98 }
AddChild(std::shared_ptr<RSBaseNode> child,int index)99 void RSProxyNode::AddChild(std::shared_ptr<RSBaseNode> child, int index)
100 {
101     // RSProxyNode::AddChild for proxyNode is not allowed
102 }
103 
RemoveChild(std::shared_ptr<RSBaseNode> child)104 void RSProxyNode::RemoveChild(std::shared_ptr<RSBaseNode> child)
105 {
106     // RSProxyNode::RemoveChild for proxyNode is not allowed
107 }
108 
ClearChildren()109 void RSProxyNode::ClearChildren()
110 {
111     // RSProxyNode::ClearChildren for proxyNode is not allowed
112 }
113 } // namespace Rosen
114 } // namespace OHOS
115