epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 10 | #include "GrPathUtils.h" |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 11 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 12 | #include "GrPoint.h" |
| 13 | |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 14 | GrScalar GrPathUtils::scaleToleranceToSrc(GrScalar devTol, |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 15 | const GrMatrix& viewM, |
| 16 | const GrRect& 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 |
| 18 | // stretch when mapping to screen coordinates. |
| 19 | GrScalar stretch = viewM.getMaxStretch(); |
| 20 | GrScalar srcTol = devTol; |
| 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) { |
| 26 | GrMatrix mat; |
| 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 | } |
| 33 | srcTol = GrScalarDiv(srcTol, stretch); |
| 34 | return srcTol; |
| 35 | } |
| 36 | |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 37 | static const int MAX_POINTS_PER_CURVE = 1 << 10; |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 38 | static const GrScalar gMinCurveTol = GrFloatToScalar(0.0001f); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 39 | |
| 40 | uint32_t GrPathUtils::quadraticPointCount(const GrPoint points[], |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 41 | GrScalar tol) { |
| 42 | if (tol < gMinCurveTol) { |
tomhudson@google.com | afec7ba | 2011-06-30 14:47:55 +0000 | [diff] [blame] | 43 | tol = gMinCurveTol; |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 44 | } |
| 45 | GrAssert(tol > 0); |
| 46 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 47 | GrScalar d = points[1].distanceToLineSegmentBetween(points[0], points[2]); |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 48 | 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); |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 55 | int temp = SkScalarCeil(SkScalarSqrt(SkScalarDiv(d, tol))); |
bsalomon@google.com | 61f3bde | 2011-06-17 20:06:49 +0000 | [diff] [blame] | 56 | int pow2 = GrNextPow2(temp); |
| 57 | // Because of NaNs & INFs we can wind up with a degenerate temp |
| 58 | // such that pow2 comes out negative. Also, our point generator |
| 59 | // will always output at least one pt. |
| 60 | if (pow2 < 1) { |
| 61 | pow2 = 1; |
| 62 | } |
| 63 | return GrMin(pow2, MAX_POINTS_PER_CURVE); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | uint32_t GrPathUtils::generateQuadraticPoints(const GrPoint& p0, |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 68 | const GrPoint& p1, |
| 69 | const GrPoint& p2, |
| 70 | GrScalar tolSqd, |
| 71 | GrPoint** points, |
| 72 | uint32_t pointsLeft) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 73 | if (pointsLeft < 2 || |
| 74 | (p1.distanceToLineSegmentBetweenSqd(p0, p2)) < tolSqd) { |
| 75 | (*points)[0] = p2; |
| 76 | *points += 1; |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | GrPoint q[] = { |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 81 | { GrScalarAve(p0.fX, p1.fX), GrScalarAve(p0.fY, p1.fY) }, |
| 82 | { GrScalarAve(p1.fX, p2.fX), GrScalarAve(p1.fY, p2.fY) }, |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 83 | }; |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 84 | GrPoint r = { GrScalarAve(q[0].fX, q[1].fX), GrScalarAve(q[0].fY, q[1].fY) }; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 85 | |
| 86 | pointsLeft >>= 1; |
| 87 | uint32_t a = generateQuadraticPoints(p0, q[0], r, tolSqd, points, pointsLeft); |
| 88 | uint32_t b = generateQuadraticPoints(r, q[1], p2, tolSqd, points, pointsLeft); |
| 89 | return a + b; |
| 90 | } |
| 91 | |
| 92 | uint32_t GrPathUtils::cubicPointCount(const GrPoint points[], |
| 93 | GrScalar tol) { |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 94 | if (tol < gMinCurveTol) { |
tomhudson@google.com | afec7ba | 2011-06-30 14:47:55 +0000 | [diff] [blame] | 95 | tol = gMinCurveTol; |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 96 | } |
| 97 | GrAssert(tol > 0); |
| 98 | |
| 99 | GrScalar d = GrMax( |
| 100 | points[1].distanceToLineSegmentBetweenSqd(points[0], points[3]), |
| 101 | points[2].distanceToLineSegmentBetweenSqd(points[0], points[3])); |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 102 | d = SkScalarSqrt(d); |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 103 | if (d <= tol) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 104 | return 1; |
| 105 | } else { |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 106 | int temp = SkScalarCeil(SkScalarSqrt(SkScalarDiv(d, tol))); |
bsalomon@google.com | 61f3bde | 2011-06-17 20:06:49 +0000 | [diff] [blame] | 107 | int pow2 = GrNextPow2(temp); |
| 108 | // Because of NaNs & INFs we can wind up with a degenerate temp |
| 109 | // such that pow2 comes out negative. Also, our point generator |
| 110 | // will always output at least one pt. |
| 111 | if (pow2 < 1) { |
| 112 | pow2 = 1; |
| 113 | } |
| 114 | return GrMin(pow2, MAX_POINTS_PER_CURVE); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | uint32_t GrPathUtils::generateCubicPoints(const GrPoint& p0, |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 119 | const GrPoint& p1, |
| 120 | const GrPoint& p2, |
| 121 | const GrPoint& p3, |
| 122 | GrScalar tolSqd, |
| 123 | GrPoint** points, |
| 124 | uint32_t pointsLeft) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 125 | if (pointsLeft < 2 || |
| 126 | (p1.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd && |
| 127 | p2.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd)) { |
| 128 | (*points)[0] = p3; |
| 129 | *points += 1; |
| 130 | return 1; |
| 131 | } |
| 132 | GrPoint q[] = { |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 133 | { GrScalarAve(p0.fX, p1.fX), GrScalarAve(p0.fY, p1.fY) }, |
| 134 | { GrScalarAve(p1.fX, p2.fX), GrScalarAve(p1.fY, p2.fY) }, |
| 135 | { GrScalarAve(p2.fX, p3.fX), GrScalarAve(p2.fY, p3.fY) } |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 136 | }; |
| 137 | GrPoint r[] = { |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 138 | { GrScalarAve(q[0].fX, q[1].fX), GrScalarAve(q[0].fY, q[1].fY) }, |
| 139 | { GrScalarAve(q[1].fX, q[2].fX), GrScalarAve(q[1].fY, q[2].fY) } |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 140 | }; |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 141 | GrPoint s = { GrScalarAve(r[0].fX, r[1].fX), GrScalarAve(r[0].fY, r[1].fY) }; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 142 | pointsLeft >>= 1; |
| 143 | uint32_t a = generateCubicPoints(p0, q[0], r[0], s, tolSqd, points, pointsLeft); |
| 144 | uint32_t b = generateCubicPoints(s, r[1], q[2], p3, tolSqd, points, pointsLeft); |
| 145 | return a + b; |
| 146 | } |
| 147 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 148 | int GrPathUtils::worstCasePointCount(const GrPath& path, int* subpaths, |
| 149 | GrScalar tol) { |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 150 | if (tol < gMinCurveTol) { |
tomhudson@google.com | afec7ba | 2011-06-30 14:47:55 +0000 | [diff] [blame] | 151 | tol = gMinCurveTol; |
tomhudson@google.com | c10a888 | 2011-06-28 15:19:32 +0000 | [diff] [blame] | 152 | } |
| 153 | GrAssert(tol > 0); |
| 154 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 155 | int pointCount = 0; |
| 156 | *subpaths = 1; |
| 157 | |
| 158 | bool first = true; |
| 159 | |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 160 | SkPath::Iter iter(path, false); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 161 | GrPathCmd cmd; |
| 162 | |
| 163 | GrPoint pts[4]; |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 164 | while ((cmd = (GrPathCmd)iter.next(pts)) != kEnd_PathCmd) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 165 | |
| 166 | switch (cmd) { |
| 167 | case kLine_PathCmd: |
| 168 | pointCount += 1; |
| 169 | break; |
| 170 | case kQuadratic_PathCmd: |
| 171 | pointCount += quadraticPointCount(pts, tol); |
| 172 | break; |
| 173 | case kCubic_PathCmd: |
| 174 | pointCount += cubicPointCount(pts, tol); |
| 175 | break; |
| 176 | case kMove_PathCmd: |
| 177 | pointCount += 1; |
| 178 | if (!first) { |
| 179 | ++(*subpaths); |
| 180 | } |
| 181 | break; |
| 182 | default: |
| 183 | break; |
| 184 | } |
| 185 | first = false; |
| 186 | } |
| 187 | return pointCount; |
| 188 | } |