blob: 5a5a04118c79439adaedffea67a54a47e78b0c03 [file] [log] [blame]
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkPathRef_DEFINED
9#define SkPathRef_DEFINED
10
Hal Canaryc640d0d2018-06-13 09:59:02 -040011#include "SkAtomics.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000012#include "SkMatrix.h"
13#include "SkPoint.h"
caryclarkda707bf2015-11-19 14:47:43 -080014#include "SkRRect.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000015#include "SkRect.h"
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000016#include "SkRefCnt.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040017#include "SkTDArray.h"
Ben Wagnerac326622017-07-31 16:57:01 -040018#include "SkTemplates.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040019#include "SkTo.h"
20
Mike Kleinbf45c702018-06-11 11:56:57 -040021#include <limits>
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000022
robertphillips@google.comca0c8382013-09-26 12:18:23 +000023class SkRBuffer;
24class SkWBuffer;
25
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000026/**
27 * Holds the path verbs and points. It is versioned by a generation ID. None of its public methods
28 * modify the contents. To modify or append to the verbs/points wrap the SkPathRef in an
29 * SkPathRef::Editor object. Installing the editor resets the generation ID. It also performs
robertphillips@google.com466310d2013-12-03 16:43:54 +000030 * copy-on-write if the SkPathRef is shared by multiple SkPaths. The caller passes the Editor's
bungeman6bd52842016-10-27 09:30:08 -070031 * constructor a pointer to a sk_sp<SkPathRef>, which may be updated to point to a new SkPathRef
32 * after the editor's constructor returns.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000033 *
34 * The points and verbs are stored in a single allocation. The points are at the begining of the
35 * allocation while the verbs are stored at end of the allocation, in reverse order. Thus the points
36 * and verbs both grow into the middle of the allocation until the meet. To access verb i in the
37 * verb array use ref.verbs()[~i] (because verbs() returns a pointer just beyond the first
38 * logical verb or the last verb in memory).
39 */
bsalomon@google.comae09f2d2012-10-03 19:57:01 +000040
mtkleinb47cd4b2016-08-09 12:20:04 -070041class SK_API SkPathRef final : public SkNVRefCnt<SkPathRef> {
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000042public:
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000043 class Editor {
44 public:
bungeman6bd52842016-10-27 09:30:08 -070045 Editor(sk_sp<SkPathRef>* pathRef,
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000046 int incReserveVerbs = 0,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000047 int incReservePoints = 0);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000048
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +000049 ~Editor() { SkDEBUGCODE(sk_atomic_dec(&fPathRef->fEditorsAttached);) }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000050
51 /**
52 * Returns the array of points.
53 */
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000054 SkPoint* points() { return fPathRef->getPoints(); }
55 const SkPoint* points() const { return fPathRef->points(); }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000056
57 /**
58 * Gets the ith point. Shortcut for this->points() + i
59 */
60 SkPoint* atPoint(int i) {
61 SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
62 return this->points() + i;
Mike Kleinfc6c37b2016-09-27 09:34:10 -040063 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000064 const SkPoint* atPoint(int i) const {
65 SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
66 return this->points() + i;
Mike Kleinfc6c37b2016-09-27 09:34:10 -040067 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000068
69 /**
70 * Adds the verb and allocates space for the number of points indicated by the verb. The
71 * return value is a pointer to where the points for the verb should be written.
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000072 * 'weight' is only used if 'verb' is kConic_Verb
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000073 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000074 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight = 0) {
robertphillips@google.com03087072013-10-02 16:42:21 +000075 SkDEBUGCODE(fPathRef->validate();)
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000076 return fPathRef->growForVerb(verb, weight);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000077 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000078
79 /**
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000080 * Allocates space for multiple instances of a particular verb and the
81 * requisite points & weights.
82 * The return pointer points at the first new point (indexed normally [<i>]).
83 * If 'verb' is kConic_Verb, 'weights' will return a pointer to the
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000084 * space for the conic weights (indexed normally).
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000085 */
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000086 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb,
87 int numVbs,
Ben Wagnera93a14a2017-08-28 10:34:05 -040088 SkScalar** weights = nullptr) {
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000089 return fPathRef->growForRepeatedVerb(verb, numVbs, weights);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000090 }
91
92 /**
93 * Resets the path ref to a new verb and point count. The new verbs and points are
94 * uninitialized.
95 */
reed@google.com277c3f82013-05-31 15:17:50 +000096 void resetToSize(int newVerbCnt, int newPointCnt, int newConicCount) {
97 fPathRef->resetToSize(newVerbCnt, newPointCnt, newConicCount);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000098 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000099
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000100 /**
101 * Gets the path ref that is wrapped in the Editor.
102 */
103 SkPathRef* pathRef() { return fPathRef; }
104
bsalomon78d58d12016-05-27 09:17:04 -0700105 void setIsOval(bool isOval, bool isCCW, unsigned start) {
106 fPathRef->setIsOval(isOval, isCCW, start);
107 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000108
bsalomon78d58d12016-05-27 09:17:04 -0700109 void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
110 fPathRef->setIsRRect(isRRect, isCCW, start);
111 }
caryclarkda707bf2015-11-19 14:47:43 -0800112
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000113 void setBounds(const SkRect& rect) { fPathRef->setBounds(rect); }
114
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000115 private:
116 SkPathRef* fPathRef;
117 };
118
caryclarkda707bf2015-11-19 14:47:43 -0800119 class SK_API Iter {
120 public:
121 Iter();
122 Iter(const SkPathRef&);
123
124 void setPathRef(const SkPathRef&);
125
126 /** Return the next verb in this iteration of the path. When all
127 segments have been visited, return kDone_Verb.
128
Mike Klein1170a552017-09-08 15:00:25 -0400129 If any point in the path is non-finite, return kDone_Verb immediately.
130
caryclarkda707bf2015-11-19 14:47:43 -0800131 @param pts The points representing the current verb and/or segment
132 This must not be NULL.
133 @return The verb for the current segment
134 */
135 uint8_t next(SkPoint pts[4]);
caryclark2028d7f2015-12-09 14:04:46 -0800136 uint8_t peek() const;
caryclarkda707bf2015-11-19 14:47:43 -0800137
138 SkScalar conicWeight() const { return *fConicWeights; }
139
140 private:
141 const SkPoint* fPts;
142 const uint8_t* fVerbs;
143 const uint8_t* fVerbStop;
144 const SkScalar* fConicWeights;
145 };
146
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000147public:
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000148 /**
149 * Gets a path ref with no verbs or points.
150 */
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000151 static SkPathRef* CreateEmpty();
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000152
153 /**
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000154 * Returns true if all of the points in this path are finite, meaning there
155 * are no infinities and no NaNs.
156 */
157 bool isFinite() const {
158 if (fBoundsIsDirty) {
159 this->computeBounds();
160 }
161 return SkToBool(fIsFinite);
162 }
163
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000164 /**
165 * Returns a mask, where each bit corresponding to a SegmentMask is
166 * set if the path contains 1 or more segments of that type.
167 * Returns 0 for an empty path (no segments).
168 */
169 uint32_t getSegmentMasks() const { return fSegmentMask; }
170
robertphillips@google.com466310d2013-12-03 16:43:54 +0000171 /** Returns true if the path is an oval.
172 *
173 * @param rect returns the bounding rect of this oval. It's a circle
174 * if the height and width are the same.
bsalomon78d58d12016-05-27 09:17:04 -0700175 * @param isCCW is the oval CCW (or CW if false).
176 * @param start indicates where the contour starts on the oval (see
177 * SkPath::addOval for intepretation of the index).
robertphillips@google.com466310d2013-12-03 16:43:54 +0000178 *
179 * @return true if this path is an oval.
180 * Tracking whether a path is an oval is considered an
181 * optimization for performance and so some paths that are in
182 * fact ovals can report false.
183 */
bsalomon78d58d12016-05-27 09:17:04 -0700184 bool isOval(SkRect* rect, bool* isCCW, unsigned* start) const {
185 if (fIsOval) {
186 if (rect) {
187 *rect = this->getBounds();
188 }
189 if (isCCW) {
190 *isCCW = SkToBool(fRRectOrOvalIsCCW);
191 }
192 if (start) {
193 *start = fRRectOrOvalStartIdx;
194 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000195 }
196
197 return SkToBool(fIsOval);
198 }
199
bsalomon78d58d12016-05-27 09:17:04 -0700200 bool isRRect(SkRRect* rrect, bool* isCCW, unsigned* start) const {
201 if (fIsRRect) {
202 if (rrect) {
203 *rrect = this->getRRect();
204 }
205 if (isCCW) {
206 *isCCW = SkToBool(fRRectOrOvalIsCCW);
207 }
208 if (start) {
209 *start = fRRectOrOvalStartIdx;
210 }
caryclarkda707bf2015-11-19 14:47:43 -0800211 }
212 return SkToBool(fIsRRect);
213 }
214
215
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000216 bool hasComputedBounds() const {
217 return !fBoundsIsDirty;
218 }
219
220 /** Returns the bounds of the path's points. If the path contains 0 or 1
221 points, the bounds is set to (0,0,0,0), and isEmpty() will return true.
222 Note: this bounds may be larger than the actual shape, since curves
223 do not extend as far as their control points.
224 */
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +0000225 const SkRect& getBounds() const {
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000226 if (fBoundsIsDirty) {
227 this->computeBounds();
228 }
229 return fBounds;
230 }
231
caryclarkda707bf2015-11-19 14:47:43 -0800232 SkRRect getRRect() const;
233
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000234 /**
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000235 * Transforms a path ref by a matrix, allocating a new one only if necessary.
236 */
bungeman6bd52842016-10-27 09:30:08 -0700237 static void CreateTransformedCopy(sk_sp<SkPathRef>* dst,
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000238 const SkPathRef& src,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000239 const SkMatrix& matrix);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000240
commit-bot@chromium.orgfed2ab62014-01-23 15:16:05 +0000241 static SkPathRef* CreateFromBuffer(SkRBuffer* buffer);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000242
243 /**
244 * Rollsback a path ref to zero verbs and points with the assumption that the path ref will be
245 * repopulated with approximately the same number of verbs and points. A new path ref is created
246 * only if necessary.
247 */
bungeman6bd52842016-10-27 09:30:08 -0700248 static void Rewind(sk_sp<SkPathRef>* pathRef);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000249
mtkleinb47cd4b2016-08-09 12:20:04 -0700250 ~SkPathRef();
Brian Osman03776fc2017-08-15 16:08:48 -0400251 int countPoints() const { return fPointCnt; }
252 int countVerbs() const { return fVerbCnt; }
253 int countWeights() const { return fConicWeights.count(); }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000254
255 /**
256 * Returns a pointer one beyond the first logical verb (last verb in memory order).
257 */
Brian Osman03776fc2017-08-15 16:08:48 -0400258 const uint8_t* verbs() const { return fVerbs; }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000259
260 /**
261 * Returns a const pointer to the first verb in memory (which is the last logical verb).
262 */
263 const uint8_t* verbsMemBegin() const { return this->verbs() - fVerbCnt; }
264
265 /**
266 * Returns a const pointer to the first point.
267 */
Brian Osman03776fc2017-08-15 16:08:48 -0400268 const SkPoint* points() const { return fPoints; }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000269
270 /**
271 * Shortcut for this->points() + this->countPoints()
272 */
273 const SkPoint* pointsEnd() const { return this->points() + this->countPoints(); }
274
Brian Osman03776fc2017-08-15 16:08:48 -0400275 const SkScalar* conicWeights() const { return fConicWeights.begin(); }
276 const SkScalar* conicWeightsEnd() const { return fConicWeights.end(); }
reed@google.com277c3f82013-05-31 15:17:50 +0000277
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000278 /**
279 * Convenience methods for getting to a verb or point by index.
280 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000281 uint8_t atVerb(int index) const {
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000282 SkASSERT((unsigned) index < (unsigned) fVerbCnt);
283 return this->verbs()[~index];
284 }
285 const SkPoint& atPoint(int index) const {
286 SkASSERT((unsigned) index < (unsigned) fPointCnt);
287 return this->points()[index];
288 }
289
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000290 bool operator== (const SkPathRef& ref) const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000291
292 /**
293 * Writes the path points and verbs to a buffer.
294 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000295 void writeToBuffer(SkWBuffer* buffer) const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000296
297 /**
298 * Gets the number of bytes that would be written in writeBuffer()
299 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000300 uint32_t writeSize() const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000301
caryclark8e7b19d2016-02-18 04:11:48 -0800302 void interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef* out) const;
303
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000304 /**
305 * Gets an ID that uniquely identifies the contents of the path ref. If two path refs have the
306 * same ID then they have the same verbs and points. However, two path refs may have the same
307 * contents but different genIDs.
308 */
309 uint32_t genID() const;
310
Chris Daltonafa11582018-06-08 12:00:44 -0600311 struct GenIDChangeListener : SkRefCnt {
senorblanco84cd6212015-08-04 10:01:58 -0700312 virtual ~GenIDChangeListener() {}
313 virtual void onChange() = 0;
314 };
315
Chris Daltonafa11582018-06-08 12:00:44 -0600316 void addGenIDChangeListener(sk_sp<GenIDChangeListener>);
senorblanco84cd6212015-08-04 10:01:58 -0700317
Adrienne Walkerad8da8e2017-08-10 12:16:37 -0700318 bool isValid() const;
319 SkDEBUGCODE(void validate() const { SkASSERT(this->isValid()); } )
reed5bcbe912014-12-15 12:28:33 -0800320
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000321private:
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000322 enum SerializationOffsets {
Brian Salomonb7d42e32017-09-21 16:59:17 -0400323 kLegacyRRectOrOvalStartIdx_SerializationShift = 28, // requires 3 bits, ignored.
324 kLegacyRRectOrOvalIsCCW_SerializationShift = 27, // requires 1 bit, ignored.
325 kLegacyIsRRect_SerializationShift = 26, // requires 1 bit, ignored.
326 kIsFinite_SerializationShift = 25, // requires 1 bit
327 kLegacyIsOval_SerializationShift = 24, // requires 1 bit, ignored.
Mike Reeda3d9e212018-02-20 09:48:13 -0500328 kSegmentMask_SerializationShift = 0 // requires 4 bits (deprecated)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000329 };
330
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000331 SkPathRef() {
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000332 fBoundsIsDirty = true; // this also invalidates fIsFinite
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000333 fPointCnt = 0;
334 fVerbCnt = 0;
Ben Wagnera93a14a2017-08-28 10:34:05 -0400335 fVerbs = nullptr;
336 fPoints = nullptr;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000337 fFreeSpace = 0;
338 fGenerationID = kEmptyGenID;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000339 fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000340 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800341 fIsRRect = false;
bsalomon78d58d12016-05-27 09:17:04 -0700342 // The next two values don't matter unless fIsOval or fIsRRect are true.
senorblanco9c1d45d2016-07-22 13:51:42 -0700343 fRRectOrOvalIsCCW = false;
344 fRRectOrOvalStartIdx = 0xAC;
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +0000345 SkDEBUGCODE(fEditorsAttached = 0;)
robertphillips@google.com03087072013-10-02 16:42:21 +0000346 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000347 }
348
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000349 void copy(const SkPathRef& ref, int additionalReserveVerbs, int additionalReservePoints);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000350
Mike Reeda3d9e212018-02-20 09:48:13 -0500351 // Doesn't read fSegmentMask, but (re)computes it from the verbs array
352 unsigned computeSegmentMask() const;
353
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000354 // Return true if the computed bounds are finite.
355 static bool ComputePtBounds(SkRect* bounds, const SkPathRef& ref) {
reed91f283b2015-07-28 06:00:50 -0700356 return bounds->setBoundsCheck(ref.points(), ref.countPoints());
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000357 }
358
359 // called, if dirty, by getBounds()
360 void computeBounds() const {
361 SkDEBUGCODE(this->validate();)
Mike Klein0b4cc892014-07-16 17:18:20 -0400362 // TODO(mtklein): remove fBoundsIsDirty and fIsFinite,
363 // using an inverted rect instead of fBoundsIsDirty and always recalculating fIsFinite.
mtklein5c9c9be2014-12-01 06:59:54 -0800364 SkASSERT(fBoundsIsDirty);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000365
mtklein5c9c9be2014-12-01 06:59:54 -0800366 fIsFinite = ComputePtBounds(&fBounds, *this);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000367 fBoundsIsDirty = false;
368 }
369
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000370 void setBounds(const SkRect& rect) {
371 SkASSERT(rect.fLeft <= rect.fRight && rect.fTop <= rect.fBottom);
372 fBounds = rect;
373 fBoundsIsDirty = false;
mtklein5c9c9be2014-12-01 06:59:54 -0800374 fIsFinite = fBounds.isFinite();
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000375 }
376
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000377 /** Makes additional room but does not change the counts or change the genID */
378 void incReserve(int additionalVerbs, int additionalPoints) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000379 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000380 size_t space = additionalVerbs * sizeof(uint8_t) + additionalPoints * sizeof (SkPoint);
381 this->makeSpace(space);
robertphillips@google.com03087072013-10-02 16:42:21 +0000382 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000383 }
384
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000385 /** Resets the path ref with verbCount verbs and pointCount points, all uninitialized. Also
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000386 * allocates space for reserveVerb additional verbs and reservePoints additional points.*/
reed@google.com277c3f82013-05-31 15:17:50 +0000387 void resetToSize(int verbCount, int pointCount, int conicCount,
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000388 int reserveVerbs = 0, int reservePoints = 0) {
389 SkDEBUGCODE(this->validate();)
390 fBoundsIsDirty = true; // this also invalidates fIsFinite
391 fGenerationID = 0;
392
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000393 fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000394 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800395 fIsRRect = false;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000396
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000397 size_t newSize = sizeof(uint8_t) * verbCount + sizeof(SkPoint) * pointCount;
398 size_t newReserve = sizeof(uint8_t) * reserveVerbs + sizeof(SkPoint) * reservePoints;
399 size_t minSize = newSize + newReserve;
400
401 ptrdiff_t sizeDelta = this->currSize() - minSize;
402
403 if (sizeDelta < 0 || static_cast<size_t>(sizeDelta) >= 3 * minSize) {
404 sk_free(fPoints);
Ben Wagnera93a14a2017-08-28 10:34:05 -0400405 fPoints = nullptr;
406 fVerbs = nullptr;
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000407 fFreeSpace = 0;
408 fVerbCnt = 0;
409 fPointCnt = 0;
410 this->makeSpace(minSize);
411 fVerbCnt = verbCount;
412 fPointCnt = pointCount;
413 fFreeSpace -= newSize;
414 } else {
415 fPointCnt = pointCount;
416 fVerbCnt = verbCount;
417 fFreeSpace = this->currSize() - minSize;
418 }
419 fConicWeights.setCount(conicCount);
420 SkDEBUGCODE(this->validate();)
421 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000422
423 /**
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000424 * Increases the verb count by numVbs and point count by the required amount.
425 * The new points are uninitialized. All the new verbs are set to the specified
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000426 * verb. If 'verb' is kConic_Verb, 'weights' will return a pointer to the
427 * uninitialized conic weights.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000428 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000429 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb, int numVbs, SkScalar** weights);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000430
431 /**
432 * Increases the verb count 1, records the new verb, and creates room for the requisite number
433 * of additional points. A pointer to the first point is returned. Any new points are
434 * uninitialized.
435 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000436 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000437
438 /**
439 * Ensures that the free space available in the path ref is >= size. The verb and point counts
440 * are not changed.
441 */
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000442 void makeSpace(size_t size) {
443 SkDEBUGCODE(this->validate();)
Ben Wagnerac326622017-07-31 16:57:01 -0400444 if (size <= fFreeSpace) {
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000445 return;
446 }
Ben Wagnerac326622017-07-31 16:57:01 -0400447 size_t growSize = size - fFreeSpace;
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000448 size_t oldSize = this->currSize();
449 // round to next multiple of 8 bytes
450 growSize = (growSize + 7) & ~static_cast<size_t>(7);
451 // we always at least double the allocation
Ben Wagnerac326622017-07-31 16:57:01 -0400452 if (growSize < oldSize) {
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000453 growSize = oldSize;
454 }
455 if (growSize < kMinSize) {
456 growSize = kMinSize;
457 }
Ben Wagnerac326622017-07-31 16:57:01 -0400458 constexpr size_t maxSize = std::numeric_limits<size_t>::max();
459 size_t newSize;
460 if (growSize <= maxSize - oldSize) {
461 newSize = oldSize + growSize;
462 } else {
463 SK_ABORT("Path too big.");
464 }
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000465 // Note that realloc could memcpy more than we need. It seems to be a win anyway. TODO:
466 // encapsulate this.
467 fPoints = reinterpret_cast<SkPoint*>(sk_realloc_throw(fPoints, newSize));
468 size_t oldVerbSize = fVerbCnt * sizeof(uint8_t);
Ben Wagnerac326622017-07-31 16:57:01 -0400469 void* newVerbsDst = SkTAddOffset<void>(fPoints, newSize - oldVerbSize);
470 void* oldVerbsSrc = SkTAddOffset<void>(fPoints, oldSize - oldVerbSize);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000471 memmove(newVerbsDst, oldVerbsSrc, oldVerbSize);
Ben Wagnerac326622017-07-31 16:57:01 -0400472 fVerbs = SkTAddOffset<uint8_t>(fPoints, newSize);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000473 fFreeSpace += growSize;
474 SkDEBUGCODE(this->validate();)
475 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000476
477 /**
478 * Private, non-const-ptr version of the public function verbsMemBegin().
479 */
480 uint8_t* verbsMemWritable() {
robertphillips@google.com03087072013-10-02 16:42:21 +0000481 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000482 return fVerbs - fVerbCnt;
483 }
484
485 /**
486 * Gets the total amount of space allocated for verbs, points, and reserve.
487 */
488 size_t currSize() const {
489 return reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints);
490 }
491
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000492 /**
493 * Called the first time someone calls CreateEmpty to actually create the singleton.
494 */
mtklein148ec592014-10-13 13:17:56 -0700495 friend SkPathRef* sk_create_empty_pathref();
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000496
bsalomon78d58d12016-05-27 09:17:04 -0700497 void setIsOval(bool isOval, bool isCCW, unsigned start) {
498 fIsOval = isOval;
499 fRRectOrOvalIsCCW = isCCW;
Kaloyan Donevdfffb392018-03-20 10:38:31 +0200500 fRRectOrOvalStartIdx = SkToU8(start);
bsalomon78d58d12016-05-27 09:17:04 -0700501 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000502
bsalomon78d58d12016-05-27 09:17:04 -0700503 void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
504 fIsRRect = isRRect;
505 fRRectOrOvalIsCCW = isCCW;
Kaloyan Donevdfffb392018-03-20 10:38:31 +0200506 fRRectOrOvalStartIdx = SkToU8(start);
bsalomon78d58d12016-05-27 09:17:04 -0700507 }
caryclarkda707bf2015-11-19 14:47:43 -0800508
509 // called only by the editor. Note that this is not a const function.
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000510 SkPoint* getPoints() {
511 SkDEBUGCODE(this->validate();)
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000512 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800513 fIsRRect = false;
514 return fPoints;
515 }
516
517 const SkPoint* getPoints() const {
518 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000519 return fPoints;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000520 }
521
senorblanco84cd6212015-08-04 10:01:58 -0700522 void callGenIDChangeListeners();
523
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000524 enum {
525 kMinSize = 256,
526 };
527
mtklein5c9c9be2014-12-01 06:59:54 -0800528 mutable SkRect fBounds;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000529
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000530 SkPoint* fPoints; // points to begining of the allocation
531 uint8_t* fVerbs; // points just past the end of the allocation (verbs grow backwards)
532 int fVerbCnt;
533 int fPointCnt;
534 size_t fFreeSpace; // redundant but saves computation
reed@google.com277c3f82013-05-31 15:17:50 +0000535 SkTDArray<SkScalar> fConicWeights;
536
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000537 enum {
538 kEmptyGenID = 1, // GenID reserved for path ref with zero points and zero verbs.
539 };
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000540 mutable uint32_t fGenerationID;
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +0000541 SkDEBUGCODE(int32_t fEditorsAttached;) // assert that only one editor in use at any time.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000542
Chris Daltonafa11582018-06-08 12:00:44 -0600543 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are reffed
senorblanco84cd6212015-08-04 10:01:58 -0700544
caryclarkda707bf2015-11-19 14:47:43 -0800545 mutable uint8_t fBoundsIsDirty;
Ben Wagnercee46e52018-06-12 16:30:29 -0400546 mutable bool fIsFinite; // only meaningful if bounds are valid
caryclarkda707bf2015-11-19 14:47:43 -0800547
Ben Wagnercee46e52018-06-12 16:30:29 -0400548 bool fIsOval;
549 bool fIsRRect;
bsalomon78d58d12016-05-27 09:17:04 -0700550 // Both the circle and rrect special cases have a notion of direction and starting point
551 // The next two variables store that information for either.
Ben Wagnercee46e52018-06-12 16:30:29 -0400552 bool fRRectOrOvalIsCCW;
bsalomon78d58d12016-05-27 09:17:04 -0700553 uint8_t fRRectOrOvalStartIdx;
caryclarkda707bf2015-11-19 14:47:43 -0800554 uint8_t fSegmentMask;
555
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000556 friend class PathRefTest_Private;
caryclarkda707bf2015-11-19 14:47:43 -0800557 friend class ForceIsRRect_Private; // unit test isRRect
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000558};
559
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000560#endif