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