1
2 /*
3 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "ui/rs_surface_node.h"
18
19 #include <algorithm>
20 #include <string>
21
22 #include "command/rs_base_node_command.h"
23 #include "command/rs_node_command.h"
24 #include "command/rs_surface_node_command.h"
25 #include "ipc_callbacks/rs_rt_refresh_callback.h"
26 #include "pipeline/rs_node_map.h"
27 #include "pipeline/rs_render_thread.h"
28 #include "platform/common/rs_log.h"
29 #ifndef ROSEN_CROSS_PLATFORM
30 #include "platform/drawing/rs_surface_converter.h"
31 #endif
32 #ifdef NEW_RENDER_CONTEXT
33 #include "render_context_base.h"
34 #else
35 #include "render_context/render_context.h"
36 #endif
37 #include "transaction/rs_render_service_client.h"
38 #include "transaction/rs_transaction_proxy.h"
39 #include "ui/rs_proxy_node.h"
40
41 #ifndef ROSEN_CROSS_PLATFORM
42 #include "surface_utils.h"
43 #endif
44
45 namespace OHOS {
46 namespace Rosen {
Create(const RSSurfaceNodeConfig & surfaceNodeConfig,bool isWindow)47 RSSurfaceNode::SharedPtr RSSurfaceNode::Create(const RSSurfaceNodeConfig& surfaceNodeConfig, bool isWindow)
48 {
49 if (!isWindow) {
50 return Create(surfaceNodeConfig, RSSurfaceNodeType::SELF_DRAWING_NODE, isWindow);
51 }
52 return Create(surfaceNodeConfig, RSSurfaceNodeType::DEFAULT, isWindow);
53 }
54
Create(const RSSurfaceNodeConfig & surfaceNodeConfig,RSSurfaceNodeType type,bool isWindow)55 RSSurfaceNode::SharedPtr RSSurfaceNode::Create(const RSSurfaceNodeConfig& surfaceNodeConfig,
56 RSSurfaceNodeType type, bool isWindow)
57 {
58 auto transactionProxy = RSTransactionProxy::GetInstance();
59 if (transactionProxy == nullptr) {
60 return nullptr;
61 }
62
63 SharedPtr node(new RSSurfaceNode(surfaceNodeConfig, isWindow));
64 RSNodeMap::MutableInstance().RegisterNode(node);
65
66 // create node in RS
67 RSSurfaceRenderNodeConfig config = {
68 .id = node->GetId(),
69 .name = node->name_,
70 .bundleName = node->bundleName_,
71 .additionalData = surfaceNodeConfig.additionalData,
72 .isTextureExportNode = surfaceNodeConfig.isTextureExportNode,
73 .isSync = surfaceNodeConfig.isSync,
74 };
75 config.nodeType = type;
76
77 RS_LOGD("RSSurfaceNode::Create name:%{public}s bundleName: %{public}s type: %{public}hhu "
78 "isWindow %{public}d %{public}d ", config.name.c_str(), config.bundleName.c_str(),
79 config.nodeType, isWindow, node->IsRenderServiceNode());
80
81 if (type == RSSurfaceNodeType::LEASH_WINDOW_NODE && node->IsUniRenderEnabled()) {
82 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeCreateWithConfig>(
83 config.id, config.name, static_cast<uint8_t>(config.nodeType), config.bundleName);
84 transactionProxy->AddCommand(command, isWindow);
85 } else {
86 if (!node->CreateNodeAndSurface(config, surfaceNodeConfig.surfaceId)) {
87 ROSEN_LOGE("RSSurfaceNode::Create, create node and surface failed");
88 return nullptr;
89 }
90 }
91
92 node->SetClipToFrame(true);
93 // create node in RT (only when in divided render and isRenderServiceNode_ == false)
94 // create node in RT if is TextureExport node
95 if (!node->IsRenderServiceNode()) {
96 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeCreate>(node->GetId(),
97 config.nodeType, surfaceNodeConfig.isTextureExportNode);
98 if (surfaceNodeConfig.isTextureExportNode) {
99 transactionProxy->AddCommand(command, false);
100 node->SetSurfaceIdToRenderNode();
101 } else {
102 transactionProxy->AddCommand(command, isWindow);
103 }
104
105 command = std::make_unique<RSSurfaceNodeConnectToNodeInRenderService>(node->GetId());
106 transactionProxy->AddCommand(command, isWindow);
107
108 RSRTRefreshCallback::Instance().SetRefresh([] { RSRenderThread::Instance().RequestNextVSync(); });
109 command = std::make_unique<RSSurfaceNodeSetCallbackForRenderThreadRefresh>(node->GetId(), true);
110 transactionProxy->AddCommand(command, isWindow);
111 node->SetFrameGravity(Gravity::RESIZE);
112 #if defined(USE_SURFACE_TEXTURE) && defined(ROSEN_ANDROID)
113 if (type == RSSurfaceNodeType::SURFACE_TEXTURE_NODE) {
114 RSSurfaceExtConfig config = {
115 .type = RSSurfaceExtType::SURFACE_TEXTURE,
116 .additionalData = nullptr,
117 };
118 node->CreateSurfaceExt(config);
119 }
120 #endif
121 }
122
123 if (node->GetName().find("battery_panel") != std::string::npos ||
124 node->GetName().find("sound_panel") != std::string::npos ||
125 node->GetName().find("RosenWeb") != std::string::npos) {
126 node->SetFrameGravity(Gravity::TOP_LEFT);
127 } else if (!isWindow) {
128 node->SetFrameGravity(Gravity::RESIZE);
129 }
130 ROSEN_LOGD("RsDebug RSSurfaceNode::Create id:%{public}" PRIu64, node->GetId());
131 return node;
132 }
133
CreateNodeInRenderThread()134 void RSSurfaceNode::CreateNodeInRenderThread()
135 {
136 if (!IsRenderServiceNode()) {
137 ROSEN_LOGI("RsDebug RSSurfaceNode::CreateNodeInRenderThread id:%{public}" PRIu64 " already has RT Node",
138 GetId());
139 return;
140 }
141
142 auto transactionProxy = RSTransactionProxy::GetInstance();
143 if (transactionProxy == nullptr) {
144 return;
145 }
146
147 isChildOperationDisallowed_ = true;
148 isRenderServiceNode_ = false;
149 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetSurfaceNodeType>(GetId(),
150 static_cast<uint8_t>(RSSurfaceNodeType::ABILITY_COMPONENT_NODE));
151 transactionProxy->AddCommand(command, true);
152
153 // create node in RT (only when in divided render and isRenderServiceNode_ == false)
154 if (!IsRenderServiceNode()) {
155 command = std::make_unique<RSSurfaceNodeCreate>(GetId(), RSSurfaceNodeType::ABILITY_COMPONENT_NODE, false);
156 transactionProxy->AddCommand(command, false);
157
158 command = std::make_unique<RSSurfaceNodeConnectToNodeInRenderService>(GetId());
159 transactionProxy->AddCommand(command, false);
160
161 RSRTRefreshCallback::Instance().SetRefresh([] { RSRenderThread::Instance().RequestNextVSync(); });
162 command = std::make_unique<RSSurfaceNodeSetCallbackForRenderThreadRefresh>(GetId(), true);
163 transactionProxy->AddCommand(command, false);
164 }
165 }
166
AddChild(std::shared_ptr<RSBaseNode> child,int index)167 void RSSurfaceNode::AddChild(std::shared_ptr<RSBaseNode> child, int index)
168 {
169 if (isChildOperationDisallowed_) {
170 ROSEN_LOGE("RSSurfaceNode::AddChild for non RenderServiceNodeType surfaceNode is not allowed");
171 return;
172 }
173 RSBaseNode::AddChild(child, index);
174 }
175
RemoveChild(std::shared_ptr<RSBaseNode> child)176 void RSSurfaceNode::RemoveChild(std::shared_ptr<RSBaseNode> child)
177 {
178 if (isChildOperationDisallowed_) {
179 ROSEN_LOGE("RSSurfaceNode::RemoveChild for non RenderServiceNodeType surfaceNode is not allowed");
180 return;
181 }
182 RSBaseNode::RemoveChild(child);
183 }
184
ClearChildren()185 void RSSurfaceNode::ClearChildren()
186 {
187 if (isChildOperationDisallowed_) {
188 ROSEN_LOGE("RSSurfaceNode::ClearChildren for non RenderServiceNodeType surfaceNode is not allowed");
189 return;
190 }
191 RSBaseNode::ClearChildren();
192 }
193
GetFollowType() const194 FollowType RSSurfaceNode::GetFollowType() const
195 {
196 if (IsRenderServiceNode()) {
197 return FollowType::NONE;
198 } else {
199 return FollowType::FOLLOW_TO_PARENT;
200 }
201 }
202
MarkUIHidden(bool isHidden)203 void RSSurfaceNode::MarkUIHidden(bool isHidden)
204 {
205 auto transactionProxy = RSTransactionProxy::GetInstance();
206 if (transactionProxy == nullptr) {
207 return;
208 }
209 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeMarkUIHidden>(GetId(), isHidden);
210 transactionProxy->AddCommand(command, IsRenderServiceNode());
211 transactionProxy->FlushImplicitTransaction();
212 }
213
OnBoundsSizeChanged() const214 void RSSurfaceNode::OnBoundsSizeChanged() const
215 {
216 auto bounds = GetStagingProperties().GetBounds();
217 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeUpdateSurfaceDefaultSize>(
218 GetId(), bounds.z_, bounds.w_);
219 auto transactionProxy = RSTransactionProxy::GetInstance();
220 if (transactionProxy != nullptr) {
221 transactionProxy->AddCommand(command, true);
222 }
223 #ifdef ROSEN_CROSS_PLATFORM
224 if (!IsRenderServiceNode()) {
225 std::unique_ptr<RSCommand> commandRt = std::make_unique<RSSurfaceNodeUpdateSurfaceDefaultSize>(
226 GetId(), bounds.z_, bounds.w_);
227 if (transactionProxy != nullptr) {
228 transactionProxy->AddCommand(commandRt, false);
229 }
230 }
231 #endif
232 std::lock_guard<std::mutex> lock(mutex_);
233 if (boundsChangedCallback_) {
234 boundsChangedCallback_(bounds);
235 }
236 }
237
SetSecurityLayer(bool isSecurityLayer)238 void RSSurfaceNode::SetSecurityLayer(bool isSecurityLayer)
239 {
240 isSecurityLayer_ = isSecurityLayer;
241 std::unique_ptr<RSCommand> command =
242 std::make_unique<RSSurfaceNodeSetSecurityLayer>(GetId(), isSecurityLayer);
243 auto transactionProxy = RSTransactionProxy::GetInstance();
244 if (transactionProxy != nullptr) {
245 transactionProxy->AddCommand(command, true);
246 }
247 ROSEN_LOGI("RSSurfaceNode::SetSecurityLayer, surfaceNodeId:[%{public}" PRIu64 "] isSecurityLayer:%{public}s",
248 GetId(), isSecurityLayer ? "true" : "false");
249 }
250
GetSecurityLayer() const251 bool RSSurfaceNode::GetSecurityLayer() const
252 {
253 return isSecurityLayer_;
254 }
255
SetSkipLayer(bool isSkipLayer)256 void RSSurfaceNode::SetSkipLayer(bool isSkipLayer)
257 {
258 isSkipLayer_ = isSkipLayer;
259 std::unique_ptr<RSCommand> command =
260 std::make_unique<RSSurfaceNodeSetSkipLayer>(GetId(), isSkipLayer);
261 auto transactionProxy = RSTransactionProxy::GetInstance();
262 if (transactionProxy != nullptr) {
263 transactionProxy->AddCommand(command, true);
264 }
265 ROSEN_LOGD("RSSurfaceNode::SetSkipLayer, surfaceNodeId:[%" PRIu64 "] isSkipLayer:%s", GetId(),
266 isSkipLayer ? "true" : "false");
267 }
268
GetSkipLayer() const269 bool RSSurfaceNode::GetSkipLayer() const
270 {
271 return isSkipLayer_;
272 }
273
SetFingerprint(bool hasFingerprint)274 void RSSurfaceNode::SetFingerprint(bool hasFingerprint)
275 {
276 hasFingerprint_ = hasFingerprint;
277 std::unique_ptr<RSCommand> command =
278 std::make_unique<RSSurfaceNodeSetFingerprint>(GetId(), hasFingerprint);
279 auto transactionProxy = RSTransactionProxy::GetInstance();
280 if (transactionProxy != nullptr) {
281 transactionProxy->AddCommand(command, true);
282 }
283 ROSEN_LOGD("RSSurfaceNode::SetFingerprint, surfaceNodeId:[%{public}" PRIu64 "] hasFingerprint:%{public}s", GetId(),
284 hasFingerprint ? "true" : "false");
285 }
286
GetFingerprint() const287 bool RSSurfaceNode::GetFingerprint() const
288 {
289 return hasFingerprint_;
290 }
291
SetColorSpace(GraphicColorGamut colorSpace)292 void RSSurfaceNode::SetColorSpace(GraphicColorGamut colorSpace)
293 {
294 colorSpace_ = colorSpace;
295 std::unique_ptr<RSCommand> command =
296 std::make_unique<RSSurfaceNodeSetColorSpace>(GetId(), colorSpace);
297 auto transactionProxy = RSTransactionProxy::GetInstance();
298 if (transactionProxy != nullptr) {
299 transactionProxy->AddCommand(command, true);
300 }
301 }
302
CreateTextExportRenderNodeInRT()303 void RSSurfaceNode::CreateTextExportRenderNodeInRT()
304 {
305 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeCreate>(GetId(),
306 RSSurfaceNodeType::SELF_DRAWING_NODE, true);
307 auto transactionProxy = RSTransactionProxy::GetInstance();
308 if (transactionProxy == nullptr) {
309 return;
310 }
311 transactionProxy->AddCommand(command, false);
312 }
313
SetIsTextureExportNode(bool isTextureExportNode)314 void RSSurfaceNode::SetIsTextureExportNode(bool isTextureExportNode)
315 {
316 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetIsTextureExportNode>(GetId(),
317 isTextureExportNode);
318 auto transactionProxy = RSTransactionProxy::GetInstance();
319 if (transactionProxy == nullptr) {
320 return;
321 }
322 transactionProxy->AddCommand(command, false);
323 // need to reset isTextureExport sign in renderService
324 transactionProxy->AddCommand(command, true);
325 }
326
SetTextureExport(bool isTextureExportNode)327 void RSSurfaceNode::SetTextureExport(bool isTextureExportNode)
328 {
329 if (isTextureExportNode == isTextureExportNode_) {
330 return;
331 }
332 isTextureExportNode_ = isTextureExportNode;
333 if (!isTextureExportNode_) {
334 SetIsTextureExportNode(isTextureExportNode);
335 DoFlushModifier();
336 return;
337 }
338 CreateTextExportRenderNodeInRT();
339 SetIsTextureExportNode(isTextureExportNode);
340 SetSurfaceIdToRenderNode();
341 DoFlushModifier();
342 }
343
SetAbilityBGAlpha(uint8_t alpha)344 void RSSurfaceNode::SetAbilityBGAlpha(uint8_t alpha)
345 {
346 std::unique_ptr<RSCommand> command =
347 std::make_unique<RSSurfaceNodeSetAbilityBGAlpha>(GetId(), alpha);
348 auto transactionProxy = RSTransactionProxy::GetInstance();
349 if (transactionProxy != nullptr) {
350 transactionProxy->AddCommand(command, true);
351 }
352 }
353
SetIsNotifyUIBufferAvailable(bool available)354 void RSSurfaceNode::SetIsNotifyUIBufferAvailable(bool available)
355 {
356 std::unique_ptr<RSCommand> command =
357 std::make_unique<RSSurfaceNodeSetIsNotifyUIBufferAvailable>(GetId(), available);
358 auto transactionProxy = RSTransactionProxy::GetInstance();
359 if (transactionProxy != nullptr) {
360 transactionProxy->AddCommand(command, true);
361 }
362 }
363
SetBufferAvailableCallback(BufferAvailableCallback callback)364 bool RSSurfaceNode::SetBufferAvailableCallback(BufferAvailableCallback callback)
365 {
366 {
367 std::lock_guard<std::mutex> lock(mutex_);
368 callback_ = callback;
369 }
370 auto renderServiceClient =
371 std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient());
372 if (renderServiceClient == nullptr) {
373 return false;
374 }
375 return renderServiceClient->RegisterBufferAvailableListener(GetId(), [weakThis = weak_from_this()]() {
376 auto rsSurfaceNode = RSBaseNode::ReinterpretCast<RSSurfaceNode>(weakThis.lock());
377 if (rsSurfaceNode == nullptr) {
378 ROSEN_LOGE("RSSurfaceNode::SetBufferAvailableCallback this == null");
379 return;
380 }
381 BufferAvailableCallback actualCallback;
382 {
383 std::lock_guard<std::mutex> lock(rsSurfaceNode->mutex_);
384 actualCallback = rsSurfaceNode->callback_;
385 }
386 rsSurfaceNode->bufferAvailable_ = true;
387 if (actualCallback) {
388 actualCallback();
389 }
390 });
391 }
392
IsBufferAvailable() const393 bool RSSurfaceNode::IsBufferAvailable() const
394 {
395 return bufferAvailable_;
396 }
397
SetBoundsChangedCallback(BoundsChangedCallback callback)398 void RSSurfaceNode::SetBoundsChangedCallback(BoundsChangedCallback callback)
399 {
400 std::lock_guard<std::mutex> lock(mutex_);
401 boundsChangedCallback_ = callback;
402 }
403
SetAnimationFinished()404 void RSSurfaceNode::SetAnimationFinished()
405 {
406 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetAnimationFinished>(GetId());
407 auto transactionProxy = RSTransactionProxy::GetInstance();
408 if (transactionProxy != nullptr) {
409 transactionProxy->AddCommand(command, true);
410 transactionProxy->FlushImplicitTransaction();
411 }
412 }
413
Marshalling(Parcel & parcel) const414 bool RSSurfaceNode::Marshalling(Parcel& parcel) const
415 {
416 return parcel.WriteUint64(GetId()) && parcel.WriteString(name_) && parcel.WriteBool(IsRenderServiceNode());
417 }
418
Unmarshalling(Parcel & parcel)419 std::shared_ptr<RSSurfaceNode> RSSurfaceNode::Unmarshalling(Parcel& parcel)
420 {
421 uint64_t id = UINT64_MAX;
422 std::string name;
423 bool isRenderServiceNode = false;
424 if (!(parcel.ReadUint64(id) && parcel.ReadString(name) && parcel.ReadBool(isRenderServiceNode))) {
425 ROSEN_LOGE("RSSurfaceNode::Unmarshalling, read param failed");
426 return nullptr;
427 }
428 RSSurfaceNodeConfig config = { name };
429
430 if (auto prevNode = RSNodeMap::Instance().GetNode(id)) {
431 // if the node id is already in the map, we should not create a new node
432 return prevNode->ReinterpretCastTo<RSSurfaceNode>();
433 }
434
435 SharedPtr surfaceNode(new RSSurfaceNode(config, isRenderServiceNode, id));
436 RSNodeMap::MutableInstance().RegisterNode(surfaceNode);
437
438 // for nodes constructed by unmarshalling, we should not destroy the corresponding render node on destruction
439 surfaceNode->skipDestroyCommandInDestructor_ = true;
440
441 return surfaceNode;
442 }
443
SetSurfaceIdToRenderNode()444 void RSSurfaceNode::SetSurfaceIdToRenderNode()
445 {
446 #ifndef ROSEN_CROSS_PLATFORM
447 auto surface = GetSurface();
448 if (surface) {
449 std::unique_ptr<RSCommand> command = std::make_unique<RSurfaceNodeSetSurfaceId>(GetId(),
450 surface->GetUniqueId());
451 auto transactionProxy = RSTransactionProxy::GetInstance();
452 if (transactionProxy == nullptr) {
453 return;
454 }
455 transactionProxy->AddCommand(command, false);
456 }
457 #endif
458 }
459
UnmarshallingAsProxyNode(Parcel & parcel)460 RSNode::SharedPtr RSSurfaceNode::UnmarshallingAsProxyNode(Parcel& parcel)
461 {
462 uint64_t id = UINT64_MAX;
463 std::string name;
464 bool isRenderServiceNode = false;
465 if (!(parcel.ReadUint64(id) && parcel.ReadString(name) && parcel.ReadBool(isRenderServiceNode))) {
466 ROSEN_LOGE("RSSurfaceNode::Unmarshalling, read param failed");
467 return nullptr;
468 }
469
470 // Create RSProxyNode by unmarshalling RSSurfaceNode, return existing node if it exists in RSNodeMap.
471 return RSProxyNode::Create(id, name);
472 }
473
CreateNode(const RSSurfaceRenderNodeConfig & config)474 bool RSSurfaceNode::CreateNode(const RSSurfaceRenderNodeConfig& config)
475 {
476 return std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient())->
477 CreateNode(config);
478 }
479
CreateNodeAndSurface(const RSSurfaceRenderNodeConfig & config,SurfaceId surfaceId)480 bool RSSurfaceNode::CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config, SurfaceId surfaceId)
481 {
482 if (surfaceId == 0) {
483 surface_ = std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient())->
484 CreateNodeAndSurface(config);
485 } else {
486 #ifndef ROSEN_CROSS_PLATFORM
487 sptr<Surface> surface = SurfaceUtils::GetInstance()->GetSurface(surfaceId);
488 if (surface == nullptr) {
489 ROSEN_LOGE("RSSurfaceNode::CreateNodeAndSurface nodeId is %llu cannot find surface by surfaceId %llu",
490 GetId(), surfaceId);
491 return false;
492 }
493 surface_ = std::static_pointer_cast<RSRenderServiceClient>(
494 RSIRenderClient::CreateRenderServiceClient())->CreateRSSurface(surface);
495 if (surface_ == nullptr) {
496 ROSEN_LOGE("RSSurfaceNode::CreateNodeAndSurface nodeId is %llu creat RSSurface fail", GetId());
497 return false;
498 }
499 #endif
500 }
501 return (surface_ != nullptr);
502 }
503
504 #ifndef ROSEN_CROSS_PLATFORM
GetSurface() const505 sptr<OHOS::Surface> RSSurfaceNode::GetSurface() const
506 {
507 if (surface_ == nullptr) {
508 ROSEN_LOGE("RSSurfaceNode::GetSurface, surface_ is nullptr");
509 return nullptr;
510 }
511 auto ohosSurface = RSSurfaceConverter::ConvertToOhosSurface(surface_);
512 return ohosSurface;
513 }
514 #endif
515
NeedForcedSendToRemote() const516 bool RSSurfaceNode::NeedForcedSendToRemote() const
517 {
518 if (IsRenderServiceNode()) {
519 // Property message should be sent to RenderService.
520 return false;
521 } else {
522 // Only when in divided render and isRenderServiceNode_ == false
523 // property message should be sent to RenderService & RenderThread.
524 return true;
525 }
526 }
527
ResetContextAlpha() const528 void RSSurfaceNode::ResetContextAlpha() const
529 {
530 // temporarily fix: manually set contextAlpha in RT and RS to 0.0f, to avoid residual alpha/context matrix from
531 // previous animation. this value will be overwritten in RenderThreadVisitor::ProcessSurfaceRenderNode.
532 auto transactionProxy = RSTransactionProxy::GetInstance();
533 if (transactionProxy == nullptr) {
534 return;
535 }
536
537 std::unique_ptr<RSCommand> commandRS = std::make_unique<RSSurfaceNodeSetContextAlpha>(GetId(), 0.0f);
538 transactionProxy->AddCommand(commandRS, true);
539 }
540
SetContainerWindow(bool hasContainerWindow,float density)541 void RSSurfaceNode::SetContainerWindow(bool hasContainerWindow, float density)
542 {
543 std::unique_ptr<RSCommand> command =
544 std::make_unique<RSSurfaceNodeSetContainerWindow>(GetId(), hasContainerWindow, density);
545 auto transactionProxy = RSTransactionProxy::GetInstance();
546 if (transactionProxy != nullptr) {
547 transactionProxy->AddCommand(command, true);
548 }
549 }
550
SetWindowId(uint32_t windowId)551 void RSSurfaceNode::SetWindowId(uint32_t windowId)
552 {
553 windowId_ = windowId;
554 }
555
SetFreeze(bool isFreeze)556 void RSSurfaceNode::SetFreeze(bool isFreeze)
557 {
558 if (!IsUniRenderEnabled()) {
559 ROSEN_LOGE("SetFreeze is not supported in separate render");
560 return;
561 }
562 std::unique_ptr<RSCommand> command = std::make_unique<RSSetFreeze>(GetId(), isFreeze);
563 auto transactionProxy = RSTransactionProxy::GetInstance();
564 if (transactionProxy != nullptr) {
565 transactionProxy->AddCommand(command, true);
566 }
567 }
568
SplitSurfaceNodeName(std::string surfaceNodeName)569 std::pair<std::string, std::string> RSSurfaceNode::SplitSurfaceNodeName(std::string surfaceNodeName)
570 {
571 if (auto position = surfaceNodeName.find("#"); position != std::string::npos) {
572 return std::make_pair(surfaceNodeName.substr(0, position), surfaceNodeName.substr(position + 1));
573 }
574 return std::make_pair("", surfaceNodeName);
575 }
576
RSSurfaceNode(const RSSurfaceNodeConfig & config,bool isRenderServiceNode)577 RSSurfaceNode::RSSurfaceNode(const RSSurfaceNodeConfig& config, bool isRenderServiceNode)
578 : RSNode(isRenderServiceNode, config.isTextureExportNode)
579 {
580 auto result = SplitSurfaceNodeName(config.SurfaceNodeName);
581 bundleName_ = result.first;
582 name_ = result.second;
583 }
584
RSSurfaceNode(const RSSurfaceNodeConfig & config,bool isRenderServiceNode,NodeId id)585 RSSurfaceNode::RSSurfaceNode(const RSSurfaceNodeConfig& config, bool isRenderServiceNode, NodeId id)
586 : RSNode(isRenderServiceNode, id, config.isTextureExportNode)
587 {
588 auto result = SplitSurfaceNodeName(config.SurfaceNodeName);
589 bundleName_ = result.first;
590 name_ = result.second;
591 }
592
~RSSurfaceNode()593 RSSurfaceNode::~RSSurfaceNode()
594 {
595 auto transactionProxy = RSTransactionProxy::GetInstance();
596 if (transactionProxy == nullptr) {
597 return;
598 }
599
600 // both divided and unirender need to unregister listener when surfaceNode destroy
601 auto renderServiceClient =
602 std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient());
603 if (renderServiceClient != nullptr) {
604 renderServiceClient->UnregisterBufferAvailableListener(GetId());
605 }
606
607 // For abilityComponent and remote window, we should destroy the corresponding render node in RenderThread
608 // The destructor of render node in RenderService should controlled by application
609 // Command sent only in divided render
610 if (skipDestroyCommandInDestructor_ && !IsUniRenderEnabled()) {
611 std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeDestroy>(GetId());
612 transactionProxy->AddCommand(command, false, FollowType::FOLLOW_TO_PARENT, GetId());
613 return;
614 }
615
616 // For self-drawing surfaceNode, we should destroy the corresponding render node in RenderService
617 // Command sent only in divided render
618 if (!IsRenderServiceNode()) {
619 std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeDestroy>(GetId());
620 transactionProxy->AddCommand(command, false, FollowType::FOLLOW_TO_PARENT, GetId());
621 return;
622 }
623 }
624
AttachToDisplay(uint64_t screenId)625 void RSSurfaceNode::AttachToDisplay(uint64_t screenId)
626 {
627 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeAttachToDisplay>(GetId(), screenId);
628 auto transactionProxy = RSTransactionProxy::GetInstance();
629 if (transactionProxy != nullptr) {
630 transactionProxy->AddCommand(command, IsRenderServiceNode());
631 }
632 }
633
DetachToDisplay(uint64_t screenId)634 void RSSurfaceNode::DetachToDisplay(uint64_t screenId)
635 {
636 std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeDetachToDisplay>(GetId(), screenId);
637 auto transactionProxy = RSTransactionProxy::GetInstance();
638 if (transactionProxy != nullptr) {
639 transactionProxy->AddCommand(command, IsRenderServiceNode());
640 }
641 }
642
SetHardwareEnabled(bool isEnabled,SelfDrawingNodeType selfDrawingType)643 void RSSurfaceNode::SetHardwareEnabled(bool isEnabled, SelfDrawingNodeType selfDrawingType)
644 {
645 auto renderServiceClient =
646 std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient());
647 if (renderServiceClient != nullptr) {
648 renderServiceClient->SetHardwareEnabled(GetId(), isEnabled, selfDrawingType);
649 }
650 }
651
SetBootAnimation(bool isBootAnimation)652 void RSSurfaceNode::SetBootAnimation(bool isBootAnimation)
653 {
654 isBootAnimation_ = isBootAnimation;
655 std::unique_ptr<RSCommand> command =
656 std::make_unique<RSSurfaceNodeSetBootAnimation>(GetId(), isBootAnimation);
657 auto transactionProxy = RSTransactionProxy::GetInstance();
658 if (transactionProxy != nullptr) {
659 transactionProxy->AddCommand(command, true);
660 }
661 ROSEN_LOGD("RSSurfaceNode::SetBootAnimation, surfaceNodeId:[%" PRIu64 "] isBootAnimation:%s",
662 GetId(), isBootAnimation ? "true" : "false");
663 }
664
GetBootAnimation() const665 bool RSSurfaceNode::GetBootAnimation() const
666 {
667 return isBootAnimation_;
668 }
669
670 #ifdef USE_SURFACE_TEXTURE
CreateSurfaceExt(const RSSurfaceExtConfig & config)671 void RSSurfaceNode::CreateSurfaceExt(const RSSurfaceExtConfig& config)
672 {
673 auto texture = surface_->GetSurfaceExt(config);
674 if (texture == nullptr) {
675 texture = surface_->CreateSurfaceExt(config);
676 }
677 if (texture == nullptr) {
678 ROSEN_LOGE("RSSurfaceNode::CreateSurfaceExt failed %{public}" PRIu64 " type %{public}u",
679 GetId(), config.type);
680 return;
681 }
682 ROSEN_LOGD("RSSurfaceNode::CreateSurfaceExt %{public}" PRIu64 " type %{public}u %{public}p",
683 GetId(), config.type, texture.get());
684 std::unique_ptr<RSCommand> command =
685 std::make_unique<RSSurfaceNodeCreateSurfaceExt>(GetId(), texture);
686 auto transactionProxy = RSTransactionProxy::GetInstance();
687 if (transactionProxy != nullptr) {
688 transactionProxy->AddCommand(command, false);
689 }
690 }
691
SetSurfaceTexture(const RSSurfaceExtConfig & config)692 void RSSurfaceNode::SetSurfaceTexture(const RSSurfaceExtConfig& config)
693 {
694 CreateSurfaceExt(config);
695 }
696
MarkUiFrameAvailable(bool available)697 void RSSurfaceNode::MarkUiFrameAvailable(bool available)
698 {
699 std::unique_ptr<RSCommand> command =
700 std::make_unique<RSSurfaceNodeSetIsNotifyUIBufferAvailable>(GetId(), available);
701 auto transactionProxy = RSTransactionProxy::GetInstance();
702 if (transactionProxy != nullptr) {
703 transactionProxy->AddCommand(command, false);
704 }
705 }
706
SetSurfaceTextureAttachCallBack(const RSSurfaceTextureAttachCallBack & attachCallback)707 void RSSurfaceNode::SetSurfaceTextureAttachCallBack(const RSSurfaceTextureAttachCallBack& attachCallback)
708 {
709 RSSurfaceTextureConfig config = {
710 .type = RSSurfaceExtType::SURFACE_TEXTURE,
711 };
712 auto texture = surface_->GetSurfaceExt(config);
713 if (texture) {
714 texture->SetAttachCallback(attachCallback);
715 }
716 }
717
SetSurfaceTextureUpdateCallBack(const RSSurfaceTextureUpdateCallBack & updateCallback)718 void RSSurfaceNode::SetSurfaceTextureUpdateCallBack(const RSSurfaceTextureUpdateCallBack& updateCallback)
719 {
720 RSSurfaceTextureConfig config = {
721 .type = RSSurfaceExtType::SURFACE_TEXTURE,
722 .additionalData = nullptr
723 };
724 auto texture = surface_->GetSurfaceExt(config);
725 if (texture) {
726 texture->SetUpdateCallback(updateCallback);
727 }
728 }
729 #endif
730
SetForeground(bool isForeground)731 void RSSurfaceNode::SetForeground(bool isForeground)
732 {
733 ROSEN_LOGD("RSSurfaceNode::SetForeground, surfaceNodeId:[%" PRIu64 "] isForeground:%s",
734 GetId(), isForeground ? "true" : "false");
735 std::unique_ptr<RSCommand> commandRS =
736 std::make_unique<RSSurfaceNodeSetForeground>(GetId(), isForeground);
737 std::unique_ptr<RSCommand> commandRT =
738 std::make_unique<RSSurfaceNodeSetForeground>(GetId(), isForeground);
739 auto transactionProxy = RSTransactionProxy::GetInstance();
740 if (transactionProxy != nullptr) {
741 transactionProxy->AddCommand(commandRS, true);
742 transactionProxy->AddCommand(commandRT, false);
743 }
744 }
745
SetForceUIFirst(bool forceUIFirst)746 void RSSurfaceNode::SetForceUIFirst(bool forceUIFirst)
747 {
748 std::unique_ptr<RSCommand> command =
749 std::make_unique<RSSurfaceNodeSetForceUIFirst>(GetId(), forceUIFirst);
750 auto transactionProxy = RSTransactionProxy::GetInstance();
751 if (transactionProxy != nullptr) {
752 transactionProxy->AddCommand(command, true);
753 }
754 }
755 } // namespace Rosen
756 } // namespace OHOS
757