bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #ifndef SkPathRef_DEFINED |
| 10 | #define SkPathRef_DEFINED |
| 11 | |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 12 | #include "../private/SkAtomics.h" |
bungeman | a7e9f05 | 2016-02-18 08:53:33 -0800 | [diff] [blame] | 13 | #include "../private/SkTDArray.h" |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 14 | #include "SkMatrix.h" |
| 15 | #include "SkPoint.h" |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 16 | #include "SkRRect.h" |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 17 | #include "SkRect.h" |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 18 | #include "SkRefCnt.h" |
Ben Wagner | ac32662 | 2017-07-31 16:57:01 -0400 | [diff] [blame] | 19 | #include "SkTemplates.h" |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 20 | |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 21 | class SkRBuffer; |
| 22 | class SkWBuffer; |
| 23 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 24 | /** |
| 25 | * Holds the path verbs and points. It is versioned by a generation ID. None of its public methods |
| 26 | * modify the contents. To modify or append to the verbs/points wrap the SkPathRef in an |
| 27 | * SkPathRef::Editor object. Installing the editor resets the generation ID. It also performs |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 28 | * copy-on-write if the SkPathRef is shared by multiple SkPaths. The caller passes the Editor's |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 29 | * constructor a pointer to a sk_sp<SkPathRef>, which may be updated to point to a new SkPathRef |
| 30 | * after the editor's constructor returns. |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 31 | * |
| 32 | * The points and verbs are stored in a single allocation. The points are at the begining of the |
| 33 | * allocation while the verbs are stored at end of the allocation, in reverse order. Thus the points |
| 34 | * and verbs both grow into the middle of the allocation until the meet. To access verb i in the |
| 35 | * verb array use ref.verbs()[~i] (because verbs() returns a pointer just beyond the first |
| 36 | * logical verb or the last verb in memory). |
| 37 | */ |
bsalomon@google.com | ae09f2d | 2012-10-03 19:57:01 +0000 | [diff] [blame] | 38 | |
mtklein | b47cd4b | 2016-08-09 12:20:04 -0700 | [diff] [blame] | 39 | class SK_API SkPathRef final : public SkNVRefCnt<SkPathRef> { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 40 | public: |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 41 | class Editor { |
| 42 | public: |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 43 | Editor(sk_sp<SkPathRef>* pathRef, |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 44 | int incReserveVerbs = 0, |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 45 | int incReservePoints = 0); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 46 | |
commit-bot@chromium.org | f48e475 | 2013-06-27 18:39:39 +0000 | [diff] [blame] | 47 | ~Editor() { SkDEBUGCODE(sk_atomic_dec(&fPathRef->fEditorsAttached);) } |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Returns the array of points. |
| 51 | */ |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 52 | SkPoint* points() { return fPathRef->getPoints(); } |
| 53 | const SkPoint* points() const { return fPathRef->points(); } |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Gets the ith point. Shortcut for this->points() + i |
| 57 | */ |
| 58 | SkPoint* atPoint(int i) { |
| 59 | SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt); |
| 60 | return this->points() + i; |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 61 | } |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 62 | const SkPoint* atPoint(int i) const { |
| 63 | SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt); |
| 64 | return this->points() + i; |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 65 | } |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Adds the verb and allocates space for the number of points indicated by the verb. The |
| 69 | * return value is a pointer to where the points for the verb should be written. |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 70 | * 'weight' is only used if 'verb' is kConic_Verb |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 71 | */ |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 72 | SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight = 0) { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 73 | SkDEBUGCODE(fPathRef->validate();) |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 74 | return fPathRef->growForVerb(verb, weight); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 75 | } |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 76 | |
| 77 | /** |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 78 | * Allocates space for multiple instances of a particular verb and the |
| 79 | * requisite points & weights. |
| 80 | * The return pointer points at the first new point (indexed normally [<i>]). |
| 81 | * If 'verb' is kConic_Verb, 'weights' will return a pointer to the |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 82 | * space for the conic weights (indexed normally). |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 83 | */ |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 84 | SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb, |
| 85 | int numVbs, |
| 86 | SkScalar** weights = NULL) { |
| 87 | return fPathRef->growForRepeatedVerb(verb, numVbs, weights); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Resets the path ref to a new verb and point count. The new verbs and points are |
| 92 | * uninitialized. |
| 93 | */ |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 94 | void resetToSize(int newVerbCnt, int newPointCnt, int newConicCount) { |
| 95 | fPathRef->resetToSize(newVerbCnt, newPointCnt, newConicCount); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 96 | } |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 97 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 98 | /** |
| 99 | * Gets the path ref that is wrapped in the Editor. |
| 100 | */ |
| 101 | SkPathRef* pathRef() { return fPathRef; } |
| 102 | |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 103 | void setIsOval(bool isOval, bool isCCW, unsigned start) { |
| 104 | fPathRef->setIsOval(isOval, isCCW, start); |
| 105 | } |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 106 | |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 107 | void setIsRRect(bool isRRect, bool isCCW, unsigned start) { |
| 108 | fPathRef->setIsRRect(isRRect, isCCW, start); |
| 109 | } |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 110 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 111 | void setBounds(const SkRect& rect) { fPathRef->setBounds(rect); } |
| 112 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 113 | private: |
| 114 | SkPathRef* fPathRef; |
| 115 | }; |
| 116 | |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 117 | class SK_API Iter { |
| 118 | public: |
| 119 | Iter(); |
| 120 | Iter(const SkPathRef&); |
| 121 | |
| 122 | void setPathRef(const SkPathRef&); |
| 123 | |
| 124 | /** Return the next verb in this iteration of the path. When all |
| 125 | segments have been visited, return kDone_Verb. |
| 126 | |
| 127 | @param pts The points representing the current verb and/or segment |
| 128 | This must not be NULL. |
| 129 | @return The verb for the current segment |
| 130 | */ |
| 131 | uint8_t next(SkPoint pts[4]); |
caryclark | 2028d7f | 2015-12-09 14:04:46 -0800 | [diff] [blame] | 132 | uint8_t peek() const; |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 133 | |
| 134 | SkScalar conicWeight() const { return *fConicWeights; } |
| 135 | |
| 136 | private: |
| 137 | const SkPoint* fPts; |
| 138 | const uint8_t* fVerbs; |
| 139 | const uint8_t* fVerbStop; |
| 140 | const SkScalar* fConicWeights; |
| 141 | }; |
| 142 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 143 | public: |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 144 | /** |
| 145 | * Gets a path ref with no verbs or points. |
| 146 | */ |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 147 | static SkPathRef* CreateEmpty(); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 148 | |
| 149 | /** |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 150 | * Returns true if all of the points in this path are finite, meaning there |
| 151 | * are no infinities and no NaNs. |
| 152 | */ |
| 153 | bool isFinite() const { |
| 154 | if (fBoundsIsDirty) { |
| 155 | this->computeBounds(); |
| 156 | } |
| 157 | return SkToBool(fIsFinite); |
| 158 | } |
| 159 | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 160 | /** |
| 161 | * Returns a mask, where each bit corresponding to a SegmentMask is |
| 162 | * set if the path contains 1 or more segments of that type. |
| 163 | * Returns 0 for an empty path (no segments). |
| 164 | */ |
| 165 | uint32_t getSegmentMasks() const { return fSegmentMask; } |
| 166 | |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 167 | /** Returns true if the path is an oval. |
| 168 | * |
| 169 | * @param rect returns the bounding rect of this oval. It's a circle |
| 170 | * if the height and width are the same. |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 171 | * @param isCCW is the oval CCW (or CW if false). |
| 172 | * @param start indicates where the contour starts on the oval (see |
| 173 | * SkPath::addOval for intepretation of the index). |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 174 | * |
| 175 | * @return true if this path is an oval. |
| 176 | * Tracking whether a path is an oval is considered an |
| 177 | * optimization for performance and so some paths that are in |
| 178 | * fact ovals can report false. |
| 179 | */ |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 180 | bool isOval(SkRect* rect, bool* isCCW, unsigned* start) const { |
| 181 | if (fIsOval) { |
| 182 | if (rect) { |
| 183 | *rect = this->getBounds(); |
| 184 | } |
| 185 | if (isCCW) { |
| 186 | *isCCW = SkToBool(fRRectOrOvalIsCCW); |
| 187 | } |
| 188 | if (start) { |
| 189 | *start = fRRectOrOvalStartIdx; |
| 190 | } |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | return SkToBool(fIsOval); |
| 194 | } |
| 195 | |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 196 | bool isRRect(SkRRect* rrect, bool* isCCW, unsigned* start) const { |
| 197 | if (fIsRRect) { |
| 198 | if (rrect) { |
| 199 | *rrect = this->getRRect(); |
| 200 | } |
| 201 | if (isCCW) { |
| 202 | *isCCW = SkToBool(fRRectOrOvalIsCCW); |
| 203 | } |
| 204 | if (start) { |
| 205 | *start = fRRectOrOvalStartIdx; |
| 206 | } |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 207 | } |
| 208 | return SkToBool(fIsRRect); |
| 209 | } |
| 210 | |
| 211 | |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 212 | bool hasComputedBounds() const { |
| 213 | return !fBoundsIsDirty; |
| 214 | } |
| 215 | |
| 216 | /** Returns the bounds of the path's points. If the path contains 0 or 1 |
| 217 | points, the bounds is set to (0,0,0,0), and isEmpty() will return true. |
| 218 | Note: this bounds may be larger than the actual shape, since curves |
| 219 | do not extend as far as their control points. |
| 220 | */ |
skia.committer@gmail.com | 65caeaf | 2013-09-27 07:01:29 +0000 | [diff] [blame] | 221 | const SkRect& getBounds() const { |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 222 | if (fBoundsIsDirty) { |
| 223 | this->computeBounds(); |
| 224 | } |
| 225 | return fBounds; |
| 226 | } |
| 227 | |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 228 | SkRRect getRRect() const; |
| 229 | |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 230 | /** |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 231 | * Transforms a path ref by a matrix, allocating a new one only if necessary. |
| 232 | */ |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 233 | static void CreateTransformedCopy(sk_sp<SkPathRef>* dst, |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 234 | const SkPathRef& src, |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 235 | const SkMatrix& matrix); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 236 | |
commit-bot@chromium.org | fed2ab6 | 2014-01-23 15:16:05 +0000 | [diff] [blame] | 237 | static SkPathRef* CreateFromBuffer(SkRBuffer* buffer); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 238 | |
| 239 | /** |
| 240 | * Rollsback a path ref to zero verbs and points with the assumption that the path ref will be |
| 241 | * repopulated with approximately the same number of verbs and points. A new path ref is created |
| 242 | * only if necessary. |
| 243 | */ |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 244 | static void Rewind(sk_sp<SkPathRef>* pathRef); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 245 | |
mtklein | b47cd4b | 2016-08-09 12:20:04 -0700 | [diff] [blame] | 246 | ~SkPathRef(); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 247 | int countPoints() const { SkDEBUGCODE(this->validate();) return fPointCnt; } |
| 248 | int countVerbs() const { SkDEBUGCODE(this->validate();) return fVerbCnt; } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 249 | int countWeights() const { SkDEBUGCODE(this->validate();) return fConicWeights.count(); } |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 250 | |
| 251 | /** |
| 252 | * Returns a pointer one beyond the first logical verb (last verb in memory order). |
| 253 | */ |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 254 | const uint8_t* verbs() const { SkDEBUGCODE(this->validate();) return fVerbs; } |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * Returns a const pointer to the first verb in memory (which is the last logical verb). |
| 258 | */ |
| 259 | const uint8_t* verbsMemBegin() const { return this->verbs() - fVerbCnt; } |
| 260 | |
| 261 | /** |
| 262 | * Returns a const pointer to the first point. |
| 263 | */ |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 264 | const SkPoint* points() const { SkDEBUGCODE(this->validate();) return fPoints; } |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * Shortcut for this->points() + this->countPoints() |
| 268 | */ |
| 269 | const SkPoint* pointsEnd() const { return this->points() + this->countPoints(); } |
| 270 | |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 271 | const SkScalar* conicWeights() const { SkDEBUGCODE(this->validate();) return fConicWeights.begin(); } |
| 272 | const SkScalar* conicWeightsEnd() const { SkDEBUGCODE(this->validate();) return fConicWeights.end(); } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 273 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 274 | /** |
| 275 | * Convenience methods for getting to a verb or point by index. |
| 276 | */ |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 277 | uint8_t atVerb(int index) const { |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 278 | SkASSERT((unsigned) index < (unsigned) fVerbCnt); |
| 279 | return this->verbs()[~index]; |
| 280 | } |
| 281 | const SkPoint& atPoint(int index) const { |
| 282 | SkASSERT((unsigned) index < (unsigned) fPointCnt); |
| 283 | return this->points()[index]; |
| 284 | } |
| 285 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 286 | bool operator== (const SkPathRef& ref) const; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 287 | |
| 288 | /** |
| 289 | * Writes the path points and verbs to a buffer. |
| 290 | */ |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 291 | void writeToBuffer(SkWBuffer* buffer) const; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 292 | |
| 293 | /** |
| 294 | * Gets the number of bytes that would be written in writeBuffer() |
| 295 | */ |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 296 | uint32_t writeSize() const; |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 297 | |
caryclark | 8e7b19d | 2016-02-18 04:11:48 -0800 | [diff] [blame] | 298 | void interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef* out) const; |
| 299 | |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 300 | /** |
| 301 | * Gets an ID that uniquely identifies the contents of the path ref. If two path refs have the |
| 302 | * same ID then they have the same verbs and points. However, two path refs may have the same |
| 303 | * contents but different genIDs. |
| 304 | */ |
| 305 | uint32_t genID() const; |
| 306 | |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 307 | struct GenIDChangeListener { |
| 308 | virtual ~GenIDChangeListener() {} |
| 309 | virtual void onChange() = 0; |
| 310 | }; |
| 311 | |
| 312 | void addGenIDChangeListener(GenIDChangeListener* listener); |
| 313 | |
reed | 5bcbe91 | 2014-12-15 12:28:33 -0800 | [diff] [blame] | 314 | SkDEBUGCODE(void validate() const;) |
| 315 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 316 | private: |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 317 | enum SerializationOffsets { |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 318 | kRRectOrOvalStartIdx_SerializationShift = 28, // requires 3 bits |
| 319 | kRRectOrOvalIsCCW_SerializationShift = 27, // requires 1 bit |
| 320 | kIsRRect_SerializationShift = 26, // requires 1 bit |
| 321 | kIsFinite_SerializationShift = 25, // requires 1 bit |
| 322 | kIsOval_SerializationShift = 24, // requires 1 bit |
| 323 | kSegmentMask_SerializationShift = 0 // requires 4 bits |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 324 | }; |
| 325 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 326 | SkPathRef() { |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 327 | fBoundsIsDirty = true; // this also invalidates fIsFinite |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 328 | fPointCnt = 0; |
| 329 | fVerbCnt = 0; |
| 330 | fVerbs = NULL; |
| 331 | fPoints = NULL; |
| 332 | fFreeSpace = 0; |
| 333 | fGenerationID = kEmptyGenID; |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 334 | fSegmentMask = 0; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 335 | fIsOval = false; |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 336 | fIsRRect = false; |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 337 | // The next two values don't matter unless fIsOval or fIsRRect are true. |
senorblanco | 9c1d45d | 2016-07-22 13:51:42 -0700 | [diff] [blame] | 338 | fRRectOrOvalIsCCW = false; |
| 339 | fRRectOrOvalStartIdx = 0xAC; |
commit-bot@chromium.org | f48e475 | 2013-06-27 18:39:39 +0000 | [diff] [blame] | 340 | SkDEBUGCODE(fEditorsAttached = 0;) |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 341 | SkDEBUGCODE(this->validate();) |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 342 | } |
| 343 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 344 | void copy(const SkPathRef& ref, int additionalReserveVerbs, int additionalReservePoints); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 345 | |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 346 | // Return true if the computed bounds are finite. |
| 347 | static bool ComputePtBounds(SkRect* bounds, const SkPathRef& ref) { |
reed | 91f283b | 2015-07-28 06:00:50 -0700 | [diff] [blame] | 348 | return bounds->setBoundsCheck(ref.points(), ref.countPoints()); |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // called, if dirty, by getBounds() |
| 352 | void computeBounds() const { |
| 353 | SkDEBUGCODE(this->validate();) |
Mike Klein | 0b4cc89 | 2014-07-16 17:18:20 -0400 | [diff] [blame] | 354 | // TODO(mtklein): remove fBoundsIsDirty and fIsFinite, |
| 355 | // using an inverted rect instead of fBoundsIsDirty and always recalculating fIsFinite. |
mtklein | 5c9c9be | 2014-12-01 06:59:54 -0800 | [diff] [blame] | 356 | SkASSERT(fBoundsIsDirty); |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 357 | |
mtklein | 5c9c9be | 2014-12-01 06:59:54 -0800 | [diff] [blame] | 358 | fIsFinite = ComputePtBounds(&fBounds, *this); |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 359 | fBoundsIsDirty = false; |
| 360 | } |
| 361 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 362 | void setBounds(const SkRect& rect) { |
| 363 | SkASSERT(rect.fLeft <= rect.fRight && rect.fTop <= rect.fBottom); |
| 364 | fBounds = rect; |
| 365 | fBoundsIsDirty = false; |
mtklein | 5c9c9be | 2014-12-01 06:59:54 -0800 | [diff] [blame] | 366 | fIsFinite = fBounds.isFinite(); |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 367 | } |
| 368 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 369 | /** Makes additional room but does not change the counts or change the genID */ |
| 370 | void incReserve(int additionalVerbs, int additionalPoints) { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 371 | SkDEBUGCODE(this->validate();) |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 372 | size_t space = additionalVerbs * sizeof(uint8_t) + additionalPoints * sizeof (SkPoint); |
| 373 | this->makeSpace(space); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 374 | SkDEBUGCODE(this->validate();) |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 375 | } |
| 376 | |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 377 | /** Resets the path ref with verbCount verbs and pointCount points, all uninitialized. Also |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 378 | * allocates space for reserveVerb additional verbs and reservePoints additional points.*/ |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 379 | void resetToSize(int verbCount, int pointCount, int conicCount, |
robertphillips@google.com | aaf3e64 | 2013-10-02 17:49:50 +0000 | [diff] [blame] | 380 | int reserveVerbs = 0, int reservePoints = 0) { |
| 381 | SkDEBUGCODE(this->validate();) |
| 382 | fBoundsIsDirty = true; // this also invalidates fIsFinite |
| 383 | fGenerationID = 0; |
| 384 | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 385 | fSegmentMask = 0; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 386 | fIsOval = false; |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 387 | fIsRRect = false; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 388 | |
robertphillips@google.com | aaf3e64 | 2013-10-02 17:49:50 +0000 | [diff] [blame] | 389 | size_t newSize = sizeof(uint8_t) * verbCount + sizeof(SkPoint) * pointCount; |
| 390 | size_t newReserve = sizeof(uint8_t) * reserveVerbs + sizeof(SkPoint) * reservePoints; |
| 391 | size_t minSize = newSize + newReserve; |
| 392 | |
| 393 | ptrdiff_t sizeDelta = this->currSize() - minSize; |
| 394 | |
| 395 | if (sizeDelta < 0 || static_cast<size_t>(sizeDelta) >= 3 * minSize) { |
| 396 | sk_free(fPoints); |
| 397 | fPoints = NULL; |
| 398 | fVerbs = NULL; |
| 399 | fFreeSpace = 0; |
| 400 | fVerbCnt = 0; |
| 401 | fPointCnt = 0; |
| 402 | this->makeSpace(minSize); |
| 403 | fVerbCnt = verbCount; |
| 404 | fPointCnt = pointCount; |
| 405 | fFreeSpace -= newSize; |
| 406 | } else { |
| 407 | fPointCnt = pointCount; |
| 408 | fVerbCnt = verbCount; |
| 409 | fFreeSpace = this->currSize() - minSize; |
| 410 | } |
| 411 | fConicWeights.setCount(conicCount); |
| 412 | SkDEBUGCODE(this->validate();) |
| 413 | } |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 414 | |
| 415 | /** |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 416 | * Increases the verb count by numVbs and point count by the required amount. |
| 417 | * The new points are uninitialized. All the new verbs are set to the specified |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 418 | * verb. If 'verb' is kConic_Verb, 'weights' will return a pointer to the |
| 419 | * uninitialized conic weights. |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 420 | */ |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 421 | SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb, int numVbs, SkScalar** weights); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 422 | |
| 423 | /** |
| 424 | * Increases the verb count 1, records the new verb, and creates room for the requisite number |
| 425 | * of additional points. A pointer to the first point is returned. Any new points are |
| 426 | * uninitialized. |
| 427 | */ |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 428 | SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight); |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 429 | |
| 430 | /** |
| 431 | * Ensures that the free space available in the path ref is >= size. The verb and point counts |
| 432 | * are not changed. |
| 433 | */ |
robertphillips@google.com | aaf3e64 | 2013-10-02 17:49:50 +0000 | [diff] [blame] | 434 | void makeSpace(size_t size) { |
| 435 | SkDEBUGCODE(this->validate();) |
Ben Wagner | ac32662 | 2017-07-31 16:57:01 -0400 | [diff] [blame] | 436 | if (size <= fFreeSpace) { |
robertphillips@google.com | aaf3e64 | 2013-10-02 17:49:50 +0000 | [diff] [blame] | 437 | return; |
| 438 | } |
Ben Wagner | ac32662 | 2017-07-31 16:57:01 -0400 | [diff] [blame] | 439 | size_t growSize = size - fFreeSpace; |
robertphillips@google.com | aaf3e64 | 2013-10-02 17:49:50 +0000 | [diff] [blame] | 440 | size_t oldSize = this->currSize(); |
| 441 | // round to next multiple of 8 bytes |
| 442 | growSize = (growSize + 7) & ~static_cast<size_t>(7); |
| 443 | // we always at least double the allocation |
Ben Wagner | ac32662 | 2017-07-31 16:57:01 -0400 | [diff] [blame] | 444 | if (growSize < oldSize) { |
robertphillips@google.com | aaf3e64 | 2013-10-02 17:49:50 +0000 | [diff] [blame] | 445 | growSize = oldSize; |
| 446 | } |
| 447 | if (growSize < kMinSize) { |
| 448 | growSize = kMinSize; |
| 449 | } |
Ben Wagner | ac32662 | 2017-07-31 16:57:01 -0400 | [diff] [blame] | 450 | constexpr size_t maxSize = std::numeric_limits<size_t>::max(); |
| 451 | size_t newSize; |
| 452 | if (growSize <= maxSize - oldSize) { |
| 453 | newSize = oldSize + growSize; |
| 454 | } else { |
| 455 | SK_ABORT("Path too big."); |
| 456 | } |
robertphillips@google.com | aaf3e64 | 2013-10-02 17:49:50 +0000 | [diff] [blame] | 457 | // Note that realloc could memcpy more than we need. It seems to be a win anyway. TODO: |
| 458 | // encapsulate this. |
| 459 | fPoints = reinterpret_cast<SkPoint*>(sk_realloc_throw(fPoints, newSize)); |
| 460 | size_t oldVerbSize = fVerbCnt * sizeof(uint8_t); |
Ben Wagner | ac32662 | 2017-07-31 16:57:01 -0400 | [diff] [blame] | 461 | void* newVerbsDst = SkTAddOffset<void>(fPoints, newSize - oldVerbSize); |
| 462 | void* oldVerbsSrc = SkTAddOffset<void>(fPoints, oldSize - oldVerbSize); |
robertphillips@google.com | aaf3e64 | 2013-10-02 17:49:50 +0000 | [diff] [blame] | 463 | memmove(newVerbsDst, oldVerbsSrc, oldVerbSize); |
Ben Wagner | ac32662 | 2017-07-31 16:57:01 -0400 | [diff] [blame] | 464 | fVerbs = SkTAddOffset<uint8_t>(fPoints, newSize); |
robertphillips@google.com | aaf3e64 | 2013-10-02 17:49:50 +0000 | [diff] [blame] | 465 | fFreeSpace += growSize; |
| 466 | SkDEBUGCODE(this->validate();) |
| 467 | } |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 468 | |
| 469 | /** |
| 470 | * Private, non-const-ptr version of the public function verbsMemBegin(). |
| 471 | */ |
| 472 | uint8_t* verbsMemWritable() { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 473 | SkDEBUGCODE(this->validate();) |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 474 | return fVerbs - fVerbCnt; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Gets the total amount of space allocated for verbs, points, and reserve. |
| 479 | */ |
| 480 | size_t currSize() const { |
| 481 | return reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints); |
| 482 | } |
| 483 | |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 484 | /** |
| 485 | * Called the first time someone calls CreateEmpty to actually create the singleton. |
| 486 | */ |
mtklein | 148ec59 | 2014-10-13 13:17:56 -0700 | [diff] [blame] | 487 | friend SkPathRef* sk_create_empty_pathref(); |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 488 | |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 489 | void setIsOval(bool isOval, bool isCCW, unsigned start) { |
| 490 | fIsOval = isOval; |
| 491 | fRRectOrOvalIsCCW = isCCW; |
| 492 | fRRectOrOvalStartIdx = start; |
| 493 | } |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 494 | |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 495 | void setIsRRect(bool isRRect, bool isCCW, unsigned start) { |
| 496 | fIsRRect = isRRect; |
| 497 | fRRectOrOvalIsCCW = isCCW; |
| 498 | fRRectOrOvalStartIdx = start; |
| 499 | } |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 500 | |
| 501 | // called only by the editor. Note that this is not a const function. |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 502 | SkPoint* getPoints() { |
| 503 | SkDEBUGCODE(this->validate();) |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 504 | fIsOval = false; |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 505 | fIsRRect = false; |
| 506 | return fPoints; |
| 507 | } |
| 508 | |
| 509 | const SkPoint* getPoints() const { |
| 510 | SkDEBUGCODE(this->validate();) |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 511 | return fPoints; |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 512 | } |
| 513 | |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 514 | void callGenIDChangeListeners(); |
| 515 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 516 | enum { |
| 517 | kMinSize = 256, |
| 518 | }; |
| 519 | |
mtklein | 5c9c9be | 2014-12-01 06:59:54 -0800 | [diff] [blame] | 520 | mutable SkRect fBounds; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 521 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 522 | SkPoint* fPoints; // points to begining of the allocation |
| 523 | uint8_t* fVerbs; // points just past the end of the allocation (verbs grow backwards) |
| 524 | int fVerbCnt; |
| 525 | int fPointCnt; |
| 526 | size_t fFreeSpace; // redundant but saves computation |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 527 | SkTDArray<SkScalar> fConicWeights; |
| 528 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 529 | enum { |
| 530 | kEmptyGenID = 1, // GenID reserved for path ref with zero points and zero verbs. |
| 531 | }; |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 532 | mutable uint32_t fGenerationID; |
commit-bot@chromium.org | f48e475 | 2013-06-27 18:39:39 +0000 | [diff] [blame] | 533 | SkDEBUGCODE(int32_t fEditorsAttached;) // assert that only one editor in use at any time. |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 534 | |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 535 | SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owned |
| 536 | |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 537 | mutable uint8_t fBoundsIsDirty; |
| 538 | mutable SkBool8 fIsFinite; // only meaningful if bounds are valid |
| 539 | |
| 540 | SkBool8 fIsOval; |
| 541 | SkBool8 fIsRRect; |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 542 | // Both the circle and rrect special cases have a notion of direction and starting point |
| 543 | // The next two variables store that information for either. |
| 544 | SkBool8 fRRectOrOvalIsCCW; |
| 545 | uint8_t fRRectOrOvalStartIdx; |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 546 | uint8_t fSegmentMask; |
| 547 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 548 | friend class PathRefTest_Private; |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 549 | friend class ForceIsRRect_Private; // unit test isRRect |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 550 | }; |
| 551 | |
bsalomon@google.com | 1dfe88e | 2012-10-03 13:46:20 +0000 | [diff] [blame] | 552 | #endif |