• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (c) 2021-2025 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 RS_ENABLE_GPU
33 #include "render_context/render_context.h"
34 #endif
35 #include "transaction/rs_render_service_client.h"
36 #include "transaction/rs_transaction_proxy.h"
37 #include "ui/rs_proxy_node.h"
38 #include "rs_trace.h"
39 #include "rs_ui_context.h"
40 
41 #ifndef ROSEN_CROSS_PLATFORM
42 #include "surface_utils.h"
43 #endif
44 
45 namespace OHOS {
46 namespace Rosen {
47 #ifdef ROSEN_OHOS
48 namespace {
49 constexpr uint32_t WATERMARK_NAME_LENGTH_LIMIT = 128;
50 }
51 #endif
Create(const RSSurfaceNodeConfig & surfaceNodeConfig,bool isWindow,std::shared_ptr<RSUIContext> rsUIContext)52 RSSurfaceNode::SharedPtr RSSurfaceNode::Create(
53     const RSSurfaceNodeConfig& surfaceNodeConfig, bool isWindow, std::shared_ptr<RSUIContext> rsUIContext)
54 {
55     if (!isWindow) {
56         return Create(surfaceNodeConfig, RSSurfaceNodeType::SELF_DRAWING_NODE, isWindow, false, rsUIContext);
57     }
58     return Create(surfaceNodeConfig, RSSurfaceNodeType::DEFAULT, isWindow);
59 }
60 
Create(const RSSurfaceNodeConfig & surfaceNodeConfig,RSSurfaceNodeType type,bool isWindow,bool unobscured,std::shared_ptr<RSUIContext> rsUIContext)61 RSSurfaceNode::SharedPtr RSSurfaceNode::Create(const RSSurfaceNodeConfig& surfaceNodeConfig,
62     RSSurfaceNodeType type, bool isWindow, bool unobscured, std::shared_ptr<RSUIContext> rsUIContext)
63 {
64     SharedPtr node(new RSSurfaceNode(surfaceNodeConfig, isWindow, rsUIContext));
65     if (rsUIContext != nullptr) {
66         rsUIContext->GetMutableNodeMap().RegisterNode(node);
67     } else {
68         RSNodeMap::MutableInstance().RegisterNode(node);
69     }
70 
71     // create node in RS
72     RSSurfaceRenderNodeConfig config = {
73         .id = node->GetId(),
74         .name = node->name_,
75         .additionalData = surfaceNodeConfig.additionalData,
76         .isTextureExportNode = surfaceNodeConfig.isTextureExportNode,
77         .isSync = surfaceNodeConfig.isSync,
78         .surfaceWindowType = surfaceNodeConfig.surfaceWindowType,
79     };
80     config.nodeType = type;
81 
82     RS_LOGD("RSSurfaceNode::Create name:%{public}s type: %{public}hhu "
83         "isWindow %{public}d %{public}d ", config.name.c_str(),
84         config.nodeType, isWindow, node->IsRenderServiceNode());
85 
86     if (type == RSSurfaceNodeType::LEASH_WINDOW_NODE && node->IsUniRenderEnabled()) {
87         std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeCreateWithConfig>(
88             config.id, config.name, static_cast<uint8_t>(config.nodeType), config.surfaceWindowType);
89         node->AddCommand(command, isWindow);
90     } else {
91         if (!node->CreateNodeAndSurface(config, surfaceNodeConfig.surfaceId, unobscured)) {
92             ROSEN_LOGE("RSSurfaceNode::Create, create node and surface failed");
93             return nullptr;
94         }
95     }
96 
97     node->SetClipToFrame(true);
98     // create node in RT (only when in divided render and isRenderServiceNode_ == false)
99     // create node in RT if is TextureExport node
100     if (!node->IsRenderServiceNode()) {
101         std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeCreate>(node->GetId(),
102             config.nodeType, surfaceNodeConfig.isTextureExportNode);
103         if (surfaceNodeConfig.isTextureExportNode) {
104             node->AddCommand(command, false);
105             node->SetSurfaceIdToRenderNode();
106         } else {
107             node->AddCommand(command, isWindow);
108         }
109         command = std::make_unique<RSSurfaceNodeConnectToNodeInRenderService>(node->GetId());
110         node->AddCommand(command, isWindow);
111 
112         RSRTRefreshCallback::Instance().SetRefresh([] { RSRenderThread::Instance().RequestNextVSync(); });
113         command = std::make_unique<RSSurfaceNodeSetCallbackForRenderThreadRefresh>(node->GetId(), true);
114         node->AddCommand(command, isWindow);
115         node->SetFrameGravity(Gravity::RESIZE);
116         // codes for arkui-x
117 #if defined(USE_SURFACE_TEXTURE) && defined(ROSEN_ANDROID)
118         if (type == RSSurfaceNodeType::SURFACE_TEXTURE_NODE) {
119             RSSurfaceExtConfig config = {
120                 .type = RSSurfaceExtType::SURFACE_TEXTURE,
121                 .additionalData = nullptr,
122             };
123             node->CreateSurfaceExt(config);
124         }
125 #endif
126         // codes for arkui-x
127 #if defined(USE_SURFACE_TEXTURE) && defined(ROSEN_IOS)
128         if ((type == RSSurfaceNodeType::SURFACE_TEXTURE_NODE) &&
129             (surfaceNodeConfig.SurfaceNodeName == "PlatformViewSurface")) {
130             RSSurfaceExtConfig config = {
131                 .type = RSSurfaceExtType::SURFACE_PLATFORM_TEXTURE,
132                 .additionalData = nullptr,
133             };
134             node->CreateSurfaceExt(config);
135         }
136 #endif
137     }
138 
139     if (node->GetName().find("battery_panel") != std::string::npos ||
140         node->GetName().find("sound_panel") != std::string::npos ||
141         node->GetName().find("RosenWeb") != std::string::npos) {
142         node->SetFrameGravity(Gravity::TOP_LEFT);
143     } else if (!isWindow) {
144         node->SetFrameGravity(Gravity::RESIZE);
145     }
146     ROSEN_LOGD("RsDebug RSSurfaceNode::Create id:%{public}" PRIu64, node->GetId());
147     return node;
148 }
149 
CreateNodeInRenderThread()150 void RSSurfaceNode::CreateNodeInRenderThread()
151 {
152     if (!IsRenderServiceNode()) {
153         ROSEN_LOGI(
154             "RsDebug RSSurfaceNode::CreateNodeInRenderThread id:%{public}" PRIu64 " already has RT Node", GetId());
155         return;
156     }
157 
158     isChildOperationDisallowed_ = true;
159     isRenderServiceNode_ = false;
160     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetSurfaceNodeType>(
161         GetId(), static_cast<uint8_t>(RSSurfaceNodeType::ABILITY_COMPONENT_NODE));
162     AddCommand(command, true);
163 
164     // create node in RT (only when in divided render and isRenderServiceNode_ == false)
165     if (!IsRenderServiceNode()) {
166         command = std::make_unique<RSSurfaceNodeCreate>(GetId(), RSSurfaceNodeType::ABILITY_COMPONENT_NODE, false);
167         AddCommand(command, false);
168 
169         command = std::make_unique<RSSurfaceNodeConnectToNodeInRenderService>(GetId());
170         AddCommand(command, false);
171 
172         RSRTRefreshCallback::Instance().SetRefresh([] { RSRenderThread::Instance().RequestNextVSync(); });
173         command = std::make_unique<RSSurfaceNodeSetCallbackForRenderThreadRefresh>(GetId(), true);
174         AddCommand(command, false);
175     }
176 }
177 
AddChild(std::shared_ptr<RSBaseNode> child,int index)178 void RSSurfaceNode::AddChild(std::shared_ptr<RSBaseNode> child, int index)
179 {
180     if (isChildOperationDisallowed_) {
181         ROSEN_LOGE("RSSurfaceNode::AddChild for non RenderServiceNodeType surfaceNode is not allowed");
182         return;
183     }
184     RSBaseNode::AddChild(child, index);
185 }
186 
RemoveChild(std::shared_ptr<RSBaseNode> child)187 void RSSurfaceNode::RemoveChild(std::shared_ptr<RSBaseNode> child)
188 {
189     if (isChildOperationDisallowed_) {
190         ROSEN_LOGE("RSSurfaceNode::RemoveChild for non RenderServiceNodeType surfaceNode is not allowed");
191         return;
192     }
193     RSBaseNode::RemoveChild(child);
194 }
195 
ClearChildren()196 void RSSurfaceNode::ClearChildren()
197 {
198     if (isChildOperationDisallowed_) {
199         ROSEN_LOGE("RSSurfaceNode::ClearChildren for non RenderServiceNodeType surfaceNode is not allowed");
200         return;
201     }
202     RSBaseNode::ClearChildren();
203 }
204 
GetFollowType() const205 FollowType RSSurfaceNode::GetFollowType() const
206 {
207     if (IsRenderServiceNode()) {
208         return FollowType::NONE;
209     } else {
210         return FollowType::FOLLOW_TO_PARENT;
211     }
212 }
213 
MarkUIHidden(bool isHidden)214 void RSSurfaceNode::MarkUIHidden(bool isHidden)
215 {
216     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeMarkUIHidden>(GetId(), isHidden);
217     auto transaction = GetRSTransaction();
218     if (transaction != nullptr) {
219         transaction->AddCommand(command, IsRenderServiceNode());
220         if (!isTextureExportNode_) {
221             transaction->FlushImplicitTransaction();
222         }
223     } else {
224         auto transactionProxy = RSTransactionProxy::GetInstance();
225         if (transactionProxy != nullptr) {
226             transactionProxy->AddCommand(command, IsRenderServiceNode());
227             if (!isTextureExportNode_) {
228                 transactionProxy->FlushImplicitTransaction();
229             }
230         }
231     }
232 }
233 
OnBoundsSizeChanged() const234 void RSSurfaceNode::OnBoundsSizeChanged() const
235 {
236     auto bounds = GetStagingProperties().GetBounds();
237     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeUpdateSurfaceDefaultSize>(
238         GetId(), bounds.z_, bounds.w_);
239     AddCommand(command, true);
240 #ifdef ROSEN_CROSS_PLATFORM
241     if (!IsRenderServiceNode()) {
242         std::unique_ptr<RSCommand> commandRt = std::make_unique<RSSurfaceNodeUpdateSurfaceDefaultSize>(
243             GetId(), bounds.z_, bounds.w_);
244         AddCommand(commandRt, false);
245     }
246 #endif
247     std::lock_guard<std::mutex> lock(mutex_);
248     if (boundsChangedCallback_) {
249         boundsChangedCallback_(bounds);
250         RS_TRACE_NAME_FMT("node:[name: %s, id: %" PRIu64 ", bounds:%f %f %f %f] already callback",
251             GetName().c_str(), GetId(), bounds.x_, bounds.y_, bounds.z_, bounds.w_);
252     }
253 }
254 
SetLeashPersistentId(LeashPersistentId leashPersistentId)255 void RSSurfaceNode::SetLeashPersistentId(LeashPersistentId leashPersistentId)
256 {
257     leashPersistentId_ = leashPersistentId;
258     std::unique_ptr<RSCommand> command =
259         std::make_unique<RSurfaceNodeSetLeashPersistentId>(GetId(), leashPersistentId);
260     AddCommand(command, true);
261     ROSEN_LOGD("RSSurfaceNode::SetLeashPersistentId, \
262         surfaceNodeId:[%{public}" PRIu64 "] leashPersistentId:[%{public}" PRIu64 "]", GetId(), leashPersistentId);
263 }
264 
GetLeashPersistentId() const265 LeashPersistentId RSSurfaceNode::GetLeashPersistentId() const
266 {
267     return leashPersistentId_;
268 }
269 
SetSecurityLayer(bool isSecurityLayer)270 void RSSurfaceNode::SetSecurityLayer(bool isSecurityLayer)
271 {
272     isSecurityLayer_ = isSecurityLayer;
273     std::unique_ptr<RSCommand> command =
274         std::make_unique<RSSurfaceNodeSetSecurityLayer>(GetId(), isSecurityLayer);
275     AddCommand(command, true);
276     ROSEN_LOGI("RSSurfaceNode::SetSecurityLayer, surfaceNodeId:[%{public}" PRIu64 "] isSecurityLayer:%{public}s",
277         GetId(), isSecurityLayer ? "true" : "false");
278 }
279 
GetSecurityLayer() const280 bool RSSurfaceNode::GetSecurityLayer() const
281 {
282     return isSecurityLayer_;
283 }
284 
SetSkipLayer(bool isSkipLayer)285 void RSSurfaceNode::SetSkipLayer(bool isSkipLayer)
286 {
287     isSkipLayer_ = isSkipLayer;
288     std::unique_ptr<RSCommand> command =
289         std::make_unique<RSSurfaceNodeSetSkipLayer>(GetId(), isSkipLayer);
290     AddCommand(command, true);
291     ROSEN_LOGD("RSSurfaceNode::SetSkipLayer, surfaceNodeId:[%" PRIu64 "] isSkipLayer:%s", GetId(),
292         isSkipLayer ? "true" : "false");
293 }
294 
GetSkipLayer() const295 bool RSSurfaceNode::GetSkipLayer() const
296 {
297     return isSkipLayer_;
298 }
299 
SetSnapshotSkipLayer(bool isSnapshotSkipLayer)300 void RSSurfaceNode::SetSnapshotSkipLayer(bool isSnapshotSkipLayer)
301 {
302     isSnapshotSkipLayer_ = isSnapshotSkipLayer;
303     std::unique_ptr<RSCommand> command =
304         std::make_unique<RSSurfaceNodeSetSnapshotSkipLayer>(GetId(), isSnapshotSkipLayer);
305     AddCommand(command, true);
306     ROSEN_LOGD("RSSurfaceNode::SetSnapshotSkipLayer, surfaceNodeId:[%" PRIu64 "] isSnapshotSkipLayer:%s", GetId(),
307         isSnapshotSkipLayer ? "true" : "false");
308 }
309 
GetSnapshotSkipLayer() const310 bool RSSurfaceNode::GetSnapshotSkipLayer() const
311 {
312     return isSnapshotSkipLayer_;
313 }
314 
SetFingerprint(bool hasFingerprint)315 void RSSurfaceNode::SetFingerprint(bool hasFingerprint)
316 {
317     hasFingerprint_ = hasFingerprint;
318     std::unique_ptr<RSCommand> command =
319         std::make_unique<RSSurfaceNodeSetFingerprint>(GetId(), hasFingerprint);
320     AddCommand(command, true);
321     ROSEN_LOGD("RSSurfaceNode::SetFingerprint, surfaceNodeId:[%{public}" PRIu64 "] hasFingerprint:%{public}s", GetId(),
322         hasFingerprint ? "true" : "false");
323 }
324 
GetFingerprint() const325 bool RSSurfaceNode::GetFingerprint() const
326 {
327     return hasFingerprint_;
328 }
329 
SetColorSpace(GraphicColorGamut colorSpace)330 void RSSurfaceNode::SetColorSpace(GraphicColorGamut colorSpace)
331 {
332     colorSpace_ = colorSpace;
333     std::unique_ptr<RSCommand> command =
334         std::make_unique<RSSurfaceNodeSetColorSpace>(GetId(), colorSpace);
335     AddCommand(command, true);
336 }
337 
CreateRenderNodeForTextureExportSwitch()338 void RSSurfaceNode::CreateRenderNodeForTextureExportSwitch()
339 {
340     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeCreate>(GetId(),
341         RSSurfaceNodeType::SELF_DRAWING_NODE, isTextureExportNode_);
342     AddCommand(command, IsRenderServiceNode());
343     if (!IsRenderServiceNode()) {
344         hasCreateRenderNodeInRT_ = true;
345         command = std::make_unique<RSSurfaceNodeConnectToNodeInRenderService>(GetId());
346         AddCommand(command, false);
347 
348         RSRTRefreshCallback::Instance().SetRefresh([] { RSRenderThread::Instance().RequestNextVSync(); });
349         command = std::make_unique<RSSurfaceNodeSetCallbackForRenderThreadRefresh>(GetId(), true);
350         AddCommand(command, false);
351     } else {
352         hasCreateRenderNodeInRS_ = true;
353     }
354 }
355 
SetIsTextureExportNode(bool isTextureExportNode)356 void RSSurfaceNode::SetIsTextureExportNode(bool isTextureExportNode)
357 {
358     std::unique_ptr<RSCommand> command =
359         std::make_unique<RSSurfaceNodeSetIsTextureExportNode>(GetId(), isTextureExportNode);
360     AddCommand(command, false);
361     // need to reset isTextureExport sign in renderService
362     command = std::make_unique<RSSurfaceNodeSetIsTextureExportNode>(GetId(), isTextureExportNode);
363     AddCommand(command, true);
364 }
365 
SetTextureExport(bool isTextureExportNode)366 void RSSurfaceNode::SetTextureExport(bool isTextureExportNode)
367 {
368     if (isTextureExportNode == isTextureExportNode_) {
369         return;
370     }
371     isTextureExportNode_ = isTextureExportNode;
372     if (!IsUniRenderEnabled()) {
373         return;
374     }
375     if (!isTextureExportNode_) {
376         SetIsTextureExportNode(isTextureExportNode);
377         DoFlushModifier();
378         return;
379     }
380     CreateRenderNodeForTextureExportSwitch();
381     SetIsTextureExportNode(isTextureExportNode);
382     SetSurfaceIdToRenderNode();
383     DoFlushModifier();
384 }
385 
SetAbilityBGAlpha(uint8_t alpha)386 void RSSurfaceNode::SetAbilityBGAlpha(uint8_t alpha)
387 {
388     std::unique_ptr<RSCommand> command =
389         std::make_unique<RSSurfaceNodeSetAbilityBGAlpha>(GetId(), alpha);
390     AddCommand(command, true);
391 }
392 
SetIsNotifyUIBufferAvailable(bool available)393 void RSSurfaceNode::SetIsNotifyUIBufferAvailable(bool available)
394 {
395     std::unique_ptr<RSCommand> command =
396         std::make_unique<RSSurfaceNodeSetIsNotifyUIBufferAvailable>(GetId(), available);
397     AddCommand(command, true);
398 }
399 
SetBufferAvailableCallback(BufferAvailableCallback callback)400 bool RSSurfaceNode::SetBufferAvailableCallback(BufferAvailableCallback callback)
401 {
402     {
403         std::lock_guard<std::mutex> lock(mutex_);
404         callback_ = callback;
405     }
406     auto renderServiceClient =
407         std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient());
408     if (renderServiceClient == nullptr) {
409         return false;
410     }
411     return renderServiceClient->RegisterBufferAvailableListener(GetId(), [weakThis = weak_from_this()]() {
412         auto rsSurfaceNode = RSBaseNode::ReinterpretCast<RSSurfaceNode>(weakThis.lock());
413         if (rsSurfaceNode == nullptr) {
414             ROSEN_LOGE("RSSurfaceNode::SetBufferAvailableCallback this == null");
415             return;
416         }
417         BufferAvailableCallback actualCallback;
418         {
419             std::lock_guard<std::mutex> lock(rsSurfaceNode->mutex_);
420             actualCallback = rsSurfaceNode->callback_;
421         }
422         rsSurfaceNode->bufferAvailable_ = true;
423         if (actualCallback) {
424             actualCallback();
425         }
426     });
427 }
428 
IsBufferAvailable() const429 bool RSSurfaceNode::IsBufferAvailable() const
430 {
431     return bufferAvailable_;
432 }
433 
SetBoundsChangedCallback(BoundsChangedCallback callback)434 void RSSurfaceNode::SetBoundsChangedCallback(BoundsChangedCallback callback)
435 {
436     std::lock_guard<std::mutex> lock(mutex_);
437     boundsChangedCallback_ = callback;
438 }
439 
SetAnimationFinished()440 void RSSurfaceNode::SetAnimationFinished()
441 {
442     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetAnimationFinished>(GetId());
443     auto transaction = GetRSTransaction();
444     if (transaction != nullptr) {
445         transaction->AddCommand(command, true);
446         transaction->FlushImplicitTransaction();
447     } else {
448         auto transactionProxy = RSTransactionProxy::GetInstance();
449         if (transactionProxy != nullptr) {
450             transactionProxy->AddCommand(command, true);
451             transactionProxy->FlushImplicitTransaction();
452         }
453     }
454 }
455 
Marshalling(Parcel & parcel) const456 bool RSSurfaceNode::Marshalling(Parcel& parcel) const
457 {
458     bool flag = parcel.WriteUint64(GetId()) && parcel.WriteString(name_) && parcel.WriteBool(IsRenderServiceNode());
459     if (!flag) {
460         ROSEN_LOGE("RSSurfaceNode::Marshalling failed");
461     }
462     return flag;
463 }
464 
Unmarshalling(Parcel & parcel)465 std::shared_ptr<RSSurfaceNode> RSSurfaceNode::Unmarshalling(Parcel& parcel)
466 {
467     uint64_t id = UINT64_MAX;
468     std::string name;
469     bool isRenderServiceNode = false;
470     if (!(parcel.ReadUint64(id) && parcel.ReadString(name) && parcel.ReadBool(isRenderServiceNode))) {
471         ROSEN_LOGE("RSSurfaceNode::Unmarshalling, read param failed");
472         return nullptr;
473     }
474     RSSurfaceNodeConfig config = { name };
475     RS_LOGI("RSSurfaceNode::Unmarshalling, Node: %{public}" PRIu64 ", Name: %{public}s", id, name.c_str());
476 
477     if (auto prevNode = RSNodeMap::Instance().GetNode(id)) { // Planning
478         RS_LOGW("RSSurfaceNode::Unmarshalling, the node id is already in the map");
479         // if the node id is already in the map, we should not create a new node
480         return prevNode->ReinterpretCastTo<RSSurfaceNode>();
481     }
482 
483     SharedPtr surfaceNode(new RSSurfaceNode(config, isRenderServiceNode, id));
484     RSNodeMap::MutableInstance().RegisterNode(surfaceNode); // Planning
485 
486     // for nodes constructed by unmarshalling, we should not destroy the corresponding render node on destruction
487     surfaceNode->skipDestroyCommandInDestructor_ = true;
488 
489     return surfaceNode;
490 }
491 
SetSurfaceIdToRenderNode()492 void RSSurfaceNode::SetSurfaceIdToRenderNode()
493 {
494 #ifndef ROSEN_CROSS_PLATFORM
495     auto surface = GetSurface();
496     if (surface) {
497         std::unique_ptr<RSCommand> command = std::make_unique<RSurfaceNodeSetSurfaceId>(GetId(),
498             surface->GetUniqueId());
499         AddCommand(command, false);
500     }
501 #endif
502 }
503 
UnmarshallingAsProxyNode(Parcel & parcel)504 RSNode::SharedPtr RSSurfaceNode::UnmarshallingAsProxyNode(Parcel& parcel)
505 {
506     uint64_t id = UINT64_MAX;
507     std::string name;
508     bool isRenderServiceNode = false;
509     if (!(parcel.ReadUint64(id) && parcel.ReadString(name) && parcel.ReadBool(isRenderServiceNode))) {
510         ROSEN_LOGE("RSSurfaceNode::Unmarshalling, read param failed");
511         return nullptr;
512     }
513 
514     // Create RSProxyNode by unmarshalling RSSurfaceNode, return existing node if it exists in RSNodeMap.
515     return RSProxyNode::Create(id, name);
516 }
517 
CreateNode(const RSSurfaceRenderNodeConfig & config)518 bool RSSurfaceNode::CreateNode(const RSSurfaceRenderNodeConfig& config)
519 {
520     return std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient())->
521         CreateNode(config);
522 }
523 
CreateNodeAndSurface(const RSSurfaceRenderNodeConfig & config,SurfaceId surfaceId,bool unobscured)524 bool RSSurfaceNode::CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config, SurfaceId surfaceId, bool unobscured)
525 {
526     if (surfaceId == 0) {
527         surface_ = std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient())->
528         CreateNodeAndSurface(config, unobscured);
529     } else {
530 #ifndef ROSEN_CROSS_PLATFORM
531         sptr<Surface> surface = SurfaceUtils::GetInstance()->GetSurface(surfaceId);
532         if (surface == nullptr) {
533             ROSEN_LOGE("RSSurfaceNode::CreateNodeAndSurface nodeId is %{public}" PRIu64
534                        " cannot find surface by surfaceId %{public}" PRIu64 "",
535                 GetId(), surfaceId);
536             return false;
537         }
538         surface_ = std::static_pointer_cast<RSRenderServiceClient>(
539             RSIRenderClient::CreateRenderServiceClient())->CreateRSSurface(surface);
540         if (surface_ == nullptr) {
541             ROSEN_LOGE(
542                 "RSSurfaceNode::CreateNodeAndSurface nodeId is %{public}" PRIu64 " creat RSSurface fail", GetId());
543             return false;
544         }
545 #endif
546     }
547     return (surface_ != nullptr);
548 }
549 
550 #ifndef ROSEN_CROSS_PLATFORM
GetSurface() const551 sptr<OHOS::Surface> RSSurfaceNode::GetSurface() const
552 {
553     if (surface_ == nullptr) {
554         ROSEN_LOGE("RSSurfaceNode::GetSurface, surface_ is nullptr");
555         return nullptr;
556     }
557     auto ohosSurface = RSSurfaceConverter::ConvertToOhosSurface(surface_);
558     return ohosSurface;
559 }
560 #endif
561 
NeedForcedSendToRemote() const562 bool RSSurfaceNode::NeedForcedSendToRemote() const
563 {
564     if (IsRenderServiceNode()) {
565         // Property message should be sent to RenderService.
566         return false;
567     } else {
568         // Only when in divided render and isRenderServiceNode_ == false
569         // property message should be sent to RenderService & RenderThread.
570         return true;
571     }
572 }
573 
ResetContextAlpha() const574 void RSSurfaceNode::ResetContextAlpha() const
575 {
576     // temporarily fix: manually set contextAlpha in RT and RS to 0.0f, to avoid residual alpha/context matrix from
577     // previous animation. this value will be overwritten in RenderThreadVisitor::ProcessSurfaceRenderNode.
578     std::unique_ptr<RSCommand> commandRS = std::make_unique<RSSurfaceNodeSetContextAlpha>(GetId(), 0.0f);
579     AddCommand(commandRS, true);
580 }
581 
SetContainerWindow(bool hasContainerWindow,RRect rrect)582 void RSSurfaceNode::SetContainerWindow(bool hasContainerWindow, RRect rrect)
583 {
584     std::unique_ptr<RSCommand> command =
585         std::make_unique<RSSurfaceNodeSetContainerWindow>(GetId(), hasContainerWindow, rrect);
586     AddCommand(command, true);
587 }
588 
SetWindowId(uint32_t windowId)589 void RSSurfaceNode::SetWindowId(uint32_t windowId)
590 {
591     windowId_ = windowId;
592 }
593 
SetFreeze(bool isFreeze)594 void RSSurfaceNode::SetFreeze(bool isFreeze)
595 {
596     if (!IsUniRenderEnabled()) {
597         ROSEN_LOGE("SetFreeze is not supported in separate render");
598         return;
599     }
600     std::unique_ptr<RSCommand> command = std::make_unique<RSSetFreeze>(GetId(), isFreeze);
601     AddCommand(command, true);
602 }
603 
SplitSurfaceNodeName(std::string surfaceNodeName)604 std::pair<std::string, std::string> RSSurfaceNode::SplitSurfaceNodeName(std::string surfaceNodeName)
605 {
606     if (auto position = surfaceNodeName.find("#");  position != std::string::npos) {
607         return std::make_pair(surfaceNodeName.substr(0, position), surfaceNodeName.substr(position + 1));
608     }
609     return std::make_pair("", surfaceNodeName);
610 }
611 
RSSurfaceNode(const RSSurfaceNodeConfig & config,bool isRenderServiceNode,std::shared_ptr<RSUIContext> rsUIContext)612 RSSurfaceNode::RSSurfaceNode(
613     const RSSurfaceNodeConfig& config, bool isRenderServiceNode, std::shared_ptr<RSUIContext> rsUIContext)
614     : RSNode(isRenderServiceNode, config.isTextureExportNode, rsUIContext), name_(config.SurfaceNodeName)
615 {}
616 
RSSurfaceNode(const RSSurfaceNodeConfig & config,bool isRenderServiceNode,NodeId id,std::shared_ptr<RSUIContext> rsUIContext)617 RSSurfaceNode::RSSurfaceNode(
618     const RSSurfaceNodeConfig& config, bool isRenderServiceNode, NodeId id, std::shared_ptr<RSUIContext> rsUIContext)
619     : RSNode(isRenderServiceNode, id, config.isTextureExportNode, rsUIContext), name_(config.SurfaceNodeName)
620 {}
621 
~RSSurfaceNode()622 RSSurfaceNode::~RSSurfaceNode()
623 {
624     RS_LOGI("RSSurfaceNode::~RSSurfaceNode, Node: %{public}" PRIu64 ", Name: %{public}s", GetId(), GetName().c_str());
625     // both divided and unirender need to unregister listener when surfaceNode destroy
626     auto renderServiceClient =
627         std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient());
628     if (renderServiceClient != nullptr) {
629         renderServiceClient->UnregisterBufferAvailableListener(GetId());
630     }
631 
632     // For abilityComponent and remote window, we should destroy the corresponding render node in RenderThread
633     // The destructor of render node in RenderService should controlled by application
634     // Command sent only in divided render
635     if (skipDestroyCommandInDestructor_ && !IsUniRenderEnabled()) {
636         std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeDestroy>(GetId());
637         AddCommand(command, false, FollowType::FOLLOW_TO_PARENT, GetId());
638         return;
639     }
640 
641     // For self-drawing surfaceNode, we should destroy the corresponding render node in RenderService
642     // Command sent only in divided render
643     if (!IsRenderServiceNode()) {
644         std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeDestroy>(GetId());
645         AddCommand(command, false, FollowType::FOLLOW_TO_PARENT, GetId());
646         return;
647     }
648 }
649 
AttachToDisplay(uint64_t screenId)650 void RSSurfaceNode::AttachToDisplay(uint64_t screenId)
651 {
652     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeAttachToDisplay>(GetId(), screenId);
653     if (AddCommand(command, IsRenderServiceNode())) {
654         if (strcmp(GetName().c_str(), "pointer window") != 0) {
655             RS_LOGI("RSSurfaceNode:attach to display, node:[name: %{public}s, id: %{public}" PRIu64 "], "
656                 "screen id: %{public}" PRIu64, GetName().c_str(), GetId(), screenId);
657         }
658         RS_TRACE_NAME_FMT("RSSurfaceNode:attach to display, node:[name: %s, id: %" PRIu64 "], "
659             "screen id: %" PRIu64, GetName().c_str(), GetId(), screenId);
660     }
661 }
662 
DetachToDisplay(uint64_t screenId)663 void RSSurfaceNode::DetachToDisplay(uint64_t screenId)
664 {
665     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeDetachToDisplay>(GetId(), screenId);
666     if (AddCommand(command, IsRenderServiceNode())) {
667         RS_LOGI("RSSurfaceNode:detach from display, node:[name: %{public}s, id: %{public}" PRIu64 "], "
668                 "screen id: %{public}" PRIu64,
669             GetName().c_str(), GetId(), screenId);
670         RS_TRACE_NAME_FMT("RSSurfaceNode:detach from display, node:[name: %s, id: %" PRIu64 "], "
671                           "screen id: %" PRIu64,
672             GetName().c_str(), GetId(), screenId);
673     }
674 }
675 
SetHardwareEnabled(bool isEnabled,SelfDrawingNodeType selfDrawingType,bool dynamicHardwareEnable)676 void RSSurfaceNode::SetHardwareEnabled(bool isEnabled, SelfDrawingNodeType selfDrawingType, bool dynamicHardwareEnable)
677 {
678     auto renderServiceClient =
679         std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient());
680     if (renderServiceClient != nullptr) {
681         renderServiceClient->SetHardwareEnabled(GetId(), isEnabled, selfDrawingType, dynamicHardwareEnable);
682     }
683 }
684 
SetForceHardwareAndFixRotation(bool flag)685 void RSSurfaceNode::SetForceHardwareAndFixRotation(bool flag)
686 {
687     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetHardwareAndFixRotation>(GetId(), flag);
688     AddCommand(command, true);
689 }
690 
SetBootAnimation(bool isBootAnimation)691 void RSSurfaceNode::SetBootAnimation(bool isBootAnimation)
692 {
693     isBootAnimation_ = isBootAnimation;
694     std::unique_ptr<RSCommand> command =
695         std::make_unique<RSSurfaceNodeSetBootAnimation>(GetId(), isBootAnimation);
696     AddCommand(command, true);
697     ROSEN_LOGD("RSSurfaceNode::SetBootAnimation, surfaceNodeId:[%" PRIu64 "] isBootAnimation:%s",
698         GetId(), isBootAnimation ? "true" : "false");
699 }
700 
GetBootAnimation() const701 bool RSSurfaceNode::GetBootAnimation() const
702 {
703     return isBootAnimation_;
704 }
705 
SetGlobalPositionEnabled(bool isEnabled)706 void RSSurfaceNode::SetGlobalPositionEnabled(bool isEnabled)
707 {
708     if (isGlobalPositionEnabled_ == isEnabled) {
709         return;
710     }
711 
712     isGlobalPositionEnabled_ = isEnabled;
713     std::unique_ptr<RSCommand> command =
714         std::make_unique<RSSurfaceNodeSetGlobalPositionEnabled>(GetId(), isEnabled);
715     AddCommand(command, true);
716     ROSEN_LOGI("RSSurfaceNode::SetGlobalPositionEnabled, surfaceNodeId:[%" PRIu64 "] isEnabled:%s",
717         GetId(), isEnabled ? "true" : "false");
718 }
719 
GetGlobalPositionEnabled() const720 bool RSSurfaceNode::GetGlobalPositionEnabled() const
721 {
722     return isGlobalPositionEnabled_;
723 }
724 
725 #ifdef USE_SURFACE_TEXTURE
CreateSurfaceExt(const RSSurfaceExtConfig & config)726 void RSSurfaceNode::CreateSurfaceExt(const RSSurfaceExtConfig& config)
727 {
728     auto texture = surface_->GetSurfaceExt(config);
729     if (texture == nullptr) {
730         texture = surface_->CreateSurfaceExt(config);
731     }
732     if (texture == nullptr) {
733         ROSEN_LOGE("RSSurfaceNode::CreateSurfaceExt failed %{public}" PRIu64 " type %{public}u",
734         GetId(), config.type);
735         return;
736     }
737 #ifdef ROSEN_IOS
738     if (texture->GetSurfaceExtConfig().additionalData == nullptr) {
739         texture->UpdateSurfaceExtConfig(config);
740     }
741 #endif
742     ROSEN_LOGD("RSSurfaceNode::CreateSurfaceExt %{public}" PRIu64 " type %{public}u", GetId(), config.type);
743     std::unique_ptr<RSCommand> command =
744         std::make_unique<RSSurfaceNodeCreateSurfaceExt>(GetId(), texture);
745     AddCommand(command, false);
746 }
747 
SetSurfaceTexture(const RSSurfaceExtConfig & config)748 void RSSurfaceNode::SetSurfaceTexture(const RSSurfaceExtConfig& config)
749 {
750     CreateSurfaceExt(config);
751 }
752 
MarkUiFrameAvailable(bool available)753 void RSSurfaceNode::MarkUiFrameAvailable(bool available)
754 {
755     std::unique_ptr<RSCommand> command =
756         std::make_unique<RSSurfaceNodeSetIsNotifyUIBufferAvailable>(GetId(), available);
757     auto transaction = GetRSTransaction();
758     if (transaction != nullptr) {
759         transaction->AddCommand(command, false);
760         transaction->FlushImplicitTransaction();
761     } else {
762         auto transactionProxy = RSTransactionProxy::GetInstance();
763         if (transactionProxy != nullptr) {
764             transactionProxy->AddCommand(command, false);
765             transactionProxy->FlushImplicitTransaction();
766         }
767     }
768 }
769 
SetSurfaceTextureAttachCallBack(const RSSurfaceTextureAttachCallBack & attachCallback)770 void RSSurfaceNode::SetSurfaceTextureAttachCallBack(const RSSurfaceTextureAttachCallBack& attachCallback)
771 {
772 #if defined(ROSEN_IOS)
773     RSSurfaceTextureConfig config = {
774         .type = RSSurfaceExtType::SURFACE_PLATFORM_TEXTURE,
775     };
776     auto texture = surface_->GetSurfaceExt(config);
777     if (texture) {
778         texture->SetAttachCallback(attachCallback);
779     }
780 #else
781     RSSurfaceTextureConfig config = {
782         .type = RSSurfaceExtType::SURFACE_TEXTURE,
783     };
784     auto texture = surface_->GetSurfaceExt(config);
785     if (texture) {
786         texture->SetAttachCallback(attachCallback);
787     }
788 #endif // ROSEN_IOS
789 }
790 
SetSurfaceTextureUpdateCallBack(const RSSurfaceTextureUpdateCallBack & updateCallback)791 void RSSurfaceNode::SetSurfaceTextureUpdateCallBack(const RSSurfaceTextureUpdateCallBack& updateCallback)
792 {
793 #if defined(ROSEN_IOS)
794     RSSurfaceTextureConfig config = {
795         .type = RSSurfaceExtType::SURFACE_PLATFORM_TEXTURE,
796     };
797     auto texture = surface_->GetSurfaceExt(config);
798     if (texture) {
799         texture->SetUpdateCallback(updateCallback);
800     }
801 #else
802     RSSurfaceTextureConfig config = {
803         .type = RSSurfaceExtType::SURFACE_TEXTURE,
804         .additionalData = nullptr
805     };
806     auto texture = surface_->GetSurfaceExt(config);
807     if (texture) {
808         texture->SetUpdateCallback(updateCallback);
809     }
810 #endif // ROSEN_IOS
811 }
812 
SetSurfaceTextureInitTypeCallBack(const RSSurfaceTextureInitTypeCallBack & initTypeCallback)813 void RSSurfaceNode::SetSurfaceTextureInitTypeCallBack(const RSSurfaceTextureInitTypeCallBack& initTypeCallback)
814 {
815 #if defined(ROSEN_IOS)
816     RSSurfaceTextureConfig config = {
817         .type = RSSurfaceExtType::SURFACE_PLATFORM_TEXTURE,
818     };
819     auto texture = surface_->GetSurfaceExt(config);
820     if (texture) {
821         texture->SetInitTypeCallback(initTypeCallback);
822     }
823 #endif // ROSEN_IOS
824 }
825 #endif
826 
SetForeground(bool isForeground)827 void RSSurfaceNode::SetForeground(bool isForeground)
828 {
829     ROSEN_LOGD("RSSurfaceNode::SetForeground, surfaceNodeId:[%" PRIu64 "] isForeground:%s",
830         GetId(), isForeground ? "true" : "false");
831     std::unique_ptr<RSCommand> commandRS =
832         std::make_unique<RSSurfaceNodeSetForeground>(GetId(), isForeground);
833     std::unique_ptr<RSCommand> commandRT =
834         std::make_unique<RSSurfaceNodeSetForeground>(GetId(), isForeground);
835     AddCommand(commandRS, true);
836     AddCommand(commandRT, false);
837 }
838 
SetClonedNodeId(NodeId nodeId)839 void RSSurfaceNode::SetClonedNodeId(NodeId nodeId)
840 {
841     std::unique_ptr<RSCommand> command =
842         std::make_unique<RSSurfaceNodeSetClonedNodeId>(GetId(), nodeId);
843     AddCommand(command, true);
844 }
845 
SetForceUIFirst(bool forceUIFirst)846 void RSSurfaceNode::SetForceUIFirst(bool forceUIFirst)
847 {
848     std::unique_ptr<RSCommand> command =
849         std::make_unique<RSSurfaceNodeSetForceUIFirst>(GetId(), forceUIFirst);
850     AddCommand(command, true);
851 }
852 
SetAncoFlags(uint32_t flags)853 void RSSurfaceNode::SetAncoFlags(uint32_t flags)
854 {
855     std::unique_ptr<RSCommand> command =
856         std::make_unique<RSSurfaceNodeSetAncoFlags>(GetId(), flags);
857     AddCommand(command, true);
858 }
SetHDRPresent(bool hdrPresent,NodeId id)859 void RSSurfaceNode::SetHDRPresent(bool hdrPresent, NodeId id)
860 {
861     std::unique_ptr<RSCommand> command =
862         std::make_unique<RSSurfaceNodeSetHDRPresent>(id, hdrPresent);
863     ROSEN_LOGD("SetHDRPresent  RSSurfaceNode");
864     AddCommand(command, true);
865 }
866 
SetSkipDraw(bool skip)867 void RSSurfaceNode::SetSkipDraw(bool skip)
868 {
869     isSkipDraw_ = skip;
870     std::unique_ptr<RSCommand> command =
871         std::make_unique<RSSurfaceNodeSetSkipDraw>(GetId(), skip);
872     AddCommand(command, true);
873     ROSEN_LOGD("RSSurfaceNode::SetSkipDraw, surfaceNodeId:[%" PRIu64 "] skipdraw:%s", GetId(),
874         skip ? "true" : "false");
875 }
876 
GetSkipDraw() const877 bool RSSurfaceNode::GetSkipDraw() const
878 {
879     return isSkipDraw_;
880 }
881 
RegisterNodeMap()882 void RSSurfaceNode::RegisterNodeMap()
883 {
884     auto rsContext = GetRSUIContext();
885     if (rsContext == nullptr) {
886         return;
887     }
888     auto& nodeMap = rsContext->GetMutableNodeMap();
889     nodeMap.RegisterNode(shared_from_this());
890 }
891 
SetWatermarkEnabled(const std::string & name,bool isEnabled)892 void RSSurfaceNode::SetWatermarkEnabled(const std::string& name, bool isEnabled)
893 {
894 #ifdef ROSEN_OHOS
895     if (name.empty() || name.length() > WATERMARK_NAME_LENGTH_LIMIT) {
896         ROSEN_LOGE("SetWatermarkEnabled name[%{public}s] is error.", name.c_str());
897         return;
898     }
899     std::unique_ptr<RSCommand> command =
900         std::make_unique<RSSurfaceNodeSetWatermarkEnabled>(GetId(), name, isEnabled);
901     ROSEN_LOGI("SetWatermarkEnabled[%{public}s, %{public}" PRIu64 " watermark:%{public}s]",
902         GetName().c_str(), GetId(), name.c_str());
903     AddCommand(command, true);
904 #endif
905 }
906 
SetAbilityState(RSSurfaceNodeAbilityState abilityState)907 void RSSurfaceNode::SetAbilityState(RSSurfaceNodeAbilityState abilityState)
908 {
909     if (abilityState_ == abilityState) {
910         ROSEN_LOGD("RSSurfaceNode::SetAbilityState, surfaceNodeId:[%{public}" PRIu64 "], "
911             "ability state same with before: %{public}s",
912             GetId(), abilityState == RSSurfaceNodeAbilityState::FOREGROUND ? "foreground" : "background");
913     }
914     std::unique_ptr<RSCommand> command =
915         std::make_unique<RSSurfaceNodeSetAbilityState>(GetId(), abilityState);
916     AddCommand(command, true);
917     abilityState_ = abilityState;
918     ROSEN_LOGI("RSSurfaceNode::SetAbilityState, surfaceNodeId:[%{public}" PRIu64 "], ability state: %{public}s",
919         GetId(), abilityState_ == RSSurfaceNodeAbilityState::FOREGROUND ? "foreground" : "background");
920 }
921 
GetAbilityState() const922 RSSurfaceNodeAbilityState RSSurfaceNode::GetAbilityState() const
923 {
924     return abilityState_;
925 }
926 
SetHidePrivacyContent(bool needHidePrivacyContent)927 RSInterfaceErrorCode RSSurfaceNode::SetHidePrivacyContent(bool needHidePrivacyContent)
928 {
929     auto renderServiceClient =
930         std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient());
931     if (renderServiceClient != nullptr) {
932         return static_cast<RSInterfaceErrorCode>(
933             renderServiceClient->SetHidePrivacyContent(GetId(), needHidePrivacyContent));
934     }
935     return RSInterfaceErrorCode::UNKNOWN_ERROR;
936 }
937 
SetHardwareEnableHint(bool enable)938 void RSSurfaceNode::SetHardwareEnableHint(bool enable)
939 {
940     std::unique_ptr<RSCommand> command =
941         std::make_unique<RSSurfaceNodeSetHardwareEnableHint>(GetId(), enable);
942     AddCommand(command, true);
943 }
944 
SetApiCompatibleVersion(uint32_t version)945 void RSSurfaceNode::SetApiCompatibleVersion(uint32_t version)
946 {
947     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetApiCompatibleVersion>(GetId(), version);
948     AddCommand(command, true);
949     RS_LOGD("RSSurfaceNode::SetApiCompatibleVersion: Node: %{public}" PRIu64 ", version: %{public}u", GetId(), version);
950 }
951 
SetSourceVirtualDisplayId(ScreenId screenId)952 void RSSurfaceNode::SetSourceVirtualDisplayId(ScreenId screenId)
953 {
954     std::unique_ptr<RSCommand> command =
955         std::make_unique<RSSurfaceNodeSetSourceVirtualDisplayId>(GetId(), screenId);
956     AddCommand(command, true);
957 }
958 
AttachToWindowContainer(ScreenId screenId)959 void RSSurfaceNode::AttachToWindowContainer(ScreenId screenId)
960 {
961     std::unique_ptr<RSCommand> command =
962         std::make_unique<RSSurfaceNodeAttachToWindowContainer>(GetId(), screenId);
963     AddCommand(command, true);
964     RS_LOGD("RSSurfaceNode::AttachToWindowContainer: Node: %{public}" PRIu64 ", screenId: %{public}" PRIu64,
965         GetId(), screenId);
966 }
967 
DetachFromWindowContainer(ScreenId screenId)968 void RSSurfaceNode::DetachFromWindowContainer(ScreenId screenId)
969 {
970     std::unique_ptr<RSCommand> command =
971         std::make_unique<RSSurfaceNodeDetachFromWindowContainer>(GetId(), screenId);
972     AddCommand(command, true);
973     RS_LOGD("RSSurfaceNode::DetachFromWindowContainer: Node: %{public}" PRIu64 ", screenId: %{public}" PRIu64,
974         GetId(), screenId);
975 }
976 } // namespace Rosen
977 } // namespace OHOS
978