Chromium Code Reviews| 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 "SkOpEdgeBuilder.h" | 7 #include "SkOpEdgeBuilder.h" |
| 8 #include "SkReduceOrder.h" | 8 #include "SkReduceOrder.h" |
| 9 | 9 |
| 10 void SkOpEdgeBuilder::init() { | 10 void SkOpEdgeBuilder::init() { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 | 21 |
| 22 void SkOpEdgeBuilder::addOperand(const SkPath& path) { | 22 void SkOpEdgeBuilder::addOperand(const SkPath& path) { |
| 23 SkASSERT(fPathVerbs.count() > 0 && fPathVerbs.end()[-1] == SkPath::kDone_Ver b); | 23 SkASSERT(fPathVerbs.count() > 0 && fPathVerbs.end()[-1] == SkPath::kDone_Ver b); |
| 24 fPathVerbs.pop(); | 24 fPathVerbs.pop(); |
| 25 fPath = &path; | 25 fPath = &path; |
| 26 fXorMask[1] = (fPath->getFillType() & 1) ? kEvenOdd_PathOpsMask | 26 fXorMask[1] = (fPath->getFillType() & 1) ? kEvenOdd_PathOpsMask |
| 27 : kWinding_PathOpsMask; | 27 : kWinding_PathOpsMask; |
| 28 preFetch(); | 28 preFetch(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 void SkOpEdgeBuilder::finish() { | 31 bool SkOpEdgeBuilder::finish() { |
| 32 walk(); | 32 if (!walk()) { |
| 33 return false; | |
| 34 } | |
| 33 complete(); | 35 complete(); |
| 34 if (fCurrentContour && !fCurrentContour->segments().count()) { | 36 if (fCurrentContour && !fCurrentContour->segments().count()) { |
| 35 fContours.pop_back(); | 37 fContours.pop_back(); |
| 36 } | 38 } |
| 37 // correct pointers in contours since fReducePts may have moved as it grew | 39 // correct pointers in contours since fReducePts may have moved as it grew |
| 38 int cIndex = 0; | 40 int cIndex = 0; |
| 39 int extraCount = fExtra.count(); | 41 int extraCount = fExtra.count(); |
| 40 SkASSERT(extraCount == 0 || fExtra[0] == -1); | 42 SkASSERT(extraCount == 0 || fExtra[0] == -1); |
| 41 int eIndex = 0; | 43 int eIndex = 0; |
| 42 int rIndex = 0; | 44 int rIndex = 0; |
| 43 while (++eIndex < extraCount) { | 45 while (++eIndex < extraCount) { |
| 44 int offset = fExtra[eIndex]; | 46 int offset = fExtra[eIndex]; |
| 45 if (offset < 0) { | 47 if (offset < 0) { |
| 46 ++cIndex; | 48 ++cIndex; |
| 47 continue; | 49 continue; |
| 48 } | 50 } |
| 49 fCurrentContour = &fContours[cIndex]; | 51 fCurrentContour = &fContours[cIndex]; |
| 50 rIndex += fCurrentContour->updateSegment(offset - 1, | 52 rIndex += fCurrentContour->updateSegment(offset - 1, |
| 51 &fReducePts[rIndex]); | 53 &fReducePts[rIndex]); |
| 52 } | 54 } |
| 53 fExtra.reset(); // we're done with this | 55 fExtra.reset(); // we're done with this |
| 56 return true; | |
| 54 } | 57 } |
| 55 | 58 |
| 56 // Note that copying the points here avoids copying the resulting path later. | 59 // Note that copying the points here avoids copying the resulting path later. |
| 57 // To allow Op() to take one of the input paths as an output parameter, either t he source data | 60 // To allow Op() to take one of the input paths as an output parameter, either t he source data |
| 58 // must be copied (as implemented below) or the result must be copied. | 61 // must be copied (as implemented below) or the result must be copied. |
| 59 // OPTIMIZATION: This copies both sets of input points every time. If the input data was read | 62 // OPTIMIZATION: This copies both sets of input points every time. If the input data was read |
| 60 // directly, the output path would only need to be copied if it was also one of the input paths. | 63 // directly, the output path would only need to be copied if it was also one of the input paths. |
| 61 int SkOpEdgeBuilder::preFetch() { | 64 int SkOpEdgeBuilder::preFetch() { |
| 62 SkPath::RawIter iter(*fPath); | 65 SkPath::RawIter iter(*fPath); |
| 63 SkPoint pts[4]; | 66 SkPoint pts[4]; |
| 64 SkPath::Verb verb; | 67 SkPath::Verb verb; |
| 65 do { | 68 do { |
| 66 verb = iter.next(pts); | 69 verb = iter.next(pts); |
| 67 *fPathVerbs.append() = verb; | 70 *fPathVerbs.append() = verb; |
| 68 if (verb == SkPath::kMove_Verb) { | 71 if (verb == SkPath::kMove_Verb) { |
| 69 *fPathPts.append() = pts[0]; | 72 *fPathPts.append() = pts[0]; |
| 70 } else if (verb >= SkPath::kLine_Verb && verb <= SkPath::kCubic_Verb) { | 73 } else if (verb >= SkPath::kLine_Verb && verb <= SkPath::kCubic_Verb) { |
| 71 fPathPts.append(verb, &pts[1]); | 74 fPathPts.append(verb, &pts[1]); |
| 72 } | 75 } |
| 73 } while (verb != SkPath::kDone_Verb); | 76 } while (verb != SkPath::kDone_Verb); |
| 74 return fPathVerbs.count() - 1; | 77 return fPathVerbs.count() - 1; |
| 75 } | 78 } |
| 76 | 79 |
| 77 void SkOpEdgeBuilder::walk() { | 80 static bool is_finite_point(const SkPoint& pt) { |
| 81 // repeat logic in SkRect::isFinite() | |
| 82 float accum = 0; | |
| 83 accum *= pt.fX; | |
| 84 accum *= pt.fY; | |
| 85 | |
| 86 // accum is either NaN or it is finite (zero). | |
| 87 SkASSERT(0 == accum || !(accum == accum)); | |
| 88 | |
| 89 // value==value will be true iff value is not NaN | |
| 90 // TODO: is it faster to say !accum or accum==accum? | |
| 91 return accum == accum; | |
| 92 } | |
| 93 | |
| 94 static bool is_finite_line(const SkPoint pts[2]) { | |
| 95 // repeat logic in SkRect::isFinite() | |
| 96 float accum = 0; | |
| 97 accum *= pts[0].fX; | |
| 98 accum *= pts[0].fY; | |
| 99 accum *= pts[1].fX; | |
| 100 accum *= pts[1].fY; | |
| 101 | |
| 102 // accum is either NaN or it is finite (zero). | |
| 103 SkASSERT(0 == accum || !(accum == accum)); | |
| 104 | |
| 105 // value==value will be true iff value is not NaN | |
| 106 // TODO: is it faster to say !accum or accum==accum? | |
| 107 return accum == accum; | |
| 108 } | |
| 109 | |
| 110 static bool is_finite_quad(const SkPoint pts[3]) { | |
| 111 // repeat logic in SkRect::isFinite() | |
| 112 float accum = 0; | |
| 113 accum *= pts[0].fX; | |
| 114 accum *= pts[0].fY; | |
| 115 accum *= pts[1].fX; | |
| 116 accum *= pts[1].fY; | |
| 117 accum *= pts[2].fX; | |
| 118 accum *= pts[2].fY; | |
| 119 | |
| 120 // accum is either NaN or it is finite (zero). | |
| 121 SkASSERT(0 == accum || !(accum == accum)); | |
| 122 | |
| 123 // value==value will be true iff value is not NaN | |
| 124 // TODO: is it faster to say !accum or accum==accum? | |
| 125 return accum == accum; | |
| 126 } | |
| 127 | |
| 128 bool SkOpEdgeBuilder::close() { | |
| 129 if (fFinalCurveStart && fFinalCurveEnd && *fFinalCurveStart != *fFinalCurveE nd) { | |
| 130 *fReducePts.append() = *fFinalCurveStart; | |
| 131 *fReducePts.append() = *fFinalCurveEnd; | |
| 132 const SkPoint* lineStart = fReducePts.end() - 2; | |
| 133 *fExtra.append() = fCurrentContour->addLine(lineStart); | |
| 134 } | |
| 135 complete(); | |
| 136 return true; | |
| 137 } | |
| 138 | |
| 139 bool SkOpEdgeBuilder::walk() { | |
| 78 SkPath::Verb reducedVerb; | 140 SkPath::Verb reducedVerb; |
| 79 uint8_t* verbPtr = fPathVerbs.begin(); | 141 uint8_t* verbPtr = fPathVerbs.begin(); |
| 80 uint8_t* endOfFirstHalf = &verbPtr[fSecondHalf]; | 142 uint8_t* endOfFirstHalf = &verbPtr[fSecondHalf]; |
| 81 const SkPoint* pointsPtr = fPathPts.begin(); | 143 const SkPoint* pointsPtr = fPathPts.begin(); |
| 82 const SkPoint* finalCurveStart = NULL; | |
| 83 const SkPoint* finalCurveEnd = NULL; | |
| 84 SkPath::Verb verb; | 144 SkPath::Verb verb; |
| 145 fFinalCurveStart = NULL; | |
| 146 fFinalCurveEnd = NULL; | |
| 85 while ((verb = (SkPath::Verb) *verbPtr) != SkPath::kDone_Verb) { | 147 while ((verb = (SkPath::Verb) *verbPtr) != SkPath::kDone_Verb) { |
| 86 if (verbPtr == endOfFirstHalf) { | 148 if (verbPtr == endOfFirstHalf) { |
| 87 fOperand = true; | 149 fOperand = true; |
| 88 } | 150 } |
| 89 verbPtr++; | 151 verbPtr++; |
| 90 switch (verb) { | 152 switch (verb) { |
| 91 case SkPath::kMove_Verb: | 153 case SkPath::kMove_Verb: |
| 92 complete(); | 154 if (fCurrentContour) { |
| 155 if (fAllowOpenContours) { | |
| 156 complete(); | |
| 157 } else if (!close()) { | |
| 158 return false; | |
| 159 } | |
| 160 } | |
| 93 if (!fCurrentContour) { | 161 if (!fCurrentContour) { |
| 94 fCurrentContour = fContours.push_back_n(1); | 162 fCurrentContour = fContours.push_back_n(1); |
| 95 fCurrentContour->setOperand(fOperand); | 163 fCurrentContour->setOperand(fOperand); |
| 96 fCurrentContour->setXor(fXorMask[fOperand] == kEvenOdd_PathO psMask); | 164 fCurrentContour->setXor(fXorMask[fOperand] == kEvenOdd_PathO psMask); |
| 97 *fExtra.append() = -1; // start new contour | 165 *fExtra.append() = -1; // start new contour |
| 98 } | 166 } |
| 99 finalCurveEnd = pointsPtr++; | 167 if (!is_finite_point(pointsPtr[0])) { |
|
reed1
2013/04/26 17:49:14
Would it be faster to just ask for the path's boun
caryclark
2013/04/26 19:47:30
Yes indeed. Done and uploaded.
On 2013/04/26 17:4
| |
| 168 return false; | |
| 169 } | |
| 170 fFinalCurveEnd = pointsPtr++; | |
| 100 continue; | 171 continue; |
| 101 case SkPath::kLine_Verb: | 172 case SkPath::kLine_Verb: { |
| 173 const SkPoint& lineEnd = pointsPtr[0]; | |
| 174 if (!is_finite_point(lineEnd)) { | |
| 175 return false; | |
| 176 } | |
| 177 const SkPoint& lineStart = pointsPtr[-1]; | |
| 102 // skip degenerate points | 178 // skip degenerate points |
| 103 if (pointsPtr[-1].fX != pointsPtr[0].fX || pointsPtr[-1].fY != p ointsPtr[0].fY) { | 179 if (lineStart.fX != lineEnd.fX || lineStart.fY != lineEnd.fY) { |
| 104 fCurrentContour->addLine(&pointsPtr[-1]); | 180 fCurrentContour->addLine(&lineStart); |
| 105 } | 181 } |
| 106 break; | 182 } break; |
| 107 case SkPath::kQuad_Verb: | 183 case SkPath::kQuad_Verb: { |
| 108 reducedVerb = SkReduceOrder::Quad(&pointsPtr[-1], &fReducePts); | 184 if (!is_finite_line(pointsPtr)) { |
| 185 return false; | |
| 186 } | |
| 187 const SkPoint* quadStart = &pointsPtr[-1]; | |
| 188 reducedVerb = SkReduceOrder::Quad(quadStart, &fReducePts); | |
| 109 if (reducedVerb == 0) { | 189 if (reducedVerb == 0) { |
| 110 break; // skip degenerate points | 190 break; // skip degenerate points |
| 111 } | 191 } |
| 112 if (reducedVerb == 1) { | 192 if (reducedVerb == 1) { |
| 113 *fExtra.append() = | 193 const SkPoint* lineStart = fReducePts.end() - 2; |
| 114 fCurrentContour->addLine(fReducePts.end() - 2); | 194 *fExtra.append() = fCurrentContour->addLine(lineStart); |
| 115 break; | 195 break; |
| 116 } | 196 } |
| 117 fCurrentContour->addQuad(&pointsPtr[-1]); | 197 fCurrentContour->addQuad(quadStart); |
| 118 break; | 198 } break; |
| 119 case SkPath::kCubic_Verb: | 199 case SkPath::kCubic_Verb: { |
| 120 reducedVerb = SkReduceOrder::Cubic(&pointsPtr[-1], &fReducePts); | 200 if (!is_finite_quad(pointsPtr)) { |
| 201 return false; | |
| 202 } | |
| 203 const SkPoint* cubicStart = &pointsPtr[-1]; | |
| 204 reducedVerb = SkReduceOrder::Cubic(cubicStart, &fReducePts); | |
| 121 if (reducedVerb == 0) { | 205 if (reducedVerb == 0) { |
| 122 break; // skip degenerate points | 206 break; // skip degenerate points |
| 123 } | 207 } |
| 124 if (reducedVerb == 1) { | 208 if (reducedVerb == 1) { |
| 125 *fExtra.append() = fCurrentContour->addLine(fReducePts.end() - 2); | 209 const SkPoint* lineStart = fReducePts.end() - 2; |
| 210 *fExtra.append() = fCurrentContour->addLine(lineStart); | |
| 126 break; | 211 break; |
| 127 } | 212 } |
| 128 if (reducedVerb == 2) { | 213 if (reducedVerb == 2) { |
| 129 *fExtra.append() = fCurrentContour->addQuad(fReducePts.end() - 3); | 214 const SkPoint* quadStart = fReducePts.end() - 3; |
| 215 *fExtra.append() = fCurrentContour->addQuad(quadStart); | |
| 130 break; | 216 break; |
| 131 } | 217 } |
| 132 fCurrentContour->addCubic(&pointsPtr[-1]); | 218 fCurrentContour->addCubic(cubicStart); |
| 133 break; | 219 } break; |
| 134 case SkPath::kClose_Verb: | 220 case SkPath::kClose_Verb: |
| 135 SkASSERT(fCurrentContour); | 221 SkASSERT(fCurrentContour); |
| 136 if (finalCurveStart && finalCurveEnd | 222 if (!close()) { |
| 137 && *finalCurveStart != *finalCurveEnd) { | 223 return false; |
| 138 *fReducePts.append() = *finalCurveStart; | |
| 139 *fReducePts.append() = *finalCurveEnd; | |
| 140 *fExtra.append() = fCurrentContour->addLine(fReducePts.end() - 2); | |
| 141 } | 224 } |
| 142 complete(); | |
| 143 continue; | 225 continue; |
| 144 default: | 226 default: |
| 145 SkDEBUGFAIL("bad verb"); | 227 SkDEBUGFAIL("bad verb"); |
| 146 return; | 228 return false; |
| 147 } | 229 } |
| 148 finalCurveStart = &pointsPtr[verb - 1]; | 230 fFinalCurveStart = &pointsPtr[verb - 1]; |
| 149 pointsPtr += verb; | 231 pointsPtr += verb; |
| 150 SkASSERT(fCurrentContour); | 232 SkASSERT(fCurrentContour); |
| 151 } | 233 } |
| 234 if (fCurrentContour && !fAllowOpenContours && !close()) { | |
| 235 return false; | |
| 236 } | |
| 237 return true; | |
| 152 } | 238 } |
| OLD | NEW |