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