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 node->SetUIContextToken();
73 return node;
74 }
75
~RSProxyNode()76 RSProxyNode::~RSProxyNode()
77 {
78 ROSEN_LOGD("RSProxyNode::~RSProxyNode, proxy id:%{public}" PRIu64 " target:%{public}" PRIu64,
79 proxyNodeId_, GetId());
80 // destroy remote RSProxyRenderNode, NOT the target node.
81 std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeDestroy>(proxyNodeId_);
82 AddCommand(command, IsUniRenderEnabled());
83 // destroy corresponding RSProxyRenderNode in RS even if uni render not enabled.
84 if (!IsUniRenderEnabled()) {
85 command = std::make_unique<RSBaseNodeDestroy>(proxyNodeId_);
86 AddCommand(command, true);
87 }
88 ROSEN_LOGD("RSProxyNode::~RSProxyNode, id:%{public}" PRIu64, GetId());
89 }
90
RegisterNodeMap()91 void RSProxyNode::RegisterNodeMap()
92 {
93 auto rsContext = GetRSUIContext();
94 if (rsContext == nullptr) {
95 return;
96 }
97 auto& nodeMap = rsContext->GetMutableNodeMap();
98 nodeMap.RegisterNode(shared_from_this());
99 }
100
RSProxyNode(NodeId targetNodeId,std::string name,std::shared_ptr<RSUIContext> rsUIContext)101 RSProxyNode::RSProxyNode(NodeId targetNodeId, std::string name, std::shared_ptr<RSUIContext> rsUIContext)
102 : RSNode(true, false, rsUIContext), name_(std::move(name))
103 {
104 // hacky trick: replace self node id with provided targetNodeId, use self generated id as proxyNodeId
105 proxyNodeId_ = GetId();
106 SetId(targetNodeId);
107 // disable base node destructor, we will destroy the proxy node in the destructor of this class.
108 skipDestroyCommandInDestructor_ = true;
109 }
110
ResetContextVariableCache() const111 void RSProxyNode::ResetContextVariableCache() const
112 {
113 // reset context variable cache in RSProxyRenderNode, make sure next visit will flush correct context variables.
114 // send command to proxy node, not the target node
115 std::unique_ptr<RSCommand> command = std::make_unique<RSProxyNodeResetContextVariableCache>(proxyNodeId_);
116 AddCommand(command, IsUniRenderEnabled());
117 }
118
AddChild(std::shared_ptr<RSBaseNode> child,int index)119 void RSProxyNode::AddChild(std::shared_ptr<RSBaseNode> child, int index)
120 {
121 // RSProxyNode::AddChild for proxyNode is not allowed
122 }
123
RemoveChild(std::shared_ptr<RSBaseNode> child)124 void RSProxyNode::RemoveChild(std::shared_ptr<RSBaseNode> child)
125 {
126 // RSProxyNode::RemoveChild for proxyNode is not allowed
127 }
128
ClearChildren()129 void RSProxyNode::ClearChildren()
130 {
131 // RSProxyNode::ClearChildren for proxyNode is not allowed
132 }
133 } // namespace Rosen
134 } // namespace OHOS
135