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 "pipeline/rs_canvas_render_node.h"
17
18 #include <algorithm>
19 #include "modifier_ng/rs_render_modifier_ng.h"
20
21 #include "common/rs_obj_abs_geometry.h"
22 #include "common/rs_common_def.h"
23 #include "recording/recording_canvas.h"
24 #include "memory/rs_memory_track.h"
25 #include "memory/rs_tag_tracker.h"
26 #include "params/rs_render_params.h"
27 #include "pipeline/rs_context.h"
28 #include "pipeline/rs_screen_render_node.h"
29 #include "pipeline/rs_paint_filter_canvas.h"
30 #include "pipeline/rs_surface_render_node.h"
31 #include "property/rs_properties_painter.h"
32 #include "render/rs_blur_filter.h"
33 #ifdef USE_VIDEO_PROCESSING_ENGINE
34 #include "render/rs_colorspace_convert.h"
35 #endif
36 #include "render/rs_light_up_effect_filter.h"
37 #include "platform/common/rs_log.h"
38 #include "platform/common/rs_system_properties.h"
39 #include "visitor/rs_node_visitor.h"
40
41 namespace OHOS {
42 namespace Rosen {
43 namespace {
44 constexpr ModifierId ANONYMOUS_MODIFIER_NG_ID = 0;
45 } // namespace
46
RSCanvasRenderNode(NodeId id,const std::weak_ptr<RSContext> & context,bool isTextureExportNode)47 RSCanvasRenderNode::RSCanvasRenderNode(NodeId id, const std::weak_ptr<RSContext>& context, bool isTextureExportNode)
48 : RSRenderNode(id, context, isTextureExportNode)
49 {
50 #ifndef ROSEN_ARKUI_X
51 MemoryInfo info = {sizeof(*this), ExtractPid(id), id, MEMORY_TYPE::MEM_RENDER_NODE};
52 MemoryTrack::Instance().AddNodeRecord(id, info);
53 #endif
54 MemorySnapshot::Instance().AddCpuMemory(ExtractPid(id), sizeof(*this));
55 }
56
~RSCanvasRenderNode()57 RSCanvasRenderNode::~RSCanvasRenderNode()
58 {
59 #ifndef ROSEN_ARKUI_X
60 MemoryTrack::Instance().RemoveNodeRecord(GetId());
61 #endif
62 MemorySnapshot::Instance().RemoveCpuMemory(ExtractPid(GetId()), sizeof(*this));
63 }
64
UpdateRecordingNG(std::shared_ptr<Drawing::DrawCmdList> drawCmds,ModifierNG::RSModifierType type,bool isSingleFrameComposer)65 void RSCanvasRenderNode::UpdateRecordingNG(
66 std::shared_ptr<Drawing::DrawCmdList> drawCmds, ModifierNG::RSModifierType type, bool isSingleFrameComposer)
67 {
68 if (!drawCmds || drawCmds->IsEmpty()) {
69 return;
70 }
71 auto renderProperty =
72 std::make_shared<RSRenderProperty<Drawing::DrawCmdListPtr>>(drawCmds, ANONYMOUS_MODIFIER_NG_ID);
73 auto renderModifier =
74 ModifierNG::RSRenderModifier::MakeRenderModifier<Drawing::DrawCmdListPtr>(type, renderProperty);
75 AddModifier(renderModifier, isSingleFrameComposer);
76 }
77
ClearRecording()78 void RSCanvasRenderNode::ClearRecording()
79 {
80 RemoveModifierNG(ANONYMOUS_MODIFIER_NG_ID);
81 }
82
QuickPrepare(const std::shared_ptr<RSNodeVisitor> & visitor)83 void RSCanvasRenderNode::QuickPrepare(const std::shared_ptr<RSNodeVisitor>& visitor)
84 {
85 if (!visitor) {
86 return;
87 }
88 ApplyModifiers();
89 #if defined(ROSEN_OHOS) && defined(ENABLE_HPAE_BLUR)
90 visitor->RegisterHpaeCallback(*this);
91 #endif
92 visitor->QuickPrepareCanvasRenderNode(*this);
93 }
94
Prepare(const std::shared_ptr<RSNodeVisitor> & visitor)95 void RSCanvasRenderNode::Prepare(const std::shared_ptr<RSNodeVisitor>& visitor)
96 {
97 if (!visitor) {
98 return;
99 }
100 ApplyModifiers();
101 visitor->PrepareCanvasRenderNode(*this);
102 }
103
OnTreeStateChanged()104 void RSCanvasRenderNode::OnTreeStateChanged()
105 {
106 if (!IsOnTheTree()) {
107 // clear node groups cache when node is removed from tree
108 if (GetCacheType() == CacheType::CONTENT) {
109 SetCacheType(CacheType::NONE);
110 ClearCacheSurfaceInThread();
111 SetDrawingCacheType(RSDrawingCacheType::DISABLED_CACHE);
112 }
113 needClearSurface_ = true;
114 AddToPendingSyncList();
115 }
116 RSRenderNode::OnTreeStateChanged();
117
118 // When the canvasNode is up or down the tree, it transmits color gamut information to appWindow node.
119 ModifyWindowWideColorGamutNum(IsOnTheTree(), graphicColorGamut_);
120 }
121
Process(const std::shared_ptr<RSNodeVisitor> & visitor)122 void RSCanvasRenderNode::Process(const std::shared_ptr<RSNodeVisitor>& visitor)
123 {
124 if (!visitor) {
125 return;
126 }
127 RSRenderNode::RenderTraceDebug();
128 visitor->ProcessCanvasRenderNode(*this);
129 }
130
ProcessTransitionBeforeChildren(RSPaintFilterCanvas & canvas)131 void RSCanvasRenderNode::ProcessTransitionBeforeChildren(RSPaintFilterCanvas& canvas)
132 {
133 DrawPropertyDrawableRange(RSDrawableSlot::SAVE_ALL, RSDrawableSlot::MASK, canvas);
134 }
135
ProcessShadowBatching(RSPaintFilterCanvas & canvas)136 void RSCanvasRenderNode::ProcessShadowBatching(RSPaintFilterCanvas& canvas)
137 {
138 RSAutoCanvasRestore acr(&canvas);
139 ApplyAlphaAndBoundsGeometry(canvas);
140 DrawPropertyDrawableRange(RSDrawableSlot::MASK, RSDrawableSlot::TRANSITION, canvas);
141 DrawPropertyDrawable(RSDrawableSlot::SHADOW, canvas);
142 }
143
DrawShadow(ModifierNG::RSModifierContext & context,RSPaintFilterCanvas & canvas)144 void RSCanvasRenderNode::DrawShadow(ModifierNG::RSModifierContext& context, RSPaintFilterCanvas& canvas)
145 {
146 ApplyDrawCmdModifier(context, ModifierNG::RSModifierType::TRANSITION_STYLE);
147 ApplyDrawCmdModifier(context, ModifierNG::RSModifierType::ENV_FOREGROUND_COLOR);
148 auto parent = GetParent().lock();
149 if (!(parent && parent->GetRenderProperties().GetUseShadowBatching())) {
150 RSPropertiesPainter::DrawShadow(GetRenderProperties(), canvas);
151 RSPropertiesPainter::DrawOutline(GetRenderProperties(), canvas);
152 }
153 }
154
PropertyDrawableRender(RSPaintFilterCanvas & canvas,bool includeProperty)155 void RSCanvasRenderNode::PropertyDrawableRender(RSPaintFilterCanvas& canvas, bool includeProperty)
156 {
157 auto parent = GetParent().lock();
158 if (parent && parent->GetRenderProperties().GetUseShadowBatching()) {
159 DrawPropertyDrawableRange(RSDrawableSlot::TRANSITION, RSDrawableSlot::ENV_FOREGROUND_COLOR, canvas);
160 if (includeProperty) {
161 // Just need to skip RSPropertyDrawableSlot::SHADOW
162 DrawPropertyDrawableRange(RSDrawableSlot::FOREGROUND_FILTER, RSDrawableSlot::CLIP_TO_FRAME, canvas);
163 } else {
164 DrawPropertyDrawableRange(RSDrawableSlot::SAVE_FRAME, RSDrawableSlot::CLIP_TO_FRAME, canvas);
165 }
166 } else {
167 if (includeProperty) {
168 DrawPropertyDrawableRange(RSDrawableSlot::TRANSITION, RSDrawableSlot::CLIP_TO_FRAME, canvas);
169 } else {
170 DrawPropertyDrawableRange(RSDrawableSlot::TRANSITION, RSDrawableSlot::OUTLINE, canvas);
171 DrawPropertyDrawableRange(RSDrawableSlot::SAVE_FRAME, RSDrawableSlot::CLIP_TO_FRAME, canvas);
172 }
173 }
174 }
175
ProcessAnimatePropertyBeforeChildren(RSPaintFilterCanvas & canvas,bool includeProperty)176 void RSCanvasRenderNode::ProcessAnimatePropertyBeforeChildren(RSPaintFilterCanvas& canvas, bool includeProperty)
177 {
178 PropertyDrawableRender(canvas, includeProperty);
179 }
180
ProcessRenderContents(RSPaintFilterCanvas & canvas)181 void RSCanvasRenderNode::ProcessRenderContents(RSPaintFilterCanvas& canvas)
182 {
183 DrawPropertyDrawable(RSDrawableSlot::CONTENT_STYLE, canvas);
184 }
185
ProcessRenderBeforeChildren(RSPaintFilterCanvas & canvas)186 void RSCanvasRenderNode::ProcessRenderBeforeChildren(RSPaintFilterCanvas& canvas)
187 {
188 auto parent = GetParent().lock();
189 if (parent && parent->GetRenderProperties().GetUseShadowBatching()) {
190 DrawPropertyDrawableRange(RSDrawableSlot::SAVE_ALL, RSDrawableSlot::ENV_FOREGROUND_COLOR, canvas);
191 // Just need to skip RSPropertyDrawableSlot::SHADOW
192 DrawPropertyDrawableRange(RSDrawableSlot::FOREGROUND_FILTER, RSDrawableSlot::CUSTOM_CLIP_TO_FRAME, canvas);
193 } else {
194 DrawPropertyDrawableRange(RSDrawableSlot::SAVE_ALL, RSDrawableSlot::CUSTOM_CLIP_TO_FRAME, canvas);
195 }
196 }
197
ProcessAnimatePropertyAfterChildren(RSPaintFilterCanvas & canvas)198 void RSCanvasRenderNode::ProcessAnimatePropertyAfterChildren(RSPaintFilterCanvas& canvas)
199 {
200 DrawPropertyDrawableRange(RSDrawableSlot::FOREGROUND_STYLE, RSDrawableSlot::PARTICLE_EFFECT, canvas);
201 }
202
ProcessTransitionAfterChildren(RSPaintFilterCanvas & canvas)203 void RSCanvasRenderNode::ProcessTransitionAfterChildren(RSPaintFilterCanvas& canvas)
204 {
205 DrawPropertyDrawableRange(RSDrawableSlot::PIXEL_STRETCH, RSDrawableSlot::RESTORE_ALL, canvas);
206 }
207
ProcessRenderAfterChildren(RSPaintFilterCanvas & canvas)208 void RSCanvasRenderNode::ProcessRenderAfterChildren(RSPaintFilterCanvas& canvas)
209 {
210 DrawPropertyDrawableRange(RSDrawableSlot::FOREGROUND_STYLE, RSDrawableSlot::RESTORE_ALL, canvas);
211 }
212
ApplyDrawCmdModifier(ModifierNG::RSModifierContext & context,ModifierNG::RSModifierType type)213 void RSCanvasRenderNode::ApplyDrawCmdModifier(ModifierNG::RSModifierContext& context, ModifierNG::RSModifierType type)
214 {
215 const auto& modifiers = GetModifiersNG(type);
216 if (modifiers.empty()) {
217 return;
218 }
219 if (RSSystemProperties::GetSingleFrameComposerEnabled()) {
220 bool needSkip = false;
221 if (GetNodeIsSingleFrameComposer() && singleFrameComposer_ != nullptr) {
222 auto& modifierList = const_cast<std::vector<std::shared_ptr<ModifierNG::RSRenderModifier>>&>(modifiers);
223 needSkip = singleFrameComposer_->SingleFrameModifierAddToListNG(type, modifierList);
224 }
225 for (const auto& modifier : modifiers) {
226 if (singleFrameComposer_ != nullptr && singleFrameComposer_->SingleFrameIsNeedSkipNG(needSkip, modifier)) {
227 continue;
228 }
229 modifier->Apply(context.canvas_, context.properties_);
230 }
231 } else {
232 for (const auto& modifier : modifiers) {
233 modifier->Apply(context.canvas_, context.properties_);
234 }
235 }
236 }
237
InternalDrawContent(RSPaintFilterCanvas & canvas,bool needApplyMatrix)238 void RSCanvasRenderNode::InternalDrawContent(RSPaintFilterCanvas& canvas, bool needApplyMatrix)
239 {
240 ModifierNG::RSModifierContext context = { GetMutableRenderProperties(), &canvas };
241
242 if (needApplyMatrix) {
243 DrawPropertyDrawableRange(RSDrawableSlot::SAVE_ALL, RSDrawableSlot::CONTENT_STYLE, canvas);
244 } else {
245 DrawPropertyDrawableRange(RSDrawableSlot::OUTLINE, RSDrawableSlot::CONTENT_STYLE, canvas);
246 }
247
248 for (auto& child : *GetSortedChildren()) {
249 if (auto canvasChild = ReinterpretCast<RSCanvasRenderNode>(child)) {
250 canvasChild->InternalDrawContent(canvas, true);
251 }
252 }
253
254 if (needApplyMatrix) {
255 DrawPropertyDrawableRange(RSDrawableSlot::FOREGROUND_STYLE, RSDrawableSlot::RESTORE_ALL, canvas);
256 } else {
257 DrawPropertyDrawableRange(RSDrawableSlot::FOREGROUND_STYLE, RSDrawableSlot::PIXEL_STRETCH, canvas);
258 }
259 }
260
261 // When the HDR node status changed, update the node list in the ancestor display node.
262 // Support to add animation on canvas nodes when display node is forced to close HDR.
UpdateScreenHDRNodeList(bool flag,NodeId screenNodeId) const263 void RSCanvasRenderNode::UpdateScreenHDRNodeList(bool flag, NodeId screenNodeId) const
264 {
265 auto context = GetContext().lock();
266 if (!context) {
267 ROSEN_LOGE("RSCanvasRenderNode::UpdateScreenHDRNodeList Invalid context");
268 return;
269 }
270 auto screenNode = context->GetNodeMap().GetRenderNode<RSScreenRenderNode>(screenNodeId);
271 if (!screenNode) {
272 ROSEN_LOGE("RSCanvasRenderNode::UpdateScreenHDRNodeList Invalid screenNode");
273 return;
274 }
275 if (flag) {
276 screenNode->InsertHDRNode(GetId());
277 } else {
278 screenNode->RemoveHDRNode(GetId());
279 }
280 }
281
SetHDRPresent(bool hasHdrPresent)282 void RSCanvasRenderNode::SetHDRPresent(bool hasHdrPresent)
283 {
284 if (hasHdrPresent_ == hasHdrPresent) {
285 return;
286 }
287 if (IsOnTheTree()) {
288 SetHdrNum(hasHdrPresent, GetInstanceRootNodeId(), HDRComponentType::IMAGE);
289 UpdateScreenHDRNodeList(hasHdrPresent, GetScreenNodeId());
290 }
291 hasHdrPresent_ = hasHdrPresent;
292 }
293
SetColorGamut(uint32_t gamut)294 void RSCanvasRenderNode::SetColorGamut(uint32_t gamut)
295 {
296 if (colorGamut_ == gamut) {
297 return;
298 }
299 #ifdef USE_VIDEO_PROCESSING_ENGINE
300 GraphicColorGamut nowGamut = graphicColorGamut_;
301 graphicColorGamut_ = RSColorSpaceConvert::ColorSpaceNameToGraphicGamut(
302 static_cast<OHOS::ColorManager::ColorSpaceName>(gamut));
303 if (IsOnTheTree()) {
304 ModifyWindowWideColorGamutNum(false, nowGamut);
305 ModifyWindowWideColorGamutNum(true, graphicColorGamut_);
306 }
307 #endif
308 colorGamut_ = gamut;
309 }
310
GetColorGamut()311 uint32_t RSCanvasRenderNode::GetColorGamut()
312 {
313 return colorGamut_;
314 }
315
ModifyWindowWideColorGamutNum(bool isOnTree,GraphicColorGamut gamut)316 void RSCanvasRenderNode::ModifyWindowWideColorGamutNum(bool isOnTree, GraphicColorGamut gamut)
317 {
318 auto parentInstance = GetInstanceRootNode();
319 if (!parentInstance) {
320 RS_LOGE("RSCanvasRenderNode::ModifyWindowWideColorGamutNum get instanceRootNode failed.");
321 return;
322 }
323 if (auto parentSurface = parentInstance->ReinterpretCastTo<RSSurfaceRenderNode>()) {
324 if (isOnTree) {
325 parentSurface->IncreaseCanvasGamutNum(gamut);
326 } else {
327 parentSurface->ReduceCanvasGamutNum(gamut);
328 }
329 }
330 }
331
332 // [Attention] Only used in PC window resize scene now
SetLinkedRootNodeId(NodeId rootNodeId)333 void RSCanvasRenderNode::SetLinkedRootNodeId(NodeId rootNodeId)
334 {
335 if (!RSSystemProperties::GetWindowKeyFrameEnabled()) {
336 RS_LOGW("RSCanvasRenderNode::SetLinkedRootNodeId WindowKeyFrame feature disabled");
337 return;
338 }
339
340 linkedRootNodeId_ = rootNodeId;
341 }
342
343 // [Attention] Only used in PC window resize scene now
GetLinkedRootNodeId() const344 NodeId RSCanvasRenderNode::GetLinkedRootNodeId() const
345 {
346 return linkedRootNodeId_;
347 }
348
349 } // namespace Rosen
350 } // namespace OHOS
351