• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SHADER_EFFECT_H
17 #define SHADER_EFFECT_H
18 
19 #include "impl_interface/shader_effect_impl.h"
20 #include "utils/drawing_macros.h"
21 #include "utils/extend_object.h"
22 #include "draw/sdf_shaper_base.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 namespace Drawing {
27 enum class TileMode {
28     CLAMP,
29     REPEAT,
30     MIRROR,
31     DECAL,
32 };
33 
34 class DRAWING_API ShaderEffect {
35 public:
36     enum class ShaderEffectType : int32_t {
37         NO_TYPE,
38         COLOR_SHADER,
39         BLEND,
40         IMAGE,
41         PICTURE,
42         LINEAR_GRADIENT,
43         RADIAL_GRADIENT,
44         CONICAL_GRADIENT,
45         SWEEP_GRADIENT,
46         LIGHT_UP,
47         EXTEND_SHADER,
48         SDF_SHADER
49     };
50 
51     /**
52      * @brief Create a ShaderEffect that ignores the color in the paint, and uses the
53      * specified color. Note: like all shaders, at draw time the paint's alpha
54      * will be respected, and is applied to the specified color.
55      *
56      * @param color the specified color
57      * @return A shared pointer to ShaderEffect
58      */
59     static std::shared_ptr<ShaderEffect> CreateColorShader(ColorQuad color);
60 
61     static std::shared_ptr<ShaderEffect> CreateColorSpaceShader(const Color4f& color,
62         std::shared_ptr<ColorSpace> colorSpace);
63 
64     static std::shared_ptr<ShaderEffect> CreateBlendShader(ShaderEffect& dst, ShaderEffect& src, BlendMode mode);
65 
66     static std::shared_ptr<ShaderEffect> CreateImageShader(
67         const Image& image, TileMode tileX, TileMode tileY, const SamplingOptions& sampling, const Matrix& matrix);
68 
69     static std::shared_ptr<ShaderEffect> CreatePictureShader(const Picture& picture, TileMode tileX, TileMode tileY,
70         FilterMode mode, const Matrix& matrix, const Rect& rect);
71 
72     static std::shared_ptr<ShaderEffect> CreateLinearGradient(const Point& startPt, const Point& endPt,
73         const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode,
74         const Matrix *matrix = nullptr);
75 
76     static std::shared_ptr<ShaderEffect> CreateRadialGradient(const Point& centerPt, scalar radius,
77         const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode,
78         const Matrix *matrix = nullptr);
79 
80     static std::shared_ptr<ShaderEffect> CreateTwoPointConical(const Point& startPt, scalar startRadius,
81         const Point& endPt, scalar endRadius, const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos,
82         TileMode mode, const Matrix *matrix = nullptr);
83 
84     static std::shared_ptr<ShaderEffect> CreateSweepGradient(const Point& centerPt,
85         const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, scalar startAngle,
86         scalar endAngle, const Matrix* matrix = nullptr);
87 
88     static std::shared_ptr<ShaderEffect> CreateExtendShader(std::shared_ptr<ExtendObject>);
89 
90     static std::shared_ptr<ShaderEffect> CreateSdfShader(const SDFShapeBase& shape);
91 
92     virtual ~ShaderEffect() = default;
93 
94     ShaderEffectType GetType() const;
95 
GetDrawingType()96     virtual DrawingType GetDrawingType() const
97     {
98         return DrawingType::COMMON;
99     }
100 
101     template<typename T>
GetImpl()102     T* GetImpl() const
103     {
104         return impl_->DowncastingTo<T>();
105     }
106 
107     /* ColorShader */
108     ShaderEffect(ShaderEffectType t, ColorQuad color) noexcept;
109 
110     /* ColorShader With ColorSpace */
111     ShaderEffect(ShaderEffectType t, const Color4f& color, std::shared_ptr<ColorSpace> colorSpace) noexcept;
112 
113     /* BlendShader */
114     ShaderEffect(ShaderEffectType t, ShaderEffect& dst, ShaderEffect& src, BlendMode mode) noexcept;
115 
116     /* ImageShader */
117     ShaderEffect(ShaderEffectType t, const Image& image, TileMode tileX, TileMode tileY,
118         const SamplingOptions& sampling, const Matrix& matrix) noexcept;
119 
120     /* PictureShader */
121     ShaderEffect(ShaderEffectType t, const Picture& picture, TileMode tileX, TileMode tileY, FilterMode mode,
122         const Matrix& matrix, const Rect& rect) noexcept;
123 
124     /* LinearGradient */
125     ShaderEffect(ShaderEffectType t, const Point& startPt, const Point& endPt, const std::vector<ColorQuad>& colors,
126         const std::vector<scalar>& pos, TileMode mode, const Matrix *matrix = nullptr) noexcept;
127 
128     /* RadialGradient */
129     ShaderEffect(ShaderEffectType t, const Point& centerPt, scalar radius, const std::vector<ColorQuad>& colors,
130         const std::vector<scalar>& pos, TileMode mode, const Matrix *matrix = nullptr) noexcept;
131 
132     /* TwoPointConicalGradient */
133     ShaderEffect(ShaderEffectType t, const Point& startPt, scalar startRadius, const Point& endPt, scalar endRadius,
134         const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode,
135         const Matrix *matrix = nullptr) noexcept;
136 
137     /* SweepGradient */
138     ShaderEffect(ShaderEffectType t, const Point& centerPt, const std::vector<ColorQuad>& colors,
139         const std::vector<scalar>& pos, TileMode mode, scalar startAngle, scalar endAngle,
140         const Matrix *matrix = nullptr) noexcept;
141 
142     /* LightUpShader */
143     ShaderEffect(ShaderEffectType t, const float& lightUpDeg, ShaderEffect& imageShader) noexcept;
144 
145     /* ExtendShader */
146     ShaderEffect(ShaderEffectType t, std::shared_ptr<ExtendObject> object) noexcept;
147 
148     ShaderEffect(ShaderEffectType t) noexcept;
149 
150     /* SdfShader*/
151     ShaderEffect(ShaderEffectType t, const SDFShapeBase& shape) noexcept;
152 
153     ShaderEffect() noexcept;
154 
GetExtendObject()155     std::shared_ptr<ExtendObject> GetExtendObject() { return object_; }
156 
157     std::shared_ptr<Data> Serialize() const;
158     bool Deserialize(std::shared_ptr<Data> data);
159 private:
160     ShaderEffectType type_;
161     std::shared_ptr<ShaderEffectImpl> impl_;
162     std::shared_ptr<ExtendObject> object_;
163 };
164 } // namespace Drawing
165 } // namespace Rosen
166 } // namespace OHOS
167 #endif
168