1 /*
2  * Copyright (c) 2022-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_BORDER_IMAGE_PAINTER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_PAINTER_BORDER_IMAGE_PAINTER_H
18 
19 #include <cmath>
20 
21 #ifndef USE_ROSEN_DRAWING
22 #include "include/core/SkCanvas.h"
23 #include "include/core/SkPaint.h"
24 #endif
25 
26 #include "base/memory/ace_type.h"
27 #include "base/utils/utils.h"
28 #include "core/components/common/layout/constants.h"
29 #include "core/components/common/properties/decoration.h"
30 #include "core/components/common/properties/edge.h"
31 #ifdef USE_ROSEN_DRAWING
32 #include "core/components_ng/render/drawing.h"
33 #endif
34 
35 
36 namespace OHOS::Ace {
37 class BorderImagePainter : public virtual AceType {
38     DECLARE_ACE_TYPE(BorderImagePainter, AceType);
39 public:
40 #ifndef USE_ROSEN_DRAWING
41     BorderImagePainter(const Size& paintSize, const RefPtr<Decoration>& decoration, const sk_sp<SkImage>& image,
42         double dipscale);
43 #else
44     BorderImagePainter(const Size& paintSize, const RefPtr<Decoration>& decoration,
45         const std::shared_ptr<RSImage>& image, double dipscale);
46 #endif
47     ~BorderImagePainter() = default;
48     void InitPainter();
49     void UpdateExtraOffsetToPaintSize(const Offset& offset);
50 #ifndef USE_ROSEN_DRAWING
51     void PaintBorderImage(const Offset& offset, SkCanvas* canvas, SkPaint& paint);
52     static sk_sp<SkShader> CreateGradientShader(const Gradient& gradient, const SkSize& size, double dipScale);
53 #else
54     void PaintBorderImage(const Offset& offset, RSCanvas* canvas, RSBrush& brush);
55     static std::shared_ptr<RSShaderEffect> CreateGradientShader(const Gradient& gradient,
56         const RSSize& size, double dipScale);
57 #endif
58 private:
59 #ifndef USE_ROSEN_DRAWING
60     void PaintBorderImageCorners(const Offset& offset, SkCanvas* canvas, SkPaint& paint);
61     void PaintBorderImageStretch(const Offset& offset, SkCanvas* canvas, SkPaint& paint);
62     void PaintBorderImageRound(const Offset& offset, SkCanvas* canvas, SkPaint& paint);
63     void PaintBorderImageSpace(const Offset& offset, SkCanvas* canvas, SkPaint& paint);
64     void PaintBorderImageRepeat(const Offset& offset, SkCanvas* canvas, SkPaint& paint);
65     void FillBorderImageCenter(const Offset& offset, SkCanvas* canvas, SkPaint& paint);
66 #else
67     void PaintBorderImageCorners(const Offset& offset, RSCanvas* canvas, RSBrush& brush);
68     void PaintBorderImageStretch(const Offset& offset, RSCanvas* canvas, RSBrush& brush);
69     void PaintBorderImageRound(const Offset& offset, RSCanvas* canvas, RSBrush& brush);
70     void PaintBorderImageSpace(const Offset& offset, RSCanvas* canvas, RSBrush& brush);
71     void PaintBorderImageRepeat(const Offset& offset, RSCanvas* canvas, RSBrush& brush);
72     void FillBorderImageCenter(const Offset& offset, RSCanvas* canvas, RSBrush& brush);
73 #endif
74 
75     void InitBorderImageWidth(Border& border, RefPtr<BorderImage>& borderImage);
76     void InitBorderImageSlice(RefPtr<BorderImage>& borderImage);
77     void InitBorderImageOutset(Border& border, RefPtr<BorderImage>& borderImage);
78     void ParseNegativeNumberToZeroOrCeil(double& value);
79 
80     Size paintSize_;
81     RefPtr<Decoration> decoration_;
82 #ifndef USE_ROSEN_DRAWING
83     sk_sp<SkImage> image_;
84 #else
85     std::shared_ptr<RSImage> image_;
86 #endif
87 
88     double imageWidth_ = 0.0;
89     double imageHeight_ = 0.0;
90     double imageCenterWidth_ = 0.0;
91     double imageCenterHeight_ = 0.0;
92     double borderCenterWidth_ = 0.0;
93     double borderCenterHeight_ = 0.0;
94 
95     double dipscale_ = 0.0;
96     double leftWidth_ = 0.0;
97     double topWidth_ = 0.0;
98     double rightWidth_ = 0.0;
99     double bottomWidth_ = 0.0;
100 
101     double leftSlice_ = 0.0;
102     double topSlice_ = 0.0;
103     double rightSlice_ = 0.0;
104     double bottomSlice_ = 0.0;
105 
106     double leftOutset_ = 0.0;
107     double topOutset_ = 0.0;
108     double rightOutset_ = 0.0;
109     double bottomOutset_ = 0.0;
110 
111 #ifndef USE_ROSEN_DRAWING
112     SkRect srcRectLeft_;
113     SkRect srcRectRight_;
114     SkRect srcRectTop_;
115     SkRect srcRectBottom_;
116 #else
117     RSRect srcRectLeft_;
118     RSRect srcRectRight_;
119     RSRect srcRectTop_;
120     RSRect srcRectBottom_;
121 #endif
122 
123     bool paintCornersOnly_ = false;
124 };
125 }
126 
127 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_PAINTER_ROSEN_DEBUG_BOUNDARY_PAINTER_H