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