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" |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 9 | #include "SkOnce.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; |
| 26 | fPathRef->fGenerationID = 0; |
| 27 | SkDEBUGCODE(sk_atomic_inc(&fPathRef->fEditorsAttached);) |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 28 | } |
| 29 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 30 | ////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 31 | void SkPathRef::CreateEmptyImpl(SkPathRef** empty) { |
| 32 | *empty = SkNEW(SkPathRef); |
| 33 | (*empty)->computeBounds(); // Preemptively avoid a race to clear fBoundsIsDirty. |
| 34 | } |
| 35 | |
| 36 | SkPathRef* SkPathRef::CreateEmpty() { |
| 37 | static SkPathRef* gEmptyPathRef; |
| 38 | SK_DECLARE_STATIC_ONCE(once); |
| 39 | SkOnce(&once, SkPathRef::CreateEmptyImpl, &gEmptyPathRef); |
| 40 | return SkRef(gEmptyPathRef); |
| 41 | } |
| 42 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 43 | void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst, |
| 44 | const SkPathRef& src, |
| 45 | const SkMatrix& matrix) { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 46 | SkDEBUGCODE(src.validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 47 | if (matrix.isIdentity()) { |
| 48 | if (*dst != &src) { |
| 49 | src.ref(); |
| 50 | dst->reset(const_cast<SkPathRef*>(&src)); |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 51 | SkDEBUGCODE((*dst)->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 52 | } |
| 53 | return; |
| 54 | } |
| 55 | |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 56 | if (!(*dst)->unique()) { |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 57 | dst->reset(SkNEW(SkPathRef)); |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | if (*dst != &src) { |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 61 | (*dst)->resetToSize(src.fVerbCnt, src.fPointCnt, src.fConicWeights.count()); |
| 62 | memcpy((*dst)->verbsMemWritable(), src.verbsMemBegin(), src.fVerbCnt * sizeof(uint8_t)); |
| 63 | (*dst)->fConicWeights = src.fConicWeights; |
| 64 | } |
| 65 | |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 66 | SkASSERT((*dst)->countPoints() == src.countPoints()); |
| 67 | SkASSERT((*dst)->countVerbs() == src.countVerbs()); |
| 68 | SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count()); |
| 69 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 70 | // Need to check this here in case (&src == dst) |
| 71 | bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1; |
| 72 | |
| 73 | matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt); |
| 74 | |
| 75 | /* |
| 76 | * Here we optimize the bounds computation, by noting if the bounds are |
| 77 | * already known, and if so, we just transform those as well and mark |
| 78 | * them as "known", rather than force the transformed path to have to |
| 79 | * recompute them. |
| 80 | * |
| 81 | * Special gotchas if the path is effectively empty (<= 1 point) or |
| 82 | * if it is non-finite. In those cases bounds need to stay empty, |
| 83 | * regardless of the matrix. |
| 84 | */ |
| 85 | if (canXformBounds) { |
| 86 | (*dst)->fBoundsIsDirty = false; |
| 87 | if (src.fIsFinite) { |
| 88 | matrix.mapRect(&(*dst)->fBounds, src.fBounds); |
| 89 | if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) { |
| 90 | (*dst)->fBounds.setEmpty(); |
| 91 | } |
| 92 | } else { |
| 93 | (*dst)->fIsFinite = false; |
| 94 | (*dst)->fBounds.setEmpty(); |
| 95 | } |
| 96 | } else { |
| 97 | (*dst)->fBoundsIsDirty = true; |
| 98 | } |
| 99 | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 100 | (*dst)->fSegmentMask = src.fSegmentMask; |
| 101 | |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 102 | // It's an oval only if it stays a rect. |
| 103 | (*dst)->fIsOval = src.fIsOval && matrix.rectStaysRect(); |
| 104 | |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 105 | SkDEBUGCODE((*dst)->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 109 | #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V16_AND_ALL_OTHER_INSTANCES_TOO |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 110 | , bool newFormat, int32_t oldPacked |
| 111 | #endif |
| 112 | ) { |
| 113 | SkPathRef* ref = SkNEW(SkPathRef); |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 114 | bool isOval; |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 115 | uint8_t segmentMask; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 116 | |
| 117 | int32_t packed; |
| 118 | if (!buffer->readS32(&packed)) { |
| 119 | SkDELETE(ref); |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1; |
| 124 | |
| 125 | #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V16_AND_ALL_OTHER_INSTANCES_TOO |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 126 | if (newFormat) { |
| 127 | #endif |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 128 | segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 129 | isOval = (packed >> kIsOval_SerializationShift) & 1; |
| 130 | #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V16_AND_ALL_OTHER_INSTANCES_TOO |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 131 | } else { |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 132 | segmentMask = (oldPacked >> SkPath::kOldSegmentMask_SerializationShift) & 0xF; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 133 | isOval = (oldPacked >> SkPath::kOldIsOval_SerializationShift) & 1; |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 134 | } |
| 135 | #endif |
| 136 | |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 137 | int32_t verbCount, pointCount, conicCount; |
| 138 | if (!buffer->readU32(&(ref->fGenerationID)) || |
| 139 | !buffer->readS32(&verbCount) || |
| 140 | !buffer->readS32(&pointCount) || |
| 141 | !buffer->readS32(&conicCount)) { |
| 142 | SkDELETE(ref); |
| 143 | return NULL; |
| 144 | } |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 145 | |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 146 | ref->resetToSize(verbCount, pointCount, conicCount); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 147 | SkASSERT(verbCount == ref->countVerbs()); |
| 148 | SkASSERT(pointCount == ref->countPoints()); |
| 149 | SkASSERT(conicCount == ref->fConicWeights.count()); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 150 | |
| 151 | if (!buffer->read(ref->verbsMemWritable(), verbCount * sizeof(uint8_t)) || |
| 152 | !buffer->read(ref->fPoints, pointCount * sizeof(SkPoint)) || |
| 153 | !buffer->read(ref->fConicWeights.begin(), conicCount * sizeof(SkScalar)) || |
| 154 | !buffer->read(&ref->fBounds, sizeof(SkRect))) { |
| 155 | SkDELETE(ref); |
| 156 | return NULL; |
| 157 | } |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 158 | ref->fBoundsIsDirty = false; |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 159 | |
| 160 | // resetToSize clears fSegmentMask and fIsOval |
| 161 | ref->fSegmentMask = segmentMask; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 162 | ref->fIsOval = isOval; |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 163 | return ref; |
| 164 | } |
| 165 | |
| 166 | void SkPathRef::Rewind(SkAutoTUnref<SkPathRef>* pathRef) { |
| 167 | if ((*pathRef)->unique()) { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 168 | SkDEBUGCODE((*pathRef)->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 169 | (*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.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 175 | (*pathRef)->fSegmentMask = 0; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 176 | (*pathRef)->fIsOval = false; |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 177 | SkDEBUGCODE((*pathRef)->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 178 | } else { |
| 179 | int oldVCnt = (*pathRef)->countVerbs(); |
| 180 | int oldPCnt = (*pathRef)->countPoints(); |
| 181 | pathRef->reset(SkNEW(SkPathRef)); |
| 182 | (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | bool SkPathRef::operator== (const SkPathRef& ref) const { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 187 | SkDEBUGCODE(this->validate();) |
| 188 | SkDEBUGCODE(ref.validate();) |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 189 | |
| 190 | // We explicitly check fSegmentMask as a quick-reject. We could skip it, |
| 191 | // since it is only a cache of info in the fVerbs, but its a fast way to |
| 192 | // notice a difference |
| 193 | if (fSegmentMask != ref.fSegmentMask) { |
| 194 | return false; |
| 195 | } |
| 196 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 197 | bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID; |
| 198 | #ifdef SK_RELEASE |
| 199 | if (genIDMatch) { |
| 200 | return true; |
| 201 | } |
| 202 | #endif |
| 203 | if (fPointCnt != ref.fPointCnt || |
| 204 | fVerbCnt != ref.fVerbCnt) { |
| 205 | SkASSERT(!genIDMatch); |
| 206 | return false; |
| 207 | } |
| 208 | if (0 != memcmp(this->verbsMemBegin(), |
| 209 | ref.verbsMemBegin(), |
| 210 | ref.fVerbCnt * sizeof(uint8_t))) { |
| 211 | SkASSERT(!genIDMatch); |
| 212 | return false; |
| 213 | } |
| 214 | if (0 != memcmp(this->points(), |
| 215 | ref.points(), |
| 216 | ref.fPointCnt * sizeof(SkPoint))) { |
| 217 | SkASSERT(!genIDMatch); |
| 218 | return false; |
| 219 | } |
| 220 | if (fConicWeights != ref.fConicWeights) { |
| 221 | SkASSERT(!genIDMatch); |
| 222 | return false; |
| 223 | } |
| 224 | // We've done the work to determine that these are equal. If either has a zero genID, copy |
| 225 | // the other's. If both are 0 then genID() will compute the next ID. |
| 226 | if (0 == fGenerationID) { |
| 227 | fGenerationID = ref.genID(); |
| 228 | } else if (0 == ref.fGenerationID) { |
| 229 | ref.fGenerationID = this->genID(); |
| 230 | } |
| 231 | return true; |
| 232 | } |
| 233 | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 234 | void SkPathRef::writeToBuffer(SkWBuffer* buffer) const { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 235 | SkDEBUGCODE(this->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 236 | SkDEBUGCODE(size_t beforePos = buffer->pos();) |
| 237 | |
| 238 | // Call getBounds() to ensure (as a side-effect) that fBounds |
| 239 | // and fIsFinite are computed. |
| 240 | const SkRect& bounds = this->getBounds(); |
| 241 | |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 242 | int32_t packed = ((fIsFinite & 1) << kIsFinite_SerializationShift) | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 243 | ((fIsOval & 1) << kIsOval_SerializationShift) | |
| 244 | (fSegmentMask << kSegmentMask_SerializationShift); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 245 | buffer->write32(packed); |
| 246 | |
| 247 | // TODO: write gen ID here. Problem: We don't know if we're cross process or not from |
| 248 | // SkWBuffer. Until this is fixed we write 0. |
| 249 | buffer->write32(0); |
| 250 | buffer->write32(fVerbCnt); |
| 251 | buffer->write32(fPointCnt); |
| 252 | buffer->write32(fConicWeights.count()); |
| 253 | buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t)); |
| 254 | buffer->write(fPoints, fPointCnt * sizeof(SkPoint)); |
| 255 | buffer->write(fConicWeights.begin(), fConicWeights.bytes()); |
| 256 | buffer->write(&bounds, sizeof(bounds)); |
| 257 | |
| 258 | SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize()); |
| 259 | } |
| 260 | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 261 | uint32_t SkPathRef::writeSize() const { |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 262 | return uint32_t(5 * sizeof(uint32_t) + |
| 263 | fVerbCnt * sizeof(uint8_t) + |
| 264 | fPointCnt * sizeof(SkPoint) + |
| 265 | fConicWeights.bytes() + |
| 266 | sizeof(SkRect)); |
| 267 | } |
| 268 | |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 269 | void SkPathRef::copy(const SkPathRef& ref, |
| 270 | int additionalReserveVerbs, |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 271 | int additionalReservePoints) { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 272 | SkDEBUGCODE(this->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 273 | this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(), |
| 274 | additionalReserveVerbs, additionalReservePoints); |
| 275 | memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt * sizeof(uint8_t)); |
| 276 | memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint)); |
| 277 | fConicWeights = ref.fConicWeights; |
| 278 | // We could call genID() here to force a real ID (instead of 0). However, if we're making |
| 279 | // a copy then presumably we intend to make a modification immediately afterwards. |
| 280 | fGenerationID = ref.fGenerationID; |
| 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) { |
| 362 | SkASSERT(NULL != weights); |
| 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 | |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 445 | #ifdef SK_DEBUG |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 446 | void SkPathRef::validate() const { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 447 | this->INHERITED::validate(); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 448 | SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0); |
| 449 | SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints) >= 0); |
| 450 | SkASSERT((NULL == fPoints) == (NULL == fVerbs)); |
| 451 | SkASSERT(!(NULL == fPoints && 0 != fFreeSpace)); |
| 452 | SkASSERT(!(NULL == fPoints && 0 != fFreeSpace)); |
| 453 | SkASSERT(!(NULL == fPoints && fPointCnt)); |
| 454 | SkASSERT(!(NULL == fVerbs && fVerbCnt)); |
| 455 | SkASSERT(this->currSize() == |
| 456 | fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt); |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 457 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 458 | if (!fBoundsIsDirty && !fBounds.isEmpty()) { |
| 459 | bool isFinite = true; |
| 460 | for (int i = 0; i < fPointCnt; ++i) { |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 461 | SkASSERT(!fPoints[i].isFinite() || ( |
| 462 | fBounds.fLeft - fPoints[i].fX < SK_ScalarNearlyZero && |
robertphillips@google.com | 1cc385b | 2013-10-17 12:17:27 +0000 | [diff] [blame] | 463 | fPoints[i].fX - fBounds.fRight < SK_ScalarNearlyZero && |
skia.committer@gmail.com | f84ad8f | 2013-10-18 07:01:59 +0000 | [diff] [blame] | 464 | fBounds.fTop - fPoints[i].fY < SK_ScalarNearlyZero && |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 465 | fPoints[i].fY - fBounds.fBottom < SK_ScalarNearlyZero)); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 466 | if (!fPoints[i].isFinite()) { |
| 467 | isFinite = false; |
| 468 | } |
| 469 | } |
| 470 | SkASSERT(SkToBool(fIsFinite) == isFinite); |
| 471 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 472 | |
| 473 | #ifdef SK_DEBUG_PATH |
| 474 | uint32_t mask = 0; |
| 475 | for (int i = 0; i < fVerbCnt; ++i) { |
| 476 | switch (fVerbs[~i]) { |
| 477 | case SkPath::kMove_Verb: |
| 478 | break; |
| 479 | case SkPath::kLine_Verb: |
| 480 | mask |= SkPath::kLine_SegmentMask; |
| 481 | break; |
| 482 | case SkPath::kQuad_Verb: |
| 483 | mask |= SkPath::kQuad_SegmentMask; |
| 484 | break; |
| 485 | case SkPath::kConic_Verb: |
| 486 | mask |= SkPath::kConic_SegmentMask; |
| 487 | break; |
| 488 | case SkPath::kCubic_Verb: |
| 489 | mask |= SkPath::kCubic_SegmentMask; |
| 490 | break; |
| 491 | case SkPath::kClose_Verb: |
| 492 | break; |
| 493 | case SkPath::kDone_Verb: |
| 494 | SkDEBUGFAIL("Done verb shouldn't be recorded."); |
| 495 | break; |
| 496 | default: |
| 497 | SkDEBUGFAIL("Unknown Verb"); |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | SkASSERT(mask == fSegmentMask); |
| 502 | #endif // SK_DEBUG_PATH |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 503 | } |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 504 | #endif |