1/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "include/core/SkPaint.h"
9
10#include "include/core/SkColorFilter.h"
11#include "include/core/SkData.h"
12#include "include/core/SkGraphics.h"
13#include "include/core/SkImageFilter.h"
14#include "include/core/SkMaskFilter.h"
15#include "include/core/SkPathEffect.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkShader.h"
18#include "include/core/SkStrokeRec.h"
19#include "include/core/SkTypeface.h"
20#include "include/private/SkMutex.h"
21#include "include/private/SkTo.h"
22#include "src/core/SkColorSpacePriv.h"
23#include "src/core/SkColorSpaceXformSteps.h"
24#include "src/core/SkDraw.h"
25#include "src/core/SkMaskGamma.h"
26#include "src/core/SkOpts.h"
27#include "src/core/SkPaintDefaults.h"
28#include "src/core/SkPaintPriv.h"
29#include "src/core/SkReadBuffer.h"
30#include "src/core/SkSafeRange.h"
31#include "src/core/SkStringUtils.h"
32#include "src/core/SkStroke.h"
33#include "src/core/SkSurfacePriv.h"
34#include "src/core/SkTLazy.h"
35#include "src/core/SkWriteBuffer.h"
36#include "src/shaders/SkShaderBase.h"
37
38// define this to get a printf for out-of-range parameter in setters
39// e.g. setTextSize(-1)
40//#define SK_REPORT_API_RANGE_CHECK
41
42
43SkPaint::SkPaint()
44 : fColor4f{0, 0, 0, 1} // opaque black
45 , fWidth{0}
46 , fMiterLimit{SkPaintDefaults_MiterLimit}
47 , fBitfields{(unsigned)false, // fAntiAlias
48 (unsigned)false, // fDither
49 (unsigned)SkPaint::kDefault_Cap, // fCapType
50 (unsigned)SkPaint::kDefault_Join, // fJoinType
51 (unsigned)SkPaint::kFill_Style, // fStyle
52 (unsigned)kNone_SkFilterQuality, // fFilterQuality
53 (unsigned)SkBlendMode::kSrcOver, // fBlendMode
54 0} // fPadding
55{
56 static_assert(sizeof(fBitfields) == sizeof(fBitfieldsUInt), "");
57}
58
59SkPaint::SkPaint(const SkColor4f& color, SkColorSpace* colorSpace) : SkPaint() {
60 this->setColor(color, colorSpace);
61}
62
63SkPaint::SkPaint(const SkPaint& src) = default;
64
65SkPaint::SkPaint(SkPaint&& src) = default;
66
67SkPaint::~SkPaint() = default;
68
69SkPaint& SkPaint::operator=(const SkPaint& src) = default;
70
71SkPaint& SkPaint::operator=(SkPaint&& src) = default;
72
73bool operator==(const SkPaint& a, const SkPaint& b) {
74#define EQUAL(field) (a.field == b.field)
75 return EQUAL(fPathEffect)
76 && EQUAL(fShader)
77 && EQUAL(fMaskFilter)
78 && EQUAL(fColorFilter)
79 && EQUAL(fImageFilter)
80 && EQUAL(fColor4f)
81 && EQUAL(fWidth)
82 && EQUAL(fMiterLimit)
83 && EQUAL(fBitfieldsUInt)
84 ;
85#undef EQUAL
86}
87
88#define DEFINE_REF_FOO(type) sk_sp<Sk##type> SkPaint::ref##type() const { return f##type; }
89DEFINE_REF_FOO(ColorFilter)
90DEFINE_REF_FOO(ImageFilter)
91DEFINE_REF_FOO(MaskFilter)
92DEFINE_REF_FOO(PathEffect)
93DEFINE_REF_FOO(Shader)
94#undef DEFINE_REF_FOO
95
96void SkPaint::reset() { *this = SkPaint(); }
97
98void SkPaint::setFilterQuality(SkFilterQuality quality) {
99 fBitfields.fFilterQuality = quality;
100}
101
102void SkPaint::setStyle(Style style) {
103 if ((unsigned)style < kStyleCount) {
104 fBitfields.fStyle = style;
105 } else {
106#ifdef SK_REPORT_API_RANGE_CHECK
107 SkDebugf("SkPaint::setStyle(%d) out of range\n", style);
108#endif
109 }
110}
111
112void SkPaint::setColor(SkColor color) {
113 fColor4f = SkColor4f::FromColor(color);
114}
115
116void SkPaint::setColor(const SkColor4f& color, SkColorSpace* colorSpace) {
117 SkASSERT(fColor4f.fA >= 0 && fColor4f.fA <= 1.0f);
118
119 SkColorSpaceXformSteps steps{colorSpace, kUnpremul_SkAlphaType,
120 sk_srgb_singleton(), kUnpremul_SkAlphaType};
121 fColor4f = color;
122 steps.apply(fColor4f.vec());
123}
124
125void SkPaint::setAlphaf(float a) {
126 SkASSERT(a >= 0 && a <= 1.0f);
127 fColor4f.fA = a;
128}
129
130void SkPaint::setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
131 this->setColor(SkColorSetARGB(a, r, g, b));
132}
133
134void SkPaint::setStrokeWidth(SkScalar width) {
135 if (width >= 0) {
136 fWidth = width;
137 } else {
138#ifdef SK_REPORT_API_RANGE_CHECK
139 SkDebugf("SkPaint::setStrokeWidth() called with negative value\n");
140#endif
141 }
142}
143
144void SkPaint::setStrokeMiter(SkScalar limit) {
145 if (limit >= 0) {
146 fMiterLimit = limit;
147 } else {
148#ifdef SK_REPORT_API_RANGE_CHECK
149 SkDebugf("SkPaint::setStrokeMiter() called with negative value\n");
150#endif
151 }
152}
153
154void SkPaint::setStrokeCap(Cap ct) {
155 if ((unsigned)ct < kCapCount) {
156 fBitfields.fCapType = SkToU8(ct);
157 } else {
158#ifdef SK_REPORT_API_RANGE_CHECK
159 SkDebugf("SkPaint::setStrokeCap(%d) out of range\n", ct);
160#endif
161 }
162}
163
164void SkPaint::setStrokeJoin(Join jt) {
165 if ((unsigned)jt < kJoinCount) {
166 fBitfields.fJoinType = SkToU8(jt);
167 } else {
168#ifdef SK_REPORT_API_RANGE_CHECK
169 SkDebugf("SkPaint::setStrokeJoin(%d) out of range\n", jt);
170#endif
171 }
172}
173
174///////////////////////////////////////////////////////////////////////////////
175
176#define MOVE_FIELD(Field) void SkPaint::set##Field(sk_sp<Sk##Field> f) { f##Field = std::move(f); }
177MOVE_FIELD(ImageFilter)
178MOVE_FIELD(Shader)
179MOVE_FIELD(ColorFilter)
180MOVE_FIELD(PathEffect)
181MOVE_FIELD(MaskFilter)
182#undef MOVE_FIELD
183
184///////////////////////////////////////////////////////////////////////////////
185
186#include "include/core/SkStream.h"
187
188#ifdef SK_DEBUG
189 static void ASSERT_FITS_IN(uint32_t value, int bitCount) {
190 SkASSERT(bitCount > 0 && bitCount <= 32);
191 uint32_t mask = ~0U;
192 mask >>= (32 - bitCount);
193 SkASSERT(0 == (value & ~mask));
194 }
195#else
196 #define ASSERT_FITS_IN(value, bitcount)
197#endif
198
199enum FlatFlags {
200 kHasTypeface_FlatFlag = 0x1,
201 kHasEffects_FlatFlag = 0x2,
202
203 kFlatFlagMask = 0x3,
204};
205
206enum BitsPerField {
207 kFlags_BPF = 16,
208 kHint_BPF = 2,
209 kFilter_BPF = 2,
210 kFlatFlags_BPF = 3,
211};
212
213static inline int BPF_Mask(int bits) {
214 return (1 << bits) - 1;
215}
216
217// SkPaint originally defined flags, some of which now apply to SkFont. These are renames
218// of those flags, split into categories depending on which objects they (now) apply to.
219
220enum PaintFlagsForPaint {
221 kAA_PaintFlagForPaint = 0x01,
222 kDither_PaintFlagForPaint = 0x04,
223};
224
225enum PaintFlagsForFont {
226 kFakeBold_PaintFlagForFont = 0x20,
227 kLinear_PaintFlagForFont = 0x40,
228 kSubpixel_PaintFlagForFont = 0x80,
229 kLCD_PaintFlagForFont = 0x200,
230 kEmbeddedBitmap_PaintFlagForFont = 0x400,
231 kAutoHinting_PaintFlagForFont = 0x800,
232};
233
234static FlatFlags unpack_paint_flags(SkPaint* paint, uint32_t packed, SkFont* font) {
235 uint32_t f = packed >> 16;
236 paint->setAntiAlias((f & kAA_PaintFlagForPaint) != 0);
237 paint->setDither((f & kDither_PaintFlagForPaint) != 0);
238 if (font) {
239 font->setEmbolden((f & kFakeBold_PaintFlagForFont) != 0);
240 font->setLinearMetrics((f & kLinear_PaintFlagForFont) != 0);
241 font->setSubpixel((f & kSubpixel_PaintFlagForFont) != 0);
242 font->setEmbeddedBitmaps((f & kEmbeddedBitmap_PaintFlagForFont) != 0);
243 font->setForceAutoHinting((f & kAutoHinting_PaintFlagForFont) != 0);
244
245 font->setHinting((SkFontHinting)((packed >> 14) & BPF_Mask(kHint_BPF)));
246
247 if (f & kAA_PaintFlagForPaint) {
248 if (f & kLCD_PaintFlagForFont) {
249 font->setEdging(SkFont::Edging::kSubpixelAntiAlias);
250 } else {
251 font->setEdging(SkFont::Edging::kAntiAlias);
252 }
253 } else {
254 font->setEdging(SkFont::Edging::kAlias);
255 }
256 }
257
258 paint->setFilterQuality((SkFilterQuality)((packed >> 10) & BPF_Mask(kFilter_BPF)));
259 return (FlatFlags)(packed & kFlatFlagMask);
260}
261
262template <typename T> uint32_t shift_bits(T value, unsigned shift, unsigned bits) {
263 SkASSERT(shift + bits <= 32);
264 uint32_t v = static_cast<uint32_t>(value);
265 ASSERT_FITS_IN(v, bits);
266 return v << shift;
267}
268
269/* Packing the paint
270 flags : 8 // 2...
271 blend : 8 // 30+
272 cap : 2 // 3
273 join : 2 // 3
274 style : 2 // 3
275 filter: 2 // 4
276 flat : 8 // 1...
277 total : 32
278 */
279static uint32_t pack_v68(const SkPaint& paint, unsigned flatFlags) {
280 uint32_t packed = 0;
281 packed |= shift_bits(((unsigned)paint.isDither() << 1) |
282 (unsigned)paint.isAntiAlias(), 0, 8);
283 packed |= shift_bits(paint.getBlendMode(), 8, 8);
284 packed |= shift_bits(paint.getStrokeCap(), 16, 2);
285 packed |= shift_bits(paint.getStrokeJoin(), 18, 2);
286 packed |= shift_bits(paint.getStyle(), 20, 2);
287 packed |= shift_bits(paint.getFilterQuality(), 22, 2);
288 packed |= shift_bits(flatFlags, 24, 8);
289 return packed;
290}
291
292static uint32_t unpack_v68(SkPaint* paint, uint32_t packed, SkSafeRange& safe) {
293 paint->setAntiAlias((packed & 1) != 0);
294 paint->setDither((packed & 2) != 0);
295 packed >>= 8;
296 paint->setBlendMode(safe.checkLE(packed & 0xFF, SkBlendMode::kLastMode));
297 packed >>= 8;
298 paint->setStrokeCap(safe.checkLE(packed & 0x3, SkPaint::kLast_Cap));
299 packed >>= 2;
300 paint->setStrokeJoin(safe.checkLE(packed & 0x3, SkPaint::kLast_Join));
301 packed >>= 2;
302 paint->setStyle(safe.checkLE(packed & 0x3, SkPaint::kStrokeAndFill_Style));
303 packed >>= 2;
304 paint->setFilterQuality(safe.checkLE(packed & 0x3, kLast_SkFilterQuality));
305 packed >>= 2;
306 return packed;
307}
308
309/* To save space/time, we analyze the paint, and write a truncated version of
310 it if there are not tricky elements like shaders, etc.
311 */
312void SkPaintPriv::Flatten(const SkPaint& paint, SkWriteBuffer& buffer) {
313 uint8_t flatFlags = 0;
314
315 if (paint.getPathEffect() ||
316 paint.getShader() ||
317 paint.getMaskFilter() ||
318 paint.getColorFilter() ||
319 paint.getImageFilter()) {
320 flatFlags |= kHasEffects_FlatFlag;
321 }
322
323 buffer.writeScalar(paint.getStrokeWidth());
324 buffer.writeScalar(paint.getStrokeMiter());
325 buffer.writeColor4f(paint.getColor4f());
326
327 buffer.write32(pack_v68(paint, flatFlags));
328
329 if (flatFlags & kHasEffects_FlatFlag) {
330 buffer.writeFlattenable(paint.getPathEffect());
331 buffer.writeFlattenable(paint.getShader());
332 buffer.writeFlattenable(paint.getMaskFilter());
333 buffer.writeFlattenable(paint.getColorFilter());
334 buffer.write32(0); // legacy, was drawlooper
335 buffer.writeFlattenable(paint.getImageFilter());
336 }
337}
338
339SkReadPaintResult SkPaintPriv::Unflatten_PreV68(SkPaint* paint, SkReadBuffer& buffer, SkFont* font) {
340 SkSafeRange safe;
341
342 {
343 SkScalar sz = buffer.readScalar();
344 SkScalar sx = buffer.readScalar();
345 SkScalar kx = buffer.readScalar();
346 if (font) {
347 font->setSize(sz);
348 font->setScaleX(sx);
349 font->setSkewX(kx);
350 }
351 }
352
353 paint->setStrokeWidth(buffer.readScalar());
354 paint->setStrokeMiter(buffer.readScalar());
355 if (buffer.isVersionLT(SkPicturePriv::kFloat4PaintColor_Version)) {
356 paint->setColor(buffer.readColor());
357 } else {
358 SkColor4f color;
359 buffer.readColor4f(&color);
360 paint->setColor(color, sk_srgb_singleton());
361 }
362
363 unsigned flatFlags = unpack_paint_flags(paint, buffer.readUInt(), font);
364
365 uint32_t tmp = buffer.readUInt();
366 paint->setStrokeCap(safe.checkLE((tmp >> 24) & 0xFF, SkPaint::kLast_Cap));
367 paint->setStrokeJoin(safe.checkLE((tmp >> 16) & 0xFF, SkPaint::kLast_Join));
368 paint->setStyle(safe.checkLE((tmp >> 12) & 0xF, SkPaint::kStrokeAndFill_Style));
369 paint->setBlendMode(safe.checkLE(tmp & 0xFF, SkBlendMode::kLastMode));
370
371 sk_sp<SkTypeface> tf;
372 if (flatFlags & kHasTypeface_FlatFlag) {
373 tf = buffer.readTypeface();
374 }
375 if (font) {
376 font->setTypeface(tf);
377 }
378
379 if (flatFlags & kHasEffects_FlatFlag) {
380 paint->setPathEffect(buffer.readPathEffect());
381 paint->setShader(buffer.readShader());
382 paint->setMaskFilter(buffer.readMaskFilter());
383 paint->setColorFilter(buffer.readColorFilter());
384 (void)buffer.read32(); // use to be SkRasterizer
385 (void)buffer.read32(); // used to be drawlooper
386 paint->setImageFilter(buffer.readImageFilter());
387 } else {
388 paint->setPathEffect(nullptr);
389 paint->setShader(nullptr);
390 paint->setMaskFilter(nullptr);
391 paint->setColorFilter(nullptr);
392 paint->setImageFilter(nullptr);
393 }
394
395 if (!buffer.validate(safe)) {
396 paint->reset();
397 return kFailed_ReadPaint;
398 }
399 return kSuccess_PaintAndFont;
400}
401
402SkReadPaintResult SkPaintPriv::Unflatten(SkPaint* paint, SkReadBuffer& buffer, SkFont* font) {
403 if (buffer.isVersionLT(SkPicturePriv::kPaintDoesntSerializeFonts_Version)) {
404 return Unflatten_PreV68(paint, buffer, font);
405 }
406
407 SkSafeRange safe;
408
409 paint->setStrokeWidth(buffer.readScalar());
410 paint->setStrokeMiter(buffer.readScalar());
411 {
412 SkColor4f color;
413 buffer.readColor4f(&color);
414 paint->setColor(color, sk_srgb_singleton());
415 }
416
417 unsigned flatFlags = unpack_v68(paint, buffer.readUInt(), safe);
418
419 if (flatFlags & kHasEffects_FlatFlag) {
420 paint->setPathEffect(buffer.readPathEffect());
421 paint->setShader(buffer.readShader());
422 paint->setMaskFilter(buffer.readMaskFilter());
423 paint->setColorFilter(buffer.readColorFilter());
424 (void)buffer.readDrawLooper();
425 paint->setImageFilter(buffer.readImageFilter());
426 } else {
427 paint->setPathEffect(nullptr);
428 paint->setShader(nullptr);
429 paint->setMaskFilter(nullptr);
430 paint->setColorFilter(nullptr);
431 paint->setImageFilter(nullptr);
432 }
433
434 if (!buffer.validate(safe)) {
435 paint->reset();
436 return kFailed_ReadPaint;
437 }
438 return kSuccess_JustPaint;
439}
440
441///////////////////////////////////////////////////////////////////////////////
442
443bool SkPaint::getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect,
444 SkScalar resScale) const {
445 if (!src.isFinite()) {
446 dst->reset();
447 return false;
448 }
449
450 SkStrokeRec rec(*this, resScale);
451
452 const SkPath* srcPtr = &src;
453 SkPath tmpPath;
454
455 if (fPathEffect && fPathEffect->filterPath(&tmpPath, src, &rec, cullRect)) {
456 srcPtr = &tmpPath;
457 }
458
459 if (!rec.applyToPath(dst, *srcPtr)) {
460 if (srcPtr == &tmpPath) {
461 // If path's were copy-on-write, this trick would not be needed.
462 // As it is, we want to save making a deep-copy from tmpPath -> dst
463 // since we know we're just going to delete tmpPath when we return,
464 // so the swap saves that copy.
465 dst->swap(tmpPath);
466 } else {
467 *dst = *srcPtr;
468 }
469 }
470
471 if (!dst->isFinite()) {
472 dst->reset();
473 return false;
474 }
475 return !rec.isHairlineStyle();
476}
477
478bool SkPaint::canComputeFastBounds() const {
479 if (this->getImageFilter() && !this->getImageFilter()->canComputeFastBounds()) {
480 return false;
481 }
482 return true;
483}
484
485const SkRect& SkPaint::doComputeFastBounds(const SkRect& origSrc,
486 SkRect* storage,
487 Style style) const {
488 SkASSERT(storage);
489
490 const SkRect* src = &origSrc;
491
492 SkRect tmpSrc;
493 if (this->getPathEffect()) {
494 this->getPathEffect()->computeFastBounds(&tmpSrc, origSrc);
495 src = &tmpSrc;
496 }
497
498 SkScalar radius = SkStrokeRec::GetInflationRadius(*this, style);
499 *storage = src->makeOutset(radius, radius);
500
501 if (this->getMaskFilter()) {
502 as_MFB(this->getMaskFilter())->computeFastBounds(*storage, storage);
503 }
504
505 if (this->getImageFilter()) {
506 *storage = this->getImageFilter()->computeFastBounds(*storage);
507 }
508
509 return *storage;
510}
511
512///////////////////////////////////////////////////////////////////////////////
513
514// return true if the filter exists, and may affect alpha
515static bool affects_alpha(const SkColorFilter* cf) {
516 return cf && !(cf->getFlags() & SkColorFilter::kAlphaUnchanged_Flag);
517}
518
519// return true if the filter exists, and may affect alpha
520static bool affects_alpha(const SkImageFilter* imf) {
521 // TODO: check if we should allow imagefilters to broadcast that they don't affect alpha
522 // ala colorfilters
523 return imf != nullptr;
524}
525
526bool SkPaint::nothingToDraw() const {
527 switch (this->getBlendMode()) {
528 case SkBlendMode::kSrcOver:
529 case SkBlendMode::kSrcATop:
530 case SkBlendMode::kDstOut:
531 case SkBlendMode::kDstOver:
532 case SkBlendMode::kPlus:
533 if (0 == this->getAlpha()) {
534 return !affects_alpha(fColorFilter.get()) && !affects_alpha(fImageFilter.get());
535 }
536 break;
537 case SkBlendMode::kDst:
538 return true;
539 default:
540 break;
541 }
542 return false;
543}
544
545uint32_t SkPaint::getHash() const {
546 // We're going to hash 5 pointers and 6 floats, finishing up with fBitfields,
547 // so fBitfields should be 5 pointers and 6 floats from the start.
548 static_assert(offsetof(SkPaint, fBitfieldsUInt) == 5 * sizeof(void*) + 6 * sizeof(float),
549 "SkPaint_notPackedTightly");
550 return SkOpts::hash(reinterpret_cast<const uint32_t*>(this),
551 offsetof(SkPaint, fBitfieldsUInt) + sizeof(fBitfieldsUInt));
552}
553