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 // create proxy node in RS and RT.
42 std::unique_ptr<RSCommand> command = std::make_unique<RSProxyNodeCreate>(proxyNodeId, targetNodeId);
43 transactionProxy->AddCommand(command, true);
44 std::unique_ptr<RSCommand> extraCommand = std::make_unique<RSProxyNodeCreate>(proxyNodeId, targetNodeId);
45 transactionProxy->AddCommand(extraCommand, false);
46
47 ROSEN_LOGD("RSProxyNode::Create, target node id:%" PRIu64 ", name %s proxy node id %" PRIu64, node->GetId(),
48 node->GetName().c_str(), proxyNodeId);
49
50 return node;
51 }
52
~RSProxyNode()53 RSProxyNode::~RSProxyNode()
54 {
55 ROSEN_LOGD("RSProxyNode::~RSProxyNode, proxy id:%" PRIu64 " target:%" PRIu64, proxyNodeId_, GetId());
56
57 auto transactionProxy = RSTransactionProxy::GetInstance();
58 if (transactionProxy == nullptr) {
59 return;
60 }
61
62 // destroy remote RSProxyRenderNode in RS and RT, NOT the target node.
63 std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeDestroy>(proxyNodeId_);
64 transactionProxy->AddCommand(command, true);
65 std::unique_ptr<RSCommand> extraCommand = std::make_unique<RSBaseNodeDestroy>(proxyNodeId_);
66 transactionProxy->AddCommand(extraCommand, false);
67
68 ROSEN_LOGD("RSProxyNode::~RSProxyNode, id:%" PRIu64, GetId());
69 }
70
RSProxyNode(NodeId targetNodeId,std::string name)71 RSProxyNode::RSProxyNode(NodeId targetNodeId, std::string name) : RSNode(true), name_(std::move(name))
72 {
73 // hacky trick: replace self node id with provided targetNodeId, use self generated id as proxyNodeId
74 proxyNodeId_ = GetId();
75 SetId(targetNodeId);
76 // disable base node destructor, we will destroy the proxy node in the destructor of this class.
77 skipDestroyCommandInDestructor_ = true;
78 }
79
ResetContextVariableCache() const80 void RSProxyNode::ResetContextVariableCache() const
81 {
82 // reset context variable cache in RSProxyRenderNode, make sure next visit will flush correct context variables.
83 auto transactionProxy = RSTransactionProxy::GetInstance();
84 if (transactionProxy == nullptr) {
85 return;
86 }
87 // send command to proxy node, not the target node
88 std::unique_ptr<RSCommand> commandRT = std::make_unique<RSProxyNodeResetContextVariableCache>(proxyNodeId_);
89 transactionProxy->AddCommand(commandRT, IsUniRenderEnabled());
90 if (isRenderServiceNode_) {
91 std::unique_ptr<RSCommand> commandRS = std::make_unique<RSProxyNodeResetContextVariableCache>(proxyNodeId_);
92 transactionProxy->AddCommand(commandRS, false);
93 }
94 }
AddChild(std::shared_ptr<RSBaseNode> child,int index)95 void RSProxyNode::AddChild(std::shared_ptr<RSBaseNode> child, int index)
96 {
97 // RSProxyNode::AddChild for proxyNode is not allowed
98 }
99
RemoveChild(std::shared_ptr<RSBaseNode> child)100 void RSProxyNode::RemoveChild(std::shared_ptr<RSBaseNode> child)
101 {
102 // RSProxyNode::RemoveChild for proxyNode is not allowed
103 }
104
ClearChildren()105 void RSProxyNode::ClearChildren()
106 {
107 // RSProxyNode::ClearChildren for proxyNode is not allowed
108 }
109 } // namespace Rosen
110 } // namespace OHOS
111