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     blendMode_ = mode;
105 }
106 
SetFilter(const Filter & filter)107 void Brush::SetFilter(const Filter& filter)
108 {
109     filter_ = filter;
110     hasFilter_ = true;
111 }
112 
GetFilter() const113 const Filter& Brush::GetFilter() const
114 {
115     return filter_;
116 }
117 
SetShaderEffect(std::shared_ptr<ShaderEffect> e)118 void Brush::SetShaderEffect(std::shared_ptr<ShaderEffect> e)
119 {
120     shaderEffect_ = e;
121 }
122 
SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper)123 void Brush::SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper)
124 {
125     blurDrawLooper_ = blurDrawLooper;
126 }
127 
GetLooper() const128 std::shared_ptr<BlurDrawLooper> Brush::GetLooper() const
129 {
130     return blurDrawLooper_;
131 }
132 
SetBlender(std::shared_ptr<Blender> blender)133 void Brush::SetBlender(std::shared_ptr<Blender> blender)
134 {
135     blender_ = blender;
136 }
137 
SetBlenderEnabled(bool blenderEnabled)138 void Brush::SetBlenderEnabled(bool blenderEnabled)
139 {
140     blenderEnabled_ = blenderEnabled;
141 }
142 
SetAntiAlias(bool aa)143 void Brush::SetAntiAlias(bool aa)
144 {
145     antiAlias_ = aa;
146 }
147 
CanComputeFastBounds()148 bool Brush::CanComputeFastBounds()
149 {
150     return StaticFactory::CanComputeFastBounds(*this);
151 }
152 
ComputeFastBounds(const Rect & orig,Rect * storage)153 const Rect& Brush::ComputeFastBounds(const Rect& orig, Rect* storage)
154 {
155     return StaticFactory::ComputeFastBounds(*this, orig, storage);
156 }
157 
Reset()158 void Brush::Reset()
159 {
160     *this = Brush();
161 }
162 
AsBlendMode()163 bool Brush::AsBlendMode()
164 {
165     return StaticFactory::AsBlendMode(*this);
166 }
167 
operator ==(const Brush & b1,const Brush & b2)168 bool operator==(const Brush& b1, const Brush& b2)
169 {
170     return b1.color_ == b2.color_ && b1.blendMode_ == b2.blendMode_ && b1.shaderEffect_ == b2.shaderEffect_ &&
171         b1.blender_ == b2.blender_ && b1.blenderEnabled_ == b2.blenderEnabled_ && b1.colorSpace_ == b2.colorSpace_ &&
172         b1.filter_ == b2.filter_ && b1.antiAlias_ == b2.antiAlias_ && b1.blurDrawLooper_ == b2.blurDrawLooper_;
173 }
174 
operator !=(const Brush & b1,const Brush & b2)175 bool operator!=(const Brush& b1, const Brush& b2)
176 {
177     return !(b1 == b2);
178 }
179 
Dump(std::string & out) const180 void Brush::Dump(std::string& out) const
181 {
182     out += "[color";
183     color_.Dump(out);
184     out += " blendMode:" + std::to_string(static_cast<int>(blendMode_));
185     out += " filter";
186     filter_.Dump(out);
187     if (colorSpace_ != nullptr) {
188         out += " colorSpaceType:" + std::to_string(static_cast<int>(colorSpace_->GetType()));
189     }
190     if (shaderEffect_ != nullptr) {
191         out += " shaderEffectType:" + std::to_string(static_cast<int>(shaderEffect_->GetType()));
192     }
193     out += " isAntiAlias:" + std::string(antiAlias_ ? "true" : "false");
194     out += " blenderEnabled:" + std::string(blenderEnabled_ ? "true" : "false");
195     out += " hasFilter:" + std::string(hasFilter_ ? "true" : "false");
196     out += ']';
197 }
198 } // namespace Drawing
199 } // namespace Rosen
200 } // namespace OHOS
201