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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_PAINTER_ROSEN_DECORATION_PAINTER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_PAINTER_ROSEN_DECORATION_PAINTER_H
18 
19 #include <math.h>
20 
21 #include "base/memory/ace_type.h"
22 #include "base/utils/utils.h"
23 #include "core/components/common/layout/constants.h"
24 #include "core/components/common/properties/border.h"
25 #include "core/components/common/properties/border_edge.h"
26 #include "core/components/common/properties/border_image_edge.h"
27 #include "core/components/common/properties/decoration.h"
28 #include "core/components/common/properties/edge.h"
29 #include "core/components/image/render_image.h"
30 #ifndef USE_ROSEN_DRAWING
31 #include "core/components_ng/render/adapter/skia_decoration_painter.h"
32 #include "include/core/SkRefCnt.h"
33 #else
34 #include "core/components_ng/render/adapter/rosen/drawing_decoration_painter.h"
35 #endif
36 
37 #ifndef USE_ROSEN_DRAWING
38 class SkPath;
39 class SkPaint;
40 class SkRRect;
41 class SkShader;
42 class SkImage;
43 struct SkSize;
44 #endif
45 
46 namespace OHOS::Ace {
47 
48 class Border;
49 class Offset;
50 class Size;
51 
52 #ifndef USE_ROSEN_DRAWING
53 class RosenDecorationPainter : public virtual NG::SkiaDecorationPainter {
54     DECLARE_ACE_TYPE(RosenDecorationPainter, NG::SkiaDecorationPainter);
55 #else
56 class RosenDecorationPainter : public virtual NG::DrawingDecorationPainter {
57     DECLARE_ACE_TYPE(RosenDecorationPainter, NG::DrawingDecorationPainter);
58 #endif
59 
60 public:
61     RosenDecorationPainter(
62         const RefPtr<Decoration>& decoration, const Rect& paintRect, const Size& paintSize, double dipScale);
63     ~RosenDecorationPainter() override = default;
64 
65 #ifndef USE_ROSEN_DRAWING
66     static void PaintShadow(const SkPath& path, const Shadow& shadow, SkCanvas* canvas, const SkPaint* paint = nullptr);
67 
68     static void PaintShadow(const SkPath& path, const Shadow& shadow, const std::shared_ptr<RSNode>& rsNode);
69 
70     static void PaintGrayScale(const SkRRect& outerRRect, SkCanvas* canvas, const Dimension& grayscale,
71         const Color& color);
72 
73     static void PaintBrightness(const SkRRect& outerRRect, SkCanvas* canvas, const Dimension& brightness,
74         const Color& color);
75 
76     static void PaintContrast(const SkRRect& outerRRect, SkCanvas* canvas, const Dimension& contrast,
77         const Color& color);
78 
79     static void PaintSaturate(const SkRRect& outerRRect, SkCanvas* canvas, const Dimension& saturate,
80         const Color& color);
81 
82     static void PaintSepia(const SkRRect& outerRRect, SkCanvas* canvas, const Dimension& sepia, const Color& color);
83 
84     static void PaintInvert(const SkRRect& outerRRect, SkCanvas* canvas, const Dimension& invert, const Color& color);
85 
86     static void PaintHueRotate(const SkRRect& outerRRect, SkCanvas* canvas, const float& hueRotate, const Color& color);
87 
88     void PaintDecoration(const Offset& offset, SkCanvas* canvas, RenderContext& context, const sk_sp<SkImage>& image);
89 
90     void PaintDecoration(const Offset& offset, SkCanvas* canvas, RenderContext& context);
91 
92     static void PaintBorderImage(RefPtr<OHOS::Ace::Decoration>& decoration, Size& paintSize, const Offset& position,
93         SkCanvas* canvas, const sk_sp<SkImage>& image, double dipScale);
94 #else
95     static void PaintShadow(const RSPath& path, const Shadow& shadow,
96         RSCanvas* canvas, const RSBrush* brush = nullptr, const RSPen* pen = nullptr);
97     static void PaintShadow(const RSPath& path, const Shadow& shadow,
98         const std::shared_ptr<RSNode>& rsNode);
99     static void PaintGrayScale(const RSRoundRect& outerRRect, RSCanvas* canvas,
100         const Dimension& grayscale, const Color& color);
101     static void PaintBrightness(const RSRoundRect& outerRRect, RSCanvas* canvas,
102         const Dimension& brightness, const Color& color);
103     static void PaintContrast(const RSRoundRect& outerRRect, RSCanvas* canvas,
104         const Dimension& contrast, const Color& color);
105     static void PaintSaturate(const RSRoundRect& outerRRect, RSCanvas* canvas,
106         const Dimension& saturate, const Color& color);
107     static void PaintSepia(const RSRoundRect& outerRRect, RSCanvas* canvas,
108         const Dimension& sepia, const Color& color);
109     static void PaintInvert(const RSRoundRect& outerRRect, RSCanvas* canvas,
110         const Dimension& invert, const Color& color);
111     static void PaintHueRotate(const RSRoundRect& outerRRect, RSCanvas* canvas,
112         const float& hueRotate, const Color& color);
113     void PaintDecoration(const Offset& offset, RSCanvas* canvas, RenderContext& context,
114         const std::shared_ptr<RSImage>& image);
115     void PaintDecoration(const Offset& offset, RSCanvas* canvas, RenderContext& context);
116     static void PaintBorderImage(RefPtr<OHOS::Ace::Decoration>& decoration, Size& paintSize, const Offset& position,
117         RSCanvas* canvas, const std::shared_ptr<RSImage>& image, double dipScale);
118 #endif
119 
120     static void PaintBoxShadows(const std::vector<Shadow>& shadows, const std::shared_ptr<RSNode>& rsNode);
121 
122     void PaintBlur(RenderContext& context, const Dimension& blurRadius);
123 
124     static void AdjustBorderStyle(Border& border);
125 
126     static void PaintBorder(std::shared_ptr<RSNode>& rsNode, Border& border, double dipScale);
127 
128 #ifndef USE_ROSEN_DRAWING
129     static void PaintColorBlend(const SkRRect& outerRRect, SkCanvas* canvas, const Color& colorBlend,
130         const Color& color);
131 #else
132     static void PaintColorBlend(const RSRoundRect& outerRRect, RSCanvas* canvas,
133         const Color& colorBlend, const Color& color);
134 #endif
135 
136     void PaintGradient(RenderContext& context);
137 
138 #ifndef USE_ROSEN_DRAWING
139     bool GetGradientPaint(SkPaint& paint);
140 
141     SkRRect GetBoxOuterRRect(const Offset& offset);
142 #else
143     bool GetGradientPaint(RSBrush& brush);
144     RSRoundRect GetBoxOuterRRect(const Offset& offset);
145 #endif
146 
SetAlpha(uint8_t opacity)147     void SetAlpha(uint8_t opacity)
148     {
149         opacity_ = opacity;
150     }
151 
SetMargin(const EdgePx & margin)152     void SetMargin(const EdgePx& margin)
153     {
154         margin_ = margin;
155     }
156 
SetRenderImage(const RefPtr<RenderImage> & renderImage)157     void SetRenderImage(const RefPtr<RenderImage>& renderImage)
158     {
159         renderImage_ = renderImage;
160     }
161 
SetDecoration(const RefPtr<Decoration> & decoration)162     void SetDecoration(const RefPtr<Decoration>& decoration)
163     {
164         decoration_ = decoration;
165     }
166 
167 #ifndef USE_ROSEN_DRAWING
168     SkRRect GetBoxRRect(const Offset& offset, const Border& border, double shrinkFactor, bool isRound);
169 #else
170     RSRoundRect GetBoxRRect(const Offset& offset, const Border& border,
171         double shrinkFactor, bool isRound);
172 #endif
173 
174     void CheckWidth(const Border& border);
175 
176 #ifndef USE_ROSEN_DRAWING
177     static sk_sp<SkShader> CreateGradientShader(const Gradient& gradient, const SkSize& size, double dipScale);
178 #else
179     static std::shared_ptr<RSShaderEffect> CreateGradientShader(const Gradient& gradient,
180         const RSSize& size, double dipScale);
181 #endif
182 
183 protected:
184 #ifndef USE_ROSEN_DRAWING
185     void PaintColorAndImage(const Offset& offset, SkCanvas* canvas, SkPaint& paint, RenderContext& context);
186 
187     void PaintAllEqualBorder(const SkRRect& rrect, const Border& border, SkCanvas* canvas, SkPaint& paint);
188     void SetBorderStyle(const BorderEdge& borderEdge, SkPaint& paint, bool useDefaultColor = false,
189         double spaceBetweenDot = 0.0, double borderLength = 0.0);
190 
191     void PaintBorder(const Offset& offset, const Border& border, SkCanvas* canvas, SkPaint& paint);
192     void PaintBorderWithLine(const Offset& offset, const Border& border, SkCanvas* canvas, SkPaint& paint);
193     void PaintImage(const Offset& offset, RenderContext& context);
194     sk_sp<SkShader> CreateGradientShader(const Gradient& gradient, const SkSize& size);
195     SkRRect GetOuterRRect(const Offset& offset, const Border& border);
196     SkRRect GetInnerRRect(const Offset& offset, const Border& border);
197     SkRRect GetClipRRect(const Offset& offset, const Border& border);
198     bool CanUseFillStyle(const Border& border, SkPaint& paint);
199     bool CanUsePathRRect(const Border& border, SkPaint& paint);
200 #else
201     void PaintColorAndImage(const Offset& offset, RSCanvas* canvas,
202         RSBrush& brush, RenderContext& context);
203 
204     void PaintAllEqualBorder(const RSRoundRect& rrect, const Border& border,
205         RSCanvas* canvas, RSBrush& brush);
206     void SetBorderStyle(const BorderEdge& borderEdge, RSPen& pen, bool useDefaultColor = false,
207         double spaceBetweenDot = 0.0, double borderLength = 0.0);
208 
209     void PaintBorder(const Offset& offset, const Border& border, RSCanvas* canvas,
210         RSPen& pen);
211     void PaintBorderWithLine(const Offset& offset, const Border& border, RSCanvas* canvas,
212         RSPen& pen);
213     void PaintImage(const Offset& offset, RenderContext& context);
214     std::shared_ptr<RSShaderEffect> CreateGradientShader(const Gradient& gradient,
215         const RSSize& size);
216     RSRoundRect GetOuterRRect(const Offset& offset, const Border& border);
217     RSRoundRect GetInnerRRect(const Offset& offset, const Border& border);
218     RSRoundRect GetClipRRect(const Offset& offset, const Border& border);
219     bool CanUseFillStyle(const Border& border, RSBrush& brush);
220     bool CanUsePathRRect(const Border& border, RSPen& pen);
221 #endif
222     bool CanUseInnerRRect(const Border& border);
223     bool CanUseFourLine(const Border& border);
224     bool CheckBorderEdgeForRRect(const Border& border);
225 
226     double NormalizeToPx(const Dimension& dimension) const;
227     double SliceNormalizePercentToPx(const Dimension& dimension, bool isVertical) const;
228     double WidthNormalizePercentToPx(const Dimension& dimension, bool isVertical) const;
229     double OutsetNormalizePercentToPx(const Dimension& dimension, bool isVertical) const;
230 
GetLayoutSize()231     Size GetLayoutSize() const
232     {
233         return paintRect_.GetSize();
234     }
235 
236     double dipScale_ = 1.0;
237     Rect paintRect_;
238 
239     RefPtr<Decoration> decoration_;
240     Size paintSize_; // exclude margin
241     EdgePx margin_;
242     double scale_ = 0.0;
243     uint8_t opacity_ = UINT8_MAX;
244 
245     RefPtr<RenderImage> renderImage_;
246 #ifndef USE_ROSEN_DRAWING
247     sk_sp<SkImage> image_;
248 #else
249     std::shared_ptr<RSImage> image_;
250 #endif
251 
252     double leftWidth_ = 0.0;
253     double topWidth_ = 0.0;
254     double rightWidth_ = 0.0;
255     double bottomWidth_ = 0.0;
256 
257     double leftSlice_ = 0.0;
258     double topSlice_ = 0.0;
259     double rightSlice_ = 0.0;
260     double bottomSlice_ = 0.0;
261 
262     double leftOutset_ = 0.0;
263     double topOutset_ = 0.0;
264     double rightOutset_ = 0.0;
265     double bottomOutset_ = 0.0;
266 };
267 
268 } // namespace OHOS::Ace
269 
270 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_PAINTER_ROSEN_DECORATION_PAINTER_H
271