blob: 3ad5ae7e0222649822f7465c073059cde5fcb187 [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
13SK_DEFINE_INST_COUNT(SkPathRef);
14
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000015//////////////////////////////////////////////////////////////////////////////
16SkPathRef::Editor::Editor(SkAutoTUnref<SkPathRef>* pathRef,
17 int incReserveVerbs,
18 int incReservePoints)
19{
20 if ((*pathRef)->unique()) {
21 (*pathRef)->incReserve(incReserveVerbs, incReservePoints);
22 } else {
23 SkPathRef* copy = SkNEW(SkPathRef);
24 copy->copy(**pathRef, incReserveVerbs, incReservePoints);
25 pathRef->reset(copy);
26 }
27 fPathRef = *pathRef;
28 fPathRef->fGenerationID = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +000029 fPathRef->fIsOval = false;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000030 SkDEBUGCODE(sk_atomic_inc(&fPathRef->fEditorsAttached);)
robertphillips@google.comca0c8382013-09-26 12:18:23 +000031}
32
33SkPoint* SkPathRef::Editor::growForConic(SkScalar w) {
robertphillips@google.com03087072013-10-02 16:42:21 +000034 SkDEBUGCODE(fPathRef->validate();)
robertphillips@google.comca0c8382013-09-26 12:18:23 +000035 SkPoint* pts = fPathRef->growForVerb(SkPath::kConic_Verb);
36 *fPathRef->fConicWeights.append() = w;
37 return pts;
38}
39
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000040//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000041void SkPathRef::CreateEmptyImpl(SkPathRef** empty) {
42 *empty = SkNEW(SkPathRef);
43 (*empty)->computeBounds(); // Preemptively avoid a race to clear fBoundsIsDirty.
44}
45
46SkPathRef* SkPathRef::CreateEmpty() {
47 static SkPathRef* gEmptyPathRef;
48 SK_DECLARE_STATIC_ONCE(once);
49 SkOnce(&once, SkPathRef::CreateEmptyImpl, &gEmptyPathRef);
50 return SkRef(gEmptyPathRef);
51}
52
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000053void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst,
54 const SkPathRef& src,
55 const SkMatrix& matrix) {
robertphillips@google.com03087072013-10-02 16:42:21 +000056 SkDEBUGCODE(src.validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000057 if (matrix.isIdentity()) {
58 if (*dst != &src) {
59 src.ref();
60 dst->reset(const_cast<SkPathRef*>(&src));
robertphillips@google.com03087072013-10-02 16:42:21 +000061 SkDEBUGCODE((*dst)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000062 }
63 return;
64 }
65
66 bool dstUnique = (*dst)->unique();
67 if (!dstUnique) {
68 dst->reset(SkNEW(SkPathRef));
69 (*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
74 // Need to check this here in case (&src == dst)
75 bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1;
76
77 matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt);
78
79 /*
80 * Here we optimize the bounds computation, by noting if the bounds are
81 * already known, and if so, we just transform those as well and mark
82 * them as "known", rather than force the transformed path to have to
83 * recompute them.
84 *
85 * Special gotchas if the path is effectively empty (<= 1 point) or
86 * if it is non-finite. In those cases bounds need to stay empty,
87 * regardless of the matrix.
88 */
89 if (canXformBounds) {
90 (*dst)->fBoundsIsDirty = false;
91 if (src.fIsFinite) {
92 matrix.mapRect(&(*dst)->fBounds, src.fBounds);
93 if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) {
94 (*dst)->fBounds.setEmpty();
95 }
96 } else {
97 (*dst)->fIsFinite = false;
98 (*dst)->fBounds.setEmpty();
99 }
100 } else {
101 (*dst)->fBoundsIsDirty = true;
102 }
103
robertphillips@google.com466310d2013-12-03 16:43:54 +0000104 // It's an oval only if it stays a rect.
105 (*dst)->fIsOval = src.fIsOval && matrix.rectStaysRect();
106
robertphillips@google.com03087072013-10-02 16:42:21 +0000107 SkDEBUGCODE((*dst)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000108}
109
110SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer
robertphillips@google.com466310d2013-12-03 16:43:54 +0000111#ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V16_AND_ALL_OTHER_INSTANCES_TOO
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000112 , bool newFormat, int32_t oldPacked
113#endif
114 ) {
115 SkPathRef* ref = SkNEW(SkPathRef);
robertphillips@google.com466310d2013-12-03 16:43:54 +0000116 bool isOval;
117
118 int32_t packed;
119 if (!buffer->readS32(&packed)) {
120 SkDELETE(ref);
121 return NULL;
122 }
123
124 ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1;
125
126#ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V16_AND_ALL_OTHER_INSTANCES_TOO
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000127 if (newFormat) {
128#endif
robertphillips@google.com466310d2013-12-03 16:43:54 +0000129 isOval = (packed >> kIsOval_SerializationShift) & 1;
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 } else {
robertphillips@google.com466310d2013-12-03 16:43:54 +0000132 isOval = (oldPacked >> SkPath::kOldIsOval_SerializationShift) & 1;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000133 }
134#endif
135
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000136 int32_t verbCount, pointCount, conicCount;
137 if (!buffer->readU32(&(ref->fGenerationID)) ||
138 !buffer->readS32(&verbCount) ||
139 !buffer->readS32(&pointCount) ||
140 !buffer->readS32(&conicCount)) {
141 SkDELETE(ref);
142 return NULL;
143 }
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000144
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000145 ref->resetToSize(verbCount, pointCount, conicCount);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000146 SkASSERT(verbCount == ref->countVerbs());
147 SkASSERT(pointCount == ref->countPoints());
148 SkASSERT(conicCount == ref->fConicWeights.count());
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000149
150 if (!buffer->read(ref->verbsMemWritable(), verbCount * sizeof(uint8_t)) ||
151 !buffer->read(ref->fPoints, pointCount * sizeof(SkPoint)) ||
152 !buffer->read(ref->fConicWeights.begin(), conicCount * sizeof(SkScalar)) ||
153 !buffer->read(&ref->fBounds, sizeof(SkRect))) {
154 SkDELETE(ref);
155 return NULL;
156 }
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000157 ref->fBoundsIsDirty = false;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000158 ref->fIsOval = isOval;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000159 return ref;
160}
161
162void SkPathRef::Rewind(SkAutoTUnref<SkPathRef>* pathRef) {
163 if ((*pathRef)->unique()) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000164 SkDEBUGCODE((*pathRef)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000165 (*pathRef)->fBoundsIsDirty = true; // this also invalidates fIsFinite
166 (*pathRef)->fVerbCnt = 0;
167 (*pathRef)->fPointCnt = 0;
168 (*pathRef)->fFreeSpace = (*pathRef)->currSize();
169 (*pathRef)->fGenerationID = 0;
170 (*pathRef)->fConicWeights.rewind();
robertphillips@google.com466310d2013-12-03 16:43:54 +0000171 (*pathRef)->fIsOval = false;
robertphillips@google.com03087072013-10-02 16:42:21 +0000172 SkDEBUGCODE((*pathRef)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000173 } else {
174 int oldVCnt = (*pathRef)->countVerbs();
175 int oldPCnt = (*pathRef)->countPoints();
176 pathRef->reset(SkNEW(SkPathRef));
177 (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt);
178 }
179}
180
181bool SkPathRef::operator== (const SkPathRef& ref) const {
robertphillips@google.com03087072013-10-02 16:42:21 +0000182 SkDEBUGCODE(this->validate();)
183 SkDEBUGCODE(ref.validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000184 bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID;
185#ifdef SK_RELEASE
186 if (genIDMatch) {
187 return true;
188 }
189#endif
190 if (fPointCnt != ref.fPointCnt ||
191 fVerbCnt != ref.fVerbCnt) {
192 SkASSERT(!genIDMatch);
193 return false;
194 }
195 if (0 != memcmp(this->verbsMemBegin(),
196 ref.verbsMemBegin(),
197 ref.fVerbCnt * sizeof(uint8_t))) {
198 SkASSERT(!genIDMatch);
199 return false;
200 }
201 if (0 != memcmp(this->points(),
202 ref.points(),
203 ref.fPointCnt * sizeof(SkPoint))) {
204 SkASSERT(!genIDMatch);
205 return false;
206 }
207 if (fConicWeights != ref.fConicWeights) {
208 SkASSERT(!genIDMatch);
209 return false;
210 }
211 // We've done the work to determine that these are equal. If either has a zero genID, copy
212 // the other's. If both are 0 then genID() will compute the next ID.
213 if (0 == fGenerationID) {
214 fGenerationID = ref.genID();
215 } else if (0 == ref.fGenerationID) {
216 ref.fGenerationID = this->genID();
217 }
218 return true;
219}
220
221void SkPathRef::writeToBuffer(SkWBuffer* buffer) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000222 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000223 SkDEBUGCODE(size_t beforePos = buffer->pos();)
224
225 // Call getBounds() to ensure (as a side-effect) that fBounds
226 // and fIsFinite are computed.
227 const SkRect& bounds = this->getBounds();
228
robertphillips@google.com466310d2013-12-03 16:43:54 +0000229 int32_t packed = ((fIsFinite & 1) << kIsFinite_SerializationShift) |
230 ((fIsOval & 1) << kIsOval_SerializationShift);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000231 buffer->write32(packed);
232
233 // TODO: write gen ID here. Problem: We don't know if we're cross process or not from
234 // SkWBuffer. Until this is fixed we write 0.
235 buffer->write32(0);
236 buffer->write32(fVerbCnt);
237 buffer->write32(fPointCnt);
238 buffer->write32(fConicWeights.count());
239 buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t));
240 buffer->write(fPoints, fPointCnt * sizeof(SkPoint));
241 buffer->write(fConicWeights.begin(), fConicWeights.bytes());
242 buffer->write(&bounds, sizeof(bounds));
243
244 SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize());
245}
246
247uint32_t SkPathRef::writeSize() {
248 return uint32_t(5 * sizeof(uint32_t) +
249 fVerbCnt * sizeof(uint8_t) +
250 fPointCnt * sizeof(SkPoint) +
251 fConicWeights.bytes() +
252 sizeof(SkRect));
253}
254
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000255void SkPathRef::copy(const SkPathRef& ref,
256 int additionalReserveVerbs,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000257 int additionalReservePoints) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000258 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000259 this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(),
260 additionalReserveVerbs, additionalReservePoints);
261 memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt * sizeof(uint8_t));
262 memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint));
263 fConicWeights = ref.fConicWeights;
264 // We could call genID() here to force a real ID (instead of 0). However, if we're making
265 // a copy then presumably we intend to make a modification immediately afterwards.
266 fGenerationID = ref.fGenerationID;
267 fBoundsIsDirty = ref.fBoundsIsDirty;
268 if (!fBoundsIsDirty) {
269 fBounds = ref.fBounds;
270 fIsFinite = ref.fIsFinite;
271 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000272 fIsOval = ref.fIsOval;
robertphillips@google.com03087072013-10-02 16:42:21 +0000273 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000274}
275
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000276SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000277 SkDEBUGCODE(this->validate();)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000278 int pCnt;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000279 bool dirtyAfterEdit = true;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000280 switch (verb) {
281 case SkPath::kMove_Verb:
282 pCnt = 1;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000283 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000284 break;
285 case SkPath::kLine_Verb:
286 pCnt = 1;
287 break;
288 case SkPath::kQuad_Verb:
289 // fall through
290 case SkPath::kConic_Verb:
291 pCnt = 2;
292 break;
293 case SkPath::kCubic_Verb:
294 pCnt = 3;
295 break;
296 case SkPath::kClose_Verb:
297 pCnt = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000298 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000299 break;
300 case SkPath::kDone_Verb:
301 SkDEBUGFAIL("growForVerb called for kDone");
302 // fall through
303 default:
304 SkDEBUGFAIL("default is not reached");
robertphillips@google.com466310d2013-12-03 16:43:54 +0000305 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000306 pCnt = 0;
307 }
308 size_t space = sizeof(uint8_t) + pCnt * sizeof (SkPoint);
309 this->makeSpace(space);
310 this->fVerbs[~fVerbCnt] = verb;
311 SkPoint* ret = fPoints + fPointCnt;
312 fVerbCnt += 1;
313 fPointCnt += pCnt;
314 fFreeSpace -= space;
315 fBoundsIsDirty = true; // this also invalidates fIsFinite
robertphillips@google.com466310d2013-12-03 16:43:54 +0000316 if (dirtyAfterEdit) {
317 fIsOval = false;
318 }
robertphillips@google.com03087072013-10-02 16:42:21 +0000319 SkDEBUGCODE(this->validate();)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000320 return ret;
321}
322
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000323uint32_t SkPathRef::genID() const {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000324 SkASSERT(!fEditorsAttached);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000325 static const uint32_t kMask = (static_cast<int64_t>(1) << SkPath::kPathRefGenIDBitCnt) - 1;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000326 if (!fGenerationID) {
327 if (0 == fPointCnt && 0 == fVerbCnt) {
328 fGenerationID = kEmptyGenID;
329 } else {
330 static int32_t gPathRefGenerationID;
331 // do a loop in case our global wraps around, as we never want to return a 0 or the
332 // empty ID
333 do {
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000334 fGenerationID = (sk_atomic_inc(&gPathRefGenerationID) + 1) & kMask;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000335 } while (fGenerationID <= kEmptyGenID);
336 }
337 }
338 return fGenerationID;
339}
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000340
robertphillips@google.com03087072013-10-02 16:42:21 +0000341#ifdef SK_DEBUG
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000342void SkPathRef::validate() const {
robertphillips@google.com03087072013-10-02 16:42:21 +0000343 this->INHERITED::validate();
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000344 SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0);
345 SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints) >= 0);
346 SkASSERT((NULL == fPoints) == (NULL == fVerbs));
347 SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
348 SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
349 SkASSERT(!(NULL == fPoints && fPointCnt));
350 SkASSERT(!(NULL == fVerbs && fVerbCnt));
351 SkASSERT(this->currSize() ==
352 fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000353
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000354 if (!fBoundsIsDirty && !fBounds.isEmpty()) {
355 bool isFinite = true;
356 for (int i = 0; i < fPointCnt; ++i) {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000357 SkASSERT(!fPoints[i].isFinite() || (
358 fBounds.fLeft - fPoints[i].fX < SK_ScalarNearlyZero &&
robertphillips@google.com1cc385b2013-10-17 12:17:27 +0000359 fPoints[i].fX - fBounds.fRight < SK_ScalarNearlyZero &&
skia.committer@gmail.comf84ad8f2013-10-18 07:01:59 +0000360 fBounds.fTop - fPoints[i].fY < SK_ScalarNearlyZero &&
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000361 fPoints[i].fY - fBounds.fBottom < SK_ScalarNearlyZero));
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000362 if (!fPoints[i].isFinite()) {
363 isFinite = false;
364 }
365 }
366 SkASSERT(SkToBool(fIsFinite) == isFinite);
367 }
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000368}
robertphillips@google.com03087072013-10-02 16:42:21 +0000369#endif