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