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 | */ |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 7 | #include "SkGeometry.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 8 | #include "SkOpEdgeBuilder.h" |
| 9 | #include "SkReduceOrder.h" |
| 10 | |
| 11 | void SkOpEdgeBuilder::init() { |
| 12 | fCurrentContour = NULL; |
| 13 | fOperand = false; |
| 14 | fXorMask[0] = fXorMask[1] = (fPath->getFillType() & 1) ? kEvenOdd_PathOpsMask |
| 15 | : kWinding_PathOpsMask; |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 16 | fUnparseable = false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 17 | fSecondHalf = preFetch(); |
| 18 | } |
| 19 | |
| 20 | void SkOpEdgeBuilder::addOperand(const SkPath& path) { |
| 21 | SkASSERT(fPathVerbs.count() > 0 && fPathVerbs.end()[-1] == SkPath::kDone_Verb); |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 22 | fPathVerbs.pop_back(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 23 | fPath = &path; |
| 24 | fXorMask[1] = (fPath->getFillType() & 1) ? kEvenOdd_PathOpsMask |
| 25 | : kWinding_PathOpsMask; |
| 26 | preFetch(); |
| 27 | } |
| 28 | |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 29 | bool SkOpEdgeBuilder::finish() { |
| 30 | if (fUnparseable || !walk()) { |
| 31 | return false; |
| 32 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 33 | complete(); |
| 34 | if (fCurrentContour && !fCurrentContour->segments().count()) { |
| 35 | fContours.pop_back(); |
| 36 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 37 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 38 | } |
| 39 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 40 | void SkOpEdgeBuilder::closeContour(const SkPoint& curveEnd, const SkPoint& curveStart) { |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 41 | if (!SkDPoint::ApproximatelyEqual(curveEnd, curveStart)) { |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 42 | fPathVerbs.push_back(SkPath::kLine_Verb); |
skia.committer@gmail.com | a4aced4 | 2013-07-09 07:00:56 +0000 | [diff] [blame] | 43 | fPathPts.push_back_n(1, &curveStart); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 44 | } else { |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 45 | fPathPts[fPathPts.count() - 1] = curveStart; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 46 | } |
| 47 | fPathVerbs.push_back(SkPath::kClose_Verb); |
| 48 | } |
| 49 | |
caryclark | 65b427c | 2014-09-18 10:32:57 -0700 | [diff] [blame] | 50 | // very tiny points cause numerical instability : don't allow them |
| 51 | static void force_small_to_zero(SkPoint* pt) { |
| 52 | if (SkScalarAbs(pt->fX) < FLT_EPSILON_ORDERABLE_ERR) { |
| 53 | pt->fX = 0; |
| 54 | } |
| 55 | if (SkScalarAbs(pt->fY) < FLT_EPSILON_ORDERABLE_ERR) { |
| 56 | pt->fY = 0; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 61 | int SkOpEdgeBuilder::preFetch() { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 62 | if (!fPath->isFinite()) { |
| 63 | fUnparseable = true; |
| 64 | return 0; |
| 65 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 66 | SkAutoConicToQuads quadder; |
| 67 | const SkScalar quadderTol = SK_Scalar1 / 16; |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 68 | SkPath::RawIter iter(*fPath); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 69 | SkPoint curveStart; |
| 70 | SkPoint curve[4]; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 71 | SkPoint pts[4]; |
| 72 | SkPath::Verb verb; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 73 | bool lastCurve = false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 74 | do { |
| 75 | verb = iter.next(pts); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 76 | switch (verb) { |
| 77 | case SkPath::kMove_Verb: |
| 78 | if (!fAllowOpenContours && lastCurve) { |
| 79 | closeContour(curve[0], curveStart); |
| 80 | } |
| 81 | fPathVerbs.push_back(verb); |
caryclark | 65b427c | 2014-09-18 10:32:57 -0700 | [diff] [blame] | 82 | force_small_to_zero(&pts[0]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 83 | fPathPts.push_back(pts[0]); |
| 84 | curveStart = curve[0] = pts[0]; |
| 85 | lastCurve = false; |
| 86 | continue; |
| 87 | case SkPath::kLine_Verb: |
caryclark | 65b427c | 2014-09-18 10:32:57 -0700 | [diff] [blame] | 88 | force_small_to_zero(&pts[1]); |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 89 | if (SkDPoint::ApproximatelyEqual(curve[0], pts[1])) { |
| 90 | uint8_t lastVerb = fPathVerbs.back(); |
| 91 | if (lastVerb != SkPath::kLine_Verb && lastVerb != SkPath::kMove_Verb) { |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 92 | fPathPts.back() = pts[1]; |
| 93 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 94 | continue; // skip degenerate points |
| 95 | } |
| 96 | break; |
| 97 | case SkPath::kQuad_Verb: |
caryclark | 65b427c | 2014-09-18 10:32:57 -0700 | [diff] [blame] | 98 | force_small_to_zero(&pts[1]); |
| 99 | force_small_to_zero(&pts[2]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 100 | curve[1] = pts[1]; |
| 101 | curve[2] = pts[2]; |
| 102 | verb = SkReduceOrder::Quad(curve, pts); |
| 103 | if (verb == SkPath::kMove_Verb) { |
| 104 | continue; // skip degenerate points |
| 105 | } |
| 106 | break; |
| 107 | case SkPath::kConic_Verb: { |
| 108 | const SkPoint* quadPts = quadder.computeQuads(pts, iter.conicWeight(), |
| 109 | quadderTol); |
| 110 | const int nQuads = quadder.countQuads(); |
| 111 | for (int i = 0; i < nQuads; ++i) { |
| 112 | fPathVerbs.push_back(SkPath::kQuad_Verb); |
| 113 | } |
reed | 3f4e045 | 2015-01-06 07:44:21 -0800 | [diff] [blame] | 114 | fPathPts.push_back_n(nQuads * 2, &quadPts[1]); |
| 115 | curve[0] = pts[2]; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 116 | lastCurve = true; |
| 117 | } |
| 118 | continue; |
| 119 | case SkPath::kCubic_Verb: |
caryclark | 65b427c | 2014-09-18 10:32:57 -0700 | [diff] [blame] | 120 | force_small_to_zero(&pts[1]); |
| 121 | force_small_to_zero(&pts[2]); |
| 122 | force_small_to_zero(&pts[3]); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 123 | curve[1] = pts[1]; |
| 124 | curve[2] = pts[2]; |
| 125 | curve[3] = pts[3]; |
| 126 | verb = SkReduceOrder::Cubic(curve, pts); |
| 127 | if (verb == SkPath::kMove_Verb) { |
| 128 | continue; // skip degenerate points |
| 129 | } |
| 130 | break; |
| 131 | case SkPath::kClose_Verb: |
| 132 | closeContour(curve[0], curveStart); |
| 133 | lastCurve = false; |
| 134 | continue; |
| 135 | case SkPath::kDone_Verb: |
| 136 | continue; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 137 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 138 | fPathVerbs.push_back(verb); |
| 139 | int ptCount = SkPathOpsVerbToPoints(verb); |
| 140 | fPathPts.push_back_n(ptCount, &pts[1]); |
| 141 | curve[0] = pts[ptCount]; |
| 142 | lastCurve = true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 143 | } while (verb != SkPath::kDone_Verb); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 144 | if (!fAllowOpenContours && lastCurve) { |
| 145 | closeContour(curve[0], curveStart); |
| 146 | } |
| 147 | fPathVerbs.push_back(SkPath::kDone_Verb); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 148 | return fPathVerbs.count() - 1; |
| 149 | } |
| 150 | |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 151 | bool SkOpEdgeBuilder::close() { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 152 | complete(); |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | bool SkOpEdgeBuilder::walk() { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 157 | uint8_t* verbPtr = fPathVerbs.begin(); |
| 158 | uint8_t* endOfFirstHalf = &verbPtr[fSecondHalf]; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 159 | const SkPoint* pointsPtr = fPathPts.begin() - 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 160 | SkPath::Verb verb; |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 161 | while ((verb = (SkPath::Verb) *verbPtr) != SkPath::kDone_Verb) { |
| 162 | if (verbPtr == endOfFirstHalf) { |
| 163 | fOperand = true; |
| 164 | } |
| 165 | verbPtr++; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 166 | switch (verb) { |
| 167 | case SkPath::kMove_Verb: |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 168 | if (fCurrentContour) { |
| 169 | if (fAllowOpenContours) { |
| 170 | complete(); |
| 171 | } else if (!close()) { |
| 172 | return false; |
| 173 | } |
| 174 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 175 | if (!fCurrentContour) { |
| 176 | fCurrentContour = fContours.push_back_n(1); |
| 177 | fCurrentContour->setOperand(fOperand); |
| 178 | fCurrentContour->setXor(fXorMask[fOperand] == kEvenOdd_PathOpsMask); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 179 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 180 | pointsPtr += 1; |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 181 | continue; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 182 | case SkPath::kLine_Verb: |
| 183 | fCurrentContour->addLine(pointsPtr); |
| 184 | break; |
| 185 | case SkPath::kQuad_Verb: |
| 186 | fCurrentContour->addQuad(pointsPtr); |
| 187 | break; |
| 188 | case SkPath::kCubic_Verb: |
| 189 | fCurrentContour->addCubic(pointsPtr); |
| 190 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 191 | case SkPath::kClose_Verb: |
| 192 | SkASSERT(fCurrentContour); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 193 | if (!close()) { |
| 194 | return false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 195 | } |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 196 | continue; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 197 | default: |
| 198 | SkDEBUGFAIL("bad verb"); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 199 | return false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 200 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 201 | pointsPtr += SkPathOpsVerbToPoints(verb); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 202 | SkASSERT(fCurrentContour); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 203 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 204 | if (fCurrentContour && !fAllowOpenContours && !close()) { |
| 205 | return false; |
| 206 | } |
| 207 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 208 | } |