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