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 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 10 | #include "GrPoint.h" |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 11 | #include "SkGeometry.h" |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 12 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 13 | SkScalar GrPathUtils::scaleToleranceToSrc(SkScalar devTol, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 14 | const SkMatrix& viewM, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 15 | const SkRect& pathBounds) { |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 16 | // In order to tesselate the path we get a bound on how much the matrix can |
| 17 | // stretch when mapping to screen coordinates. |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 18 | SkScalar stretch = viewM.getMaxStretch(); |
| 19 | SkScalar srcTol = devTol; |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 20 | |
| 21 | if (stretch < 0) { |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 22 | // take worst case mapRadius amoung four corners. |
| 23 | // (less than perfect) |
| 24 | for (int i = 0; i < 4; ++i) { |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 25 | SkMatrix mat; |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 26 | mat.setTranslate((i % 2) ? pathBounds.fLeft : pathBounds.fRight, |
| 27 | (i < 2) ? pathBounds.fTop : pathBounds.fBottom); |
| 28 | mat.postConcat(viewM); |
| 29 | stretch = SkMaxScalar(stretch, mat.mapRadius(SK_Scalar1)); |
| 30 | } |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 31 | } |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 32 | srcTol = SkScalarDiv(srcTol, stretch); |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 33 | return srcTol; |
| 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; |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 37 | static const SkScalar gMinCurveTol = SkFloatToScalar(0.0001f); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 38 | |
| 39 | uint32_t GrPathUtils::quadraticPointCount(const GrPoint 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 | } |
| 44 | GrAssert(tol > 0); |
| 45 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 46 | SkScalar d = points[1].distanceToLineSegmentBetween(points[0], points[2]); |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 47 | if (d <= tol) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 48 | return 1; |
| 49 | } else { |
| 50 | // Each time we subdivide, d should be cut in 4. So we need to |
| 51 | // subdivide x = log4(d/tol) times. x subdivisions creates 2^(x) |
| 52 | // points. |
| 53 | // 2^(log4(x)) = sqrt(x); |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 54 | int temp = SkScalarCeil(SkScalarSqrt(SkScalarDiv(d, tol))); |
bsalomon@google.com | 61f3bde | 2011-06-17 20:06:49 +0000 | [diff] [blame] | 55 | int pow2 = GrNextPow2(temp); |
| 56 | // Because of NaNs & INFs we can wind up with a degenerate temp |
| 57 | // such that pow2 comes out negative. Also, our point generator |
| 58 | // will always output at least one pt. |
| 59 | if (pow2 < 1) { |
| 60 | pow2 = 1; |
| 61 | } |
| 62 | return GrMin(pow2, MAX_POINTS_PER_CURVE); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | uint32_t GrPathUtils::generateQuadraticPoints(const GrPoint& p0, |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 67 | const GrPoint& p1, |
| 68 | const GrPoint& p2, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 69 | SkScalar tolSqd, |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 70 | GrPoint** points, |
| 71 | uint32_t pointsLeft) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 72 | if (pointsLeft < 2 || |
| 73 | (p1.distanceToLineSegmentBetweenSqd(p0, p2)) < tolSqd) { |
| 74 | (*points)[0] = p2; |
| 75 | *points += 1; |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | GrPoint q[] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 80 | { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, |
| 81 | { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 82 | }; |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 83 | GrPoint 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] | 84 | |
| 85 | pointsLeft >>= 1; |
| 86 | uint32_t a = generateQuadraticPoints(p0, q[0], r, tolSqd, points, pointsLeft); |
| 87 | uint32_t b = generateQuadraticPoints(r, q[1], p2, tolSqd, points, pointsLeft); |
| 88 | return a + b; |
| 89 | } |
| 90 | |
| 91 | uint32_t GrPathUtils::cubicPointCount(const GrPoint points[], |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 92 | SkScalar tol) { |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 93 | if (tol < gMinCurveTol) { |
tomhudson@google.com | afec7ba | 2011-06-30 14:47:55 +0000 | [diff] [blame] | 94 | tol = gMinCurveTol; |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 95 | } |
| 96 | GrAssert(tol > 0); |
| 97 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 98 | SkScalar d = GrMax( |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 99 | points[1].distanceToLineSegmentBetweenSqd(points[0], points[3]), |
| 100 | points[2].distanceToLineSegmentBetweenSqd(points[0], points[3])); |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 101 | d = SkScalarSqrt(d); |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 102 | if (d <= tol) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 103 | return 1; |
| 104 | } else { |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 105 | int temp = SkScalarCeil(SkScalarSqrt(SkScalarDiv(d, tol))); |
bsalomon@google.com | 61f3bde | 2011-06-17 20:06:49 +0000 | [diff] [blame] | 106 | int pow2 = GrNextPow2(temp); |
| 107 | // Because of NaNs & INFs we can wind up with a degenerate temp |
| 108 | // such that pow2 comes out negative. Also, our point generator |
| 109 | // will always output at least one pt. |
| 110 | if (pow2 < 1) { |
| 111 | pow2 = 1; |
| 112 | } |
| 113 | return GrMin(pow2, MAX_POINTS_PER_CURVE); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | uint32_t GrPathUtils::generateCubicPoints(const GrPoint& p0, |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 118 | const GrPoint& p1, |
| 119 | const GrPoint& p2, |
| 120 | const GrPoint& p3, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 121 | SkScalar tolSqd, |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 122 | GrPoint** points, |
| 123 | uint32_t pointsLeft) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 124 | if (pointsLeft < 2 || |
| 125 | (p1.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd && |
| 126 | p2.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd)) { |
| 127 | (*points)[0] = p3; |
| 128 | *points += 1; |
| 129 | return 1; |
| 130 | } |
| 131 | GrPoint q[] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 132 | { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, |
| 133 | { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, |
| 134 | { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) } |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 135 | }; |
| 136 | GrPoint r[] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 137 | { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }, |
| 138 | { 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] | 139 | }; |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 140 | GrPoint 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] | 141 | pointsLeft >>= 1; |
| 142 | uint32_t a = generateCubicPoints(p0, q[0], r[0], s, tolSqd, points, pointsLeft); |
| 143 | uint32_t b = generateCubicPoints(s, r[1], q[2], p3, tolSqd, points, pointsLeft); |
| 144 | return a + b; |
| 145 | } |
| 146 | |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 147 | int GrPathUtils::worstCasePointCount(const SkPath& path, int* subpaths, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 148 | SkScalar tol) { |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 149 | if (tol < gMinCurveTol) { |
tomhudson@google.com | afec7ba | 2011-06-30 14:47:55 +0000 | [diff] [blame] | 150 | tol = gMinCurveTol; |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 151 | } |
| 152 | GrAssert(tol > 0); |
| 153 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 154 | int pointCount = 0; |
| 155 | *subpaths = 1; |
| 156 | |
| 157 | bool first = true; |
| 158 | |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 159 | SkPath::Iter iter(path, false); |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 160 | SkPath::Verb verb; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 161 | |
| 162 | GrPoint pts[4]; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 163 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 164 | |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 165 | switch (verb) { |
| 166 | case SkPath::kLine_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 167 | pointCount += 1; |
| 168 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 169 | case SkPath::kQuad_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 170 | pointCount += quadraticPointCount(pts, tol); |
| 171 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 172 | case SkPath::kCubic_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 173 | pointCount += cubicPointCount(pts, tol); |
| 174 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 175 | case SkPath::kMove_Verb: |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 176 | pointCount += 1; |
| 177 | if (!first) { |
| 178 | ++(*subpaths); |
| 179 | } |
| 180 | break; |
| 181 | default: |
| 182 | break; |
| 183 | } |
| 184 | first = false; |
| 185 | } |
| 186 | return pointCount; |
| 187 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 188 | |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 189 | void GrPathUtils::QuadUVMatrix::set(const GrPoint qPts[3]) { |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 190 | // can't make this static, no cons :( |
| 191 | SkMatrix UVpts; |
bsalomon@google.com | 5e9bf82 | 2012-01-17 14:39:21 +0000 | [diff] [blame] | 192 | #ifndef SK_SCALAR_IS_FLOAT |
| 193 | GrCrash("Expected scalar is float."); |
| 194 | #endif |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 195 | SkMatrix m; |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 196 | // We want M such that M * xy_pt = uv_pt |
| 197 | // We know M * control_pts = [0 1/2 1] |
| 198 | // [0 0 1] |
| 199 | // [1 1 1] |
| 200 | // We invert the control pt matrix and post concat to both sides to get M. |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 201 | UVpts.setAll(0, SK_ScalarHalf, SK_Scalar1, |
| 202 | 0, 0, SK_Scalar1, |
| 203 | SkScalarToPersp(SK_Scalar1), |
| 204 | SkScalarToPersp(SK_Scalar1), |
| 205 | SkScalarToPersp(SK_Scalar1)); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 206 | m.setAll(qPts[0].fX, qPts[1].fX, qPts[2].fX, |
| 207 | qPts[0].fY, qPts[1].fY, qPts[2].fY, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 208 | SkScalarToPersp(SK_Scalar1), |
| 209 | SkScalarToPersp(SK_Scalar1), |
| 210 | SkScalarToPersp(SK_Scalar1)); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 211 | if (!m.invert(&m)) { |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 212 | // The quad is degenerate. Hopefully this is rare. Find the pts that are |
| 213 | // farthest apart to compute a line (unless it is really a pt). |
| 214 | SkScalar maxD = qPts[0].distanceToSqd(qPts[1]); |
| 215 | int maxEdge = 0; |
| 216 | SkScalar d = qPts[1].distanceToSqd(qPts[2]); |
| 217 | if (d > maxD) { |
| 218 | maxD = d; |
| 219 | maxEdge = 1; |
| 220 | } |
| 221 | d = qPts[2].distanceToSqd(qPts[0]); |
| 222 | if (d > maxD) { |
| 223 | maxD = d; |
| 224 | maxEdge = 2; |
| 225 | } |
| 226 | // We could have a tolerance here, not sure if it would improve anything |
| 227 | if (maxD > 0) { |
| 228 | // Set the matrix to give (u = 0, v = distance_to_line) |
bsalomon@google.com | 20e542e | 2012-02-15 18:49:41 +0000 | [diff] [blame] | 229 | GrVec lineVec = qPts[(maxEdge + 1)%3] - qPts[maxEdge]; |
| 230 | // when looking from the point 0 down the line we want positive |
| 231 | // distances to be to the left. This matches the non-degenerate |
| 232 | // case. |
| 233 | lineVec.setOrthog(lineVec, GrPoint::kLeft_Side); |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 234 | lineVec.dot(qPts[0]); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 235 | // first row |
| 236 | fM[0] = 0; |
| 237 | fM[1] = 0; |
| 238 | fM[2] = 0; |
| 239 | // second row |
| 240 | fM[3] = lineVec.fX; |
| 241 | fM[4] = lineVec.fY; |
| 242 | fM[5] = -lineVec.dot(qPts[maxEdge]); |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 243 | } else { |
| 244 | // It's a point. It should cover zero area. Just set the matrix such |
| 245 | // that (u, v) will always be far away from the quad. |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 246 | fM[0] = 0; fM[1] = 0; fM[2] = 100.f; |
| 247 | fM[3] = 0; fM[4] = 0; fM[5] = 100.f; |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 248 | } |
| 249 | } else { |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 250 | m.postConcat(UVpts); |
| 251 | |
| 252 | // The matrix should not have perspective. |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 253 | SkDEBUGCODE(static const SkScalar gTOL = SkFloatToScalar(1.f / 100.f)); |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 254 | GrAssert(SkScalarAbs(m.get(SkMatrix::kMPersp0)) < gTOL); |
| 255 | GrAssert(SkScalarAbs(m.get(SkMatrix::kMPersp1)) < gTOL); |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 256 | |
| 257 | // It may not be normalized to have 1.0 in the bottom right |
| 258 | float m33 = m.get(SkMatrix::kMPersp2); |
| 259 | if (1.f != m33) { |
| 260 | m33 = 1.f / m33; |
| 261 | fM[0] = m33 * m.get(SkMatrix::kMScaleX); |
| 262 | fM[1] = m33 * m.get(SkMatrix::kMSkewX); |
| 263 | fM[2] = m33 * m.get(SkMatrix::kMTransX); |
| 264 | fM[3] = m33 * m.get(SkMatrix::kMSkewY); |
| 265 | fM[4] = m33 * m.get(SkMatrix::kMScaleY); |
| 266 | fM[5] = m33 * m.get(SkMatrix::kMTransY); |
| 267 | } else { |
| 268 | fM[0] = m.get(SkMatrix::kMScaleX); |
| 269 | fM[1] = m.get(SkMatrix::kMSkewX); |
| 270 | fM[2] = m.get(SkMatrix::kMTransX); |
| 271 | fM[3] = m.get(SkMatrix::kMSkewY); |
| 272 | fM[4] = m.get(SkMatrix::kMScaleY); |
| 273 | fM[5] = m.get(SkMatrix::kMTransY); |
| 274 | } |
bsalomon@google.com | dc3c780 | 2012-01-31 20:46:32 +0000 | [diff] [blame] | 275 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | namespace { |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 279 | |
| 280 | // a is the first control point of the cubic. |
| 281 | // ab is the vector from a to the second control point. |
| 282 | // dc is the vector from the fourth to the third control point. |
| 283 | // d is the fourth control point. |
| 284 | // p is the candidate quadratic control point. |
| 285 | // this assumes that the cubic doesn't inflect and is simple |
| 286 | bool is_point_within_cubic_tangents(const SkPoint& a, |
| 287 | const SkVector& ab, |
| 288 | const SkVector& dc, |
| 289 | const SkPoint& d, |
| 290 | SkPath::Direction dir, |
| 291 | const SkPoint p) { |
| 292 | SkVector ap = p - a; |
| 293 | SkScalar apXab = ap.cross(ab); |
| 294 | if (SkPath::kCW_Direction == dir) { |
| 295 | if (apXab > 0) { |
| 296 | return false; |
| 297 | } |
| 298 | } else { |
| 299 | GrAssert(SkPath::kCCW_Direction == dir); |
| 300 | if (apXab < 0) { |
| 301 | return false; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | SkVector dp = p - d; |
| 306 | SkScalar dpXdc = dp.cross(dc); |
| 307 | if (SkPath::kCW_Direction == dir) { |
| 308 | if (dpXdc < 0) { |
| 309 | return false; |
| 310 | } |
| 311 | } else { |
| 312 | GrAssert(SkPath::kCCW_Direction == dir); |
| 313 | if (dpXdc > 0) { |
| 314 | return false; |
| 315 | } |
| 316 | } |
| 317 | return true; |
| 318 | } |
| 319 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 320 | void convert_noninflect_cubic_to_quads(const SkPoint p[4], |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 321 | SkScalar toleranceSqd, |
| 322 | bool constrainWithinTangents, |
| 323 | SkPath::Direction dir, |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 324 | SkTArray<SkPoint, true>* quads, |
| 325 | int sublevel = 0) { |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 326 | |
| 327 | // Notation: Point a is always p[0]. Point b is p[1] unless p[1] == p[0], in which case it is |
| 328 | // 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]. |
| 329 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 330 | SkVector ab = p[1] - p[0]; |
| 331 | SkVector dc = p[2] - p[3]; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 332 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 333 | if (ab.isZero()) { |
| 334 | if (dc.isZero()) { |
| 335 | SkPoint* degQuad = quads->push_back_n(3); |
| 336 | degQuad[0] = p[0]; |
| 337 | degQuad[1] = p[0]; |
| 338 | degQuad[2] = p[3]; |
| 339 | return; |
| 340 | } |
| 341 | ab = p[2] - p[0]; |
| 342 | } |
| 343 | if (dc.isZero()) { |
| 344 | dc = p[1] - p[3]; |
| 345 | } |
| 346 | |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 347 | // When the ab and cd tangents are nearly parallel with vector from d to a the constraint that |
| 348 | // the quad point falls between the tangents becomes hard to enforce and we are likely to hit |
| 349 | // the max subdivision count. However, in this case the cubic is approaching a line and the |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 350 | // accuracy of the quad point isn't so important. We check if the two middle cubic control |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 351 | // points are very close to the baseline vector. If so then we just pick quadratic points on the |
| 352 | // control polygon. |
| 353 | |
| 354 | if (constrainWithinTangents) { |
| 355 | SkVector da = p[0] - p[3]; |
| 356 | SkScalar invDALengthSqd = da.lengthSqd(); |
| 357 | if (invDALengthSqd > SK_ScalarNearlyZero) { |
| 358 | invDALengthSqd = SkScalarInvert(invDALengthSqd); |
| 359 | // cross(ab, da)^2/length(da)^2 == sqd distance from b to line from d to a. |
| 360 | // same goed for point c using vector cd. |
| 361 | SkScalar detABSqd = ab.cross(da); |
| 362 | detABSqd = SkScalarSquare(detABSqd); |
| 363 | SkScalar detDCSqd = dc.cross(da); |
| 364 | detDCSqd = SkScalarSquare(detDCSqd); |
| 365 | if (SkScalarMul(detABSqd, invDALengthSqd) < toleranceSqd && |
| 366 | SkScalarMul(detDCSqd, invDALengthSqd) < toleranceSqd) { |
| 367 | SkPoint b = p[0] + ab; |
| 368 | SkPoint c = p[3] + dc; |
| 369 | SkPoint mid = b + c; |
| 370 | mid.scale(SK_ScalarHalf); |
| 371 | // Insert two quadratics to cover the case when ab points away from d and/or dc |
| 372 | // points away from a. |
| 373 | if (SkVector::DotProduct(da, dc) < 0 || SkVector::DotProduct(ab,da) > 0) { |
| 374 | SkPoint* qpts = quads->push_back_n(6); |
| 375 | qpts[0] = p[0]; |
| 376 | qpts[1] = b; |
| 377 | qpts[2] = mid; |
| 378 | qpts[3] = mid; |
| 379 | qpts[4] = c; |
| 380 | qpts[5] = p[3]; |
| 381 | } else { |
| 382 | SkPoint* qpts = quads->push_back_n(3); |
| 383 | qpts[0] = p[0]; |
| 384 | qpts[1] = mid; |
| 385 | qpts[2] = p[3]; |
| 386 | } |
| 387 | return; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 392 | static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 393 | static const int kMaxSubdivs = 10; |
| 394 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 395 | ab.scale(kLengthScale); |
| 396 | dc.scale(kLengthScale); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 397 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 398 | // e0 and e1 are extrapolations along vectors ab and dc. |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 399 | SkVector c0 = p[0]; |
| 400 | c0 += ab; |
| 401 | SkVector c1 = p[3]; |
| 402 | c1 += dc; |
| 403 | |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 404 | SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : c0.distanceToSqd(c1); |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 405 | if (dSqd < toleranceSqd) { |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 406 | SkPoint cAvg = c0; |
| 407 | cAvg += c1; |
| 408 | cAvg.scale(SK_ScalarHalf); |
| 409 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 410 | bool subdivide = false; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 411 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 412 | if (constrainWithinTangents && |
| 413 | !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] | 414 | // 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] | 415 | ab.setOrthog(ab); |
| 416 | SkScalar z0 = -ab.dot(p[0]); |
| 417 | dc.setOrthog(dc); |
| 418 | SkScalar z1 = -dc.dot(p[3]); |
| 419 | cAvg.fX = SkScalarMul(ab.fY, z1) - SkScalarMul(z0, dc.fY); |
| 420 | cAvg.fY = SkScalarMul(z0, dc.fX) - SkScalarMul(ab.fX, z1); |
| 421 | SkScalar z = SkScalarMul(ab.fX, dc.fY) - SkScalarMul(ab.fY, dc.fX); |
| 422 | z = SkScalarInvert(z); |
| 423 | cAvg.fX *= z; |
| 424 | cAvg.fY *= z; |
| 425 | if (sublevel <= kMaxSubdivs) { |
| 426 | SkScalar d0Sqd = c0.distanceToSqd(cAvg); |
| 427 | SkScalar d1Sqd = c1.distanceToSqd(cAvg); |
bsalomon@google.com | 54ad851 | 2012-08-02 14:55:45 +0000 | [diff] [blame] | 428 | // We need to subdivide if d0 + d1 > tolerance but we have the sqd values. We know |
| 429 | // the distances and tolerance can't be negative. |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 430 | // (d0 + d1)^2 > toleranceSqd |
| 431 | // d0Sqd + 2*d0*d1 + d1Sqd > toleranceSqd |
| 432 | SkScalar d0d1 = SkScalarSqrt(SkScalarMul(d0Sqd, d1Sqd)); |
| 433 | subdivide = 2 * d0d1 + d0Sqd + d1Sqd > toleranceSqd; |
| 434 | } |
| 435 | } |
| 436 | if (!subdivide) { |
| 437 | SkPoint* pts = quads->push_back_n(3); |
| 438 | pts[0] = p[0]; |
| 439 | pts[1] = cAvg; |
| 440 | pts[2] = p[3]; |
| 441 | return; |
| 442 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 443 | } |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 444 | SkPoint choppedPts[7]; |
| 445 | SkChopCubicAtHalf(p, choppedPts); |
| 446 | convert_noninflect_cubic_to_quads(choppedPts + 0, |
| 447 | toleranceSqd, |
| 448 | constrainWithinTangents, |
| 449 | dir, |
| 450 | quads, |
| 451 | sublevel + 1); |
| 452 | convert_noninflect_cubic_to_quads(choppedPts + 3, |
| 453 | toleranceSqd, |
| 454 | constrainWithinTangents, |
| 455 | dir, |
| 456 | quads, |
| 457 | sublevel + 1); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | void GrPathUtils::convertCubicToQuads(const GrPoint p[4], |
| 462 | SkScalar tolScale, |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 463 | bool constrainWithinTangents, |
| 464 | SkPath::Direction dir, |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 465 | SkTArray<SkPoint, true>* quads) { |
| 466 | SkPoint chopped[10]; |
| 467 | int count = SkChopCubicAtInflections(p, chopped); |
| 468 | |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 469 | // base tolerance is 1 pixel. |
| 470 | static const SkScalar kTolerance = SK_Scalar1; |
| 471 | const SkScalar tolSqd = SkScalarSquare(SkScalarMul(tolScale, kTolerance)); |
| 472 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 473 | for (int i = 0; i < count; ++i) { |
| 474 | SkPoint* cubic = chopped + 3*i; |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 475 | convert_noninflect_cubic_to_quads(cubic, tolSqd, constrainWithinTangents, dir, quads); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | } |