blob: 844c40a505b1df9e20daa737f121bd60537f7c98 [file] [log] [blame]
robertphillips@google.comca0c8382013-09-26 12:18:23 +00001/*
2 * Copyright 2013 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#include "SkBuffer.h"
mtkleinffa4a922016-05-05 16:05:56 -07009#include "SkOnce.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000010#include "SkPath.h"
11#include "SkPathRef.h"
ajuma0735de62016-02-02 06:14:47 -080012#include <limits>
robertphillips@google.comca0c8382013-09-26 12:18:23 +000013
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000014//////////////////////////////////////////////////////////////////////////////
15SkPathRef::Editor::Editor(SkAutoTUnref<SkPathRef>* pathRef,
16 int incReserveVerbs,
17 int incReservePoints)
18{
19 if ((*pathRef)->unique()) {
20 (*pathRef)->incReserve(incReserveVerbs, incReservePoints);
21 } else {
halcanary385fe4d2015-08-26 13:07:48 -070022 SkPathRef* copy = new SkPathRef;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000023 copy->copy(**pathRef, incReserveVerbs, incReservePoints);
24 pathRef->reset(copy);
25 }
26 fPathRef = *pathRef;
senorblanco84cd6212015-08-04 10:01:58 -070027 fPathRef->callGenIDChangeListeners();
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000028 fPathRef->fGenerationID = 0;
29 SkDEBUGCODE(sk_atomic_inc(&fPathRef->fEditorsAttached);)
robertphillips@google.comca0c8382013-09-26 12:18:23 +000030}
31
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000032//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org709ca752014-01-24 22:38:39 +000033
senorblanco84cd6212015-08-04 10:01:58 -070034SkPathRef::~SkPathRef() {
35 this->callGenIDChangeListeners();
36 SkDEBUGCODE(this->validate();)
37 sk_free(fPoints);
38
halcanary96fcdcc2015-08-27 07:41:13 -070039 SkDEBUGCODE(fPoints = nullptr;)
40 SkDEBUGCODE(fVerbs = nullptr;)
senorblanco84cd6212015-08-04 10:01:58 -070041 SkDEBUGCODE(fVerbCnt = 0x9999999;)
42 SkDEBUGCODE(fPointCnt = 0xAAAAAAA;)
43 SkDEBUGCODE(fPointCnt = 0xBBBBBBB;)
44 SkDEBUGCODE(fGenerationID = 0xEEEEEEEE;)
45 SkDEBUGCODE(fEditorsAttached = 0x7777777;)
46}
47
mtkleinffa4a922016-05-05 16:05:56 -070048static SkPathRef* gEmpty = nullptr;
49
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000050SkPathRef* SkPathRef::CreateEmpty() {
mtkleinffa4a922016-05-05 16:05:56 -070051 static SkOnce once;
52 once([]{
53 gEmpty = new SkPathRef;
54 gEmpty->computeBounds(); // Avoids races later to be the first to do this.
55 });
56 return SkRef(gEmpty);
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000057}
58
bsalomon78d58d12016-05-27 09:17:04 -070059static void transform_dir_and_start(const SkMatrix& matrix, bool isRRect, bool* isCCW,
60 unsigned* start) {
61 int inStart = *start;
62 int rm = 0;
63 if (isRRect) {
64 // Degenerate rrect indices to oval indices and remember the remainder.
65 // Ovals have one index per side whereas rrects have two.
66 rm = inStart & 0b1;
67 inStart /= 2;
68 }
69 // Is the antidiagonal non-zero (otherwise the diagonal is zero)
70 int antiDiag;
71 // Is the non-zero value in the top row (either kMScaleX or kMSkewX) negative
72 int topNeg;
73 // Are the two non-zero diagonal or antidiagonal values the same sign.
74 int sameSign;
75 if (matrix.get(SkMatrix::kMScaleX) != 0) {
76 antiDiag = 0b00;
77 if (matrix.get(SkMatrix::kMScaleX) > 0) {
78 topNeg = 0b00;
79 sameSign = matrix.get(SkMatrix::kMScaleY) > 0 ? 0b01 : 0b00;
80 } else {
81 topNeg = 0b10;
82 sameSign = matrix.get(SkMatrix::kMScaleY) > 0 ? 0b00 : 0b01;
83 }
84 } else {
85 antiDiag = 0b01;
86 if (matrix.get(SkMatrix::kMSkewX) > 0) {
87 topNeg = 0b00;
88 sameSign = matrix.get(SkMatrix::kMSkewY) > 0 ? 0b01 : 0b00;
89 } else {
90 topNeg = 0b10;
91 sameSign = matrix.get(SkMatrix::kMSkewY) > 0 ? 0b00 : 0b01;
92 }
93 }
94 if (sameSign != antiDiag) {
95 // This is a rotation (and maybe scale). The direction is unchanged.
96 // Trust me on the start computation (or draw yourself some pictures)
97 *start = (inStart + 4 - (topNeg | antiDiag)) % 4;
98 SkASSERT(*start < 4);
99 if (isRRect) {
100 *start = 2 * *start + rm;
101 }
102 } else {
103 // This is a mirror (and maybe scale). The direction is reversed.
104 *isCCW = !*isCCW;
105 // Trust me on the start computation (or draw yourself some pictures)
106 *start = (6 + (topNeg | antiDiag) - inStart) % 4;
107 SkASSERT(*start < 4);
108 if (isRRect) {
109 *start = 2 * *start + (rm ? 0 : 1);
110 }
111 }
112}
113
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000114void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst,
115 const SkPathRef& src,
116 const SkMatrix& matrix) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000117 SkDEBUGCODE(src.validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000118 if (matrix.isIdentity()) {
119 if (*dst != &src) {
120 src.ref();
121 dst->reset(const_cast<SkPathRef*>(&src));
robertphillips@google.com03087072013-10-02 16:42:21 +0000122 SkDEBUGCODE((*dst)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000123 }
124 return;
125 }
126
robertphillips@google.comb06e88d2013-12-03 17:15:36 +0000127 if (!(*dst)->unique()) {
halcanary385fe4d2015-08-26 13:07:48 -0700128 dst->reset(new SkPathRef);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +0000129 }
130
131 if (*dst != &src) {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000132 (*dst)->resetToSize(src.fVerbCnt, src.fPointCnt, src.fConicWeights.count());
mtkleincc881da2015-12-08 11:55:17 -0800133 sk_careful_memcpy((*dst)->verbsMemWritable(), src.verbsMemBegin(),
134 src.fVerbCnt * sizeof(uint8_t));
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000135 (*dst)->fConicWeights = src.fConicWeights;
136 }
137
robertphillips@google.comb06e88d2013-12-03 17:15:36 +0000138 SkASSERT((*dst)->countPoints() == src.countPoints());
139 SkASSERT((*dst)->countVerbs() == src.countVerbs());
140 SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count());
141
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000142 // Need to check this here in case (&src == dst)
143 bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1;
144
145 matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt);
146
147 /*
bsalomon78d58d12016-05-27 09:17:04 -0700148 * Here we optimize the bounds computation, by noting if the bounds are
149 * already known, and if so, we just transform those as well and mark
150 * them as "known", rather than force the transformed path to have to
151 * recompute them.
152 *
153 * Special gotchas if the path is effectively empty (<= 1 point) or
154 * if it is non-finite. In those cases bounds need to stay empty,
155 * regardless of the matrix.
156 */
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000157 if (canXformBounds) {
158 (*dst)->fBoundsIsDirty = false;
159 if (src.fIsFinite) {
mtklein5c9c9be2014-12-01 06:59:54 -0800160 matrix.mapRect(&(*dst)->fBounds, src.fBounds);
161 if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) {
162 (*dst)->fBounds.setEmpty();
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000163 }
164 } else {
165 (*dst)->fIsFinite = false;
mtklein5c9c9be2014-12-01 06:59:54 -0800166 (*dst)->fBounds.setEmpty();
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000167 }
168 } else {
169 (*dst)->fBoundsIsDirty = true;
170 }
171
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000172 (*dst)->fSegmentMask = src.fSegmentMask;
173
robertphillips@google.com466310d2013-12-03 16:43:54 +0000174 // It's an oval only if it stays a rect.
caryclarkda707bf2015-11-19 14:47:43 -0800175 bool rectStaysRect = matrix.rectStaysRect();
176 (*dst)->fIsOval = src.fIsOval && rectStaysRect;
177 (*dst)->fIsRRect = src.fIsRRect && rectStaysRect;
bsalomon78d58d12016-05-27 09:17:04 -0700178 if ((*dst)->fIsOval || (*dst)->fIsRRect) {
179 unsigned start = src.fRRectOrOvalStartIdx;
180 bool isCCW = SkToBool(src.fRRectOrOvalIsCCW);
181 transform_dir_and_start(matrix, (*dst)->fIsRRect, &isCCW, &start);
182 (*dst)->fRRectOrOvalIsCCW = isCCW;
183 (*dst)->fRRectOrOvalStartIdx = start;
184 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000185
robertphillips@google.com03087072013-10-02 16:42:21 +0000186 SkDEBUGCODE((*dst)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000187}
188
commit-bot@chromium.orgfed2ab62014-01-23 15:16:05 +0000189SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer) {
halcanary385fe4d2015-08-26 13:07:48 -0700190 SkPathRef* ref = new SkPathRef;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000191
192 int32_t packed;
193 if (!buffer->readS32(&packed)) {
halcanary385fe4d2015-08-26 13:07:48 -0700194 delete ref;
halcanary96fcdcc2015-08-27 07:41:13 -0700195 return nullptr;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000196 }
197
198 ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1;
caryclarkda707bf2015-11-19 14:47:43 -0800199 uint8_t segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF;
200 bool isOval = (packed >> kIsOval_SerializationShift) & 1;
201 bool isRRect = (packed >> kIsRRect_SerializationShift) & 1;
bsalomon78d58d12016-05-27 09:17:04 -0700202 bool rrectOrOvalIsCCW = (packed >> kRRectOrOvalIsCCW_SerializationShift) & 1;
203 unsigned rrectOrOvalStartIdx = (packed >> kRRectOrOvalStartIdx_SerializationShift) & 0x7;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000204
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000205 int32_t verbCount, pointCount, conicCount;
ajuma0735de62016-02-02 06:14:47 -0800206 ptrdiff_t maxPtrDiff = std::numeric_limits<ptrdiff_t>::max();
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000207 if (!buffer->readU32(&(ref->fGenerationID)) ||
208 !buffer->readS32(&verbCount) ||
ajumaf8aec582016-01-13 13:46:31 -0800209 verbCount < 0 ||
ajuma0735de62016-02-02 06:14:47 -0800210 static_cast<uint32_t>(verbCount) > maxPtrDiff/sizeof(uint8_t) ||
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000211 !buffer->readS32(&pointCount) ||
ajumaf8aec582016-01-13 13:46:31 -0800212 pointCount < 0 ||
ajuma0735de62016-02-02 06:14:47 -0800213 static_cast<uint32_t>(pointCount) > maxPtrDiff/sizeof(SkPoint) ||
214 sizeof(uint8_t) * verbCount + sizeof(SkPoint) * pointCount >
215 static_cast<size_t>(maxPtrDiff) ||
ajumaf8aec582016-01-13 13:46:31 -0800216 !buffer->readS32(&conicCount) ||
217 conicCount < 0) {
halcanary385fe4d2015-08-26 13:07:48 -0700218 delete ref;
halcanary96fcdcc2015-08-27 07:41:13 -0700219 return nullptr;
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000220 }
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000221
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000222 ref->resetToSize(verbCount, pointCount, conicCount);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000223 SkASSERT(verbCount == ref->countVerbs());
224 SkASSERT(pointCount == ref->countPoints());
225 SkASSERT(conicCount == ref->fConicWeights.count());
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000226
227 if (!buffer->read(ref->verbsMemWritable(), verbCount * sizeof(uint8_t)) ||
228 !buffer->read(ref->fPoints, pointCount * sizeof(SkPoint)) ||
229 !buffer->read(ref->fConicWeights.begin(), conicCount * sizeof(SkScalar)) ||
230 !buffer->read(&ref->fBounds, sizeof(SkRect))) {
halcanary385fe4d2015-08-26 13:07:48 -0700231 delete ref;
halcanary96fcdcc2015-08-27 07:41:13 -0700232 return nullptr;
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000233 }
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000234 ref->fBoundsIsDirty = false;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000235
236 // resetToSize clears fSegmentMask and fIsOval
237 ref->fSegmentMask = segmentMask;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000238 ref->fIsOval = isOval;
caryclarkda707bf2015-11-19 14:47:43 -0800239 ref->fIsRRect = isRRect;
bsalomon78d58d12016-05-27 09:17:04 -0700240 ref->fRRectOrOvalIsCCW = rrectOrOvalIsCCW;
241 ref->fRRectOrOvalStartIdx = rrectOrOvalStartIdx;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000242 return ref;
243}
244
245void SkPathRef::Rewind(SkAutoTUnref<SkPathRef>* pathRef) {
246 if ((*pathRef)->unique()) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000247 SkDEBUGCODE((*pathRef)->validate();)
senorblanco84cd6212015-08-04 10:01:58 -0700248 (*pathRef)->callGenIDChangeListeners();
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000249 (*pathRef)->fBoundsIsDirty = true; // this also invalidates fIsFinite
250 (*pathRef)->fVerbCnt = 0;
251 (*pathRef)->fPointCnt = 0;
252 (*pathRef)->fFreeSpace = (*pathRef)->currSize();
253 (*pathRef)->fGenerationID = 0;
254 (*pathRef)->fConicWeights.rewind();
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000255 (*pathRef)->fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000256 (*pathRef)->fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800257 (*pathRef)->fIsRRect = false;
robertphillips@google.com03087072013-10-02 16:42:21 +0000258 SkDEBUGCODE((*pathRef)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000259 } else {
260 int oldVCnt = (*pathRef)->countVerbs();
261 int oldPCnt = (*pathRef)->countPoints();
halcanary385fe4d2015-08-26 13:07:48 -0700262 pathRef->reset(new SkPathRef);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000263 (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt);
264 }
265}
266
267bool SkPathRef::operator== (const SkPathRef& ref) const {
robertphillips@google.com03087072013-10-02 16:42:21 +0000268 SkDEBUGCODE(this->validate();)
269 SkDEBUGCODE(ref.validate();)
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000270
271 // We explicitly check fSegmentMask as a quick-reject. We could skip it,
272 // since it is only a cache of info in the fVerbs, but its a fast way to
273 // notice a difference
274 if (fSegmentMask != ref.fSegmentMask) {
275 return false;
276 }
277
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000278 bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID;
279#ifdef SK_RELEASE
280 if (genIDMatch) {
281 return true;
282 }
283#endif
284 if (fPointCnt != ref.fPointCnt ||
285 fVerbCnt != ref.fVerbCnt) {
286 SkASSERT(!genIDMatch);
287 return false;
288 }
mtkleind4897592014-11-14 09:22:40 -0800289 if (0 == ref.fVerbCnt) {
290 SkASSERT(0 == ref.fPointCnt);
291 return true;
292 }
293 SkASSERT(this->verbsMemBegin() && ref.verbsMemBegin());
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000294 if (0 != memcmp(this->verbsMemBegin(),
295 ref.verbsMemBegin(),
296 ref.fVerbCnt * sizeof(uint8_t))) {
297 SkASSERT(!genIDMatch);
298 return false;
299 }
mtkleind4897592014-11-14 09:22:40 -0800300 SkASSERT(this->points() && ref.points());
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000301 if (0 != memcmp(this->points(),
302 ref.points(),
303 ref.fPointCnt * sizeof(SkPoint))) {
304 SkASSERT(!genIDMatch);
305 return false;
306 }
307 if (fConicWeights != ref.fConicWeights) {
308 SkASSERT(!genIDMatch);
309 return false;
310 }
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000311 return true;
312}
313
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000314void SkPathRef::writeToBuffer(SkWBuffer* buffer) const {
robertphillips@google.com03087072013-10-02 16:42:21 +0000315 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000316 SkDEBUGCODE(size_t beforePos = buffer->pos();)
317
318 // Call getBounds() to ensure (as a side-effect) that fBounds
319 // and fIsFinite are computed.
320 const SkRect& bounds = this->getBounds();
321
bsalomon78d58d12016-05-27 09:17:04 -0700322 int32_t packed = ((fRRectOrOvalStartIdx & 7) << kRRectOrOvalStartIdx_SerializationShift) |
323 ((fRRectOrOvalIsCCW & 1) << kRRectOrOvalIsCCW_SerializationShift) |
324 ((fIsFinite & 1) << kIsFinite_SerializationShift) |
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000325 ((fIsOval & 1) << kIsOval_SerializationShift) |
caryclarkda707bf2015-11-19 14:47:43 -0800326 ((fIsRRect & 1) << kIsRRect_SerializationShift) |
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000327 (fSegmentMask << kSegmentMask_SerializationShift);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000328 buffer->write32(packed);
329
330 // TODO: write gen ID here. Problem: We don't know if we're cross process or not from
331 // SkWBuffer. Until this is fixed we write 0.
332 buffer->write32(0);
333 buffer->write32(fVerbCnt);
334 buffer->write32(fPointCnt);
335 buffer->write32(fConicWeights.count());
336 buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t));
337 buffer->write(fPoints, fPointCnt * sizeof(SkPoint));
338 buffer->write(fConicWeights.begin(), fConicWeights.bytes());
339 buffer->write(&bounds, sizeof(bounds));
340
341 SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize());
342}
343
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000344uint32_t SkPathRef::writeSize() const {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000345 return uint32_t(5 * sizeof(uint32_t) +
346 fVerbCnt * sizeof(uint8_t) +
347 fPointCnt * sizeof(SkPoint) +
348 fConicWeights.bytes() +
349 sizeof(SkRect));
350}
351
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000352void SkPathRef::copy(const SkPathRef& ref,
353 int additionalReserveVerbs,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000354 int additionalReservePoints) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000355 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000356 this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(),
357 additionalReserveVerbs, additionalReservePoints);
mtkleincc881da2015-12-08 11:55:17 -0800358 sk_careful_memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt*sizeof(uint8_t));
359 sk_careful_memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint));
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000360 fConicWeights = ref.fConicWeights;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000361 fBoundsIsDirty = ref.fBoundsIsDirty;
362 if (!fBoundsIsDirty) {
363 fBounds = ref.fBounds;
364 fIsFinite = ref.fIsFinite;
365 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000366 fSegmentMask = ref.fSegmentMask;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000367 fIsOval = ref.fIsOval;
caryclarkda707bf2015-11-19 14:47:43 -0800368 fIsRRect = ref.fIsRRect;
bsalomon78d58d12016-05-27 09:17:04 -0700369 fRRectOrOvalIsCCW = ref.fRRectOrOvalIsCCW;
370 fRRectOrOvalStartIdx = ref.fRRectOrOvalStartIdx;
robertphillips@google.com03087072013-10-02 16:42:21 +0000371 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000372}
373
caryclark8e7b19d2016-02-18 04:11:48 -0800374
375void SkPathRef::interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef* out) const {
376 const SkScalar* inValues = &ending.getPoints()->fX;
377 SkScalar* outValues = &out->getPoints()->fX;
378 int count = out->countPoints() * 2;
379 for (int index = 0; index < count; ++index) {
380 outValues[index] = outValues[index] * weight + inValues[index] * (1 - weight);
381 }
382 out->fBoundsIsDirty = true;
383 out->fIsOval = false;
384 out->fIsRRect = false;
385}
386
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000387SkPoint* SkPathRef::growForRepeatedVerb(int /*SkPath::Verb*/ verb,
388 int numVbs,
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000389 SkScalar** weights) {
390 // This value is just made-up for now. When count is 4, calling memset was much
391 // slower than just writing the loop. This seems odd, and hopefully in the
392 // future this will appear to have been a fluke...
393 static const unsigned int kMIN_COUNT_FOR_MEMSET_TO_BE_FAST = 16;
394
395 SkDEBUGCODE(this->validate();)
396 int pCnt;
397 bool dirtyAfterEdit = true;
398 switch (verb) {
399 case SkPath::kMove_Verb:
400 pCnt = numVbs;
401 dirtyAfterEdit = false;
402 break;
403 case SkPath::kLine_Verb:
404 fSegmentMask |= SkPath::kLine_SegmentMask;
405 pCnt = numVbs;
406 break;
407 case SkPath::kQuad_Verb:
408 fSegmentMask |= SkPath::kQuad_SegmentMask;
409 pCnt = 2 * numVbs;
410 break;
411 case SkPath::kConic_Verb:
412 fSegmentMask |= SkPath::kConic_SegmentMask;
413 pCnt = 2 * numVbs;
414 break;
415 case SkPath::kCubic_Verb:
416 fSegmentMask |= SkPath::kCubic_SegmentMask;
417 pCnt = 3 * numVbs;
418 break;
419 case SkPath::kClose_Verb:
420 SkDEBUGFAIL("growForRepeatedVerb called for kClose_Verb");
421 pCnt = 0;
422 dirtyAfterEdit = false;
423 break;
424 case SkPath::kDone_Verb:
425 SkDEBUGFAIL("growForRepeatedVerb called for kDone");
426 // fall through
427 default:
428 SkDEBUGFAIL("default should not be reached");
429 pCnt = 0;
430 dirtyAfterEdit = false;
431 }
432
433 size_t space = numVbs * sizeof(uint8_t) + pCnt * sizeof (SkPoint);
434 this->makeSpace(space);
435
436 SkPoint* ret = fPoints + fPointCnt;
437 uint8_t* vb = fVerbs - fVerbCnt;
438
439 // cast to unsigned, so if kMIN_COUNT_FOR_MEMSET_TO_BE_FAST is defined to
440 // be 0, the compiler will remove the test/branch entirely.
441 if ((unsigned)numVbs >= kMIN_COUNT_FOR_MEMSET_TO_BE_FAST) {
442 memset(vb - numVbs, verb, numVbs);
443 } else {
444 for (int i = 0; i < numVbs; ++i) {
445 vb[~i] = verb;
446 }
447 }
448
449 fVerbCnt += numVbs;
450 fPointCnt += pCnt;
451 fFreeSpace -= space;
452 fBoundsIsDirty = true; // this also invalidates fIsFinite
453 if (dirtyAfterEdit) {
454 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800455 fIsRRect = false;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000456 }
457
458 if (SkPath::kConic_Verb == verb) {
bsalomon49f085d2014-09-05 13:34:00 -0700459 SkASSERT(weights);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000460 *weights = fConicWeights.append(numVbs);
461 }
462
463 SkDEBUGCODE(this->validate();)
464 return ret;
465}
466
467SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb, SkScalar weight) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000468 SkDEBUGCODE(this->validate();)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000469 int pCnt;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000470 bool dirtyAfterEdit = true;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000471 switch (verb) {
472 case SkPath::kMove_Verb:
473 pCnt = 1;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000474 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000475 break;
476 case SkPath::kLine_Verb:
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000477 fSegmentMask |= SkPath::kLine_SegmentMask;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000478 pCnt = 1;
479 break;
480 case SkPath::kQuad_Verb:
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000481 fSegmentMask |= SkPath::kQuad_SegmentMask;
482 pCnt = 2;
483 break;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000484 case SkPath::kConic_Verb:
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000485 fSegmentMask |= SkPath::kConic_SegmentMask;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000486 pCnt = 2;
487 break;
488 case SkPath::kCubic_Verb:
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000489 fSegmentMask |= SkPath::kCubic_SegmentMask;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000490 pCnt = 3;
491 break;
492 case SkPath::kClose_Verb:
493 pCnt = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000494 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000495 break;
496 case SkPath::kDone_Verb:
497 SkDEBUGFAIL("growForVerb called for kDone");
498 // fall through
499 default:
500 SkDEBUGFAIL("default is not reached");
robertphillips@google.com466310d2013-12-03 16:43:54 +0000501 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000502 pCnt = 0;
503 }
504 size_t space = sizeof(uint8_t) + pCnt * sizeof (SkPoint);
505 this->makeSpace(space);
506 this->fVerbs[~fVerbCnt] = verb;
507 SkPoint* ret = fPoints + fPointCnt;
508 fVerbCnt += 1;
509 fPointCnt += pCnt;
510 fFreeSpace -= space;
511 fBoundsIsDirty = true; // this also invalidates fIsFinite
robertphillips@google.com466310d2013-12-03 16:43:54 +0000512 if (dirtyAfterEdit) {
513 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800514 fIsRRect = false;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000515 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000516
517 if (SkPath::kConic_Verb == verb) {
518 *fConicWeights.append() = weight;
519 }
520
robertphillips@google.com03087072013-10-02 16:42:21 +0000521 SkDEBUGCODE(this->validate();)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000522 return ret;
523}
524
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000525uint32_t SkPathRef::genID() const {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000526 SkASSERT(!fEditorsAttached);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000527 static const uint32_t kMask = (static_cast<int64_t>(1) << SkPath::kPathRefGenIDBitCnt) - 1;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000528 if (!fGenerationID) {
529 if (0 == fPointCnt && 0 == fVerbCnt) {
530 fGenerationID = kEmptyGenID;
531 } else {
532 static int32_t gPathRefGenerationID;
533 // do a loop in case our global wraps around, as we never want to return a 0 or the
534 // empty ID
535 do {
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000536 fGenerationID = (sk_atomic_inc(&gPathRefGenerationID) + 1) & kMask;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000537 } while (fGenerationID <= kEmptyGenID);
538 }
539 }
540 return fGenerationID;
541}
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000542
senorblanco84cd6212015-08-04 10:01:58 -0700543void SkPathRef::addGenIDChangeListener(GenIDChangeListener* listener) {
mtkleinffa4a922016-05-05 16:05:56 -0700544 if (nullptr == listener || this == gEmpty) {
halcanary385fe4d2015-08-26 13:07:48 -0700545 delete listener;
senorblanco84cd6212015-08-04 10:01:58 -0700546 return;
547 }
548 *fGenIDChangeListeners.append() = listener;
549}
550
551// we need to be called *before* the genID gets changed or zerod
552void SkPathRef::callGenIDChangeListeners() {
553 for (int i = 0; i < fGenIDChangeListeners.count(); i++) {
554 fGenIDChangeListeners[i]->onChange();
555 }
556
557 // Listeners get at most one shot, so whether these triggered or not, blow them away.
558 fGenIDChangeListeners.deleteAll();
559}
560
caryclarkda707bf2015-11-19 14:47:43 -0800561SkRRect SkPathRef::getRRect() const {
562 const SkRect& bounds = this->getBounds();
563 SkVector radii[4] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
564 Iter iter(*this);
565 SkPoint pts[4];
566 uint8_t verb = iter.next(pts);
567 SkASSERT(SkPath::kMove_Verb == verb);
568 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
569 if (SkPath::kConic_Verb == verb) {
570 SkVector v1_0 = pts[1] - pts[0];
571 SkVector v2_1 = pts[2] - pts[1];
572 SkVector dxdy;
573 if (v1_0.fX) {
574 SkASSERT(!v2_1.fX && !v1_0.fY);
575 dxdy.set(SkScalarAbs(v1_0.fX), SkScalarAbs(v2_1.fY));
576 } else if (!v1_0.fY) {
577 SkASSERT(!v2_1.fX || !v2_1.fY);
578 dxdy.set(SkScalarAbs(v2_1.fX), SkScalarAbs(v2_1.fY));
579 } else {
580 SkASSERT(!v2_1.fY);
581 dxdy.set(SkScalarAbs(v2_1.fX), SkScalarAbs(v1_0.fY));
582 }
583 SkRRect::Corner corner =
584 pts[1].fX == bounds.fLeft ?
585 pts[1].fY == bounds.fTop ?
586 SkRRect::kUpperLeft_Corner : SkRRect::kLowerLeft_Corner :
587 pts[1].fY == bounds.fTop ?
588 SkRRect::kUpperRight_Corner : SkRRect::kLowerRight_Corner;
589 SkASSERT(!radii[corner].fX && !radii[corner].fY);
590 radii[corner] = dxdy;
591 } else {
592 SkASSERT((verb == SkPath::kLine_Verb
593 && (!(pts[1].fX - pts[0].fX) || !(pts[1].fY - pts[0].fY)))
594 || verb == SkPath::kClose_Verb);
595 }
596 }
597 SkRRect rrect;
598 rrect.setRectRadii(bounds, radii);
599 return rrect;
600}
601
602///////////////////////////////////////////////////////////////////////////////
603
604SkPathRef::Iter::Iter() {
605#ifdef SK_DEBUG
606 fPts = nullptr;
607 fConicWeights = nullptr;
608#endif
609 // need to init enough to make next() harmlessly return kDone_Verb
610 fVerbs = nullptr;
611 fVerbStop = nullptr;
612}
613
614SkPathRef::Iter::Iter(const SkPathRef& path) {
615 this->setPathRef(path);
616}
617
618void SkPathRef::Iter::setPathRef(const SkPathRef& path) {
619 fPts = path.points();
620 fVerbs = path.verbs();
621 fVerbStop = path.verbsMemBegin();
caryclark69424422016-10-04 13:06:17 -0700622 fConicWeights = path.conicWeights();
623 if (fConicWeights) {
624 fConicWeights -= 1; // begin one behind
625 }
caryclarkda707bf2015-11-19 14:47:43 -0800626}
627
628uint8_t SkPathRef::Iter::next(SkPoint pts[4]) {
629 SkASSERT(pts);
630 if (fVerbs == fVerbStop) {
631 return (uint8_t) SkPath::kDone_Verb;
632 }
633
634 // fVerbs points one beyond next verb so decrement first.
635 unsigned verb = *(--fVerbs);
636 const SkPoint* srcPts = fPts;
637
638 switch (verb) {
639 case SkPath::kMove_Verb:
640 pts[0] = srcPts[0];
641 srcPts += 1;
642 break;
643 case SkPath::kLine_Verb:
644 pts[0] = srcPts[-1];
645 pts[1] = srcPts[0];
646 srcPts += 1;
647 break;
648 case SkPath::kConic_Verb:
649 fConicWeights += 1;
650 // fall-through
651 case SkPath::kQuad_Verb:
652 pts[0] = srcPts[-1];
653 pts[1] = srcPts[0];
654 pts[2] = srcPts[1];
655 srcPts += 2;
656 break;
657 case SkPath::kCubic_Verb:
658 pts[0] = srcPts[-1];
659 pts[1] = srcPts[0];
660 pts[2] = srcPts[1];
661 pts[3] = srcPts[2];
662 srcPts += 3;
663 break;
664 case SkPath::kClose_Verb:
665 break;
666 case SkPath::kDone_Verb:
667 SkASSERT(fVerbs == fVerbStop);
668 break;
669 }
670 fPts = srcPts;
671 return (uint8_t) verb;
672}
673
caryclark2028d7f2015-12-09 14:04:46 -0800674uint8_t SkPathRef::Iter::peek() const {
675 const uint8_t* next = fVerbs - 1;
676 return next <= fVerbStop ? (uint8_t) SkPath::kDone_Verb : *next;
677}
678
robertphillips@google.com03087072013-10-02 16:42:21 +0000679#ifdef SK_DEBUG
caryclark0c52b172016-10-05 12:16:52 -0700680
681#include "SkNx.h"
682
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000683void SkPathRef::validate() const {
684 SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0);
685 SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints) >= 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700686 SkASSERT((nullptr == fPoints) == (nullptr == fVerbs));
687 SkASSERT(!(nullptr == fPoints && 0 != fFreeSpace));
688 SkASSERT(!(nullptr == fPoints && 0 != fFreeSpace));
689 SkASSERT(!(nullptr == fPoints && fPointCnt));
690 SkASSERT(!(nullptr == fVerbs && fVerbCnt));
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000691 SkASSERT(this->currSize() ==
692 fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000693
bsalomon78d58d12016-05-27 09:17:04 -0700694 if (fIsOval || fIsRRect) {
695 // Currently we don't allow both of these to be set, even though ovals are round rects.
696 SkASSERT(fIsOval != fIsRRect);
697 if (fIsOval) {
698 SkASSERT(fRRectOrOvalStartIdx < 4);
699 } else {
700 SkASSERT(fRRectOrOvalStartIdx < 8);
701 }
702 }
703
mtklein5c9c9be2014-12-01 06:59:54 -0800704 if (!fBoundsIsDirty && !fBounds.isEmpty()) {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000705 bool isFinite = true;
caryclark0c52b172016-10-05 12:16:52 -0700706 Sk2s leftTop = Sk2s(fBounds.fLeft, fBounds.fTop);
707 Sk2s rightBot = Sk2s(fBounds.fRight, fBounds.fBottom);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000708 for (int i = 0; i < fPointCnt; ++i) {
caryclark0c52b172016-10-05 12:16:52 -0700709 Sk2s point = Sk2s(fPoints[i].fX, fPoints[i].fY);
robertphillipsf1cdead2015-01-05 09:20:04 -0800710#ifdef SK_DEBUG
711 if (fPoints[i].isFinite() &&
caryclark0c52b172016-10-05 12:16:52 -0700712 ((point < leftTop).anyTrue() || (point > rightBot).anyTrue())) {
robertphillipsf1cdead2015-01-05 09:20:04 -0800713 SkDebugf("bounds: %f %f %f %f\n",
714 fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom);
715 for (int j = 0; j < fPointCnt; ++j) {
716 if (i == j) {
717 SkDebugf("*");
718 }
719 SkDebugf("%f %f\n", fPoints[j].fX, fPoints[j].fY);
720 }
721 }
722#endif
723
robertphillips0e912462014-12-12 12:47:59 -0800724 SkASSERT(!fPoints[i].isFinite() ||
caryclark0c52b172016-10-05 12:16:52 -0700725 (!(point < leftTop).anyTrue() && !(point > rightBot).anyTrue()));
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000726 if (!fPoints[i].isFinite()) {
727 isFinite = false;
728 }
729 }
730 SkASSERT(SkToBool(fIsFinite) == isFinite);
731 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000732
733#ifdef SK_DEBUG_PATH
734 uint32_t mask = 0;
735 for (int i = 0; i < fVerbCnt; ++i) {
736 switch (fVerbs[~i]) {
737 case SkPath::kMove_Verb:
738 break;
739 case SkPath::kLine_Verb:
740 mask |= SkPath::kLine_SegmentMask;
741 break;
742 case SkPath::kQuad_Verb:
743 mask |= SkPath::kQuad_SegmentMask;
744 break;
745 case SkPath::kConic_Verb:
746 mask |= SkPath::kConic_SegmentMask;
747 break;
748 case SkPath::kCubic_Verb:
749 mask |= SkPath::kCubic_SegmentMask;
750 break;
751 case SkPath::kClose_Verb:
752 break;
753 case SkPath::kDone_Verb:
754 SkDEBUGFAIL("Done verb shouldn't be recorded.");
755 break;
756 default:
757 SkDEBUGFAIL("Unknown Verb");
758 break;
759 }
760 }
761 SkASSERT(mask == fSegmentMask);
762#endif // SK_DEBUG_PATH
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000763}
robertphillips@google.com03087072013-10-02 16:42:21 +0000764#endif