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