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