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