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