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" |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 12 | #include "SkGeometry.h" |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 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 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 189 | |
| 190 | namespace { |
| 191 | // The matrix computed for quadDesignSpaceToUVCoordsMatrix should never really |
| 192 | // have perspective and we really want to avoid perspective matrix muls. |
| 193 | // However, the first two entries of the perspective row may be really close to |
| 194 | // 0 and the third may not be 1 due to a scale on the entire matrix. |
| 195 | inline void fixup_matrix(GrMatrix* mat) { |
bsalomon@google.com | 5ce2796 | 2012-01-17 14:31:23 +0000 | [diff] [blame^] | 196 | GrAssert(SK_SCALAR_IS_FLOAT); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 197 | static const GrScalar gTOL = 1.f / 100.f; |
| 198 | GrAssert(GrScalarAbs(mat->get(SkMatrix::kMPersp0)) < gTOL); |
| 199 | GrAssert(GrScalarAbs(mat->get(SkMatrix::kMPersp1)) < gTOL); |
| 200 | float m33 = mat->get(SkMatrix::kMPersp2); |
| 201 | if (1.f != m33) { |
| 202 | m33 = 1.f / m33; |
| 203 | mat->setAll(m33 * mat->get(SkMatrix::kMScaleX), |
| 204 | m33 * mat->get(SkMatrix::kMSkewX), |
| 205 | m33 * mat->get(SkMatrix::kMTransX), |
| 206 | m33 * mat->get(SkMatrix::kMSkewY), |
| 207 | m33 * mat->get(SkMatrix::kMScaleY), |
| 208 | m33 * mat->get(SkMatrix::kMTransY), |
| 209 | 0.f, 0.f, 1.f); |
| 210 | } else { |
| 211 | mat->setPerspX(0); |
| 212 | mat->setPerspY(0); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Compute a matrix that goes from the 2d space coordinates to UV space where |
| 218 | // u^2-v = 0 specifies the quad. |
| 219 | void GrPathUtils::quadDesignSpaceToUVCoordsMatrix(const SkPoint qPts[3], |
| 220 | GrMatrix* matrix) { |
| 221 | // can't make this static, no cons :( |
| 222 | SkMatrix UVpts; |
bsalomon@google.com | 5ce2796 | 2012-01-17 14:31:23 +0000 | [diff] [blame^] | 223 | GrAssert(SK_SCALAR_IS_FLOAT); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 224 | UVpts.setAll(0, 0.5f, 1.f, |
| 225 | 0, 0, 1.f, |
| 226 | 1.f, 1.f, 1.f); |
| 227 | matrix->setAll(qPts[0].fX, qPts[1].fX, qPts[2].fX, |
| 228 | qPts[0].fY, qPts[1].fY, qPts[2].fY, |
| 229 | 1.f, 1.f, 1.f); |
| 230 | matrix->invert(matrix); |
| 231 | matrix->postConcat(UVpts); |
| 232 | fixup_matrix(matrix); |
| 233 | } |
| 234 | |
| 235 | namespace { |
| 236 | void convert_noninflect_cubic_to_quads(const SkPoint p[4], |
| 237 | SkScalar tolScale, |
| 238 | SkTArray<SkPoint, true>* quads, |
| 239 | int sublevel = 0) { |
| 240 | SkVector ab = p[1]; |
| 241 | ab -= p[0]; |
| 242 | SkVector dc = p[2]; |
| 243 | dc -= p[3]; |
| 244 | |
| 245 | static const SkScalar gLengthScale = 3 * SK_Scalar1 / 2; |
| 246 | // base tolerance is 2 pixels in dev coords. |
| 247 | const SkScalar distanceSqdTol = SkScalarMul(tolScale, 1 * SK_Scalar1); |
| 248 | static const int kMaxSubdivs = 10; |
| 249 | |
| 250 | ab.scale(gLengthScale); |
| 251 | dc.scale(gLengthScale); |
| 252 | |
| 253 | SkVector c0 = p[0]; |
| 254 | c0 += ab; |
| 255 | SkVector c1 = p[3]; |
| 256 | c1 += dc; |
| 257 | |
| 258 | SkScalar dSqd = c0.distanceToSqd(c1); |
| 259 | if (sublevel > kMaxSubdivs || dSqd <= distanceSqdTol) { |
| 260 | SkPoint cAvg = c0; |
| 261 | cAvg += c1; |
| 262 | cAvg.scale(SK_ScalarHalf); |
| 263 | |
| 264 | SkPoint* pts = quads->push_back_n(3); |
| 265 | pts[0] = p[0]; |
| 266 | pts[1] = cAvg; |
| 267 | pts[2] = p[3]; |
| 268 | |
| 269 | return; |
| 270 | } else { |
| 271 | SkPoint choppedPts[7]; |
| 272 | SkChopCubicAtHalf(p, choppedPts); |
| 273 | convert_noninflect_cubic_to_quads(choppedPts + 0, tolScale, |
| 274 | quads, sublevel + 1); |
| 275 | convert_noninflect_cubic_to_quads(choppedPts + 3, tolScale, |
| 276 | quads, sublevel + 1); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | void GrPathUtils::convertCubicToQuads(const GrPoint p[4], |
| 282 | SkScalar tolScale, |
| 283 | SkTArray<SkPoint, true>* quads) { |
| 284 | SkPoint chopped[10]; |
| 285 | int count = SkChopCubicAtInflections(p, chopped); |
| 286 | |
| 287 | for (int i = 0; i < count; ++i) { |
| 288 | SkPoint* cubic = chopped + 3*i; |
| 289 | convert_noninflect_cubic_to_quads(cubic, tolScale, quads); |
| 290 | } |
| 291 | |
| 292 | } |