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