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/rs_modifier_type.h"
20
21 #include "common/rs_obj_abs_geometry.h"
22 #include "common/rs_common_def.h"
23 #ifndef USE_ROSEN_DRAWING
24 #include "include/core/SkCanvas.h"
25 #else
26 #include "recording/recording_canvas.h"
27 #endif
28 #include "memory/rs_memory_track.h"
29 #include "memory/rs_tag_tracker.h"
30 #include "pipeline/rs_paint_filter_canvas.h"
31 #include "property/rs_properties_painter.h"
32 #include "render/rs_blur_filter.h"
33 #include "render/rs_light_up_effect_filter.h"
34 #include "platform/common/rs_log.h"
35 #include "visitor/rs_node_visitor.h"
36 #include "property/rs_property_drawable.h"
37
38 namespace OHOS {
39 namespace Rosen {
40 namespace {
41 constexpr PropertyId ANONYMOUS_MODIFIER_ID = 0;
42 }
43
RSCanvasRenderNode(NodeId id,const std::weak_ptr<RSContext> & context,bool isTextureExportNode)44 RSCanvasRenderNode::RSCanvasRenderNode(NodeId id, const std::weak_ptr<RSContext>& context, bool isTextureExportNode)
45 : RSRenderNode(id, context, isTextureExportNode)
46 {
47 #ifndef ROSEN_ARKUI_X
48 MemoryInfo info = {sizeof(*this), ExtractPid(id), id, MEMORY_TYPE::MEM_RENDER_NODE};
49 MemoryTrack::Instance().AddNodeRecord(id, info);
50 #endif
51 }
52
~RSCanvasRenderNode()53 RSCanvasRenderNode::~RSCanvasRenderNode()
54 {
55 #ifndef ROSEN_ARKUI_X
56 MemoryTrack::Instance().RemoveNodeRecord(GetId());
57 #endif
58 }
59
60 #ifndef USE_ROSEN_DRAWING
UpdateRecording(std::shared_ptr<DrawCmdList> drawCmds,RSModifierType type,bool isSingleFrameComposer)61 void RSCanvasRenderNode::UpdateRecording(std::shared_ptr<DrawCmdList> drawCmds,
62 RSModifierType type, bool isSingleFrameComposer)
63 {
64 if (!drawCmds || drawCmds->GetSize() == 0) {
65 return;
66 }
67 auto renderProperty = std::make_shared<RSRenderProperty<DrawCmdListPtr>>(drawCmds, ANONYMOUS_MODIFIER_ID);
68 auto renderModifier = std::make_shared<RSDrawCmdListRenderModifier>(renderProperty);
69 renderModifier->SetType(type);
70 AddModifier(renderModifier, isSingleFrameComposer);
71 }
72 #else
UpdateRecording(std::shared_ptr<Drawing::DrawCmdList> drawCmds,RSModifierType type,bool isSingleFrameComposer)73 void RSCanvasRenderNode::UpdateRecording(std::shared_ptr<Drawing::DrawCmdList> drawCmds,
74 RSModifierType type, bool isSingleFrameComposer)
75 {
76 if (!drawCmds || drawCmds->IsEmpty()) {
77 return;
78 }
79 auto renderProperty = std::make_shared<RSRenderProperty<Drawing::DrawCmdListPtr>>(drawCmds, ANONYMOUS_MODIFIER_ID);
80 auto renderModifier = std::make_shared<RSDrawCmdListRenderModifier>(renderProperty);
81 renderModifier->SetType(type);
82 AddModifier(renderModifier, isSingleFrameComposer);
83 }
84 #endif
85
ClearRecording()86 void RSCanvasRenderNode::ClearRecording()
87 {
88 RemoveModifier(ANONYMOUS_MODIFIER_ID);
89 }
90
Prepare(const std::shared_ptr<RSNodeVisitor> & visitor)91 void RSCanvasRenderNode::Prepare(const std::shared_ptr<RSNodeVisitor>& visitor)
92 {
93 if (!visitor) {
94 return;
95 }
96 visitor->PrepareCanvasRenderNode(*this);
97 }
98
OnTreeStateChanged()99 void RSCanvasRenderNode::OnTreeStateChanged()
100 {
101 if (!IsOnTheTree()) {
102 // clear node groups cache when node is removed from tree
103 if (GetCacheType() == CacheType::CONTENT) {
104 SetCacheType(CacheType::NONE);
105 ClearCacheSurfaceInThread();
106 SetDrawingCacheType(RSDrawingCacheType::DISABLED_CACHE);
107 }
108 }
109 RSRenderNode::OnTreeStateChanged();
110 }
111
Process(const std::shared_ptr<RSNodeVisitor> & visitor)112 void RSCanvasRenderNode::Process(const std::shared_ptr<RSNodeVisitor>& visitor)
113 {
114 std::unique_lock<std::mutex> lock(canvasNodeProcessMutex_);
115 if (!visitor) {
116 return;
117 }
118 RSRenderNode::RenderTraceDebug();
119 visitor->ProcessCanvasRenderNode(*this);
120 }
121
ProcessTransitionBeforeChildren(RSPaintFilterCanvas & canvas)122 void RSCanvasRenderNode::ProcessTransitionBeforeChildren(RSPaintFilterCanvas& canvas)
123 {
124 if (RSSystemProperties::GetPropertyDrawableEnable()) {
125 DrawPropertyDrawableRange(RSPropertyDrawableSlot::SAVE_ALL, RSPropertyDrawableSlot::MASK, canvas);
126 return;
127 }
128 RSRenderNode::ProcessTransitionBeforeChildren(canvas);
129 }
130
ProcessShadowBatching(RSPaintFilterCanvas & canvas)131 void RSCanvasRenderNode::ProcessShadowBatching(RSPaintFilterCanvas& canvas)
132 {
133 RSAutoCanvasRestore acr(&canvas);
134 if (RSSystemProperties::GetPropertyDrawableEnable()) {
135 DrawPropertyDrawableRange(
136 RSPropertyDrawableSlot::BOUNDS_MATRIX, RSPropertyDrawableSlot::TRANSITION, canvas);
137 DrawPropertyDrawable(
138 RSPropertyDrawableSlot::SHADOW, canvas);
139 return;
140 }
141 RSModifierContext context = { GetMutableRenderProperties(), &canvas };
142 ApplyBoundsGeometry(canvas);
143 ApplyAlpha(canvas);
144 RSPropertiesPainter::DrawMask(GetRenderProperties(), canvas);
145 RSPropertiesPainter::DrawShadow(GetRenderProperties(), canvas);
146 RSPropertiesPainter::DrawOutline(GetRenderProperties(), canvas);
147 }
148
DrawShadow(RSModifierContext & context,RSPaintFilterCanvas & canvas)149 void RSCanvasRenderNode::DrawShadow(RSModifierContext& context, RSPaintFilterCanvas& canvas)
150 {
151 ApplyDrawCmdModifier(context, RSModifierType::TRANSITION);
152 ApplyDrawCmdModifier(context, RSModifierType::ENV_FOREGROUND_COLOR);
153
154 if (RSSystemProperties::GetUseShadowBatchingEnabled()) {
155 auto parent = GetParent().lock();
156 if (!(parent && parent->GetRenderProperties().GetUseShadowBatching())) {
157 RSPropertiesPainter::DrawShadow(GetRenderProperties(), canvas);
158 RSPropertiesPainter::DrawOutline(GetRenderProperties(), canvas);
159 }
160 } else {
161 RSPropertiesPainter::DrawShadow(GetRenderProperties(), canvas);
162 RSPropertiesPainter::DrawOutline(GetRenderProperties(), canvas);
163 }
164 }
165
PropertyDrawableRender(RSPaintFilterCanvas & canvas,bool includeProperty)166 void RSCanvasRenderNode::PropertyDrawableRender(RSPaintFilterCanvas& canvas, bool includeProperty)
167 {
168 auto parent = GetParent().lock();
169 if (RSSystemProperties::GetUseShadowBatchingEnabled() && parent &&
170 parent->GetRenderProperties().GetUseShadowBatching()) {
171 DrawPropertyDrawableRange(
172 RSPropertyDrawableSlot::TRANSITION, RSPropertyDrawableSlot::ENV_FOREGROUND_COLOR, canvas);
173 if (includeProperty) {
174 DrawPropertyDrawableRange(
175 RSPropertyDrawableSlot::BG_SAVE_BOUNDS, RSPropertyDrawableSlot::CLIP_TO_FRAME, canvas);
176 } else {
177 DrawPropertyDrawableRange(
178 RSPropertyDrawableSlot::SAVE_FRAME, RSPropertyDrawableSlot::CLIP_TO_FRAME, canvas);
179 }
180 } else {
181 if (includeProperty) {
182 DrawPropertyDrawableRange(RSPropertyDrawableSlot::TRANSITION, RSPropertyDrawableSlot::CLIP_TO_FRAME,
183 canvas);
184 } else {
185 DrawPropertyDrawableRange(RSPropertyDrawableSlot::TRANSITION, RSPropertyDrawableSlot::OUTLINE, canvas);
186 DrawPropertyDrawableRange(
187 RSPropertyDrawableSlot::SAVE_FRAME, RSPropertyDrawableSlot::CLIP_TO_FRAME, canvas);
188 }
189 }
190 }
191
ProcessAnimatePropertyBeforeChildren(RSPaintFilterCanvas & canvas,bool includeProperty)192 void RSCanvasRenderNode::ProcessAnimatePropertyBeforeChildren(RSPaintFilterCanvas& canvas, bool includeProperty)
193 {
194 if (RSSystemProperties::GetPropertyDrawableEnable()) {
195 PropertyDrawableRender(canvas, includeProperty);
196 return;
197 }
198 RSModifierContext context = { GetMutableRenderProperties(), &canvas };
199 DrawShadow(context, canvas);
200
201 // BlendMode-Begin
202 RSPropertiesPainter::BeginBlendMode(canvas, context.properties_);
203
204 // In NEW_SKIA version, L96 code will cause dump if the 3rd parameter is true.
205 #ifdef NEW_SKIA
206 RSPropertiesPainter::DrawBackground(GetRenderProperties(), canvas, false);
207 #else
208 RSPropertiesPainter::DrawBackground(GetRenderProperties(), canvas);
209 #endif
210
211 if (canvas.GetCacheType() != RSPaintFilterCanvas::CacheType::OFFSCREEN) {
212 if (GetRenderProperties().GetUseEffect()) {
213 RSPropertiesPainter::ApplyBackgroundEffect(GetRenderProperties(), canvas);
214 }
215 RSPropertiesPainter::DrawFilter(GetRenderProperties(), canvas, FilterType::BACKGROUND_FILTER);
216 }
217
218 ApplyDrawCmdModifier(context, RSModifierType::BACKGROUND_STYLE);
219
220 if (GetRenderProperties().IsDynamicLightUpValid()) {
221 RSPropertiesPainter::DrawDynamicLightUp(GetRenderProperties(), canvas);
222 }
223
224 #ifndef USE_ROSEN_DRAWING
225 canvasNodeSaveCount_ = canvas.Save();
226 #else
227 canvasNodeSaveCount_ = canvas.SaveAllStatus();
228 #endif
229 ApplyDrawCmdModifier(context, RSModifierType::ENV_FOREGROUND_COLOR_STRATEGY);
230 #ifndef USE_ROSEN_DRAWING
231 canvas.translate(GetRenderProperties().GetFrameOffsetX(), GetRenderProperties().GetFrameOffsetY());
232 #else
233 canvas.Translate(GetRenderProperties().GetFrameOffsetX(), GetRenderProperties().GetFrameOffsetY());
234 #endif
235
236 if (GetRenderProperties().GetClipToFrame()) {
237 // In NEW_SKIA version, L116 code will cause dump if the 3rd parameter is true.
238 #ifdef NEW_SKIA
239 RSPropertiesPainter::Clip(canvas, GetRenderProperties().GetFrameRect(), false);
240 #else
241 RSPropertiesPainter::Clip(canvas, GetRenderProperties().GetFrameRect());
242 #endif
243 }
244 }
245
ProcessRenderContents(RSPaintFilterCanvas & canvas)246 void RSCanvasRenderNode::ProcessRenderContents(RSPaintFilterCanvas& canvas)
247 {
248 if (RSSystemProperties::GetPropertyDrawableEnable()) {
249 DrawPropertyDrawable(RSPropertyDrawableSlot::CONTENT_STYLE, canvas);
250 return;
251 }
252 RSModifierContext context = { GetMutableRenderProperties(), &canvas };
253 ApplyDrawCmdModifier(context, RSModifierType::CONTENT_STYLE);
254 }
255
ProcessRenderBeforeChildren(RSPaintFilterCanvas & canvas)256 void RSCanvasRenderNode::ProcessRenderBeforeChildren(RSPaintFilterCanvas& canvas)
257 {
258 if (RSSystemProperties::GetPropertyDrawableEnable()) {
259 DrawPropertyDrawableRange(RSPropertyDrawableSlot::SAVE_ALL, RSPropertyDrawableSlot::CLIP_TO_FRAME, canvas);
260 return;
261 }
262 ProcessTransitionBeforeChildren(canvas);
263 ProcessAnimatePropertyBeforeChildren(canvas, true);
264 }
265
ProcessAnimatePropertyAfterChildren(RSPaintFilterCanvas & canvas)266 void RSCanvasRenderNode::ProcessAnimatePropertyAfterChildren(RSPaintFilterCanvas& canvas)
267 {
268 if (RSSystemProperties::GetPropertyDrawableEnable()) {
269 DrawPropertyDrawableRange(
270 RSPropertyDrawableSlot::FOREGROUND_STYLE, RSPropertyDrawableSlot::PARTICLE_EFFECT, canvas);
271 return;
272 }
273 RSModifierContext context = { GetMutableRenderProperties(), &canvas };
274 ApplyDrawCmdModifier(context, RSModifierType::FOREGROUND_STYLE);
275
276 auto& aiInvert = GetRenderProperties().GetAiInvert();
277 if (aiInvert.has_value()) {
278 RSPropertiesPainter::DrawBinarizationShader(GetRenderProperties(), canvas);
279 }
280 RSPropertiesPainter::DrawColorFilter(GetRenderProperties(), canvas);
281
282 canvas.RestoreStatus(canvasNodeSaveCount_);
283
284 if (GetRenderProperties().IsLightUpEffectValid()) {
285 RSPropertiesPainter::DrawLightUpEffect(GetRenderProperties(), canvas);
286 }
287 RSPropertiesPainter::DrawFilter(GetRenderProperties(), canvas, FilterType::FOREGROUND_FILTER);
288
289 auto illuminatedPtr_ = GetRenderProperties().GetIlluminated();
290 if (illuminatedPtr_ && illuminatedPtr_->IsIlluminated()) {
291 RSPropertiesPainter::DrawLight(GetRenderProperties(), canvas);
292 }
293 RSPropertiesPainter::DrawOutline(GetRenderProperties(), canvas);
294 RSPropertiesPainter::DrawBorder(GetRenderProperties(), canvas);
295 ApplyDrawCmdModifier(context, RSModifierType::OVERLAY_STYLE);
296 RSPropertiesPainter::DrawForegroundColor(GetRenderProperties(), canvas);
297 RSPropertiesPainter::DrawParticle(GetRenderProperties(), canvas);
298
299 // BlendMode-End
300 RSPropertiesPainter::EndBlendMode(canvas, context.properties_);
301 }
302
ProcessTransitionAfterChildren(RSPaintFilterCanvas & canvas)303 void RSCanvasRenderNode::ProcessTransitionAfterChildren(RSPaintFilterCanvas& canvas)
304 {
305 if (RSSystemProperties::GetPropertyDrawableEnable()) {
306 DrawPropertyDrawableRange(RSPropertyDrawableSlot::PIXEL_STRETCH, RSPropertyDrawableSlot::RESTORE_ALL, canvas);
307 return;
308 }
309 RSPropertiesPainter::DrawPixelStretch(GetRenderProperties(), canvas);
310 RSRenderNode::ProcessRenderAfterChildren(canvas);
311 }
312
ProcessRenderAfterChildren(RSPaintFilterCanvas & canvas)313 void RSCanvasRenderNode::ProcessRenderAfterChildren(RSPaintFilterCanvas& canvas)
314 {
315 if (RSSystemProperties::GetPropertyDrawableEnable()) {
316 DrawPropertyDrawableRange(
317 RSPropertyDrawableSlot::FOREGROUND_STYLE, RSPropertyDrawableSlot::RESTORE_ALL, canvas);
318 return;
319 }
320 ProcessAnimatePropertyAfterChildren(canvas);
321 ProcessTransitionAfterChildren(canvas);
322 canvas.RestoreEnv();
323 }
324
ApplyDrawCmdModifier(RSModifierContext & context,RSModifierType type)325 void RSCanvasRenderNode::ApplyDrawCmdModifier(RSModifierContext& context, RSModifierType type)
326 {
327 // temporary workaround, PLANNING: refactor single frame compose without modifing draw cmd list
328 auto& drawCmdModifiers = const_cast<RSRenderContent::DrawCmdContainer&>(GetDrawCmdModifiers());
329 auto itr = drawCmdModifiers.find(type);
330 if (itr == drawCmdModifiers.end() || itr->second.empty()) {
331 return;
332 }
333
334 if (RSSystemProperties::GetSingleFrameComposerEnabled()) {
335 bool needSkip = false;
336 if (GetNodeIsSingleFrameComposer() && singleFrameComposer_ != nullptr) {
337 needSkip = singleFrameComposer_->SingleFrameModifierAddToList(type, itr->second);
338 }
339 for (const auto& modifier : itr->second) {
340 if (singleFrameComposer_ != nullptr && singleFrameComposer_->SingleFrameIsNeedSkip(needSkip, modifier)) {
341 continue;
342 }
343 modifier->Apply(context);
344 }
345 } else {
346 for (const auto& modifier : itr->second) {
347 modifier->Apply(context);
348 }
349 }
350 }
351
InternalDrawContent(RSPaintFilterCanvas & canvas)352 void RSCanvasRenderNode::InternalDrawContent(RSPaintFilterCanvas& canvas)
353 {
354 RSModifierContext context = { GetMutableRenderProperties(), &canvas };
355 #ifndef USE_ROSEN_DRAWING
356 canvas.translate(GetRenderProperties().GetFrameOffsetX(), GetRenderProperties().GetFrameOffsetY());
357 #else
358 canvas.Translate(GetRenderProperties().GetFrameOffsetX(), GetRenderProperties().GetFrameOffsetY());
359 #endif
360
361 if (GetRenderProperties().GetClipToFrame()) {
362 RSPropertiesPainter::Clip(canvas, GetRenderProperties().GetFrameRect());
363 }
364 ApplyDrawCmdModifier(context, RSModifierType::CONTENT_STYLE);
365
366 // temporary solution for drawing children
367 for (auto& child : *GetSortedChildren()) {
368 if (auto canvasChild = ReinterpretCast<RSCanvasRenderNode>(child)) {
369 canvasChild->InternalDrawContent(canvas);
370 }
371 }
372 }
373
ProcessDrivenBackgroundRender(RSPaintFilterCanvas & canvas)374 void RSCanvasRenderNode::ProcessDrivenBackgroundRender(RSPaintFilterCanvas& canvas)
375 {
376 #if defined(RS_ENABLE_DRIVEN_RENDER)
377 RSRenderNode::ProcessRenderBeforeChildren(canvas);
378 RSModifierContext context = { GetMutableRenderProperties(), &canvas };
379 ApplyDrawCmdModifier(context, RSModifierType::TRANSITION);
380 ApplyDrawCmdModifier(context, RSModifierType::ENV_FOREGROUND_COLOR);
381
382 RSPropertiesPainter::DrawShadow(GetRenderProperties(), canvas);
383 RSPropertiesPainter::DrawOutline(GetRenderProperties(), canvas);
384 RSPropertiesPainter::DrawBackground(GetRenderProperties(), canvas);
385 RSPropertiesPainter::DrawFilter(GetRenderProperties(), canvas, FilterType::BACKGROUND_FILTER);
386 ApplyDrawCmdModifier(context, RSModifierType::BACKGROUND_STYLE);
387 RSRenderNode::ProcessRenderAfterChildren(canvas);
388 #endif
389 }
390
ProcessDrivenContentRender(RSPaintFilterCanvas & canvas)391 void RSCanvasRenderNode::ProcessDrivenContentRender(RSPaintFilterCanvas& canvas)
392 {
393 #if defined(RS_ENABLE_DRIVEN_RENDER)
394 #ifndef USE_ROSEN_DRAWING
395 canvasNodeSaveCount_ = canvas.Save();
396 canvas.translate(GetRenderProperties().GetFrameOffsetX(), GetRenderProperties().GetFrameOffsetY());
397 #else
398 canvasNodeSaveCount_ = canvas.SaveAllStatus();
399 canvas.Translate(GetRenderProperties().GetFrameOffsetX(), GetRenderProperties().GetFrameOffsetY());
400 #endif
401 DrawDrivenContent(canvas);
402 #endif
403 }
404
ProcessDrivenContentRenderAfterChildren(RSPaintFilterCanvas & canvas)405 void RSCanvasRenderNode::ProcessDrivenContentRenderAfterChildren(RSPaintFilterCanvas& canvas)
406 {
407 #if defined(RS_ENABLE_DRIVEN_RENDER)
408 // Unresolvable bug: Driven render do not support DrawFilter/DrawBorder/FOREGROUND_STYLE/OVERLAY_STYLE
409 RSModifierContext context = { GetMutableRenderProperties(), &canvas };
410 ApplyDrawCmdModifier(context, RSModifierType::FOREGROUND_STYLE);
411
412 canvas.RestoreStatus(canvasNodeSaveCount_);
413 #endif
414 }
415
GetDrivenContentClipFrameRect() const416 RectF RSCanvasRenderNode::GetDrivenContentClipFrameRect() const
417 {
418 #if defined(RS_ENABLE_DRIVEN_RENDER)
419 // temporary solution for driven content clip
420 RectF rect;
421 auto itr = GetDrawCmdModifiers().find(RSModifierType::CONTENT_STYLE);
422 if (itr == GetDrawCmdModifiers().end() || itr->second.empty()) {
423 return rect;
424 }
425 if (!itr->second.empty()) {
426 auto drawCmdModifier = std::static_pointer_cast<RSDrawCmdListRenderModifier>(itr->second.front());
427 if (drawCmdModifier != nullptr) {
428 rect = drawCmdModifier->GetCmdsClipRect();
429 }
430 }
431 return rect;
432 #else
433 return RectF { 0.0f, 0.0f, 0.0f, 0.0f };
434 #endif
435 }
436
DrawDrivenContent(RSPaintFilterCanvas & canvas)437 void RSCanvasRenderNode::DrawDrivenContent(RSPaintFilterCanvas& canvas)
438 {
439 #if defined(RS_ENABLE_DRIVEN_RENDER)
440 RSModifierContext context = { GetMutableRenderProperties(), &canvas };
441 auto itr = GetDrawCmdModifiers().find(RSModifierType::CONTENT_STYLE);
442 if (itr == GetDrawCmdModifiers().end() || itr->second.empty()) {
443 return;
444 }
445 int32_t index = 0;
446 for (const auto& modifier : itr->second) {
447 if (index == 0) {
448 // temporary solution for driven content clip
449 auto drawCmdModifier = std::static_pointer_cast<RSDrawCmdListRenderModifier>(modifier);
450 if (drawCmdModifier != nullptr) {
451 drawCmdModifier->ApplyForDrivenContent(context);
452 index++;
453 continue;
454 }
455 }
456 if (modifier != nullptr) {
457 modifier->Apply(context);
458 }
459 index++;
460 }
461 #endif
462 }
463 } // namespace Rosen
464 } // namespace OHOS
465