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,bool unobscured)65 std::shared_ptr<RSSurfaceRenderNode> SurfaceNodeCommandHelper::CreateWithConfigInRS(
66 const RSSurfaceRenderNodeConfig& config, RSContext& context, bool unobscured)
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 node->SetUIExtensionUnobscured(unobscured);
76 return node;
77 }
78
SetContextMatrix(RSContext & context,NodeId id,const std::optional<Drawing::Matrix> & matrix)79 void SurfaceNodeCommandHelper::SetContextMatrix(
80 RSContext& context, NodeId id, const std::optional<Drawing::Matrix>& matrix)
81 {
82 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
83 node->SetContextMatrix(matrix, false);
84 }
85 }
86
SetContextAlpha(RSContext & context,NodeId id,float alpha)87 void SurfaceNodeCommandHelper::SetContextAlpha(RSContext& context, NodeId id, float alpha)
88 {
89 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
90 node->SetContextAlpha(alpha, false);
91 }
92 }
93
SetContextClipRegion(RSContext & context,NodeId id,const std::optional<Drawing::Rect> & clipRect)94 void SurfaceNodeCommandHelper::SetContextClipRegion(
95 RSContext& context, NodeId id, const std::optional<Drawing::Rect>& clipRect)
96 {
97 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
98 node->SetContextClipRegion(clipRect, false);
99 }
100 }
101
SetSecurityLayer(RSContext & context,NodeId id,bool isSecurityLayer)102 void SurfaceNodeCommandHelper::SetSecurityLayer(RSContext& context, NodeId id, bool isSecurityLayer)
103 {
104 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
105 node->SetSecurityLayer(isSecurityLayer);
106 }
107 }
108
SetLeashPersistentId(RSContext & context,NodeId id,LeashPersistentId leashPersistentId)109 void SurfaceNodeCommandHelper::SetLeashPersistentId(RSContext& context, NodeId id, LeashPersistentId leashPersistentId)
110 {
111 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
112 node->SetLeashPersistentId(leashPersistentId);
113 }
114 }
115
SetIsTextureExportNode(RSContext & context,NodeId id,bool isTextureExportNode)116 void SurfaceNodeCommandHelper::SetIsTextureExportNode(RSContext& context, NodeId id, bool isTextureExportNode)
117 {
118 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
119 node->SetIsTextureExportNode(isTextureExportNode);
120 }
121 }
122
SetSkipLayer(RSContext & context,NodeId id,bool isSkipLayer)123 void SurfaceNodeCommandHelper::SetSkipLayer(RSContext& context, NodeId id, bool isSkipLayer)
124 {
125 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
126 node->SetSkipLayer(isSkipLayer);
127 }
128 }
129
SetSnapshotSkipLayer(RSContext & context,NodeId id,bool isSnapshotSkipLayer)130 void SurfaceNodeCommandHelper::SetSnapshotSkipLayer(RSContext& context, NodeId id, bool isSnapshotSkipLayer)
131 {
132 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
133 node->SetSnapshotSkipLayer(isSnapshotSkipLayer);
134 }
135 }
136
SetFingerprint(RSContext & context,NodeId id,bool hasFingerprint)137 void SurfaceNodeCommandHelper::SetFingerprint(RSContext& context, NodeId id, bool hasFingerprint)
138 {
139 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
140 node->SetFingerprint(hasFingerprint);
141 }
142 }
143
SetColorSpace(RSContext & context,NodeId id,GraphicColorGamut colorSpace)144 void SurfaceNodeCommandHelper::SetColorSpace(RSContext& context, NodeId id, GraphicColorGamut colorSpace)
145 {
146 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
147 node->SetColorSpace(colorSpace);
148 }
149 }
150
UpdateSurfaceDefaultSize(RSContext & context,NodeId id,float width,float height)151 void SurfaceNodeCommandHelper::UpdateSurfaceDefaultSize(RSContext& context, NodeId id, float width, float height)
152 {
153 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
154 node->UpdateSurfaceDefaultSize(width, height);
155 }
156 }
157
ConnectToNodeInRenderService(RSContext & context,NodeId id)158 void SurfaceNodeCommandHelper::ConnectToNodeInRenderService(RSContext& context, NodeId id)
159 {
160 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
161 node->ConnectToNodeInRenderService();
162 }
163 }
164
SetCallbackForRenderThreadRefresh(RSContext & context,NodeId id,bool isRefresh)165 void SurfaceNodeCommandHelper::SetCallbackForRenderThreadRefresh(
166 RSContext& context, NodeId id, bool isRefresh)
167 {
168 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
169 if (node->NeedSetCallbackForRenderThreadRefresh()) {
170 node->SetCallbackForRenderThreadRefresh(isRefresh);
171 }
172 }
173 }
174
SetContextBounds(RSContext & context,NodeId id,Vector4f bounds)175 void SurfaceNodeCommandHelper::SetContextBounds(RSContext& context, NodeId id, Vector4f bounds)
176 {
177 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
178 node->GetMutableRenderProperties().SetBounds(bounds);
179 }
180 }
181
SetAbilityBGAlpha(RSContext & context,NodeId id,uint8_t alpha)182 void SurfaceNodeCommandHelper::SetAbilityBGAlpha(RSContext& context, NodeId id, uint8_t alpha)
183 {
184 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
185 node->SetAbilityBGAlpha(alpha);
186 }
187 }
188
SetIsNotifyUIBufferAvailable(RSContext & context,NodeId id,bool available)189 void SurfaceNodeCommandHelper::SetIsNotifyUIBufferAvailable(RSContext& context, NodeId id, bool available)
190 {
191 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
192 node->SetIsNotifyUIBufferAvailable(available);
193 }
194 }
195
MarkUIHidden(RSContext & context,NodeId id,bool isHidden)196 void SurfaceNodeCommandHelper::MarkUIHidden(RSContext& context, NodeId id, bool isHidden)
197 {
198 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
199 node->MarkUIHidden(isHidden);
200 }
201 }
202
SetSurfaceNodeType(RSContext & context,NodeId nodeId,uint8_t surfaceNodeType)203 void SurfaceNodeCommandHelper::SetSurfaceNodeType(RSContext& context, NodeId nodeId, uint8_t surfaceNodeType)
204 {
205 auto type = static_cast<RSSurfaceNodeType>(surfaceNodeType);
206 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
207 node->SetSurfaceNodeType(type);
208 }
209 }
210
SetContainerWindow(RSContext & context,NodeId nodeId,bool hasContainerWindow,float density)211 void SurfaceNodeCommandHelper::SetContainerWindow(
212 RSContext& context, NodeId nodeId, bool hasContainerWindow, float density)
213 {
214 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
215 node->SetContainerWindow(hasContainerWindow, density);
216 }
217 }
218
SetAnimationFinished(RSContext & context,NodeId nodeId)219 void SurfaceNodeCommandHelper::SetAnimationFinished(RSContext& context, NodeId nodeId)
220 {
221 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
222 node->SetStartAnimationFinished();
223 }
224 }
225
AttachToDisplay(RSContext & context,NodeId nodeId,uint64_t screenId)226 void SurfaceNodeCommandHelper::AttachToDisplay(RSContext& context, NodeId nodeId, uint64_t screenId)
227 {
228 const auto& nodeMap = context.GetNodeMap();
229 auto surfaceRenderNode = nodeMap.GetRenderNode<RSSurfaceRenderNode>(nodeId);
230 if (surfaceRenderNode == nullptr) {
231 return;
232 }
233 nodeMap.TraverseDisplayNodes(
234 [&surfaceRenderNode, &screenId](const std::shared_ptr<RSDisplayRenderNode>& displayRenderNode) {
235 if (displayRenderNode == nullptr || displayRenderNode->GetScreenId() != screenId ||
236 displayRenderNode->GetBootAnimation() != surfaceRenderNode->GetBootAnimation()) {
237 return;
238 }
239 displayRenderNode->AddChild(surfaceRenderNode);
240 });
241 }
242
DetachToDisplay(RSContext & context,NodeId nodeId,uint64_t screenId)243 void SurfaceNodeCommandHelper::DetachToDisplay(RSContext& context, NodeId nodeId, uint64_t screenId)
244 {
245 const auto& nodeMap = context.GetNodeMap();
246 auto surfaceRenderNode = nodeMap.GetRenderNode<RSSurfaceRenderNode>(nodeId);
247 if (surfaceRenderNode == nullptr) {
248 return;
249 }
250 nodeMap.TraverseDisplayNodes(
251 [&surfaceRenderNode, &screenId](const std::shared_ptr<RSDisplayRenderNode>& displayRenderNode) {
252 if (displayRenderNode == nullptr || displayRenderNode->GetScreenId() != screenId ||
253 displayRenderNode->GetBootAnimation() != surfaceRenderNode->GetBootAnimation()) {
254 return;
255 }
256 displayRenderNode->RemoveChild(surfaceRenderNode);
257 });
258 }
259
SetBootAnimation(RSContext & context,NodeId nodeId,bool isBootAnimation)260 void SurfaceNodeCommandHelper::SetBootAnimation(RSContext& context, NodeId nodeId, bool isBootAnimation)
261 {
262 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
263 node->SetBootAnimation(isBootAnimation);
264 }
265 }
266
267 #ifdef USE_SURFACE_TEXTURE
CreateSurfaceExt(RSContext & context,NodeId id,const std::shared_ptr<RSSurfaceTexture> & surfaceExt)268 void SurfaceNodeCommandHelper::CreateSurfaceExt(RSContext& context, NodeId id,
269 const std::shared_ptr<RSSurfaceTexture>& surfaceExt)
270 {
271 auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id);
272 if (node != nullptr) {
273 node->SetSurfaceTexture(surfaceExt);
274 }
275 }
276 #endif
277
SetForceHardwareAndFixRotation(RSContext & context,NodeId nodeId,bool flag)278 void SurfaceNodeCommandHelper::SetForceHardwareAndFixRotation(RSContext& context, NodeId nodeId, bool flag)
279 {
280 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
281 node->SetForceHardwareAndFixRotation(flag);
282 }
283 }
284
SetForeground(RSContext & context,NodeId nodeId,bool isForeground)285 void SurfaceNodeCommandHelper::SetForeground(RSContext& context, NodeId nodeId, bool isForeground)
286 {
287 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
288 node->SetForeground(isForeground);
289 }
290 }
291
SetSurfaceId(RSContext & context,NodeId nodeId,SurfaceId surfaceId)292 void SurfaceNodeCommandHelper::SetSurfaceId(RSContext& context, NodeId nodeId, SurfaceId surfaceId)
293 {
294 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
295 node->SetSurfaceId(surfaceId);
296 }
297 }
298
SetForceUIFirst(RSContext & context,NodeId nodeId,bool forceUIFirst)299 void SurfaceNodeCommandHelper::SetForceUIFirst(RSContext& context, NodeId nodeId, bool forceUIFirst)
300 {
301 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
302 node->SetForceUIFirst(forceUIFirst);
303 }
304 }
305
SetAncoFlags(RSContext & context,NodeId nodeId,uint32_t flags)306 void SurfaceNodeCommandHelper::SetAncoFlags(RSContext& context, NodeId nodeId, uint32_t flags)
307 {
308 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
309 node->SetAncoFlags(flags);
310 }
311 }
312
SetHDRPresent(RSContext & context,NodeId nodeId,bool hdrPresent)313 void SurfaceNodeCommandHelper::SetHDRPresent(RSContext& context, NodeId nodeId, bool hdrPresent)
314 {
315 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
316 node->SetHDRPresent(hdrPresent);
317 }
318 }
319
SetSkipDraw(RSContext & context,NodeId nodeId,bool skip)320 void SurfaceNodeCommandHelper::SetSkipDraw(RSContext& context, NodeId nodeId, bool skip)
321 {
322 if (auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId)) {
323 node->SetSkipDraw(skip);
324 }
325 }
326
SetAbilityState(RSContext & context,NodeId nodeId,RSSurfaceNodeAbilityState abilityState)327 void SurfaceNodeCommandHelper::SetAbilityState(RSContext& context, NodeId nodeId,
328 RSSurfaceNodeAbilityState abilityState)
329 {
330 auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId);
331 if (!node) {
332 ROSEN_LOGE("SurfaceNodeCommandHelper::SetAbilityState node is null!");
333 return;
334 }
335 node->SetAbilityState(abilityState);
336 }
337
SetApiCompatibleVersion(RSContext & context,NodeId nodeId,uint32_t apiCompatibleVersion)338 void SurfaceNodeCommandHelper::SetApiCompatibleVersion(RSContext& context, NodeId nodeId, uint32_t apiCompatibleVersion)
339 {
340 auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(nodeId);
341 if (!node) {
342 RS_LOGE("SurfaceNodeCommandHelper::SetApiCompatibleVersion node is null!");
343 return;
344 }
345 node->SetApiCompatibleVersion(apiCompatibleVersion);
346 }
347
AttachToWindowContainer(RSContext & context,NodeId nodeId,ScreenId screenId)348 void SurfaceNodeCommandHelper::AttachToWindowContainer(RSContext& context, NodeId nodeId, ScreenId screenId)
349 {
350 const auto& nodeMap = context.GetNodeMap();
351 auto surfaceRenderNode = nodeMap.GetRenderNode<RSSurfaceRenderNode>(nodeId);
352 if (surfaceRenderNode == nullptr) {
353 RS_LOGE("SurfaceNodeCommandHelper::AttachToWindowContainer Invalid surfaceRenderNode");
354 return;
355 }
356 nodeMap.TraverseDisplayNodes(
357 [surfaceRenderNode, screenId](const std::shared_ptr<RSDisplayRenderNode>& displayRenderNode) {
358 if (displayRenderNode == nullptr || displayRenderNode->GetScreenId() != screenId ||
359 displayRenderNode->GetBootAnimation() != surfaceRenderNode->GetBootAnimation()) {
360 return;
361 }
362 auto windowContainer = displayRenderNode->GetWindowContainer();
363 if (windowContainer == nullptr) {
364 displayRenderNode->AddChild(surfaceRenderNode);
365 RS_LOGD("SurfaceNodeCommandHelper::AttachToWindowContainer %{public}" PRIu64 " attach to %{public}"
366 PRIu64, surfaceRenderNode->GetId(), displayRenderNode->GetId());
367 } else {
368 windowContainer->AddChild(surfaceRenderNode);
369 RS_LOGD("SurfaceNodeCommandHelper::AttachToWindowContainer %{public}" PRIu64 " attach to %{public}"
370 PRIu64, surfaceRenderNode->GetId(), windowContainer->GetId());
371 }
372 }
373 );
374 }
375
DetachFromWindowContainer(RSContext & context,NodeId nodeId,ScreenId screenId)376 void SurfaceNodeCommandHelper::DetachFromWindowContainer(RSContext& context, NodeId nodeId, ScreenId screenId)
377 {
378 const auto& nodeMap = context.GetNodeMap();
379 auto surfaceRenderNode = nodeMap.GetRenderNode<RSSurfaceRenderNode>(nodeId);
380 if (surfaceRenderNode == nullptr) {
381 RS_LOGE("SurfaceNodeCommandHelper::DetachFromWindowContainer Invalid surfaceRenderNode");
382 return;
383 }
384 nodeMap.TraverseDisplayNodes(
385 [surfaceRenderNode, screenId](const std::shared_ptr<RSDisplayRenderNode>& displayRenderNode) {
386 if (displayRenderNode == nullptr || displayRenderNode->GetScreenId() != screenId ||
387 displayRenderNode->GetBootAnimation() != surfaceRenderNode->GetBootAnimation()) {
388 return;
389 }
390 auto windowContainer = displayRenderNode->GetWindowContainer();
391 if (windowContainer == nullptr) {
392 displayRenderNode->RemoveChild(surfaceRenderNode);
393 RS_LOGD("SurfaceNodeCommandHelper::DetachFromWindowContainer %{public}" PRIu64 " detach from %{public}"
394 PRIu64, surfaceRenderNode->GetId(), displayRenderNode->GetId());
395 } else {
396 windowContainer->RemoveChild(surfaceRenderNode);
397 RS_LOGD("SurfaceNodeCommandHelper::DetachFromWindowContainer %{public}" PRIu64 " detach from %{public}"
398 PRIu64, surfaceRenderNode->GetId(), windowContainer->GetId());
399 }
400 }
401 );
402 }
403 } // namespace Rosen
404 } // namespace OHOS
405