blob: 5f64f1ae0737d9e972d01ad5c9341861f769d967 [file] [log] [blame]
Hal Canaryfdcfb8b2018-06-13 09:42:32 -04001
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +00002/*
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
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040012#include "../private/SkAtomics.h"
13#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"
Mike Kleinbf45c702018-06-11 11:56:57 -040020#include <limits>
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000021
robertphillips@google.comca0c8382013-09-26 12:18:23 +000022class SkRBuffer;
23class SkWBuffer;
24
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000025/**
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.com466310d2013-12-03 16:43:54 +000029 * copy-on-write if the SkPathRef is shared by multiple SkPaths. The caller passes the Editor's
bungeman6bd52842016-10-27 09:30:08 -070030 * 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.com1dfe88e2012-10-03 13:46:20 +000032 *
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.comae09f2d2012-10-03 19:57:01 +000039
mtkleinb47cd4b2016-08-09 12:20:04 -070040class SK_API SkPathRef final : public SkNVRefCnt<SkPathRef> {
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000041public:
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000042 class Editor {
43 public:
bungeman6bd52842016-10-27 09:30:08 -070044 Editor(sk_sp<SkPathRef>* pathRef,
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000045 int incReserveVerbs = 0,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000046 int incReservePoints = 0);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000047
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +000048 ~Editor() { SkDEBUGCODE(sk_atomic_dec(&fPathRef->fEditorsAttached);) }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000049
50 /**
51 * Returns the array of points.
52 */
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000053 SkPoint* points() { return fPathRef->getPoints(); }
54 const SkPoint* points() const { return fPathRef->points(); }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000055
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 Kleinfc6c37b2016-09-27 09:34:10 -040062 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000063 const SkPoint* atPoint(int i) const {
64 SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
65 return this->points() + i;
Mike Kleinfc6c37b2016-09-27 09:34:10 -040066 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000067
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.com6b8dbb62013-12-12 23:03:51 +000071 * 'weight' is only used if 'verb' is kConic_Verb
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000072 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000073 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight = 0) {
robertphillips@google.com03087072013-10-02 16:42:21 +000074 SkDEBUGCODE(fPathRef->validate();)
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000075 return fPathRef->growForVerb(verb, weight);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000076 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000077
78 /**
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000079 * 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.com96f5fa02013-12-16 07:01:40 +000083 * space for the conic weights (indexed normally).
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000084 */
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000085 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb,
86 int numVbs,
Ben Wagnera93a14a2017-08-28 10:34:05 -040087 SkScalar** weights = nullptr) {
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000088 return fPathRef->growForRepeatedVerb(verb, numVbs, weights);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000089 }
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.com277c3f82013-05-31 15:17:50 +000095 void resetToSize(int newVerbCnt, int newPointCnt, int newConicCount) {
96 fPathRef->resetToSize(newVerbCnt, newPointCnt, newConicCount);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000097 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000098
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000099 /**
100 * Gets the path ref that is wrapped in the Editor.
101 */
102 SkPathRef* pathRef() { return fPathRef; }
103
bsalomon78d58d12016-05-27 09:17:04 -0700104 void setIsOval(bool isOval, bool isCCW, unsigned start) {
105 fPathRef->setIsOval(isOval, isCCW, start);
106 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000107
bsalomon78d58d12016-05-27 09:17:04 -0700108 void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
109 fPathRef->setIsRRect(isRRect, isCCW, start);
110 }
caryclarkda707bf2015-11-19 14:47:43 -0800111
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000112 void setBounds(const SkRect& rect) { fPathRef->setBounds(rect); }
113
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000114 private:
115 SkPathRef* fPathRef;
116 };
117
caryclarkda707bf2015-11-19 14:47:43 -0800118 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 Klein1170a552017-09-08 15:00:25 -0400128 If any point in the path is non-finite, return kDone_Verb immediately.
129
caryclarkda707bf2015-11-19 14:47:43 -0800130 @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]);
caryclark2028d7f2015-12-09 14:04:46 -0800135 uint8_t peek() const;
caryclarkda707bf2015-11-19 14:47:43 -0800136
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.com1dfe88e2012-10-03 13:46:20 +0000146public:
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000147 /**
148 * Gets a path ref with no verbs or points.
149 */
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000150 static SkPathRef* CreateEmpty();
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000151
152 /**
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000153 * 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.com6b8dbb62013-12-12 23:03:51 +0000163 /**
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.com466310d2013-12-03 16:43:54 +0000170 /** 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.
bsalomon78d58d12016-05-27 09:17:04 -0700174 * @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.com466310d2013-12-03 16:43:54 +0000177 *
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 */
bsalomon78d58d12016-05-27 09:17:04 -0700183 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.com466310d2013-12-03 16:43:54 +0000194 }
195
196 return SkToBool(fIsOval);
197 }
198
bsalomon78d58d12016-05-27 09:17:04 -0700199 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 }
caryclarkda707bf2015-11-19 14:47:43 -0800210 }
211 return SkToBool(fIsRRect);
212 }
213
214
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000215 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.com65caeaf2013-09-27 07:01:29 +0000224 const SkRect& getBounds() const {
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000225 if (fBoundsIsDirty) {
226 this->computeBounds();
227 }
228 return fBounds;
229 }
230
caryclarkda707bf2015-11-19 14:47:43 -0800231 SkRRect getRRect() const;
232
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000233 /**
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000234 * Transforms a path ref by a matrix, allocating a new one only if necessary.
235 */
bungeman6bd52842016-10-27 09:30:08 -0700236 static void CreateTransformedCopy(sk_sp<SkPathRef>* dst,
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000237 const SkPathRef& src,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000238 const SkMatrix& matrix);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000239
commit-bot@chromium.orgfed2ab62014-01-23 15:16:05 +0000240 static SkPathRef* CreateFromBuffer(SkRBuffer* buffer);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000241
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 */
bungeman6bd52842016-10-27 09:30:08 -0700247 static void Rewind(sk_sp<SkPathRef>* pathRef);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000248
mtkleinb47cd4b2016-08-09 12:20:04 -0700249 ~SkPathRef();
Brian Osman03776fc2017-08-15 16:08:48 -0400250 int countPoints() const { return fPointCnt; }
251 int countVerbs() const { return fVerbCnt; }
252 int countWeights() const { return fConicWeights.count(); }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000253
254 /**
255 * Returns a pointer one beyond the first logical verb (last verb in memory order).
256 */
Brian Osman03776fc2017-08-15 16:08:48 -0400257 const uint8_t* verbs() const { return fVerbs; }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000258
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 Osman03776fc2017-08-15 16:08:48 -0400267 const SkPoint* points() const { return fPoints; }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000268
269 /**
270 * Shortcut for this->points() + this->countPoints()
271 */
272 const SkPoint* pointsEnd() const { return this->points() + this->countPoints(); }
273
Brian Osman03776fc2017-08-15 16:08:48 -0400274 const SkScalar* conicWeights() const { return fConicWeights.begin(); }
275 const SkScalar* conicWeightsEnd() const { return fConicWeights.end(); }
reed@google.com277c3f82013-05-31 15:17:50 +0000276
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000277 /**
278 * Convenience methods for getting to a verb or point by index.
279 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000280 uint8_t atVerb(int index) const {
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000281 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.com3e292aa2013-09-27 17:48:49 +0000289 bool operator== (const SkPathRef& ref) const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000290
291 /**
292 * Writes the path points and verbs to a buffer.
293 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000294 void writeToBuffer(SkWBuffer* buffer) const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000295
296 /**
297 * Gets the number of bytes that would be written in writeBuffer()
298 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000299 uint32_t writeSize() const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000300
caryclark8e7b19d2016-02-18 04:11:48 -0800301 void interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef* out) const;
302
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000303 /**
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 Daltonafa11582018-06-08 12:00:44 -0600310 struct GenIDChangeListener : SkRefCnt {
senorblanco84cd6212015-08-04 10:01:58 -0700311 virtual ~GenIDChangeListener() {}
312 virtual void onChange() = 0;
313 };
314
Chris Daltonafa11582018-06-08 12:00:44 -0600315 void addGenIDChangeListener(sk_sp<GenIDChangeListener>);
senorblanco84cd6212015-08-04 10:01:58 -0700316
Adrienne Walkerad8da8e2017-08-10 12:16:37 -0700317 bool isValid() const;
318 SkDEBUGCODE(void validate() const { SkASSERT(this->isValid()); } )
reed5bcbe912014-12-15 12:28:33 -0800319
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000320private:
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000321 enum SerializationOffsets {
Brian Salomonb7d42e32017-09-21 16:59:17 -0400322 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 Reeda3d9e212018-02-20 09:48:13 -0500327 kSegmentMask_SerializationShift = 0 // requires 4 bits (deprecated)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000328 };
329
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000330 SkPathRef() {
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000331 fBoundsIsDirty = true; // this also invalidates fIsFinite
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000332 fPointCnt = 0;
333 fVerbCnt = 0;
Ben Wagnera93a14a2017-08-28 10:34:05 -0400334 fVerbs = nullptr;
335 fPoints = nullptr;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000336 fFreeSpace = 0;
337 fGenerationID = kEmptyGenID;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000338 fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000339 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800340 fIsRRect = false;
bsalomon78d58d12016-05-27 09:17:04 -0700341 // The next two values don't matter unless fIsOval or fIsRRect are true.
senorblanco9c1d45d2016-07-22 13:51:42 -0700342 fRRectOrOvalIsCCW = false;
343 fRRectOrOvalStartIdx = 0xAC;
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +0000344 SkDEBUGCODE(fEditorsAttached = 0;)
robertphillips@google.com03087072013-10-02 16:42:21 +0000345 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000346 }
347
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000348 void copy(const SkPathRef& ref, int additionalReserveVerbs, int additionalReservePoints);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000349
Mike Reeda3d9e212018-02-20 09:48:13 -0500350 // Doesn't read fSegmentMask, but (re)computes it from the verbs array
351 unsigned computeSegmentMask() const;
352
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000353 // Return true if the computed bounds are finite.
354 static bool ComputePtBounds(SkRect* bounds, const SkPathRef& ref) {
reed91f283b2015-07-28 06:00:50 -0700355 return bounds->setBoundsCheck(ref.points(), ref.countPoints());
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000356 }
357
358 // called, if dirty, by getBounds()
359 void computeBounds() const {
360 SkDEBUGCODE(this->validate();)
Mike Klein0b4cc892014-07-16 17:18:20 -0400361 // TODO(mtklein): remove fBoundsIsDirty and fIsFinite,
362 // using an inverted rect instead of fBoundsIsDirty and always recalculating fIsFinite.
mtklein5c9c9be2014-12-01 06:59:54 -0800363 SkASSERT(fBoundsIsDirty);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000364
mtklein5c9c9be2014-12-01 06:59:54 -0800365 fIsFinite = ComputePtBounds(&fBounds, *this);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000366 fBoundsIsDirty = false;
367 }
368
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000369 void setBounds(const SkRect& rect) {
370 SkASSERT(rect.fLeft <= rect.fRight && rect.fTop <= rect.fBottom);
371 fBounds = rect;
372 fBoundsIsDirty = false;
mtklein5c9c9be2014-12-01 06:59:54 -0800373 fIsFinite = fBounds.isFinite();
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000374 }
375
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000376 /** Makes additional room but does not change the counts or change the genID */
377 void incReserve(int additionalVerbs, int additionalPoints) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000378 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000379 size_t space = additionalVerbs * sizeof(uint8_t) + additionalPoints * sizeof (SkPoint);
380 this->makeSpace(space);
robertphillips@google.com03087072013-10-02 16:42:21 +0000381 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000382 }
383
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000384 /** Resets the path ref with verbCount verbs and pointCount points, all uninitialized. Also
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000385 * allocates space for reserveVerb additional verbs and reservePoints additional points.*/
reed@google.com277c3f82013-05-31 15:17:50 +0000386 void resetToSize(int verbCount, int pointCount, int conicCount,
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000387 int reserveVerbs = 0, int reservePoints = 0) {
388 SkDEBUGCODE(this->validate();)
389 fBoundsIsDirty = true; // this also invalidates fIsFinite
390 fGenerationID = 0;
391
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000392 fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000393 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800394 fIsRRect = false;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000395
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000396 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 Wagnera93a14a2017-08-28 10:34:05 -0400404 fPoints = nullptr;
405 fVerbs = nullptr;
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000406 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.com1dfe88e2012-10-03 13:46:20 +0000421
422 /**
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000423 * 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.com6b8dbb62013-12-12 23:03:51 +0000425 * verb. If 'verb' is kConic_Verb, 'weights' will return a pointer to the
426 * uninitialized conic weights.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000427 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000428 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb, int numVbs, SkScalar** weights);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000429
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.com6b8dbb62013-12-12 23:03:51 +0000435 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000436
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.comaaf3e642013-10-02 17:49:50 +0000441 void makeSpace(size_t size) {
442 SkDEBUGCODE(this->validate();)
Ben Wagnerac326622017-07-31 16:57:01 -0400443 if (size <= fFreeSpace) {
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000444 return;
445 }
Ben Wagnerac326622017-07-31 16:57:01 -0400446 size_t growSize = size - fFreeSpace;
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000447 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 Wagnerac326622017-07-31 16:57:01 -0400451 if (growSize < oldSize) {
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000452 growSize = oldSize;
453 }
454 if (growSize < kMinSize) {
455 growSize = kMinSize;
456 }
Ben Wagnerac326622017-07-31 16:57:01 -0400457 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.comaaf3e642013-10-02 17:49:50 +0000464 // 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 Wagnerac326622017-07-31 16:57:01 -0400468 void* newVerbsDst = SkTAddOffset<void>(fPoints, newSize - oldVerbSize);
469 void* oldVerbsSrc = SkTAddOffset<void>(fPoints, oldSize - oldVerbSize);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000470 memmove(newVerbsDst, oldVerbsSrc, oldVerbSize);
Ben Wagnerac326622017-07-31 16:57:01 -0400471 fVerbs = SkTAddOffset<uint8_t>(fPoints, newSize);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000472 fFreeSpace += growSize;
473 SkDEBUGCODE(this->validate();)
474 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000475
476 /**
477 * Private, non-const-ptr version of the public function verbsMemBegin().
478 */
479 uint8_t* verbsMemWritable() {
robertphillips@google.com03087072013-10-02 16:42:21 +0000480 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000481 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.org1f81fd62013-10-23 14:44:08 +0000491 /**
492 * Called the first time someone calls CreateEmpty to actually create the singleton.
493 */
mtklein148ec592014-10-13 13:17:56 -0700494 friend SkPathRef* sk_create_empty_pathref();
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000495
bsalomon78d58d12016-05-27 09:17:04 -0700496 void setIsOval(bool isOval, bool isCCW, unsigned start) {
497 fIsOval = isOval;
498 fRRectOrOvalIsCCW = isCCW;
Kaloyan Donevdfffb392018-03-20 10:38:31 +0200499 fRRectOrOvalStartIdx = SkToU8(start);
bsalomon78d58d12016-05-27 09:17:04 -0700500 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000501
bsalomon78d58d12016-05-27 09:17:04 -0700502 void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
503 fIsRRect = isRRect;
504 fRRectOrOvalIsCCW = isCCW;
Kaloyan Donevdfffb392018-03-20 10:38:31 +0200505 fRRectOrOvalStartIdx = SkToU8(start);
bsalomon78d58d12016-05-27 09:17:04 -0700506 }
caryclarkda707bf2015-11-19 14:47:43 -0800507
508 // called only by the editor. Note that this is not a const function.
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000509 SkPoint* getPoints() {
510 SkDEBUGCODE(this->validate();)
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000511 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800512 fIsRRect = false;
513 return fPoints;
514 }
515
516 const SkPoint* getPoints() const {
517 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000518 return fPoints;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000519 }
520
senorblanco84cd6212015-08-04 10:01:58 -0700521 void callGenIDChangeListeners();
522
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000523 enum {
524 kMinSize = 256,
525 };
526
mtklein5c9c9be2014-12-01 06:59:54 -0800527 mutable SkRect fBounds;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000528
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000529 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.com277c3f82013-05-31 15:17:50 +0000534 SkTDArray<SkScalar> fConicWeights;
535
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000536 enum {
537 kEmptyGenID = 1, // GenID reserved for path ref with zero points and zero verbs.
538 };
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000539 mutable uint32_t fGenerationID;
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +0000540 SkDEBUGCODE(int32_t fEditorsAttached;) // assert that only one editor in use at any time.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000541
Chris Daltonafa11582018-06-08 12:00:44 -0600542 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are reffed
senorblanco84cd6212015-08-04 10:01:58 -0700543
caryclarkda707bf2015-11-19 14:47:43 -0800544 mutable uint8_t fBoundsIsDirty;
Ben Wagnercee46e52018-06-12 16:30:29 -0400545 mutable bool fIsFinite; // only meaningful if bounds are valid
caryclarkda707bf2015-11-19 14:47:43 -0800546
Ben Wagnercee46e52018-06-12 16:30:29 -0400547 bool fIsOval;
548 bool fIsRRect;
bsalomon78d58d12016-05-27 09:17:04 -0700549 // 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.
Ben Wagnercee46e52018-06-12 16:30:29 -0400551 bool fRRectOrOvalIsCCW;
bsalomon78d58d12016-05-27 09:17:04 -0700552 uint8_t fRRectOrOvalStartIdx;
caryclarkda707bf2015-11-19 14:47:43 -0800553 uint8_t fSegmentMask;
554
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000555 friend class PathRefTest_Private;
caryclarkda707bf2015-11-19 14:47:43 -0800556 friend class ForceIsRRect_Private; // unit test isRRect
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000557};
558
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000559#endif