senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 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. |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "GrPathUtils.h" |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 9 | |
robertphillips | d537341 | 2014-06-02 10:20:14 -0700 | [diff] [blame] | 10 | #include "GrTypes.h" |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 11 | #include "SkGeometry.h" |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 12 | #include "SkMathPriv.h" |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 13 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 14 | SkScalar GrPathUtils::scaleToleranceToSrc(SkScalar devTol, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 15 | const SkMatrix& viewM, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 16 | const SkRect& pathBounds) { |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 17 | // In order to tesselate the path we get a bound on how much the matrix can |
commit-bot@chromium.org | 1878651 | 2014-05-20 14:53:45 +0000 | [diff] [blame] | 18 | // scale when mapping to screen coordinates. |
| 19 | SkScalar stretch = viewM.getMaxScale(); |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 20 | SkScalar srcTol = devTol; |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 21 | |
| 22 | if (stretch < 0) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 23 | // take worst case mapRadius amoung four corners. |
| 24 | // (less than perfect) |
| 25 | for (int i = 0; i < 4; ++i) { |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 26 | SkMatrix mat; |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 27 | mat.setTranslate((i % 2) ? pathBounds.fLeft : pathBounds.fRight, |
| 28 | (i < 2) ? pathBounds.fTop : pathBounds.fBottom); |
| 29 | mat.postConcat(viewM); |
| 30 | stretch = SkMaxScalar(stretch, mat.mapRadius(SK_Scalar1)); |
| 31 | } |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 32 | } |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 33 | return srcTol / stretch; |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 34 | } |
| 35 | |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 36 | static const int MAX_POINTS_PER_CURVE = 1 << 10; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 37 | static const SkScalar gMinCurveTol = 0.0001f; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 38 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 39 | uint32_t GrPathUtils::quadraticPointCount(const SkPoint points[], |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 40 | SkScalar tol) { |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 41 | if (tol < gMinCurveTol) { |
tomhudson@google.com | afec7ba | 2011-06-30 14:47:55 +0000 | [diff] [blame] | 42 | tol = gMinCurveTol; |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 43 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 44 | SkASSERT(tol > 0); |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 45 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 46 | SkScalar d = points[1].distanceToLineSegmentBetween(points[0], points[2]); |
senorblanco | b6a40b8 | 2016-08-19 08:07:22 -0700 | [diff] [blame] | 47 | if (!SkScalarIsFinite(d)) { |
| 48 | return MAX_POINTS_PER_CURVE; |
| 49 | } else if (d <= tol) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 50 | return 1; |
| 51 | } else { |
| 52 | // Each time we subdivide, d should be cut in 4. So we need to |
| 53 | // subdivide x = log4(d/tol) times. x subdivisions creates 2^(x) |
| 54 | // points. |
| 55 | // 2^(log4(x)) = sqrt(x); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 56 | SkScalar divSqrt = SkScalarSqrt(d / tol); |
egdaniel | 5a23a14 | 2015-02-25 06:41:47 -0800 | [diff] [blame] | 57 | if (((SkScalar)SK_MaxS32) <= divSqrt) { |
| 58 | return MAX_POINTS_PER_CURVE; |
| 59 | } else { |
| 60 | int temp = SkScalarCeilToInt(divSqrt); |
| 61 | int pow2 = GrNextPow2(temp); |
| 62 | // Because of NaNs & INFs we can wind up with a degenerate temp |
| 63 | // such that pow2 comes out negative. Also, our point generator |
| 64 | // will always output at least one pt. |
| 65 | if (pow2 < 1) { |
| 66 | pow2 = 1; |
| 67 | } |
| 68 | return SkTMin(pow2, MAX_POINTS_PER_CURVE); |
bsalomon@google.com | 61f3bde | 2011-06-17 20:06:49 +0000 | [diff] [blame] | 69 | } |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 73 | uint32_t GrPathUtils::generateQuadraticPoints(const SkPoint& p0, |
| 74 | const SkPoint& p1, |
| 75 | const SkPoint& p2, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 76 | SkScalar tolSqd, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 77 | SkPoint** points, |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 78 | uint32_t pointsLeft) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 79 | if (pointsLeft < 2 || |
| 80 | (p1.distanceToLineSegmentBetweenSqd(p0, p2)) < tolSqd) { |
| 81 | (*points)[0] = p2; |
| 82 | *points += 1; |
| 83 | return 1; |
| 84 | } |
| 85 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 86 | SkPoint q[] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 87 | { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, |
| 88 | { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 89 | }; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 90 | SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 91 | |
| 92 | pointsLeft >>= 1; |
| 93 | uint32_t a = generateQuadraticPoints(p0, q[0], r, tolSqd, points, pointsLeft); |
| 94 | uint32_t b = generateQuadraticPoints(r, q[1], p2, tolSqd, points, pointsLeft); |
| 95 | return a + b; |
| 96 | } |
| 97 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 98 | uint32_t GrPathUtils::cubicPointCount(const SkPoint points[], |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 99 | SkScalar tol) { |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 100 | if (tol < gMinCurveTol) { |
tomhudson@google.com | afec7ba | 2011-06-30 14:47:55 +0000 | [diff] [blame] | 101 | tol = gMinCurveTol; |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 102 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 103 | SkASSERT(tol > 0); |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 104 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 105 | SkScalar d = SkTMax( |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 106 | points[1].distanceToLineSegmentBetweenSqd(points[0], points[3]), |
| 107 | points[2].distanceToLineSegmentBetweenSqd(points[0], points[3])); |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 108 | d = SkScalarSqrt(d); |
senorblanco | b6a40b8 | 2016-08-19 08:07:22 -0700 | [diff] [blame] | 109 | if (!SkScalarIsFinite(d)) { |
| 110 | return MAX_POINTS_PER_CURVE; |
| 111 | } else if (d <= tol) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 112 | return 1; |
| 113 | } else { |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 114 | SkScalar divSqrt = SkScalarSqrt(d / tol); |
egdaniel | 5a23a14 | 2015-02-25 06:41:47 -0800 | [diff] [blame] | 115 | if (((SkScalar)SK_MaxS32) <= divSqrt) { |
| 116 | return MAX_POINTS_PER_CURVE; |
| 117 | } else { |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 118 | int temp = SkScalarCeilToInt(SkScalarSqrt(d / tol)); |
egdaniel | 5a23a14 | 2015-02-25 06:41:47 -0800 | [diff] [blame] | 119 | int pow2 = GrNextPow2(temp); |
| 120 | // Because of NaNs & INFs we can wind up with a degenerate temp |
| 121 | // such that pow2 comes out negative. Also, our point generator |
| 122 | // will always output at least one pt. |
| 123 | if (pow2 < 1) { |
| 124 | pow2 = 1; |
| 125 | } |
| 126 | return SkTMin(pow2, MAX_POINTS_PER_CURVE); |
bsalomon@google.com | 61f3bde | 2011-06-17 20:06:49 +0000 | [diff] [blame] | 127 | } |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 131 | uint32_t GrPathUtils::generateCubicPoints(const SkPoint& p0, |
| 132 | const SkPoint& p1, |
| 133 | const SkPoint& p2, |
| 134 | const SkPoint& p3, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 135 | SkScalar tolSqd, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 136 | SkPoint** points, |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 137 | uint32_t pointsLeft) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 138 | if (pointsLeft < 2 || |
| 139 | (p1.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd && |
| 140 | p2.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd)) { |
robertphillips | f08ce6c | 2015-12-08 05:19:12 -0800 | [diff] [blame] | 141 | (*points)[0] = p3; |
| 142 | *points += 1; |
| 143 | return 1; |
| 144 | } |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 145 | SkPoint q[] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 146 | { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, |
| 147 | { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, |
| 148 | { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) } |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 149 | }; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 150 | SkPoint r[] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 151 | { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }, |
| 152 | { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) } |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 153 | }; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 154 | SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) }; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 155 | pointsLeft >>= 1; |
| 156 | uint32_t a = generateCubicPoints(p0, q[0], r[0], s, tolSqd, points, pointsLeft); |
| 157 | uint32_t b = generateCubicPoints(s, r[1], q[2], p3, tolSqd, points, pointsLeft); |
| 158 | return a + b; |
| 159 | } |
| 160 | |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 161 | int GrPathUtils::worstCasePointCount(const SkPath& path, int* subpaths, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 162 | SkScalar tol) { |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 163 | if (tol < gMinCurveTol) { |
tomhudson@google.com | afec7ba | 2011-06-30 14:47:55 +0000 | [diff] [blame] | 164 | tol = gMinCurveTol; |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 165 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 166 | SkASSERT(tol > 0); |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 167 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 168 | int pointCount = 0; |
| 169 | *subpaths = 1; |
| 170 | |
| 171 | bool first = true; |
| 172 | |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 173 | SkPath::Iter iter(path, false); |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 174 | SkPath::Verb verb; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 175 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 176 | SkPoint pts[4]; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 177 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 178 | |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 179 | switch (verb) { |
| 180 | case SkPath::kLine_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 181 | pointCount += 1; |
| 182 | break; |
egdaniel | af18a09 | 2015-01-05 10:22:28 -0800 | [diff] [blame] | 183 | case SkPath::kConic_Verb: { |
| 184 | SkScalar weight = iter.conicWeight(); |
| 185 | SkAutoConicToQuads converter; |
| 186 | const SkPoint* quadPts = converter.computeQuads(pts, weight, 0.25f); |
| 187 | for (int i = 0; i < converter.countQuads(); ++i) { |
| 188 | pointCount += quadraticPointCount(quadPts + 2*i, tol); |
| 189 | } |
| 190 | } |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 191 | case SkPath::kQuad_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 192 | pointCount += quadraticPointCount(pts, tol); |
| 193 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 194 | case SkPath::kCubic_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 195 | pointCount += cubicPointCount(pts, tol); |
| 196 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 197 | case SkPath::kMove_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 198 | pointCount += 1; |
| 199 | if (!first) { |
| 200 | ++(*subpaths); |
| 201 | } |
| 202 | break; |
| 203 | default: |
| 204 | break; |
| 205 | } |
| 206 | first = false; |
| 207 | } |
| 208 | return pointCount; |
| 209 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 210 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 211 | void GrPathUtils::QuadUVMatrix::set(const SkPoint qPts[3]) { |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 212 | SkMatrix m; |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 213 | // We want M such that M * xy_pt = uv_pt |
| 214 | // We know M * control_pts = [0 1/2 1] |
| 215 | // [0 0 1] |
| 216 | // [1 1 1] |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 217 | // And control_pts = [x0 x1 x2] |
| 218 | // [y0 y1 y2] |
| 219 | // [1 1 1 ] |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 220 | // We invert the control pt matrix and post concat to both sides to get M. |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 221 | // Using the known form of the control point matrix and the result, we can |
| 222 | // optimize and improve precision. |
| 223 | |
| 224 | double x0 = qPts[0].fX; |
| 225 | double y0 = qPts[0].fY; |
| 226 | double x1 = qPts[1].fX; |
| 227 | double y1 = qPts[1].fY; |
| 228 | double x2 = qPts[2].fX; |
| 229 | double y2 = qPts[2].fY; |
| 230 | double det = x0*y1 - y0*x1 + x2*y0 - y2*x0 + x1*y2 - y1*x2; |
| 231 | |
skia.committer@gmail.com | 8491d24 | 2013-12-05 07:02:16 +0000 | [diff] [blame] | 232 | if (!sk_float_isfinite(det) |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 233 | || SkScalarNearlyZero((float)det, SK_ScalarNearlyZero * SK_ScalarNearlyZero)) { |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 234 | // The quad is degenerate. Hopefully this is rare. Find the pts that are |
| 235 | // farthest apart to compute a line (unless it is really a pt). |
| 236 | SkScalar maxD = qPts[0].distanceToSqd(qPts[1]); |
| 237 | int maxEdge = 0; |
| 238 | SkScalar d = qPts[1].distanceToSqd(qPts[2]); |
| 239 | if (d > maxD) { |
| 240 | maxD = d; |
| 241 | maxEdge = 1; |
| 242 | } |
| 243 | d = qPts[2].distanceToSqd(qPts[0]); |
| 244 | if (d > maxD) { |
| 245 | maxD = d; |
| 246 | maxEdge = 2; |
| 247 | } |
| 248 | // We could have a tolerance here, not sure if it would improve anything |
| 249 | if (maxD > 0) { |
| 250 | // Set the matrix to give (u = 0, v = distance_to_line) |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 251 | SkVector lineVec = qPts[(maxEdge + 1)%3] - qPts[maxEdge]; |
bsalomon@google.com | 20e542e | 2012-02-15 18:49:41 +0000 | [diff] [blame] | 252 | // when looking from the point 0 down the line we want positive |
| 253 | // distances to be to the left. This matches the non-degenerate |
| 254 | // case. |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 255 | lineVec.setOrthog(lineVec, SkPoint::kLeft_Side); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 256 | // first row |
| 257 | fM[0] = 0; |
| 258 | fM[1] = 0; |
| 259 | fM[2] = 0; |
| 260 | // second row |
| 261 | fM[3] = lineVec.fX; |
| 262 | fM[4] = lineVec.fY; |
| 263 | fM[5] = -lineVec.dot(qPts[maxEdge]); |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 264 | } else { |
| 265 | // It's a point. It should cover zero area. Just set the matrix such |
| 266 | // that (u, v) will always be far away from the quad. |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 267 | fM[0] = 0; fM[1] = 0; fM[2] = 100.f; |
| 268 | fM[3] = 0; fM[4] = 0; fM[5] = 100.f; |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 269 | } |
| 270 | } else { |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 271 | double scale = 1.0/det; |
| 272 | |
| 273 | // compute adjugate matrix |
robertphillips | 87a2234 | 2016-03-01 14:49:44 -0800 | [diff] [blame] | 274 | double a2, a3, a4, a5, a6, a7, a8; |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 275 | a2 = x1*y2-x2*y1; |
| 276 | |
| 277 | a3 = y2-y0; |
| 278 | a4 = x0-x2; |
| 279 | a5 = x2*y0-x0*y2; |
| 280 | |
| 281 | a6 = y0-y1; |
| 282 | a7 = x1-x0; |
| 283 | a8 = x0*y1-x1*y0; |
| 284 | |
skia.committer@gmail.com | 8491d24 | 2013-12-05 07:02:16 +0000 | [diff] [blame] | 285 | // this performs the uv_pts*adjugate(control_pts) multiply, |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 286 | // then does the scale by 1/det afterwards to improve precision |
| 287 | m[SkMatrix::kMScaleX] = (float)((0.5*a3 + a6)*scale); |
| 288 | m[SkMatrix::kMSkewX] = (float)((0.5*a4 + a7)*scale); |
| 289 | m[SkMatrix::kMTransX] = (float)((0.5*a5 + a8)*scale); |
| 290 | |
| 291 | m[SkMatrix::kMSkewY] = (float)(a6*scale); |
| 292 | m[SkMatrix::kMScaleY] = (float)(a7*scale); |
| 293 | m[SkMatrix::kMTransY] = (float)(a8*scale); |
| 294 | |
robertphillips | 87a2234 | 2016-03-01 14:49:44 -0800 | [diff] [blame] | 295 | // kMPersp0 & kMPersp1 should algebraically be zero |
| 296 | m[SkMatrix::kMPersp0] = 0.0f; |
| 297 | m[SkMatrix::kMPersp1] = 0.0f; |
commit-bot@chromium.org | f543fd9 | 2013-12-04 21:33:08 +0000 | [diff] [blame] | 298 | m[SkMatrix::kMPersp2] = (float)((a2 + a5 + a8)*scale); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 299 | |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 300 | // It may not be normalized to have 1.0 in the bottom right |
| 301 | float m33 = m.get(SkMatrix::kMPersp2); |
| 302 | if (1.f != m33) { |
| 303 | m33 = 1.f / m33; |
| 304 | fM[0] = m33 * m.get(SkMatrix::kMScaleX); |
| 305 | fM[1] = m33 * m.get(SkMatrix::kMSkewX); |
| 306 | fM[2] = m33 * m.get(SkMatrix::kMTransX); |
| 307 | fM[3] = m33 * m.get(SkMatrix::kMSkewY); |
| 308 | fM[4] = m33 * m.get(SkMatrix::kMScaleY); |
| 309 | fM[5] = m33 * m.get(SkMatrix::kMTransY); |
| 310 | } else { |
| 311 | fM[0] = m.get(SkMatrix::kMScaleX); |
| 312 | fM[1] = m.get(SkMatrix::kMSkewX); |
| 313 | fM[2] = m.get(SkMatrix::kMTransX); |
| 314 | fM[3] = m.get(SkMatrix::kMSkewY); |
| 315 | fM[4] = m.get(SkMatrix::kMScaleY); |
| 316 | fM[5] = m.get(SkMatrix::kMTransY); |
| 317 | } |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 318 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 319 | } |
| 320 | |
commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 321 | //////////////////////////////////////////////////////////////////////////////// |
| 322 | |
Dean McNamee | 3b830a9 | 2017-01-13 12:17:09 +0000 | [diff] [blame] | 323 | // k = (y2 - y0, x0 - x2, x2*y0 - x0*y2) |
| 324 | // l = (y1 - y0, x0 - x1, x1*y0 - x0*y1) * 2*w |
| 325 | // m = (y2 - y1, x1 - x2, x2*y1 - x1*y2) * 2*w |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 326 | void GrPathUtils::getConicKLM(const SkPoint p[3], const SkScalar weight, SkMatrix* out) { |
| 327 | SkMatrix& klm = *out; |
commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 328 | const SkScalar w2 = 2.f * weight; |
| 329 | klm[0] = p[2].fY - p[0].fY; |
| 330 | klm[1] = p[0].fX - p[2].fX; |
Dean McNamee | 3b830a9 | 2017-01-13 12:17:09 +0000 | [diff] [blame] | 331 | klm[2] = p[2].fX * p[0].fY - p[0].fX * p[2].fY; |
commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 332 | |
| 333 | klm[3] = w2 * (p[1].fY - p[0].fY); |
| 334 | klm[4] = w2 * (p[0].fX - p[1].fX); |
| 335 | klm[5] = w2 * (p[1].fX * p[0].fY - p[0].fX * p[1].fY); |
| 336 | |
| 337 | klm[6] = w2 * (p[2].fY - p[1].fY); |
| 338 | klm[7] = w2 * (p[1].fX - p[2].fX); |
| 339 | klm[8] = w2 * (p[2].fX * p[1].fY - p[1].fX * p[2].fY); |
| 340 | |
| 341 | // scale the max absolute value of coeffs to 10 |
| 342 | SkScalar scale = 0.f; |
| 343 | for (int i = 0; i < 9; ++i) { |
| 344 | scale = SkMaxScalar(scale, SkScalarAbs(klm[i])); |
| 345 | } |
| 346 | SkASSERT(scale > 0.f); |
| 347 | scale = 10.f / scale; |
| 348 | for (int i = 0; i < 9; ++i) { |
| 349 | klm[i] *= scale; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | //////////////////////////////////////////////////////////////////////////////// |
| 354 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 355 | namespace { |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 356 | |
| 357 | // a is the first control point of the cubic. |
| 358 | // ab is the vector from a to the second control point. |
| 359 | // dc is the vector from the fourth to the third control point. |
| 360 | // d is the fourth control point. |
| 361 | // p is the candidate quadratic control point. |
| 362 | // this assumes that the cubic doesn't inflect and is simple |
| 363 | bool is_point_within_cubic_tangents(const SkPoint& a, |
| 364 | const SkVector& ab, |
| 365 | const SkVector& dc, |
| 366 | const SkPoint& d, |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 367 | SkPathPriv::FirstDirection dir, |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 368 | const SkPoint p) { |
| 369 | SkVector ap = p - a; |
| 370 | SkScalar apXab = ap.cross(ab); |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 371 | if (SkPathPriv::kCW_FirstDirection == dir) { |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 372 | if (apXab > 0) { |
| 373 | return false; |
| 374 | } |
| 375 | } else { |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 376 | SkASSERT(SkPathPriv::kCCW_FirstDirection == dir); |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 377 | if (apXab < 0) { |
| 378 | return false; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | SkVector dp = p - d; |
| 383 | SkScalar dpXdc = dp.cross(dc); |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 384 | if (SkPathPriv::kCW_FirstDirection == dir) { |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 385 | if (dpXdc < 0) { |
| 386 | return false; |
| 387 | } |
| 388 | } else { |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 389 | SkASSERT(SkPathPriv::kCCW_FirstDirection == dir); |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 390 | if (dpXdc > 0) { |
| 391 | return false; |
| 392 | } |
| 393 | } |
| 394 | return true; |
| 395 | } |
| 396 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 397 | void convert_noninflect_cubic_to_quads(const SkPoint p[4], |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 398 | SkScalar toleranceSqd, |
| 399 | bool constrainWithinTangents, |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 400 | SkPathPriv::FirstDirection dir, |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 401 | SkTArray<SkPoint, true>* quads, |
| 402 | int sublevel = 0) { |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 403 | |
| 404 | // Notation: Point a is always p[0]. Point b is p[1] unless p[1] == p[0], in which case it is |
| 405 | // p[2]. Point d is always p[3]. Point c is p[2] unless p[2] == p[3], in which case it is p[1]. |
| 406 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 407 | SkVector ab = p[1] - p[0]; |
| 408 | SkVector dc = p[2] - p[3]; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 409 | |
robertphillips | f08ce6c | 2015-12-08 05:19:12 -0800 | [diff] [blame] | 410 | if (ab.lengthSqd() < SK_ScalarNearlyZero) { |
| 411 | if (dc.lengthSqd() < SK_ScalarNearlyZero) { |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 412 | SkPoint* degQuad = quads->push_back_n(3); |
| 413 | degQuad[0] = p[0]; |
| 414 | degQuad[1] = p[0]; |
| 415 | degQuad[2] = p[3]; |
| 416 | return; |
| 417 | } |
| 418 | ab = p[2] - p[0]; |
| 419 | } |
robertphillips | f08ce6c | 2015-12-08 05:19:12 -0800 | [diff] [blame] | 420 | if (dc.lengthSqd() < SK_ScalarNearlyZero) { |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 421 | dc = p[1] - p[3]; |
| 422 | } |
| 423 | |
bsalomon | 3935a7b | 2014-06-19 12:33:08 -0700 | [diff] [blame] | 424 | // When the ab and cd tangents are degenerate or nearly parallel with vector from d to a the |
| 425 | // constraint that the quad point falls between the tangents becomes hard to enforce and we are |
| 426 | // likely to hit the max subdivision count. However, in this case the cubic is approaching a |
| 427 | // line and the accuracy of the quad point isn't so important. We check if the two middle cubic |
| 428 | // control points are very close to the baseline vector. If so then we just pick quadratic |
| 429 | // points on the control polygon. |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 430 | |
| 431 | if (constrainWithinTangents) { |
| 432 | SkVector da = p[0] - p[3]; |
bsalomon | 3935a7b | 2014-06-19 12:33:08 -0700 | [diff] [blame] | 433 | bool doQuads = dc.lengthSqd() < SK_ScalarNearlyZero || |
| 434 | ab.lengthSqd() < SK_ScalarNearlyZero; |
| 435 | if (!doQuads) { |
| 436 | SkScalar invDALengthSqd = da.lengthSqd(); |
| 437 | if (invDALengthSqd > SK_ScalarNearlyZero) { |
| 438 | invDALengthSqd = SkScalarInvert(invDALengthSqd); |
| 439 | // cross(ab, da)^2/length(da)^2 == sqd distance from b to line from d to a. |
| 440 | // same goes for point c using vector cd. |
| 441 | SkScalar detABSqd = ab.cross(da); |
| 442 | detABSqd = SkScalarSquare(detABSqd); |
| 443 | SkScalar detDCSqd = dc.cross(da); |
| 444 | detDCSqd = SkScalarSquare(detDCSqd); |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 445 | if (detABSqd * invDALengthSqd < toleranceSqd && |
| 446 | detDCSqd * invDALengthSqd < toleranceSqd) |
| 447 | { |
bsalomon | 3935a7b | 2014-06-19 12:33:08 -0700 | [diff] [blame] | 448 | doQuads = true; |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 449 | } |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
bsalomon | 3935a7b | 2014-06-19 12:33:08 -0700 | [diff] [blame] | 452 | if (doQuads) { |
| 453 | SkPoint b = p[0] + ab; |
| 454 | SkPoint c = p[3] + dc; |
| 455 | SkPoint mid = b + c; |
| 456 | mid.scale(SK_ScalarHalf); |
| 457 | // Insert two quadratics to cover the case when ab points away from d and/or dc |
| 458 | // points away from a. |
| 459 | if (SkVector::DotProduct(da, dc) < 0 || SkVector::DotProduct(ab,da) > 0) { |
| 460 | SkPoint* qpts = quads->push_back_n(6); |
| 461 | qpts[0] = p[0]; |
| 462 | qpts[1] = b; |
| 463 | qpts[2] = mid; |
| 464 | qpts[3] = mid; |
| 465 | qpts[4] = c; |
| 466 | qpts[5] = p[3]; |
| 467 | } else { |
| 468 | SkPoint* qpts = quads->push_back_n(3); |
| 469 | qpts[0] = p[0]; |
| 470 | qpts[1] = mid; |
| 471 | qpts[2] = p[3]; |
| 472 | } |
| 473 | return; |
| 474 | } |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 475 | } |
| 476 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 477 | static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 478 | static const int kMaxSubdivs = 10; |
| 479 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 480 | ab.scale(kLengthScale); |
| 481 | dc.scale(kLengthScale); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 482 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 483 | // e0 and e1 are extrapolations along vectors ab and dc. |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 484 | SkVector c0 = p[0]; |
| 485 | c0 += ab; |
| 486 | SkVector c1 = p[3]; |
| 487 | c1 += dc; |
| 488 | |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 489 | SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : c0.distanceToSqd(c1); |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 490 | if (dSqd < toleranceSqd) { |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 491 | SkPoint cAvg = c0; |
| 492 | cAvg += c1; |
| 493 | cAvg.scale(SK_ScalarHalf); |
| 494 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 495 | bool subdivide = false; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 496 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 497 | if (constrainWithinTangents && |
| 498 | !is_point_within_cubic_tangents(p[0], ab, dc, p[3], dir, cAvg)) { |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 499 | // choose a new cAvg that is the intersection of the two tangent lines. |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 500 | ab.setOrthog(ab); |
| 501 | SkScalar z0 = -ab.dot(p[0]); |
| 502 | dc.setOrthog(dc); |
| 503 | SkScalar z1 = -dc.dot(p[3]); |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 504 | cAvg.fX = ab.fY * z1 - z0 * dc.fY; |
| 505 | cAvg.fY = z0 * dc.fX - ab.fX * z1; |
| 506 | SkScalar z = ab.fX * dc.fY - ab.fY * dc.fX; |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 507 | z = SkScalarInvert(z); |
| 508 | cAvg.fX *= z; |
| 509 | cAvg.fY *= z; |
| 510 | if (sublevel <= kMaxSubdivs) { |
| 511 | SkScalar d0Sqd = c0.distanceToSqd(cAvg); |
| 512 | SkScalar d1Sqd = c1.distanceToSqd(cAvg); |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 513 | // We need to subdivide if d0 + d1 > tolerance but we have the sqd values. We know |
| 514 | // the distances and tolerance can't be negative. |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 515 | // (d0 + d1)^2 > toleranceSqd |
| 516 | // d0Sqd + 2*d0*d1 + d1Sqd > toleranceSqd |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 517 | SkScalar d0d1 = SkScalarSqrt(d0Sqd * d1Sqd); |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 518 | subdivide = 2 * d0d1 + d0Sqd + d1Sqd > toleranceSqd; |
| 519 | } |
| 520 | } |
| 521 | if (!subdivide) { |
| 522 | SkPoint* pts = quads->push_back_n(3); |
| 523 | pts[0] = p[0]; |
| 524 | pts[1] = cAvg; |
| 525 | pts[2] = p[3]; |
| 526 | return; |
| 527 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 528 | } |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 529 | SkPoint choppedPts[7]; |
| 530 | SkChopCubicAtHalf(p, choppedPts); |
| 531 | convert_noninflect_cubic_to_quads(choppedPts + 0, |
| 532 | toleranceSqd, |
| 533 | constrainWithinTangents, |
| 534 | dir, |
| 535 | quads, |
| 536 | sublevel + 1); |
| 537 | convert_noninflect_cubic_to_quads(choppedPts + 3, |
| 538 | toleranceSqd, |
| 539 | constrainWithinTangents, |
| 540 | dir, |
| 541 | quads, |
| 542 | sublevel + 1); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 546 | void GrPathUtils::convertCubicToQuads(const SkPoint p[4], |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 547 | SkScalar tolScale, |
| 548 | SkTArray<SkPoint, true>* quads) { |
| 549 | SkPoint chopped[10]; |
| 550 | int count = SkChopCubicAtInflections(p, chopped); |
| 551 | |
bsalomon | 18fab30 | 2016-02-16 08:00:05 -0800 | [diff] [blame] | 552 | const SkScalar tolSqd = SkScalarSquare(tolScale); |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 553 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 554 | for (int i = 0; i < count; ++i) { |
| 555 | SkPoint* cubic = chopped + 3*i; |
bsalomon | 18fab30 | 2016-02-16 08:00:05 -0800 | [diff] [blame] | 556 | // The direction param is ignored if the third param is false. |
| 557 | convert_noninflect_cubic_to_quads(cubic, tolSqd, false, |
| 558 | SkPathPriv::kCCW_FirstDirection, quads); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 559 | } |
bsalomon | 18fab30 | 2016-02-16 08:00:05 -0800 | [diff] [blame] | 560 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 561 | |
bsalomon | 18fab30 | 2016-02-16 08:00:05 -0800 | [diff] [blame] | 562 | void GrPathUtils::convertCubicToQuadsConstrainToTangents(const SkPoint p[4], |
| 563 | SkScalar tolScale, |
| 564 | SkPathPriv::FirstDirection dir, |
| 565 | SkTArray<SkPoint, true>* quads) { |
| 566 | SkPoint chopped[10]; |
| 567 | int count = SkChopCubicAtInflections(p, chopped); |
| 568 | |
| 569 | const SkScalar tolSqd = SkScalarSquare(tolScale); |
| 570 | |
| 571 | for (int i = 0; i < count; ++i) { |
| 572 | SkPoint* cubic = chopped + 3*i; |
| 573 | convert_noninflect_cubic_to_quads(cubic, tolSqd, true, dir, quads); |
| 574 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 575 | } |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 576 | |
| 577 | //////////////////////////////////////////////////////////////////////////////// |
| 578 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 579 | /** |
| 580 | * Computes an SkMatrix that can find the cubic KLM functionals as follows: |
| 581 | * |
| 582 | * | ..K.. | | ..kcoeffs.. | |
| 583 | * | ..L.. | = | ..lcoeffs.. | * inverse_transpose_power_basis_matrix |
| 584 | * | ..M.. | | ..mcoeffs.. | |
| 585 | * |
| 586 | * 'kcoeffs' are the power basis coefficients to a scalar valued cubic function that returns the |
| 587 | * signed distance to line K from a given point on the curve: |
| 588 | * |
| 589 | * k(t,s) = C(t,s) * K [C(t,s) is defined in the following comment] |
| 590 | * |
| 591 | * The same applies for lcoeffs and mcoeffs. These are found separately, depending on the type of |
| 592 | * curve. There are 4 coefficients but 3 rows in the matrix, so in order to do this calculation the |
| 593 | * caller must first remove a specific column of coefficients. |
| 594 | * |
| 595 | * @return which column of klm coefficients to exclude from the calculation. |
| 596 | */ |
| 597 | static int calc_inverse_transpose_power_basis_matrix(const SkPoint pts[4], SkMatrix* out) { |
| 598 | using SkScalar4 = SkNx<4, SkScalar>; |
| 599 | |
| 600 | // First we convert the bezier coordinates 'pts' to power basis coefficients X,Y,W=[0 0 0 1]. |
| 601 | // M3 is the matrix that does this conversion. The homogeneous equation for the cubic becomes: |
| 602 | // |
| 603 | // | X Y 0 | |
| 604 | // C(t,s) = [t^3 t^2*s t*s^2 s^3] * | . . 0 | |
| 605 | // | . . 0 | |
| 606 | // | . . 1 | |
| 607 | // |
| 608 | const SkScalar4 M3[3] = {SkScalar4(-1, 3, -3, 1), |
| 609 | SkScalar4(3, -6, 3, 0), |
| 610 | SkScalar4(-3, 3, 0, 0)}; |
| 611 | // 4th column of M3 = SkScalar4(1, 0, 0, 0)}; |
| 612 | SkScalar4 X(pts[3].x(), 0, 0, 0); |
| 613 | SkScalar4 Y(pts[3].y(), 0, 0, 0); |
| 614 | for (int i = 2; i >= 0; --i) { |
| 615 | X += M3[i] * pts[i].x(); |
| 616 | Y += M3[i] * pts[i].y(); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 617 | } |
| 618 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 619 | // The matrix is 3x4. In order to invert it, we first need to make it square by throwing out one |
csmartdalton | 0f47a25 | 2017-04-10 11:58:17 -0600 | [diff] [blame] | 620 | // of the top three rows. We toss the row that leaves us with the largest absolute determinant. |
| 621 | // Since the right column will be [0 0 1], the determinant reduces to x0*y1 - y0*x1. |
| 622 | SkScalar absDet[4]; |
| 623 | const SkScalar4 DETX1 = SkNx_shuffle<1,0,0,3>(X), DETY1 = SkNx_shuffle<1,0,0,3>(Y); |
| 624 | const SkScalar4 DETX2 = SkNx_shuffle<2,2,1,3>(X), DETY2 = SkNx_shuffle<2,2,1,3>(Y); |
| 625 | const SkScalar4 DET = DETX1 * DETY2 - DETY1 * DETX2; |
| 626 | DET.abs().store(absDet); |
| 627 | const int skipRow = absDet[0] > absDet[2] ? (absDet[0] > absDet[1] ? 0 : 1) |
| 628 | : (absDet[1] > absDet[2] ? 1 : 2); |
| 629 | const SkScalar rdet = 1 / DET[skipRow]; |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 630 | const int row0 = (0 != skipRow) ? 0 : 1; |
| 631 | const int row1 = (2 == skipRow) ? 1 : 2; |
| 632 | |
| 633 | // Compute the inverse-transpose of the power basis matrix with the 'skipRow'th row removed. |
| 634 | // Since W=[0 0 0 1], it follows that our corresponding solution will be equal to: |
| 635 | // |
| 636 | // | y1 -x1 x1*y2 - y1*x2 | |
| 637 | // 1/det * | -y0 x0 -x0*y2 + y0*x2 | |
| 638 | // | 0 0 det | |
| 639 | // |
| 640 | const SkScalar4 R(rdet, rdet, rdet, 1); |
| 641 | X *= R; |
| 642 | Y *= R; |
| 643 | |
| 644 | SkScalar x[4], y[4], z[4]; |
| 645 | X.store(x); |
| 646 | Y.store(y); |
| 647 | (X * SkNx_shuffle<3,3,3,3>(Y) - Y * SkNx_shuffle<3,3,3,3>(X)).store(z); |
| 648 | |
| 649 | out->setAll( y[row1], -x[row1], z[row1], |
| 650 | -y[row0], x[row0], -z[row0], |
| 651 | 0, 0, 1); |
| 652 | |
| 653 | return skipRow; |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 654 | } |
| 655 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 656 | static void negate_kl(SkMatrix* klm) { |
| 657 | // We could use klm->postScale(-1, -1), but it ends up doing a full matrix multiply. |
| 658 | for (int i = 0; i < 6; ++i) { |
| 659 | (*klm)[i] = -(*klm)[i]; |
| 660 | } |
| 661 | } |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 662 | |
Chris Dalton | 4343654 | 2017-04-13 14:26:00 -0600 | [diff] [blame^] | 663 | static void calc_serp_klm(const SkPoint pts[4], const SkScalar d[4], SkMatrix* klm) { |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 664 | SkMatrix CIT; |
| 665 | int skipCol = calc_inverse_transpose_power_basis_matrix(pts, &CIT); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 666 | |
Chris Dalton | 4343654 | 2017-04-13 14:26:00 -0600 | [diff] [blame^] | 667 | SkASSERT(d[0] >= 0); |
| 668 | const SkScalar root = SkScalarSqrt(3 * d[0]); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 669 | |
Chris Dalton | 4343654 | 2017-04-13 14:26:00 -0600 | [diff] [blame^] | 670 | const SkScalar tl = 3 * d[2] + root; |
| 671 | const SkScalar sl = 6 * d[1]; |
| 672 | const SkScalar tm = 3 * d[2] - root; |
| 673 | const SkScalar sm = 6 * d[1]; |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 674 | |
| 675 | SkMatrix klmCoeffs; |
| 676 | int col = 0; |
| 677 | if (0 != skipCol) { |
| 678 | klmCoeffs[0] = 0; |
| 679 | klmCoeffs[3] = -sl * sl * sl; |
| 680 | klmCoeffs[6] = -sm * sm * sm; |
| 681 | ++col; |
| 682 | } |
| 683 | if (1 != skipCol) { |
| 684 | klmCoeffs[col + 0] = sl * sm; |
| 685 | klmCoeffs[col + 3] = 3 * sl * sl * tl; |
| 686 | klmCoeffs[col + 6] = 3 * sm * sm * tm; |
| 687 | ++col; |
| 688 | } |
| 689 | if (2 != skipCol) { |
| 690 | klmCoeffs[col + 0] = -tl * sm - tm * sl; |
| 691 | klmCoeffs[col + 3] = -3 * sl * tl * tl; |
| 692 | klmCoeffs[col + 6] = -3 * sm * tm * tm; |
| 693 | ++col; |
| 694 | } |
| 695 | |
| 696 | SkASSERT(2 == col); |
| 697 | klmCoeffs[2] = tl * tm; |
| 698 | klmCoeffs[5] = tl * tl * tl; |
| 699 | klmCoeffs[8] = tm * tm * tm; |
| 700 | |
| 701 | klm->setConcat(klmCoeffs, CIT); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 702 | |
Chris Dalton | 4343654 | 2017-04-13 14:26:00 -0600 | [diff] [blame^] | 703 | // If d1 > 0 we need to flip the orientation of our curve |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 704 | // This is done by negating the k and l values |
| 705 | // We want negative distance values to be on the inside |
Chris Dalton | 4343654 | 2017-04-13 14:26:00 -0600 | [diff] [blame^] | 706 | if (d[1] > 0) { |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 707 | negate_kl(klm); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 711 | static void calc_loop_klm(const SkPoint pts[4], SkScalar d1, SkScalar td, SkScalar sd, |
| 712 | SkScalar te, SkScalar se, SkMatrix* klm) { |
| 713 | SkMatrix CIT; |
| 714 | int skipCol = calc_inverse_transpose_power_basis_matrix(pts, &CIT); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 715 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 716 | const SkScalar tesd = te * sd; |
| 717 | const SkScalar tdse = td * se; |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 718 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 719 | SkMatrix klmCoeffs; |
| 720 | int col = 0; |
| 721 | if (0 != skipCol) { |
| 722 | klmCoeffs[0] = 0; |
| 723 | klmCoeffs[3] = -sd * sd * se; |
| 724 | klmCoeffs[6] = -se * se * sd; |
| 725 | ++col; |
| 726 | } |
| 727 | if (1 != skipCol) { |
| 728 | klmCoeffs[col + 0] = sd * se; |
| 729 | klmCoeffs[col + 3] = sd * (2 * tdse + tesd); |
| 730 | klmCoeffs[col + 6] = se * (2 * tesd + tdse); |
| 731 | ++col; |
| 732 | } |
| 733 | if (2 != skipCol) { |
| 734 | klmCoeffs[col + 0] = -tdse - tesd; |
| 735 | klmCoeffs[col + 3] = -td * (tdse + 2 * tesd); |
| 736 | klmCoeffs[col + 6] = -te * (tesd + 2 * tdse); |
| 737 | ++col; |
| 738 | } |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 739 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 740 | SkASSERT(2 == col); |
| 741 | klmCoeffs[2] = td * te; |
| 742 | klmCoeffs[5] = td * td * te; |
| 743 | klmCoeffs[8] = te * te * td; |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 744 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 745 | klm->setConcat(klmCoeffs, CIT); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 746 | |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 747 | // For the general loop curve, we flip the orientation in the same pattern as the serp case |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 748 | // above. Thus we only check d1. Technically we should check the value of the hessian as well |
| 749 | // cause we care about the sign of d1*Hessian. However, the Hessian is always negative outside |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 750 | // the loop section and positive inside. We take care of the flipping for the loop sections |
| 751 | // later on. |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 752 | if (d1 > 0) { |
| 753 | negate_kl(klm); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 757 | // For the case when we have a cusp at a parameter value of infinity (discr == 0, d1 == 0). |
| 758 | static void calc_inf_cusp_klm(const SkPoint pts[4], SkScalar d2, SkScalar d3, SkMatrix* klm) { |
| 759 | SkMatrix CIT; |
| 760 | int skipCol = calc_inverse_transpose_power_basis_matrix(pts, &CIT); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 761 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 762 | const SkScalar tn = d3; |
| 763 | const SkScalar sn = 3 * d2; |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 764 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 765 | SkMatrix klmCoeffs; |
| 766 | int col = 0; |
| 767 | if (0 != skipCol) { |
| 768 | klmCoeffs[0] = 0; |
| 769 | klmCoeffs[3] = -sn * sn * sn; |
| 770 | ++col; |
| 771 | } |
| 772 | if (1 != skipCol) { |
| 773 | klmCoeffs[col + 0] = 0; |
| 774 | klmCoeffs[col + 3] = 3 * sn * sn * tn; |
| 775 | ++col; |
| 776 | } |
| 777 | if (2 != skipCol) { |
| 778 | klmCoeffs[col + 0] = -sn; |
| 779 | klmCoeffs[col + 3] = -3 * sn * tn * tn; |
| 780 | ++col; |
| 781 | } |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 782 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 783 | SkASSERT(2 == col); |
| 784 | klmCoeffs[2] = tn; |
| 785 | klmCoeffs[5] = tn * tn * tn; |
| 786 | |
| 787 | klmCoeffs[6] = 0; |
| 788 | klmCoeffs[7] = 0; |
| 789 | klmCoeffs[8] = 1; |
| 790 | |
| 791 | klm->setConcat(klmCoeffs, CIT); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 792 | } |
| 793 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 794 | // For the case when a cubic bezier is actually a quadratic. We duplicate k in l so that the |
| 795 | // implicit becomes: |
| 796 | // |
| 797 | // k^3 - l*m == k^3 - l*k == k * (k^2 - l) |
| 798 | // |
| 799 | // In the quadratic case we can simply assign fixed values at each control point: |
| 800 | // |
| 801 | // | ..K.. | | pts[0] pts[1] pts[2] pts[3] | | 0 1/3 2/3 1 | |
| 802 | // | ..L.. | * | . . . . | == | 0 0 1/3 1 | |
| 803 | // | ..K.. | | 1 1 1 1 | | 0 1/3 2/3 1 | |
| 804 | // |
| 805 | static void calc_quadratic_klm(const SkPoint pts[4], SkScalar d3, SkMatrix* klm) { |
| 806 | SkMatrix klmAtPts; |
| 807 | klmAtPts.setAll(0, 1.f/3, 1, |
| 808 | 0, 0, 1, |
| 809 | 0, 1.f/3, 1); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 810 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 811 | SkMatrix inversePts; |
| 812 | inversePts.setAll(pts[0].x(), pts[1].x(), pts[3].x(), |
| 813 | pts[0].y(), pts[1].y(), pts[3].y(), |
| 814 | 1, 1, 1); |
| 815 | SkAssertResult(inversePts.invert(&inversePts)); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 816 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 817 | klm->setConcat(klmAtPts, inversePts); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 818 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 819 | // If d3 > 0 we need to flip the orientation of our curve |
| 820 | // This is done by negating the k and l values |
| 821 | if (d3 > 0) { |
| 822 | negate_kl(klm); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 826 | // For the case when a cubic bezier is actually a line. We set K=0, L=1, M=-line, which results in |
| 827 | // the following implicit: |
| 828 | // |
| 829 | // k^3 - l*m == 0^3 - 1*(-line) == -(-line) == line |
| 830 | // |
| 831 | static void calc_line_klm(const SkPoint pts[4], SkMatrix* klm) { |
| 832 | SkScalar ny = pts[0].x() - pts[3].x(); |
| 833 | SkScalar nx = pts[3].y() - pts[0].y(); |
| 834 | SkScalar k = nx * pts[0].x() + ny * pts[0].y(); |
| 835 | klm->setAll( 0, 0, 0, |
| 836 | 0, 0, 1, |
| 837 | -nx, -ny, k); |
| 838 | } |
| 839 | |
| 840 | int GrPathUtils::chopCubicAtLoopIntersection(const SkPoint src[4], SkPoint dst[10], SkMatrix* klm, |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 841 | int* loopIndex) { |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 842 | // Variables to store the two parametric values at the loop double point. |
| 843 | SkScalar t1 = 0, t2 = 0; |
| 844 | |
| 845 | // Homogeneous parametric values at the loop double point. |
| 846 | SkScalar td, sd, te, se; |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 847 | |
Chris Dalton | 4343654 | 2017-04-13 14:26:00 -0600 | [diff] [blame^] | 848 | SkScalar d[4]; |
caryclark | 8dd31cf | 2014-12-12 09:11:23 -0800 | [diff] [blame] | 849 | SkCubicType cType = SkClassifyCubic(src, d); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 850 | |
| 851 | int chop_count = 0; |
Chris Dalton | 4343654 | 2017-04-13 14:26:00 -0600 | [diff] [blame^] | 852 | if (SkCubicType::kLoop == cType) { |
| 853 | SkASSERT(d[0] < 0); |
| 854 | const SkScalar tempSqrt = SkScalarSqrt(-d[0]); |
| 855 | td = d[2] + tempSqrt; |
| 856 | sd = 2.f * d[1]; |
| 857 | te = d[2] - tempSqrt; |
| 858 | se = 2.f * d[1]; |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 859 | |
| 860 | t1 = td / sd; |
| 861 | t2 = te / se; |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 862 | // need to have t values sorted since this is what is expected by SkChopCubicAt |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 863 | if (t1 > t2) { |
| 864 | SkTSwap(t1, t2); |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | SkScalar chop_ts[2]; |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 868 | if (t1 > 0.f && t1 < 1.f) { |
| 869 | chop_ts[chop_count++] = t1; |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 870 | } |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 871 | if (t2 > 0.f && t2 < 1.f) { |
| 872 | chop_ts[chop_count++] = t2; |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 873 | } |
| 874 | if(dst) { |
| 875 | SkChopCubicAt(src, dst, chop_ts, chop_count); |
| 876 | } |
| 877 | } else { |
| 878 | if (dst) { |
| 879 | memcpy(dst, src, sizeof(SkPoint) * 4); |
| 880 | } |
| 881 | } |
| 882 | |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 883 | if (loopIndex) { |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 884 | if (2 == chop_count) { |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 885 | *loopIndex = 1; |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 886 | } else if (1 == chop_count) { |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 887 | if (t1 < 0.f) { |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 888 | *loopIndex = 0; |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 889 | } else { |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 890 | *loopIndex = 1; |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 891 | } |
| 892 | } else { |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 893 | if (t1 < 0.f && t2 > 1.f) { |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 894 | *loopIndex = 0; |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 895 | } else { |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 896 | *loopIndex = -1; |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 899 | } |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 900 | |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 901 | if (klm) { |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 902 | switch (cType) { |
Chris Dalton | 4343654 | 2017-04-13 14:26:00 -0600 | [diff] [blame^] | 903 | case SkCubicType::kSerpentine: |
| 904 | case SkCubicType::kLocalCusp: |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 905 | calc_serp_klm(src, d, klm); |
| 906 | break; |
Chris Dalton | 4343654 | 2017-04-13 14:26:00 -0600 | [diff] [blame^] | 907 | case SkCubicType::kLoop: |
| 908 | calc_loop_klm(src, d[1], td, sd, te, se, klm); |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 909 | break; |
Chris Dalton | 4343654 | 2017-04-13 14:26:00 -0600 | [diff] [blame^] | 910 | case SkCubicType::kInfiniteCusp: |
| 911 | calc_inf_cusp_klm(src, d[2], d[3], klm); |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 912 | break; |
Chris Dalton | 4343654 | 2017-04-13 14:26:00 -0600 | [diff] [blame^] | 913 | case SkCubicType::kQuadratic: |
| 914 | calc_quadratic_klm(src, d[3], klm); |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 915 | break; |
Chris Dalton | 4343654 | 2017-04-13 14:26:00 -0600 | [diff] [blame^] | 916 | case SkCubicType::kLineOrPoint: |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 917 | calc_line_klm(src, klm); |
| 918 | break; |
| 919 | }; |
commit-bot@chromium.org | 858638d | 2013-08-20 14:45:45 +0000 | [diff] [blame] | 920 | } |
| 921 | return chop_count + 1; |
| 922 | } |