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