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 "pipeline/rs_proxy_render_node.h"
17
18 #include "command/rs_surface_node_command.h"
19 #include "pipeline/rs_surface_render_node.h"
20 #include "platform/common/rs_log.h"
21 #include "visitor/rs_node_visitor.h"
22
23 namespace OHOS {
24 namespace Rosen {
RSProxyRenderNode(NodeId id,std::weak_ptr<RSSurfaceRenderNode> target,NodeId targetId,const std::weak_ptr<RSContext> & context)25 RSProxyRenderNode::RSProxyRenderNode(
26 NodeId id, std::weak_ptr<RSSurfaceRenderNode> target, NodeId targetId, const std::weak_ptr<RSContext>& context)
27 : RSRenderNode(id, context), target_(target), targetId_(targetId)
28 {
29 #ifndef ROSEN_ARKUI_X
30 MemoryInfo info = {sizeof(*this), ExtractPid(id), id, MEMORY_TYPE::MEM_RENDER_NODE};
31 MemoryTrack::Instance().AddNodeRecord(id, info);
32 #endif
33 MemorySnapshot::Instance().AddCpuMemory(ExtractPid(id), sizeof(*this));
34 }
35
~RSProxyRenderNode()36 RSProxyRenderNode::~RSProxyRenderNode()
37 {
38 ROSEN_LOGD("RSProxyRenderNode::~RSProxyRenderNode, proxy id:%{public}" PRIu64 " target:%{public}" PRIu64,
39 GetId(), targetId_);
40 #ifndef ROSEN_ARKUI_X
41 MemoryTrack::Instance().RemoveNodeRecord(GetId());
42 #endif
43 MemorySnapshot::Instance().RemoveCpuMemory(ExtractPid(GetId()), sizeof(*this));
44 CleanUp(true);
45 }
46
Prepare(const std::shared_ptr<RSNodeVisitor> & visitor)47 void RSProxyRenderNode::Prepare(const std::shared_ptr<RSNodeVisitor>& visitor)
48 {
49 if (!visitor) {
50 return;
51 }
52 ApplyModifiers();
53 visitor->PrepareProxyRenderNode(*this);
54 }
55
Process(const std::shared_ptr<RSNodeVisitor> & visitor)56 void RSProxyRenderNode::Process(const std::shared_ptr<RSNodeVisitor>& visitor)
57 {
58 if (!visitor) {
59 return;
60 }
61 RSRenderNode::RenderTraceDebug();
62 visitor->ProcessProxyRenderNode(*this);
63 }
64
SetContextMatrix(const std::optional<Drawing::Matrix> & matrix)65 void RSProxyRenderNode::SetContextMatrix(const std::optional<Drawing::Matrix>& matrix)
66 {
67 if (contextMatrix_ == matrix) {
68 return;
69 }
70 contextMatrix_ = matrix;
71 if (auto target = target_.lock()) {
72 target->SetContextMatrix(matrix, false);
73 return;
74 }
75 // send a Command
76 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetContextMatrix>(targetId_, matrix);
77 SendCommandFromRT(command, GetId());
78 }
79
SetContextAlpha(float alpha)80 void RSProxyRenderNode::SetContextAlpha(float alpha)
81 {
82 if (contextAlpha_ == alpha) {
83 return;
84 }
85 contextAlpha_ = alpha;
86 if (auto target = target_.lock()) {
87 target->SetContextAlpha(alpha, false);
88 return;
89 }
90 // send a Command
91 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetContextAlpha>(targetId_, alpha);
92 SendCommandFromRT(command, GetId());
93 }
94
SetContextClipRegion(const std::optional<Drawing::Rect> & clipRegion)95 void RSProxyRenderNode::SetContextClipRegion(const std::optional<Drawing::Rect>& clipRegion)
96 {
97 if (contextClipRect_ == clipRegion) {
98 return;
99 }
100 contextClipRect_ = clipRegion;
101 if (auto target = target_.lock()) {
102 target->SetContextClipRegion(clipRegion, false);
103 return;
104 }
105 // send a Command
106 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetContextClipRegion>(targetId_, clipRegion);
107 SendCommandFromRT(command, GetId());
108 }
109
ResetContextVariableCache()110 void RSProxyRenderNode::ResetContextVariableCache()
111 {
112 // reset context variable cache, make sure next visit will flush correct context variables.
113 contextAlpha_ = -1.0f;
114 contextMatrix_.reset();
115 contextClipRect_.reset();
116 }
117
OnTreeStateChanged()118 void RSProxyRenderNode::OnTreeStateChanged()
119 {
120 RSRenderNode::OnTreeStateChanged();
121 if (!IsOnTheTree()) {
122 // removed from tree, clean up context variables
123 CleanUp(false);
124 }
125 ResetContextVariableCache();
126 }
127
CleanUp(bool removeModifiers)128 void RSProxyRenderNode::CleanUp(bool removeModifiers)
129 {
130 // if target do not exist (in RT) or already destroyed (in RS), skip reset
131 auto target = target_.lock();
132 if (target == nullptr) {
133 return;
134 }
135
136 // reset target node context properties
137 target->SetContextAlpha(1.0f, false);
138 target->SetContextMatrix(std::nullopt, false);
139 target->SetContextClipRegion(std::nullopt, false);
140
141 if (!removeModifiers) {
142 return;
143 }
144
145 // remove all modifiers and animations added via proxy node
146 const auto pid_of_this_node = ExtractPid(GetId());
147 target->FilterModifiersByPid(pid_of_this_node);
148 target->GetAnimationManager().FilterAnimationByPid(pid_of_this_node);
149 }
150 } // namespace Rosen
151 } // namespace OHOS
152