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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "src/core/SkGeometry.h" |
Chris Dalton | 957189b | 2020-05-07 12:47:26 -0600 | [diff] [blame] | 8 | #include "src/core/SkPathPriv.h" |
John Stiles | df07800 | 2020-07-14 09:44:57 -0400 | [diff] [blame] | 9 | #include "src/core/SkTSort.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/pathops/SkOpEdgeBuilder.h" |
| 11 | #include "src/pathops/SkReduceOrder.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 12 | |
| 13 | void SkOpEdgeBuilder::init() { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 14 | fOperand = false; |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 15 | fXorMask[0] = fXorMask[1] = ((int)fPath->getFillType() & 1) ? kEvenOdd_PathOpsMask |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 16 | : kWinding_PathOpsMask; |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 17 | fUnparseable = false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 18 | fSecondHalf = preFetch(); |
| 19 | } |
| 20 | |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 21 | // very tiny points cause numerical instability : don't allow them |
Chris Dalton | 1fc1bd3 | 2020-05-06 11:44:51 -0600 | [diff] [blame] | 22 | static SkPoint force_small_to_zero(const SkPoint& pt) { |
| 23 | SkPoint ret = pt; |
| 24 | if (SkScalarAbs(ret.fX) < FLT_EPSILON_ORDERABLE_ERR) { |
| 25 | ret.fX = 0; |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 26 | } |
Chris Dalton | 1fc1bd3 | 2020-05-06 11:44:51 -0600 | [diff] [blame] | 27 | if (SkScalarAbs(ret.fY) < FLT_EPSILON_ORDERABLE_ERR) { |
| 28 | ret.fY = 0; |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 29 | } |
Chris Dalton | 1fc1bd3 | 2020-05-06 11:44:51 -0600 | [diff] [blame] | 30 | return ret; |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static bool can_add_curve(SkPath::Verb verb, SkPoint* curve) { |
| 34 | if (SkPath::kMove_Verb == verb) { |
| 35 | return false; |
| 36 | } |
caryclark | cc09372 | 2016-09-23 09:32:26 -0700 | [diff] [blame] | 37 | for (int index = 0; index <= SkPathOpsVerbToPoints(verb); ++index) { |
Chris Dalton | 1fc1bd3 | 2020-05-06 11:44:51 -0600 | [diff] [blame] | 38 | curve[index] = force_small_to_zero(curve[index]); |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 39 | } |
| 40 | return SkPath::kLine_Verb != verb || !SkDPoint::ApproximatelyEqual(curve[0], curve[1]); |
| 41 | } |
| 42 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 43 | void SkOpEdgeBuilder::addOperand(const SkPath& path) { |
| 44 | SkASSERT(fPathVerbs.count() > 0 && fPathVerbs.end()[-1] == SkPath::kDone_Verb); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 45 | fPathVerbs.pop(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 46 | fPath = &path; |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 47 | fXorMask[1] = ((int)fPath->getFillType() & 1) ? kEvenOdd_PathOpsMask |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 48 | : kWinding_PathOpsMask; |
| 49 | preFetch(); |
| 50 | } |
| 51 | |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 52 | bool SkOpEdgeBuilder::finish() { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 53 | fOperand = false; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 54 | if (fUnparseable || !walk()) { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 55 | return false; |
| 56 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 57 | complete(); |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 58 | SkOpContour* contour = fContourBuilder.contour(); |
| 59 | if (contour && !contour->count()) { |
| 60 | fContoursHead->remove(contour); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 61 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 62 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 63 | } |
| 64 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 65 | void SkOpEdgeBuilder::closeContour(const SkPoint& curveEnd, const SkPoint& curveStart) { |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 66 | if (!SkDPoint::ApproximatelyEqual(curveEnd, curveStart)) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 67 | *fPathVerbs.append() = SkPath::kLine_Verb; |
| 68 | *fPathPts.append() = curveStart; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 69 | } else { |
Cary Clark | b9ae537 | 2016-10-05 10:40:07 -0400 | [diff] [blame] | 70 | int verbCount = fPathVerbs.count(); |
| 71 | int ptsCount = fPathPts.count(); |
| 72 | if (SkPath::kLine_Verb == fPathVerbs[verbCount - 1] |
| 73 | && fPathPts[ptsCount - 2] == curveStart) { |
| 74 | fPathVerbs.pop(); |
| 75 | fPathPts.pop(); |
| 76 | } else { |
| 77 | fPathPts[ptsCount - 1] = curveStart; |
| 78 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 79 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 80 | *fPathVerbs.append() = SkPath::kClose_Verb; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 81 | } |
| 82 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 83 | int SkOpEdgeBuilder::preFetch() { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 84 | if (!fPath->isFinite()) { |
| 85 | fUnparseable = true; |
| 86 | return 0; |
| 87 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 88 | SkPoint curveStart; |
| 89 | SkPoint curve[4]; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 90 | bool lastCurve = false; |
Chris Dalton | 957189b | 2020-05-07 12:47:26 -0600 | [diff] [blame] | 91 | for (auto [pathVerb, pts, w] : SkPathPriv::Iterate(*fPath)) { |
| 92 | auto verb = static_cast<SkPath::Verb>(pathVerb); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 93 | switch (verb) { |
| 94 | case SkPath::kMove_Verb: |
| 95 | if (!fAllowOpenContours && lastCurve) { |
| 96 | closeContour(curve[0], curveStart); |
| 97 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 98 | *fPathVerbs.append() = verb; |
Chris Dalton | 1fc1bd3 | 2020-05-06 11:44:51 -0600 | [diff] [blame] | 99 | curve[0] = force_small_to_zero(pts[0]); |
| 100 | *fPathPts.append() = curve[0]; |
| 101 | curveStart = curve[0]; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 102 | lastCurve = false; |
| 103 | continue; |
| 104 | case SkPath::kLine_Verb: |
Chris Dalton | 1fc1bd3 | 2020-05-06 11:44:51 -0600 | [diff] [blame] | 105 | curve[1] = force_small_to_zero(pts[1]); |
| 106 | if (SkDPoint::ApproximatelyEqual(curve[0], curve[1])) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 107 | uint8_t lastVerb = fPathVerbs.top(); |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 108 | if (lastVerb != SkPath::kLine_Verb && lastVerb != SkPath::kMove_Verb) { |
Chris Dalton | 1fc1bd3 | 2020-05-06 11:44:51 -0600 | [diff] [blame] | 109 | fPathPts.top() = curve[0] = curve[1]; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 110 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 111 | continue; // skip degenerate points |
| 112 | } |
| 113 | break; |
| 114 | case SkPath::kQuad_Verb: |
Chris Dalton | 1fc1bd3 | 2020-05-06 11:44:51 -0600 | [diff] [blame] | 115 | curve[1] = force_small_to_zero(pts[1]); |
| 116 | curve[2] = force_small_to_zero(pts[2]); |
| 117 | verb = SkReduceOrder::Quad(curve, curve); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 118 | if (verb == SkPath::kMove_Verb) { |
| 119 | continue; // skip degenerate points |
| 120 | } |
| 121 | break; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 122 | case SkPath::kConic_Verb: |
Chris Dalton | 1fc1bd3 | 2020-05-06 11:44:51 -0600 | [diff] [blame] | 123 | curve[1] = force_small_to_zero(pts[1]); |
| 124 | curve[2] = force_small_to_zero(pts[2]); |
| 125 | verb = SkReduceOrder::Quad(curve, curve); |
Chris Dalton | 957189b | 2020-05-07 12:47:26 -0600 | [diff] [blame] | 126 | if (SkPath::kQuad_Verb == verb && 1 != *w) { |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 127 | verb = SkPath::kConic_Verb; |
| 128 | } else if (verb == SkPath::kMove_Verb) { |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 129 | continue; // skip degenerate points |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 130 | } |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 131 | break; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 132 | case SkPath::kCubic_Verb: |
Chris Dalton | 1fc1bd3 | 2020-05-06 11:44:51 -0600 | [diff] [blame] | 133 | curve[1] = force_small_to_zero(pts[1]); |
| 134 | curve[2] = force_small_to_zero(pts[2]); |
| 135 | curve[3] = force_small_to_zero(pts[3]); |
| 136 | verb = SkReduceOrder::Cubic(curve, curve); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 137 | if (verb == SkPath::kMove_Verb) { |
| 138 | continue; // skip degenerate points |
| 139 | } |
| 140 | break; |
| 141 | case SkPath::kClose_Verb: |
| 142 | closeContour(curve[0], curveStart); |
| 143 | lastCurve = false; |
| 144 | continue; |
| 145 | case SkPath::kDone_Verb: |
| 146 | continue; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 147 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 148 | *fPathVerbs.append() = verb; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 149 | int ptCount = SkPathOpsVerbToPoints(verb); |
Chris Dalton | 1fc1bd3 | 2020-05-06 11:44:51 -0600 | [diff] [blame] | 150 | fPathPts.append(ptCount, &curve[1]); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 151 | if (verb == SkPath::kConic_Verb) { |
Chris Dalton | 957189b | 2020-05-07 12:47:26 -0600 | [diff] [blame] | 152 | *fWeights.append() = *w; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 153 | } |
Chris Dalton | 1fc1bd3 | 2020-05-06 11:44:51 -0600 | [diff] [blame] | 154 | curve[0] = curve[ptCount]; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 155 | lastCurve = true; |
Chris Dalton | 957189b | 2020-05-07 12:47:26 -0600 | [diff] [blame] | 156 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 157 | if (!fAllowOpenContours && lastCurve) { |
| 158 | closeContour(curve[0], curveStart); |
| 159 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 160 | *fPathVerbs.append() = SkPath::kDone_Verb; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 161 | return fPathVerbs.count() - 1; |
| 162 | } |
| 163 | |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 164 | bool SkOpEdgeBuilder::close() { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 165 | complete(); |
| 166 | return true; |
| 167 | } |
| 168 | |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 169 | bool SkOpEdgeBuilder::walk() { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 170 | uint8_t* verbPtr = fPathVerbs.begin(); |
| 171 | uint8_t* endOfFirstHalf = &verbPtr[fSecondHalf]; |
Cary Clark | 79aa2f1 | 2018-05-31 16:22:02 -0400 | [diff] [blame] | 172 | SkPoint* pointsPtr = fPathPts.begin(); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 173 | SkScalar* weightPtr = fWeights.begin(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 174 | SkPath::Verb verb; |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 175 | SkOpContour* contour = fContourBuilder.contour(); |
Cary Clark | 79aa2f1 | 2018-05-31 16:22:02 -0400 | [diff] [blame] | 176 | int moveToPtrBump = 0; |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 177 | while ((verb = (SkPath::Verb) *verbPtr) != SkPath::kDone_Verb) { |
| 178 | if (verbPtr == endOfFirstHalf) { |
| 179 | fOperand = true; |
| 180 | } |
| 181 | verbPtr++; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 182 | switch (verb) { |
| 183 | case SkPath::kMove_Verb: |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 184 | if (contour && contour->count()) { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 185 | if (fAllowOpenContours) { |
| 186 | complete(); |
| 187 | } else if (!close()) { |
| 188 | return false; |
| 189 | } |
| 190 | } |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 191 | if (!contour) { |
| 192 | fContourBuilder.setContour(contour = fContoursHead->appendContour()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 193 | } |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 194 | contour->init(fGlobalState, fOperand, |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 195 | fXorMask[fOperand] == kEvenOdd_PathOpsMask); |
Cary Clark | 79aa2f1 | 2018-05-31 16:22:02 -0400 | [diff] [blame] | 196 | pointsPtr += moveToPtrBump; |
| 197 | moveToPtrBump = 1; |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 198 | continue; |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 199 | case SkPath::kLine_Verb: |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 200 | fContourBuilder.addLine(pointsPtr); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 201 | break; |
| 202 | case SkPath::kQuad_Verb: |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 203 | { |
John Stiles | 14f8d79 | 2021-08-10 16:22:22 -0400 | [diff] [blame] | 204 | SkVector vec1 = pointsPtr[1] - pointsPtr[0]; |
| 205 | SkVector vec2 = pointsPtr[2] - pointsPtr[1]; |
| 206 | if (vec1.dot(vec2) < 0) { |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 207 | SkPoint pair[5]; |
| 208 | if (SkChopQuadAtMaxCurvature(pointsPtr, pair) == 1) { |
| 209 | goto addOneQuad; |
| 210 | } |
| 211 | if (!SkScalarsAreFinite(&pair[0].fX, SK_ARRAY_COUNT(pair) * 2)) { |
| 212 | return false; |
| 213 | } |
Cary Clark | afca4d6 | 2017-12-01 15:23:00 -0500 | [diff] [blame] | 214 | for (unsigned index = 0; index < SK_ARRAY_COUNT(pair); ++index) { |
Chris Dalton | 1fc1bd3 | 2020-05-06 11:44:51 -0600 | [diff] [blame] | 215 | pair[index] = force_small_to_zero(pair[index]); |
Cary Clark | afca4d6 | 2017-12-01 15:23:00 -0500 | [diff] [blame] | 216 | } |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 217 | SkPoint cStorage[2][2]; |
| 218 | SkPath::Verb v1 = SkReduceOrder::Quad(&pair[0], cStorage[0]); |
| 219 | SkPath::Verb v2 = SkReduceOrder::Quad(&pair[2], cStorage[1]); |
caryclark | cc09372 | 2016-09-23 09:32:26 -0700 | [diff] [blame] | 220 | SkPoint* curve1 = v1 != SkPath::kLine_Verb ? &pair[0] : cStorage[0]; |
| 221 | SkPoint* curve2 = v2 != SkPath::kLine_Verb ? &pair[2] : cStorage[1]; |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 222 | if (can_add_curve(v1, curve1) && can_add_curve(v2, curve2)) { |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 223 | fContourBuilder.addCurve(v1, curve1); |
| 224 | fContourBuilder.addCurve(v2, curve2); |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 225 | break; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | addOneQuad: |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 230 | fContourBuilder.addQuad(pointsPtr); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 231 | break; |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 232 | case SkPath::kConic_Verb: { |
John Stiles | 14f8d79 | 2021-08-10 16:22:22 -0400 | [diff] [blame] | 233 | SkVector vec1 = pointsPtr[1] - pointsPtr[0]; |
| 234 | SkVector vec2 = pointsPtr[2] - pointsPtr[1]; |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 235 | SkScalar weight = *weightPtr++; |
John Stiles | 14f8d79 | 2021-08-10 16:22:22 -0400 | [diff] [blame] | 236 | if (vec1.dot(vec2) < 0) { |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 237 | // FIXME: max curvature for conics hasn't been implemented; use placeholder |
| 238 | SkScalar maxCurvature = SkFindQuadMaxCurvature(pointsPtr); |
Chris Dalton | 1d474dd | 2018-07-24 01:08:31 -0600 | [diff] [blame] | 239 | if (0 < maxCurvature && maxCurvature < 1) { |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 240 | SkConic conic(pointsPtr, weight); |
| 241 | SkConic pair[2]; |
caryclark | 414c429 | 2016-09-26 11:03:54 -0700 | [diff] [blame] | 242 | if (!conic.chopAt(maxCurvature, pair)) { |
| 243 | // if result can't be computed, use original |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 244 | fContourBuilder.addConic(pointsPtr, weight); |
caryclark | 414c429 | 2016-09-26 11:03:54 -0700 | [diff] [blame] | 245 | break; |
| 246 | } |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 247 | SkPoint cStorage[2][3]; |
| 248 | SkPath::Verb v1 = SkReduceOrder::Conic(pair[0], cStorage[0]); |
| 249 | SkPath::Verb v2 = SkReduceOrder::Conic(pair[1], cStorage[1]); |
caryclark | cc09372 | 2016-09-23 09:32:26 -0700 | [diff] [blame] | 250 | SkPoint* curve1 = v1 != SkPath::kLine_Verb ? pair[0].fPts : cStorage[0]; |
| 251 | SkPoint* curve2 = v2 != SkPath::kLine_Verb ? pair[1].fPts : cStorage[1]; |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 252 | if (can_add_curve(v1, curve1) && can_add_curve(v2, curve2)) { |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 253 | fContourBuilder.addCurve(v1, curve1, pair[0].fW); |
| 254 | fContourBuilder.addCurve(v2, curve2, pair[1].fW); |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 255 | break; |
| 256 | } |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 257 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 258 | } |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 259 | fContourBuilder.addConic(pointsPtr, weight); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 260 | } break; |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 261 | case SkPath::kCubic_Verb: |
| 262 | { |
| 263 | // Split complex cubics (such as self-intersecting curves or |
| 264 | // ones with difficult curvature) in two before proceeding. |
| 265 | // This can be required for intersection to succeed. |
Cary Clark | 7eb01e0 | 2016-12-08 14:36:32 -0500 | [diff] [blame] | 266 | SkScalar splitT[3]; |
| 267 | int breaks = SkDCubic::ComplexBreak(pointsPtr, splitT); |
| 268 | if (!breaks) { |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 269 | fContourBuilder.addCubic(pointsPtr); |
Cary Clark | 7eb01e0 | 2016-12-08 14:36:32 -0500 | [diff] [blame] | 270 | break; |
| 271 | } |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 272 | SkASSERT(breaks <= (int) SK_ARRAY_COUNT(splitT)); |
| 273 | struct Splitsville { |
| 274 | double fT[2]; |
| 275 | SkPoint fPts[4]; |
| 276 | SkPoint fReduced[4]; |
| 277 | SkPath::Verb fVerb; |
| 278 | bool fCanAdd; |
Cary Clark | 0eb6ed4 | 2016-12-16 16:31:11 -0500 | [diff] [blame] | 279 | } splits[4]; |
| 280 | SkASSERT(SK_ARRAY_COUNT(splits) == SK_ARRAY_COUNT(splitT) + 1); |
John Stiles | 886a904 | 2020-07-14 16:28:33 -0400 | [diff] [blame] | 281 | SkTQSort(splitT, splitT + breaks); |
Cary Clark | 7eb01e0 | 2016-12-08 14:36:32 -0500 | [diff] [blame] | 282 | for (int index = 0; index <= breaks; ++index) { |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 283 | Splitsville* split = &splits[index]; |
| 284 | split->fT[0] = index ? splitT[index - 1] : 0; |
| 285 | split->fT[1] = index < breaks ? splitT[index] : 1; |
| 286 | SkDCubic part = SkDCubic::SubDivide(pointsPtr, split->fT[0], split->fT[1]); |
| 287 | if (!part.toFloatPoints(split->fPts)) { |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 288 | return false; |
| 289 | } |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 290 | split->fVerb = SkReduceOrder::Cubic(split->fPts, split->fReduced); |
Kevin Lubick | 67c905c | 2020-04-21 12:20:38 -0400 | [diff] [blame] | 291 | SkPoint* curve = SkPath::kCubic_Verb == split->fVerb |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 292 | ? split->fPts : split->fReduced; |
| 293 | split->fCanAdd = can_add_curve(split->fVerb, curve); |
| 294 | } |
| 295 | for (int index = 0; index <= breaks; ++index) { |
| 296 | Splitsville* split = &splits[index]; |
| 297 | if (!split->fCanAdd) { |
| 298 | continue; |
| 299 | } |
| 300 | int prior = index; |
| 301 | while (prior > 0 && !splits[prior - 1].fCanAdd) { |
| 302 | --prior; |
| 303 | } |
| 304 | if (prior < index) { |
| 305 | split->fT[0] = splits[prior].fT[0]; |
Cary Clark | 4efcb7d | 2018-02-02 15:09:49 -0500 | [diff] [blame] | 306 | split->fPts[0] = splits[prior].fPts[0]; |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 307 | } |
| 308 | int next = index; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 309 | int breakLimit = std::min(breaks, (int) SK_ARRAY_COUNT(splits) - 1); |
Eric Boren | 746e263 | 2017-06-21 13:39:32 -0400 | [diff] [blame] | 310 | while (next < breakLimit && !splits[next + 1].fCanAdd) { |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 311 | ++next; |
| 312 | } |
| 313 | if (next > index) { |
| 314 | split->fT[1] = splits[next].fT[1]; |
Cary Clark | 4efcb7d | 2018-02-02 15:09:49 -0500 | [diff] [blame] | 315 | split->fPts[3] = splits[next].fPts[3]; |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 316 | } |
| 317 | if (prior < index || next > index) { |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 318 | split->fVerb = SkReduceOrder::Cubic(split->fPts, split->fReduced); |
| 319 | } |
| 320 | SkPoint* curve = SkPath::kCubic_Verb == split->fVerb |
| 321 | ? split->fPts : split->fReduced; |
Cary Clark | 74b4290 | 2018-03-09 07:38:47 -0500 | [diff] [blame] | 322 | if (!can_add_curve(split->fVerb, curve)) { |
| 323 | return false; |
| 324 | } |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 325 | fContourBuilder.addCurve(split->fVerb, curve); |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 328 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 329 | case SkPath::kClose_Verb: |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 330 | SkASSERT(contour); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 331 | if (!close()) { |
| 332 | return false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 333 | } |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 334 | contour = nullptr; |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 335 | continue; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 336 | default: |
| 337 | SkDEBUGFAIL("bad verb"); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 338 | return false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 339 | } |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 340 | SkASSERT(contour); |
| 341 | if (contour->count()) { |
| 342 | contour->debugValidate(); |
| 343 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 344 | pointsPtr += SkPathOpsVerbToPoints(verb); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 345 | } |
Cary Clark | ff11428 | 2016-12-14 11:56:16 -0500 | [diff] [blame] | 346 | fContourBuilder.flush(); |
| 347 | if (contour && contour->count() &&!fAllowOpenContours && !close()) { |
| 348 | return false; |
| 349 | } |
| 350 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 351 | } |