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