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/SkData.h"
11 #include "include/core/SkGraphics.h"
12 #include "include/core/SkImageFilter.h"
13 #include "include/core/SkMaskFilter.h"
14 #include "include/core/SkPathEffect.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkShader.h"
17 #include "include/core/SkStrokeRec.h"
18 #include "include/core/SkTypeface.h"
19 #include "include/private/SkMutex.h"
20 #include "include/private/SkTo.h"
21 #include "src/core/SkColorFilterBase.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/SkPathEffectPriv.h"
30 #include "src/core/SkReadBuffer.h"
31 #include "src/core/SkSafeRange.h"
32 #include "src/core/SkStringUtils.h"
33 #include "src/core/SkStroke.h"
34 #include "src/core/SkSurfacePriv.h"
35 #include "src/core/SkTLazy.h"
36 #include "src/core/SkWriteBuffer.h"
37 #include "src/shaders/SkShaderBase.h"
38 
39 // define this to get a printf for out-of-range parameter in setters
40 // e.g. setTextSize(-1)
41 //#define SK_REPORT_API_RANGE_CHECK
42 
43 
SkPaint()44 SkPaint::SkPaint()
45     : fColor4f{0, 0, 0, 1}  // opaque black
46     , fWidth{0}
47     , fMiterLimit{SkPaintDefaults_MiterLimit}
48     , fBitfields{(unsigned)false,                   // fAntiAlias
49                  (unsigned)false,                   // fDither
50                  (unsigned)SkPaint::kDefault_Cap,   // fCapType
51                  (unsigned)SkPaint::kDefault_Join,  // fJoinType
52                  (unsigned)SkPaint::kFill_Style,    // fStyle
53                  (unsigned)kNone_SkFilterQuality,   // fFilterQuality
54                  (unsigned)SkBlendMode::kSrcOver,   // fBlendMode
55                  0}                                 // fPadding
56 {
57     static_assert(sizeof(fBitfields) == sizeof(fBitfieldsUInt), "");
58 }
59 
SkPaint(const SkColor4f & color,SkColorSpace * colorSpace)60 SkPaint::SkPaint(const SkColor4f& color, SkColorSpace* colorSpace) : SkPaint() {
61     this->setColor(color, colorSpace);
62 }
63 
64 SkPaint::SkPaint(const SkPaint& src) = default;
65 
66 SkPaint::SkPaint(SkPaint&& src) = default;
67 
68 SkPaint::~SkPaint() = default;
69 
70 SkPaint& SkPaint::operator=(const SkPaint& src) = default;
71 
72 SkPaint& SkPaint::operator=(SkPaint&& src) = default;
73 
operator ==(const SkPaint & a,const SkPaint & b)74 bool operator==(const SkPaint& a, const SkPaint& b) {
75 #define EQUAL(field) (a.field == b.field)
76     return EQUAL(fPathEffect)
77         && EQUAL(fShader)
78         && EQUAL(fMaskFilter)
79         && EQUAL(fColorFilter)
80         && EQUAL(fImageFilter)
81         && EQUAL(fColor4f)
82         && EQUAL(fWidth)
83         && EQUAL(fMiterLimit)
84         && EQUAL(fBitfieldsUInt)
85         ;
86 #undef EQUAL
87 }
88 
89 #define DEFINE_REF_FOO(type)    sk_sp<Sk##type> SkPaint::ref##type() const { return f##type; }
90 DEFINE_REF_FOO(ColorFilter)
DEFINE_REF_FOO(ImageFilter)91 DEFINE_REF_FOO(ImageFilter)
92 DEFINE_REF_FOO(MaskFilter)
93 DEFINE_REF_FOO(PathEffect)
94 DEFINE_REF_FOO(Shader)
95 #undef DEFINE_REF_FOO
96 
97 void SkPaint::reset() { *this = SkPaint(); }
98 
setStyle(Style style)99 void SkPaint::setStyle(Style style) {
100     if ((unsigned)style < kStyleCount) {
101         fBitfields.fStyle = style;
102     } else {
103 #ifdef SK_REPORT_API_RANGE_CHECK
104         SkDebugf("SkPaint::setStyle(%d) out of range\n", style);
105 #endif
106     }
107 }
108 
setStroke(bool isStroke)109 void SkPaint::setStroke(bool isStroke) {
110     fBitfields.fStyle = isStroke ? kStroke_Style : kFill_Style;
111 }
112 
setColor(SkColor color)113 void SkPaint::setColor(SkColor color) {
114     fColor4f = SkColor4f::FromColor(color);
115 }
116 
setColor(const SkColor4f & color,SkColorSpace * colorSpace)117 void SkPaint::setColor(const SkColor4f& color, SkColorSpace* colorSpace) {
118     SkASSERT(fColor4f.fA >= 0 && fColor4f.fA <= 1.0f);
119 
120     SkColorSpaceXformSteps steps{colorSpace,          kUnpremul_SkAlphaType,
121                                  sk_srgb_singleton(), kUnpremul_SkAlphaType};
122     fColor4f = color;
123     steps.apply(fColor4f.vec());
124 }
125 
setAlphaf(float a)126 void SkPaint::setAlphaf(float a) {
127     SkASSERT(a >= 0 && a <= 1.0f);
128     fColor4f.fA = a;
129 }
130 
setARGB(U8CPU a,U8CPU r,U8CPU g,U8CPU b)131 void SkPaint::setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
132     this->setColor(SkColorSetARGB(a, r, g, b));
133 }
134 
setStrokeWidth(SkScalar width)135 void SkPaint::setStrokeWidth(SkScalar width) {
136     if (width >= 0) {
137         fWidth = width;
138     } else {
139 #ifdef SK_REPORT_API_RANGE_CHECK
140         SkDebugf("SkPaint::setStrokeWidth() called with negative value\n");
141 #endif
142     }
143 }
144 
setStrokeMiter(SkScalar limit)145 void SkPaint::setStrokeMiter(SkScalar limit) {
146     if (limit >= 0) {
147         fMiterLimit = limit;
148     } else {
149 #ifdef SK_REPORT_API_RANGE_CHECK
150         SkDebugf("SkPaint::setStrokeMiter() called with negative value\n");
151 #endif
152     }
153 }
154 
setStrokeCap(Cap ct)155 void SkPaint::setStrokeCap(Cap ct) {
156     if ((unsigned)ct < kCapCount) {
157         fBitfields.fCapType = SkToU8(ct);
158     } else {
159 #ifdef SK_REPORT_API_RANGE_CHECK
160         SkDebugf("SkPaint::setStrokeCap(%d) out of range\n", ct);
161 #endif
162     }
163 }
164 
setStrokeJoin(Join jt)165 void SkPaint::setStrokeJoin(Join jt) {
166     if ((unsigned)jt < kJoinCount) {
167         fBitfields.fJoinType = SkToU8(jt);
168     } else {
169 #ifdef SK_REPORT_API_RANGE_CHECK
170         SkDebugf("SkPaint::setStrokeJoin(%d) out of range\n", jt);
171 #endif
172     }
173 }
174 
175 ///////////////////////////////////////////////////////////////////////////////
176 
177 #define MOVE_FIELD(Field) void SkPaint::set##Field(sk_sp<Sk##Field> f) { f##Field = std::move(f); }
178 MOVE_FIELD(ImageFilter)
MOVE_FIELD(Shader)179 MOVE_FIELD(Shader)
180 MOVE_FIELD(ColorFilter)
181 MOVE_FIELD(PathEffect)
182 MOVE_FIELD(MaskFilter)
183 #undef MOVE_FIELD
184 
185 ///////////////////////////////////////////////////////////////////////////////
186 
187 #include "include/core/SkStream.h"
188 
189 #ifdef SK_DEBUG
190     static void ASSERT_FITS_IN(uint32_t value, int bitCount) {
191         SkASSERT(bitCount > 0 && bitCount <= 32);
192         uint32_t mask = ~0U;
193         mask >>= (32 - bitCount);
194         SkASSERT(0 == (value & ~mask));
195     }
196 #else
197     #define ASSERT_FITS_IN(value, bitcount)
198 #endif
199 
200 enum FlatFlags {
201     kHasTypeface_FlatFlag = 0x1,
202     kHasEffects_FlatFlag  = 0x2,
203 
204     kFlatFlagMask         = 0x3,
205 };
206 
207 // SkPaint originally defined flags, some of which now apply to SkFont. These are renames
208 // of those flags, split into categories depending on which objects they (now) apply to.
209 
shift_bits(T value,unsigned shift,unsigned bits)210 template <typename T> uint32_t shift_bits(T value, unsigned shift, unsigned bits) {
211     SkASSERT(shift + bits <= 32);
212     uint32_t v = static_cast<uint32_t>(value);
213     ASSERT_FITS_IN(v, bits);
214     return v << shift;
215 }
216 
217 /*  Packing the paint
218  flags :  8  // 2...
219  blend :  8  // 30+
220  cap   :  2  // 3
221  join  :  2  // 3
222  style :  2  // 3
223  filter:  2  // 4
224  flat  :  8  // 1...
225  total : 32
226  */
pack_v68(const SkPaint & paint,unsigned flatFlags)227 static uint32_t pack_v68(const SkPaint& paint, unsigned flatFlags) {
228     uint32_t packed = 0;
229     packed |= shift_bits(((unsigned)paint.isDither() << 1) |
230                           (unsigned)paint.isAntiAlias(), 0, 8);
231     packed |= shift_bits(paint.getBlendMode(),      8, 8);
232     packed |= shift_bits(paint.getStrokeCap(),     16, 2);
233     packed |= shift_bits(paint.getStrokeJoin(),    18, 2);
234     packed |= shift_bits(paint.getStyle(),         20, 2);
235 #ifdef SK_SUPPORT_LEGACY_SETFILTERQUALITY
236     packed |= shift_bits(paint.getFilterQuality(), 22, 2);
237 #endif
238     packed |= shift_bits(flatFlags,                24, 8);
239     return packed;
240 }
241 
unpack_v68(SkPaint * paint,uint32_t packed,SkSafeRange & safe)242 static uint32_t unpack_v68(SkPaint* paint, uint32_t packed, SkSafeRange& safe) {
243     paint->setAntiAlias((packed & 1) != 0);
244     paint->setDither((packed & 2) != 0);
245     packed >>= 8;
246     paint->setBlendMode(safe.checkLE(packed & 0xFF, SkBlendMode::kLastMode));
247     packed >>= 8;
248     paint->setStrokeCap(safe.checkLE(packed & 0x3, SkPaint::kLast_Cap));
249     packed >>= 2;
250     paint->setStrokeJoin(safe.checkLE(packed & 0x3, SkPaint::kLast_Join));
251     packed >>= 2;
252     paint->setStyle(safe.checkLE(packed & 0x3, SkPaint::kStrokeAndFill_Style));
253     packed >>= 2;
254 #ifdef SK_SUPPORT_LEGACY_SETFILTERQUALITY
255     paint->setFilterQuality(safe.checkLE(packed & 0x3, kLast_SkFilterQuality));
256 #endif
257     packed >>= 2;
258     return packed;
259 }
260 
261 /*  To save space/time, we analyze the paint, and write a truncated version of
262     it if there are not tricky elements like shaders, etc.
263  */
Flatten(const SkPaint & paint,SkWriteBuffer & buffer)264 void SkPaintPriv::Flatten(const SkPaint& paint, SkWriteBuffer& buffer) {
265     uint8_t flatFlags = 0;
266 
267     if (paint.getPathEffect() ||
268         paint.getShader() ||
269         paint.getMaskFilter() ||
270         paint.getColorFilter() ||
271         paint.getImageFilter()) {
272         flatFlags |= kHasEffects_FlatFlag;
273     }
274 
275     buffer.writeScalar(paint.getStrokeWidth());
276     buffer.writeScalar(paint.getStrokeMiter());
277     buffer.writeColor4f(paint.getColor4f());
278 
279     buffer.write32(pack_v68(paint, flatFlags));
280 
281     if (flatFlags & kHasEffects_FlatFlag) {
282         buffer.writeFlattenable(paint.getPathEffect());
283         buffer.writeFlattenable(paint.getShader());
284         buffer.writeFlattenable(paint.getMaskFilter());
285         buffer.writeFlattenable(paint.getColorFilter());
286         buffer.write32(0);  // legacy, was drawlooper
287         buffer.writeFlattenable(paint.getImageFilter());
288     }
289 }
290 
Unflatten(SkPaint * paint,SkReadBuffer & buffer,SkFont * font)291 SkReadPaintResult SkPaintPriv::Unflatten(SkPaint* paint, SkReadBuffer& buffer, SkFont* font) {
292     SkSafeRange safe;
293 
294     paint->setStrokeWidth(buffer.readScalar());
295     paint->setStrokeMiter(buffer.readScalar());
296     {
297         SkColor4f color;
298         buffer.readColor4f(&color);
299         paint->setColor(color, sk_srgb_singleton());
300     }
301 
302     unsigned flatFlags = unpack_v68(paint, buffer.readUInt(), safe);
303 
304     if (flatFlags & kHasEffects_FlatFlag) {
305         paint->setPathEffect(buffer.readPathEffect());
306         paint->setShader(buffer.readShader());
307         paint->setMaskFilter(buffer.readMaskFilter());
308         paint->setColorFilter(buffer.readColorFilter());
309         (void)buffer.read32();  // was drawLooper (zero)
310         paint->setImageFilter(buffer.readImageFilter());
311     } else {
312         paint->setPathEffect(nullptr);
313         paint->setShader(nullptr);
314         paint->setMaskFilter(nullptr);
315         paint->setColorFilter(nullptr);
316         paint->setImageFilter(nullptr);
317     }
318 
319     if (!buffer.validate(safe)) {
320         paint->reset();
321         return kFailed_ReadPaint;
322     }
323     return kSuccess_JustPaint;
324 }
325 
326 ///////////////////////////////////////////////////////////////////////////////
327 
getFillPath(const SkPath & src,SkPath * dst,const SkRect * cullRect,SkScalar resScale) const328 bool SkPaint::getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect,
329                           SkScalar resScale) const {
330     if (!src.isFinite()) {
331         dst->reset();
332         return false;
333     }
334 
335     SkStrokeRec rec(*this, resScale);
336 
337 #if defined(SK_BUILD_FOR_FUZZER)
338     // Prevent lines with small widths from timing out.
339     if (rec.getStyle() == SkStrokeRec::Style::kStroke_Style && rec.getWidth() < 0.001) {
340         return false;
341     }
342 #endif
343 
344     const SkPath* srcPtr = &src;
345     SkPath tmpPath;
346 
347     if (fPathEffect && fPathEffect->filterPath(&tmpPath, src, &rec, cullRect)) {
348         srcPtr = &tmpPath;
349     }
350 
351     if (!rec.applyToPath(dst, *srcPtr)) {
352         if (srcPtr == &tmpPath) {
353             // If path's were copy-on-write, this trick would not be needed.
354             // As it is, we want to save making a deep-copy from tmpPath -> dst
355             // since we know we're just going to delete tmpPath when we return,
356             // so the swap saves that copy.
357             dst->swap(tmpPath);
358         } else {
359             *dst = *srcPtr;
360         }
361     }
362 
363     if (!dst->isFinite()) {
364         dst->reset();
365         return false;
366     }
367     return !rec.isHairlineStyle();
368 }
369 
canComputeFastBounds() const370 bool SkPaint::canComputeFastBounds() const {
371     if (this->getImageFilter() && !this->getImageFilter()->canComputeFastBounds()) {
372         return false;
373     }
374     // Pass nullptr for the bounds to determine if they can be computed
375     if (this->getPathEffect() &&
376         !SkPathEffectPriv::ComputeFastBounds(this->getPathEffect(), nullptr)) {
377         return false;
378     }
379     return true;
380 }
381 
doComputeFastBounds(const SkRect & origSrc,SkRect * storage,Style style) const382 const SkRect& SkPaint::doComputeFastBounds(const SkRect& origSrc,
383                                            SkRect* storage,
384                                            Style style) const {
385     SkASSERT(storage);
386 
387     const SkRect* src = &origSrc;
388 
389     SkRect tmpSrc;
390     if (this->getPathEffect()) {
391         tmpSrc = origSrc;
392         SkAssertResult(SkPathEffectPriv::ComputeFastBounds(this->getPathEffect(), &tmpSrc));
393         src = &tmpSrc;
394     }
395 
396     SkScalar radius = SkStrokeRec::GetInflationRadius(*this, style);
397     *storage = src->makeOutset(radius, radius);
398 
399     if (this->getMaskFilter()) {
400         as_MFB(this->getMaskFilter())->computeFastBounds(*storage, storage);
401     }
402 
403     if (this->getImageFilter()) {
404         *storage = this->getImageFilter()->computeFastBounds(*storage);
405     }
406 
407     return *storage;
408 }
409 
410 ///////////////////////////////////////////////////////////////////////////////
411 
412 // return true if the filter exists, and may affect alpha
affects_alpha(const SkColorFilter * cf)413 static bool affects_alpha(const SkColorFilter* cf) {
414     return cf && !as_CFB(cf)->isAlphaUnchanged();
415 }
416 
417 // return true if the filter exists, and may affect alpha
affects_alpha(const SkImageFilter * imf)418 static bool affects_alpha(const SkImageFilter* imf) {
419     // TODO: check if we should allow imagefilters to broadcast that they don't affect alpha
420     // ala colorfilters
421     return imf != nullptr;
422 }
423 
nothingToDraw() const424 bool SkPaint::nothingToDraw() const {
425     switch (this->getBlendMode()) {
426         case SkBlendMode::kSrcOver:
427         case SkBlendMode::kSrcATop:
428         case SkBlendMode::kDstOut:
429         case SkBlendMode::kDstOver:
430         case SkBlendMode::kPlus:
431             if (0 == this->getAlpha()) {
432                 return !affects_alpha(fColorFilter.get()) && !affects_alpha(fImageFilter.get());
433             }
434             break;
435         case SkBlendMode::kDst:
436             return true;
437         default:
438             break;
439     }
440     return false;
441 }
442 
getHash() const443 uint32_t SkPaint::getHash() const {
444     // We're going to hash 5 pointers and 6 floats, finishing up with fBitfields,
445     // so fBitfields should be 5 pointers and 6 floats from the start.
446     static_assert(offsetof(SkPaint, fBitfieldsUInt) == 5 * sizeof(void*) + 6 * sizeof(float),
447                   "SkPaint_notPackedTightly");
448     return SkOpts::hash(reinterpret_cast<const uint32_t*>(this),
449                         offsetof(SkPaint, fBitfieldsUInt) + sizeof(fBitfieldsUInt));
450 }
451