blob: 91c05ba01c31b47d77d6dcdab9d3d3f3570e39bd [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
robertphillips@google.comca0c8382013-09-26 12:18:23 +000011#include "SkMatrix.h"
Mike Klein0191ed82018-09-17 17:29:39 -040012#include "SkMutex.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000013#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"
Mike Kleinde2244c2018-12-04 11:16:08 -050020#include <atomic>
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
Mike Klein408ef212018-10-30 15:23:00 +000041class 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
Mike Kleinde2244c2018-12-04 11:16:08 -050049 ~Editor() { SkDEBUGCODE(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 Daltona9441422018-10-25 18:35:55 -0600311 class GenIDChangeListener : public SkRefCnt {
312 public:
313 GenIDChangeListener() : fShouldUnregisterFromPath(false) {}
senorblanco84cd6212015-08-04 10:01:58 -0700314 virtual ~GenIDChangeListener() {}
Chris Daltona9441422018-10-25 18:35:55 -0600315
senorblanco84cd6212015-08-04 10:01:58 -0700316 virtual void onChange() = 0;
Chris Daltona9441422018-10-25 18:35:55 -0600317
318 // The caller can use this method to notify the path that it no longer needs to listen. Once
319 // called, the path will remove this listener from the list at some future point.
320 void markShouldUnregisterFromPath() {
321 fShouldUnregisterFromPath.store(true, std::memory_order_relaxed);
322 }
323 bool shouldUnregisterFromPath() {
324 return fShouldUnregisterFromPath.load(std::memory_order_acquire);
325 }
326
327 private:
328 std::atomic<bool> fShouldUnregisterFromPath;
senorblanco84cd6212015-08-04 10:01:58 -0700329 };
330
Mike Klein0191ed82018-09-17 17:29:39 -0400331 void addGenIDChangeListener(sk_sp<GenIDChangeListener>); // Threadsafe.
senorblanco84cd6212015-08-04 10:01:58 -0700332
Adrienne Walkerad8da8e2017-08-10 12:16:37 -0700333 bool isValid() const;
334 SkDEBUGCODE(void validate() const { SkASSERT(this->isValid()); } )
reed5bcbe912014-12-15 12:28:33 -0800335
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000336private:
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000337 enum SerializationOffsets {
Brian Salomonb7d42e32017-09-21 16:59:17 -0400338 kLegacyRRectOrOvalStartIdx_SerializationShift = 28, // requires 3 bits, ignored.
339 kLegacyRRectOrOvalIsCCW_SerializationShift = 27, // requires 1 bit, ignored.
340 kLegacyIsRRect_SerializationShift = 26, // requires 1 bit, ignored.
341 kIsFinite_SerializationShift = 25, // requires 1 bit
342 kLegacyIsOval_SerializationShift = 24, // requires 1 bit, ignored.
Mike Reeda3d9e212018-02-20 09:48:13 -0500343 kSegmentMask_SerializationShift = 0 // requires 4 bits (deprecated)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000344 };
345
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000346 SkPathRef() {
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000347 fBoundsIsDirty = true; // this also invalidates fIsFinite
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000348 fPointCnt = 0;
349 fVerbCnt = 0;
Ben Wagnera93a14a2017-08-28 10:34:05 -0400350 fVerbs = nullptr;
351 fPoints = nullptr;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000352 fFreeSpace = 0;
353 fGenerationID = kEmptyGenID;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000354 fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000355 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800356 fIsRRect = false;
bsalomon78d58d12016-05-27 09:17:04 -0700357 // The next two values don't matter unless fIsOval or fIsRRect are true.
senorblanco9c1d45d2016-07-22 13:51:42 -0700358 fRRectOrOvalIsCCW = false;
359 fRRectOrOvalStartIdx = 0xAC;
Mike Kleinde2244c2018-12-04 11:16:08 -0500360 SkDEBUGCODE(fEditorsAttached.store(0);)
robertphillips@google.com03087072013-10-02 16:42:21 +0000361 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000362 }
363
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000364 void copy(const SkPathRef& ref, int additionalReserveVerbs, int additionalReservePoints);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000365
Mike Reeda3d9e212018-02-20 09:48:13 -0500366 // Doesn't read fSegmentMask, but (re)computes it from the verbs array
367 unsigned computeSegmentMask() const;
368
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000369 // Return true if the computed bounds are finite.
370 static bool ComputePtBounds(SkRect* bounds, const SkPathRef& ref) {
reed91f283b2015-07-28 06:00:50 -0700371 return bounds->setBoundsCheck(ref.points(), ref.countPoints());
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000372 }
373
374 // called, if dirty, by getBounds()
375 void computeBounds() const {
376 SkDEBUGCODE(this->validate();)
Mike Klein0b4cc892014-07-16 17:18:20 -0400377 // TODO(mtklein): remove fBoundsIsDirty and fIsFinite,
378 // using an inverted rect instead of fBoundsIsDirty and always recalculating fIsFinite.
mtklein5c9c9be2014-12-01 06:59:54 -0800379 SkASSERT(fBoundsIsDirty);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000380
mtklein5c9c9be2014-12-01 06:59:54 -0800381 fIsFinite = ComputePtBounds(&fBounds, *this);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000382 fBoundsIsDirty = false;
383 }
384
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000385 void setBounds(const SkRect& rect) {
386 SkASSERT(rect.fLeft <= rect.fRight && rect.fTop <= rect.fBottom);
387 fBounds = rect;
388 fBoundsIsDirty = false;
mtklein5c9c9be2014-12-01 06:59:54 -0800389 fIsFinite = fBounds.isFinite();
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000390 }
391
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000392 /** Makes additional room but does not change the counts or change the genID */
393 void incReserve(int additionalVerbs, int additionalPoints) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000394 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000395 size_t space = additionalVerbs * sizeof(uint8_t) + additionalPoints * sizeof (SkPoint);
396 this->makeSpace(space);
robertphillips@google.com03087072013-10-02 16:42:21 +0000397 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000398 }
399
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000400 /** Resets the path ref with verbCount verbs and pointCount points, all uninitialized. Also
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000401 * allocates space for reserveVerb additional verbs and reservePoints additional points.*/
reed@google.com277c3f82013-05-31 15:17:50 +0000402 void resetToSize(int verbCount, int pointCount, int conicCount,
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000403 int reserveVerbs = 0, int reservePoints = 0) {
404 SkDEBUGCODE(this->validate();)
405 fBoundsIsDirty = true; // this also invalidates fIsFinite
406 fGenerationID = 0;
407
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000408 fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000409 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800410 fIsRRect = false;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000411
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000412 size_t newSize = sizeof(uint8_t) * verbCount + sizeof(SkPoint) * pointCount;
413 size_t newReserve = sizeof(uint8_t) * reserveVerbs + sizeof(SkPoint) * reservePoints;
414 size_t minSize = newSize + newReserve;
415
416 ptrdiff_t sizeDelta = this->currSize() - minSize;
417
418 if (sizeDelta < 0 || static_cast<size_t>(sizeDelta) >= 3 * minSize) {
419 sk_free(fPoints);
Ben Wagnera93a14a2017-08-28 10:34:05 -0400420 fPoints = nullptr;
421 fVerbs = nullptr;
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000422 fFreeSpace = 0;
423 fVerbCnt = 0;
424 fPointCnt = 0;
Florin Malita3d413c52018-09-11 14:01:42 -0400425 this->makeSpace(minSize, true);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000426 fVerbCnt = verbCount;
427 fPointCnt = pointCount;
428 fFreeSpace -= newSize;
429 } else {
430 fPointCnt = pointCount;
431 fVerbCnt = verbCount;
432 fFreeSpace = this->currSize() - minSize;
433 }
434 fConicWeights.setCount(conicCount);
435 SkDEBUGCODE(this->validate();)
436 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000437
438 /**
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000439 * Increases the verb count by numVbs and point count by the required amount.
440 * The new points are uninitialized. All the new verbs are set to the specified
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000441 * verb. If 'verb' is kConic_Verb, 'weights' will return a pointer to the
442 * uninitialized conic weights.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000443 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000444 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb, int numVbs, SkScalar** weights);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000445
446 /**
447 * Increases the verb count 1, records the new verb, and creates room for the requisite number
448 * of additional points. A pointer to the first point is returned. Any new points are
449 * uninitialized.
450 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000451 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000452
453 /**
454 * Ensures that the free space available in the path ref is >= size. The verb and point counts
Florin Malita3d413c52018-09-11 14:01:42 -0400455 * are not changed. May allocate extra capacity, unless |exact| is true.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000456 */
Florin Malita3d413c52018-09-11 14:01:42 -0400457 void makeSpace(size_t size, bool exact = false) {
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000458 SkDEBUGCODE(this->validate();)
Ben Wagnerac326622017-07-31 16:57:01 -0400459 if (size <= fFreeSpace) {
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000460 return;
461 }
Ben Wagnerac326622017-07-31 16:57:01 -0400462 size_t growSize = size - fFreeSpace;
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000463 size_t oldSize = this->currSize();
Florin Malita3d413c52018-09-11 14:01:42 -0400464
465 if (!exact) {
466 // round to next multiple of 8 bytes
467 growSize = (growSize + 7) & ~static_cast<size_t>(7);
468 // we always at least double the allocation
469 if (growSize < oldSize) {
470 growSize = oldSize;
471 }
472 if (growSize < kMinSize) {
473 growSize = kMinSize;
474 }
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000475 }
Florin Malita3d413c52018-09-11 14:01:42 -0400476
Ben Wagnerac326622017-07-31 16:57:01 -0400477 constexpr size_t maxSize = std::numeric_limits<size_t>::max();
478 size_t newSize;
479 if (growSize <= maxSize - oldSize) {
480 newSize = oldSize + growSize;
481 } else {
482 SK_ABORT("Path too big.");
483 }
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000484 // Note that realloc could memcpy more than we need. It seems to be a win anyway. TODO:
485 // encapsulate this.
486 fPoints = reinterpret_cast<SkPoint*>(sk_realloc_throw(fPoints, newSize));
487 size_t oldVerbSize = fVerbCnt * sizeof(uint8_t);
Ben Wagnerac326622017-07-31 16:57:01 -0400488 void* newVerbsDst = SkTAddOffset<void>(fPoints, newSize - oldVerbSize);
489 void* oldVerbsSrc = SkTAddOffset<void>(fPoints, oldSize - oldVerbSize);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000490 memmove(newVerbsDst, oldVerbsSrc, oldVerbSize);
Ben Wagnerac326622017-07-31 16:57:01 -0400491 fVerbs = SkTAddOffset<uint8_t>(fPoints, newSize);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000492 fFreeSpace += growSize;
493 SkDEBUGCODE(this->validate();)
494 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000495
496 /**
497 * Private, non-const-ptr version of the public function verbsMemBegin().
498 */
499 uint8_t* verbsMemWritable() {
robertphillips@google.com03087072013-10-02 16:42:21 +0000500 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000501 return fVerbs - fVerbCnt;
502 }
503
504 /**
505 * Gets the total amount of space allocated for verbs, points, and reserve.
506 */
507 size_t currSize() const {
508 return reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints);
509 }
510
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000511 /**
512 * Called the first time someone calls CreateEmpty to actually create the singleton.
513 */
mtklein148ec592014-10-13 13:17:56 -0700514 friend SkPathRef* sk_create_empty_pathref();
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000515
bsalomon78d58d12016-05-27 09:17:04 -0700516 void setIsOval(bool isOval, bool isCCW, unsigned start) {
517 fIsOval = isOval;
518 fRRectOrOvalIsCCW = isCCW;
Kaloyan Donevdfffb392018-03-20 10:38:31 +0200519 fRRectOrOvalStartIdx = SkToU8(start);
bsalomon78d58d12016-05-27 09:17:04 -0700520 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000521
bsalomon78d58d12016-05-27 09:17:04 -0700522 void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
523 fIsRRect = isRRect;
524 fRRectOrOvalIsCCW = isCCW;
Kaloyan Donevdfffb392018-03-20 10:38:31 +0200525 fRRectOrOvalStartIdx = SkToU8(start);
bsalomon78d58d12016-05-27 09:17:04 -0700526 }
caryclarkda707bf2015-11-19 14:47:43 -0800527
528 // called only by the editor. Note that this is not a const function.
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000529 SkPoint* getPoints() {
530 SkDEBUGCODE(this->validate();)
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000531 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800532 fIsRRect = false;
533 return fPoints;
534 }
535
536 const SkPoint* getPoints() const {
537 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000538 return fPoints;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000539 }
540
senorblanco84cd6212015-08-04 10:01:58 -0700541 void callGenIDChangeListeners();
542
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000543 enum {
544 kMinSize = 256,
545 };
546
mtklein5c9c9be2014-12-01 06:59:54 -0800547 mutable SkRect fBounds;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000548
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000549 SkPoint* fPoints; // points to begining of the allocation
550 uint8_t* fVerbs; // points just past the end of the allocation (verbs grow backwards)
551 int fVerbCnt;
552 int fPointCnt;
553 size_t fFreeSpace; // redundant but saves computation
reed@google.com277c3f82013-05-31 15:17:50 +0000554 SkTDArray<SkScalar> fConicWeights;
555
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000556 enum {
557 kEmptyGenID = 1, // GenID reserved for path ref with zero points and zero verbs.
558 };
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000559 mutable uint32_t fGenerationID;
Mike Kleinde2244c2018-12-04 11:16:08 -0500560 SkDEBUGCODE(std::atomic<int> fEditorsAttached;) // assert only one editor in use at any time.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000561
Greg Danieleb772c02018-10-19 15:00:06 +0000562 SkMutex fGenIDChangeListenersMutex;
563 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are reffed
senorblanco84cd6212015-08-04 10:01:58 -0700564
caryclarkda707bf2015-11-19 14:47:43 -0800565 mutable uint8_t fBoundsIsDirty;
Ben Wagnercee46e52018-06-12 16:30:29 -0400566 mutable bool fIsFinite; // only meaningful if bounds are valid
caryclarkda707bf2015-11-19 14:47:43 -0800567
Ben Wagnercee46e52018-06-12 16:30:29 -0400568 bool fIsOval;
569 bool fIsRRect;
bsalomon78d58d12016-05-27 09:17:04 -0700570 // Both the circle and rrect special cases have a notion of direction and starting point
571 // The next two variables store that information for either.
Ben Wagnercee46e52018-06-12 16:30:29 -0400572 bool fRRectOrOvalIsCCW;
bsalomon78d58d12016-05-27 09:17:04 -0700573 uint8_t fRRectOrOvalStartIdx;
caryclarkda707bf2015-11-19 14:47:43 -0800574 uint8_t fSegmentMask;
575
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000576 friend class PathRefTest_Private;
caryclarkda707bf2015-11-19 14:47:43 -0800577 friend class ForceIsRRect_Private; // unit test isRRect
Florin Malita3d413c52018-09-11 14:01:42 -0400578 friend class SkPath;
Mike Reed07105bb2018-12-01 14:07:49 -0500579 friend class SkPathPriv;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000580};
581
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000582#endif