1 /*
2 * Copyright (c) 2021 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_surface_render_node.h"
17
18 #include "command/rs_surface_node_command.h"
19 #include "display_type.h"
20 #include "pipeline/rs_root_render_node.h"
21 #include "platform/common/rs_log.h"
22 #include "transaction/rs_transaction_proxy.h"
23 #include "transaction/rs_render_service_client.h"
24 #include "visitor/rs_node_visitor.h"
25
26 namespace OHOS {
27 namespace Rosen {
RSSurfaceRenderNode(NodeId id,std::weak_ptr<RSContext> context)28 RSSurfaceRenderNode::RSSurfaceRenderNode(NodeId id, std::weak_ptr<RSContext> context) : RSRenderNode(id, context) {}
RSSurfaceRenderNode(const RSSurfaceRenderNodeConfig & config,std::weak_ptr<RSContext> context)29 RSSurfaceRenderNode::RSSurfaceRenderNode(const RSSurfaceRenderNodeConfig& config, std::weak_ptr<RSContext> context)
30 : RSRenderNode(config.id, context), name_(config.name)
31 {}
32
~RSSurfaceRenderNode()33 RSSurfaceRenderNode::~RSSurfaceRenderNode() {}
34
SetConsumer(const sptr<Surface> & consumer)35 void RSSurfaceRenderNode::SetConsumer(const sptr<Surface>& consumer)
36 {
37 consumer_ = consumer;
38 }
39
SetBuffer(const sptr<SurfaceBuffer> & buffer)40 void RSSurfaceRenderNode::SetBuffer(const sptr<SurfaceBuffer>& buffer)
41 {
42 if (buffer_ != nullptr) {
43 preBuffer_ = buffer_;
44 buffer_ = buffer;
45 } else {
46 buffer_ = buffer;
47 }
48 }
49
SetFence(sptr<SyncFence> fence)50 void RSSurfaceRenderNode::SetFence(sptr<SyncFence> fence)
51 {
52 preFence_ = fence_;
53 fence_ = std::move(fence);
54 }
55
SetDamageRegion(const Rect & damage)56 void RSSurfaceRenderNode::SetDamageRegion(const Rect& damage)
57 {
58 damageRect_ = damage;
59 }
60
IncreaseAvailableBuffer()61 void RSSurfaceRenderNode::IncreaseAvailableBuffer()
62 {
63 bufferAvailableCount_++;
64 }
65
ReduceAvailableBuffer()66 int32_t RSSurfaceRenderNode::ReduceAvailableBuffer()
67 {
68 return --bufferAvailableCount_;
69 }
70
Prepare(const std::shared_ptr<RSNodeVisitor> & visitor)71 void RSSurfaceRenderNode::Prepare(const std::shared_ptr<RSNodeVisitor>& visitor)
72 {
73 if (!visitor) {
74 return;
75 }
76 visitor->PrepareSurfaceRenderNode(*this);
77 }
78
Process(const std::shared_ptr<RSNodeVisitor> & visitor)79 void RSSurfaceRenderNode::Process(const std::shared_ptr<RSNodeVisitor>& visitor)
80 {
81 if (!visitor) {
82 return;
83 }
84 visitor->ProcessSurfaceRenderNode(*this);
85 }
86
SetMatrix(const SkMatrix & matrix,bool sendMsg)87 void RSSurfaceRenderNode::SetMatrix(const SkMatrix& matrix, bool sendMsg)
88 {
89 if (matrix_ == matrix) {
90 return;
91 }
92 matrix_ = matrix;
93 if (!sendMsg) {
94 return;
95 }
96 // send a Command
97 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetMatrix>(GetId(), matrix);
98 SendPropertyCommand(command);
99 }
100
GetMatrix() const101 const SkMatrix& RSSurfaceRenderNode::GetMatrix() const
102 {
103 return matrix_;
104 }
105
SetAlpha(float alpha,bool sendMsg)106 void RSSurfaceRenderNode::SetAlpha(float alpha, bool sendMsg)
107 {
108 if (alpha_ == alpha) {
109 return;
110 }
111 alpha_ = alpha;
112 if (!sendMsg) {
113 return;
114 }
115 // send a Command
116 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetAlpha>(GetId(), alpha);
117 SendPropertyCommand(command);
118 }
119
GetAlpha() const120 float RSSurfaceRenderNode::GetAlpha() const
121 {
122 return alpha_;
123 }
124
SetClipRegion(Vector4f clipRegion,bool sendMsg)125 void RSSurfaceRenderNode::SetClipRegion(Vector4f clipRegion, bool sendMsg)
126 {
127 if (clipRect_ == clipRegion) {
128 return;
129 }
130 clipRect_ = clipRegion;
131 if (!sendMsg) {
132 return;
133 }
134 // send a Command
135 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetClipRegion>(GetId(), clipRegion);
136 SendPropertyCommand(command);
137 }
138
SetSecurityLayer(bool isSecurityLayer)139 void RSSurfaceRenderNode::SetSecurityLayer(bool isSecurityLayer)
140 {
141 isSecurityLayer_ = isSecurityLayer;
142 }
143
GetSecurityLayer() const144 bool RSSurfaceRenderNode::GetSecurityLayer() const
145 {
146 return isSecurityLayer_;
147 }
148
SetGlobalZOrder(float globalZOrder)149 void RSSurfaceRenderNode::SetGlobalZOrder(float globalZOrder)
150 {
151 globalZOrder_ = globalZOrder;
152 }
153
GetGlobalZOrder() const154 float RSSurfaceRenderNode::GetGlobalZOrder() const
155 {
156 return globalZOrder_;
157 }
158
SetParentId(NodeId parentId,bool sendMsg)159 void RSSurfaceRenderNode::SetParentId(NodeId parentId, bool sendMsg)
160 {
161 parentId_ = parentId;
162 if (!sendMsg) {
163 return;
164 }
165 // find parent surface
166 auto node = GetParent().lock();
167 std::unique_ptr<RSCommand> command;
168 while (true) {
169 if (node == nullptr) {
170 return;
171 } else if (auto rootnode = node->ReinterpretCastTo<RSRootRenderNode>()) {
172 command = std::make_unique<RSSurfaceNodeSetParentSurface>(GetId(), rootnode->GetRSSurfaceNodeId());
173 break;
174 } else if (auto surfaceNode = node->ReinterpretCastTo<RSSurfaceRenderNode>()) {
175 command = std::make_unique<RSSurfaceNodeSetParentSurface>(GetId(), surfaceNode->GetId());
176 break;
177 } else {
178 node = node->GetParent().lock();
179 }
180 }
181 // send a Command
182 if (command) {
183 SendPropertyCommand(command);
184 }
185 }
186
GetParentId() const187 NodeId RSSurfaceRenderNode::GetParentId() const
188 {
189 return parentId_;
190 }
191
UpdateSurfaceDefaultSize(float width,float height)192 void RSSurfaceRenderNode::UpdateSurfaceDefaultSize(float width, float height)
193 {
194 consumer_->SetDefaultWidthAndHeight(width, height);
195 }
196
SendPropertyCommand(std::unique_ptr<RSCommand> & command)197 void RSSurfaceRenderNode::SendPropertyCommand(std::unique_ptr<RSCommand>& command)
198 {
199 auto transactionProxy = RSTransactionProxy::GetInstance();
200 if (transactionProxy != nullptr) {
201 transactionProxy->AddCommandFromRT(command);
202 }
203 }
204
GetBlendType()205 BlendType RSSurfaceRenderNode::GetBlendType()
206 {
207 return blendType_;
208 }
209
SetBlendType(BlendType blendType)210 void RSSurfaceRenderNode::SetBlendType(BlendType blendType)
211 {
212 blendType_ = blendType;
213 }
214
RegisterBufferAvailableListener(sptr<RSIBufferAvailableCallback> callback)215 void RSSurfaceRenderNode::RegisterBufferAvailableListener(sptr<RSIBufferAvailableCallback> callback)
216 {
217 {
218 std::lock_guard<std::mutex> lock(mutex_);
219 callback_ = callback;
220 }
221 }
222
ConnectToNodeInRenderService()223 void RSSurfaceRenderNode::ConnectToNodeInRenderService()
224 {
225 ROSEN_LOGI("RSSurfaceRenderNode::ConnectToNodeInRenderService nodeId = %llu", this->GetId());
226 auto renderServiceClient =
227 std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient());
228 if (renderServiceClient != nullptr) {
229 renderServiceClient->RegisterBufferAvailableListener(
230 GetId(), [weakThis = weak_from_this()](bool isBufferAvailable) {
231 auto node = RSBaseRenderNode::ReinterpretCast<RSSurfaceRenderNode>(weakThis.lock());
232 if (node == nullptr) {
233 return;
234 }
235 node->NotifyBufferAvailable(isBufferAvailable);
236 });
237 }
238 }
239
NotifyBufferAvailable(bool isBufferAvailable)240 void RSSurfaceRenderNode::NotifyBufferAvailable(bool isBufferAvailable)
241 {
242 ROSEN_LOGI("RSSurfaceRenderNode::NotifyBufferAvailable nodeId = %llu", this->GetId());
243
244 // In RS, "isBufferAvailable_ = true" means buffer is ready and need to trigger ipc callback.
245 // In RT, "isBufferAvailable_ = true" means RT know that RS have had available buffer
246 // and ready to trigger "callbackForRenderThreadRefresh_" to "clip" on parent surface.
247 isBufferAvailable_ = isBufferAvailable;
248
249 if (isBufferAvailable == true && callbackForRenderThreadRefresh_ != nullptr) {
250 callbackForRenderThreadRefresh_();
251 }
252
253 {
254 std::lock_guard<std::mutex> lock(mutex_);
255 if (callback_ != nullptr) {
256 callback_->OnBufferAvailable(true);
257 }
258 }
259 }
260
IsBufferAvailable() const261 bool RSSurfaceRenderNode::IsBufferAvailable() const
262 {
263 return isBufferAvailable_;
264 }
265
SetCallbackForRenderThreadRefresh(std::function<void (void)> callback)266 void RSSurfaceRenderNode::SetCallbackForRenderThreadRefresh(std::function<void(void)> callback)
267 {
268 callbackForRenderThreadRefresh_ = callback;
269 }
270
NeedSetCallbackForRenderThreadRefresh()271 bool RSSurfaceRenderNode::NeedSetCallbackForRenderThreadRefresh()
272 {
273 return (callbackForRenderThreadRefresh_ == nullptr);
274 }
275
276 } // namespace Rosen
277 } // namespace OHOS
278