| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 #include "SkGeometry.h" | 7 #include "SkGeometry.h" |
| 8 #include "SkOpEdgeBuilder.h" | 8 #include "SkOpEdgeBuilder.h" |
| 9 #include "SkReduceOrder.h" | 9 #include "SkReduceOrder.h" |
| 10 | 10 |
| 11 void SkOpEdgeBuilder::init() { | 11 void SkOpEdgeBuilder::init() { |
| 12 fCurrentContour = NULL; | 12 fCurrentContour = fContoursHead; |
| 13 fOperand = false; | 13 fOperand = false; |
| 14 fXorMask[0] = fXorMask[1] = (fPath->getFillType() & 1) ? kEvenOdd_PathOpsMas
k | 14 fXorMask[0] = fXorMask[1] = (fPath->getFillType() & 1) ? kEvenOdd_PathOpsMas
k |
| 15 : kWinding_PathOpsMask; | 15 : kWinding_PathOpsMask; |
| 16 fUnparseable = false; | 16 fUnparseable = false; |
| 17 fSecondHalf = preFetch(); | 17 fSecondHalf = preFetch(); |
| 18 } | 18 } |
| 19 | 19 |
| 20 void SkOpEdgeBuilder::addOperand(const SkPath& path) { | 20 void SkOpEdgeBuilder::addOperand(const SkPath& path) { |
| 21 SkASSERT(fPathVerbs.count() > 0 && fPathVerbs.end()[-1] == SkPath::kDone_Ver
b); | 21 SkASSERT(fPathVerbs.count() > 0 && fPathVerbs.end()[-1] == SkPath::kDone_Ver
b); |
| 22 fPathVerbs.pop_back(); | 22 fPathVerbs.pop(); |
| 23 fPath = &path; | 23 fPath = &path; |
| 24 fXorMask[1] = (fPath->getFillType() & 1) ? kEvenOdd_PathOpsMask | 24 fXorMask[1] = (fPath->getFillType() & 1) ? kEvenOdd_PathOpsMask |
| 25 : kWinding_PathOpsMask; | 25 : kWinding_PathOpsMask; |
| 26 preFetch(); | 26 preFetch(); |
| 27 } | 27 } |
| 28 | 28 |
| 29 bool SkOpEdgeBuilder::finish() { | 29 int SkOpEdgeBuilder::count() const { |
| 30 if (fUnparseable || !walk()) { | 30 SkOpContour* contour = fContoursHead; |
| 31 int count = 0; |
| 32 while (contour) { |
| 33 count += contour->count() > 0; |
| 34 contour = contour->next(); |
| 35 } |
| 36 return count; |
| 37 } |
| 38 |
| 39 bool SkOpEdgeBuilder::finish(SkChunkAlloc* allocator) { |
| 40 fOperand = false; |
| 41 if (fUnparseable || !walk(allocator)) { |
| 31 return false; | 42 return false; |
| 32 } | 43 } |
| 33 complete(); | 44 complete(); |
| 34 if (fCurrentContour && !fCurrentContour->segments().count()) { | 45 if (fCurrentContour && !fCurrentContour->count()) { |
| 35 fContours.pop_back(); | 46 fContoursHead->remove(fCurrentContour); |
| 36 } | 47 } |
| 37 return true; | 48 return true; |
| 38 } | 49 } |
| 39 | 50 |
| 40 void SkOpEdgeBuilder::closeContour(const SkPoint& curveEnd, const SkPoint& curve
Start) { | 51 void SkOpEdgeBuilder::closeContour(const SkPoint& curveEnd, const SkPoint& curve
Start) { |
| 41 if (!SkDPoint::ApproximatelyEqual(curveEnd, curveStart)) { | 52 if (!SkDPoint::ApproximatelyEqual(curveEnd, curveStart)) { |
| 42 fPathVerbs.push_back(SkPath::kLine_Verb); | 53 *fPathVerbs.append() = SkPath::kLine_Verb; |
| 43 fPathPts.push_back_n(1, &curveStart); | 54 *fPathPts.append() = curveStart; |
| 44 } else { | 55 } else { |
| 45 fPathPts[fPathPts.count() - 1] = curveStart; | 56 fPathPts[fPathPts.count() - 1] = curveStart; |
| 46 } | 57 } |
| 47 fPathVerbs.push_back(SkPath::kClose_Verb); | 58 *fPathVerbs.append() = SkPath::kClose_Verb; |
| 48 } | 59 } |
| 49 | 60 |
| 50 // very tiny points cause numerical instability : don't allow them | 61 // very tiny points cause numerical instability : don't allow them |
| 51 static void force_small_to_zero(SkPoint* pt) { | 62 static void force_small_to_zero(SkPoint* pt) { |
| 52 if (SkScalarAbs(pt->fX) < FLT_EPSILON_ORDERABLE_ERR) { | 63 if (SkScalarAbs(pt->fX) < FLT_EPSILON_ORDERABLE_ERR) { |
| 53 pt->fX = 0; | 64 pt->fX = 0; |
| 54 } | 65 } |
| 55 if (SkScalarAbs(pt->fY) < FLT_EPSILON_ORDERABLE_ERR) { | 66 if (SkScalarAbs(pt->fY) < FLT_EPSILON_ORDERABLE_ERR) { |
| 56 pt->fY = 0; | 67 pt->fY = 0; |
| 57 } | 68 } |
| 58 } | 69 } |
| 59 | 70 |
| 60 | |
| 61 int SkOpEdgeBuilder::preFetch() { | 71 int SkOpEdgeBuilder::preFetch() { |
| 62 if (!fPath->isFinite()) { | 72 if (!fPath->isFinite()) { |
| 63 fUnparseable = true; | 73 fUnparseable = true; |
| 64 return 0; | 74 return 0; |
| 65 } | 75 } |
| 66 SkAutoConicToQuads quadder; | 76 SkAutoConicToQuads quadder; |
| 67 const SkScalar quadderTol = SK_Scalar1 / 16; | 77 const SkScalar quadderTol = SK_Scalar1 / 16; |
| 68 SkPath::RawIter iter(*fPath); | 78 SkPath::RawIter iter(*fPath); |
| 69 SkPoint curveStart; | 79 SkPoint curveStart; |
| 70 SkPoint curve[4]; | 80 SkPoint curve[4]; |
| 71 SkPoint pts[4]; | 81 SkPoint pts[4]; |
| 72 SkPath::Verb verb; | 82 SkPath::Verb verb; |
| 73 bool lastCurve = false; | 83 bool lastCurve = false; |
| 74 do { | 84 do { |
| 75 verb = iter.next(pts); | 85 verb = iter.next(pts); |
| 76 switch (verb) { | 86 switch (verb) { |
| 77 case SkPath::kMove_Verb: | 87 case SkPath::kMove_Verb: |
| 78 if (!fAllowOpenContours && lastCurve) { | 88 if (!fAllowOpenContours && lastCurve) { |
| 79 closeContour(curve[0], curveStart); | 89 closeContour(curve[0], curveStart); |
| 80 } | 90 } |
| 81 fPathVerbs.push_back(verb); | 91 *fPathVerbs.append() = verb; |
| 82 force_small_to_zero(&pts[0]); | 92 force_small_to_zero(&pts[0]); |
| 83 fPathPts.push_back(pts[0]); | 93 *fPathPts.append() = pts[0]; |
| 84 curveStart = curve[0] = pts[0]; | 94 curveStart = curve[0] = pts[0]; |
| 85 lastCurve = false; | 95 lastCurve = false; |
| 86 continue; | 96 continue; |
| 87 case SkPath::kLine_Verb: | 97 case SkPath::kLine_Verb: |
| 88 force_small_to_zero(&pts[1]); | 98 force_small_to_zero(&pts[1]); |
| 89 if (SkDPoint::ApproximatelyEqual(curve[0], pts[1])) { | 99 if (SkDPoint::ApproximatelyEqual(curve[0], pts[1])) { |
| 90 uint8_t lastVerb = fPathVerbs.back(); | 100 uint8_t lastVerb = fPathVerbs.top(); |
| 91 if (lastVerb != SkPath::kLine_Verb && lastVerb != SkPath::kM
ove_Verb) { | 101 if (lastVerb != SkPath::kLine_Verb && lastVerb != SkPath::kM
ove_Verb) { |
| 92 fPathPts.back() = pts[1]; | 102 fPathPts.top() = pts[1]; |
| 93 } | 103 } |
| 94 continue; // skip degenerate points | 104 continue; // skip degenerate points |
| 95 } | 105 } |
| 96 break; | 106 break; |
| 97 case SkPath::kQuad_Verb: | 107 case SkPath::kQuad_Verb: |
| 98 force_small_to_zero(&pts[1]); | 108 force_small_to_zero(&pts[1]); |
| 99 force_small_to_zero(&pts[2]); | 109 force_small_to_zero(&pts[2]); |
| 100 curve[1] = pts[1]; | 110 curve[1] = pts[1]; |
| 101 curve[2] = pts[2]; | 111 curve[2] = pts[2]; |
| 102 verb = SkReduceOrder::Quad(curve, pts); | 112 verb = SkReduceOrder::Quad(curve, pts); |
| 103 if (verb == SkPath::kMove_Verb) { | 113 if (verb == SkPath::kMove_Verb) { |
| 104 continue; // skip degenerate points | 114 continue; // skip degenerate points |
| 105 } | 115 } |
| 106 break; | 116 break; |
| 107 case SkPath::kConic_Verb: { | 117 case SkPath::kConic_Verb: { |
| 108 const SkPoint* quadPts = quadder.computeQuads(pts, iter.coni
cWeight(), | 118 const SkPoint* quadPts = quadder.computeQuads(pts, iter.coni
cWeight(), |
| 109 quadderTol); | 119 quadderTol); |
| 110 const int nQuads = quadder.countQuads(); | 120 const int nQuads = quadder.countQuads(); |
| 111 for (int i = 0; i < nQuads; ++i) { | 121 for (int i = 0; i < nQuads; ++i) { |
| 112 fPathVerbs.push_back(SkPath::kQuad_Verb); | 122 *fPathVerbs.append() = SkPath::kQuad_Verb; |
| 113 } | 123 } |
| 114 fPathPts.push_back_n(nQuads * 2, &quadPts[1]); | 124 fPathPts.append(nQuads * 2, &quadPts[1]); |
| 115 curve[0] = pts[2]; | 125 curve[0] = pts[2]; |
| 116 lastCurve = true; | 126 lastCurve = true; |
| 117 } | 127 } |
| 118 continue; | 128 continue; |
| 119 case SkPath::kCubic_Verb: | 129 case SkPath::kCubic_Verb: |
| 120 force_small_to_zero(&pts[1]); | 130 force_small_to_zero(&pts[1]); |
| 121 force_small_to_zero(&pts[2]); | 131 force_small_to_zero(&pts[2]); |
| 122 force_small_to_zero(&pts[3]); | 132 force_small_to_zero(&pts[3]); |
| 123 curve[1] = pts[1]; | 133 curve[1] = pts[1]; |
| 124 curve[2] = pts[2]; | 134 curve[2] = pts[2]; |
| 125 curve[3] = pts[3]; | 135 curve[3] = pts[3]; |
| 126 verb = SkReduceOrder::Cubic(curve, pts); | 136 verb = SkReduceOrder::Cubic(curve, pts); |
| 127 if (verb == SkPath::kMove_Verb) { | 137 if (verb == SkPath::kMove_Verb) { |
| 128 continue; // skip degenerate points | 138 continue; // skip degenerate points |
| 129 } | 139 } |
| 130 break; | 140 break; |
| 131 case SkPath::kClose_Verb: | 141 case SkPath::kClose_Verb: |
| 132 closeContour(curve[0], curveStart); | 142 closeContour(curve[0], curveStart); |
| 133 lastCurve = false; | 143 lastCurve = false; |
| 134 continue; | 144 continue; |
| 135 case SkPath::kDone_Verb: | 145 case SkPath::kDone_Verb: |
| 136 continue; | 146 continue; |
| 137 } | 147 } |
| 138 fPathVerbs.push_back(verb); | 148 *fPathVerbs.append() = verb; |
| 139 int ptCount = SkPathOpsVerbToPoints(verb); | 149 int ptCount = SkPathOpsVerbToPoints(verb); |
| 140 fPathPts.push_back_n(ptCount, &pts[1]); | 150 fPathPts.append(ptCount, &pts[1]); |
| 141 curve[0] = pts[ptCount]; | 151 curve[0] = pts[ptCount]; |
| 142 lastCurve = true; | 152 lastCurve = true; |
| 143 } while (verb != SkPath::kDone_Verb); | 153 } while (verb != SkPath::kDone_Verb); |
| 144 if (!fAllowOpenContours && lastCurve) { | 154 if (!fAllowOpenContours && lastCurve) { |
| 145 closeContour(curve[0], curveStart); | 155 closeContour(curve[0], curveStart); |
| 146 } | 156 } |
| 147 fPathVerbs.push_back(SkPath::kDone_Verb); | 157 *fPathVerbs.append() = SkPath::kDone_Verb; |
| 148 return fPathVerbs.count() - 1; | 158 return fPathVerbs.count() - 1; |
| 149 } | 159 } |
| 150 | 160 |
| 151 bool SkOpEdgeBuilder::close() { | 161 bool SkOpEdgeBuilder::close() { |
| 152 complete(); | 162 complete(); |
| 153 return true; | 163 return true; |
| 154 } | 164 } |
| 155 | 165 |
| 156 bool SkOpEdgeBuilder::walk() { | 166 bool SkOpEdgeBuilder::walk(SkChunkAlloc* allocator) { |
| 157 uint8_t* verbPtr = fPathVerbs.begin(); | 167 uint8_t* verbPtr = fPathVerbs.begin(); |
| 158 uint8_t* endOfFirstHalf = &verbPtr[fSecondHalf]; | 168 uint8_t* endOfFirstHalf = &verbPtr[fSecondHalf]; |
| 159 const SkPoint* pointsPtr = fPathPts.begin() - 1; | 169 SkPoint* pointsPtr = fPathPts.begin() - 1; |
| 160 SkPath::Verb verb; | 170 SkPath::Verb verb; |
| 161 while ((verb = (SkPath::Verb) *verbPtr) != SkPath::kDone_Verb) { | 171 while ((verb = (SkPath::Verb) *verbPtr) != SkPath::kDone_Verb) { |
| 162 if (verbPtr == endOfFirstHalf) { | 172 if (verbPtr == endOfFirstHalf) { |
| 163 fOperand = true; | 173 fOperand = true; |
| 164 } | 174 } |
| 165 verbPtr++; | 175 verbPtr++; |
| 166 switch (verb) { | 176 switch (verb) { |
| 167 case SkPath::kMove_Verb: | 177 case SkPath::kMove_Verb: |
| 168 if (fCurrentContour) { | 178 if (fCurrentContour && fCurrentContour->count()) { |
| 169 if (fAllowOpenContours) { | 179 if (fAllowOpenContours) { |
| 170 complete(); | 180 complete(); |
| 171 } else if (!close()) { | 181 } else if (!close()) { |
| 172 return false; | 182 return false; |
| 173 } | 183 } |
| 174 } | 184 } |
| 175 if (!fCurrentContour) { | 185 if (!fCurrentContour) { |
| 176 fCurrentContour = fContours.push_back_n(1); | 186 fCurrentContour = fContoursHead->appendContour(allocator); |
| 177 fCurrentContour->setOperand(fOperand); | |
| 178 fCurrentContour->setXor(fXorMask[fOperand] == kEvenOdd_PathO
psMask); | |
| 179 } | 187 } |
| 188 fCurrentContour->init(fGlobalState, fOperand, |
| 189 fXorMask[fOperand] == kEvenOdd_PathOpsMask); |
| 180 pointsPtr += 1; | 190 pointsPtr += 1; |
| 181 continue; | 191 continue; |
| 182 case SkPath::kLine_Verb: | 192 case SkPath::kLine_Verb: |
| 183 fCurrentContour->addLine(pointsPtr); | 193 fCurrentContour->addLine(pointsPtr, fAllocator); |
| 184 break; | 194 break; |
| 185 case SkPath::kQuad_Verb: | 195 case SkPath::kQuad_Verb: |
| 186 fCurrentContour->addQuad(pointsPtr); | 196 fCurrentContour->addQuad(pointsPtr, fAllocator); |
| 187 break; | 197 break; |
| 188 case SkPath::kCubic_Verb: | 198 case SkPath::kCubic_Verb: { |
| 189 fCurrentContour->addCubic(pointsPtr); | 199 // split self-intersecting cubics in two before proceeding |
| 190 break; | 200 // if the cubic is convex, it doesn't self intersect. |
| 201 SkScalar loopT; |
| 202 if (SkDCubic::ComplexBreak(pointsPtr, &loopT)) { |
| 203 SkPoint cubicPair[7]; |
| 204 SkChopCubicAt(pointsPtr, cubicPair, loopT); |
| 205 SkPoint cStorage[2][4]; |
| 206 SkPath::Verb v1 = SkReduceOrder::Cubic(&cubicPair[0], cStora
ge[0]); |
| 207 SkPath::Verb v2 = SkReduceOrder::Cubic(&cubicPair[3], cStora
ge[1]); |
| 208 if (v1 != SkPath::kMove_Verb && v2 != SkPath::kMove_Verb) { |
| 209 SkPoint* curve1 = v1 == SkPath::kCubic_Verb ? &cubicPair
[0] : cStorage[0]; |
| 210 SkPoint* curve2 = v2 == SkPath::kCubic_Verb ? &cubicPair
[3] : cStorage[1]; |
| 211 for (size_t index = 0; index < SK_ARRAY_COUNT(curve1); +
+index) { |
| 212 force_small_to_zero(&curve1[index]); |
| 213 force_small_to_zero(&curve2[index]); |
| 214 } |
| 215 fCurrentContour->addCurve(v1, curve1, fAllocator); |
| 216 fCurrentContour->addCurve(v2, curve2, fAllocator); |
| 217 } else { |
| 218 fCurrentContour->addCubic(pointsPtr, fAllocator); |
| 219 } |
| 220 } else { |
| 221 fCurrentContour->addCubic(pointsPtr, fAllocator); |
| 222 } |
| 223 } break; |
| 191 case SkPath::kClose_Verb: | 224 case SkPath::kClose_Verb: |
| 192 SkASSERT(fCurrentContour); | 225 SkASSERT(fCurrentContour); |
| 193 if (!close()) { | 226 if (!close()) { |
| 194 return false; | 227 return false; |
| 195 } | 228 } |
| 196 continue; | 229 continue; |
| 197 default: | 230 default: |
| 198 SkDEBUGFAIL("bad verb"); | 231 SkDEBUGFAIL("bad verb"); |
| 199 return false; | 232 return false; |
| 200 } | 233 } |
| 234 SkASSERT(fCurrentContour); |
| 235 fCurrentContour->debugValidate(); |
| 201 pointsPtr += SkPathOpsVerbToPoints(verb); | 236 pointsPtr += SkPathOpsVerbToPoints(verb); |
| 202 SkASSERT(fCurrentContour); | |
| 203 } | 237 } |
| 204 if (fCurrentContour && !fAllowOpenContours && !close()) { | 238 if (fCurrentContour && fCurrentContour->count() &&!fAllowOpenContours && !clo
se()) { |
| 205 return false; | 239 return false; |
| 206 } | 240 } |
| 207 return true; | 241 return true; |
| 208 } | 242 } |
| OLD | NEW |