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