1 /*
2 * Copyright (c) 2023 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 <string>
17 #include "draw/paint.h"
18
19 namespace OHOS {
20 namespace Rosen {
21 namespace Drawing {
Paint()22 Paint::Paint() noexcept
23 : filter_() {}
24
Paint(const Paint & other)25 Paint::Paint(const Paint& other) noexcept
26 {
27 // Tell the compiler there is no alias and to select wider load/store instructions.
28 bool antiAlias = other.antiAlias_;
29 bool blenderEnabled = other.blenderEnabled_;
30 bool hasFilter = other.hasFilter_;
31 PaintStyle style = other.style_;
32 BlendMode blendMode = other.blendMode_;
33 scalar width = other.width_;
34 scalar miterLimit = other.miterLimit_;
35 Pen::JoinStyle join = other.join_;
36 Pen::CapStyle cap = other.cap_;
37 antiAlias_ = antiAlias;
38 blenderEnabled_ = blenderEnabled;
39 hasFilter_ = hasFilter;
40 style_ = style;
41 blendMode_ = blendMode;
42 width_ = width;
43 miterLimit_ = miterLimit;
44 join_ = join;
45 cap_ = cap;
46
47 colorSpace_ = other.colorSpace_;
48 shaderEffect_ = other.shaderEffect_;
49 pathEffect_ = other.pathEffect_;
50 blender_ = other.blender_;
51 blurDrawLooper_ = other.blurDrawLooper_;
52 color_ = other.color_;
53 if (other.hasFilter_) {
54 filter_ = other.filter_;
55 } else {
56 filter_.Reset();
57 }
58 }
59
Paint(const Color & c,std::shared_ptr<ColorSpace> colorSpace)60 Paint::Paint(const Color& c, std::shared_ptr<ColorSpace> colorSpace) noexcept
61 : colorSpace_(colorSpace), color_(c), filter_() {}
62
operator =(const Paint & other)63 Paint& Paint::operator=(const Paint& other)
64 {
65 // Tell the compiler there is no alias and to select wider load/store instructions.
66 bool antiAlias = other.antiAlias_;
67 bool blenderEnabled = other.blenderEnabled_;
68 bool hasFilter = other.hasFilter_;
69 PaintStyle style = other.style_;
70 BlendMode blendMode = other.blendMode_;
71 scalar width = other.width_;
72 scalar miterLimit = other.miterLimit_;
73 Pen::JoinStyle join = other.join_;
74 Pen::CapStyle cap = other.cap_;
75 antiAlias_ = antiAlias;
76 blenderEnabled_ = blenderEnabled;
77 hasFilter_ = hasFilter;
78 style_ = style;
79 blendMode_ = blendMode;
80 width_ = width;
81 miterLimit_ = miterLimit;
82 join_ = join;
83 cap_ = cap;
84
85 colorSpace_ = other.colorSpace_;
86 shaderEffect_ = other.shaderEffect_;
87 pathEffect_ = other.pathEffect_;
88 blender_ = other.blender_;
89 blurDrawLooper_ = other.blurDrawLooper_;
90 color_ = other.color_;
91 if (other.hasFilter_) {
92 filter_ = other.filter_;
93 } else {
94 filter_.Reset();
95 }
96 return *this;
97 }
98
CanCombinePaint(const Paint & pen,const Paint & brush)99 bool Paint::CanCombinePaint(const Paint& pen, const Paint& brush)
100 {
101 return pen.antiAlias_ == brush.antiAlias_ &&
102 pen.color_ == brush.color_ &&
103 pen.blendMode_ == brush.blendMode_ &&
104 pen.hasFilter_ == brush.hasFilter_ &&
105 pen.filter_ == brush.filter_ &&
106 pen.colorSpace_ == brush.colorSpace_ &&
107 pen.shaderEffect_ == brush.shaderEffect_ &&
108 pen.blender_ == brush.blender_ &&
109 pen.blenderEnabled_ == brush.blenderEnabled_ &&
110 pen.blurDrawLooper_ == brush.blurDrawLooper_;
111 }
112
AttachBrush(const Brush & brush)113 void Paint::AttachBrush(const Brush& brush)
114 {
115 antiAlias_ = brush.IsAntiAlias();
116 color_ = brush.GetColor();
117 blendMode_ = brush.GetBlendMode();
118 style_ = PaintStyle::PAINT_FILL;
119 if (brush.HasFilter()) {
120 filter_ = brush.GetFilter();
121 hasFilter_ = true;
122 } else {
123 filter_.Reset();
124 hasFilter_ = false;
125 }
126 colorSpace_ = brush.GetColorSpace();
127 shaderEffect_ = brush.GetShaderEffect();
128 blender_ = brush.GetBlender();
129 blenderEnabled_ = brush.GetBlenderEnabled();
130 blurDrawLooper_ = brush.GetLooper();
131 }
132
AttachPen(const Pen & pen)133 void Paint::AttachPen(const Pen& pen)
134 {
135 antiAlias_ = pen.IsAntiAlias();
136 color_ = pen.GetColor();
137 blendMode_ = pen.GetBlendMode();
138 style_ = PaintStyle::PAINT_STROKE;
139 width_ = pen.GetWidth();
140 miterLimit_ = pen.GetMiterLimit();
141 cap_ = pen.GetCapStyle();
142 join_ = pen.GetJoinStyle();
143 if (pen.HasFilter()) {
144 filter_ = pen.GetFilter();
145 hasFilter_ = true;
146 } else {
147 filter_.Reset();
148 hasFilter_ = false;
149 }
150 colorSpace_ = pen.GetColorSpace();
151 shaderEffect_ = pen.GetShaderEffect();
152 pathEffect_ = pen.GetPathEffect();
153 blender_ = pen.GetBlender();
154 blenderEnabled_ = pen.GetBlenderEnabled();
155 blurDrawLooper_ = pen.GetLooper();
156 }
157
SetStyle(const PaintStyle & style)158 void Paint::SetStyle(const PaintStyle& style)
159 {
160 style_ = style;
161 }
162
HasStrokeStyle() const163 bool Paint::HasStrokeStyle() const
164 {
165 return style_ == PaintStyle::PAINT_FILL_STROKE || style_ == PaintStyle::PAINT_STROKE;
166 }
167
SetColor(const Color & c)168 void Paint::SetColor(const Color& c)
169 {
170 color_ = c;
171 }
172
SetARGB(int a,int r,int g,int b)173 void Paint::SetARGB(int a, int r, int g, int b)
174 {
175 color_.SetRgb(r, g, b, a);
176 }
177
SetColor(const Color4f & cf,std::shared_ptr<ColorSpace> s)178 void Paint::SetColor(const Color4f& cf, std::shared_ptr<ColorSpace> s)
179 {
180 color_.SetRgbF(cf.redF_, cf.greenF_, cf.blueF_, cf.alphaF_);
181 colorSpace_ = s;
182 }
183
SetAlpha(uint32_t a)184 void Paint::SetAlpha(uint32_t a)
185 {
186 color_.SetAlpha(a);
187 }
188
SetAlphaF(scalar a)189 void Paint::SetAlphaF(scalar a)
190 {
191 color_.SetAlphaF(a);
192 }
193
SetWidth(scalar width)194 void Paint::SetWidth(scalar width)
195 {
196 width_ = width;
197 }
198
SetMiterLimit(scalar limit)199 void Paint::SetMiterLimit(scalar limit)
200 {
201 miterLimit_ = limit;
202 }
203
SetCapStyle(Pen::CapStyle cs)204 void Paint::SetCapStyle(Pen::CapStyle cs)
205 {
206 cap_ = cs;
207 }
208
SetJoinStyle(Pen::JoinStyle js)209 void Paint::SetJoinStyle(Pen::JoinStyle js)
210 {
211 join_ = js;
212 }
213
SetBlendMode(BlendMode mode)214 void Paint::SetBlendMode(BlendMode mode)
215 {
216 blender_ = nullptr;
217 blendMode_ = mode;
218 }
219
SetFilter(const Filter & filter)220 void Paint::SetFilter(const Filter& filter)
221 {
222 filter_ = filter;
223 hasFilter_ = true;
224 }
225
SetShaderEffect(std::shared_ptr<ShaderEffect> e)226 void Paint::SetShaderEffect(std::shared_ptr<ShaderEffect> e)
227 {
228 shaderEffect_ = e;
229 }
230
SetPathEffect(std::shared_ptr<PathEffect> e)231 void Paint::SetPathEffect(std::shared_ptr<PathEffect> e)
232 {
233 pathEffect_ = e;
234 }
235
SetBlender(std::shared_ptr<Blender> blender)236 void Paint::SetBlender(std::shared_ptr<Blender> blender)
237 {
238 blender_ = blender;
239 }
240
SetBlenderEnabled(bool blenderEnabled)241 void Paint::SetBlenderEnabled(bool blenderEnabled)
242 {
243 blenderEnabled_ = blenderEnabled;
244 }
245
SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper)246 void Paint::SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper)
247 {
248 blurDrawLooper_ = blurDrawLooper;
249 }
250
GetLooper() const251 std::shared_ptr<BlurDrawLooper> Paint::GetLooper() const
252 {
253 return blurDrawLooper_;
254 }
255
SetAntiAlias(bool aa)256 void Paint::SetAntiAlias(bool aa)
257 {
258 antiAlias_ = aa;
259 }
260
Reset()261 void Paint::Reset()
262 {
263 antiAlias_ = false;
264 color_ = Color::COLOR_BLACK;
265 blendMode_ = BlendMode::SRC_OVER;
266 style_ = PaintStyle::PAINT_NONE;
267 width_ = 0;
268 miterLimit_ = DEFAULT_MITER_VAL;
269 join_ = Pen::JoinStyle::DEFAULT_JOIN;
270 cap_ = Pen::CapStyle::DEFAULT_CAP;
271
272 hasFilter_ = false;
273 filter_.Reset();
274
275 colorSpace_ = nullptr;
276 shaderEffect_ = nullptr;
277 pathEffect_ = nullptr;
278 blurDrawLooper_ = nullptr;
279 }
280
Disable()281 void Paint::Disable()
282 {
283 style_ = PaintStyle::PAINT_NONE;
284 hasFilter_ = false;
285 }
286
operator ==(const Paint & p1,const Paint & p2)287 bool operator==(const Paint& p1, const Paint& p2)
288 {
289 return p1.antiAlias_ == p2.antiAlias_ &&
290 p1.color_ == p2.color_ &&
291 p1.blendMode_ == p2.blendMode_ &&
292 p1.style_ == p2.style_ &&
293 IsScalarAlmostEqual(p1.width_, p2.width_) &&
294 IsScalarAlmostEqual(p1.miterLimit_, p2.miterLimit_) &&
295 p1.join_ == p2.join_ &&
296 p1.cap_ == p2.cap_ &&
297 p1.filter_ == p2.filter_ &&
298 p1.colorSpace_ == p2.colorSpace_ &&
299 p1.shaderEffect_ == p2.shaderEffect_ &&
300 p1.pathEffect_ == p2.pathEffect_ &&
301 p1.blender_ == p2.blender_ &&
302 p1.blenderEnabled_ == p2.blenderEnabled_ &&
303 p1.blurDrawLooper_ == p2.blurDrawLooper_;
304 }
305
operator !=(const Paint & p1,const Paint & p2)306 bool operator!=(const Paint& p1, const Paint& p2)
307 {
308 return p1.antiAlias_ != p2.antiAlias_ ||
309 p1.color_ != p2.color_ ||
310 p1.blendMode_ != p2.blendMode_ ||
311 p1.style_ != p2.style_ ||
312 !IsScalarAlmostEqual(p1.width_, p2.width_) ||
313 !IsScalarAlmostEqual(p1.miterLimit_, p2.miterLimit_) ||
314 p1.join_ != p2.join_ ||
315 p1.cap_ != p2.cap_ ||
316 p1.filter_ != p2.filter_ ||
317 p1.colorSpace_ != p2.colorSpace_ ||
318 p1.shaderEffect_ != p2.shaderEffect_ ||
319 p1.pathEffect_ != p2.pathEffect_ ||
320 p1.blender_ != p2.blender_ ||
321 p1.blenderEnabled_ != p2.blenderEnabled_ ||
322 p1.blurDrawLooper_ != p2.blurDrawLooper_;
323 }
324
Dump(std::string & out) const325 void Paint::Dump(std::string& out) const
326 {
327 out += "[antiAlias:" + std::string(antiAlias_ ? "true" : "false");
328 out += " color";
329 color_.Dump(out);
330 out += " blendMode:" + std::to_string(static_cast<int>(blendMode_));
331 out += " style:" + std::to_string(static_cast<uint8_t>(style_));
332 out += " width:" + std::to_string(width_);
333 out += " miterLimit:" + std::to_string(miterLimit_);
334 out += " join:" + std::to_string(static_cast<int>(join_));
335 out += " cap:" + std::to_string(static_cast<int>(cap_));
336 out += " blenderEnabled:" + std::string(blenderEnabled_ ? "true" : "false");
337 out += " hasFilter:" + std::string(hasFilter_ ? "true" : "false");
338 out += ']';
339 }
340
341 } // namespace Drawing
342 } // namespace Rosen
343 } // namespace OHOS