1 /*
2 * Copyright (c) 2021 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 "effect/shader_effect.h"
17
18 #include "impl_factory.h"
19
20 namespace OHOS {
21 namespace Rosen {
22 namespace Drawing {
23 /* ColorShader */
ShaderEffect(ShaderEffectType t,ColorQuad color)24 ShaderEffect::ShaderEffect(ShaderEffectType t, ColorQuad color) noexcept : ShaderEffect(t)
25 {
26 impl_->InitWithColor(color);
27 }
28
29 /* ColorShader With ColorSpace */
ShaderEffect(ShaderEffectType t,const Color4f & color,std::shared_ptr<ColorSpace> colorSpace)30 ShaderEffect::ShaderEffect(ShaderEffectType t, const Color4f& color, std::shared_ptr<ColorSpace> colorSpace) noexcept
31 : ShaderEffect(t)
32 {
33 impl_->InitWithColorSpace(color, colorSpace);
34 }
35
36 /* BlendShader */
ShaderEffect(ShaderEffectType t,ShaderEffect & dst,ShaderEffect & src,BlendMode mode)37 ShaderEffect::ShaderEffect(ShaderEffectType t, ShaderEffect& dst, ShaderEffect& src, BlendMode mode) noexcept
38 : ShaderEffect(t)
39 {
40 impl_->InitWithBlend(dst, src, mode);
41 }
42
43 /* ImageShader */
ShaderEffect(ShaderEffectType t,const Image & image,TileMode tileX,TileMode tileY,const SamplingOptions & sampling,const Matrix & matrix)44 ShaderEffect::ShaderEffect(ShaderEffectType t, const Image& image, TileMode tileX, TileMode tileY,
45 const SamplingOptions& sampling, const Matrix& matrix) noexcept
46 : ShaderEffect(t)
47 {
48 impl_->InitWithImage(image, tileX, tileY, sampling, matrix);
49 }
50
51 /* PictureShader */
ShaderEffect(ShaderEffectType t,const Picture & picture,TileMode tileX,TileMode tileY,FilterMode mode,const Matrix & matrix,const Rect & rect)52 ShaderEffect::ShaderEffect(ShaderEffectType t, const Picture& picture, TileMode tileX, TileMode tileY, FilterMode mode,
53 const Matrix& matrix, const Rect& rect) noexcept
54 : ShaderEffect(t)
55 {
56 impl_->InitWithPicture(picture, tileX, tileY, mode, matrix, rect);
57 }
58
59 /* LinearGradient */
ShaderEffect(ShaderEffectType t,const Point & startPt,const Point & endPt,const std::vector<ColorQuad> & colors,const std::vector<scalar> & pos,TileMode mode,const Matrix * matrix)60 ShaderEffect::ShaderEffect(ShaderEffectType t, const Point& startPt, const Point& endPt,
61 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, const Matrix *matrix) noexcept
62 : ShaderEffect(t)
63 {
64 impl_->InitWithLinearGradient(startPt, endPt, colors, pos, mode, matrix);
65 }
66
67 /* RadialGradient */
ShaderEffect(ShaderEffectType t,const Point & centerPt,scalar radius,const std::vector<ColorQuad> & colors,const std::vector<scalar> & pos,TileMode mode,const Matrix * matrix)68 ShaderEffect::ShaderEffect(ShaderEffectType t, const Point& centerPt, scalar radius,
69 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, const Matrix *matrix) noexcept
70 : ShaderEffect(t)
71 {
72 impl_->InitWithRadialGradient(centerPt, radius, colors, pos, mode, matrix);
73 }
74
75 /* TwoPointConicalGradient */
ShaderEffect(ShaderEffectType t,const Point & startPt,scalar startRadius,const Point & endPt,scalar endRadius,const std::vector<ColorQuad> & colors,const std::vector<scalar> & pos,TileMode mode,const Matrix * matrix)76 ShaderEffect::ShaderEffect(ShaderEffectType t, const Point& startPt, scalar startRadius, const Point& endPt,
77 scalar endRadius, const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode,
78 const Matrix *matrix) noexcept
79 : ShaderEffect(t)
80 {
81 impl_->InitWithTwoPointConical(startPt, startRadius, endPt, endRadius, colors, pos, mode, matrix);
82 }
83
84 /* SweepGradient */
ShaderEffect(ShaderEffectType t,const Point & centerPt,const std::vector<ColorQuad> & colors,const std::vector<scalar> & pos,TileMode mode,scalar startAngle,scalar endAngle,const Matrix * matrix)85 ShaderEffect::ShaderEffect(ShaderEffectType t, const Point& centerPt, const std::vector<ColorQuad>& colors,
86 const std::vector<scalar>& pos, TileMode mode, scalar startAngle, scalar endAngle, const Matrix *matrix) noexcept
87 : ShaderEffect(t)
88 {
89 impl_->InitWithSweepGradient(centerPt, colors, pos, mode, startAngle, endAngle, matrix);
90 }
91
92 /* LightUpShader */
ShaderEffect(ShaderEffectType t,const float & lightUpDeg,ShaderEffect & imageShader)93 ShaderEffect::ShaderEffect(ShaderEffectType t, const float& lightUpDeg, ShaderEffect& imageShader) noexcept
94 : ShaderEffect(t)
95 {
96 impl_->InitWithLightUp(lightUpDeg, imageShader);
97 }
98
99 /* ExtendShader */
ShaderEffect(ShaderEffectType t,std::shared_ptr<ExtendObject> object)100 ShaderEffect::ShaderEffect(ShaderEffectType t, std::shared_ptr<ExtendObject> object) noexcept
101 : type_(t), object_(object) {}
102
ShaderEffect()103 ShaderEffect::ShaderEffect() noexcept
104 : type_(ShaderEffect::ShaderEffectType::NO_TYPE), impl_(ImplFactory::CreateShaderEffectImpl())
105 {}
106
ShaderEffect(ShaderEffectType t)107 ShaderEffect::ShaderEffect(ShaderEffectType t) noexcept : type_(t), impl_(ImplFactory::CreateShaderEffectImpl()) {}
108
GetType() const109 ShaderEffect::ShaderEffectType ShaderEffect::GetType() const
110 {
111 return type_;
112 }
113
CreateColorShader(ColorQuad color)114 std::shared_ptr<ShaderEffect> ShaderEffect::CreateColorShader(ColorQuad color)
115 {
116 return std::make_shared<ShaderEffect>(ShaderEffect::ShaderEffectType::COLOR_SHADER, color);
117 }
118
CreateColorSpaceShader(const Color4f & color,std::shared_ptr<ColorSpace> colorSpace)119 std::shared_ptr<ShaderEffect> ShaderEffect::CreateColorSpaceShader(const Color4f& color,
120 std::shared_ptr<ColorSpace> colorSpace)
121 {
122 return std::make_shared<ShaderEffect>(ShaderEffect::ShaderEffectType::COLOR_SHADER, color, colorSpace);
123 }
124
CreateBlendShader(ShaderEffect & dst,ShaderEffect & src,BlendMode mode)125 std::shared_ptr<ShaderEffect> ShaderEffect::CreateBlendShader(ShaderEffect& dst, ShaderEffect& src, BlendMode mode)
126 {
127 return std::make_shared<ShaderEffect>(ShaderEffect::ShaderEffectType::BLEND, dst, src, mode);
128 }
129
CreateImageShader(const Image & image,TileMode tileX,TileMode tileY,const SamplingOptions & sampling,const Matrix & matrix)130 std::shared_ptr<ShaderEffect> ShaderEffect::CreateImageShader(
131 const Image& image, TileMode tileX, TileMode tileY, const SamplingOptions& sampling, const Matrix& matrix)
132 {
133 return std::make_shared<ShaderEffect>(ShaderEffect::ShaderEffectType::IMAGE, image, tileX, tileY, sampling, matrix);
134 }
135
CreatePictureShader(const Picture & picture,TileMode tileX,TileMode tileY,FilterMode mode,const Matrix & matrix,const Rect & rect)136 std::shared_ptr<ShaderEffect> ShaderEffect::CreatePictureShader(
137 const Picture& picture, TileMode tileX, TileMode tileY, FilterMode mode, const Matrix& matrix, const Rect& rect)
138 {
139 return std::make_shared<ShaderEffect>(
140 ShaderEffect::ShaderEffectType::PICTURE, picture, tileX, tileY, mode, matrix, rect);
141 }
142
CreateLinearGradient(const Point & startPt,const Point & endPt,const std::vector<ColorQuad> & colors,const std::vector<scalar> & pos,TileMode mode,const Matrix * matrix)143 std::shared_ptr<ShaderEffect> ShaderEffect::CreateLinearGradient(const Point& startPt, const Point& endPt,
144 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, const Matrix *matrix)
145 {
146 return std::make_shared<ShaderEffect>(
147 ShaderEffect::ShaderEffectType::LINEAR_GRADIENT, startPt, endPt, colors, pos, mode, matrix);
148 }
149
CreateRadialGradient(const Point & centerPt,scalar radius,const std::vector<ColorQuad> & colors,const std::vector<scalar> & pos,TileMode mode,const Matrix * matrix)150 std::shared_ptr<ShaderEffect> ShaderEffect::CreateRadialGradient(const Point& centerPt, scalar radius,
151 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, const Matrix *matrix)
152 {
153 return std::make_shared<ShaderEffect>(
154 ShaderEffect::ShaderEffectType::RADIAL_GRADIENT, centerPt, radius, colors, pos, mode, matrix);
155 }
156
CreateTwoPointConical(const Point & startPt,scalar startRadius,const Point & endPt,scalar endRadius,const std::vector<ColorQuad> & colors,const std::vector<scalar> & pos,TileMode mode,const Matrix * matrix)157 std::shared_ptr<ShaderEffect> ShaderEffect::CreateTwoPointConical(const Point& startPt, scalar startRadius,
158 const Point& endPt, scalar endRadius, const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos,
159 TileMode mode, const Matrix *matrix)
160 {
161 return std::make_shared<ShaderEffect>(
162 ShaderEffect::ShaderEffectType::CONICAL_GRADIENT, startPt, startRadius, endPt, endRadius, colors, pos, mode,
163 matrix);
164 }
165
CreateSweepGradient(const Point & centerPt,const std::vector<ColorQuad> & colors,const std::vector<scalar> & pos,TileMode mode,scalar startAngle,scalar endAngle,const Matrix * matrix)166 std::shared_ptr<ShaderEffect> ShaderEffect::CreateSweepGradient(const Point& centerPt,
167 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, scalar startAngle,
168 scalar endAngle, const Matrix *matrix)
169 {
170 return std::make_shared<ShaderEffect>(
171 ShaderEffect::ShaderEffectType::SWEEP_GRADIENT, centerPt, colors, pos, mode, startAngle, endAngle, matrix);
172 }
173
CreateLightUp(const float & lightUpDeg,ShaderEffect & imageShader)174 std::shared_ptr<ShaderEffect> ShaderEffect::CreateLightUp(const float& lightUpDeg, ShaderEffect& imageShader)
175 {
176 return std::make_shared<ShaderEffect>(ShaderEffect::ShaderEffectType::LIGHT_UP, lightUpDeg, imageShader);
177 }
178
CreateExtendShader(std::shared_ptr<ExtendObject> object)179 std::shared_ptr<ShaderEffect> ShaderEffect::CreateExtendShader(std::shared_ptr<ExtendObject> object)
180 {
181 return std::make_shared<ShaderEffect>(ShaderEffect::ShaderEffectType::EXTEND_SHADER, object);
182 }
183
Serialize() const184 std::shared_ptr<Data> ShaderEffect::Serialize() const
185 {
186 return impl_->Serialize();
187 }
188
Deserialize(std::shared_ptr<Data> data)189 bool ShaderEffect::Deserialize(std::shared_ptr<Data> data)
190 {
191 return impl_->Deserialize(data);
192 }
193 } // namespace Drawing
194 } // namespace Rosen
195 } // namespace OHOS
196