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 "draw/brush.h"
17
18 #include "config/DrawingConfig.h"
19 #include "static_factory.h"
20
21 namespace OHOS {
22 namespace Rosen {
23 namespace Drawing {
Brush()24 Brush::Brush() noexcept
25 : color_(),
26 blendMode_(BlendMode::SRC_OVER),
27 filter_(),
28 colorSpace_(nullptr),
29 shaderEffect_(nullptr),
30 blender_(nullptr),
31 blurDrawLooper_(nullptr),
32 antiAlias_(false)
33 {}
34
Brush(const Color & c)35 Brush::Brush(const Color& c) noexcept
36 : color_(c),
37 blendMode_(BlendMode::SRC_OVER),
38 filter_(),
39 colorSpace_(nullptr),
40 shaderEffect_(nullptr),
41 blender_(nullptr),
42 blurDrawLooper_(nullptr),
43 antiAlias_(false)
44 {}
45
Brush(int rgba)46 Brush::Brush(int rgba) noexcept
47 : color_(rgba),
48 blendMode_(BlendMode::SRC_OVER),
49 filter_(),
50 colorSpace_(nullptr),
51 shaderEffect_(nullptr),
52 blender_(nullptr),
53 blurDrawLooper_(nullptr),
54 antiAlias_(false)
55 {}
56
Brush(std::shared_ptr<ShaderEffect> e)57 Brush::Brush(std::shared_ptr<ShaderEffect> e) noexcept
58 : color_(), blendMode_(BlendMode::SRC_OVER), filter_(), colorSpace_(nullptr), shaderEffect_(e), blender_(nullptr),
59 blurDrawLooper_(nullptr), antiAlias_(false)
60 {}
61
GetColor() const62 const Color& Brush::GetColor() const
63 {
64 return color_;
65 }
66
SetColor(const Color & c)67 void Brush::SetColor(const Color& c)
68 {
69 color_ = c;
70 }
71
SetColor(uint32_t c)72 void Brush::SetColor(uint32_t c)
73 {
74 color_.SetColorQuad(c);
75 }
76
SetARGB(int a,int r,int g,int b)77 void Brush::SetARGB(int a, int r, int g, int b)
78 {
79 color_.SetRgb(r, g, b, a);
80 }
81
GetColor4f()82 const Color4f& Brush::GetColor4f()
83 {
84 return color_.GetColor4f();
85 }
86
SetColor(const Color4f & cf,std::shared_ptr<ColorSpace> s)87 void Brush::SetColor(const Color4f& cf, std::shared_ptr<ColorSpace> s)
88 {
89 color_.SetRgbF(cf.redF_, cf.greenF_, cf.blueF_, cf.alphaF_);
90 colorSpace_ = s;
91 }
92
SetAlpha(uint32_t a)93 void Brush::SetAlpha(uint32_t a)
94 {
95 color_.SetAlpha(a);
96 }
97
SetAlphaF(scalar a)98 void Brush::SetAlphaF(scalar a)
99 {
100 color_.SetAlphaF(a);
101 }
102
SetBlendMode(const BlendMode & mode)103 void Brush::SetBlendMode(const BlendMode& mode)
104 {
105 blender_ = nullptr;
106 blendMode_ = mode;
107 }
108
SetFilter(const Filter & filter)109 void Brush::SetFilter(const Filter& filter)
110 {
111 filter_ = filter;
112 hasFilter_ = true;
113 }
114
GetFilter() const115 const Filter& Brush::GetFilter() const
116 {
117 return filter_;
118 }
119
SetShaderEffect(std::shared_ptr<ShaderEffect> e)120 void Brush::SetShaderEffect(std::shared_ptr<ShaderEffect> e)
121 {
122 #ifdef DRAWING_DISABLE_API
123 if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_SHADER)) {
124 shaderEffect_ = nullptr;
125 return;
126 }
127 #endif
128 shaderEffect_ = e;
129 }
130
SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper)131 void Brush::SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper)
132 {
133 blurDrawLooper_ = blurDrawLooper;
134 }
135
GetLooper() const136 std::shared_ptr<BlurDrawLooper> Brush::GetLooper() const
137 {
138 return blurDrawLooper_;
139 }
140
SetBlender(std::shared_ptr<Blender> blender)141 void Brush::SetBlender(std::shared_ptr<Blender> blender)
142 {
143 #ifdef DRAWING_DISABLE_API
144 if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_BLENDER)) {
145 blender_ = nullptr;
146 return;
147 }
148 #endif
149 blender_ = blender;
150 }
151
SetBlenderEnabled(bool blenderEnabled)152 void Brush::SetBlenderEnabled(bool blenderEnabled)
153 {
154 blenderEnabled_ = blenderEnabled;
155 }
156
SetAntiAlias(bool aa)157 void Brush::SetAntiAlias(bool aa)
158 {
159 antiAlias_ = aa;
160 }
161
CanComputeFastBounds()162 bool Brush::CanComputeFastBounds()
163 {
164 return StaticFactory::CanComputeFastBounds(*this);
165 }
166
ComputeFastBounds(const Rect & orig,Rect * storage)167 const Rect& Brush::ComputeFastBounds(const Rect& orig, Rect* storage)
168 {
169 return StaticFactory::ComputeFastBounds(*this, orig, storage);
170 }
171
Reset()172 void Brush::Reset()
173 {
174 *this = Brush();
175 }
176
AsBlendMode()177 bool Brush::AsBlendMode()
178 {
179 return StaticFactory::AsBlendMode(*this);
180 }
181
operator ==(const Brush & b1,const Brush & b2)182 bool operator==(const Brush& b1, const Brush& b2)
183 {
184 return b1.color_ == b2.color_ && b1.blendMode_ == b2.blendMode_ && b1.shaderEffect_ == b2.shaderEffect_ &&
185 b1.blender_ == b2.blender_ && b1.blenderEnabled_ == b2.blenderEnabled_ && b1.colorSpace_ == b2.colorSpace_ &&
186 b1.filter_ == b2.filter_ && b1.antiAlias_ == b2.antiAlias_ && b1.blurDrawLooper_ == b2.blurDrawLooper_;
187 }
188
operator !=(const Brush & b1,const Brush & b2)189 bool operator!=(const Brush& b1, const Brush& b2)
190 {
191 return !(b1 == b2);
192 }
193
Dump(std::string & out) const194 void Brush::Dump(std::string& out) const
195 {
196 out += "[color";
197 color_.Dump(out);
198 out += " blendMode:" + std::to_string(static_cast<int>(blendMode_));
199 out += " filter";
200 filter_.Dump(out);
201 if (colorSpace_ != nullptr) {
202 out += " colorSpaceType:" + std::to_string(static_cast<int>(colorSpace_->GetType()));
203 }
204 if (shaderEffect_ != nullptr) {
205 out += " shaderEffectType:" + std::to_string(static_cast<int>(shaderEffect_->GetType()));
206 }
207 out += " isAntiAlias:" + std::string(antiAlias_ ? "true" : "false");
208 out += " blenderEnabled:" + std::string(blenderEnabled_ ? "true" : "false");
209 out += " hasFilter:" + std::string(hasFilter_ ? "true" : "false");
210 out += ']';
211 }
212 } // namespace Drawing
213 } // namespace Rosen
214 } // namespace OHOS
215