blob: 5678eb73c831a1984a24ec56e51d77c0c65d3870 [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"
Mike Klein0191ed82018-09-17 17:29:39 -040013#include "SkMutex.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000014#include "SkPoint.h"
caryclarkda707bf2015-11-19 14:47:43 -080015#include "SkRRect.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000016#include "SkRect.h"
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000017#include "SkRefCnt.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040018#include "SkTDArray.h"
Ben Wagnerac326622017-07-31 16:57:01 -040019#include "SkTemplates.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040020#include "SkTo.h"
21
Mike Kleinbf45c702018-06-11 11:56:57 -040022#include <limits>
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000023
robertphillips@google.comca0c8382013-09-26 12:18:23 +000024class SkRBuffer;
25class SkWBuffer;
26
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000027/**
28 * Holds the path verbs and points. It is versioned by a generation ID. None of its public methods
29 * modify the contents. To modify or append to the verbs/points wrap the SkPathRef in an
30 * SkPathRef::Editor object. Installing the editor resets the generation ID. It also performs
robertphillips@google.com466310d2013-12-03 16:43:54 +000031 * copy-on-write if the SkPathRef is shared by multiple SkPaths. The caller passes the Editor's
bungeman6bd52842016-10-27 09:30:08 -070032 * constructor a pointer to a sk_sp<SkPathRef>, which may be updated to point to a new SkPathRef
33 * after the editor's constructor returns.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000034 *
35 * The points and verbs are stored in a single allocation. The points are at the begining of the
36 * allocation while the verbs are stored at end of the allocation, in reverse order. Thus the points
37 * and verbs both grow into the middle of the allocation until the meet. To access verb i in the
38 * verb array use ref.verbs()[~i] (because verbs() returns a pointer just beyond the first
39 * logical verb or the last verb in memory).
40 */
bsalomon@google.comae09f2d2012-10-03 19:57:01 +000041
Mike Klein408ef212018-10-30 15:23:00 +000042class SK_API SkPathRef final : public SkNVRefCnt<SkPathRef> {
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000043public:
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000044 class Editor {
45 public:
bungeman6bd52842016-10-27 09:30:08 -070046 Editor(sk_sp<SkPathRef>* pathRef,
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000047 int incReserveVerbs = 0,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000048 int incReservePoints = 0);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000049
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +000050 ~Editor() { SkDEBUGCODE(sk_atomic_dec(&fPathRef->fEditorsAttached);) }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000051
52 /**
53 * Returns the array of points.
54 */
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000055 SkPoint* points() { return fPathRef->getPoints(); }
56 const SkPoint* points() const { return fPathRef->points(); }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000057
58 /**
59 * Gets the ith point. Shortcut for this->points() + i
60 */
61 SkPoint* atPoint(int i) {
62 SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
63 return this->points() + i;
Mike Kleinfc6c37b2016-09-27 09:34:10 -040064 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000065 const SkPoint* atPoint(int i) const {
66 SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
67 return this->points() + i;
Mike Kleinfc6c37b2016-09-27 09:34:10 -040068 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000069
70 /**
71 * Adds the verb and allocates space for the number of points indicated by the verb. The
72 * return value is a pointer to where the points for the verb should be written.
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000073 * 'weight' is only used if 'verb' is kConic_Verb
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000074 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000075 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight = 0) {
robertphillips@google.com03087072013-10-02 16:42:21 +000076 SkDEBUGCODE(fPathRef->validate();)
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000077 return fPathRef->growForVerb(verb, weight);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000078 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000079
80 /**
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000081 * Allocates space for multiple instances of a particular verb and the
82 * requisite points & weights.
83 * The return pointer points at the first new point (indexed normally [<i>]).
84 * If 'verb' is kConic_Verb, 'weights' will return a pointer to the
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000085 * space for the conic weights (indexed normally).
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000086 */
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000087 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb,
88 int numVbs,
Ben Wagnera93a14a2017-08-28 10:34:05 -040089 SkScalar** weights = nullptr) {
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000090 return fPathRef->growForRepeatedVerb(verb, numVbs, weights);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000091 }
92
93 /**
94 * Resets the path ref to a new verb and point count. The new verbs and points are
95 * uninitialized.
96 */
reed@google.com277c3f82013-05-31 15:17:50 +000097 void resetToSize(int newVerbCnt, int newPointCnt, int newConicCount) {
98 fPathRef->resetToSize(newVerbCnt, newPointCnt, newConicCount);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000099 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000100
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000101 /**
102 * Gets the path ref that is wrapped in the Editor.
103 */
104 SkPathRef* pathRef() { return fPathRef; }
105
bsalomon78d58d12016-05-27 09:17:04 -0700106 void setIsOval(bool isOval, bool isCCW, unsigned start) {
107 fPathRef->setIsOval(isOval, isCCW, start);
108 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000109
bsalomon78d58d12016-05-27 09:17:04 -0700110 void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
111 fPathRef->setIsRRect(isRRect, isCCW, start);
112 }
caryclarkda707bf2015-11-19 14:47:43 -0800113
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000114 void setBounds(const SkRect& rect) { fPathRef->setBounds(rect); }
115
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000116 private:
117 SkPathRef* fPathRef;
118 };
119
caryclarkda707bf2015-11-19 14:47:43 -0800120 class SK_API Iter {
121 public:
122 Iter();
123 Iter(const SkPathRef&);
124
125 void setPathRef(const SkPathRef&);
126
127 /** Return the next verb in this iteration of the path. When all
128 segments have been visited, return kDone_Verb.
129
Mike Klein1170a552017-09-08 15:00:25 -0400130 If any point in the path is non-finite, return kDone_Verb immediately.
131
caryclarkda707bf2015-11-19 14:47:43 -0800132 @param pts The points representing the current verb and/or segment
133 This must not be NULL.
134 @return The verb for the current segment
135 */
136 uint8_t next(SkPoint pts[4]);
caryclark2028d7f2015-12-09 14:04:46 -0800137 uint8_t peek() const;
caryclarkda707bf2015-11-19 14:47:43 -0800138
139 SkScalar conicWeight() const { return *fConicWeights; }
140
141 private:
142 const SkPoint* fPts;
143 const uint8_t* fVerbs;
144 const uint8_t* fVerbStop;
145 const SkScalar* fConicWeights;
146 };
147
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000148public:
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000149 /**
150 * Gets a path ref with no verbs or points.
151 */
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000152 static SkPathRef* CreateEmpty();
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000153
154 /**
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000155 * Returns true if all of the points in this path are finite, meaning there
156 * are no infinities and no NaNs.
157 */
158 bool isFinite() const {
159 if (fBoundsIsDirty) {
160 this->computeBounds();
161 }
162 return SkToBool(fIsFinite);
163 }
164
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000165 /**
166 * Returns a mask, where each bit corresponding to a SegmentMask is
167 * set if the path contains 1 or more segments of that type.
168 * Returns 0 for an empty path (no segments).
169 */
170 uint32_t getSegmentMasks() const { return fSegmentMask; }
171
robertphillips@google.com466310d2013-12-03 16:43:54 +0000172 /** Returns true if the path is an oval.
173 *
174 * @param rect returns the bounding rect of this oval. It's a circle
175 * if the height and width are the same.
bsalomon78d58d12016-05-27 09:17:04 -0700176 * @param isCCW is the oval CCW (or CW if false).
177 * @param start indicates where the contour starts on the oval (see
178 * SkPath::addOval for intepretation of the index).
robertphillips@google.com466310d2013-12-03 16:43:54 +0000179 *
180 * @return true if this path is an oval.
181 * Tracking whether a path is an oval is considered an
182 * optimization for performance and so some paths that are in
183 * fact ovals can report false.
184 */
bsalomon78d58d12016-05-27 09:17:04 -0700185 bool isOval(SkRect* rect, bool* isCCW, unsigned* start) const {
186 if (fIsOval) {
187 if (rect) {
188 *rect = this->getBounds();
189 }
190 if (isCCW) {
191 *isCCW = SkToBool(fRRectOrOvalIsCCW);
192 }
193 if (start) {
194 *start = fRRectOrOvalStartIdx;
195 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000196 }
197
198 return SkToBool(fIsOval);
199 }
200
bsalomon78d58d12016-05-27 09:17:04 -0700201 bool isRRect(SkRRect* rrect, bool* isCCW, unsigned* start) const {
202 if (fIsRRect) {
203 if (rrect) {
204 *rrect = this->getRRect();
205 }
206 if (isCCW) {
207 *isCCW = SkToBool(fRRectOrOvalIsCCW);
208 }
209 if (start) {
210 *start = fRRectOrOvalStartIdx;
211 }
caryclarkda707bf2015-11-19 14:47:43 -0800212 }
213 return SkToBool(fIsRRect);
214 }
215
216
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000217 bool hasComputedBounds() const {
218 return !fBoundsIsDirty;
219 }
220
221 /** Returns the bounds of the path's points. If the path contains 0 or 1
222 points, the bounds is set to (0,0,0,0), and isEmpty() will return true.
223 Note: this bounds may be larger than the actual shape, since curves
224 do not extend as far as their control points.
225 */
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +0000226 const SkRect& getBounds() const {
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000227 if (fBoundsIsDirty) {
228 this->computeBounds();
229 }
230 return fBounds;
231 }
232
caryclarkda707bf2015-11-19 14:47:43 -0800233 SkRRect getRRect() const;
234
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000235 /**
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000236 * Transforms a path ref by a matrix, allocating a new one only if necessary.
237 */
bungeman6bd52842016-10-27 09:30:08 -0700238 static void CreateTransformedCopy(sk_sp<SkPathRef>* dst,
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000239 const SkPathRef& src,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000240 const SkMatrix& matrix);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000241
commit-bot@chromium.orgfed2ab62014-01-23 15:16:05 +0000242 static SkPathRef* CreateFromBuffer(SkRBuffer* buffer);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000243
244 /**
245 * Rollsback a path ref to zero verbs and points with the assumption that the path ref will be
246 * repopulated with approximately the same number of verbs and points. A new path ref is created
247 * only if necessary.
248 */
bungeman6bd52842016-10-27 09:30:08 -0700249 static void Rewind(sk_sp<SkPathRef>* pathRef);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000250
mtkleinb47cd4b2016-08-09 12:20:04 -0700251 ~SkPathRef();
Brian Osman03776fc2017-08-15 16:08:48 -0400252 int countPoints() const { return fPointCnt; }
253 int countVerbs() const { return fVerbCnt; }
254 int countWeights() const { return fConicWeights.count(); }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000255
256 /**
257 * Returns a pointer one beyond the first logical verb (last verb in memory order).
258 */
Brian Osman03776fc2017-08-15 16:08:48 -0400259 const uint8_t* verbs() const { return fVerbs; }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000260
261 /**
262 * Returns a const pointer to the first verb in memory (which is the last logical verb).
263 */
264 const uint8_t* verbsMemBegin() const { return this->verbs() - fVerbCnt; }
265
266 /**
267 * Returns a const pointer to the first point.
268 */
Brian Osman03776fc2017-08-15 16:08:48 -0400269 const SkPoint* points() const { return fPoints; }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000270
271 /**
272 * Shortcut for this->points() + this->countPoints()
273 */
274 const SkPoint* pointsEnd() const { return this->points() + this->countPoints(); }
275
Brian Osman03776fc2017-08-15 16:08:48 -0400276 const SkScalar* conicWeights() const { return fConicWeights.begin(); }
277 const SkScalar* conicWeightsEnd() const { return fConicWeights.end(); }
reed@google.com277c3f82013-05-31 15:17:50 +0000278
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000279 /**
280 * Convenience methods for getting to a verb or point by index.
281 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000282 uint8_t atVerb(int index) const {
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000283 SkASSERT((unsigned) index < (unsigned) fVerbCnt);
284 return this->verbs()[~index];
285 }
286 const SkPoint& atPoint(int index) const {
287 SkASSERT((unsigned) index < (unsigned) fPointCnt);
288 return this->points()[index];
289 }
290
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000291 bool operator== (const SkPathRef& ref) const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000292
293 /**
294 * Writes the path points and verbs to a buffer.
295 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000296 void writeToBuffer(SkWBuffer* buffer) const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000297
298 /**
299 * Gets the number of bytes that would be written in writeBuffer()
300 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000301 uint32_t writeSize() const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000302
caryclark8e7b19d2016-02-18 04:11:48 -0800303 void interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef* out) const;
304
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000305 /**
306 * Gets an ID that uniquely identifies the contents of the path ref. If two path refs have the
307 * same ID then they have the same verbs and points. However, two path refs may have the same
308 * contents but different genIDs.
309 */
310 uint32_t genID() const;
311
Chris Daltona9441422018-10-25 18:35:55 -0600312 class GenIDChangeListener : public SkRefCnt {
313 public:
314 GenIDChangeListener() : fShouldUnregisterFromPath(false) {}
senorblanco84cd6212015-08-04 10:01:58 -0700315 virtual ~GenIDChangeListener() {}
Chris Daltona9441422018-10-25 18:35:55 -0600316
senorblanco84cd6212015-08-04 10:01:58 -0700317 virtual void onChange() = 0;
Chris Daltona9441422018-10-25 18:35:55 -0600318
319 // The caller can use this method to notify the path that it no longer needs to listen. Once
320 // called, the path will remove this listener from the list at some future point.
321 void markShouldUnregisterFromPath() {
322 fShouldUnregisterFromPath.store(true, std::memory_order_relaxed);
323 }
324 bool shouldUnregisterFromPath() {
325 return fShouldUnregisterFromPath.load(std::memory_order_acquire);
326 }
327
328 private:
329 std::atomic<bool> fShouldUnregisterFromPath;
senorblanco84cd6212015-08-04 10:01:58 -0700330 };
331
Mike Klein0191ed82018-09-17 17:29:39 -0400332 void addGenIDChangeListener(sk_sp<GenIDChangeListener>); // Threadsafe.
senorblanco84cd6212015-08-04 10:01:58 -0700333
Adrienne Walkerad8da8e2017-08-10 12:16:37 -0700334 bool isValid() const;
335 SkDEBUGCODE(void validate() const { SkASSERT(this->isValid()); } )
reed5bcbe912014-12-15 12:28:33 -0800336
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000337private:
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000338 enum SerializationOffsets {
Brian Salomonb7d42e32017-09-21 16:59:17 -0400339 kLegacyRRectOrOvalStartIdx_SerializationShift = 28, // requires 3 bits, ignored.
340 kLegacyRRectOrOvalIsCCW_SerializationShift = 27, // requires 1 bit, ignored.
341 kLegacyIsRRect_SerializationShift = 26, // requires 1 bit, ignored.
342 kIsFinite_SerializationShift = 25, // requires 1 bit
343 kLegacyIsOval_SerializationShift = 24, // requires 1 bit, ignored.
Mike Reeda3d9e212018-02-20 09:48:13 -0500344 kSegmentMask_SerializationShift = 0 // requires 4 bits (deprecated)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000345 };
346
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000347 SkPathRef() {
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000348 fBoundsIsDirty = true; // this also invalidates fIsFinite
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000349 fPointCnt = 0;
350 fVerbCnt = 0;
Ben Wagnera93a14a2017-08-28 10:34:05 -0400351 fVerbs = nullptr;
352 fPoints = nullptr;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000353 fFreeSpace = 0;
354 fGenerationID = kEmptyGenID;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000355 fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000356 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800357 fIsRRect = false;
bsalomon78d58d12016-05-27 09:17:04 -0700358 // The next two values don't matter unless fIsOval or fIsRRect are true.
senorblanco9c1d45d2016-07-22 13:51:42 -0700359 fRRectOrOvalIsCCW = false;
360 fRRectOrOvalStartIdx = 0xAC;
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +0000361 SkDEBUGCODE(fEditorsAttached = 0;)
robertphillips@google.com03087072013-10-02 16:42:21 +0000362 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000363 }
364
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000365 void copy(const SkPathRef& ref, int additionalReserveVerbs, int additionalReservePoints);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000366
Mike Reeda3d9e212018-02-20 09:48:13 -0500367 // Doesn't read fSegmentMask, but (re)computes it from the verbs array
368 unsigned computeSegmentMask() const;
369
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000370 // Return true if the computed bounds are finite.
371 static bool ComputePtBounds(SkRect* bounds, const SkPathRef& ref) {
reed91f283b2015-07-28 06:00:50 -0700372 return bounds->setBoundsCheck(ref.points(), ref.countPoints());
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000373 }
374
375 // called, if dirty, by getBounds()
376 void computeBounds() const {
377 SkDEBUGCODE(this->validate();)
Mike Klein0b4cc892014-07-16 17:18:20 -0400378 // TODO(mtklein): remove fBoundsIsDirty and fIsFinite,
379 // using an inverted rect instead of fBoundsIsDirty and always recalculating fIsFinite.
mtklein5c9c9be2014-12-01 06:59:54 -0800380 SkASSERT(fBoundsIsDirty);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000381
mtklein5c9c9be2014-12-01 06:59:54 -0800382 fIsFinite = ComputePtBounds(&fBounds, *this);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000383 fBoundsIsDirty = false;
384 }
385
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000386 void setBounds(const SkRect& rect) {
387 SkASSERT(rect.fLeft <= rect.fRight && rect.fTop <= rect.fBottom);
388 fBounds = rect;
389 fBoundsIsDirty = false;
mtklein5c9c9be2014-12-01 06:59:54 -0800390 fIsFinite = fBounds.isFinite();
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000391 }
392
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000393 /** Makes additional room but does not change the counts or change the genID */
394 void incReserve(int additionalVerbs, int additionalPoints) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000395 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000396 size_t space = additionalVerbs * sizeof(uint8_t) + additionalPoints * sizeof (SkPoint);
397 this->makeSpace(space);
robertphillips@google.com03087072013-10-02 16:42:21 +0000398 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000399 }
400
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000401 /** Resets the path ref with verbCount verbs and pointCount points, all uninitialized. Also
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000402 * allocates space for reserveVerb additional verbs and reservePoints additional points.*/
reed@google.com277c3f82013-05-31 15:17:50 +0000403 void resetToSize(int verbCount, int pointCount, int conicCount,
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000404 int reserveVerbs = 0, int reservePoints = 0) {
405 SkDEBUGCODE(this->validate();)
406 fBoundsIsDirty = true; // this also invalidates fIsFinite
407 fGenerationID = 0;
408
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000409 fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000410 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800411 fIsRRect = false;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000412
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000413 size_t newSize = sizeof(uint8_t) * verbCount + sizeof(SkPoint) * pointCount;
414 size_t newReserve = sizeof(uint8_t) * reserveVerbs + sizeof(SkPoint) * reservePoints;
415 size_t minSize = newSize + newReserve;
416
417 ptrdiff_t sizeDelta = this->currSize() - minSize;
418
419 if (sizeDelta < 0 || static_cast<size_t>(sizeDelta) >= 3 * minSize) {
420 sk_free(fPoints);
Ben Wagnera93a14a2017-08-28 10:34:05 -0400421 fPoints = nullptr;
422 fVerbs = nullptr;
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000423 fFreeSpace = 0;
424 fVerbCnt = 0;
425 fPointCnt = 0;
Florin Malita3d413c52018-09-11 14:01:42 -0400426 this->makeSpace(minSize, true);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000427 fVerbCnt = verbCount;
428 fPointCnt = pointCount;
429 fFreeSpace -= newSize;
430 } else {
431 fPointCnt = pointCount;
432 fVerbCnt = verbCount;
433 fFreeSpace = this->currSize() - minSize;
434 }
435 fConicWeights.setCount(conicCount);
436 SkDEBUGCODE(this->validate();)
437 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000438
439 /**
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000440 * Increases the verb count by numVbs and point count by the required amount.
441 * The new points are uninitialized. All the new verbs are set to the specified
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000442 * verb. If 'verb' is kConic_Verb, 'weights' will return a pointer to the
443 * uninitialized conic weights.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000444 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000445 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb, int numVbs, SkScalar** weights);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000446
447 /**
448 * Increases the verb count 1, records the new verb, and creates room for the requisite number
449 * of additional points. A pointer to the first point is returned. Any new points are
450 * uninitialized.
451 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000452 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000453
454 /**
455 * Ensures that the free space available in the path ref is >= size. The verb and point counts
Florin Malita3d413c52018-09-11 14:01:42 -0400456 * are not changed. May allocate extra capacity, unless |exact| is true.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000457 */
Florin Malita3d413c52018-09-11 14:01:42 -0400458 void makeSpace(size_t size, bool exact = false) {
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000459 SkDEBUGCODE(this->validate();)
Ben Wagnerac326622017-07-31 16:57:01 -0400460 if (size <= fFreeSpace) {
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000461 return;
462 }
Ben Wagnerac326622017-07-31 16:57:01 -0400463 size_t growSize = size - fFreeSpace;
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000464 size_t oldSize = this->currSize();
Florin Malita3d413c52018-09-11 14:01:42 -0400465
466 if (!exact) {
467 // round to next multiple of 8 bytes
468 growSize = (growSize + 7) & ~static_cast<size_t>(7);
469 // we always at least double the allocation
470 if (growSize < oldSize) {
471 growSize = oldSize;
472 }
473 if (growSize < kMinSize) {
474 growSize = kMinSize;
475 }
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000476 }
Florin Malita3d413c52018-09-11 14:01:42 -0400477
Ben Wagnerac326622017-07-31 16:57:01 -0400478 constexpr size_t maxSize = std::numeric_limits<size_t>::max();
479 size_t newSize;
480 if (growSize <= maxSize - oldSize) {
481 newSize = oldSize + growSize;
482 } else {
483 SK_ABORT("Path too big.");
484 }
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000485 // Note that realloc could memcpy more than we need. It seems to be a win anyway. TODO:
486 // encapsulate this.
487 fPoints = reinterpret_cast<SkPoint*>(sk_realloc_throw(fPoints, newSize));
488 size_t oldVerbSize = fVerbCnt * sizeof(uint8_t);
Ben Wagnerac326622017-07-31 16:57:01 -0400489 void* newVerbsDst = SkTAddOffset<void>(fPoints, newSize - oldVerbSize);
490 void* oldVerbsSrc = SkTAddOffset<void>(fPoints, oldSize - oldVerbSize);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000491 memmove(newVerbsDst, oldVerbsSrc, oldVerbSize);
Ben Wagnerac326622017-07-31 16:57:01 -0400492 fVerbs = SkTAddOffset<uint8_t>(fPoints, newSize);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000493 fFreeSpace += growSize;
494 SkDEBUGCODE(this->validate();)
495 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000496
497 /**
498 * Private, non-const-ptr version of the public function verbsMemBegin().
499 */
500 uint8_t* verbsMemWritable() {
robertphillips@google.com03087072013-10-02 16:42:21 +0000501 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000502 return fVerbs - fVerbCnt;
503 }
504
505 /**
506 * Gets the total amount of space allocated for verbs, points, and reserve.
507 */
508 size_t currSize() const {
509 return reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints);
510 }
511
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000512 /**
513 * Called the first time someone calls CreateEmpty to actually create the singleton.
514 */
mtklein148ec592014-10-13 13:17:56 -0700515 friend SkPathRef* sk_create_empty_pathref();
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000516
bsalomon78d58d12016-05-27 09:17:04 -0700517 void setIsOval(bool isOval, bool isCCW, unsigned start) {
518 fIsOval = isOval;
519 fRRectOrOvalIsCCW = isCCW;
Kaloyan Donevdfffb392018-03-20 10:38:31 +0200520 fRRectOrOvalStartIdx = SkToU8(start);
bsalomon78d58d12016-05-27 09:17:04 -0700521 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000522
bsalomon78d58d12016-05-27 09:17:04 -0700523 void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
524 fIsRRect = isRRect;
525 fRRectOrOvalIsCCW = isCCW;
Kaloyan Donevdfffb392018-03-20 10:38:31 +0200526 fRRectOrOvalStartIdx = SkToU8(start);
bsalomon78d58d12016-05-27 09:17:04 -0700527 }
caryclarkda707bf2015-11-19 14:47:43 -0800528
529 // called only by the editor. Note that this is not a const function.
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000530 SkPoint* getPoints() {
531 SkDEBUGCODE(this->validate();)
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000532 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800533 fIsRRect = false;
534 return fPoints;
535 }
536
537 const SkPoint* getPoints() const {
538 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000539 return fPoints;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000540 }
541
senorblanco84cd6212015-08-04 10:01:58 -0700542 void callGenIDChangeListeners();
543
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000544 enum {
545 kMinSize = 256,
546 };
547
mtklein5c9c9be2014-12-01 06:59:54 -0800548 mutable SkRect fBounds;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000549
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000550 SkPoint* fPoints; // points to begining of the allocation
551 uint8_t* fVerbs; // points just past the end of the allocation (verbs grow backwards)
552 int fVerbCnt;
553 int fPointCnt;
554 size_t fFreeSpace; // redundant but saves computation
reed@google.com277c3f82013-05-31 15:17:50 +0000555 SkTDArray<SkScalar> fConicWeights;
556
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000557 enum {
558 kEmptyGenID = 1, // GenID reserved for path ref with zero points and zero verbs.
559 };
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000560 mutable uint32_t fGenerationID;
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +0000561 SkDEBUGCODE(int32_t fEditorsAttached;) // assert that only one editor in use at any time.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000562
Greg Danieleb772c02018-10-19 15:00:06 +0000563 SkMutex fGenIDChangeListenersMutex;
564 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are reffed
senorblanco84cd6212015-08-04 10:01:58 -0700565
caryclarkda707bf2015-11-19 14:47:43 -0800566 mutable uint8_t fBoundsIsDirty;
Ben Wagnercee46e52018-06-12 16:30:29 -0400567 mutable bool fIsFinite; // only meaningful if bounds are valid
caryclarkda707bf2015-11-19 14:47:43 -0800568
Ben Wagnercee46e52018-06-12 16:30:29 -0400569 bool fIsOval;
570 bool fIsRRect;
bsalomon78d58d12016-05-27 09:17:04 -0700571 // Both the circle and rrect special cases have a notion of direction and starting point
572 // The next two variables store that information for either.
Ben Wagnercee46e52018-06-12 16:30:29 -0400573 bool fRRectOrOvalIsCCW;
bsalomon78d58d12016-05-27 09:17:04 -0700574 uint8_t fRRectOrOvalStartIdx;
caryclarkda707bf2015-11-19 14:47:43 -0800575 uint8_t fSegmentMask;
576
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000577 friend class PathRefTest_Private;
caryclarkda707bf2015-11-19 14:47:43 -0800578 friend class ForceIsRRect_Private; // unit test isRRect
Florin Malita3d413c52018-09-11 14:01:42 -0400579 friend class SkPath;
Mike Reed07105bb2018-12-01 14:07:49 -0500580 friend class SkPathPriv;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000581};
582
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000583#endif