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/SkBlenderBase.h" 22#include "src/core/SkColorFilterBase.h" 23#include "src/core/SkColorSpacePriv.h" 24#include "src/core/SkColorSpaceXformSteps.h" 25#include "src/core/SkDraw.h" 26#include "src/core/SkMaskGamma.h" 27#include "src/core/SkOpts.h" 28#include "src/core/SkPaintDefaults.h" 29#include "src/core/SkPaintPriv.h" 30#include "src/core/SkPathEffectBase.h" 31#include "src/core/SkReadBuffer.h" 32#include "src/core/SkSafeRange.h" 33#include "src/core/SkStringUtils.h" 34#include "src/core/SkStroke.h" 35#include "src/core/SkSurfacePriv.h" 36#include "src/core/SkTLazy.h" 37#include "src/core/SkWriteBuffer.h" 38#include "src/shaders/SkShaderBase.h" 39 40// define this to get a printf for out-of-range parameter in setters 41// e.g. setTextSize(-1) 42//#define SK_REPORT_API_RANGE_CHECK 43 44 45SkPaint::SkPaint() 46 : fColor4f{0, 0, 0, 1} // opaque black 47 , fWidth{0} 48 , fMiterLimit{SkPaintDefaults_MiterLimit} 49 , fBitfields{(unsigned)false, // fAntiAlias 50 (unsigned)false, // fDither 51 (unsigned)SkPaint::kDefault_Cap, // fCapType 52 (unsigned)SkPaint::kDefault_Join, // fJoinType 53 (unsigned)SkPaint::kFill_Style, // fStyle 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(fBlender) 80 && EQUAL(fImageFilter) 81 && EQUAL(fColor4f) 82 && EQUAL(fWidth) 83 && EQUAL(fMiterLimit) 84 && EQUAL(fBitfieldsUInt) 85 ; 86#undef EQUAL 87} 88 89#define DEFINE_FIELD_REF(type) \ 90 sk_sp<Sk##type> SkPaint::ref##type() const { return f##type; } 91DEFINE_FIELD_REF(ColorFilter) 92DEFINE_FIELD_REF(Blender) 93DEFINE_FIELD_REF(ImageFilter) 94DEFINE_FIELD_REF(MaskFilter) 95DEFINE_FIELD_REF(PathEffect) 96DEFINE_FIELD_REF(Shader) 97#undef DEFINE_FIELD_REF 98 99#define DEFINE_FIELD_SET(Field) \ 100 void SkPaint::set##Field(sk_sp<Sk##Field> f) { f##Field = std::move(f); } 101DEFINE_FIELD_SET(ColorFilter) 102DEFINE_FIELD_SET(ImageFilter) 103DEFINE_FIELD_SET(MaskFilter) 104DEFINE_FIELD_SET(PathEffect) 105DEFINE_FIELD_SET(Shader) 106#undef DEFINE_FIELD_SET 107 108/////////////////////////////////////////////////////////////////////////////// 109 110void SkPaint::reset() { *this = SkPaint(); } 111 112void SkPaint::setStyle(Style style) { 113 if ((unsigned)style < kStyleCount) { 114 fBitfields.fStyle = style; 115 } else { 116#ifdef SK_REPORT_API_RANGE_CHECK 117 SkDebugf("SkPaint::setStyle(%d) out of range\n", style); 118#endif 119 } 120} 121 122void SkPaint::setStroke(bool isStroke) { 123 fBitfields.fStyle = isStroke ? kStroke_Style : kFill_Style; 124} 125 126void SkPaint::setColor(SkColor color) { 127 fColor4f = SkColor4f::FromColor(color); 128} 129 130void SkPaint::setColor(const SkColor4f& color, SkColorSpace* colorSpace) { 131 SkColorSpaceXformSteps steps{colorSpace, kUnpremul_SkAlphaType, 132 sk_srgb_singleton(), kUnpremul_SkAlphaType}; 133 fColor4f = {color.fR, color.fG, color.fB, SkTPin(color.fA, 0.0f, 1.0f)}; 134 steps.apply(fColor4f.vec()); 135} 136 137void SkPaint::setAlphaf(float a) { 138 fColor4f.fA = SkTPin(a, 0.0f, 1.0f); 139} 140 141void SkPaint::setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) { 142 this->setColor(SkColorSetARGB(a, r, g, b)); 143} 144 145skstd::optional<SkBlendMode> SkPaint::asBlendMode() const { 146 return fBlender ? as_BB(fBlender)->asBlendMode() 147 : SkBlendMode::kSrcOver; 148} 149 150SkBlendMode SkPaint::getBlendMode_or(SkBlendMode defaultMode) const { 151 return this->asBlendMode().value_or(defaultMode); 152} 153 154bool SkPaint::isSrcOver() const { 155 return !fBlender || as_BB(fBlender)->asBlendMode() == SkBlendMode::kSrcOver; 156} 157 158void SkPaint::setBlendMode(SkBlendMode mode) { 159 this->setBlender(mode == SkBlendMode::kSrcOver ? nullptr : SkBlender::Mode(mode)); 160} 161 162void SkPaint::setBlender(sk_sp<SkBlender> blender) { 163 fBlender = std::move(blender); 164} 165 166void SkPaint::setStrokeWidth(SkScalar width) { 167 if (width >= 0) { 168 fWidth = width; 169 } else { 170#ifdef SK_REPORT_API_RANGE_CHECK 171 SkDebugf("SkPaint::setStrokeWidth() called with negative value\n"); 172#endif 173 } 174} 175 176void SkPaint::setStrokeMiter(SkScalar limit) { 177 if (limit >= 0) { 178 fMiterLimit = limit; 179 } else { 180#ifdef SK_REPORT_API_RANGE_CHECK 181 SkDebugf("SkPaint::setStrokeMiter() called with negative value\n"); 182#endif 183 } 184} 185 186void SkPaint::setStrokeCap(Cap ct) { 187 if ((unsigned)ct < kCapCount) { 188 fBitfields.fCapType = SkToU8(ct); 189 } else { 190#ifdef SK_REPORT_API_RANGE_CHECK 191 SkDebugf("SkPaint::setStrokeCap(%d) out of range\n", ct); 192#endif 193 } 194} 195 196void SkPaint::setStrokeJoin(Join jt) { 197 if ((unsigned)jt < kJoinCount) { 198 fBitfields.fJoinType = SkToU8(jt); 199 } else { 200#ifdef SK_REPORT_API_RANGE_CHECK 201 SkDebugf("SkPaint::setStrokeJoin(%d) out of range\n", jt); 202#endif 203 } 204} 205 206/////////////////////////////////////////////////////////////////////////////// 207 208#include "include/core/SkStream.h" 209 210#ifdef SK_DEBUG 211 static void ASSERT_FITS_IN(uint32_t value, int bitCount) { 212 SkASSERT(bitCount > 0 && bitCount <= 32); 213 uint32_t mask = ~0U; 214 mask >>= (32 - bitCount); 215 SkASSERT(0 == (value & ~mask)); 216 } 217#else 218 #define ASSERT_FITS_IN(value, bitcount) 219#endif 220 221enum FlatFlags { 222 kHasTypeface_FlatFlag = 0x1, 223 kHasEffects_FlatFlag = 0x2, 224 225 kFlatFlagMask = 0x3, 226}; 227 228// SkPaint originally defined flags, some of which now apply to SkFont. These are renames 229// of those flags, split into categories depending on which objects they (now) apply to. 230 231template <typename T> uint32_t shift_bits(T value, unsigned shift, unsigned bits) { 232 SkASSERT(shift + bits <= 32); 233 uint32_t v = static_cast<uint32_t>(value); 234 ASSERT_FITS_IN(v, bits); 235 return v << shift; 236} 237 238constexpr uint8_t CUSTOM_BLEND_MODE_SENTINEL = 0xFF; 239 240/* Packing the paint 241 flags : 8 // 2... 242 blend : 8 // 30+ 243 cap : 2 // 3 244 join : 2 // 3 245 style : 2 // 3 246 filter: 2 // 4 247 flat : 8 // 1... 248 total : 32 249 */ 250static uint32_t pack_v68(const SkPaint& paint, unsigned flatFlags) { 251 uint32_t packed = 0; 252 const auto bm = paint.asBlendMode(); 253 const unsigned mode = bm ? static_cast<unsigned>(bm.value()) 254 : CUSTOM_BLEND_MODE_SENTINEL; 255 256 packed |= shift_bits(((unsigned)paint.isDither() << 1) | 257 (unsigned)paint.isAntiAlias(), 0, 8); 258 packed |= shift_bits(mode, 8, 8); 259 packed |= shift_bits(paint.getStrokeCap(), 16, 2); 260 packed |= shift_bits(paint.getStrokeJoin(), 18, 2); 261 packed |= shift_bits(paint.getStyle(), 20, 2); 262 packed |= shift_bits(0, 22, 2); // was filterquality 263 packed |= shift_bits(flatFlags, 24, 8); 264 return packed; 265} 266 267static uint32_t unpack_v68(SkPaint* paint, uint32_t packed, SkSafeRange& safe) { 268 paint->setAntiAlias((packed & 1) != 0); 269 paint->setDither((packed & 2) != 0); 270 packed >>= 8; 271 { 272 unsigned mode = packed & 0xFF; 273 if (mode != CUSTOM_BLEND_MODE_SENTINEL) { // sentinel for custom blender 274 paint->setBlendMode(safe.checkLE(mode, SkBlendMode::kLastMode)); 275 } 276 // else we will unflatten the custom blender 277 } 278 packed >>= 8; 279 paint->setStrokeCap(safe.checkLE(packed & 0x3, SkPaint::kLast_Cap)); 280 packed >>= 2; 281 paint->setStrokeJoin(safe.checkLE(packed & 0x3, SkPaint::kLast_Join)); 282 packed >>= 2; 283 paint->setStyle(safe.checkLE(packed & 0x3, SkPaint::kStrokeAndFill_Style)); 284 packed >>= 2; 285 // skip the (now ignored) filterquality bits 286 packed >>= 2; 287 288 return packed; 289} 290 291/* To save space/time, we analyze the paint, and write a truncated version of 292 it if there are not tricky elements like shaders, etc. 293 */ 294void SkPaintPriv::Flatten(const SkPaint& paint, SkWriteBuffer& buffer) { 295 uint8_t flatFlags = 0; 296 297 if (paint.getPathEffect() || 298 paint.getShader() || 299 paint.getMaskFilter() || 300 paint.getColorFilter() || 301 paint.getImageFilter() || 302 !paint.asBlendMode()) { 303 flatFlags |= kHasEffects_FlatFlag; 304 } 305 306 buffer.writeScalar(paint.getStrokeWidth()); 307 buffer.writeScalar(paint.getStrokeMiter()); 308 buffer.writeColor4f(paint.getColor4f()); 309 310 buffer.write32(pack_v68(paint, flatFlags)); 311 312 if (flatFlags & kHasEffects_FlatFlag) { 313 buffer.writeFlattenable(paint.getPathEffect()); 314 buffer.writeFlattenable(paint.getShader()); 315 buffer.writeFlattenable(paint.getMaskFilter()); 316 buffer.writeFlattenable(paint.getColorFilter()); 317 buffer.writeFlattenable(paint.getImageFilter()); 318 buffer.writeFlattenable(paint.getBlender()); 319 } 320} 321 322SkPaint SkPaintPriv::Unflatten(SkReadBuffer& buffer) { 323 SkPaint paint; 324 325 paint.setStrokeWidth(buffer.readScalar()); 326 paint.setStrokeMiter(buffer.readScalar()); 327 { 328 SkColor4f color; 329 buffer.readColor4f(&color); 330 paint.setColor(color, sk_srgb_singleton()); 331 } 332 333 SkSafeRange safe; 334 unsigned flatFlags = unpack_v68(&paint, buffer.readUInt(), safe); 335 336 if (!(flatFlags & kHasEffects_FlatFlag)) { 337 // This is a simple SkPaint without any effects, so clear all the effect-related fields. 338 paint.setPathEffect(nullptr); 339 paint.setShader(nullptr); 340 paint.setMaskFilter(nullptr); 341 paint.setColorFilter(nullptr); 342 paint.setImageFilter(nullptr); 343 } else if (buffer.isVersionLT(SkPicturePriv::kSkBlenderInSkPaint)) { 344 // This paint predates the introduction of user blend functions (via SkBlender). 345 paint.setPathEffect(buffer.readPathEffect()); 346 paint.setShader(buffer.readShader()); 347 paint.setMaskFilter(buffer.readMaskFilter()); 348 paint.setColorFilter(buffer.readColorFilter()); 349 (void)buffer.read32(); // was drawLooper (now deprecated) 350 paint.setImageFilter(buffer.readImageFilter()); 351 } else { 352 paint.setPathEffect(buffer.readPathEffect()); 353 paint.setShader(buffer.readShader()); 354 paint.setMaskFilter(buffer.readMaskFilter()); 355 paint.setColorFilter(buffer.readColorFilter()); 356 paint.setImageFilter(buffer.readImageFilter()); 357 paint.setBlender(buffer.readBlender()); 358 } 359 360 if (!buffer.validate(safe.ok())) { 361 paint.reset(); 362 } 363 return paint; 364} 365 366/////////////////////////////////////////////////////////////////////////////// 367 368bool SkPaint::getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect, 369 SkScalar resScale) const { 370 return this->getFillPath(src, dst, cullRect, SkMatrix::Scale(resScale, resScale)); 371} 372 373bool SkPaint::getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect, 374 const SkMatrix& ctm) const { 375 if (!src.isFinite()) { 376 dst->reset(); 377 return false; 378 } 379 380 const SkScalar resScale = SkPaintPriv::ComputeResScaleForStroking(ctm); 381 SkStrokeRec rec(*this, resScale); 382 383#if defined(SK_BUILD_FOR_FUZZER) 384 // Prevent lines with small widths from timing out. 385 if (rec.getStyle() == SkStrokeRec::Style::kStroke_Style && rec.getWidth() < 0.001) { 386 return false; 387 } 388#endif 389 390 const SkPath* srcPtr = &src; 391 SkPath tmpPath; 392 393 if (fPathEffect && fPathEffect->filterPath(&tmpPath, src, &rec, cullRect, ctm)) { 394 srcPtr = &tmpPath; 395 } 396 397 if (!rec.applyToPath(dst, *srcPtr)) { 398 if (srcPtr == &tmpPath) { 399 // If path's were copy-on-write, this trick would not be needed. 400 // As it is, we want to save making a deep-copy from tmpPath -> dst 401 // since we know we're just going to delete tmpPath when we return, 402 // so the swap saves that copy. 403 dst->swap(tmpPath); 404 } else { 405 *dst = *srcPtr; 406 } 407 } 408 409 if (!dst->isFinite()) { 410 dst->reset(); 411 return false; 412 } 413 return !rec.isHairlineStyle(); 414} 415 416bool SkPaint::canComputeFastBounds() const { 417 if (this->getImageFilter() && !this->getImageFilter()->canComputeFastBounds()) { 418 return false; 419 } 420 // Pass nullptr for the bounds to determine if they can be computed 421 if (this->getPathEffect() && 422 !as_PEB(this->getPathEffect())->computeFastBounds(nullptr)) { 423 return false; 424 } 425 return true; 426} 427 428const SkRect& SkPaint::doComputeFastBounds(const SkRect& origSrc, 429 SkRect* storage, 430 Style style) const { 431 SkASSERT(storage); 432 433 const SkRect* src = &origSrc; 434 435 SkRect tmpSrc; 436 if (this->getPathEffect()) { 437 tmpSrc = origSrc; 438 SkAssertResult(as_PEB(this->getPathEffect())->computeFastBounds(&tmpSrc)); 439 src = &tmpSrc; 440 } 441 442 SkScalar radius = SkStrokeRec::GetInflationRadius(*this, style); 443 *storage = src->makeOutset(radius, radius); 444 445 if (this->getMaskFilter()) { 446 as_MFB(this->getMaskFilter())->computeFastBounds(*storage, storage); 447 } 448 449 if (this->getImageFilter()) { 450 *storage = this->getImageFilter()->computeFastBounds(*storage); 451 } 452 453 return *storage; 454} 455 456/////////////////////////////////////////////////////////////////////////////// 457 458// return true if the filter exists, and may affect alpha 459static bool affects_alpha(const SkColorFilter* cf) { 460 return cf && !as_CFB(cf)->isAlphaUnchanged(); 461} 462 463// return true if the filter exists, and may affect alpha 464static bool affects_alpha(const SkImageFilter* imf) { 465 // TODO: check if we should allow imagefilters to broadcast that they don't affect alpha 466 // ala colorfilters 467 return imf != nullptr; 468} 469 470bool SkPaint::nothingToDraw() const { 471 auto bm = this->asBlendMode(); 472 if (!bm) { 473 return false; 474 } 475 switch (bm.value()) { 476 case SkBlendMode::kSrcOver: 477 case SkBlendMode::kSrcATop: 478 case SkBlendMode::kDstOut: 479 case SkBlendMode::kDstOver: 480 case SkBlendMode::kPlus: 481 if (0 == this->getAlpha()) { 482 return !affects_alpha(fColorFilter.get()) && !affects_alpha(fImageFilter.get()); 483 } 484 break; 485 case SkBlendMode::kDst: 486 return true; 487 default: 488 break; 489 } 490 return false; 491} 492