1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "command/rs_surface_node_command.h"
17 
18 #include "command/rs_command_verify_helper.h"
19 #include "common/rs_vector4.h"
20 #include "pipeline/rs_surface_render_node.h"
21 #include "pipeline/rs_display_render_node.h"
22 #include "pipeline/rs_render_node_gc.h"
23 #include "platform/common/rs_log.h"
24 #ifndef ROSEN_CROSS_PLATFORM
25 #include "surface_type.h"
26 #endif
27 
28 namespace OHOS {
29 namespace Rosen {
30 
Create(RSContext & context,NodeId id,RSSurfaceNodeType type,bool isTextureExportNode)31 void SurfaceNodeCommandHelper::Create(RSContext& context, NodeId id, RSSurfaceNodeType type, bool isTextureExportNode)
32 {
33     if (!context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
34         if (!RsCommandVerifyHelper::GetInstance().IsSurfaceNodeCreateCommandVaild(ExtractPid(id))) {
35             ROSEN_LOGI("SurfaceNodeCommandHelper::Create command is not vaild because there "
36             "have been too many surfaceNodes, nodeId:%{public}" PRIu64 "", id);
37             return;
38         }
39         auto node = std::shared_ptr<RSSurfaceRenderNode>(new RSSurfaceRenderNode(id,
40             context.weak_from_this(), isTextureExportNode), RSRenderNodeGC::NodeDestructor);
41         node->SetSurfaceNodeType(type);
42         auto& nodeMap = context.GetMutableNodeMap();
43         nodeMap.RegisterRenderNode(node);
44     }
45 }
46 
CreateWithConfig(RSContext & context,NodeId nodeId,std::string name,uint8_t type,std::string bundleName,enum SurfaceWindowType windowType)47 void SurfaceNodeCommandHelper::CreateWithConfig(
48     RSContext& context, NodeId nodeId, std::string name, uint8_t type,
49     std::string bundleName, enum SurfaceWindowType windowType)
50 {
51     RSSurfaceRenderNodeConfig config = {
52         .id = nodeId, .name = name, .bundleName = bundleName,
53         .nodeType = static_cast<RSSurfaceNodeType>(type), .surfaceWindowType = windowType
54     };
55     if (!RsCommandVerifyHelper::GetInstance().IsSurfaceNodeCreateCommandVaild(ExtractPid(nodeId))) {
56         ROSEN_LOGI("SurfaceNodeCommandHelper::CreateWithConfig command is not vaild because there "
57             "have been too many surfaceNodes, nodeId:%{public}" PRIu64 "", nodeId);
58         return;
59     }
60     auto node = std::shared_ptr<RSSurfaceRenderNode>(new RSSurfaceRenderNode(config,
61         context.weak_from_this()), RSRenderNodeGC::NodeDestructor);
62     context.GetMutableNodeMap().RegisterRenderNode(node);
63 }
64 
CreateWithConfigInRS(const RSSurfaceRenderNodeConfig & config,RSContext & context)65 std::shared_ptr<RSSurfaceRenderNode> SurfaceNodeCommandHelper::CreateWithConfigInRS(
66     const RSSurfaceRenderNodeConfig& config, RSContext& context)
67 {
68     if (!RsCommandVerifyHelper::GetInstance().IsSurfaceNodeCreateCommandVaild(ExtractPid(config.id))) {
69         ROSEN_LOGI("SurfaceNodeCommandHelper::CreateWithConfigInRS command is not vaild because there have "
70             "been too many surfaceNodes, nodeId:%{public}" PRIu64 "", config.id);
71         return nullptr;
72     }
73     auto node = std::shared_ptr<RSSurfaceRenderNode>(new RSSurfaceRenderNode(config,
74         context.weak_from_this()), RSRenderNodeGC::NodeDestructor);
75     return node;
76 }
77 
SetContextMatrix(RSContext & context,NodeId id,const std::optional<Drawing::Matrix> & matrix)78 void SurfaceNodeCommandHelper::SetContextMatrix(
79     RSContext& context, NodeId id, const std::optional<Drawing::Matrix>& matrix)
80 {
81     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
82         node->SetContextMatrix(matrix, false);
83     }
84 }
85 
SetContextAlpha(RSContext & context,NodeId id,float alpha)86 void SurfaceNodeCommandHelper::SetContextAlpha(RSContext& context, NodeId id, float alpha)
87 {
88     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
89         node->SetContextAlpha(alpha, false);
90     }
91 }
92 
SetContextClipRegion(RSContext & context,NodeId id,const std::optional<Drawing::Rect> & clipRect)93 void SurfaceNodeCommandHelper::SetContextClipRegion(
94     RSContext& context, NodeId id, const std::optional<Drawing::Rect>& clipRect)
95 {
96     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
97         node->SetContextClipRegion(clipRect, false);
98     }
99 }
100 
SetSecurityLayer(RSContext & context,NodeId id,bool isSecurityLayer)101 void SurfaceNodeCommandHelper::SetSecurityLayer(RSContext& context, NodeId id, bool isSecurityLayer)
102 {
103     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
104         node->SetSecurityLayer(isSecurityLayer);
105     }
106 }
107 
SetLeashPersistentId(RSContext & context,NodeId id,LeashPersistentId leashPersistentId)108 void SurfaceNodeCommandHelper::SetLeashPersistentId(RSContext& context, NodeId id, LeashPersistentId leashPersistentId)
109 {
110     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
111         node->SetLeashPersistentId(leashPersistentId);
112     }
113 }
114 
SetIsTextureExportNode(RSContext & context,NodeId id,bool isTextureExportNode)115 void SurfaceNodeCommandHelper::SetIsTextureExportNode(RSContext& context, NodeId id, bool isTextureExportNode)
116 {
117     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
118         node->SetIsTextureExportNode(isTextureExportNode);
119     }
120 }
121 
SetSkipLayer(RSContext & context,NodeId id,bool isSkipLayer)122 void SurfaceNodeCommandHelper::SetSkipLayer(RSContext& context, NodeId id, bool isSkipLayer)
123 {
124     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
125         node->SetSkipLayer(isSkipLayer);
126     }
127 }
128 
SetFingerprint(RSContext & context,NodeId id,bool hasFingerprint)129 void SurfaceNodeCommandHelper::SetFingerprint(RSContext& context, NodeId id, bool hasFingerprint)
130 {
131     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
132         node->SetFingerprint(hasFingerprint);
133     }
134 }
135 
SetColorSpace(RSContext & context,NodeId id,GraphicColorGamut colorSpace)136 void SurfaceNodeCommandHelper::SetColorSpace(RSContext& context, NodeId id, GraphicColorGamut colorSpace)
137 {
138     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
139         node->SetColorSpace(colorSpace);
140     }
141 }
142 
UpdateSurfaceDefaultSize(RSContext & context,NodeId id,float width,float height)143 void SurfaceNodeCommandHelper::UpdateSurfaceDefaultSize(RSContext& context, NodeId id, float width, float height)
144 {
145     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
146         node->UpdateSurfaceDefaultSize(width, height);
147     }
148 }
149 
ConnectToNodeInRenderService(RSContext & context,NodeId id)150 void SurfaceNodeCommandHelper::ConnectToNodeInRenderService(RSContext& context, NodeId id)
151 {
152     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
153         node->ConnectToNodeInRenderService();
154     }
155 }
156 
SetCallbackForRenderThreadRefresh(RSContext & context,NodeId id,bool isRefresh)157 void SurfaceNodeCommandHelper::SetCallbackForRenderThreadRefresh(
158     RSContext& context, NodeId id, bool isRefresh)
159 {
160     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
161         if (node->NeedSetCallbackForRenderThreadRefresh()) {
162             node->SetCallbackForRenderThreadRefresh(isRefresh);
163         }
164     }
165 }
166 
SetContextBounds(RSContext & context,NodeId id,Vector4f bounds)167 void SurfaceNodeCommandHelper::SetContextBounds(RSContext& context, NodeId id, Vector4f bounds)
168 {
169     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
170         node->GetMutableRenderProperties().SetBounds(bounds);
171     }
172 }
173 
SetAbilityBGAlpha(RSContext & context,NodeId id,uint8_t alpha)174 void SurfaceNodeCommandHelper::SetAbilityBGAlpha(RSContext& context, NodeId id, uint8_t alpha)
175 {
176     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
177         node->SetAbilityBGAlpha(alpha);
178     }
179 }
180 
SetIsNotifyUIBufferAvailable(RSContext & context,NodeId id,bool available)181 void SurfaceNodeCommandHelper::SetIsNotifyUIBufferAvailable(RSContext& context, NodeId id, bool available)
182 {
183     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
184         node->SetIsNotifyUIBufferAvailable(available);
185     }
186 }
187 
MarkUIHidden(RSContext & context,NodeId id,bool isHidden)188 void SurfaceNodeCommandHelper::MarkUIHidden(RSContext& context, NodeId id, bool isHidden)
189 {
190     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
191         node->MarkUIHidden(isHidden);
192     }
193 }
194 
SetSurfaceNodeType(RSContext & context,NodeId nodeId,uint8_t surfaceNodeType)195 void SurfaceNodeCommandHelper::SetSurfaceNodeType(RSContext& context, NodeId nodeId, uint8_t surfaceNodeType)
196 {
197     auto type = static_cast<RSSurfaceNodeType>(surfaceNodeType);
198     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
199         node->SetSurfaceNodeType(type);
200     }
201 }
202 
SetContainerWindow(RSContext & context,NodeId nodeId,bool hasContainerWindow,float density)203 void SurfaceNodeCommandHelper::SetContainerWindow(
204     RSContext& context, NodeId nodeId, bool hasContainerWindow, float density)
205 {
206     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
207         node->SetContainerWindow(hasContainerWindow, density);
208     }
209 }
210 
SetAnimationFinished(RSContext & context,NodeId nodeId)211 void SurfaceNodeCommandHelper::SetAnimationFinished(RSContext& context, NodeId nodeId)
212 {
213     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
214         node->SetStartAnimationFinished();
215     }
216 }
217 
AttachToDisplay(RSContext & context,NodeId nodeId,uint64_t screenId)218 void SurfaceNodeCommandHelper::AttachToDisplay(RSContext& context, NodeId nodeId, uint64_t screenId)
219 {
220     const auto& nodeMap = context.GetNodeMap();
221     auto surfaceRenderNode = nodeMap.GetRenderNode<RSSurfaceRenderNode>(nodeId);
222     if (surfaceRenderNode == nullptr) {
223         return;
224     }
225     nodeMap.TraverseDisplayNodes(
226         [&surfaceRenderNode, &screenId](const std::shared_ptr<RSDisplayRenderNode>& displayRenderNode) {
227             if (displayRenderNode == nullptr || displayRenderNode->GetScreenId() != screenId ||
228                 displayRenderNode->GetBootAnimation() != surfaceRenderNode->GetBootAnimation()) {
229                 return;
230             }
231             displayRenderNode->AddChild(surfaceRenderNode);
232         });
233 }
234 
DetachToDisplay(RSContext & context,NodeId nodeId,uint64_t screenId)235 void SurfaceNodeCommandHelper::DetachToDisplay(RSContext& context, NodeId nodeId, uint64_t screenId)
236 {
237     const auto& nodeMap = context.GetNodeMap();
238     auto surfaceRenderNode = nodeMap.GetRenderNode<RSSurfaceRenderNode>(nodeId);
239     if (surfaceRenderNode == nullptr) {
240         return;
241     }
242     nodeMap.TraverseDisplayNodes(
243         [&surfaceRenderNode, &screenId](const std::shared_ptr<RSDisplayRenderNode>& displayRenderNode) {
244             if (displayRenderNode == nullptr || displayRenderNode->GetScreenId() != screenId ||
245                 displayRenderNode->GetBootAnimation() != surfaceRenderNode->GetBootAnimation()) {
246                 return;
247             }
248             displayRenderNode->RemoveChild(surfaceRenderNode);
249         });
250 }
251 
SetBootAnimation(RSContext & context,NodeId nodeId,bool isBootAnimation)252 void SurfaceNodeCommandHelper::SetBootAnimation(RSContext& context, NodeId nodeId, bool isBootAnimation)
253 {
254     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
255         node->SetBootAnimation(isBootAnimation);
256     }
257 }
258 
259 #ifdef USE_SURFACE_TEXTURE
CreateSurfaceExt(RSContext & context,NodeId id,const std::shared_ptr<RSSurfaceTexture> & surfaceExt)260 void SurfaceNodeCommandHelper::CreateSurfaceExt(RSContext& context, NodeId id,
261     const std::shared_ptr<RSSurfaceTexture>& surfaceExt)
262 {
263     auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id);
264     if (node != nullptr) {
265         node->SetSurfaceTexture(surfaceExt);
266     }
267 }
268 #endif
269 
SetForceHardwareAndFixRotation(RSContext & context,NodeId nodeId,bool flag)270 void SurfaceNodeCommandHelper::SetForceHardwareAndFixRotation(RSContext& context, NodeId nodeId, bool flag)
271 {
272     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
273         node->SetForceHardwareAndFixRotation(flag);
274     }
275 }
276 
SetForeground(RSContext & context,NodeId nodeId,bool isForeground)277 void SurfaceNodeCommandHelper::SetForeground(RSContext& context, NodeId nodeId, bool isForeground)
278 {
279     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
280         node->SetForeground(isForeground);
281     }
282 }
283 
SetSurfaceId(RSContext & context,NodeId nodeId,SurfaceId surfaceId)284 void SurfaceNodeCommandHelper::SetSurfaceId(RSContext& context, NodeId nodeId, SurfaceId surfaceId)
285 {
286     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
287         node->SetSurfaceId(surfaceId);
288     }
289 }
290 
SetForceUIFirst(RSContext & context,NodeId nodeId,bool forceUIFirst)291 void SurfaceNodeCommandHelper::SetForceUIFirst(RSContext& context, NodeId nodeId, bool forceUIFirst)
292 {
293     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
294         node->SetForceUIFirst(forceUIFirst);
295     }
296 }
297 
SetAncoFlags(RSContext & context,NodeId nodeId,uint32_t flags)298 void SurfaceNodeCommandHelper::SetAncoFlags(RSContext& context, NodeId nodeId, uint32_t flags)
299 {
300     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
301         node->SetAncoFlags(flags);
302     }
303 }
304 
SetHDRPresent(RSContext & context,NodeId nodeId,bool hdrPresent)305 void SurfaceNodeCommandHelper::SetHDRPresent(RSContext& context, NodeId nodeId, bool hdrPresent)
306 {
307     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
308         node->SetHDRPresent(hdrPresent);
309     }
310 }
311 
SetSkipDraw(RSContext & context,NodeId nodeId,bool skip)312 void SurfaceNodeCommandHelper::SetSkipDraw(RSContext& context, NodeId nodeId, bool skip)
313 {
314     if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
315         node->SetSkipDraw(skip);
316     }
317 }
318 
SetAbilityState(RSContext & context,NodeId nodeId,RSSurfaceNodeAbilityState abilityState)319 void SurfaceNodeCommandHelper::SetAbilityState(RSContext& context, NodeId nodeId,
320     RSSurfaceNodeAbilityState abilityState)
321 {
322     auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId);
323     if (!node) {
324         ROSEN_LOGE("SurfaceNodeCommandHelper::SetAbilityState node is null!");
325         return;
326     }
327     node->SetAbilityState(abilityState);
328 }
329 
SetApiCompatibleVersion(RSContext & context,NodeId nodeId,uint32_t apiCompatibleVersion)330 void SurfaceNodeCommandHelper::SetApiCompatibleVersion(RSContext& context, NodeId nodeId, uint32_t apiCompatibleVersion)
331 {
332     auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId);
333     if (!node) {
334         RS_LOGE("SurfaceNodeCommandHelper::SetApiCompatibleVersion node is null!");
335         return;
336     }
337     node->SetApiCompatibleVersion(apiCompatibleVersion);
338 }
339 } // namespace Rosen
340 } // namespace OHOS
341