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 /* ExtendShader */
ShaderEffect(ShaderEffectType t,std::shared_ptr<ExtendObject> object)93 ShaderEffect::ShaderEffect(ShaderEffectType t, std::shared_ptr<ExtendObject> object) noexcept
94 : type_(t), object_(object) {}
95
ShaderEffect()96 ShaderEffect::ShaderEffect() noexcept
97 : type_(ShaderEffect::ShaderEffectType::NO_TYPE), impl_(ImplFactory::CreateShaderEffectImpl())
98 {}
99
ShaderEffect(ShaderEffectType t)100 ShaderEffect::ShaderEffect(ShaderEffectType t) noexcept : type_(t), impl_(ImplFactory::CreateShaderEffectImpl()) {}
101
GetType() const102 ShaderEffect::ShaderEffectType ShaderEffect::GetType() const
103 {
104 return type_;
105 }
106
CreateColorShader(ColorQuad color)107 std::shared_ptr<ShaderEffect> ShaderEffect::CreateColorShader(ColorQuad color)
108 {
109 return std::make_shared<ShaderEffect>(ShaderEffect::ShaderEffectType::COLOR_SHADER, color);
110 }
111
CreateColorSpaceShader(const Color4f & color,std::shared_ptr<ColorSpace> colorSpace)112 std::shared_ptr<ShaderEffect> ShaderEffect::CreateColorSpaceShader(const Color4f& color,
113 std::shared_ptr<ColorSpace> colorSpace)
114 {
115 return std::make_shared<ShaderEffect>(ShaderEffect::ShaderEffectType::COLOR_SHADER, color, colorSpace);
116 }
117
CreateBlendShader(ShaderEffect & dst,ShaderEffect & src,BlendMode mode)118 std::shared_ptr<ShaderEffect> ShaderEffect::CreateBlendShader(ShaderEffect& dst, ShaderEffect& src, BlendMode mode)
119 {
120 return std::make_shared<ShaderEffect>(ShaderEffect::ShaderEffectType::BLEND, dst, src, mode);
121 }
122
CreateImageShader(const Image & image,TileMode tileX,TileMode tileY,const SamplingOptions & sampling,const Matrix & matrix)123 std::shared_ptr<ShaderEffect> ShaderEffect::CreateImageShader(
124 const Image& image, TileMode tileX, TileMode tileY, const SamplingOptions& sampling, const Matrix& matrix)
125 {
126 return std::make_shared<ShaderEffect>(ShaderEffect::ShaderEffectType::IMAGE, image, tileX, tileY, sampling, matrix);
127 }
128
CreatePictureShader(const Picture & picture,TileMode tileX,TileMode tileY,FilterMode mode,const Matrix & matrix,const Rect & rect)129 std::shared_ptr<ShaderEffect> ShaderEffect::CreatePictureShader(
130 const Picture& picture, TileMode tileX, TileMode tileY, FilterMode mode, const Matrix& matrix, const Rect& rect)
131 {
132 return std::make_shared<ShaderEffect>(
133 ShaderEffect::ShaderEffectType::PICTURE, picture, tileX, tileY, mode, matrix, rect);
134 }
135
CreateLinearGradient(const Point & startPt,const Point & endPt,const std::vector<ColorQuad> & colors,const std::vector<scalar> & pos,TileMode mode,const Matrix * matrix)136 std::shared_ptr<ShaderEffect> ShaderEffect::CreateLinearGradient(const Point& startPt, const Point& endPt,
137 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, const Matrix *matrix)
138 {
139 return std::make_shared<ShaderEffect>(
140 ShaderEffect::ShaderEffectType::LINEAR_GRADIENT, startPt, endPt, colors, pos, mode, matrix);
141 }
142
CreateRadialGradient(const Point & centerPt,scalar radius,const std::vector<ColorQuad> & colors,const std::vector<scalar> & pos,TileMode mode,const Matrix * matrix)143 std::shared_ptr<ShaderEffect> ShaderEffect::CreateRadialGradient(const Point& centerPt, scalar radius,
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::RADIAL_GRADIENT, centerPt, radius, colors, pos, mode, matrix);
148 }
149
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)150 std::shared_ptr<ShaderEffect> ShaderEffect::CreateTwoPointConical(const Point& startPt, scalar startRadius,
151 const Point& endPt, scalar endRadius, const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos,
152 TileMode mode, const Matrix *matrix)
153 {
154 return std::make_shared<ShaderEffect>(
155 ShaderEffect::ShaderEffectType::CONICAL_GRADIENT, startPt, startRadius, endPt, endRadius, colors, pos, mode,
156 matrix);
157 }
158
CreateSweepGradient(const Point & centerPt,const std::vector<ColorQuad> & colors,const std::vector<scalar> & pos,TileMode mode,scalar startAngle,scalar endAngle,const Matrix * matrix)159 std::shared_ptr<ShaderEffect> ShaderEffect::CreateSweepGradient(const Point& centerPt,
160 const std::vector<ColorQuad>& colors, const std::vector<scalar>& pos, TileMode mode, scalar startAngle,
161 scalar endAngle, const Matrix *matrix)
162 {
163 return std::make_shared<ShaderEffect>(
164 ShaderEffect::ShaderEffectType::SWEEP_GRADIENT, centerPt, colors, pos, mode, startAngle, endAngle, matrix);
165 }
166
CreateExtendShader(std::shared_ptr<ExtendObject> object)167 std::shared_ptr<ShaderEffect> ShaderEffect::CreateExtendShader(std::shared_ptr<ExtendObject> object)
168 {
169 return std::make_shared<ShaderEffect>(ShaderEffect::ShaderEffectType::EXTEND_SHADER, object);
170 }
171
Serialize() const172 std::shared_ptr<Data> ShaderEffect::Serialize() const
173 {
174 return impl_->Serialize();
175 }
176
Deserialize(std::shared_ptr<Data> data)177 bool ShaderEffect::Deserialize(std::shared_ptr<Data> data)
178 {
179 return impl_->Deserialize(data);
180 }
181 } // namespace Drawing
182 } // namespace Rosen
183 } // namespace OHOS
184