caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | #include "SkOpEdgeBuilder.h" |
| 8 | #include "SkReduceOrder.h" |
| 9 | |
| 10 | void SkOpEdgeBuilder::init() { |
| 11 | fCurrentContour = NULL; |
| 12 | fOperand = false; |
| 13 | fXorMask[0] = fXorMask[1] = (fPath->getFillType() & 1) ? kEvenOdd_PathOpsMask |
| 14 | : kWinding_PathOpsMask; |
| 15 | #if DEBUG_DUMP |
| 16 | gContourID = 0; |
| 17 | gSegmentID = 0; |
| 18 | #endif |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 19 | fUnparseable = false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 20 | fSecondHalf = preFetch(); |
| 21 | } |
| 22 | |
| 23 | void SkOpEdgeBuilder::addOperand(const SkPath& path) { |
| 24 | SkASSERT(fPathVerbs.count() > 0 && fPathVerbs.end()[-1] == SkPath::kDone_Verb); |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 25 | fPathVerbs.pop_back(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 26 | fPath = &path; |
| 27 | fXorMask[1] = (fPath->getFillType() & 1) ? kEvenOdd_PathOpsMask |
| 28 | : kWinding_PathOpsMask; |
| 29 | preFetch(); |
| 30 | } |
| 31 | |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 32 | bool SkOpEdgeBuilder::finish() { |
| 33 | if (fUnparseable || !walk()) { |
| 34 | return false; |
| 35 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 36 | complete(); |
| 37 | if (fCurrentContour && !fCurrentContour->segments().count()) { |
| 38 | fContours.pop_back(); |
| 39 | } |
| 40 | // correct pointers in contours since fReducePts may have moved as it grew |
| 41 | int cIndex = 0; |
| 42 | int extraCount = fExtra.count(); |
| 43 | SkASSERT(extraCount == 0 || fExtra[0] == -1); |
| 44 | int eIndex = 0; |
| 45 | int rIndex = 0; |
| 46 | while (++eIndex < extraCount) { |
| 47 | int offset = fExtra[eIndex]; |
| 48 | if (offset < 0) { |
| 49 | ++cIndex; |
| 50 | continue; |
| 51 | } |
| 52 | fCurrentContour = &fContours[cIndex]; |
| 53 | rIndex += fCurrentContour->updateSegment(offset - 1, |
| 54 | &fReducePts[rIndex]); |
| 55 | } |
| 56 | fExtra.reset(); // we're done with this |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 57 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 58 | } |
| 59 | |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 60 | // Note that copying the points here avoids copying the resulting path later. |
| 61 | // To allow Op() to take one of the input paths as an output parameter, either the source data |
| 62 | // must be copied (as implemented below) or the result must be copied. |
| 63 | // OPTIMIZATION: This copies both sets of input points every time. If the input data was read |
| 64 | // directly, the output path would only need to be copied if it was also one of the input paths. |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 65 | int SkOpEdgeBuilder::preFetch() { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 66 | if (!fPath->isFinite()) { |
| 67 | fUnparseable = true; |
| 68 | return 0; |
| 69 | } |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 70 | SkPath::RawIter iter(*fPath); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 71 | SkPoint pts[4]; |
| 72 | SkPath::Verb verb; |
| 73 | do { |
| 74 | verb = iter.next(pts); |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 75 | fPathVerbs.push_back(verb); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 76 | if (verb == SkPath::kMove_Verb) { |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 77 | fPathPts.push_back(pts[0]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 78 | } else if (verb >= SkPath::kLine_Verb && verb <= SkPath::kCubic_Verb) { |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 79 | fPathPts.push_back_n(SkPathOpsVerbToPoints(verb), &pts[1]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 80 | } |
| 81 | } while (verb != SkPath::kDone_Verb); |
| 82 | return fPathVerbs.count() - 1; |
| 83 | } |
| 84 | |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 85 | bool SkOpEdgeBuilder::close() { |
| 86 | if (fFinalCurveStart && fFinalCurveEnd && *fFinalCurveStart != *fFinalCurveEnd) { |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 87 | fReducePts.push_back(*fFinalCurveStart); |
| 88 | fReducePts.push_back(*fFinalCurveEnd); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 89 | const SkPoint* lineStart = fReducePts.end() - 2; |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 90 | fExtra.push_back(fCurrentContour->addLine(lineStart)); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 91 | } |
| 92 | complete(); |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | bool SkOpEdgeBuilder::walk() { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 97 | SkPath::Verb reducedVerb; |
| 98 | uint8_t* verbPtr = fPathVerbs.begin(); |
| 99 | uint8_t* endOfFirstHalf = &verbPtr[fSecondHalf]; |
| 100 | const SkPoint* pointsPtr = fPathPts.begin(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 101 | SkPath::Verb verb; |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 102 | fFinalCurveStart = NULL; |
| 103 | fFinalCurveEnd = NULL; |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 104 | while ((verb = (SkPath::Verb) *verbPtr) != SkPath::kDone_Verb) { |
| 105 | if (verbPtr == endOfFirstHalf) { |
| 106 | fOperand = true; |
| 107 | } |
| 108 | verbPtr++; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 109 | switch (verb) { |
| 110 | case SkPath::kMove_Verb: |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 111 | if (fCurrentContour) { |
| 112 | if (fAllowOpenContours) { |
| 113 | complete(); |
| 114 | } else if (!close()) { |
| 115 | return false; |
| 116 | } |
| 117 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 118 | if (!fCurrentContour) { |
| 119 | fCurrentContour = fContours.push_back_n(1); |
| 120 | fCurrentContour->setOperand(fOperand); |
| 121 | fCurrentContour->setXor(fXorMask[fOperand] == kEvenOdd_PathOpsMask); |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 122 | fExtra.push_back(-1); // start new contour |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 123 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 124 | fFinalCurveEnd = pointsPtr++; |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 125 | continue; |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 126 | case SkPath::kLine_Verb: { |
| 127 | const SkPoint& lineEnd = pointsPtr[0]; |
| 128 | const SkPoint& lineStart = pointsPtr[-1]; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 129 | // skip degenerate points |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 130 | if (lineStart.fX != lineEnd.fX || lineStart.fY != lineEnd.fY) { |
| 131 | fCurrentContour->addLine(&lineStart); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 132 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 133 | } break; |
| 134 | case SkPath::kQuad_Verb: { |
| 135 | const SkPoint* quadStart = &pointsPtr[-1]; |
| 136 | reducedVerb = SkReduceOrder::Quad(quadStart, &fReducePts); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 137 | if (reducedVerb == 0) { |
| 138 | break; // skip degenerate points |
| 139 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 140 | if (reducedVerb == SkPath::kLine_Verb) { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 141 | const SkPoint* lineStart = fReducePts.end() - 2; |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 142 | fExtra.push_back(fCurrentContour->addLine(lineStart)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 143 | break; |
| 144 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 145 | fCurrentContour->addQuad(quadStart); |
| 146 | } break; |
| 147 | case SkPath::kCubic_Verb: { |
| 148 | const SkPoint* cubicStart = &pointsPtr[-1]; |
| 149 | reducedVerb = SkReduceOrder::Cubic(cubicStart, &fReducePts); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 150 | if (reducedVerb == 0) { |
| 151 | break; // skip degenerate points |
| 152 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 153 | if (reducedVerb == SkPath::kLine_Verb) { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 154 | const SkPoint* lineStart = fReducePts.end() - 2; |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 155 | fExtra.push_back(fCurrentContour->addLine(lineStart)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 156 | break; |
| 157 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 158 | if (reducedVerb == SkPath::kQuad_Verb) { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 159 | const SkPoint* quadStart = fReducePts.end() - 3; |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 160 | fExtra.push_back(fCurrentContour->addQuad(quadStart)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 161 | break; |
| 162 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 163 | fCurrentContour->addCubic(cubicStart); |
| 164 | } break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 165 | case SkPath::kClose_Verb: |
| 166 | SkASSERT(fCurrentContour); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 167 | if (!close()) { |
| 168 | return false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 169 | } |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 170 | continue; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 171 | default: |
| 172 | SkDEBUGFAIL("bad verb"); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 173 | return false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 174 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 175 | fFinalCurveStart = &pointsPtr[SkPathOpsVerbToPoints(verb) - 1]; |
| 176 | pointsPtr += SkPathOpsVerbToPoints(verb); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 177 | SkASSERT(fCurrentContour); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 178 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 179 | if (fCurrentContour && !fAllowOpenContours && !close()) { |
| 180 | return false; |
| 181 | } |
| 182 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 183 | } |