OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBuffer.h" | 8 #include "SkBuffer.h" |
9 #include "SkOnce.h" | 9 #include "SkOnce.h" |
10 #include "SkPath.h" | 10 #include "SkPath.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 | 49 |
50 SkPathRef* SkPathRef::CreateEmpty() { | 50 SkPathRef* SkPathRef::CreateEmpty() { |
51 static SkOnce once; | 51 static SkOnce once; |
52 once([]{ | 52 once([]{ |
53 gEmpty = new SkPathRef; | 53 gEmpty = new SkPathRef; |
54 gEmpty->computeBounds(); // Avoids races later to be the first to do t
his. | 54 gEmpty->computeBounds(); // Avoids races later to be the first to do t
his. |
55 }); | 55 }); |
56 return SkRef(gEmpty); | 56 return SkRef(gEmpty); |
57 } | 57 } |
58 | 58 |
| 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) negativ
e |
| 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 |
59 void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst, | 114 void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst, |
60 const SkPathRef& src, | 115 const SkPathRef& src, |
61 const SkMatrix& matrix) { | 116 const SkMatrix& matrix) { |
62 SkDEBUGCODE(src.validate();) | 117 SkDEBUGCODE(src.validate();) |
63 if (matrix.isIdentity()) { | 118 if (matrix.isIdentity()) { |
64 if (*dst != &src) { | 119 if (*dst != &src) { |
65 src.ref(); | 120 src.ref(); |
66 dst->reset(const_cast<SkPathRef*>(&src)); | 121 dst->reset(const_cast<SkPathRef*>(&src)); |
67 SkDEBUGCODE((*dst)->validate();) | 122 SkDEBUGCODE((*dst)->validate();) |
68 } | 123 } |
(...skipping 14 matching lines...) Expand all Loading... |
83 SkASSERT((*dst)->countPoints() == src.countPoints()); | 138 SkASSERT((*dst)->countPoints() == src.countPoints()); |
84 SkASSERT((*dst)->countVerbs() == src.countVerbs()); | 139 SkASSERT((*dst)->countVerbs() == src.countVerbs()); |
85 SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count()); | 140 SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count()); |
86 | 141 |
87 // Need to check this here in case (&src == dst) | 142 // Need to check this here in case (&src == dst) |
88 bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.c
ountPoints() > 1; | 143 bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.c
ountPoints() > 1; |
89 | 144 |
90 matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt); | 145 matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt); |
91 | 146 |
92 /* | 147 /* |
93 * Here we optimize the bounds computation, by noting if the bounds are | 148 * Here we optimize the bounds computation, by noting if the bounds are |
94 * already known, and if so, we just transform those as well and mark | 149 * already known, and if so, we just transform those as well and mark |
95 * them as "known", rather than force the transformed path to have to | 150 * them as "known", rather than force the transformed path to have to |
96 * recompute them. | 151 * recompute them. |
97 * | 152 * |
98 * Special gotchas if the path is effectively empty (<= 1 point) or | 153 * Special gotchas if the path is effectively empty (<= 1 point) or |
99 * if it is non-finite. In those cases bounds need to stay empty, | 154 * if it is non-finite. In those cases bounds need to stay empty, |
100 * regardless of the matrix. | 155 * regardless of the matrix. |
101 */ | 156 */ |
102 if (canXformBounds) { | 157 if (canXformBounds) { |
103 (*dst)->fBoundsIsDirty = false; | 158 (*dst)->fBoundsIsDirty = false; |
104 if (src.fIsFinite) { | 159 if (src.fIsFinite) { |
105 matrix.mapRect(&(*dst)->fBounds, src.fBounds); | 160 matrix.mapRect(&(*dst)->fBounds, src.fBounds); |
106 if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) { | 161 if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) { |
107 (*dst)->fBounds.setEmpty(); | 162 (*dst)->fBounds.setEmpty(); |
108 } | 163 } |
109 } else { | 164 } else { |
110 (*dst)->fIsFinite = false; | 165 (*dst)->fIsFinite = false; |
111 (*dst)->fBounds.setEmpty(); | 166 (*dst)->fBounds.setEmpty(); |
112 } | 167 } |
113 } else { | 168 } else { |
114 (*dst)->fBoundsIsDirty = true; | 169 (*dst)->fBoundsIsDirty = true; |
115 } | 170 } |
116 | 171 |
117 (*dst)->fSegmentMask = src.fSegmentMask; | 172 (*dst)->fSegmentMask = src.fSegmentMask; |
118 | 173 |
119 // It's an oval only if it stays a rect. | 174 // It's an oval only if it stays a rect. |
120 bool rectStaysRect = matrix.rectStaysRect(); | 175 bool rectStaysRect = matrix.rectStaysRect(); |
121 (*dst)->fIsOval = src.fIsOval && rectStaysRect; | 176 (*dst)->fIsOval = src.fIsOval && rectStaysRect; |
122 (*dst)->fIsRRect = src.fIsRRect && rectStaysRect; | 177 (*dst)->fIsRRect = src.fIsRRect && rectStaysRect; |
| 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 } |
123 | 185 |
124 SkDEBUGCODE((*dst)->validate();) | 186 SkDEBUGCODE((*dst)->validate();) |
125 } | 187 } |
126 | 188 |
127 SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer) { | 189 SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer) { |
128 SkPathRef* ref = new SkPathRef; | 190 SkPathRef* ref = new SkPathRef; |
129 | 191 |
130 int32_t packed; | 192 int32_t packed; |
131 if (!buffer->readS32(&packed)) { | 193 if (!buffer->readS32(&packed)) { |
132 delete ref; | 194 delete ref; |
133 return nullptr; | 195 return nullptr; |
134 } | 196 } |
135 | 197 |
136 ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1; | 198 ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1; |
137 uint8_t segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF; | 199 uint8_t segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF; |
138 bool isOval = (packed >> kIsOval_SerializationShift) & 1; | 200 bool isOval = (packed >> kIsOval_SerializationShift) & 1; |
139 bool isRRect = (packed >> kIsRRect_SerializationShift) & 1; | 201 bool isRRect = (packed >> kIsRRect_SerializationShift) & 1; |
| 202 bool rrectOrOvalIsCCW = (packed >> kRRectOrOvalIsCCW_SerializationShift) & 1
; |
| 203 unsigned rrectOrOvalStartIdx = (packed >> kRRectOrOvalStartIdx_Serialization
Shift) & 0x7; |
140 | 204 |
141 int32_t verbCount, pointCount, conicCount; | 205 int32_t verbCount, pointCount, conicCount; |
142 ptrdiff_t maxPtrDiff = std::numeric_limits<ptrdiff_t>::max(); | 206 ptrdiff_t maxPtrDiff = std::numeric_limits<ptrdiff_t>::max(); |
143 if (!buffer->readU32(&(ref->fGenerationID)) || | 207 if (!buffer->readU32(&(ref->fGenerationID)) || |
144 !buffer->readS32(&verbCount) || | 208 !buffer->readS32(&verbCount) || |
145 verbCount < 0 || | 209 verbCount < 0 || |
146 static_cast<uint32_t>(verbCount) > maxPtrDiff/sizeof(uint8_t) || | 210 static_cast<uint32_t>(verbCount) > maxPtrDiff/sizeof(uint8_t) || |
147 !buffer->readS32(&pointCount) || | 211 !buffer->readS32(&pointCount) || |
148 pointCount < 0 || | 212 pointCount < 0 || |
149 static_cast<uint32_t>(pointCount) > maxPtrDiff/sizeof(SkPoint) || | 213 static_cast<uint32_t>(pointCount) > maxPtrDiff/sizeof(SkPoint) || |
(...skipping 16 matching lines...) Expand all Loading... |
166 !buffer->read(&ref->fBounds, sizeof(SkRect))) { | 230 !buffer->read(&ref->fBounds, sizeof(SkRect))) { |
167 delete ref; | 231 delete ref; |
168 return nullptr; | 232 return nullptr; |
169 } | 233 } |
170 ref->fBoundsIsDirty = false; | 234 ref->fBoundsIsDirty = false; |
171 | 235 |
172 // resetToSize clears fSegmentMask and fIsOval | 236 // resetToSize clears fSegmentMask and fIsOval |
173 ref->fSegmentMask = segmentMask; | 237 ref->fSegmentMask = segmentMask; |
174 ref->fIsOval = isOval; | 238 ref->fIsOval = isOval; |
175 ref->fIsRRect = isRRect; | 239 ref->fIsRRect = isRRect; |
| 240 ref->fRRectOrOvalIsCCW = rrectOrOvalIsCCW; |
| 241 ref->fRRectOrOvalStartIdx = rrectOrOvalStartIdx; |
176 return ref; | 242 return ref; |
177 } | 243 } |
178 | 244 |
179 void SkPathRef::Rewind(SkAutoTUnref<SkPathRef>* pathRef) { | 245 void SkPathRef::Rewind(SkAutoTUnref<SkPathRef>* pathRef) { |
180 if ((*pathRef)->unique()) { | 246 if ((*pathRef)->unique()) { |
181 SkDEBUGCODE((*pathRef)->validate();) | 247 SkDEBUGCODE((*pathRef)->validate();) |
182 (*pathRef)->callGenIDChangeListeners(); | 248 (*pathRef)->callGenIDChangeListeners(); |
183 (*pathRef)->fBoundsIsDirty = true; // this also invalidates fIsFinite | 249 (*pathRef)->fBoundsIsDirty = true; // this also invalidates fIsFinite |
184 (*pathRef)->fVerbCnt = 0; | 250 (*pathRef)->fVerbCnt = 0; |
185 (*pathRef)->fPointCnt = 0; | 251 (*pathRef)->fPointCnt = 0; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 } | 312 } |
247 | 313 |
248 void SkPathRef::writeToBuffer(SkWBuffer* buffer) const { | 314 void SkPathRef::writeToBuffer(SkWBuffer* buffer) const { |
249 SkDEBUGCODE(this->validate();) | 315 SkDEBUGCODE(this->validate();) |
250 SkDEBUGCODE(size_t beforePos = buffer->pos();) | 316 SkDEBUGCODE(size_t beforePos = buffer->pos();) |
251 | 317 |
252 // Call getBounds() to ensure (as a side-effect) that fBounds | 318 // Call getBounds() to ensure (as a side-effect) that fBounds |
253 // and fIsFinite are computed. | 319 // and fIsFinite are computed. |
254 const SkRect& bounds = this->getBounds(); | 320 const SkRect& bounds = this->getBounds(); |
255 | 321 |
256 int32_t packed = ((fIsFinite & 1) << kIsFinite_SerializationShift) | | 322 int32_t packed = ((fRRectOrOvalStartIdx & 7) << kRRectOrOvalStartIdx_Seriali
zationShift) | |
| 323 ((fRRectOrOvalIsCCW & 1) << kRRectOrOvalIsCCW_Serialization
Shift) | |
| 324 ((fIsFinite & 1) << kIsFinite_SerializationShift) | |
257 ((fIsOval & 1) << kIsOval_SerializationShift) | | 325 ((fIsOval & 1) << kIsOval_SerializationShift) | |
258 ((fIsRRect & 1) << kIsRRect_SerializationShift) | | 326 ((fIsRRect & 1) << kIsRRect_SerializationShift) | |
259 (fSegmentMask << kSegmentMask_SerializationShift); | 327 (fSegmentMask << kSegmentMask_SerializationShift); |
260 buffer->write32(packed); | 328 buffer->write32(packed); |
261 | 329 |
262 // TODO: write gen ID here. Problem: We don't know if we're cross process or
not from | 330 // TODO: write gen ID here. Problem: We don't know if we're cross process or
not from |
263 // SkWBuffer. Until this is fixed we write 0. | 331 // SkWBuffer. Until this is fixed we write 0. |
264 buffer->write32(0); | 332 buffer->write32(0); |
265 buffer->write32(fVerbCnt); | 333 buffer->write32(fVerbCnt); |
266 buffer->write32(fPointCnt); | 334 buffer->write32(fPointCnt); |
(...skipping 24 matching lines...) Expand all Loading... |
291 sk_careful_memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint
)); | 359 sk_careful_memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint
)); |
292 fConicWeights = ref.fConicWeights; | 360 fConicWeights = ref.fConicWeights; |
293 fBoundsIsDirty = ref.fBoundsIsDirty; | 361 fBoundsIsDirty = ref.fBoundsIsDirty; |
294 if (!fBoundsIsDirty) { | 362 if (!fBoundsIsDirty) { |
295 fBounds = ref.fBounds; | 363 fBounds = ref.fBounds; |
296 fIsFinite = ref.fIsFinite; | 364 fIsFinite = ref.fIsFinite; |
297 } | 365 } |
298 fSegmentMask = ref.fSegmentMask; | 366 fSegmentMask = ref.fSegmentMask; |
299 fIsOval = ref.fIsOval; | 367 fIsOval = ref.fIsOval; |
300 fIsRRect = ref.fIsRRect; | 368 fIsRRect = ref.fIsRRect; |
| 369 fRRectOrOvalIsCCW = ref.fRRectOrOvalIsCCW; |
| 370 fRRectOrOvalStartIdx = ref.fRRectOrOvalStartIdx; |
301 SkDEBUGCODE(this->validate();) | 371 SkDEBUGCODE(this->validate();) |
302 } | 372 } |
303 | 373 |
304 | 374 |
305 void SkPathRef::interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef*
out) const { | 375 void SkPathRef::interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef*
out) const { |
306 const SkScalar* inValues = &ending.getPoints()->fX; | 376 const SkScalar* inValues = &ending.getPoints()->fX; |
307 SkScalar* outValues = &out->getPoints()->fX; | 377 SkScalar* outValues = &out->getPoints()->fX; |
308 int count = out->countPoints() * 2; | 378 int count = out->countPoints() * 2; |
309 for (int index = 0; index < count; ++index) { | 379 for (int index = 0; index < count; ++index) { |
310 outValues[index] = outValues[index] * weight + inValues[index] * (1 - we
ight); | 380 outValues[index] = outValues[index] * weight + inValues[index] * (1 - we
ight); |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0); | 679 SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0); |
610 SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPo
ints) >= 0); | 680 SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPo
ints) >= 0); |
611 SkASSERT((nullptr == fPoints) == (nullptr == fVerbs)); | 681 SkASSERT((nullptr == fPoints) == (nullptr == fVerbs)); |
612 SkASSERT(!(nullptr == fPoints && 0 != fFreeSpace)); | 682 SkASSERT(!(nullptr == fPoints && 0 != fFreeSpace)); |
613 SkASSERT(!(nullptr == fPoints && 0 != fFreeSpace)); | 683 SkASSERT(!(nullptr == fPoints && 0 != fFreeSpace)); |
614 SkASSERT(!(nullptr == fPoints && fPointCnt)); | 684 SkASSERT(!(nullptr == fPoints && fPointCnt)); |
615 SkASSERT(!(nullptr == fVerbs && fVerbCnt)); | 685 SkASSERT(!(nullptr == fVerbs && fVerbCnt)); |
616 SkASSERT(this->currSize() == | 686 SkASSERT(this->currSize() == |
617 fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVe
rbCnt); | 687 fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVe
rbCnt); |
618 | 688 |
| 689 if (fIsOval || fIsRRect) { |
| 690 // Currently we don't allow both of these to be set, even though ovals a
re round rects. |
| 691 SkASSERT(fIsOval != fIsRRect); |
| 692 if (fIsOval) { |
| 693 SkASSERT(fRRectOrOvalStartIdx < 4); |
| 694 } else { |
| 695 SkASSERT(fRRectOrOvalStartIdx < 8); |
| 696 } |
| 697 } |
| 698 |
619 if (!fBoundsIsDirty && !fBounds.isEmpty()) { | 699 if (!fBoundsIsDirty && !fBounds.isEmpty()) { |
620 bool isFinite = true; | 700 bool isFinite = true; |
621 for (int i = 0; i < fPointCnt; ++i) { | 701 for (int i = 0; i < fPointCnt; ++i) { |
622 #ifdef SK_DEBUG | 702 #ifdef SK_DEBUG |
623 if (fPoints[i].isFinite() && | 703 if (fPoints[i].isFinite() && |
624 (fPoints[i].fX < fBounds.fLeft || fPoints[i].fX > fBounds.fRight
|| | 704 (fPoints[i].fX < fBounds.fLeft || fPoints[i].fX > fBounds.fRight
|| |
625 fPoints[i].fY < fBounds.fTop || fPoints[i].fY > fBounds.fBottom
)) { | 705 fPoints[i].fY < fBounds.fTop || fPoints[i].fY > fBounds.fBottom
)) { |
626 SkDebugf("bounds: %f %f %f %f\n", | 706 SkDebugf("bounds: %f %f %f %f\n", |
627 fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fB
ottom); | 707 fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fB
ottom); |
628 for (int j = 0; j < fPointCnt; ++j) { | 708 for (int j = 0; j < fPointCnt; ++j) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
669 break; | 749 break; |
670 default: | 750 default: |
671 SkDEBUGFAIL("Unknown Verb"); | 751 SkDEBUGFAIL("Unknown Verb"); |
672 break; | 752 break; |
673 } | 753 } |
674 } | 754 } |
675 SkASSERT(mask == fSegmentMask); | 755 SkASSERT(mask == fSegmentMask); |
676 #endif // SK_DEBUG_PATH | 756 #endif // SK_DEBUG_PATH |
677 } | 757 } |
678 #endif | 758 #endif |
OLD | NEW |