blob: a02df3024e796e59fe012e82c282bb0591a9e0e7 [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"
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +00009#include "SkOnce.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000010#include "SkPath.h"
11#include "SkPathRef.h"
12
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000013//////////////////////////////////////////////////////////////////////////////
14SkPathRef::Editor::Editor(SkAutoTUnref<SkPathRef>* pathRef,
15 int incReserveVerbs,
16 int incReservePoints)
17{
18 if ((*pathRef)->unique()) {
19 (*pathRef)->incReserve(incReserveVerbs, incReservePoints);
20 } else {
21 SkPathRef* copy = SkNEW(SkPathRef);
22 copy->copy(**pathRef, incReserveVerbs, incReservePoints);
23 pathRef->reset(copy);
24 }
25 fPathRef = *pathRef;
26 fPathRef->fGenerationID = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +000027 fPathRef->fIsOval = false;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000028 SkDEBUGCODE(sk_atomic_inc(&fPathRef->fEditorsAttached);)
robertphillips@google.comca0c8382013-09-26 12:18:23 +000029}
30
31SkPoint* SkPathRef::Editor::growForConic(SkScalar w) {
robertphillips@google.com03087072013-10-02 16:42:21 +000032 SkDEBUGCODE(fPathRef->validate();)
robertphillips@google.comca0c8382013-09-26 12:18:23 +000033 SkPoint* pts = fPathRef->growForVerb(SkPath::kConic_Verb);
34 *fPathRef->fConicWeights.append() = w;
35 return pts;
36}
37
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000038//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000039void SkPathRef::CreateEmptyImpl(SkPathRef** empty) {
40 *empty = SkNEW(SkPathRef);
41 (*empty)->computeBounds(); // Preemptively avoid a race to clear fBoundsIsDirty.
42}
43
44SkPathRef* SkPathRef::CreateEmpty() {
45 static SkPathRef* gEmptyPathRef;
46 SK_DECLARE_STATIC_ONCE(once);
47 SkOnce(&once, SkPathRef::CreateEmptyImpl, &gEmptyPathRef);
48 return SkRef(gEmptyPathRef);
49}
50
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000051void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst,
52 const SkPathRef& src,
53 const SkMatrix& matrix) {
robertphillips@google.com03087072013-10-02 16:42:21 +000054 SkDEBUGCODE(src.validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000055 if (matrix.isIdentity()) {
56 if (*dst != &src) {
57 src.ref();
58 dst->reset(const_cast<SkPathRef*>(&src));
robertphillips@google.com03087072013-10-02 16:42:21 +000059 SkDEBUGCODE((*dst)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000060 }
61 return;
62 }
63
robertphillips@google.comb06e88d2013-12-03 17:15:36 +000064 if (!(*dst)->unique()) {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000065 dst->reset(SkNEW(SkPathRef));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +000066 }
67
68 if (*dst != &src) {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000069 (*dst)->resetToSize(src.fVerbCnt, src.fPointCnt, src.fConicWeights.count());
70 memcpy((*dst)->verbsMemWritable(), src.verbsMemBegin(), src.fVerbCnt * sizeof(uint8_t));
71 (*dst)->fConicWeights = src.fConicWeights;
72 }
73
robertphillips@google.comb06e88d2013-12-03 17:15:36 +000074 SkASSERT((*dst)->countPoints() == src.countPoints());
75 SkASSERT((*dst)->countVerbs() == src.countVerbs());
76 SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count());
77
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000078 // Need to check this here in case (&src == dst)
79 bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1;
80
81 matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt);
82
83 /*
84 * Here we optimize the bounds computation, by noting if the bounds are
85 * already known, and if so, we just transform those as well and mark
86 * them as "known", rather than force the transformed path to have to
87 * recompute them.
88 *
89 * Special gotchas if the path is effectively empty (<= 1 point) or
90 * if it is non-finite. In those cases bounds need to stay empty,
91 * regardless of the matrix.
92 */
93 if (canXformBounds) {
94 (*dst)->fBoundsIsDirty = false;
95 if (src.fIsFinite) {
96 matrix.mapRect(&(*dst)->fBounds, src.fBounds);
97 if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) {
98 (*dst)->fBounds.setEmpty();
99 }
100 } else {
101 (*dst)->fIsFinite = false;
102 (*dst)->fBounds.setEmpty();
103 }
104 } else {
105 (*dst)->fBoundsIsDirty = true;
106 }
107
robertphillips@google.com466310d2013-12-03 16:43:54 +0000108 // It's an oval only if it stays a rect.
109 (*dst)->fIsOval = src.fIsOval && matrix.rectStaysRect();
110
robertphillips@google.com03087072013-10-02 16:42:21 +0000111 SkDEBUGCODE((*dst)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000112}
113
114SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer
robertphillips@google.com466310d2013-12-03 16:43:54 +0000115#ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V16_AND_ALL_OTHER_INSTANCES_TOO
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000116 , bool newFormat, int32_t oldPacked
117#endif
118 ) {
119 SkPathRef* ref = SkNEW(SkPathRef);
robertphillips@google.com466310d2013-12-03 16:43:54 +0000120 bool isOval;
121
122 int32_t packed;
123 if (!buffer->readS32(&packed)) {
124 SkDELETE(ref);
125 return NULL;
126 }
127
128 ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1;
129
130#ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V16_AND_ALL_OTHER_INSTANCES_TOO
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000131 if (newFormat) {
132#endif
robertphillips@google.com466310d2013-12-03 16:43:54 +0000133 isOval = (packed >> kIsOval_SerializationShift) & 1;
134#ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V16_AND_ALL_OTHER_INSTANCES_TOO
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000135 } else {
robertphillips@google.com466310d2013-12-03 16:43:54 +0000136 isOval = (oldPacked >> SkPath::kOldIsOval_SerializationShift) & 1;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000137 }
138#endif
139
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000140 int32_t verbCount, pointCount, conicCount;
141 if (!buffer->readU32(&(ref->fGenerationID)) ||
142 !buffer->readS32(&verbCount) ||
143 !buffer->readS32(&pointCount) ||
144 !buffer->readS32(&conicCount)) {
145 SkDELETE(ref);
146 return NULL;
147 }
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000148
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000149 ref->resetToSize(verbCount, pointCount, conicCount);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000150 SkASSERT(verbCount == ref->countVerbs());
151 SkASSERT(pointCount == ref->countPoints());
152 SkASSERT(conicCount == ref->fConicWeights.count());
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000153
154 if (!buffer->read(ref->verbsMemWritable(), verbCount * sizeof(uint8_t)) ||
155 !buffer->read(ref->fPoints, pointCount * sizeof(SkPoint)) ||
156 !buffer->read(ref->fConicWeights.begin(), conicCount * sizeof(SkScalar)) ||
157 !buffer->read(&ref->fBounds, sizeof(SkRect))) {
158 SkDELETE(ref);
159 return NULL;
160 }
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000161 ref->fBoundsIsDirty = false;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000162 ref->fIsOval = isOval;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000163 return ref;
164}
165
166void SkPathRef::Rewind(SkAutoTUnref<SkPathRef>* pathRef) {
167 if ((*pathRef)->unique()) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000168 SkDEBUGCODE((*pathRef)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000169 (*pathRef)->fBoundsIsDirty = true; // this also invalidates fIsFinite
170 (*pathRef)->fVerbCnt = 0;
171 (*pathRef)->fPointCnt = 0;
172 (*pathRef)->fFreeSpace = (*pathRef)->currSize();
173 (*pathRef)->fGenerationID = 0;
174 (*pathRef)->fConicWeights.rewind();
robertphillips@google.com466310d2013-12-03 16:43:54 +0000175 (*pathRef)->fIsOval = false;
robertphillips@google.com03087072013-10-02 16:42:21 +0000176 SkDEBUGCODE((*pathRef)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000177 } else {
178 int oldVCnt = (*pathRef)->countVerbs();
179 int oldPCnt = (*pathRef)->countPoints();
180 pathRef->reset(SkNEW(SkPathRef));
181 (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt);
182 }
183}
184
185bool SkPathRef::operator== (const SkPathRef& ref) const {
robertphillips@google.com03087072013-10-02 16:42:21 +0000186 SkDEBUGCODE(this->validate();)
187 SkDEBUGCODE(ref.validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000188 bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID;
189#ifdef SK_RELEASE
190 if (genIDMatch) {
191 return true;
192 }
193#endif
194 if (fPointCnt != ref.fPointCnt ||
195 fVerbCnt != ref.fVerbCnt) {
196 SkASSERT(!genIDMatch);
197 return false;
198 }
199 if (0 != memcmp(this->verbsMemBegin(),
200 ref.verbsMemBegin(),
201 ref.fVerbCnt * sizeof(uint8_t))) {
202 SkASSERT(!genIDMatch);
203 return false;
204 }
205 if (0 != memcmp(this->points(),
206 ref.points(),
207 ref.fPointCnt * sizeof(SkPoint))) {
208 SkASSERT(!genIDMatch);
209 return false;
210 }
211 if (fConicWeights != ref.fConicWeights) {
212 SkASSERT(!genIDMatch);
213 return false;
214 }
215 // We've done the work to determine that these are equal. If either has a zero genID, copy
216 // the other's. If both are 0 then genID() will compute the next ID.
217 if (0 == fGenerationID) {
218 fGenerationID = ref.genID();
219 } else if (0 == ref.fGenerationID) {
220 ref.fGenerationID = this->genID();
221 }
222 return true;
223}
224
225void SkPathRef::writeToBuffer(SkWBuffer* buffer) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000226 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000227 SkDEBUGCODE(size_t beforePos = buffer->pos();)
228
229 // Call getBounds() to ensure (as a side-effect) that fBounds
230 // and fIsFinite are computed.
231 const SkRect& bounds = this->getBounds();
232
robertphillips@google.com466310d2013-12-03 16:43:54 +0000233 int32_t packed = ((fIsFinite & 1) << kIsFinite_SerializationShift) |
234 ((fIsOval & 1) << kIsOval_SerializationShift);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000235 buffer->write32(packed);
236
237 // TODO: write gen ID here. Problem: We don't know if we're cross process or not from
238 // SkWBuffer. Until this is fixed we write 0.
239 buffer->write32(0);
240 buffer->write32(fVerbCnt);
241 buffer->write32(fPointCnt);
242 buffer->write32(fConicWeights.count());
243 buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t));
244 buffer->write(fPoints, fPointCnt * sizeof(SkPoint));
245 buffer->write(fConicWeights.begin(), fConicWeights.bytes());
246 buffer->write(&bounds, sizeof(bounds));
247
248 SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize());
249}
250
251uint32_t SkPathRef::writeSize() {
252 return uint32_t(5 * sizeof(uint32_t) +
253 fVerbCnt * sizeof(uint8_t) +
254 fPointCnt * sizeof(SkPoint) +
255 fConicWeights.bytes() +
256 sizeof(SkRect));
257}
258
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000259void SkPathRef::copy(const SkPathRef& ref,
260 int additionalReserveVerbs,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000261 int additionalReservePoints) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000262 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000263 this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(),
264 additionalReserveVerbs, additionalReservePoints);
265 memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt * sizeof(uint8_t));
266 memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint));
267 fConicWeights = ref.fConicWeights;
268 // We could call genID() here to force a real ID (instead of 0). However, if we're making
269 // a copy then presumably we intend to make a modification immediately afterwards.
270 fGenerationID = ref.fGenerationID;
271 fBoundsIsDirty = ref.fBoundsIsDirty;
272 if (!fBoundsIsDirty) {
273 fBounds = ref.fBounds;
274 fIsFinite = ref.fIsFinite;
275 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000276 fIsOval = ref.fIsOval;
robertphillips@google.com03087072013-10-02 16:42:21 +0000277 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000278}
279
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000280SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000281 SkDEBUGCODE(this->validate();)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000282 int pCnt;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000283 bool dirtyAfterEdit = true;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000284 switch (verb) {
285 case SkPath::kMove_Verb:
286 pCnt = 1;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000287 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000288 break;
289 case SkPath::kLine_Verb:
290 pCnt = 1;
291 break;
292 case SkPath::kQuad_Verb:
293 // fall through
294 case SkPath::kConic_Verb:
295 pCnt = 2;
296 break;
297 case SkPath::kCubic_Verb:
298 pCnt = 3;
299 break;
300 case SkPath::kClose_Verb:
301 pCnt = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000302 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000303 break;
304 case SkPath::kDone_Verb:
305 SkDEBUGFAIL("growForVerb called for kDone");
306 // fall through
307 default:
308 SkDEBUGFAIL("default is not reached");
robertphillips@google.com466310d2013-12-03 16:43:54 +0000309 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000310 pCnt = 0;
311 }
312 size_t space = sizeof(uint8_t) + pCnt * sizeof (SkPoint);
313 this->makeSpace(space);
314 this->fVerbs[~fVerbCnt] = verb;
315 SkPoint* ret = fPoints + fPointCnt;
316 fVerbCnt += 1;
317 fPointCnt += pCnt;
318 fFreeSpace -= space;
319 fBoundsIsDirty = true; // this also invalidates fIsFinite
robertphillips@google.com466310d2013-12-03 16:43:54 +0000320 if (dirtyAfterEdit) {
321 fIsOval = false;
322 }
robertphillips@google.com03087072013-10-02 16:42:21 +0000323 SkDEBUGCODE(this->validate();)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000324 return ret;
325}
326
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000327uint32_t SkPathRef::genID() const {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000328 SkASSERT(!fEditorsAttached);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000329 static const uint32_t kMask = (static_cast<int64_t>(1) << SkPath::kPathRefGenIDBitCnt) - 1;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000330 if (!fGenerationID) {
331 if (0 == fPointCnt && 0 == fVerbCnt) {
332 fGenerationID = kEmptyGenID;
333 } else {
334 static int32_t gPathRefGenerationID;
335 // do a loop in case our global wraps around, as we never want to return a 0 or the
336 // empty ID
337 do {
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000338 fGenerationID = (sk_atomic_inc(&gPathRefGenerationID) + 1) & kMask;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000339 } while (fGenerationID <= kEmptyGenID);
340 }
341 }
342 return fGenerationID;
343}
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000344
robertphillips@google.com03087072013-10-02 16:42:21 +0000345#ifdef SK_DEBUG
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000346void SkPathRef::validate() const {
robertphillips@google.com03087072013-10-02 16:42:21 +0000347 this->INHERITED::validate();
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000348 SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0);
349 SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints) >= 0);
350 SkASSERT((NULL == fPoints) == (NULL == fVerbs));
351 SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
352 SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
353 SkASSERT(!(NULL == fPoints && fPointCnt));
354 SkASSERT(!(NULL == fVerbs && fVerbCnt));
355 SkASSERT(this->currSize() ==
356 fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000357
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000358 if (!fBoundsIsDirty && !fBounds.isEmpty()) {
359 bool isFinite = true;
360 for (int i = 0; i < fPointCnt; ++i) {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000361 SkASSERT(!fPoints[i].isFinite() || (
362 fBounds.fLeft - fPoints[i].fX < SK_ScalarNearlyZero &&
robertphillips@google.com1cc385b2013-10-17 12:17:27 +0000363 fPoints[i].fX - fBounds.fRight < SK_ScalarNearlyZero &&
skia.committer@gmail.comf84ad8f2013-10-18 07:01:59 +0000364 fBounds.fTop - fPoints[i].fY < SK_ScalarNearlyZero &&
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000365 fPoints[i].fY - fBounds.fBottom < SK_ScalarNearlyZero));
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000366 if (!fPoints[i].isFinite()) {
367 isFinite = false;
368 }
369 }
370 SkASSERT(SkToBool(fIsFinite) == isFinite);
371 }
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000372}
robertphillips@google.com03087072013-10-02 16:42:21 +0000373#endif