• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,bool unobscured)57 RSSurfaceNode::SharedPtr RSSurfaceNode::Create(const RSSurfaceNodeConfig& surfaceNodeConfig,
58     RSSurfaceNodeType type, bool isWindow, bool unobscured)
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, unobscured)) {
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 
SetSnapshotSkipLayer(bool isSnapshotSkipLayer)310 void RSSurfaceNode::SetSnapshotSkipLayer(bool isSnapshotSkipLayer)
311 {
312     isSnapshotSkipLayer_ = isSnapshotSkipLayer;
313     std::unique_ptr<RSCommand> command =
314         std::make_unique<RSSurfaceNodeSetSnapshotSkipLayer>(GetId(), isSnapshotSkipLayer);
315     auto transactionProxy = RSTransactionProxy::GetInstance();
316     if (transactionProxy != nullptr) {
317         transactionProxy->AddCommand(command, true);
318     }
319     ROSEN_LOGD("RSSurfaceNode::SetSnapshotSkipLayer, surfaceNodeId:[%" PRIu64 "] isSnapshotSkipLayer:%s", GetId(),
320         isSnapshotSkipLayer ? "true" : "false");
321 }
322 
GetSnapshotSkipLayer() const323 bool RSSurfaceNode::GetSnapshotSkipLayer() const
324 {
325     return isSnapshotSkipLayer_;
326 }
327 
SetFingerprint(bool hasFingerprint)328 void RSSurfaceNode::SetFingerprint(bool hasFingerprint)
329 {
330     hasFingerprint_ = hasFingerprint;
331     std::unique_ptr<RSCommand> command =
332         std::make_unique<RSSurfaceNodeSetFingerprint>(GetId(), hasFingerprint);
333     auto transactionProxy = RSTransactionProxy::GetInstance();
334     if (transactionProxy != nullptr) {
335         transactionProxy->AddCommand(command, true);
336     }
337     ROSEN_LOGD("RSSurfaceNode::SetFingerprint, surfaceNodeId:[%{public}" PRIu64 "] hasFingerprint:%{public}s", GetId(),
338         hasFingerprint ? "true" : "false");
339 }
340 
GetFingerprint() const341 bool RSSurfaceNode::GetFingerprint() const
342 {
343     return hasFingerprint_;
344 }
345 
SetColorSpace(GraphicColorGamut colorSpace)346 void RSSurfaceNode::SetColorSpace(GraphicColorGamut colorSpace)
347 {
348     colorSpace_ = colorSpace;
349     std::unique_ptr<RSCommand> command =
350         std::make_unique<RSSurfaceNodeSetColorSpace>(GetId(), colorSpace);
351     auto transactionProxy = RSTransactionProxy::GetInstance();
352     if (transactionProxy != nullptr) {
353         transactionProxy->AddCommand(command, true);
354     }
355 }
356 
CreateRenderNodeForTextureExportSwitch()357 void RSSurfaceNode::CreateRenderNodeForTextureExportSwitch()
358 {
359     auto transactionProxy = RSTransactionProxy::GetInstance();
360     if (transactionProxy == nullptr) {
361         return;
362     }
363     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeCreate>(GetId(),
364         RSSurfaceNodeType::SELF_DRAWING_NODE, isTextureExportNode_);
365     transactionProxy->AddCommand(command, IsRenderServiceNode());
366     if (!IsRenderServiceNode()) {
367         hasCreateRenderNodeInRT_ = true;
368         command = std::make_unique<RSSurfaceNodeConnectToNodeInRenderService>(GetId());
369         transactionProxy->AddCommand(command, false);
370 
371         RSRTRefreshCallback::Instance().SetRefresh([] { RSRenderThread::Instance().RequestNextVSync(); });
372         command = std::make_unique<RSSurfaceNodeSetCallbackForRenderThreadRefresh>(GetId(), true);
373         transactionProxy->AddCommand(command, false);
374     } else {
375         hasCreateRenderNodeInRS_ = true;
376     }
377 }
378 
SetIsTextureExportNode(bool isTextureExportNode)379 void RSSurfaceNode::SetIsTextureExportNode(bool isTextureExportNode)
380 {
381     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetIsTextureExportNode>(GetId(),
382         isTextureExportNode);
383     auto transactionProxy = RSTransactionProxy::GetInstance();
384     if (transactionProxy == nullptr) {
385         return;
386     }
387     transactionProxy->AddCommand(command, false);
388     // need to reset isTextureExport sign in renderService
389     command = std::make_unique<RSSurfaceNodeSetIsTextureExportNode>(GetId(), isTextureExportNode);
390     transactionProxy->AddCommand(command, true);
391 }
392 
SetTextureExport(bool isTextureExportNode)393 void RSSurfaceNode::SetTextureExport(bool isTextureExportNode)
394 {
395     if (isTextureExportNode == isTextureExportNode_) {
396         return;
397     }
398     isTextureExportNode_ = isTextureExportNode;
399     if (!IsUniRenderEnabled()) {
400         return;
401     }
402     if (!isTextureExportNode_) {
403         SetIsTextureExportNode(isTextureExportNode);
404         DoFlushModifier();
405         return;
406     }
407     CreateRenderNodeForTextureExportSwitch();
408     SetIsTextureExportNode(isTextureExportNode);
409     SetSurfaceIdToRenderNode();
410     DoFlushModifier();
411 }
412 
SetAbilityBGAlpha(uint8_t alpha)413 void RSSurfaceNode::SetAbilityBGAlpha(uint8_t alpha)
414 {
415     std::unique_ptr<RSCommand> command =
416         std::make_unique<RSSurfaceNodeSetAbilityBGAlpha>(GetId(), alpha);
417     auto transactionProxy = RSTransactionProxy::GetInstance();
418     if (transactionProxy != nullptr) {
419         transactionProxy->AddCommand(command, true);
420     }
421 }
422 
SetIsNotifyUIBufferAvailable(bool available)423 void RSSurfaceNode::SetIsNotifyUIBufferAvailable(bool available)
424 {
425     std::unique_ptr<RSCommand> command =
426         std::make_unique<RSSurfaceNodeSetIsNotifyUIBufferAvailable>(GetId(), available);
427     auto transactionProxy = RSTransactionProxy::GetInstance();
428     if (transactionProxy != nullptr) {
429         transactionProxy->AddCommand(command, true);
430     }
431 }
432 
SetBufferAvailableCallback(BufferAvailableCallback callback)433 bool RSSurfaceNode::SetBufferAvailableCallback(BufferAvailableCallback callback)
434 {
435     {
436         std::lock_guard<std::mutex> lock(mutex_);
437         callback_ = callback;
438     }
439     auto renderServiceClient =
440         std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient());
441     if (renderServiceClient == nullptr) {
442         return false;
443     }
444     return renderServiceClient->RegisterBufferAvailableListener(GetId(), [weakThis = weak_from_this()]() {
445         auto rsSurfaceNode = RSBaseNode::ReinterpretCast<RSSurfaceNode>(weakThis.lock());
446         if (rsSurfaceNode == nullptr) {
447             ROSEN_LOGE("RSSurfaceNode::SetBufferAvailableCallback this == null");
448             return;
449         }
450         BufferAvailableCallback actualCallback;
451         {
452             std::lock_guard<std::mutex> lock(rsSurfaceNode->mutex_);
453             actualCallback = rsSurfaceNode->callback_;
454         }
455         rsSurfaceNode->bufferAvailable_ = true;
456         if (actualCallback) {
457             actualCallback();
458         }
459     });
460 }
461 
IsBufferAvailable() const462 bool RSSurfaceNode::IsBufferAvailable() const
463 {
464     return bufferAvailable_;
465 }
466 
SetBoundsChangedCallback(BoundsChangedCallback callback)467 void RSSurfaceNode::SetBoundsChangedCallback(BoundsChangedCallback callback)
468 {
469     std::lock_guard<std::mutex> lock(mutex_);
470     boundsChangedCallback_ = callback;
471 }
472 
SetAnimationFinished()473 void RSSurfaceNode::SetAnimationFinished()
474 {
475     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetAnimationFinished>(GetId());
476     auto transactionProxy = RSTransactionProxy::GetInstance();
477     if (transactionProxy != nullptr) {
478         transactionProxy->AddCommand(command, true);
479         transactionProxy->FlushImplicitTransaction();
480     }
481 }
482 
Marshalling(Parcel & parcel) const483 bool RSSurfaceNode::Marshalling(Parcel& parcel) const
484 {
485     return parcel.WriteUint64(GetId()) && parcel.WriteString(name_) && parcel.WriteBool(IsRenderServiceNode());
486 }
487 
Unmarshalling(Parcel & parcel)488 std::shared_ptr<RSSurfaceNode> RSSurfaceNode::Unmarshalling(Parcel& parcel)
489 {
490     uint64_t id = UINT64_MAX;
491     std::string name;
492     bool isRenderServiceNode = false;
493     if (!(parcel.ReadUint64(id) && parcel.ReadString(name) && parcel.ReadBool(isRenderServiceNode))) {
494         ROSEN_LOGE("RSSurfaceNode::Unmarshalling, read param failed");
495         return nullptr;
496     }
497     RSSurfaceNodeConfig config = { name };
498 
499     if (auto prevNode = RSNodeMap::Instance().GetNode(id)) {
500         // if the node id is already in the map, we should not create a new node
501         return prevNode->ReinterpretCastTo<RSSurfaceNode>();
502     }
503 
504     SharedPtr surfaceNode(new RSSurfaceNode(config, isRenderServiceNode, id));
505     RSNodeMap::MutableInstance().RegisterNode(surfaceNode);
506 
507     // for nodes constructed by unmarshalling, we should not destroy the corresponding render node on destruction
508     surfaceNode->skipDestroyCommandInDestructor_ = true;
509 
510     return surfaceNode;
511 }
512 
SetSurfaceIdToRenderNode()513 void RSSurfaceNode::SetSurfaceIdToRenderNode()
514 {
515 #ifndef ROSEN_CROSS_PLATFORM
516     auto surface = GetSurface();
517     if (surface) {
518         std::unique_ptr<RSCommand> command = std::make_unique<RSurfaceNodeSetSurfaceId>(GetId(),
519             surface->GetUniqueId());
520         auto transactionProxy = RSTransactionProxy::GetInstance();
521         if (transactionProxy == nullptr) {
522             return;
523         }
524         transactionProxy->AddCommand(command, false);
525     }
526 #endif
527 }
528 
UnmarshallingAsProxyNode(Parcel & parcel)529 RSNode::SharedPtr RSSurfaceNode::UnmarshallingAsProxyNode(Parcel& parcel)
530 {
531     uint64_t id = UINT64_MAX;
532     std::string name;
533     bool isRenderServiceNode = false;
534     if (!(parcel.ReadUint64(id) && parcel.ReadString(name) && parcel.ReadBool(isRenderServiceNode))) {
535         ROSEN_LOGE("RSSurfaceNode::Unmarshalling, read param failed");
536         return nullptr;
537     }
538 
539     // Create RSProxyNode by unmarshalling RSSurfaceNode, return existing node if it exists in RSNodeMap.
540     return RSProxyNode::Create(id, name);
541 }
542 
CreateNode(const RSSurfaceRenderNodeConfig & config)543 bool RSSurfaceNode::CreateNode(const RSSurfaceRenderNodeConfig& config)
544 {
545     return std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient())->
546         CreateNode(config);
547 }
548 
CreateNodeAndSurface(const RSSurfaceRenderNodeConfig & config,SurfaceId surfaceId,bool unobscured)549 bool RSSurfaceNode::CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config, SurfaceId surfaceId, bool unobscured)
550 {
551     if (surfaceId == 0) {
552         surface_ = std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient())->
553         CreateNodeAndSurface(config, unobscured);
554     } else {
555 #ifndef ROSEN_CROSS_PLATFORM
556         sptr<Surface> surface = SurfaceUtils::GetInstance()->GetSurface(surfaceId);
557         if (surface == nullptr) {
558             ROSEN_LOGE("RSSurfaceNode::CreateNodeAndSurface nodeId is %{public}" PRIu64
559                        " cannot find surface by surfaceId %{public}" PRIu64 "",
560                 GetId(), surfaceId);
561             return false;
562         }
563         surface_ = std::static_pointer_cast<RSRenderServiceClient>(
564             RSIRenderClient::CreateRenderServiceClient())->CreateRSSurface(surface);
565         if (surface_ == nullptr) {
566             ROSEN_LOGE(
567                 "RSSurfaceNode::CreateNodeAndSurface nodeId is %{public}" PRIu64 " creat RSSurface fail", GetId());
568             return false;
569         }
570 #endif
571     }
572     return (surface_ != nullptr);
573 }
574 
575 #ifndef ROSEN_CROSS_PLATFORM
GetSurface() const576 sptr<OHOS::Surface> RSSurfaceNode::GetSurface() const
577 {
578     if (surface_ == nullptr) {
579         ROSEN_LOGE("RSSurfaceNode::GetSurface, surface_ is nullptr");
580         return nullptr;
581     }
582     auto ohosSurface = RSSurfaceConverter::ConvertToOhosSurface(surface_);
583     return ohosSurface;
584 }
585 #endif
586 
NeedForcedSendToRemote() const587 bool RSSurfaceNode::NeedForcedSendToRemote() const
588 {
589     if (IsRenderServiceNode()) {
590         // Property message should be sent to RenderService.
591         return false;
592     } else {
593         // Only when in divided render and isRenderServiceNode_ == false
594         // property message should be sent to RenderService & RenderThread.
595         return true;
596     }
597 }
598 
ResetContextAlpha() const599 void RSSurfaceNode::ResetContextAlpha() const
600 {
601     // temporarily fix: manually set contextAlpha in RT and RS to 0.0f, to avoid residual alpha/context matrix from
602     // previous animation. this value will be overwritten in RenderThreadVisitor::ProcessSurfaceRenderNode.
603     auto transactionProxy = RSTransactionProxy::GetInstance();
604     if (transactionProxy == nullptr) {
605         return;
606     }
607 
608     std::unique_ptr<RSCommand> commandRS = std::make_unique<RSSurfaceNodeSetContextAlpha>(GetId(), 0.0f);
609     transactionProxy->AddCommand(commandRS, true);
610 }
611 
SetContainerWindow(bool hasContainerWindow,float density)612 void RSSurfaceNode::SetContainerWindow(bool hasContainerWindow, float density)
613 {
614     std::unique_ptr<RSCommand> command =
615         std::make_unique<RSSurfaceNodeSetContainerWindow>(GetId(), hasContainerWindow, density);
616     auto transactionProxy = RSTransactionProxy::GetInstance();
617     if (transactionProxy != nullptr) {
618         transactionProxy->AddCommand(command, true);
619     }
620 }
621 
SetWindowId(uint32_t windowId)622 void RSSurfaceNode::SetWindowId(uint32_t windowId)
623 {
624     windowId_ = windowId;
625 }
626 
SetFreeze(bool isFreeze)627 void RSSurfaceNode::SetFreeze(bool isFreeze)
628 {
629     if (!IsUniRenderEnabled()) {
630         ROSEN_LOGE("SetFreeze is not supported in separate render");
631         return;
632     }
633     std::unique_ptr<RSCommand> command = std::make_unique<RSSetFreeze>(GetId(), isFreeze);
634     auto transactionProxy = RSTransactionProxy::GetInstance();
635     if (transactionProxy != nullptr) {
636         transactionProxy->AddCommand(command, true);
637     }
638 }
639 
SplitSurfaceNodeName(std::string surfaceNodeName)640 std::pair<std::string, std::string> RSSurfaceNode::SplitSurfaceNodeName(std::string surfaceNodeName)
641 {
642     if (auto position = surfaceNodeName.find("#");  position != std::string::npos) {
643         return std::make_pair(surfaceNodeName.substr(0, position), surfaceNodeName.substr(position + 1));
644     }
645     return std::make_pair("", surfaceNodeName);
646 }
647 
RSSurfaceNode(const RSSurfaceNodeConfig & config,bool isRenderServiceNode)648 RSSurfaceNode::RSSurfaceNode(const RSSurfaceNodeConfig& config, bool isRenderServiceNode)
649     : RSNode(isRenderServiceNode, config.isTextureExportNode)
650 {
651     auto result = SplitSurfaceNodeName(config.SurfaceNodeName);
652     bundleName_ = result.first;
653     name_ = result.second;
654 }
655 
RSSurfaceNode(const RSSurfaceNodeConfig & config,bool isRenderServiceNode,NodeId id)656 RSSurfaceNode::RSSurfaceNode(const RSSurfaceNodeConfig& config, bool isRenderServiceNode, NodeId id)
657     : RSNode(isRenderServiceNode, id, config.isTextureExportNode)
658 {
659     auto result = SplitSurfaceNodeName(config.SurfaceNodeName);
660     bundleName_ = result.first;
661     name_ = result.second;
662 }
663 
~RSSurfaceNode()664 RSSurfaceNode::~RSSurfaceNode()
665 {
666     auto transactionProxy = RSTransactionProxy::GetInstance();
667     if (transactionProxy == nullptr) {
668         return;
669     }
670 
671     // For abilityComponent and remote window, we should destroy the corresponding render node in RenderThread
672     // The destructor of render node in RenderService should controlled by application
673     // Command sent only in divided render
674     if (skipDestroyCommandInDestructor_ && !IsUniRenderEnabled()) {
675         std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeDestroy>(GetId());
676         transactionProxy->AddCommand(command, false, FollowType::FOLLOW_TO_PARENT, GetId());
677         return;
678     }
679 
680     auto renderServiceClient =
681         std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient());
682     if (renderServiceClient != nullptr) {
683         renderServiceClient->UnregisterBufferAvailableListener(GetId());
684     }
685 
686     // For self-drawing surfaceNode, we should destroy the corresponding render node in RenderService
687     // Command sent only in divided render
688     if (!IsRenderServiceNode()) {
689         std::unique_ptr<RSCommand> command = std::make_unique<RSBaseNodeDestroy>(GetId());
690         transactionProxy->AddCommand(command, false, FollowType::FOLLOW_TO_PARENT, GetId());
691         return;
692     }
693 }
694 
AttachToDisplay(uint64_t screenId)695 void RSSurfaceNode::AttachToDisplay(uint64_t screenId)
696 {
697     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeAttachToDisplay>(GetId(), screenId);
698     auto transactionProxy = RSTransactionProxy::GetInstance();
699     if (transactionProxy != nullptr) {
700         transactionProxy->AddCommand(command, IsRenderServiceNode());
701         RS_LOGI("RSSurfaceNode:attach to display, node:[name: %{public}s, id: %{public}" PRIu64 "], "
702             "screen id: %{public}" PRIu64, GetName().c_str(), GetId(), screenId);
703         RS_TRACE_NAME_FMT("RSSurfaceNode:attach to display, node:[name: %s, id: %" PRIu64 "], "
704             "screen id: %" PRIu64, GetName().c_str(), GetId(), screenId);
705     }
706 }
707 
DetachToDisplay(uint64_t screenId)708 void RSSurfaceNode::DetachToDisplay(uint64_t screenId)
709 {
710     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeDetachToDisplay>(GetId(), screenId);
711     auto transactionProxy = RSTransactionProxy::GetInstance();
712     if (transactionProxy != nullptr) {
713         transactionProxy->AddCommand(command, IsRenderServiceNode());
714         RS_LOGI("RSSurfaceNode:detach from display, node:[name: %{public}s, id: %{public}" PRIu64 "], "
715             "screen id: %{public}" PRIu64, GetName().c_str(), GetId(), screenId);
716         RS_TRACE_NAME_FMT("RSSurfaceNode:detach from display, node:[name: %s, id: %" PRIu64 "], "
717             "screen id: %" PRIu64, GetName().c_str(), GetId(), screenId);
718     }
719 }
720 
SetHardwareEnabled(bool isEnabled,SelfDrawingNodeType selfDrawingType,bool dynamicHardwareEnable)721 void RSSurfaceNode::SetHardwareEnabled(bool isEnabled, SelfDrawingNodeType selfDrawingType, bool dynamicHardwareEnable)
722 {
723     auto renderServiceClient =
724         std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient());
725     if (renderServiceClient != nullptr) {
726         renderServiceClient->SetHardwareEnabled(GetId(), isEnabled, selfDrawingType, dynamicHardwareEnable);
727     }
728 }
729 
SetForceHardwareAndFixRotation(bool flag)730 void RSSurfaceNode::SetForceHardwareAndFixRotation(bool flag)
731 {
732     std::unique_ptr<RSCommand> command = std::make_unique<RSSurfaceNodeSetHardwareAndFixRotation>(GetId(), flag);
733     auto transactionProxy = RSTransactionProxy::GetInstance();
734     if (transactionProxy != nullptr) {
735         transactionProxy->AddCommand(command, true);
736     }
737 #ifdef ROSEN_OHOS
738     std::lock_guard<std::mutex> lock(apiInitMutex_);
739     static bool apiCompatibleVersionInitialized = false;
740     if (apiCompatibleVersionInitialized) {
741         return;
742     }
743     uint32_t apiCompatibleVersion = RSRenderThreadUtil::GetApiCompatibleVersion();
744     if (apiCompatibleVersion != INVALID_API_COMPATIBLE_VERSION && transactionProxy != nullptr) {
745         std::unique_ptr<RSCommand> command =
746             std::make_unique<RSSurfaceNodeSetApiCompatibleVersion>(GetId(), apiCompatibleVersion);
747         transactionProxy->AddCommand(command, true);
748         apiCompatibleVersionInitialized = true;
749     }
750 #endif
751 }
752 
SetBootAnimation(bool isBootAnimation)753 void RSSurfaceNode::SetBootAnimation(bool isBootAnimation)
754 {
755     isBootAnimation_ = isBootAnimation;
756     std::unique_ptr<RSCommand> command =
757         std::make_unique<RSSurfaceNodeSetBootAnimation>(GetId(), isBootAnimation);
758     auto transactionProxy = RSTransactionProxy::GetInstance();
759     if (transactionProxy != nullptr) {
760         transactionProxy->AddCommand(command, true);
761     }
762     ROSEN_LOGD("RSSurfaceNode::SetBootAnimation, surfaceNodeId:[%" PRIu64 "] isBootAnimation:%s",
763         GetId(), isBootAnimation ? "true" : "false");
764 }
765 
GetBootAnimation() const766 bool RSSurfaceNode::GetBootAnimation() const
767 {
768     return isBootAnimation_;
769 }
770 
771 #ifdef USE_SURFACE_TEXTURE
CreateSurfaceExt(const RSSurfaceExtConfig & config)772 void RSSurfaceNode::CreateSurfaceExt(const RSSurfaceExtConfig& config)
773 {
774     auto texture = surface_->GetSurfaceExt(config);
775     if (texture == nullptr) {
776         texture = surface_->CreateSurfaceExt(config);
777     }
778     if (texture == nullptr) {
779         ROSEN_LOGE("RSSurfaceNode::CreateSurfaceExt failed %{public}" PRIu64 " type %{public}u",
780         GetId(), config.type);
781         return;
782     }
783 #ifdef ROSEN_IOS
784     if (texture->GetSurfaceExtConfig().additionalData == nullptr) {
785         texture->UpdateSurfaceExtConfig(config);
786     }
787 #endif
788     ROSEN_LOGD("RSSurfaceNode::CreateSurfaceExt %{public}" PRIu64 " type %{public}u %{public}p",
789         GetId(), config.type, texture.get());
790     std::unique_ptr<RSCommand> command =
791         std::make_unique<RSSurfaceNodeCreateSurfaceExt>(GetId(), texture);
792     auto transactionProxy = RSTransactionProxy::GetInstance();
793     if (transactionProxy != nullptr) {
794         transactionProxy->AddCommand(command, false);
795     }
796 }
797 
SetSurfaceTexture(const RSSurfaceExtConfig & config)798 void RSSurfaceNode::SetSurfaceTexture(const RSSurfaceExtConfig& config)
799 {
800     CreateSurfaceExt(config);
801 }
802 
MarkUiFrameAvailable(bool available)803 void RSSurfaceNode::MarkUiFrameAvailable(bool available)
804 {
805     std::unique_ptr<RSCommand> command =
806         std::make_unique<RSSurfaceNodeSetIsNotifyUIBufferAvailable>(GetId(), available);
807     auto transactionProxy = RSTransactionProxy::GetInstance();
808     if (transactionProxy != nullptr) {
809         transactionProxy->AddCommand(command, false);
810         transactionProxy->FlushImplicitTransaction();
811     }
812 }
813 
SetSurfaceTextureAttachCallBack(const RSSurfaceTextureAttachCallBack & attachCallback)814 void RSSurfaceNode::SetSurfaceTextureAttachCallBack(const RSSurfaceTextureAttachCallBack& attachCallback)
815 {
816 #if defined(ROSEN_IOS)
817     RSSurfaceTextureConfig config = {
818         .type = RSSurfaceExtType::SURFACE_PLATFORM_TEXTURE,
819     };
820     auto texture = surface_->GetSurfaceExt(config);
821     if (texture) {
822         texture->SetAttachCallback(attachCallback);
823     }
824 #else
825     RSSurfaceTextureConfig config = {
826         .type = RSSurfaceExtType::SURFACE_TEXTURE,
827     };
828     auto texture = surface_->GetSurfaceExt(config);
829     if (texture) {
830         texture->SetAttachCallback(attachCallback);
831     }
832 #endif // ROSEN_IOS
833 }
834 
SetSurfaceTextureUpdateCallBack(const RSSurfaceTextureUpdateCallBack & updateCallback)835 void RSSurfaceNode::SetSurfaceTextureUpdateCallBack(const RSSurfaceTextureUpdateCallBack& updateCallback)
836 {
837 #if defined(ROSEN_IOS)
838     RSSurfaceTextureConfig config = {
839         .type = RSSurfaceExtType::SURFACE_PLATFORM_TEXTURE,
840     };
841     auto texture = surface_->GetSurfaceExt(config);
842     if (texture) {
843         texture->SetUpdateCallback(updateCallback);
844     }
845 #else
846     RSSurfaceTextureConfig config = {
847         .type = RSSurfaceExtType::SURFACE_TEXTURE,
848         .additionalData = nullptr
849     };
850     auto texture = surface_->GetSurfaceExt(config);
851     if (texture) {
852         texture->SetUpdateCallback(updateCallback);
853     }
854 #endif // ROSEN_IOS
855 }
856 
SetSurfaceTextureInitTypeCallBack(const RSSurfaceTextureInitTypeCallBack & initTypeCallback)857 void RSSurfaceNode::SetSurfaceTextureInitTypeCallBack(const RSSurfaceTextureInitTypeCallBack& initTypeCallback)
858 {
859 #if defined(ROSEN_IOS)
860     RSSurfaceTextureConfig config = {
861         .type = RSSurfaceExtType::SURFACE_PLATFORM_TEXTURE,
862     };
863     auto texture = surface_->GetSurfaceExt(config);
864     if (texture) {
865         texture->SetInitTypeCallback(initTypeCallback);
866     }
867 #endif // ROSEN_IOS
868 }
869 #endif
870 
SetForeground(bool isForeground)871 void RSSurfaceNode::SetForeground(bool isForeground)
872 {
873     ROSEN_LOGD("RSSurfaceNode::SetForeground, surfaceNodeId:[%" PRIu64 "] isForeground:%s",
874         GetId(), isForeground ? "true" : "false");
875     std::unique_ptr<RSCommand> commandRS =
876         std::make_unique<RSSurfaceNodeSetForeground>(GetId(), isForeground);
877     std::unique_ptr<RSCommand> commandRT =
878         std::make_unique<RSSurfaceNodeSetForeground>(GetId(), isForeground);
879     auto transactionProxy = RSTransactionProxy::GetInstance();
880     if (transactionProxy != nullptr) {
881         transactionProxy->AddCommand(commandRS, true);
882         transactionProxy->AddCommand(commandRT, false);
883     }
884 }
885 
SetForceUIFirst(bool forceUIFirst)886 void RSSurfaceNode::SetForceUIFirst(bool forceUIFirst)
887 {
888     std::unique_ptr<RSCommand> command =
889         std::make_unique<RSSurfaceNodeSetForceUIFirst>(GetId(), forceUIFirst);
890     auto transactionProxy = RSTransactionProxy::GetInstance();
891     if (transactionProxy != nullptr) {
892         transactionProxy->AddCommand(command, true);
893     }
894 }
895 
SetAncoFlags(uint32_t flags)896 void RSSurfaceNode::SetAncoFlags(uint32_t flags)
897 {
898     std::unique_ptr<RSCommand> command =
899         std::make_unique<RSSurfaceNodeSetAncoFlags>(GetId(), flags);
900     auto transactionProxy = RSTransactionProxy::GetInstance();
901     if (transactionProxy != nullptr) {
902         transactionProxy->AddCommand(command, true);
903     }
904 }
SetHDRPresent(bool hdrPresent,NodeId id)905 void RSSurfaceNode::SetHDRPresent(bool hdrPresent, NodeId id)
906 {
907     std::unique_ptr<RSCommand> command =
908         std::make_unique<RSSurfaceNodeSetHDRPresent>(id, hdrPresent);
909     auto transactionProxy = RSTransactionProxy::GetInstance();
910     if (transactionProxy != nullptr) {
911         ROSEN_LOGD("SetHDRPresent  RSSurfaceNode");
912         transactionProxy->AddCommand(command, true);
913     }
914 }
915 
SetSkipDraw(bool skip)916 void RSSurfaceNode::SetSkipDraw(bool skip)
917 {
918     isSkipDraw_ = skip;
919     std::unique_ptr<RSCommand> command =
920         std::make_unique<RSSurfaceNodeSetSkipDraw>(GetId(), skip);
921     auto transactionProxy = RSTransactionProxy::GetInstance();
922     if (transactionProxy != nullptr) {
923         transactionProxy->AddCommand(command, true);
924     }
925     ROSEN_LOGD("RSSurfaceNode::SetSkipDraw, surfaceNodeId:[%" PRIu64 "] skipdraw:%s", GetId(),
926         skip ? "true" : "false");
927 }
928 
GetSkipDraw() const929 bool RSSurfaceNode::GetSkipDraw() const
930 {
931     return isSkipDraw_;
932 }
933 
SetHidePrivacyContent(bool needHidePrivacyContent)934 RSInterfaceErrorCode RSSurfaceNode::SetHidePrivacyContent(bool needHidePrivacyContent)
935 {
936     auto renderServiceClient =
937         std::static_pointer_cast<RSRenderServiceClient>(RSIRenderClient::CreateRenderServiceClient());
938     if (renderServiceClient != nullptr) {
939         return static_cast<RSInterfaceErrorCode>(
940             renderServiceClient->SetHidePrivacyContent(GetId(), needHidePrivacyContent));
941     }
942     return RSInterfaceErrorCode::UNKNOWN_ERROR;
943 }
944 
SetAbilityState(RSSurfaceNodeAbilityState abilityState)945 void RSSurfaceNode::SetAbilityState(RSSurfaceNodeAbilityState abilityState)
946 {
947     if (abilityState_ == abilityState) {
948         ROSEN_LOGD("RSSurfaceNode::SetAbilityState, surfaceNodeId:[%{public}" PRIu64 "], "
949             "ability state same with before: %{public}s",
950             GetId(), abilityState == RSSurfaceNodeAbilityState::FOREGROUND ? "foreground" : "background");
951     }
952     std::unique_ptr<RSCommand> command =
953         std::make_unique<RSSurfaceNodeSetAbilityState>(GetId(), abilityState);
954     auto transactionProxy = RSTransactionProxy::GetInstance();
955     if (transactionProxy == nullptr) {
956         ROSEN_LOGE("RSSurfaceNode::SetAbilityState, transactionProxy is null!");
957         return;
958     }
959     abilityState_ = abilityState;
960     transactionProxy->AddCommand(command, true);
961     ROSEN_LOGI("RSSurfaceNode::SetAbilityState, surfaceNodeId:[%{public}" PRIu64 "], ability state: %{public}s",
962         GetId(), abilityState_ == RSSurfaceNodeAbilityState::FOREGROUND ? "foreground" : "background");
963 }
964 
GetAbilityState() const965 RSSurfaceNodeAbilityState RSSurfaceNode::GetAbilityState() const
966 {
967     return abilityState_;
968 }
969 
AttachToWindowContainer(ScreenId screenId)970 void RSSurfaceNode::AttachToWindowContainer(ScreenId screenId)
971 {
972     std::unique_ptr<RSCommand> command =
973         std::make_unique<RSSurfaceNodeAttachToWindowContainer>(GetId(), screenId);
974     auto transactionProxy = RSTransactionProxy::GetInstance();
975     if (transactionProxy != nullptr) {
976         transactionProxy->AddCommand(command, true);
977         RS_LOGD("RSSurfaceNode::AttachToWindowContainer: Node: %{public}" PRIu64 ", screenId: %{public}" PRIu64,
978             GetId(), screenId);
979     }
980 }
981 
DetachFromWindowContainer(ScreenId screenId)982 void RSSurfaceNode::DetachFromWindowContainer(ScreenId screenId)
983 {
984     std::unique_ptr<RSCommand> command =
985         std::make_unique<RSSurfaceNodeDetachFromWindowContainer>(GetId(), screenId);
986     auto transactionProxy = RSTransactionProxy::GetInstance();
987     if (transactionProxy != nullptr) {
988         transactionProxy->AddCommand(command, true);
989         RS_LOGD("RSSurfaceNode::DetachFromWindowContainer: Node: %{public}" PRIu64 ", screenId: %{public}" PRIu64,
990             GetId(), screenId);
991     }
992 }
993 } // namespace Rosen
994 } // namespace OHOS
995