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