1 /*
2 * Copyright (c) 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_effect_render_node.h"
17
18 #include "common/rs_obj_abs_geometry.h"
19 #include "memory/rs_memory_track.h"
20 #include "platform/common/rs_log.h"
21 #include "property/rs_properties_painter.h"
22 #include "render/rs_skia_filter.h"
23 #include "visitor/rs_node_visitor.h"
24
25 namespace OHOS {
26 namespace Rosen {
RSEffectRenderNode(NodeId id,std::weak_ptr<RSContext> context)27 RSEffectRenderNode::RSEffectRenderNode(NodeId id, std::weak_ptr<RSContext> context)
28 : RSRenderNode(id, context)
29 {
30 MemoryInfo info = {sizeof(*this), ExtractPid(id), id, MEMORY_TYPE::MEM_RENDER_NODE};
31 MemoryTrack::Instance().AddNodeRecord(id, info);
32 }
33
~RSEffectRenderNode()34 RSEffectRenderNode::~RSEffectRenderNode()
35 {
36 MemoryTrack::Instance().RemoveNodeRecord(GetId());
37 }
38
Prepare(const std::shared_ptr<RSNodeVisitor> & visitor)39 void RSEffectRenderNode::Prepare(const std::shared_ptr<RSNodeVisitor>& visitor)
40 {
41 if (!visitor) {
42 return;
43 }
44 visitor->PrepareEffectRenderNode(*this);
45 }
46
Process(const std::shared_ptr<RSNodeVisitor> & visitor)47 void RSEffectRenderNode::Process(const std::shared_ptr<RSNodeVisitor>& visitor)
48 {
49 if (!visitor) {
50 return;
51 }
52 RSRenderNode::RenderTraceDebug();
53 visitor->ProcessEffectRenderNode(*this);
54 }
55
ProcessRenderBeforeChildren(RSPaintFilterCanvas & canvas)56 void RSEffectRenderNode::ProcessRenderBeforeChildren(RSPaintFilterCanvas& canvas)
57 {
58 #ifndef USE_ROSEN_DRAWING
59 canvas.SaveEffectData();
60 auto& properties = GetRenderProperties();
61 auto boundsGeo = (properties.GetBoundsGeometry());
62 if (boundsGeo && !boundsGeo->IsEmpty()) {
63 canvas.concat(boundsGeo->GetMatrix());
64 }
65 auto alpha = properties.GetAlpha();
66 if (alpha < 1.f) {
67 if ((GetChildrenCount() == 0) || !(properties.GetAlphaOffscreen() || IsForcedDrawInGroup())) {
68 canvas.MultiplyAlpha(alpha);
69 } else {
70 auto rect = RSPropertiesPainter::Rect2SkRect(properties.GetBoundsRect());
71 canvas.saveLayerAlpha(&rect, std::clamp(alpha, 0.f, 1.f) * UINT8_MAX);
72 }
73 }
74
75 if (effectRegion_.has_value() && !(effectRegion_.value().isEmpty())) {
76 if (properties.GetBackgroundFilter() != nullptr) {
77 SkPath& path = effectRegion_.value();
78 auto effectIRect = path.getBounds().roundOut();
79 RSPropertiesPainter::DrawBackgroundEffect(properties, canvas, effectIRect);
80 }
81 if (properties.GetColorFilter() != nullptr) {
82 canvas.SetChildrenPath(effectRegion_.value());
83 }
84 }
85 #endif
86 }
87
ProcessRenderAfterChildren(RSPaintFilterCanvas & canvas)88 void RSEffectRenderNode::ProcessRenderAfterChildren(RSPaintFilterCanvas& canvas)
89 {
90 #ifndef USE_ROSEN_DRAWING
91 RSPropertiesPainter::DrawForegroundEffect(GetRenderProperties(), canvas);
92 canvas.RestoreEffectData();
93 #endif
94 }
95
GetFilterRect() const96 RectI RSEffectRenderNode::GetFilterRect() const
97 {
98 if (effectRegion_.has_value()) {
99 #ifndef USE_ROSEN_DRAWING
100 auto bounds = effectRegion_->getBounds();
101 return {bounds.x(), bounds.y(), bounds.width(), bounds.height()};
102 #else
103 auto bounds = effectRegion_->GetBounds();
104 return {bounds.GetLeft(), bounds.GetTop(), bounds.GetWidth(), bounds.GetHeight()};
105 #endif
106 } else {
107 return {};
108 }
109 }
110
111 #ifndef USE_ROSEN_DRAWING
SetEffectRegion(const std::optional<SkPath> & region)112 void RSEffectRenderNode::SetEffectRegion(const std::optional<SkPath>& region)
113 #else
114 void RSEffectRenderNode::SetEffectRegion(const std::optional<Drawing::Path>& region)
115 #endif
116 {
117 effectRegion_ = region;
118 }
119 } // namespace Rosen
120 } // namespace OHOS
121