1 /* 2 * Copyright 2012 Google Inc. 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 #ifndef SkPathRef_DEFINED 9 #define SkPathRef_DEFINED 10 11 #include "include/core/SkArc.h" 12 #include "include/core/SkPoint.h" 13 #include "include/core/SkRect.h" 14 #include "include/core/SkRefCnt.h" 15 #include "include/core/SkScalar.h" 16 #include "include/core/SkTypes.h" 17 #include "include/private/SkIDChangeListener.h" 18 #include "include/private/base/SkDebug.h" 19 #include "include/private/base/SkSpan_impl.h" 20 #include "include/private/base/SkTArray.h" 21 #include "include/private/base/SkTo.h" 22 23 #include <atomic> 24 #include <cstddef> 25 #include <cstdint> 26 #include <tuple> 27 28 class SkMatrix; 29 class SkRRect; 30 31 /** 32 * Holds the path verbs and points. It is versioned by a generation ID. None of its public methods 33 * modify the contents. To modify or append to the verbs/points wrap the SkPathRef in an 34 * SkPathRef::Editor object. Installing the editor resets the generation ID. It also performs 35 * copy-on-write if the SkPathRef is shared by multiple SkPaths. The caller passes the Editor's 36 * constructor a pointer to a sk_sp<SkPathRef>, which may be updated to point to a new SkPathRef 37 * after the editor's constructor returns. 38 * 39 * The points and verbs are stored in a single allocation. The points are at the begining of the 40 * allocation while the verbs are stored at end of the allocation, in reverse order. Thus the points 41 * and verbs both grow into the middle of the allocation until the meet. To access verb i in the 42 * verb array use ref.verbs()[~i] (because verbs() returns a pointer just beyond the first 43 * logical verb or the last verb in memory). 44 */ 45 46 class SK_API SkPathRef final : public SkNVRefCnt<SkPathRef> { 47 public: 48 // See https://bugs.chromium.org/p/skia/issues/detail?id=13817 for how these sizes were 49 // determined. 50 using PointsArray = skia_private::STArray<4, SkPoint>; 51 using VerbsArray = skia_private::STArray<4, uint8_t>; 52 using ConicWeightsArray = skia_private::STArray<2, SkScalar>; 53 54 enum class PathType : uint8_t { 55 kGeneral, 56 kOval, 57 kRRect, 58 kArc, 59 }; 60 SkPathRef(SkSpan<const SkPoint> points,SkSpan<const uint8_t> verbs,SkSpan<const SkScalar> weights,unsigned segmentMask)61 SkPathRef(SkSpan<const SkPoint> points, SkSpan<const uint8_t> verbs, 62 SkSpan<const SkScalar> weights, unsigned segmentMask) 63 : fPoints(points) 64 , fVerbs(verbs) 65 , fConicWeights(weights) 66 { 67 fBoundsIsDirty = true; // this also invalidates fIsFinite 68 fGenerationID = 0; // recompute 69 fSegmentMask = segmentMask; 70 fType = PathType::kGeneral; 71 // The next two values don't matter unless fType is kOval or kRRect 72 fRRectOrOvalIsCCW = false; 73 fRRectOrOvalStartIdx = 0xAC; 74 fArcOval.setEmpty(); 75 fArcStartAngle = fArcSweepAngle = 0.0f; 76 fArcType = SkArc::Type::kArc; 77 SkDEBUGCODE(fEditorsAttached.store(0);) 78 79 this->computeBounds(); // do this now, before we worry about multiple owners/threads 80 SkDEBUGCODE(this->validate();) 81 } 82 83 class Editor { 84 public: 85 Editor(sk_sp<SkPathRef>* pathRef, 86 int incReserveVerbs = 0, 87 int incReservePoints = 0, 88 int incReserveConics = 0); 89 ~Editor()90 ~Editor() { SkDEBUGCODE(fPathRef->fEditorsAttached--;) } 91 92 /** 93 * Returns the array of points. 94 */ writablePoints()95 SkPoint* writablePoints() { return fPathRef->getWritablePoints(); } points()96 const SkPoint* points() const { return fPathRef->points(); } 97 98 /** 99 * Gets the ith point. Shortcut for this->points() + i 100 */ atPoint(int i)101 SkPoint* atPoint(int i) { return fPathRef->getWritablePoints() + i; } atPoint(int i)102 const SkPoint* atPoint(int i) const { return &fPathRef->fPoints[i]; } 103 104 /** 105 * Adds the verb and allocates space for the number of points indicated by the verb. The 106 * return value is a pointer to where the points for the verb should be written. 107 * 'weight' is only used if 'verb' is kConic_Verb 108 */ 109 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight = 0) { 110 SkDEBUGCODE(fPathRef->validate();) 111 return fPathRef->growForVerb(verb, weight); 112 } 113 114 /** 115 * Allocates space for multiple instances of a particular verb and the 116 * requisite points & weights. 117 * The return pointer points at the first new point (indexed normally [<i>]). 118 * If 'verb' is kConic_Verb, 'weights' will return a pointer to the 119 * space for the conic weights (indexed normally). 120 */ 121 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb, 122 int numVbs, 123 SkScalar** weights = nullptr) { 124 return fPathRef->growForRepeatedVerb(verb, numVbs, weights); 125 } 126 127 /** 128 * Concatenates all verbs from 'path' onto the pathRef's verbs array. Increases the point 129 * count by the number of points in 'path', and the conic weight count by the number of 130 * conics in 'path'. 131 * 132 * Returns pointers to the uninitialized points and conic weights data. 133 */ growForVerbsInPath(const SkPathRef & path)134 std::tuple<SkPoint*, SkScalar*> growForVerbsInPath(const SkPathRef& path) { 135 return fPathRef->growForVerbsInPath(path); 136 } 137 138 /** 139 * Resets the path ref to a new verb and point count. The new verbs and points are 140 * uninitialized. 141 */ resetToSize(int newVerbCnt,int newPointCnt,int newConicCount)142 void resetToSize(int newVerbCnt, int newPointCnt, int newConicCount) { 143 fPathRef->resetToSize(newVerbCnt, newPointCnt, newConicCount); 144 } 145 146 /** 147 * Gets the path ref that is wrapped in the Editor. 148 */ pathRef()149 SkPathRef* pathRef() { return fPathRef; } 150 setIsOval(bool isCCW,unsigned start)151 void setIsOval(bool isCCW, unsigned start) { 152 fPathRef->setIsOval(isCCW, start); 153 } 154 setIsRRect(bool isCCW,unsigned start)155 void setIsRRect(bool isCCW, unsigned start) { 156 fPathRef->setIsRRect(isCCW, start); 157 } 158 setIsArc(const SkArc & arc)159 void setIsArc(const SkArc& arc) { 160 fPathRef->setIsArc(arc); 161 } 162 setBounds(const SkRect & rect)163 void setBounds(const SkRect& rect) { fPathRef->setBounds(rect); } 164 165 private: 166 SkPathRef* fPathRef; 167 }; 168 169 class SK_API Iter { 170 public: 171 Iter(); 172 Iter(const SkPathRef&); 173 174 void setPathRef(const SkPathRef&); 175 176 /** Return the next verb in this iteration of the path. When all 177 segments have been visited, return kDone_Verb. 178 179 If any point in the path is non-finite, return kDone_Verb immediately. 180 181 @param pts The points representing the current verb and/or segment 182 This must not be NULL. 183 @return The verb for the current segment 184 */ 185 uint8_t next(SkPoint pts[4]); 186 uint8_t peek() const; 187 conicWeight()188 SkScalar conicWeight() const { return *fConicWeights; } 189 190 private: 191 const SkPoint* fPts; 192 const uint8_t* fVerbs; 193 const uint8_t* fVerbStop; 194 const SkScalar* fConicWeights; 195 }; 196 197 public: 198 /** 199 * Gets a path ref with no verbs or points. 200 */ 201 static SkPathRef* CreateEmpty(); 202 203 /** 204 * Returns true if all of the points in this path are finite, meaning there 205 * are no infinities and no NaNs. 206 */ isFinite()207 bool isFinite() const { 208 if (fBoundsIsDirty) { 209 this->computeBounds(); 210 } 211 return SkToBool(fIsFinite); 212 } 213 214 /** 215 * Returns a mask, where each bit corresponding to a SegmentMask is 216 * set if the path contains 1 or more segments of that type. 217 * Returns 0 for an empty path (no segments). 218 */ getSegmentMasks()219 uint32_t getSegmentMasks() const { return fSegmentMask; } 220 221 /** Returns true if the path is an oval. 222 * 223 * @param rect returns the bounding rect of this oval. It's a circle 224 * if the height and width are the same. 225 * @param isCCW is the oval CCW (or CW if false). 226 * @param start indicates where the contour starts on the oval (see 227 * SkPath::addOval for intepretation of the index). 228 * 229 * @return true if this path is an oval. 230 * Tracking whether a path is an oval is considered an 231 * optimization for performance and so some paths that are in 232 * fact ovals can report false. 233 */ isOval(SkRect * rect,bool * isCCW,unsigned * start)234 bool isOval(SkRect* rect, bool* isCCW, unsigned* start) const { 235 if (fType == PathType::kOval) { 236 if (rect) { 237 *rect = this->getBounds(); 238 } 239 if (isCCW) { 240 *isCCW = SkToBool(fRRectOrOvalIsCCW); 241 } 242 if (start) { 243 *start = fRRectOrOvalStartIdx; 244 } 245 } 246 247 return fType == PathType::kOval; 248 } 249 250 bool isRRect(SkRRect* rrect, bool* isCCW, unsigned* start) const; 251 isArc(SkArc * arc)252 bool isArc(SkArc* arc) const { 253 if (fType == PathType::kArc) { 254 if (arc) { 255 *arc = SkArc::Make(fArcOval, fArcStartAngle, fArcSweepAngle, fArcType); 256 } 257 } 258 259 return fType == PathType::kArc; 260 } 261 hasComputedBounds()262 bool hasComputedBounds() const { 263 return !fBoundsIsDirty; 264 } 265 266 /** Returns the bounds of the path's points. If the path contains 0 or 1 267 points, the bounds is set to (0,0,0,0), and isEmpty() will return true. 268 Note: this bounds may be larger than the actual shape, since curves 269 do not extend as far as their control points. 270 */ getBounds()271 const SkRect& getBounds() const { 272 if (fBoundsIsDirty) { 273 this->computeBounds(); 274 } 275 return fBounds; 276 } 277 278 SkRRect getRRect() const; 279 280 /** 281 * Transforms a path ref by a matrix, allocating a new one only if necessary. 282 */ 283 static void CreateTransformedCopy(sk_sp<SkPathRef>* dst, 284 const SkPathRef& src, 285 const SkMatrix& matrix); 286 287 // static SkPathRef* CreateFromBuffer(SkRBuffer* buffer); 288 289 /** 290 * Rollsback a path ref to zero verbs and points with the assumption that the path ref will be 291 * repopulated with approximately the same number of verbs and points. A new path ref is created 292 * only if necessary. 293 */ 294 static void Rewind(sk_sp<SkPathRef>* pathRef); 295 296 ~SkPathRef(); countPoints()297 int countPoints() const { return fPoints.size(); } countVerbs()298 int countVerbs() const { return fVerbs.size(); } countWeights()299 int countWeights() const { return fConicWeights.size(); } 300 301 size_t approximateBytesUsed() const; 302 303 /** 304 * Returns a pointer one beyond the first logical verb (last verb in memory order). 305 */ verbsBegin()306 const uint8_t* verbsBegin() const { return fVerbs.begin(); } 307 308 /** 309 * Returns a const pointer to the first verb in memory (which is the last logical verb). 310 */ verbsEnd()311 const uint8_t* verbsEnd() const { return fVerbs.end(); } 312 313 /** 314 * Returns a const pointer to the first point. 315 */ points()316 const SkPoint* points() const { return fPoints.begin(); } 317 318 /** 319 * Shortcut for this->points() + this->countPoints() 320 */ pointsEnd()321 const SkPoint* pointsEnd() const { return this->points() + this->countPoints(); } 322 conicWeights()323 const SkScalar* conicWeights() const { return fConicWeights.begin(); } conicWeightsEnd()324 const SkScalar* conicWeightsEnd() const { return fConicWeights.end(); } 325 326 /** 327 * Convenience methods for getting to a verb or point by index. 328 */ atVerb(int index)329 uint8_t atVerb(int index) const { return fVerbs[index]; } atPoint(int index)330 const SkPoint& atPoint(int index) const { return fPoints[index]; } 331 332 bool operator== (const SkPathRef& ref) const; 333 334 void interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef* out) const; 335 336 /** 337 * Gets an ID that uniquely identifies the contents of the path ref. If two path refs have the 338 * same ID then they have the same verbs and points. However, two path refs may have the same 339 * contents but different genIDs. 340 * skbug.com/1762 for background on why fillType is necessary (for now). 341 */ 342 uint32_t genID(uint8_t fillType) const; 343 344 void addGenIDChangeListener(sk_sp<SkIDChangeListener>); // Threadsafe. 345 int genIDChangeListenerCount(); // Threadsafe 346 347 bool dataMatchesVerbs() const; 348 bool isValid() const; 349 SkDEBUGCODE(void validate() const { SkASSERT(this->isValid()); } ) 350 351 /** 352 * Resets this SkPathRef to a clean state. 353 */ 354 void reset(); 355 isInitialEmptyPathRef()356 bool isInitialEmptyPathRef() const { 357 return fGenerationID == kEmptyGenID; 358 } 359 360 private: 361 enum SerializationOffsets { 362 kLegacyRRectOrOvalStartIdx_SerializationShift = 28, // requires 3 bits, ignored. 363 kLegacyRRectOrOvalIsCCW_SerializationShift = 27, // requires 1 bit, ignored. 364 kLegacyIsRRect_SerializationShift = 26, // requires 1 bit, ignored. 365 kIsFinite_SerializationShift = 25, // requires 1 bit 366 kLegacyIsOval_SerializationShift = 24, // requires 1 bit, ignored. 367 kSegmentMask_SerializationShift = 0 // requires 4 bits (deprecated) 368 }; 369 370 SkPathRef(int numVerbs = 0, int numPoints = 0, int numConics = 0) { 371 fBoundsIsDirty = true; // this also invalidates fIsFinite 372 fGenerationID = kEmptyGenID; 373 fSegmentMask = 0; 374 fType = PathType::kGeneral; 375 // The next two values don't matter unless fType is kOval or kRRect 376 fRRectOrOvalIsCCW = false; 377 fRRectOrOvalStartIdx = 0xAC; 378 fArcOval.setEmpty(); 379 fArcStartAngle = fArcSweepAngle = 0.0f; 380 fArcType = SkArc::Type::kArc; 381 if (numPoints > 0) { 382 fPoints.reserve_exact(numPoints); 383 } 384 if (numVerbs > 0) { 385 fVerbs.reserve_exact(numVerbs); 386 } 387 if (numConics > 0) { 388 fConicWeights.reserve_exact(numConics); 389 } 390 SkDEBUGCODE(fEditorsAttached.store(0);) 391 SkDEBUGCODE(this->validate();) 392 } 393 394 void copy(const SkPathRef& ref, int additionalReserveVerbs, int additionalReservePoints, int additionalReserveConics); 395 396 // Return true if the computed bounds are finite. ComputePtBounds(SkRect * bounds,const SkPathRef & ref)397 static bool ComputePtBounds(SkRect* bounds, const SkPathRef& ref) { 398 return bounds->setBoundsCheck(ref.points(), ref.countPoints()); 399 } 400 401 // called, if dirty, by getBounds() computeBounds()402 void computeBounds() const { 403 SkDEBUGCODE(this->validate();) 404 // TODO: remove fBoundsIsDirty and fIsFinite, 405 // using an inverted rect instead of fBoundsIsDirty and always recalculating fIsFinite. 406 SkASSERT(fBoundsIsDirty); 407 408 fIsFinite = ComputePtBounds(&fBounds, *this); 409 fBoundsIsDirty = false; 410 } 411 setBounds(const SkRect & rect)412 void setBounds(const SkRect& rect) { 413 SkASSERT(rect.fLeft <= rect.fRight && rect.fTop <= rect.fBottom); 414 fBounds = rect; 415 fBoundsIsDirty = false; 416 fIsFinite = fBounds.isFinite(); 417 } 418 419 /** Makes additional room but does not change the counts or change the genID */ incReserve(int additionalVerbs,int additionalPoints,int additionalConics)420 void incReserve(int additionalVerbs, int additionalPoints, int additionalConics) { 421 SkDEBUGCODE(this->validate();) 422 // Use reserve() so that if there is not enough space, the array will grow with some 423 // additional space. This ensures repeated calls to grow won't always allocate. 424 if (additionalPoints > 0) { 425 fPoints.reserve(fPoints.size() + additionalPoints); 426 } 427 if (additionalVerbs > 0) { 428 fVerbs.reserve(fVerbs.size() + additionalVerbs); 429 } 430 if (additionalConics > 0) { 431 fConicWeights.reserve(fConicWeights.size() + additionalConics); 432 } 433 SkDEBUGCODE(this->validate();) 434 } 435 436 /** 437 * Resets all state except that of the verbs, points, and conic-weights. 438 * Intended to be called from other functions that reset state. 439 */ commonReset()440 void commonReset() { 441 SkDEBUGCODE(this->validate();) 442 this->callGenIDChangeListeners(); 443 fBoundsIsDirty = true; // this also invalidates fIsFinite 444 fGenerationID = 0; 445 446 fSegmentMask = 0; 447 fType = PathType::kGeneral; 448 } 449 450 /** Resets the path ref with verbCount verbs and pointCount points, all uninitialized. Also 451 * allocates space for reserveVerb additional verbs and reservePoints additional points.*/ 452 void resetToSize(int verbCount, int pointCount, int conicCount, 453 int reserveVerbs = 0, int reservePoints = 0, 454 int reserveConics = 0) { 455 this->commonReset(); 456 // Use reserve_exact() so the arrays are sized to exactly fit the data. 457 fPoints.reserve_exact(pointCount + reservePoints); 458 fPoints.resize_back(pointCount); 459 460 fVerbs.reserve_exact(verbCount + reserveVerbs); 461 fVerbs.resize_back(verbCount); 462 463 fConicWeights.reserve_exact(conicCount + reserveConics); 464 fConicWeights.resize_back(conicCount); 465 SkDEBUGCODE(this->validate();) 466 } 467 468 /** 469 * Increases the verb count by numVbs and point count by the required amount. 470 * The new points are uninitialized. All the new verbs are set to the specified 471 * verb. If 'verb' is kConic_Verb, 'weights' will return a pointer to the 472 * uninitialized conic weights. 473 */ 474 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb, int numVbs, SkScalar** weights); 475 476 /** 477 * Increases the verb count 1, records the new verb, and creates room for the requisite number 478 * of additional points. A pointer to the first point is returned. Any new points are 479 * uninitialized. 480 */ 481 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight); 482 483 /** 484 * Concatenates all verbs from 'path' onto our own verbs array. Increases the point count by the 485 * number of points in 'path', and the conic weight count by the number of conics in 'path'. 486 * 487 * Returns pointers to the uninitialized points and conic weights data. 488 */ 489 std::tuple<SkPoint*, SkScalar*> growForVerbsInPath(const SkPathRef& path); 490 491 /** 492 * Private, non-const-ptr version of the public function verbsMemBegin(). 493 */ verbsBeginWritable()494 uint8_t* verbsBeginWritable() { return fVerbs.begin(); } 495 496 /** 497 * Called the first time someone calls CreateEmpty to actually create the singleton. 498 */ 499 friend SkPathRef* sk_create_empty_pathref(); 500 setIsOval(bool isCCW,unsigned start)501 void setIsOval(bool isCCW, unsigned start) { 502 fType = PathType::kOval; 503 fRRectOrOvalIsCCW = isCCW; 504 fRRectOrOvalStartIdx = SkToU8(start); 505 } 506 setIsRRect(bool isCCW,unsigned start)507 void setIsRRect(bool isCCW, unsigned start) { 508 fType = PathType::kRRect; 509 fRRectOrOvalIsCCW = isCCW; 510 fRRectOrOvalStartIdx = SkToU8(start); 511 } 512 setIsArc(const SkArc & arc)513 void setIsArc(const SkArc& arc) { 514 fType = PathType::kArc; 515 fArcOval = arc.fOval; 516 fArcStartAngle = arc.fStartAngle; 517 fArcSweepAngle = arc.fSweepAngle; 518 fArcType = arc.fType; 519 } 520 521 // called only by the editor. Note that this is not a const function. getWritablePoints()522 SkPoint* getWritablePoints() { 523 SkDEBUGCODE(this->validate();) 524 fType = PathType::kGeneral; 525 return fPoints.begin(); 526 } 527 getPoints()528 const SkPoint* getPoints() const { 529 SkDEBUGCODE(this->validate();) 530 return fPoints.begin(); 531 } 532 533 void callGenIDChangeListeners(); 534 535 PointsArray fPoints; 536 VerbsArray fVerbs; 537 ConicWeightsArray fConicWeights; 538 539 mutable SkRect fBounds; 540 SkRect fArcOval; 541 542 enum { 543 kEmptyGenID = 1, // GenID reserved for path ref with zero points and zero verbs. 544 }; 545 mutable uint32_t fGenerationID; 546 SkIDChangeListener::List fGenIDChangeListeners; 547 548 SkDEBUGCODE(std::atomic<int> fEditorsAttached;) // assert only one editor in use at any time. 549 550 SkScalar fArcStartAngle; 551 SkScalar fArcSweepAngle; 552 553 PathType fType; 554 555 mutable uint8_t fBoundsIsDirty; 556 557 uint8_t fRRectOrOvalStartIdx; 558 uint8_t fSegmentMask; 559 // If the path is an arc, these four variables store that information. 560 // We should just store an SkArc, but alignment would cost us 8 more bytes. 561 SkArc::Type fArcType; 562 563 mutable bool fIsFinite; // only meaningful if bounds are valid 564 // Both the circle and rrect special cases have a notion of direction and starting point 565 // The next two variables store that information for either. 566 bool fRRectOrOvalIsCCW; 567 568 friend class PathRefTest_Private; 569 friend class ForceIsRRect_Private; // unit test isRRect 570 friend class SkPath; 571 friend class SkPathBuilder; 572 friend class SkPathPriv; 573 }; 574 575 #endif 576