robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 1 | /* |
| 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" |
mtklein | 78358bf | 2014-06-02 08:44:27 -0700 | [diff] [blame] | 9 | #include "SkLazyPtr.h" |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 10 | #include "SkPath.h" |
| 11 | #include "SkPathRef.h" |
| 12 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 13 | ////////////////////////////////////////////////////////////////////////////// |
| 14 | SkPathRef::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; |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 26 | fPathRef->callGenIDChangeListeners(); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 27 | fPathRef->fGenerationID = 0; |
| 28 | SkDEBUGCODE(sk_atomic_inc(&fPathRef->fEditorsAttached);) |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 29 | } |
| 30 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 31 | ////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 32 | |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 33 | SkPathRef::~SkPathRef() { |
| 34 | this->callGenIDChangeListeners(); |
| 35 | SkDEBUGCODE(this->validate();) |
| 36 | sk_free(fPoints); |
| 37 | |
| 38 | SkDEBUGCODE(fPoints = NULL;) |
| 39 | SkDEBUGCODE(fVerbs = NULL;) |
| 40 | SkDEBUGCODE(fVerbCnt = 0x9999999;) |
| 41 | SkDEBUGCODE(fPointCnt = 0xAAAAAAA;) |
| 42 | SkDEBUGCODE(fPointCnt = 0xBBBBBBB;) |
| 43 | SkDEBUGCODE(fGenerationID = 0xEEEEEEEE;) |
| 44 | SkDEBUGCODE(fEditorsAttached = 0x7777777;) |
| 45 | } |
| 46 | |
mtklein | 148ec59 | 2014-10-13 13:17:56 -0700 | [diff] [blame] | 47 | // As a template argument, this must have external linkage. |
| 48 | SkPathRef* sk_create_empty_pathref() { |
mtklein | 51936a3 | 2014-09-29 05:27:59 -0700 | [diff] [blame] | 49 | SkPathRef* empty = SkNEW(SkPathRef); |
| 50 | empty->computeBounds(); // Avoids races later to be the first to do this. |
| 51 | return empty; |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 52 | } |
| 53 | |
mtklein | 148ec59 | 2014-10-13 13:17:56 -0700 | [diff] [blame] | 54 | SK_DECLARE_STATIC_LAZY_PTR(SkPathRef, empty, sk_create_empty_pathref); |
| 55 | |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 56 | SkPathRef* SkPathRef::CreateEmpty() { |
mtklein | 78358bf | 2014-06-02 08:44:27 -0700 | [diff] [blame] | 57 | return SkRef(empty.get()); |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 58 | } |
| 59 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 60 | void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst, |
| 61 | const SkPathRef& src, |
| 62 | const SkMatrix& matrix) { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 63 | SkDEBUGCODE(src.validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 64 | if (matrix.isIdentity()) { |
| 65 | if (*dst != &src) { |
| 66 | src.ref(); |
| 67 | dst->reset(const_cast<SkPathRef*>(&src)); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 68 | SkDEBUGCODE((*dst)->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 69 | } |
| 70 | return; |
| 71 | } |
| 72 | |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 73 | if (!(*dst)->unique()) { |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 74 | dst->reset(SkNEW(SkPathRef)); |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if (*dst != &src) { |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 78 | (*dst)->resetToSize(src.fVerbCnt, src.fPointCnt, src.fConicWeights.count()); |
| 79 | memcpy((*dst)->verbsMemWritable(), src.verbsMemBegin(), src.fVerbCnt * sizeof(uint8_t)); |
| 80 | (*dst)->fConicWeights = src.fConicWeights; |
| 81 | } |
| 82 | |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 83 | SkASSERT((*dst)->countPoints() == src.countPoints()); |
| 84 | SkASSERT((*dst)->countVerbs() == src.countVerbs()); |
| 85 | SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count()); |
| 86 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 87 | // Need to check this here in case (&src == dst) |
| 88 | bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1; |
| 89 | |
| 90 | matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt); |
| 91 | |
| 92 | /* |
| 93 | * Here we optimize the bounds computation, by noting if the bounds are |
| 94 | * already known, and if so, we just transform those as well and mark |
| 95 | * them as "known", rather than force the transformed path to have to |
| 96 | * recompute them. |
| 97 | * |
| 98 | * Special gotchas if the path is effectively empty (<= 1 point) or |
| 99 | * if it is non-finite. In those cases bounds need to stay empty, |
| 100 | * regardless of the matrix. |
| 101 | */ |
| 102 | if (canXformBounds) { |
| 103 | (*dst)->fBoundsIsDirty = false; |
| 104 | if (src.fIsFinite) { |
mtklein | 5c9c9be | 2014-12-01 06:59:54 -0800 | [diff] [blame] | 105 | matrix.mapRect(&(*dst)->fBounds, src.fBounds); |
| 106 | if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) { |
| 107 | (*dst)->fBounds.setEmpty(); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 108 | } |
| 109 | } else { |
| 110 | (*dst)->fIsFinite = false; |
mtklein | 5c9c9be | 2014-12-01 06:59:54 -0800 | [diff] [blame] | 111 | (*dst)->fBounds.setEmpty(); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 112 | } |
| 113 | } else { |
| 114 | (*dst)->fBoundsIsDirty = true; |
| 115 | } |
| 116 | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 117 | (*dst)->fSegmentMask = src.fSegmentMask; |
| 118 | |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 119 | // It's an oval only if it stays a rect. |
| 120 | (*dst)->fIsOval = src.fIsOval && matrix.rectStaysRect(); |
| 121 | |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 122 | SkDEBUGCODE((*dst)->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 123 | } |
| 124 | |
commit-bot@chromium.org | fed2ab6 | 2014-01-23 15:16:05 +0000 | [diff] [blame] | 125 | SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer) { |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 126 | SkPathRef* ref = SkNEW(SkPathRef); |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 127 | bool isOval; |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 128 | uint8_t segmentMask; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 129 | |
| 130 | int32_t packed; |
| 131 | if (!buffer->readS32(&packed)) { |
| 132 | SkDELETE(ref); |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1; |
commit-bot@chromium.org | fed2ab6 | 2014-01-23 15:16:05 +0000 | [diff] [blame] | 137 | segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF; |
| 138 | isOval = (packed >> kIsOval_SerializationShift) & 1; |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 139 | |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 140 | 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.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 148 | |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 149 | ref->resetToSize(verbCount, pointCount, conicCount); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 150 | SkASSERT(verbCount == ref->countVerbs()); |
| 151 | SkASSERT(pointCount == ref->countPoints()); |
| 152 | SkASSERT(conicCount == ref->fConicWeights.count()); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 153 | |
| 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.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 161 | ref->fBoundsIsDirty = false; |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 162 | |
| 163 | // resetToSize clears fSegmentMask and fIsOval |
| 164 | ref->fSegmentMask = segmentMask; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 165 | ref->fIsOval = isOval; |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 166 | return ref; |
| 167 | } |
| 168 | |
| 169 | void SkPathRef::Rewind(SkAutoTUnref<SkPathRef>* pathRef) { |
| 170 | if ((*pathRef)->unique()) { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 171 | SkDEBUGCODE((*pathRef)->validate();) |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 172 | (*pathRef)->callGenIDChangeListeners(); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 173 | (*pathRef)->fBoundsIsDirty = true; // this also invalidates fIsFinite |
| 174 | (*pathRef)->fVerbCnt = 0; |
| 175 | (*pathRef)->fPointCnt = 0; |
| 176 | (*pathRef)->fFreeSpace = (*pathRef)->currSize(); |
| 177 | (*pathRef)->fGenerationID = 0; |
| 178 | (*pathRef)->fConicWeights.rewind(); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 179 | (*pathRef)->fSegmentMask = 0; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 180 | (*pathRef)->fIsOval = false; |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 181 | SkDEBUGCODE((*pathRef)->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 182 | } else { |
| 183 | int oldVCnt = (*pathRef)->countVerbs(); |
| 184 | int oldPCnt = (*pathRef)->countPoints(); |
| 185 | pathRef->reset(SkNEW(SkPathRef)); |
| 186 | (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | bool SkPathRef::operator== (const SkPathRef& ref) const { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 191 | SkDEBUGCODE(this->validate();) |
| 192 | SkDEBUGCODE(ref.validate();) |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 193 | |
| 194 | // We explicitly check fSegmentMask as a quick-reject. We could skip it, |
| 195 | // since it is only a cache of info in the fVerbs, but its a fast way to |
| 196 | // notice a difference |
| 197 | if (fSegmentMask != ref.fSegmentMask) { |
| 198 | return false; |
| 199 | } |
| 200 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 201 | bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID; |
| 202 | #ifdef SK_RELEASE |
| 203 | if (genIDMatch) { |
| 204 | return true; |
| 205 | } |
| 206 | #endif |
| 207 | if (fPointCnt != ref.fPointCnt || |
| 208 | fVerbCnt != ref.fVerbCnt) { |
| 209 | SkASSERT(!genIDMatch); |
| 210 | return false; |
| 211 | } |
mtklein | d489759 | 2014-11-14 09:22:40 -0800 | [diff] [blame] | 212 | if (0 == ref.fVerbCnt) { |
| 213 | SkASSERT(0 == ref.fPointCnt); |
| 214 | return true; |
| 215 | } |
| 216 | SkASSERT(this->verbsMemBegin() && ref.verbsMemBegin()); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 217 | if (0 != memcmp(this->verbsMemBegin(), |
| 218 | ref.verbsMemBegin(), |
| 219 | ref.fVerbCnt * sizeof(uint8_t))) { |
| 220 | SkASSERT(!genIDMatch); |
| 221 | return false; |
| 222 | } |
mtklein | d489759 | 2014-11-14 09:22:40 -0800 | [diff] [blame] | 223 | SkASSERT(this->points() && ref.points()); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 224 | if (0 != memcmp(this->points(), |
| 225 | ref.points(), |
| 226 | ref.fPointCnt * sizeof(SkPoint))) { |
| 227 | SkASSERT(!genIDMatch); |
| 228 | return false; |
| 229 | } |
| 230 | if (fConicWeights != ref.fConicWeights) { |
| 231 | SkASSERT(!genIDMatch); |
| 232 | return false; |
| 233 | } |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 234 | return true; |
| 235 | } |
| 236 | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 237 | void SkPathRef::writeToBuffer(SkWBuffer* buffer) const { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 238 | SkDEBUGCODE(this->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 239 | SkDEBUGCODE(size_t beforePos = buffer->pos();) |
| 240 | |
| 241 | // Call getBounds() to ensure (as a side-effect) that fBounds |
| 242 | // and fIsFinite are computed. |
| 243 | const SkRect& bounds = this->getBounds(); |
| 244 | |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 245 | int32_t packed = ((fIsFinite & 1) << kIsFinite_SerializationShift) | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 246 | ((fIsOval & 1) << kIsOval_SerializationShift) | |
| 247 | (fSegmentMask << kSegmentMask_SerializationShift); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 248 | buffer->write32(packed); |
| 249 | |
| 250 | // TODO: write gen ID here. Problem: We don't know if we're cross process or not from |
| 251 | // SkWBuffer. Until this is fixed we write 0. |
| 252 | buffer->write32(0); |
| 253 | buffer->write32(fVerbCnt); |
| 254 | buffer->write32(fPointCnt); |
| 255 | buffer->write32(fConicWeights.count()); |
| 256 | buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t)); |
| 257 | buffer->write(fPoints, fPointCnt * sizeof(SkPoint)); |
| 258 | buffer->write(fConicWeights.begin(), fConicWeights.bytes()); |
| 259 | buffer->write(&bounds, sizeof(bounds)); |
| 260 | |
| 261 | SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize()); |
| 262 | } |
| 263 | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 264 | uint32_t SkPathRef::writeSize() const { |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 265 | return uint32_t(5 * sizeof(uint32_t) + |
| 266 | fVerbCnt * sizeof(uint8_t) + |
| 267 | fPointCnt * sizeof(SkPoint) + |
| 268 | fConicWeights.bytes() + |
| 269 | sizeof(SkRect)); |
| 270 | } |
| 271 | |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 272 | void SkPathRef::copy(const SkPathRef& ref, |
| 273 | int additionalReserveVerbs, |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 274 | int additionalReservePoints) { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 275 | SkDEBUGCODE(this->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 276 | this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(), |
| 277 | additionalReserveVerbs, additionalReservePoints); |
| 278 | memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt * sizeof(uint8_t)); |
| 279 | memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint)); |
| 280 | fConicWeights = ref.fConicWeights; |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 281 | fBoundsIsDirty = ref.fBoundsIsDirty; |
| 282 | if (!fBoundsIsDirty) { |
| 283 | fBounds = ref.fBounds; |
| 284 | fIsFinite = ref.fIsFinite; |
| 285 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 286 | fSegmentMask = ref.fSegmentMask; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 287 | fIsOval = ref.fIsOval; |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 288 | SkDEBUGCODE(this->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 289 | } |
| 290 | |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 291 | SkPoint* SkPathRef::growForRepeatedVerb(int /*SkPath::Verb*/ verb, |
| 292 | int numVbs, |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 293 | SkScalar** weights) { |
| 294 | // This value is just made-up for now. When count is 4, calling memset was much |
| 295 | // slower than just writing the loop. This seems odd, and hopefully in the |
| 296 | // future this will appear to have been a fluke... |
| 297 | static const unsigned int kMIN_COUNT_FOR_MEMSET_TO_BE_FAST = 16; |
| 298 | |
| 299 | SkDEBUGCODE(this->validate();) |
| 300 | int pCnt; |
| 301 | bool dirtyAfterEdit = true; |
| 302 | switch (verb) { |
| 303 | case SkPath::kMove_Verb: |
| 304 | pCnt = numVbs; |
| 305 | dirtyAfterEdit = false; |
| 306 | break; |
| 307 | case SkPath::kLine_Verb: |
| 308 | fSegmentMask |= SkPath::kLine_SegmentMask; |
| 309 | pCnt = numVbs; |
| 310 | break; |
| 311 | case SkPath::kQuad_Verb: |
| 312 | fSegmentMask |= SkPath::kQuad_SegmentMask; |
| 313 | pCnt = 2 * numVbs; |
| 314 | break; |
| 315 | case SkPath::kConic_Verb: |
| 316 | fSegmentMask |= SkPath::kConic_SegmentMask; |
| 317 | pCnt = 2 * numVbs; |
| 318 | break; |
| 319 | case SkPath::kCubic_Verb: |
| 320 | fSegmentMask |= SkPath::kCubic_SegmentMask; |
| 321 | pCnt = 3 * numVbs; |
| 322 | break; |
| 323 | case SkPath::kClose_Verb: |
| 324 | SkDEBUGFAIL("growForRepeatedVerb called for kClose_Verb"); |
| 325 | pCnt = 0; |
| 326 | dirtyAfterEdit = false; |
| 327 | break; |
| 328 | case SkPath::kDone_Verb: |
| 329 | SkDEBUGFAIL("growForRepeatedVerb called for kDone"); |
| 330 | // fall through |
| 331 | default: |
| 332 | SkDEBUGFAIL("default should not be reached"); |
| 333 | pCnt = 0; |
| 334 | dirtyAfterEdit = false; |
| 335 | } |
| 336 | |
| 337 | size_t space = numVbs * sizeof(uint8_t) + pCnt * sizeof (SkPoint); |
| 338 | this->makeSpace(space); |
| 339 | |
| 340 | SkPoint* ret = fPoints + fPointCnt; |
| 341 | uint8_t* vb = fVerbs - fVerbCnt; |
| 342 | |
| 343 | // cast to unsigned, so if kMIN_COUNT_FOR_MEMSET_TO_BE_FAST is defined to |
| 344 | // be 0, the compiler will remove the test/branch entirely. |
| 345 | if ((unsigned)numVbs >= kMIN_COUNT_FOR_MEMSET_TO_BE_FAST) { |
| 346 | memset(vb - numVbs, verb, numVbs); |
| 347 | } else { |
| 348 | for (int i = 0; i < numVbs; ++i) { |
| 349 | vb[~i] = verb; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | fVerbCnt += numVbs; |
| 354 | fPointCnt += pCnt; |
| 355 | fFreeSpace -= space; |
| 356 | fBoundsIsDirty = true; // this also invalidates fIsFinite |
| 357 | if (dirtyAfterEdit) { |
| 358 | fIsOval = false; |
| 359 | } |
| 360 | |
| 361 | if (SkPath::kConic_Verb == verb) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 362 | SkASSERT(weights); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 363 | *weights = fConicWeights.append(numVbs); |
| 364 | } |
| 365 | |
| 366 | SkDEBUGCODE(this->validate();) |
| 367 | return ret; |
| 368 | } |
| 369 | |
| 370 | SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb, SkScalar weight) { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 371 | SkDEBUGCODE(this->validate();) |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 372 | int pCnt; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 373 | bool dirtyAfterEdit = true; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 374 | switch (verb) { |
| 375 | case SkPath::kMove_Verb: |
| 376 | pCnt = 1; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 377 | dirtyAfterEdit = false; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 378 | break; |
| 379 | case SkPath::kLine_Verb: |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 380 | fSegmentMask |= SkPath::kLine_SegmentMask; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 381 | pCnt = 1; |
| 382 | break; |
| 383 | case SkPath::kQuad_Verb: |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 384 | fSegmentMask |= SkPath::kQuad_SegmentMask; |
| 385 | pCnt = 2; |
| 386 | break; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 387 | case SkPath::kConic_Verb: |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 388 | fSegmentMask |= SkPath::kConic_SegmentMask; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 389 | pCnt = 2; |
| 390 | break; |
| 391 | case SkPath::kCubic_Verb: |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 392 | fSegmentMask |= SkPath::kCubic_SegmentMask; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 393 | pCnt = 3; |
| 394 | break; |
| 395 | case SkPath::kClose_Verb: |
| 396 | pCnt = 0; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 397 | dirtyAfterEdit = false; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 398 | break; |
| 399 | case SkPath::kDone_Verb: |
| 400 | SkDEBUGFAIL("growForVerb called for kDone"); |
| 401 | // fall through |
| 402 | default: |
| 403 | SkDEBUGFAIL("default is not reached"); |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 404 | dirtyAfterEdit = false; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 405 | pCnt = 0; |
| 406 | } |
| 407 | size_t space = sizeof(uint8_t) + pCnt * sizeof (SkPoint); |
| 408 | this->makeSpace(space); |
| 409 | this->fVerbs[~fVerbCnt] = verb; |
| 410 | SkPoint* ret = fPoints + fPointCnt; |
| 411 | fVerbCnt += 1; |
| 412 | fPointCnt += pCnt; |
| 413 | fFreeSpace -= space; |
| 414 | fBoundsIsDirty = true; // this also invalidates fIsFinite |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 415 | if (dirtyAfterEdit) { |
| 416 | fIsOval = false; |
| 417 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 418 | |
| 419 | if (SkPath::kConic_Verb == verb) { |
| 420 | *fConicWeights.append() = weight; |
| 421 | } |
| 422 | |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 423 | SkDEBUGCODE(this->validate();) |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 424 | return ret; |
| 425 | } |
| 426 | |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 427 | uint32_t SkPathRef::genID() const { |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 428 | SkASSERT(!fEditorsAttached); |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 429 | static const uint32_t kMask = (static_cast<int64_t>(1) << SkPath::kPathRefGenIDBitCnt) - 1; |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 430 | if (!fGenerationID) { |
| 431 | if (0 == fPointCnt && 0 == fVerbCnt) { |
| 432 | fGenerationID = kEmptyGenID; |
| 433 | } else { |
| 434 | static int32_t gPathRefGenerationID; |
| 435 | // do a loop in case our global wraps around, as we never want to return a 0 or the |
| 436 | // empty ID |
| 437 | do { |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 438 | fGenerationID = (sk_atomic_inc(&gPathRefGenerationID) + 1) & kMask; |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 439 | } while (fGenerationID <= kEmptyGenID); |
| 440 | } |
| 441 | } |
| 442 | return fGenerationID; |
| 443 | } |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 444 | |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 445 | void SkPathRef::addGenIDChangeListener(GenIDChangeListener* listener) { |
| 446 | if (NULL == listener || this == empty.get()) { |
| 447 | SkDELETE(listener); |
| 448 | return; |
| 449 | } |
| 450 | *fGenIDChangeListeners.append() = listener; |
| 451 | } |
| 452 | |
| 453 | // we need to be called *before* the genID gets changed or zerod |
| 454 | void SkPathRef::callGenIDChangeListeners() { |
| 455 | for (int i = 0; i < fGenIDChangeListeners.count(); i++) { |
| 456 | fGenIDChangeListeners[i]->onChange(); |
| 457 | } |
| 458 | |
| 459 | // Listeners get at most one shot, so whether these triggered or not, blow them away. |
| 460 | fGenIDChangeListeners.deleteAll(); |
| 461 | } |
| 462 | |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 463 | #ifdef SK_DEBUG |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 464 | void SkPathRef::validate() const { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 465 | this->INHERITED::validate(); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 466 | SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0); |
| 467 | SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints) >= 0); |
| 468 | SkASSERT((NULL == fPoints) == (NULL == fVerbs)); |
| 469 | SkASSERT(!(NULL == fPoints && 0 != fFreeSpace)); |
| 470 | SkASSERT(!(NULL == fPoints && 0 != fFreeSpace)); |
| 471 | SkASSERT(!(NULL == fPoints && fPointCnt)); |
| 472 | SkASSERT(!(NULL == fVerbs && fVerbCnt)); |
| 473 | SkASSERT(this->currSize() == |
| 474 | fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt); |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 475 | |
mtklein | 5c9c9be | 2014-12-01 06:59:54 -0800 | [diff] [blame] | 476 | if (!fBoundsIsDirty && !fBounds.isEmpty()) { |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 477 | bool isFinite = true; |
| 478 | for (int i = 0; i < fPointCnt; ++i) { |
robertphillips | f1cdead | 2015-01-05 09:20:04 -0800 | [diff] [blame] | 479 | #ifdef SK_DEBUG |
| 480 | if (fPoints[i].isFinite() && |
| 481 | (fPoints[i].fX < fBounds.fLeft || fPoints[i].fX > fBounds.fRight || |
| 482 | fPoints[i].fY < fBounds.fTop || fPoints[i].fY > fBounds.fBottom)) { |
| 483 | SkDebugf("bounds: %f %f %f %f\n", |
| 484 | fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom); |
| 485 | for (int j = 0; j < fPointCnt; ++j) { |
| 486 | if (i == j) { |
| 487 | SkDebugf("*"); |
| 488 | } |
| 489 | SkDebugf("%f %f\n", fPoints[j].fX, fPoints[j].fY); |
| 490 | } |
| 491 | } |
| 492 | #endif |
| 493 | |
robertphillips | 0e91246 | 2014-12-12 12:47:59 -0800 | [diff] [blame] | 494 | SkASSERT(!fPoints[i].isFinite() || |
| 495 | (fPoints[i].fX >= fBounds.fLeft && fPoints[i].fX <= fBounds.fRight && |
| 496 | fPoints[i].fY >= fBounds.fTop && fPoints[i].fY <= fBounds.fBottom)); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 497 | if (!fPoints[i].isFinite()) { |
| 498 | isFinite = false; |
| 499 | } |
| 500 | } |
| 501 | SkASSERT(SkToBool(fIsFinite) == isFinite); |
| 502 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 503 | |
| 504 | #ifdef SK_DEBUG_PATH |
| 505 | uint32_t mask = 0; |
| 506 | for (int i = 0; i < fVerbCnt; ++i) { |
| 507 | switch (fVerbs[~i]) { |
| 508 | case SkPath::kMove_Verb: |
| 509 | break; |
| 510 | case SkPath::kLine_Verb: |
| 511 | mask |= SkPath::kLine_SegmentMask; |
| 512 | break; |
| 513 | case SkPath::kQuad_Verb: |
| 514 | mask |= SkPath::kQuad_SegmentMask; |
| 515 | break; |
| 516 | case SkPath::kConic_Verb: |
| 517 | mask |= SkPath::kConic_SegmentMask; |
| 518 | break; |
| 519 | case SkPath::kCubic_Verb: |
| 520 | mask |= SkPath::kCubic_SegmentMask; |
| 521 | break; |
| 522 | case SkPath::kClose_Verb: |
| 523 | break; |
| 524 | case SkPath::kDone_Verb: |
| 525 | SkDEBUGFAIL("Done verb shouldn't be recorded."); |
| 526 | break; |
| 527 | default: |
| 528 | SkDEBUGFAIL("Unknown Verb"); |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | SkASSERT(mask == fSegmentMask); |
| 533 | #endif // SK_DEBUG_PATH |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 534 | } |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 535 | #endif |