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 | ffa4a92 | 2016-05-05 16:05:56 -0700 | [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" |
ajuma | 0735de6 | 2016-02-02 06:14:47 -0800 | [diff] [blame] | 12 | #include <limits> |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 13 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 14 | ////////////////////////////////////////////////////////////////////////////// |
| 15 | SkPathRef::Editor::Editor(SkAutoTUnref<SkPathRef>* pathRef, |
| 16 | int incReserveVerbs, |
| 17 | int incReservePoints) |
| 18 | { |
| 19 | if ((*pathRef)->unique()) { |
| 20 | (*pathRef)->incReserve(incReserveVerbs, incReservePoints); |
| 21 | } else { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 22 | SkPathRef* copy = new SkPathRef; |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 23 | copy->copy(**pathRef, incReserveVerbs, incReservePoints); |
| 24 | pathRef->reset(copy); |
| 25 | } |
| 26 | fPathRef = *pathRef; |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 27 | fPathRef->callGenIDChangeListeners(); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 28 | fPathRef->fGenerationID = 0; |
| 29 | SkDEBUGCODE(sk_atomic_inc(&fPathRef->fEditorsAttached);) |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 30 | } |
| 31 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 32 | ////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 33 | |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 34 | SkPathRef::~SkPathRef() { |
| 35 | this->callGenIDChangeListeners(); |
| 36 | SkDEBUGCODE(this->validate();) |
| 37 | sk_free(fPoints); |
| 38 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 39 | SkDEBUGCODE(fPoints = nullptr;) |
| 40 | SkDEBUGCODE(fVerbs = nullptr;) |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 41 | SkDEBUGCODE(fVerbCnt = 0x9999999;) |
| 42 | SkDEBUGCODE(fPointCnt = 0xAAAAAAA;) |
| 43 | SkDEBUGCODE(fPointCnt = 0xBBBBBBB;) |
| 44 | SkDEBUGCODE(fGenerationID = 0xEEEEEEEE;) |
| 45 | SkDEBUGCODE(fEditorsAttached = 0x7777777;) |
| 46 | } |
| 47 | |
mtklein | ffa4a92 | 2016-05-05 16:05:56 -0700 | [diff] [blame] | 48 | static SkPathRef* gEmpty = nullptr; |
| 49 | |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 50 | SkPathRef* SkPathRef::CreateEmpty() { |
mtklein | ffa4a92 | 2016-05-05 16:05:56 -0700 | [diff] [blame] | 51 | static SkOnce once; |
| 52 | once([]{ |
| 53 | gEmpty = new SkPathRef; |
| 54 | gEmpty->computeBounds(); // Avoids races later to be the first to do this. |
| 55 | }); |
| 56 | return SkRef(gEmpty); |
commit-bot@chromium.org | 1f81fd6 | 2013-10-23 14:44:08 +0000 | [diff] [blame] | 57 | } |
| 58 | |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 59 | static void transform_dir_and_start(const SkMatrix& matrix, bool isRRect, bool* isCCW, |
| 60 | unsigned* start) { |
| 61 | int inStart = *start; |
| 62 | int rm = 0; |
| 63 | if (isRRect) { |
| 64 | // Degenerate rrect indices to oval indices and remember the remainder. |
| 65 | // Ovals have one index per side whereas rrects have two. |
| 66 | rm = inStart & 0b1; |
| 67 | inStart /= 2; |
| 68 | } |
| 69 | // Is the antidiagonal non-zero (otherwise the diagonal is zero) |
| 70 | int antiDiag; |
| 71 | // Is the non-zero value in the top row (either kMScaleX or kMSkewX) negative |
| 72 | int topNeg; |
| 73 | // Are the two non-zero diagonal or antidiagonal values the same sign. |
| 74 | int sameSign; |
| 75 | if (matrix.get(SkMatrix::kMScaleX) != 0) { |
| 76 | antiDiag = 0b00; |
| 77 | if (matrix.get(SkMatrix::kMScaleX) > 0) { |
| 78 | topNeg = 0b00; |
| 79 | sameSign = matrix.get(SkMatrix::kMScaleY) > 0 ? 0b01 : 0b00; |
| 80 | } else { |
| 81 | topNeg = 0b10; |
| 82 | sameSign = matrix.get(SkMatrix::kMScaleY) > 0 ? 0b00 : 0b01; |
| 83 | } |
| 84 | } else { |
| 85 | antiDiag = 0b01; |
| 86 | if (matrix.get(SkMatrix::kMSkewX) > 0) { |
| 87 | topNeg = 0b00; |
| 88 | sameSign = matrix.get(SkMatrix::kMSkewY) > 0 ? 0b01 : 0b00; |
| 89 | } else { |
| 90 | topNeg = 0b10; |
| 91 | sameSign = matrix.get(SkMatrix::kMSkewY) > 0 ? 0b00 : 0b01; |
| 92 | } |
| 93 | } |
| 94 | if (sameSign != antiDiag) { |
| 95 | // This is a rotation (and maybe scale). The direction is unchanged. |
| 96 | // Trust me on the start computation (or draw yourself some pictures) |
| 97 | *start = (inStart + 4 - (topNeg | antiDiag)) % 4; |
| 98 | SkASSERT(*start < 4); |
| 99 | if (isRRect) { |
| 100 | *start = 2 * *start + rm; |
| 101 | } |
| 102 | } else { |
| 103 | // This is a mirror (and maybe scale). The direction is reversed. |
| 104 | *isCCW = !*isCCW; |
| 105 | // Trust me on the start computation (or draw yourself some pictures) |
| 106 | *start = (6 + (topNeg | antiDiag) - inStart) % 4; |
| 107 | SkASSERT(*start < 4); |
| 108 | if (isRRect) { |
| 109 | *start = 2 * *start + (rm ? 0 : 1); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 114 | void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst, |
| 115 | const SkPathRef& src, |
| 116 | const SkMatrix& matrix) { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 117 | SkDEBUGCODE(src.validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 118 | if (matrix.isIdentity()) { |
| 119 | if (*dst != &src) { |
| 120 | src.ref(); |
| 121 | dst->reset(const_cast<SkPathRef*>(&src)); |
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 | return; |
| 125 | } |
| 126 | |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 127 | if (!(*dst)->unique()) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 128 | dst->reset(new SkPathRef); |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | if (*dst != &src) { |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 132 | (*dst)->resetToSize(src.fVerbCnt, src.fPointCnt, src.fConicWeights.count()); |
mtklein | cc881da | 2015-12-08 11:55:17 -0800 | [diff] [blame] | 133 | sk_careful_memcpy((*dst)->verbsMemWritable(), src.verbsMemBegin(), |
| 134 | src.fVerbCnt * sizeof(uint8_t)); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 135 | (*dst)->fConicWeights = src.fConicWeights; |
| 136 | } |
| 137 | |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 138 | SkASSERT((*dst)->countPoints() == src.countPoints()); |
| 139 | SkASSERT((*dst)->countVerbs() == src.countVerbs()); |
| 140 | SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count()); |
| 141 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 142 | // Need to check this here in case (&src == dst) |
| 143 | bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1; |
| 144 | |
| 145 | matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt); |
| 146 | |
| 147 | /* |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 148 | * Here we optimize the bounds computation, by noting if the bounds are |
| 149 | * already known, and if so, we just transform those as well and mark |
| 150 | * them as "known", rather than force the transformed path to have to |
| 151 | * recompute them. |
| 152 | * |
| 153 | * Special gotchas if the path is effectively empty (<= 1 point) or |
| 154 | * if it is non-finite. In those cases bounds need to stay empty, |
| 155 | * regardless of the matrix. |
| 156 | */ |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 157 | if (canXformBounds) { |
| 158 | (*dst)->fBoundsIsDirty = false; |
| 159 | if (src.fIsFinite) { |
mtklein | 5c9c9be | 2014-12-01 06:59:54 -0800 | [diff] [blame] | 160 | matrix.mapRect(&(*dst)->fBounds, src.fBounds); |
| 161 | if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) { |
| 162 | (*dst)->fBounds.setEmpty(); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 163 | } |
| 164 | } else { |
| 165 | (*dst)->fIsFinite = false; |
mtklein | 5c9c9be | 2014-12-01 06:59:54 -0800 | [diff] [blame] | 166 | (*dst)->fBounds.setEmpty(); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 167 | } |
| 168 | } else { |
| 169 | (*dst)->fBoundsIsDirty = true; |
| 170 | } |
| 171 | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 172 | (*dst)->fSegmentMask = src.fSegmentMask; |
| 173 | |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 174 | // It's an oval only if it stays a rect. |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 175 | bool rectStaysRect = matrix.rectStaysRect(); |
| 176 | (*dst)->fIsOval = src.fIsOval && rectStaysRect; |
| 177 | (*dst)->fIsRRect = src.fIsRRect && rectStaysRect; |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 178 | if ((*dst)->fIsOval || (*dst)->fIsRRect) { |
| 179 | unsigned start = src.fRRectOrOvalStartIdx; |
| 180 | bool isCCW = SkToBool(src.fRRectOrOvalIsCCW); |
| 181 | transform_dir_and_start(matrix, (*dst)->fIsRRect, &isCCW, &start); |
| 182 | (*dst)->fRRectOrOvalIsCCW = isCCW; |
| 183 | (*dst)->fRRectOrOvalStartIdx = start; |
| 184 | } |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 185 | |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 186 | SkDEBUGCODE((*dst)->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 187 | } |
| 188 | |
commit-bot@chromium.org | fed2ab6 | 2014-01-23 15:16:05 +0000 | [diff] [blame] | 189 | SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 190 | SkPathRef* ref = new SkPathRef; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 191 | |
| 192 | int32_t packed; |
| 193 | if (!buffer->readS32(&packed)) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 194 | delete ref; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 195 | return nullptr; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1; |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 199 | uint8_t segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF; |
| 200 | bool isOval = (packed >> kIsOval_SerializationShift) & 1; |
| 201 | bool isRRect = (packed >> kIsRRect_SerializationShift) & 1; |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 202 | bool rrectOrOvalIsCCW = (packed >> kRRectOrOvalIsCCW_SerializationShift) & 1; |
| 203 | unsigned rrectOrOvalStartIdx = (packed >> kRRectOrOvalStartIdx_SerializationShift) & 0x7; |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 204 | |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 205 | int32_t verbCount, pointCount, conicCount; |
ajuma | 0735de6 | 2016-02-02 06:14:47 -0800 | [diff] [blame] | 206 | ptrdiff_t maxPtrDiff = std::numeric_limits<ptrdiff_t>::max(); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 207 | if (!buffer->readU32(&(ref->fGenerationID)) || |
| 208 | !buffer->readS32(&verbCount) || |
ajuma | f8aec58 | 2016-01-13 13:46:31 -0800 | [diff] [blame] | 209 | verbCount < 0 || |
ajuma | 0735de6 | 2016-02-02 06:14:47 -0800 | [diff] [blame] | 210 | static_cast<uint32_t>(verbCount) > maxPtrDiff/sizeof(uint8_t) || |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 211 | !buffer->readS32(&pointCount) || |
ajuma | f8aec58 | 2016-01-13 13:46:31 -0800 | [diff] [blame] | 212 | pointCount < 0 || |
ajuma | 0735de6 | 2016-02-02 06:14:47 -0800 | [diff] [blame] | 213 | static_cast<uint32_t>(pointCount) > maxPtrDiff/sizeof(SkPoint) || |
| 214 | sizeof(uint8_t) * verbCount + sizeof(SkPoint) * pointCount > |
| 215 | static_cast<size_t>(maxPtrDiff) || |
ajuma | f8aec58 | 2016-01-13 13:46:31 -0800 | [diff] [blame] | 216 | !buffer->readS32(&conicCount) || |
| 217 | conicCount < 0) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 218 | delete ref; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 219 | return nullptr; |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 220 | } |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 221 | |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 222 | ref->resetToSize(verbCount, pointCount, conicCount); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 223 | SkASSERT(verbCount == ref->countVerbs()); |
| 224 | SkASSERT(pointCount == ref->countPoints()); |
| 225 | SkASSERT(conicCount == ref->fConicWeights.count()); |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 226 | |
| 227 | if (!buffer->read(ref->verbsMemWritable(), verbCount * sizeof(uint8_t)) || |
| 228 | !buffer->read(ref->fPoints, pointCount * sizeof(SkPoint)) || |
| 229 | !buffer->read(ref->fConicWeights.begin(), conicCount * sizeof(SkScalar)) || |
| 230 | !buffer->read(&ref->fBounds, sizeof(SkRect))) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 231 | delete ref; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 232 | return nullptr; |
commit-bot@chromium.org | 8f457e3 | 2013-11-08 19:22:57 +0000 | [diff] [blame] | 233 | } |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 234 | ref->fBoundsIsDirty = false; |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 235 | |
| 236 | // resetToSize clears fSegmentMask and fIsOval |
| 237 | ref->fSegmentMask = segmentMask; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 238 | ref->fIsOval = isOval; |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 239 | ref->fIsRRect = isRRect; |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 240 | ref->fRRectOrOvalIsCCW = rrectOrOvalIsCCW; |
| 241 | ref->fRRectOrOvalStartIdx = rrectOrOvalStartIdx; |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 242 | return ref; |
| 243 | } |
| 244 | |
| 245 | void SkPathRef::Rewind(SkAutoTUnref<SkPathRef>* pathRef) { |
| 246 | if ((*pathRef)->unique()) { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 247 | SkDEBUGCODE((*pathRef)->validate();) |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 248 | (*pathRef)->callGenIDChangeListeners(); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 249 | (*pathRef)->fBoundsIsDirty = true; // this also invalidates fIsFinite |
| 250 | (*pathRef)->fVerbCnt = 0; |
| 251 | (*pathRef)->fPointCnt = 0; |
| 252 | (*pathRef)->fFreeSpace = (*pathRef)->currSize(); |
| 253 | (*pathRef)->fGenerationID = 0; |
| 254 | (*pathRef)->fConicWeights.rewind(); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 255 | (*pathRef)->fSegmentMask = 0; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 256 | (*pathRef)->fIsOval = false; |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 257 | (*pathRef)->fIsRRect = false; |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 258 | SkDEBUGCODE((*pathRef)->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 259 | } else { |
| 260 | int oldVCnt = (*pathRef)->countVerbs(); |
| 261 | int oldPCnt = (*pathRef)->countPoints(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 262 | pathRef->reset(new SkPathRef); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 263 | (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | bool SkPathRef::operator== (const SkPathRef& ref) const { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 268 | SkDEBUGCODE(this->validate();) |
| 269 | SkDEBUGCODE(ref.validate();) |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 270 | |
| 271 | // We explicitly check fSegmentMask as a quick-reject. We could skip it, |
| 272 | // since it is only a cache of info in the fVerbs, but its a fast way to |
| 273 | // notice a difference |
| 274 | if (fSegmentMask != ref.fSegmentMask) { |
| 275 | return false; |
| 276 | } |
| 277 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 278 | bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID; |
| 279 | #ifdef SK_RELEASE |
| 280 | if (genIDMatch) { |
| 281 | return true; |
| 282 | } |
| 283 | #endif |
| 284 | if (fPointCnt != ref.fPointCnt || |
| 285 | fVerbCnt != ref.fVerbCnt) { |
| 286 | SkASSERT(!genIDMatch); |
| 287 | return false; |
| 288 | } |
mtklein | d489759 | 2014-11-14 09:22:40 -0800 | [diff] [blame] | 289 | if (0 == ref.fVerbCnt) { |
| 290 | SkASSERT(0 == ref.fPointCnt); |
| 291 | return true; |
| 292 | } |
| 293 | SkASSERT(this->verbsMemBegin() && ref.verbsMemBegin()); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 294 | if (0 != memcmp(this->verbsMemBegin(), |
| 295 | ref.verbsMemBegin(), |
| 296 | ref.fVerbCnt * sizeof(uint8_t))) { |
| 297 | SkASSERT(!genIDMatch); |
| 298 | return false; |
| 299 | } |
mtklein | d489759 | 2014-11-14 09:22:40 -0800 | [diff] [blame] | 300 | SkASSERT(this->points() && ref.points()); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 301 | if (0 != memcmp(this->points(), |
| 302 | ref.points(), |
| 303 | ref.fPointCnt * sizeof(SkPoint))) { |
| 304 | SkASSERT(!genIDMatch); |
| 305 | return false; |
| 306 | } |
| 307 | if (fConicWeights != ref.fConicWeights) { |
| 308 | SkASSERT(!genIDMatch); |
| 309 | return false; |
| 310 | } |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 311 | return true; |
| 312 | } |
| 313 | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 314 | void SkPathRef::writeToBuffer(SkWBuffer* buffer) const { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 315 | SkDEBUGCODE(this->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 316 | SkDEBUGCODE(size_t beforePos = buffer->pos();) |
| 317 | |
| 318 | // Call getBounds() to ensure (as a side-effect) that fBounds |
| 319 | // and fIsFinite are computed. |
| 320 | const SkRect& bounds = this->getBounds(); |
| 321 | |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 322 | int32_t packed = ((fRRectOrOvalStartIdx & 7) << kRRectOrOvalStartIdx_SerializationShift) | |
| 323 | ((fRRectOrOvalIsCCW & 1) << kRRectOrOvalIsCCW_SerializationShift) | |
| 324 | ((fIsFinite & 1) << kIsFinite_SerializationShift) | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 325 | ((fIsOval & 1) << kIsOval_SerializationShift) | |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 326 | ((fIsRRect & 1) << kIsRRect_SerializationShift) | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 327 | (fSegmentMask << kSegmentMask_SerializationShift); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 328 | buffer->write32(packed); |
| 329 | |
| 330 | // TODO: write gen ID here. Problem: We don't know if we're cross process or not from |
| 331 | // SkWBuffer. Until this is fixed we write 0. |
| 332 | buffer->write32(0); |
| 333 | buffer->write32(fVerbCnt); |
| 334 | buffer->write32(fPointCnt); |
| 335 | buffer->write32(fConicWeights.count()); |
| 336 | buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t)); |
| 337 | buffer->write(fPoints, fPointCnt * sizeof(SkPoint)); |
| 338 | buffer->write(fConicWeights.begin(), fConicWeights.bytes()); |
| 339 | buffer->write(&bounds, sizeof(bounds)); |
| 340 | |
| 341 | SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize()); |
| 342 | } |
| 343 | |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 344 | uint32_t SkPathRef::writeSize() const { |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 345 | return uint32_t(5 * sizeof(uint32_t) + |
| 346 | fVerbCnt * sizeof(uint8_t) + |
| 347 | fPointCnt * sizeof(SkPoint) + |
| 348 | fConicWeights.bytes() + |
| 349 | sizeof(SkRect)); |
| 350 | } |
| 351 | |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 352 | void SkPathRef::copy(const SkPathRef& ref, |
| 353 | int additionalReserveVerbs, |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 354 | int additionalReservePoints) { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 355 | SkDEBUGCODE(this->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 356 | this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(), |
| 357 | additionalReserveVerbs, additionalReservePoints); |
mtklein | cc881da | 2015-12-08 11:55:17 -0800 | [diff] [blame] | 358 | sk_careful_memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt*sizeof(uint8_t)); |
| 359 | sk_careful_memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint)); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 360 | fConicWeights = ref.fConicWeights; |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 361 | fBoundsIsDirty = ref.fBoundsIsDirty; |
| 362 | if (!fBoundsIsDirty) { |
| 363 | fBounds = ref.fBounds; |
| 364 | fIsFinite = ref.fIsFinite; |
| 365 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 366 | fSegmentMask = ref.fSegmentMask; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 367 | fIsOval = ref.fIsOval; |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 368 | fIsRRect = ref.fIsRRect; |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 369 | fRRectOrOvalIsCCW = ref.fRRectOrOvalIsCCW; |
| 370 | fRRectOrOvalStartIdx = ref.fRRectOrOvalStartIdx; |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 371 | SkDEBUGCODE(this->validate();) |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 372 | } |
| 373 | |
caryclark | 8e7b19d | 2016-02-18 04:11:48 -0800 | [diff] [blame] | 374 | |
| 375 | void SkPathRef::interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef* out) const { |
| 376 | const SkScalar* inValues = &ending.getPoints()->fX; |
| 377 | SkScalar* outValues = &out->getPoints()->fX; |
| 378 | int count = out->countPoints() * 2; |
| 379 | for (int index = 0; index < count; ++index) { |
| 380 | outValues[index] = outValues[index] * weight + inValues[index] * (1 - weight); |
| 381 | } |
| 382 | out->fBoundsIsDirty = true; |
| 383 | out->fIsOval = false; |
| 384 | out->fIsRRect = false; |
| 385 | } |
| 386 | |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 387 | SkPoint* SkPathRef::growForRepeatedVerb(int /*SkPath::Verb*/ verb, |
| 388 | int numVbs, |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 389 | SkScalar** weights) { |
| 390 | // This value is just made-up for now. When count is 4, calling memset was much |
| 391 | // slower than just writing the loop. This seems odd, and hopefully in the |
| 392 | // future this will appear to have been a fluke... |
| 393 | static const unsigned int kMIN_COUNT_FOR_MEMSET_TO_BE_FAST = 16; |
| 394 | |
| 395 | SkDEBUGCODE(this->validate();) |
| 396 | int pCnt; |
| 397 | bool dirtyAfterEdit = true; |
| 398 | switch (verb) { |
| 399 | case SkPath::kMove_Verb: |
| 400 | pCnt = numVbs; |
| 401 | dirtyAfterEdit = false; |
| 402 | break; |
| 403 | case SkPath::kLine_Verb: |
| 404 | fSegmentMask |= SkPath::kLine_SegmentMask; |
| 405 | pCnt = numVbs; |
| 406 | break; |
| 407 | case SkPath::kQuad_Verb: |
| 408 | fSegmentMask |= SkPath::kQuad_SegmentMask; |
| 409 | pCnt = 2 * numVbs; |
| 410 | break; |
| 411 | case SkPath::kConic_Verb: |
| 412 | fSegmentMask |= SkPath::kConic_SegmentMask; |
| 413 | pCnt = 2 * numVbs; |
| 414 | break; |
| 415 | case SkPath::kCubic_Verb: |
| 416 | fSegmentMask |= SkPath::kCubic_SegmentMask; |
| 417 | pCnt = 3 * numVbs; |
| 418 | break; |
| 419 | case SkPath::kClose_Verb: |
| 420 | SkDEBUGFAIL("growForRepeatedVerb called for kClose_Verb"); |
| 421 | pCnt = 0; |
| 422 | dirtyAfterEdit = false; |
| 423 | break; |
| 424 | case SkPath::kDone_Verb: |
| 425 | SkDEBUGFAIL("growForRepeatedVerb called for kDone"); |
| 426 | // fall through |
| 427 | default: |
| 428 | SkDEBUGFAIL("default should not be reached"); |
| 429 | pCnt = 0; |
| 430 | dirtyAfterEdit = false; |
| 431 | } |
| 432 | |
| 433 | size_t space = numVbs * sizeof(uint8_t) + pCnt * sizeof (SkPoint); |
| 434 | this->makeSpace(space); |
| 435 | |
| 436 | SkPoint* ret = fPoints + fPointCnt; |
| 437 | uint8_t* vb = fVerbs - fVerbCnt; |
| 438 | |
| 439 | // cast to unsigned, so if kMIN_COUNT_FOR_MEMSET_TO_BE_FAST is defined to |
| 440 | // be 0, the compiler will remove the test/branch entirely. |
| 441 | if ((unsigned)numVbs >= kMIN_COUNT_FOR_MEMSET_TO_BE_FAST) { |
| 442 | memset(vb - numVbs, verb, numVbs); |
| 443 | } else { |
| 444 | for (int i = 0; i < numVbs; ++i) { |
| 445 | vb[~i] = verb; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | fVerbCnt += numVbs; |
| 450 | fPointCnt += pCnt; |
| 451 | fFreeSpace -= space; |
| 452 | fBoundsIsDirty = true; // this also invalidates fIsFinite |
| 453 | if (dirtyAfterEdit) { |
| 454 | fIsOval = false; |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 455 | fIsRRect = false; |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | if (SkPath::kConic_Verb == verb) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 459 | SkASSERT(weights); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 460 | *weights = fConicWeights.append(numVbs); |
| 461 | } |
| 462 | |
| 463 | SkDEBUGCODE(this->validate();) |
| 464 | return ret; |
| 465 | } |
| 466 | |
| 467 | SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb, SkScalar weight) { |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 468 | SkDEBUGCODE(this->validate();) |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 469 | int pCnt; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 470 | bool dirtyAfterEdit = true; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 471 | switch (verb) { |
| 472 | case SkPath::kMove_Verb: |
| 473 | pCnt = 1; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 474 | dirtyAfterEdit = false; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 475 | break; |
| 476 | case SkPath::kLine_Verb: |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 477 | fSegmentMask |= SkPath::kLine_SegmentMask; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 478 | pCnt = 1; |
| 479 | break; |
| 480 | case SkPath::kQuad_Verb: |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 481 | fSegmentMask |= SkPath::kQuad_SegmentMask; |
| 482 | pCnt = 2; |
| 483 | break; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 484 | case SkPath::kConic_Verb: |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 485 | fSegmentMask |= SkPath::kConic_SegmentMask; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 486 | pCnt = 2; |
| 487 | break; |
| 488 | case SkPath::kCubic_Verb: |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 489 | fSegmentMask |= SkPath::kCubic_SegmentMask; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 490 | pCnt = 3; |
| 491 | break; |
| 492 | case SkPath::kClose_Verb: |
| 493 | pCnt = 0; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 494 | dirtyAfterEdit = false; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 495 | break; |
| 496 | case SkPath::kDone_Verb: |
| 497 | SkDEBUGFAIL("growForVerb called for kDone"); |
| 498 | // fall through |
| 499 | default: |
| 500 | SkDEBUGFAIL("default is not reached"); |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 501 | dirtyAfterEdit = false; |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 502 | pCnt = 0; |
| 503 | } |
| 504 | size_t space = sizeof(uint8_t) + pCnt * sizeof (SkPoint); |
| 505 | this->makeSpace(space); |
| 506 | this->fVerbs[~fVerbCnt] = verb; |
| 507 | SkPoint* ret = fPoints + fPointCnt; |
| 508 | fVerbCnt += 1; |
| 509 | fPointCnt += pCnt; |
| 510 | fFreeSpace -= space; |
| 511 | fBoundsIsDirty = true; // this also invalidates fIsFinite |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 512 | if (dirtyAfterEdit) { |
| 513 | fIsOval = false; |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 514 | fIsRRect = false; |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 515 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 516 | |
| 517 | if (SkPath::kConic_Verb == verb) { |
| 518 | *fConicWeights.append() = weight; |
| 519 | } |
| 520 | |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 521 | SkDEBUGCODE(this->validate();) |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 522 | return ret; |
| 523 | } |
| 524 | |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 525 | uint32_t SkPathRef::genID() const { |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 526 | SkASSERT(!fEditorsAttached); |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 527 | 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] | 528 | if (!fGenerationID) { |
| 529 | if (0 == fPointCnt && 0 == fVerbCnt) { |
| 530 | fGenerationID = kEmptyGenID; |
| 531 | } else { |
| 532 | static int32_t gPathRefGenerationID; |
| 533 | // do a loop in case our global wraps around, as we never want to return a 0 or the |
| 534 | // empty ID |
| 535 | do { |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 536 | fGenerationID = (sk_atomic_inc(&gPathRefGenerationID) + 1) & kMask; |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 537 | } while (fGenerationID <= kEmptyGenID); |
| 538 | } |
| 539 | } |
| 540 | return fGenerationID; |
| 541 | } |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 542 | |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 543 | void SkPathRef::addGenIDChangeListener(GenIDChangeListener* listener) { |
mtklein | ffa4a92 | 2016-05-05 16:05:56 -0700 | [diff] [blame] | 544 | if (nullptr == listener || this == gEmpty) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 545 | delete listener; |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 546 | return; |
| 547 | } |
| 548 | *fGenIDChangeListeners.append() = listener; |
| 549 | } |
| 550 | |
| 551 | // we need to be called *before* the genID gets changed or zerod |
| 552 | void SkPathRef::callGenIDChangeListeners() { |
| 553 | for (int i = 0; i < fGenIDChangeListeners.count(); i++) { |
| 554 | fGenIDChangeListeners[i]->onChange(); |
| 555 | } |
| 556 | |
| 557 | // Listeners get at most one shot, so whether these triggered or not, blow them away. |
| 558 | fGenIDChangeListeners.deleteAll(); |
| 559 | } |
| 560 | |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 561 | SkRRect SkPathRef::getRRect() const { |
| 562 | const SkRect& bounds = this->getBounds(); |
| 563 | SkVector radii[4] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}}; |
| 564 | Iter iter(*this); |
| 565 | SkPoint pts[4]; |
| 566 | uint8_t verb = iter.next(pts); |
| 567 | SkASSERT(SkPath::kMove_Verb == verb); |
| 568 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 569 | if (SkPath::kConic_Verb == verb) { |
| 570 | SkVector v1_0 = pts[1] - pts[0]; |
| 571 | SkVector v2_1 = pts[2] - pts[1]; |
| 572 | SkVector dxdy; |
| 573 | if (v1_0.fX) { |
| 574 | SkASSERT(!v2_1.fX && !v1_0.fY); |
| 575 | dxdy.set(SkScalarAbs(v1_0.fX), SkScalarAbs(v2_1.fY)); |
| 576 | } else if (!v1_0.fY) { |
| 577 | SkASSERT(!v2_1.fX || !v2_1.fY); |
| 578 | dxdy.set(SkScalarAbs(v2_1.fX), SkScalarAbs(v2_1.fY)); |
| 579 | } else { |
| 580 | SkASSERT(!v2_1.fY); |
| 581 | dxdy.set(SkScalarAbs(v2_1.fX), SkScalarAbs(v1_0.fY)); |
| 582 | } |
| 583 | SkRRect::Corner corner = |
| 584 | pts[1].fX == bounds.fLeft ? |
| 585 | pts[1].fY == bounds.fTop ? |
| 586 | SkRRect::kUpperLeft_Corner : SkRRect::kLowerLeft_Corner : |
| 587 | pts[1].fY == bounds.fTop ? |
| 588 | SkRRect::kUpperRight_Corner : SkRRect::kLowerRight_Corner; |
| 589 | SkASSERT(!radii[corner].fX && !radii[corner].fY); |
| 590 | radii[corner] = dxdy; |
| 591 | } else { |
| 592 | SkASSERT((verb == SkPath::kLine_Verb |
| 593 | && (!(pts[1].fX - pts[0].fX) || !(pts[1].fY - pts[0].fY))) |
| 594 | || verb == SkPath::kClose_Verb); |
| 595 | } |
| 596 | } |
| 597 | SkRRect rrect; |
| 598 | rrect.setRectRadii(bounds, radii); |
| 599 | return rrect; |
| 600 | } |
| 601 | |
| 602 | /////////////////////////////////////////////////////////////////////////////// |
| 603 | |
| 604 | SkPathRef::Iter::Iter() { |
| 605 | #ifdef SK_DEBUG |
| 606 | fPts = nullptr; |
| 607 | fConicWeights = nullptr; |
| 608 | #endif |
| 609 | // need to init enough to make next() harmlessly return kDone_Verb |
| 610 | fVerbs = nullptr; |
| 611 | fVerbStop = nullptr; |
| 612 | } |
| 613 | |
| 614 | SkPathRef::Iter::Iter(const SkPathRef& path) { |
| 615 | this->setPathRef(path); |
| 616 | } |
| 617 | |
| 618 | void SkPathRef::Iter::setPathRef(const SkPathRef& path) { |
| 619 | fPts = path.points(); |
| 620 | fVerbs = path.verbs(); |
| 621 | fVerbStop = path.verbsMemBegin(); |
caryclark | 6942442 | 2016-10-04 13:06:17 -0700 | [diff] [blame] | 622 | fConicWeights = path.conicWeights(); |
| 623 | if (fConicWeights) { |
| 624 | fConicWeights -= 1; // begin one behind |
| 625 | } |
caryclark | da707bf | 2015-11-19 14:47:43 -0800 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | uint8_t SkPathRef::Iter::next(SkPoint pts[4]) { |
| 629 | SkASSERT(pts); |
| 630 | if (fVerbs == fVerbStop) { |
| 631 | return (uint8_t) SkPath::kDone_Verb; |
| 632 | } |
| 633 | |
| 634 | // fVerbs points one beyond next verb so decrement first. |
| 635 | unsigned verb = *(--fVerbs); |
| 636 | const SkPoint* srcPts = fPts; |
| 637 | |
| 638 | switch (verb) { |
| 639 | case SkPath::kMove_Verb: |
| 640 | pts[0] = srcPts[0]; |
| 641 | srcPts += 1; |
| 642 | break; |
| 643 | case SkPath::kLine_Verb: |
| 644 | pts[0] = srcPts[-1]; |
| 645 | pts[1] = srcPts[0]; |
| 646 | srcPts += 1; |
| 647 | break; |
| 648 | case SkPath::kConic_Verb: |
| 649 | fConicWeights += 1; |
| 650 | // fall-through |
| 651 | case SkPath::kQuad_Verb: |
| 652 | pts[0] = srcPts[-1]; |
| 653 | pts[1] = srcPts[0]; |
| 654 | pts[2] = srcPts[1]; |
| 655 | srcPts += 2; |
| 656 | break; |
| 657 | case SkPath::kCubic_Verb: |
| 658 | pts[0] = srcPts[-1]; |
| 659 | pts[1] = srcPts[0]; |
| 660 | pts[2] = srcPts[1]; |
| 661 | pts[3] = srcPts[2]; |
| 662 | srcPts += 3; |
| 663 | break; |
| 664 | case SkPath::kClose_Verb: |
| 665 | break; |
| 666 | case SkPath::kDone_Verb: |
| 667 | SkASSERT(fVerbs == fVerbStop); |
| 668 | break; |
| 669 | } |
| 670 | fPts = srcPts; |
| 671 | return (uint8_t) verb; |
| 672 | } |
| 673 | |
caryclark | 2028d7f | 2015-12-09 14:04:46 -0800 | [diff] [blame] | 674 | uint8_t SkPathRef::Iter::peek() const { |
| 675 | const uint8_t* next = fVerbs - 1; |
| 676 | return next <= fVerbStop ? (uint8_t) SkPath::kDone_Verb : *next; |
| 677 | } |
| 678 | |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 679 | #ifdef SK_DEBUG |
caryclark | 0c52b17 | 2016-10-05 12:16:52 -0700 | [diff] [blame] | 680 | |
| 681 | #include "SkNx.h" |
| 682 | |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 683 | void SkPathRef::validate() const { |
| 684 | SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0); |
| 685 | SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints) >= 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 686 | SkASSERT((nullptr == fPoints) == (nullptr == fVerbs)); |
| 687 | SkASSERT(!(nullptr == fPoints && 0 != fFreeSpace)); |
| 688 | SkASSERT(!(nullptr == fPoints && 0 != fFreeSpace)); |
| 689 | SkASSERT(!(nullptr == fPoints && fPointCnt)); |
| 690 | SkASSERT(!(nullptr == fVerbs && fVerbCnt)); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 691 | SkASSERT(this->currSize() == |
| 692 | fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt); |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 693 | |
bsalomon | 78d58d1 | 2016-05-27 09:17:04 -0700 | [diff] [blame] | 694 | if (fIsOval || fIsRRect) { |
| 695 | // Currently we don't allow both of these to be set, even though ovals are round rects. |
| 696 | SkASSERT(fIsOval != fIsRRect); |
| 697 | if (fIsOval) { |
| 698 | SkASSERT(fRRectOrOvalStartIdx < 4); |
| 699 | } else { |
| 700 | SkASSERT(fRRectOrOvalStartIdx < 8); |
| 701 | } |
| 702 | } |
| 703 | |
mtklein | 5c9c9be | 2014-12-01 06:59:54 -0800 | [diff] [blame] | 704 | if (!fBoundsIsDirty && !fBounds.isEmpty()) { |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 705 | bool isFinite = true; |
caryclark | 0c52b17 | 2016-10-05 12:16:52 -0700 | [diff] [blame] | 706 | Sk2s leftTop = Sk2s(fBounds.fLeft, fBounds.fTop); |
| 707 | Sk2s rightBot = Sk2s(fBounds.fRight, fBounds.fBottom); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 708 | for (int i = 0; i < fPointCnt; ++i) { |
caryclark | 0c52b17 | 2016-10-05 12:16:52 -0700 | [diff] [blame] | 709 | Sk2s point = Sk2s(fPoints[i].fX, fPoints[i].fY); |
robertphillips | f1cdead | 2015-01-05 09:20:04 -0800 | [diff] [blame] | 710 | #ifdef SK_DEBUG |
| 711 | if (fPoints[i].isFinite() && |
caryclark | 0c52b17 | 2016-10-05 12:16:52 -0700 | [diff] [blame] | 712 | ((point < leftTop).anyTrue() || (point > rightBot).anyTrue())) { |
robertphillips | f1cdead | 2015-01-05 09:20:04 -0800 | [diff] [blame] | 713 | SkDebugf("bounds: %f %f %f %f\n", |
| 714 | fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom); |
| 715 | for (int j = 0; j < fPointCnt; ++j) { |
| 716 | if (i == j) { |
| 717 | SkDebugf("*"); |
| 718 | } |
| 719 | SkDebugf("%f %f\n", fPoints[j].fX, fPoints[j].fY); |
| 720 | } |
| 721 | } |
| 722 | #endif |
| 723 | |
robertphillips | 0e91246 | 2014-12-12 12:47:59 -0800 | [diff] [blame] | 724 | SkASSERT(!fPoints[i].isFinite() || |
caryclark | 0c52b17 | 2016-10-05 12:16:52 -0700 | [diff] [blame] | 725 | (!(point < leftTop).anyTrue() && !(point > rightBot).anyTrue())); |
robertphillips@google.com | 3e292aa | 2013-09-27 17:48:49 +0000 | [diff] [blame] | 726 | if (!fPoints[i].isFinite()) { |
| 727 | isFinite = false; |
| 728 | } |
| 729 | } |
| 730 | SkASSERT(SkToBool(fIsFinite) == isFinite); |
| 731 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 732 | |
| 733 | #ifdef SK_DEBUG_PATH |
| 734 | uint32_t mask = 0; |
| 735 | for (int i = 0; i < fVerbCnt; ++i) { |
| 736 | switch (fVerbs[~i]) { |
| 737 | case SkPath::kMove_Verb: |
| 738 | break; |
| 739 | case SkPath::kLine_Verb: |
| 740 | mask |= SkPath::kLine_SegmentMask; |
| 741 | break; |
| 742 | case SkPath::kQuad_Verb: |
| 743 | mask |= SkPath::kQuad_SegmentMask; |
| 744 | break; |
| 745 | case SkPath::kConic_Verb: |
| 746 | mask |= SkPath::kConic_SegmentMask; |
| 747 | break; |
| 748 | case SkPath::kCubic_Verb: |
| 749 | mask |= SkPath::kCubic_SegmentMask; |
| 750 | break; |
| 751 | case SkPath::kClose_Verb: |
| 752 | break; |
| 753 | case SkPath::kDone_Verb: |
| 754 | SkDEBUGFAIL("Done verb shouldn't be recorded."); |
| 755 | break; |
| 756 | default: |
| 757 | SkDEBUGFAIL("Unknown Verb"); |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | SkASSERT(mask == fSegmentMask); |
| 762 | #endif // SK_DEBUG_PATH |
robertphillips@google.com | ca0c838 | 2013-09-26 12:18:23 +0000 | [diff] [blame] | 763 | } |
robertphillips@google.com | 0308707 | 2013-10-02 16:42:21 +0000 | [diff] [blame] | 764 | #endif |