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