blob: b9b52d41c8951d8fa04c53662fca94a561e5ffab [file] [log] [blame]
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +00001
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
bungeman2c4bd072016-04-08 06:58:51 -070012#include "../private/SkAtomics.h"
bungemana7e9f052016-02-18 08:53:33 -080013#include "../private/SkTDArray.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000014#include "SkMatrix.h"
15#include "SkPoint.h"
caryclarkda707bf2015-11-19 14:47:43 -080016#include "SkRRect.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000017#include "SkRect.h"
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000018#include "SkRefCnt.h"
Ben Wagnerac326622017-07-31 16:57:01 -040019#include "SkTemplates.h"
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000020
robertphillips@google.comca0c8382013-09-26 12:18:23 +000021class SkRBuffer;
22class SkWBuffer;
23
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000024/**
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.com466310d2013-12-03 16:43:54 +000028 * copy-on-write if the SkPathRef is shared by multiple SkPaths. The caller passes the Editor's
bungeman6bd52842016-10-27 09:30:08 -070029 * 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.com1dfe88e2012-10-03 13:46:20 +000031 *
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.comae09f2d2012-10-03 19:57:01 +000038
mtkleinb47cd4b2016-08-09 12:20:04 -070039class SK_API SkPathRef final : public SkNVRefCnt<SkPathRef> {
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000040public:
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000041 class Editor {
42 public:
bungeman6bd52842016-10-27 09:30:08 -070043 Editor(sk_sp<SkPathRef>* pathRef,
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000044 int incReserveVerbs = 0,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000045 int incReservePoints = 0);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000046
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +000047 ~Editor() { SkDEBUGCODE(sk_atomic_dec(&fPathRef->fEditorsAttached);) }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000048
49 /**
50 * Returns the array of points.
51 */
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000052 SkPoint* points() { return fPathRef->getPoints(); }
53 const SkPoint* points() const { return fPathRef->points(); }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000054
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 Kleinfc6c37b2016-09-27 09:34:10 -040061 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000062 const SkPoint* atPoint(int i) const {
63 SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
64 return this->points() + i;
Mike Kleinfc6c37b2016-09-27 09:34:10 -040065 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000066
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.com6b8dbb62013-12-12 23:03:51 +000070 * 'weight' is only used if 'verb' is kConic_Verb
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000071 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000072 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight = 0) {
robertphillips@google.com03087072013-10-02 16:42:21 +000073 SkDEBUGCODE(fPathRef->validate();)
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000074 return fPathRef->growForVerb(verb, weight);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000075 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000076
77 /**
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000078 * 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.com96f5fa02013-12-16 07:01:40 +000082 * space for the conic weights (indexed normally).
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000083 */
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000084 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb,
85 int numVbs,
Ben Wagnera93a14a2017-08-28 10:34:05 -040086 SkScalar** weights = nullptr) {
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000087 return fPathRef->growForRepeatedVerb(verb, numVbs, weights);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000088 }
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.com277c3f82013-05-31 15:17:50 +000094 void resetToSize(int newVerbCnt, int newPointCnt, int newConicCount) {
95 fPathRef->resetToSize(newVerbCnt, newPointCnt, newConicCount);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000096 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000097
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000098 /**
99 * Gets the path ref that is wrapped in the Editor.
100 */
101 SkPathRef* pathRef() { return fPathRef; }
102
bsalomon78d58d12016-05-27 09:17:04 -0700103 void setIsOval(bool isOval, bool isCCW, unsigned start) {
104 fPathRef->setIsOval(isOval, isCCW, start);
105 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000106
bsalomon78d58d12016-05-27 09:17:04 -0700107 void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
108 fPathRef->setIsRRect(isRRect, isCCW, start);
109 }
caryclarkda707bf2015-11-19 14:47:43 -0800110
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000111 void setBounds(const SkRect& rect) { fPathRef->setBounds(rect); }
112
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000113 private:
114 SkPathRef* fPathRef;
115 };
116
caryclarkda707bf2015-11-19 14:47:43 -0800117 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 Klein1170a552017-09-08 15:00:25 -0400127 If any point in the path is non-finite, return kDone_Verb immediately.
128
caryclarkda707bf2015-11-19 14:47:43 -0800129 @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]);
caryclark2028d7f2015-12-09 14:04:46 -0800134 uint8_t peek() const;
caryclarkda707bf2015-11-19 14:47:43 -0800135
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.com1dfe88e2012-10-03 13:46:20 +0000145public:
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000146 /**
147 * Gets a path ref with no verbs or points.
148 */
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000149 static SkPathRef* CreateEmpty();
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000150
151 /**
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000152 * 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.com6b8dbb62013-12-12 23:03:51 +0000162 /**
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.com466310d2013-12-03 16:43:54 +0000169 /** 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.
bsalomon78d58d12016-05-27 09:17:04 -0700173 * @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.com466310d2013-12-03 16:43:54 +0000176 *
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 */
bsalomon78d58d12016-05-27 09:17:04 -0700182 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.com466310d2013-12-03 16:43:54 +0000193 }
194
195 return SkToBool(fIsOval);
196 }
197
bsalomon78d58d12016-05-27 09:17:04 -0700198 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 }
caryclarkda707bf2015-11-19 14:47:43 -0800209 }
210 return SkToBool(fIsRRect);
211 }
212
213
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000214 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.com65caeaf2013-09-27 07:01:29 +0000223 const SkRect& getBounds() const {
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000224 if (fBoundsIsDirty) {
225 this->computeBounds();
226 }
227 return fBounds;
228 }
229
caryclarkda707bf2015-11-19 14:47:43 -0800230 SkRRect getRRect() const;
231
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000232 /**
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000233 * Transforms a path ref by a matrix, allocating a new one only if necessary.
234 */
bungeman6bd52842016-10-27 09:30:08 -0700235 static void CreateTransformedCopy(sk_sp<SkPathRef>* dst,
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000236 const SkPathRef& src,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000237 const SkMatrix& matrix);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000238
commit-bot@chromium.orgfed2ab62014-01-23 15:16:05 +0000239 static SkPathRef* CreateFromBuffer(SkRBuffer* buffer);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000240
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 */
bungeman6bd52842016-10-27 09:30:08 -0700246 static void Rewind(sk_sp<SkPathRef>* pathRef);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000247
mtkleinb47cd4b2016-08-09 12:20:04 -0700248 ~SkPathRef();
Brian Osman03776fc2017-08-15 16:08:48 -0400249 int countPoints() const { return fPointCnt; }
250 int countVerbs() const { return fVerbCnt; }
251 int countWeights() const { return fConicWeights.count(); }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000252
253 /**
254 * Returns a pointer one beyond the first logical verb (last verb in memory order).
255 */
Brian Osman03776fc2017-08-15 16:08:48 -0400256 const uint8_t* verbs() const { return fVerbs; }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000257
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 Osman03776fc2017-08-15 16:08:48 -0400266 const SkPoint* points() const { return fPoints; }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000267
268 /**
269 * Shortcut for this->points() + this->countPoints()
270 */
271 const SkPoint* pointsEnd() const { return this->points() + this->countPoints(); }
272
Brian Osman03776fc2017-08-15 16:08:48 -0400273 const SkScalar* conicWeights() const { return fConicWeights.begin(); }
274 const SkScalar* conicWeightsEnd() const { return fConicWeights.end(); }
reed@google.com277c3f82013-05-31 15:17:50 +0000275
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000276 /**
277 * Convenience methods for getting to a verb or point by index.
278 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000279 uint8_t atVerb(int index) const {
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000280 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.com3e292aa2013-09-27 17:48:49 +0000288 bool operator== (const SkPathRef& ref) const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000289
290 /**
291 * Writes the path points and verbs to a buffer.
292 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000293 void writeToBuffer(SkWBuffer* buffer) const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000294
295 /**
296 * Gets the number of bytes that would be written in writeBuffer()
297 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000298 uint32_t writeSize() const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000299
caryclark8e7b19d2016-02-18 04:11:48 -0800300 void interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef* out) const;
301
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000302 /**
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
senorblanco84cd6212015-08-04 10:01:58 -0700309 struct GenIDChangeListener {
310 virtual ~GenIDChangeListener() {}
311 virtual void onChange() = 0;
312 };
313
314 void addGenIDChangeListener(GenIDChangeListener* listener);
315
Adrienne Walkerad8da8e2017-08-10 12:16:37 -0700316 bool isValid() const;
317 SkDEBUGCODE(void validate() const { SkASSERT(this->isValid()); } )
reed5bcbe912014-12-15 12:28:33 -0800318
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000319private:
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000320 enum SerializationOffsets {
Brian Salomonb7d42e32017-09-21 16:59:17 -0400321 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 Reeda3d9e212018-02-20 09:48:13 -0500326 kSegmentMask_SerializationShift = 0 // requires 4 bits (deprecated)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000327 };
328
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000329 SkPathRef() {
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000330 fBoundsIsDirty = true; // this also invalidates fIsFinite
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000331 fPointCnt = 0;
332 fVerbCnt = 0;
Ben Wagnera93a14a2017-08-28 10:34:05 -0400333 fVerbs = nullptr;
334 fPoints = nullptr;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000335 fFreeSpace = 0;
336 fGenerationID = kEmptyGenID;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000337 fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000338 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800339 fIsRRect = false;
bsalomon78d58d12016-05-27 09:17:04 -0700340 // The next two values don't matter unless fIsOval or fIsRRect are true.
senorblanco9c1d45d2016-07-22 13:51:42 -0700341 fRRectOrOvalIsCCW = false;
342 fRRectOrOvalStartIdx = 0xAC;
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +0000343 SkDEBUGCODE(fEditorsAttached = 0;)
robertphillips@google.com03087072013-10-02 16:42:21 +0000344 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000345 }
346
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000347 void copy(const SkPathRef& ref, int additionalReserveVerbs, int additionalReservePoints);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000348
Mike Reeda3d9e212018-02-20 09:48:13 -0500349 // Doesn't read fSegmentMask, but (re)computes it from the verbs array
350 unsigned computeSegmentMask() const;
351
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000352 // Return true if the computed bounds are finite.
353 static bool ComputePtBounds(SkRect* bounds, const SkPathRef& ref) {
reed91f283b2015-07-28 06:00:50 -0700354 return bounds->setBoundsCheck(ref.points(), ref.countPoints());
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000355 }
356
357 // called, if dirty, by getBounds()
358 void computeBounds() const {
359 SkDEBUGCODE(this->validate();)
Mike Klein0b4cc892014-07-16 17:18:20 -0400360 // TODO(mtklein): remove fBoundsIsDirty and fIsFinite,
361 // using an inverted rect instead of fBoundsIsDirty and always recalculating fIsFinite.
mtklein5c9c9be2014-12-01 06:59:54 -0800362 SkASSERT(fBoundsIsDirty);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000363
mtklein5c9c9be2014-12-01 06:59:54 -0800364 fIsFinite = ComputePtBounds(&fBounds, *this);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000365 fBoundsIsDirty = false;
366 }
367
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000368 void setBounds(const SkRect& rect) {
369 SkASSERT(rect.fLeft <= rect.fRight && rect.fTop <= rect.fBottom);
370 fBounds = rect;
371 fBoundsIsDirty = false;
mtklein5c9c9be2014-12-01 06:59:54 -0800372 fIsFinite = fBounds.isFinite();
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000373 }
374
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000375 /** Makes additional room but does not change the counts or change the genID */
376 void incReserve(int additionalVerbs, int additionalPoints) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000377 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000378 size_t space = additionalVerbs * sizeof(uint8_t) + additionalPoints * sizeof (SkPoint);
379 this->makeSpace(space);
robertphillips@google.com03087072013-10-02 16:42:21 +0000380 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000381 }
382
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000383 /** Resets the path ref with verbCount verbs and pointCount points, all uninitialized. Also
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000384 * allocates space for reserveVerb additional verbs and reservePoints additional points.*/
reed@google.com277c3f82013-05-31 15:17:50 +0000385 void resetToSize(int verbCount, int pointCount, int conicCount,
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000386 int reserveVerbs = 0, int reservePoints = 0) {
387 SkDEBUGCODE(this->validate();)
388 fBoundsIsDirty = true; // this also invalidates fIsFinite
389 fGenerationID = 0;
390
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000391 fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000392 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800393 fIsRRect = false;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000394
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000395 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 Wagnera93a14a2017-08-28 10:34:05 -0400403 fPoints = nullptr;
404 fVerbs = nullptr;
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000405 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.com1dfe88e2012-10-03 13:46:20 +0000420
421 /**
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000422 * 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.com6b8dbb62013-12-12 23:03:51 +0000424 * verb. If 'verb' is kConic_Verb, 'weights' will return a pointer to the
425 * uninitialized conic weights.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000426 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000427 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb, int numVbs, SkScalar** weights);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000428
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.com6b8dbb62013-12-12 23:03:51 +0000434 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000435
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.comaaf3e642013-10-02 17:49:50 +0000440 void makeSpace(size_t size) {
441 SkDEBUGCODE(this->validate();)
Ben Wagnerac326622017-07-31 16:57:01 -0400442 if (size <= fFreeSpace) {
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000443 return;
444 }
Ben Wagnerac326622017-07-31 16:57:01 -0400445 size_t growSize = size - fFreeSpace;
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000446 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 Wagnerac326622017-07-31 16:57:01 -0400450 if (growSize < oldSize) {
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000451 growSize = oldSize;
452 }
453 if (growSize < kMinSize) {
454 growSize = kMinSize;
455 }
Ben Wagnerac326622017-07-31 16:57:01 -0400456 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.comaaf3e642013-10-02 17:49:50 +0000463 // 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 Wagnerac326622017-07-31 16:57:01 -0400467 void* newVerbsDst = SkTAddOffset<void>(fPoints, newSize - oldVerbSize);
468 void* oldVerbsSrc = SkTAddOffset<void>(fPoints, oldSize - oldVerbSize);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000469 memmove(newVerbsDst, oldVerbsSrc, oldVerbSize);
Ben Wagnerac326622017-07-31 16:57:01 -0400470 fVerbs = SkTAddOffset<uint8_t>(fPoints, newSize);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000471 fFreeSpace += growSize;
472 SkDEBUGCODE(this->validate();)
473 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000474
475 /**
476 * Private, non-const-ptr version of the public function verbsMemBegin().
477 */
478 uint8_t* verbsMemWritable() {
robertphillips@google.com03087072013-10-02 16:42:21 +0000479 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000480 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.org1f81fd62013-10-23 14:44:08 +0000490 /**
491 * Called the first time someone calls CreateEmpty to actually create the singleton.
492 */
mtklein148ec592014-10-13 13:17:56 -0700493 friend SkPathRef* sk_create_empty_pathref();
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000494
bsalomon78d58d12016-05-27 09:17:04 -0700495 void setIsOval(bool isOval, bool isCCW, unsigned start) {
496 fIsOval = isOval;
497 fRRectOrOvalIsCCW = isCCW;
498 fRRectOrOvalStartIdx = start;
499 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000500
bsalomon78d58d12016-05-27 09:17:04 -0700501 void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
502 fIsRRect = isRRect;
503 fRRectOrOvalIsCCW = isCCW;
504 fRRectOrOvalStartIdx = start;
505 }
caryclarkda707bf2015-11-19 14:47:43 -0800506
507 // called only by the editor. Note that this is not a const function.
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000508 SkPoint* getPoints() {
509 SkDEBUGCODE(this->validate();)
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000510 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800511 fIsRRect = false;
512 return fPoints;
513 }
514
515 const SkPoint* getPoints() const {
516 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000517 return fPoints;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000518 }
519
senorblanco84cd6212015-08-04 10:01:58 -0700520 void callGenIDChangeListeners();
521
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000522 enum {
523 kMinSize = 256,
524 };
525
mtklein5c9c9be2014-12-01 06:59:54 -0800526 mutable SkRect fBounds;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000527
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000528 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.com277c3f82013-05-31 15:17:50 +0000533 SkTDArray<SkScalar> fConicWeights;
534
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000535 enum {
536 kEmptyGenID = 1, // GenID reserved for path ref with zero points and zero verbs.
537 };
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000538 mutable uint32_t fGenerationID;
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +0000539 SkDEBUGCODE(int32_t fEditorsAttached;) // assert that only one editor in use at any time.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000540
senorblanco84cd6212015-08-04 10:01:58 -0700541 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owned
542
caryclarkda707bf2015-11-19 14:47:43 -0800543 mutable uint8_t fBoundsIsDirty;
544 mutable SkBool8 fIsFinite; // only meaningful if bounds are valid
545
546 SkBool8 fIsOval;
547 SkBool8 fIsRRect;
bsalomon78d58d12016-05-27 09:17:04 -0700548 // 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;
caryclarkda707bf2015-11-19 14:47:43 -0800552 uint8_t fSegmentMask;
553
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000554 friend class PathRefTest_Private;
caryclarkda707bf2015-11-19 14:47:43 -0800555 friend class ForceIsRRect_Private; // unit test isRRect
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000556};
557
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000558#endif