| 1 | /* |
| 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. |
| 6 | */ |
| 7 | |
| 8 | #include "src/gpu/geometry/GrPathUtils.h" |
| 9 | |
| 10 | #include "include/gpu/GrTypes.h" |
| 11 | #include "src/core/SkMathPriv.h" |
| 12 | #include "src/core/SkPointPriv.h" |
| 13 | |
| 14 | static const SkScalar gMinCurveTol = 0.0001f; |
| 15 | |
| 16 | SkScalar GrPathUtils::scaleToleranceToSrc(SkScalar devTol, |
| 17 | const SkMatrix& viewM, |
| 18 | const SkRect& pathBounds) { |
| 19 | // In order to tesselate the path we get a bound on how much the matrix can |
| 20 | // scale when mapping to screen coordinates. |
| 21 | SkScalar stretch = viewM.getMaxScale(); |
| 22 | |
| 23 | if (stretch < 0) { |
| 24 | // take worst case mapRadius amoung four corners. |
| 25 | // (less than perfect) |
| 26 | for (int i = 0; i < 4; ++i) { |
| 27 | SkMatrix mat; |
| 28 | mat.setTranslate((i % 2) ? pathBounds.fLeft : pathBounds.fRight, |
| 29 | (i < 2) ? pathBounds.fTop : pathBounds.fBottom); |
| 30 | mat.postConcat(viewM); |
| 31 | stretch = std::max(stretch, mat.mapRadius(SK_Scalar1)); |
| 32 | } |
| 33 | } |
| 34 | SkScalar srcTol = 0; |
| 35 | if (stretch <= 0) { |
| 36 | // We have degenerate bounds or some degenerate matrix. Thus we set the tolerance to be the |
| 37 | // max of the path pathBounds width and height. |
| 38 | srcTol = std::max(pathBounds.width(), pathBounds.height()); |
| 39 | } else { |
| 40 | srcTol = devTol / stretch; |
| 41 | } |
| 42 | if (srcTol < gMinCurveTol) { |
| 43 | srcTol = gMinCurveTol; |
| 44 | } |
| 45 | return srcTol; |
| 46 | } |
| 47 | |
| 48 | uint32_t GrPathUtils::quadraticPointCount(const SkPoint points[], SkScalar tol) { |
| 49 | // You should have called scaleToleranceToSrc, which guarantees this |
| 50 | SkASSERT(tol >= gMinCurveTol); |
| 51 | |
| 52 | SkScalar d = SkPointPriv::DistanceToLineSegmentBetween(points[1], points[0], points[2]); |
| 53 | if (!SkScalarIsFinite(d)) { |
| 54 | return kMaxPointsPerCurve; |
| 55 | } else if (d <= tol) { |
| 56 | return 1; |
| 57 | } else { |
| 58 | // Each time we subdivide, d should be cut in 4. So we need to |
| 59 | // subdivide x = log4(d/tol) times. x subdivisions creates 2^(x) |
| 60 | // points. |
| 61 | // 2^(log4(x)) = sqrt(x); |
| 62 | SkScalar divSqrt = SkScalarSqrt(d / tol); |
| 63 | if (((SkScalar)SK_MaxS32) <= divSqrt) { |
| 64 | return kMaxPointsPerCurve; |
| 65 | } else { |
| 66 | int temp = SkScalarCeilToInt(divSqrt); |
| 67 | int pow2 = GrNextPow2(temp); |
| 68 | // Because of NaNs & INFs we can wind up with a degenerate temp |
| 69 | // such that pow2 comes out negative. Also, our point generator |
| 70 | // will always output at least one pt. |
| 71 | if (pow2 < 1) { |
| 72 | pow2 = 1; |
| 73 | } |
| 74 | return std::min(pow2, kMaxPointsPerCurve); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | uint32_t GrPathUtils::generateQuadraticPoints(const SkPoint& p0, |
| 80 | const SkPoint& p1, |
| 81 | const SkPoint& p2, |
| 82 | SkScalar tolSqd, |
| 83 | SkPoint** points, |
| 84 | uint32_t pointsLeft) { |
| 85 | if (pointsLeft < 2 || |
| 86 | (SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p2)) < tolSqd) { |
| 87 | (*points)[0] = p2; |
| 88 | *points += 1; |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | SkPoint q[] = { |
| 93 | { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, |
| 94 | { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, |
| 95 | }; |
| 96 | SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }; |
| 97 | |
| 98 | pointsLeft >>= 1; |
| 99 | uint32_t a = generateQuadraticPoints(p0, q[0], r, tolSqd, points, pointsLeft); |
| 100 | uint32_t b = generateQuadraticPoints(r, q[1], p2, tolSqd, points, pointsLeft); |
| 101 | return a + b; |
| 102 | } |
| 103 | |
| 104 | uint32_t GrPathUtils::cubicPointCount(const SkPoint points[], |
| 105 | SkScalar tol) { |
| 106 | // You should have called scaleToleranceToSrc, which guarantees this |
| 107 | SkASSERT(tol >= gMinCurveTol); |
| 108 | |
| 109 | SkScalar d = std::max( |
| 110 | SkPointPriv::DistanceToLineSegmentBetweenSqd(points[1], points[0], points[3]), |
| 111 | SkPointPriv::DistanceToLineSegmentBetweenSqd(points[2], points[0], points[3])); |
| 112 | d = SkScalarSqrt(d); |
| 113 | if (!SkScalarIsFinite(d)) { |
| 114 | return kMaxPointsPerCurve; |
| 115 | } else if (d <= tol) { |
| 116 | return 1; |
| 117 | } else { |
| 118 | SkScalar divSqrt = SkScalarSqrt(d / tol); |
| 119 | if (((SkScalar)SK_MaxS32) <= divSqrt) { |
| 120 | return kMaxPointsPerCurve; |
| 121 | } else { |
| 122 | int temp = SkScalarCeilToInt(SkScalarSqrt(d / tol)); |
| 123 | int pow2 = GrNextPow2(temp); |
| 124 | // Because of NaNs & INFs we can wind up with a degenerate temp |
| 125 | // such that pow2 comes out negative. Also, our point generator |
| 126 | // will always output at least one pt. |
| 127 | if (pow2 < 1) { |
| 128 | pow2 = 1; |
| 129 | } |
| 130 | return std::min(pow2, kMaxPointsPerCurve); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | uint32_t GrPathUtils::generateCubicPoints(const SkPoint& p0, |
| 136 | const SkPoint& p1, |
| 137 | const SkPoint& p2, |
| 138 | const SkPoint& p3, |
| 139 | SkScalar tolSqd, |
| 140 | SkPoint** points, |
| 141 | uint32_t pointsLeft) { |
| 142 | if (pointsLeft < 2 || |
| 143 | (SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3) < tolSqd && |
| 144 | SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3) < tolSqd)) { |
| 145 | (*points)[0] = p3; |
| 146 | *points += 1; |
| 147 | return 1; |
| 148 | } |
| 149 | SkPoint q[] = { |
| 150 | { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, |
| 151 | { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, |
| 152 | { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) } |
| 153 | }; |
| 154 | SkPoint r[] = { |
| 155 | { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }, |
| 156 | { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) } |
| 157 | }; |
| 158 | SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) }; |
| 159 | pointsLeft >>= 1; |
| 160 | uint32_t a = generateCubicPoints(p0, q[0], r[0], s, tolSqd, points, pointsLeft); |
| 161 | uint32_t b = generateCubicPoints(s, r[1], q[2], p3, tolSqd, points, pointsLeft); |
| 162 | return a + b; |
| 163 | } |
| 164 | |
| 165 | int GrPathUtils::worstCasePointCount(const SkPath& path, int* subpaths, SkScalar tol) { |
| 166 | // You should have called scaleToleranceToSrc, which guarantees this |
| 167 | SkASSERT(tol >= gMinCurveTol); |
| 168 | |
| 169 | int pointCount = 0; |
| 170 | *subpaths = 1; |
| 171 | |
| 172 | bool first = true; |
| 173 | |
| 174 | SkPath::Iter iter(path, false); |
| 175 | SkPath::Verb verb; |
| 176 | |
| 177 | SkPoint pts[4]; |
| 178 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 179 | |
| 180 | switch (verb) { |
| 181 | case SkPath::kLine_Verb: |
| 182 | pointCount += 1; |
| 183 | break; |
| 184 | case SkPath::kConic_Verb: { |
| 185 | SkScalar weight = iter.conicWeight(); |
| 186 | SkAutoConicToQuads converter; |
| 187 | const SkPoint* quadPts = converter.computeQuads(pts, weight, tol); |
| 188 | for (int i = 0; i < converter.countQuads(); ++i) { |
| 189 | pointCount += quadraticPointCount(quadPts + 2*i, tol); |
| 190 | } |
| 191 | [[fallthrough]]; |
| 192 | } |
| 193 | case SkPath::kQuad_Verb: |
| 194 | pointCount += quadraticPointCount(pts, tol); |
| 195 | break; |
| 196 | case SkPath::kCubic_Verb: |
| 197 | pointCount += cubicPointCount(pts, tol); |
| 198 | break; |
| 199 | case SkPath::kMove_Verb: |
| 200 | pointCount += 1; |
| 201 | if (!first) { |
| 202 | ++(*subpaths); |
| 203 | } |
| 204 | break; |
| 205 | default: |
| 206 | break; |
| 207 | } |
| 208 | first = false; |
| 209 | } |
| 210 | return pointCount; |
| 211 | } |
| 212 | |
| 213 | void GrPathUtils::QuadUVMatrix::set(const SkPoint qPts[3]) { |
| 214 | SkMatrix m; |
| 215 | // We want M such that M * xy_pt = uv_pt |
| 216 | // We know M * control_pts = [0 1/2 1] |
| 217 | // [0 0 1] |
| 218 | // [1 1 1] |
| 219 | // And control_pts = [x0 x1 x2] |
| 220 | // [y0 y1 y2] |
| 221 | // [1 1 1 ] |
| 222 | // We invert the control pt matrix and post concat to both sides to get M. |
| 223 | // Using the known form of the control point matrix and the result, we can |
| 224 | // optimize and improve precision. |
| 225 | |
| 226 | double x0 = qPts[0].fX; |
| 227 | double y0 = qPts[0].fY; |
| 228 | double x1 = qPts[1].fX; |
| 229 | double y1 = qPts[1].fY; |
| 230 | double x2 = qPts[2].fX; |
| 231 | double y2 = qPts[2].fY; |
| 232 | double det = x0*y1 - y0*x1 + x2*y0 - y2*x0 + x1*y2 - y1*x2; |
| 233 | |
| 234 | if (!sk_float_isfinite(det) |
| 235 | || SkScalarNearlyZero((float)det, SK_ScalarNearlyZero * SK_ScalarNearlyZero)) { |
| 236 | // The quad is degenerate. Hopefully this is rare. Find the pts that are |
| 237 | // farthest apart to compute a line (unless it is really a pt). |
| 238 | SkScalar maxD = SkPointPriv::DistanceToSqd(qPts[0], qPts[1]); |
| 239 | int maxEdge = 0; |
| 240 | SkScalar d = SkPointPriv::DistanceToSqd(qPts[1], qPts[2]); |
| 241 | if (d > maxD) { |
| 242 | maxD = d; |
| 243 | maxEdge = 1; |
| 244 | } |
| 245 | d = SkPointPriv::DistanceToSqd(qPts[2], qPts[0]); |
| 246 | if (d > maxD) { |
| 247 | maxD = d; |
| 248 | maxEdge = 2; |
| 249 | } |
| 250 | // We could have a tolerance here, not sure if it would improve anything |
| 251 | if (maxD > 0) { |
| 252 | // Set the matrix to give (u = 0, v = distance_to_line) |
| 253 | SkVector lineVec = qPts[(maxEdge + 1)%3] - qPts[maxEdge]; |
| 254 | // when looking from the point 0 down the line we want positive |
| 255 | // distances to be to the left. This matches the non-degenerate |
| 256 | // case. |
| 257 | lineVec = SkPointPriv::MakeOrthog(lineVec, SkPointPriv::kLeft_Side); |
| 258 | // first row |
| 259 | fM[0] = 0; |
| 260 | fM[1] = 0; |
| 261 | fM[2] = 0; |
| 262 | // second row |
| 263 | fM[3] = lineVec.fX; |
| 264 | fM[4] = lineVec.fY; |
| 265 | fM[5] = -lineVec.dot(qPts[maxEdge]); |
| 266 | } else { |
| 267 | // It's a point. It should cover zero area. Just set the matrix such |
| 268 | // that (u, v) will always be far away from the quad. |
| 269 | fM[0] = 0; fM[1] = 0; fM[2] = 100.f; |
| 270 | fM[3] = 0; fM[4] = 0; fM[5] = 100.f; |
| 271 | } |
| 272 | } else { |
| 273 | double scale = 1.0/det; |
| 274 | |
| 275 | // compute adjugate matrix |
| 276 | double a2, a3, a4, a5, a6, a7, a8; |
| 277 | a2 = x1*y2-x2*y1; |
| 278 | |
| 279 | a3 = y2-y0; |
| 280 | a4 = x0-x2; |
| 281 | a5 = x2*y0-x0*y2; |
| 282 | |
| 283 | a6 = y0-y1; |
| 284 | a7 = x1-x0; |
| 285 | a8 = x0*y1-x1*y0; |
| 286 | |
| 287 | // this performs the uv_pts*adjugate(control_pts) multiply, |
| 288 | // then does the scale by 1/det afterwards to improve precision |
| 289 | m[SkMatrix::kMScaleX] = (float)((0.5*a3 + a6)*scale); |
| 290 | m[SkMatrix::kMSkewX] = (float)((0.5*a4 + a7)*scale); |
| 291 | m[SkMatrix::kMTransX] = (float)((0.5*a5 + a8)*scale); |
| 292 | |
| 293 | m[SkMatrix::kMSkewY] = (float)(a6*scale); |
| 294 | m[SkMatrix::kMScaleY] = (float)(a7*scale); |
| 295 | m[SkMatrix::kMTransY] = (float)(a8*scale); |
| 296 | |
| 297 | // kMPersp0 & kMPersp1 should algebraically be zero |
| 298 | m[SkMatrix::kMPersp0] = 0.0f; |
| 299 | m[SkMatrix::kMPersp1] = 0.0f; |
| 300 | m[SkMatrix::kMPersp2] = (float)((a2 + a5 + a8)*scale); |
| 301 | |
| 302 | // It may not be normalized to have 1.0 in the bottom right |
| 303 | float m33 = m.get(SkMatrix::kMPersp2); |
| 304 | if (1.f != m33) { |
| 305 | m33 = 1.f / m33; |
| 306 | fM[0] = m33 * m.get(SkMatrix::kMScaleX); |
| 307 | fM[1] = m33 * m.get(SkMatrix::kMSkewX); |
| 308 | fM[2] = m33 * m.get(SkMatrix::kMTransX); |
| 309 | fM[3] = m33 * m.get(SkMatrix::kMSkewY); |
| 310 | fM[4] = m33 * m.get(SkMatrix::kMScaleY); |
| 311 | fM[5] = m33 * m.get(SkMatrix::kMTransY); |
| 312 | } else { |
| 313 | fM[0] = m.get(SkMatrix::kMScaleX); |
| 314 | fM[1] = m.get(SkMatrix::kMSkewX); |
| 315 | fM[2] = m.get(SkMatrix::kMTransX); |
| 316 | fM[3] = m.get(SkMatrix::kMSkewY); |
| 317 | fM[4] = m.get(SkMatrix::kMScaleY); |
| 318 | fM[5] = m.get(SkMatrix::kMTransY); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | //////////////////////////////////////////////////////////////////////////////// |
| 324 | |
| 325 | // k = (y2 - y0, x0 - x2, x2*y0 - x0*y2) |
| 326 | // l = (y1 - y0, x0 - x1, x1*y0 - x0*y1) * 2*w |
| 327 | // m = (y2 - y1, x1 - x2, x2*y1 - x1*y2) * 2*w |
| 328 | void GrPathUtils::getConicKLM(const SkPoint p[3], const SkScalar weight, SkMatrix* out) { |
| 329 | SkMatrix& klm = *out; |
| 330 | const SkScalar w2 = 2.f * weight; |
| 331 | klm[0] = p[2].fY - p[0].fY; |
| 332 | klm[1] = p[0].fX - p[2].fX; |
| 333 | klm[2] = p[2].fX * p[0].fY - p[0].fX * p[2].fY; |
| 334 | |
| 335 | klm[3] = w2 * (p[1].fY - p[0].fY); |
| 336 | klm[4] = w2 * (p[0].fX - p[1].fX); |
| 337 | klm[5] = w2 * (p[1].fX * p[0].fY - p[0].fX * p[1].fY); |
| 338 | |
| 339 | klm[6] = w2 * (p[2].fY - p[1].fY); |
| 340 | klm[7] = w2 * (p[1].fX - p[2].fX); |
| 341 | klm[8] = w2 * (p[2].fX * p[1].fY - p[1].fX * p[2].fY); |
| 342 | |
| 343 | // scale the max absolute value of coeffs to 10 |
| 344 | SkScalar scale = 0.f; |
| 345 | for (int i = 0; i < 9; ++i) { |
| 346 | scale = std::max(scale, SkScalarAbs(klm[i])); |
| 347 | } |
| 348 | SkASSERT(scale > 0.f); |
| 349 | scale = 10.f / scale; |
| 350 | for (int i = 0; i < 9; ++i) { |
| 351 | klm[i] *= scale; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | //////////////////////////////////////////////////////////////////////////////// |
| 356 | |
| 357 | namespace { |
| 358 | |
| 359 | // a is the first control point of the cubic. |
| 360 | // ab is the vector from a to the second control point. |
| 361 | // dc is the vector from the fourth to the third control point. |
| 362 | // d is the fourth control point. |
| 363 | // p is the candidate quadratic control point. |
| 364 | // this assumes that the cubic doesn't inflect and is simple |
| 365 | bool is_point_within_cubic_tangents(const SkPoint& a, |
| 366 | const SkVector& ab, |
| 367 | const SkVector& dc, |
| 368 | const SkPoint& d, |
| 369 | SkPathPriv::FirstDirection dir, |
| 370 | const SkPoint p) { |
| 371 | SkVector ap = p - a; |
| 372 | SkScalar apXab = ap.cross(ab); |
| 373 | if (SkPathPriv::kCW_FirstDirection == dir) { |
| 374 | if (apXab > 0) { |
| 375 | return false; |
| 376 | } |
| 377 | } else { |
| 378 | SkASSERT(SkPathPriv::kCCW_FirstDirection == dir); |
| 379 | if (apXab < 0) { |
| 380 | return false; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | SkVector dp = p - d; |
| 385 | SkScalar dpXdc = dp.cross(dc); |
| 386 | if (SkPathPriv::kCW_FirstDirection == dir) { |
| 387 | if (dpXdc < 0) { |
| 388 | return false; |
| 389 | } |
| 390 | } else { |
| 391 | SkASSERT(SkPathPriv::kCCW_FirstDirection == dir); |
| 392 | if (dpXdc > 0) { |
| 393 | return false; |
| 394 | } |
| 395 | } |
| 396 | return true; |
| 397 | } |
| 398 | |
| 399 | void convert_noninflect_cubic_to_quads(const SkPoint p[4], |
| 400 | SkScalar toleranceSqd, |
| 401 | SkTArray<SkPoint, true>* quads, |
| 402 | int sublevel = 0, |
| 403 | bool preserveFirstTangent = true, |
| 404 | bool preserveLastTangent = true) { |
| 405 | // Notation: Point a is always p[0]. Point b is p[1] unless p[1] == p[0], in which case it is |
| 406 | // 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]. |
| 407 | SkVector ab = p[1] - p[0]; |
| 408 | SkVector dc = p[2] - p[3]; |
| 409 | |
| 410 | if (SkPointPriv::LengthSqd(ab) < SK_ScalarNearlyZero) { |
| 411 | if (SkPointPriv::LengthSqd(dc) < SK_ScalarNearlyZero) { |
| 412 | SkPoint* degQuad = quads->push_back_n(3); |
| 413 | degQuad[0] = p[0]; |
| 414 | degQuad[1] = p[0]; |
| 415 | degQuad[2] = p[3]; |
| 416 | return; |
| 417 | } |
| 418 | ab = p[2] - p[0]; |
| 419 | } |
| 420 | if (SkPointPriv::LengthSqd(dc) < SK_ScalarNearlyZero) { |
| 421 | dc = p[1] - p[3]; |
| 422 | } |
| 423 | |
| 424 | static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2; |
| 425 | static const int kMaxSubdivs = 10; |
| 426 | |
| 427 | ab.scale(kLengthScale); |
| 428 | dc.scale(kLengthScale); |
| 429 | |
| 430 | // c0 and c1 are extrapolations along vectors ab and dc. |
| 431 | SkPoint c0 = p[0] + ab; |
| 432 | SkPoint c1 = p[3] + dc; |
| 433 | |
| 434 | SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : SkPointPriv::DistanceToSqd(c0, c1); |
| 435 | if (dSqd < toleranceSqd) { |
| 436 | SkPoint newC; |
| 437 | if (preserveFirstTangent == preserveLastTangent) { |
| 438 | // We used to force a split when both tangents need to be preserved and c0 != c1. |
| 439 | // This introduced a large performance regression for tiny paths for no noticeable |
| 440 | // quality improvement. However, we aren't quite fulfilling our contract of guaranteeing |
| 441 | // the two tangent vectors and this could introduce a missed pixel in |
| 442 | // GrAAHairlinePathRenderer. |
| 443 | newC = (c0 + c1) * 0.5f; |
| 444 | } else if (preserveFirstTangent) { |
| 445 | newC = c0; |
| 446 | } else { |
| 447 | newC = c1; |
| 448 | } |
| 449 | |
| 450 | SkPoint* pts = quads->push_back_n(3); |
| 451 | pts[0] = p[0]; |
| 452 | pts[1] = newC; |
| 453 | pts[2] = p[3]; |
| 454 | return; |
| 455 | } |
| 456 | SkPoint choppedPts[7]; |
| 457 | SkChopCubicAtHalf(p, choppedPts); |
| 458 | convert_noninflect_cubic_to_quads( |
| 459 | choppedPts + 0, toleranceSqd, quads, sublevel + 1, preserveFirstTangent, false); |
| 460 | convert_noninflect_cubic_to_quads( |
| 461 | choppedPts + 3, toleranceSqd, quads, sublevel + 1, false, preserveLastTangent); |
| 462 | } |
| 463 | |
| 464 | void convert_noninflect_cubic_to_quads_with_constraint(const SkPoint p[4], |
| 465 | SkScalar toleranceSqd, |
| 466 | SkPathPriv::FirstDirection dir, |
| 467 | SkTArray<SkPoint, true>* quads, |
| 468 | int sublevel = 0) { |
| 469 | // Notation: Point a is always p[0]. Point b is p[1] unless p[1] == p[0], in which case it is |
| 470 | // 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]. |
| 471 | |
| 472 | SkVector ab = p[1] - p[0]; |
| 473 | SkVector dc = p[2] - p[3]; |
| 474 | |
| 475 | if (SkPointPriv::LengthSqd(ab) < SK_ScalarNearlyZero) { |
| 476 | if (SkPointPriv::LengthSqd(dc) < SK_ScalarNearlyZero) { |
| 477 | SkPoint* degQuad = quads->push_back_n(3); |
| 478 | degQuad[0] = p[0]; |
| 479 | degQuad[1] = p[0]; |
| 480 | degQuad[2] = p[3]; |
| 481 | return; |
| 482 | } |
| 483 | ab = p[2] - p[0]; |
| 484 | } |
| 485 | if (SkPointPriv::LengthSqd(dc) < SK_ScalarNearlyZero) { |
| 486 | dc = p[1] - p[3]; |
| 487 | } |
| 488 | |
| 489 | // When the ab and cd tangents are degenerate or nearly parallel with vector from d to a the |
| 490 | // constraint that the quad point falls between the tangents becomes hard to enforce and we are |
| 491 | // likely to hit the max subdivision count. However, in this case the cubic is approaching a |
| 492 | // line and the accuracy of the quad point isn't so important. We check if the two middle cubic |
| 493 | // control points are very close to the baseline vector. If so then we just pick quadratic |
| 494 | // points on the control polygon. |
| 495 | |
| 496 | SkVector da = p[0] - p[3]; |
| 497 | bool doQuads = SkPointPriv::LengthSqd(dc) < SK_ScalarNearlyZero || |
| 498 | SkPointPriv::LengthSqd(ab) < SK_ScalarNearlyZero; |
| 499 | if (!doQuads) { |
| 500 | SkScalar invDALengthSqd = SkPointPriv::LengthSqd(da); |
| 501 | if (invDALengthSqd > SK_ScalarNearlyZero) { |
| 502 | invDALengthSqd = SkScalarInvert(invDALengthSqd); |
| 503 | // cross(ab, da)^2/length(da)^2 == sqd distance from b to line from d to a. |
| 504 | // same goes for point c using vector cd. |
| 505 | SkScalar detABSqd = ab.cross(da); |
| 506 | detABSqd = SkScalarSquare(detABSqd); |
| 507 | SkScalar detDCSqd = dc.cross(da); |
| 508 | detDCSqd = SkScalarSquare(detDCSqd); |
| 509 | if (detABSqd * invDALengthSqd < toleranceSqd && |
| 510 | detDCSqd * invDALengthSqd < toleranceSqd) { |
| 511 | doQuads = true; |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | if (doQuads) { |
| 516 | SkPoint b = p[0] + ab; |
| 517 | SkPoint c = p[3] + dc; |
| 518 | SkPoint mid = b + c; |
| 519 | mid.scale(SK_ScalarHalf); |
| 520 | // Insert two quadratics to cover the case when ab points away from d and/or dc |
| 521 | // points away from a. |
| 522 | if (SkVector::DotProduct(da, dc) < 0 || SkVector::DotProduct(ab, da) > 0) { |
| 523 | SkPoint* qpts = quads->push_back_n(6); |
| 524 | qpts[0] = p[0]; |
| 525 | qpts[1] = b; |
| 526 | qpts[2] = mid; |
| 527 | qpts[3] = mid; |
| 528 | qpts[4] = c; |
| 529 | qpts[5] = p[3]; |
| 530 | } else { |
| 531 | SkPoint* qpts = quads->push_back_n(3); |
| 532 | qpts[0] = p[0]; |
| 533 | qpts[1] = mid; |
| 534 | qpts[2] = p[3]; |
| 535 | } |
| 536 | return; |
| 537 | } |
| 538 | |
| 539 | static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2; |
| 540 | static const int kMaxSubdivs = 10; |
| 541 | |
| 542 | ab.scale(kLengthScale); |
| 543 | dc.scale(kLengthScale); |
| 544 | |
| 545 | // c0 and c1 are extrapolations along vectors ab and dc. |
| 546 | SkVector c0 = p[0] + ab; |
| 547 | SkVector c1 = p[3] + dc; |
| 548 | |
| 549 | SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : SkPointPriv::DistanceToSqd(c0, c1); |
| 550 | if (dSqd < toleranceSqd) { |
| 551 | SkPoint cAvg = (c0 + c1) * 0.5f; |
| 552 | bool subdivide = false; |
| 553 | |
| 554 | if (!is_point_within_cubic_tangents(p[0], ab, dc, p[3], dir, cAvg)) { |
| 555 | // choose a new cAvg that is the intersection of the two tangent lines. |
| 556 | ab = SkPointPriv::MakeOrthog(ab); |
| 557 | SkScalar z0 = -ab.dot(p[0]); |
| 558 | dc = SkPointPriv::MakeOrthog(dc); |
| 559 | SkScalar z1 = -dc.dot(p[3]); |
| 560 | cAvg.fX = ab.fY * z1 - z0 * dc.fY; |
| 561 | cAvg.fY = z0 * dc.fX - ab.fX * z1; |
| 562 | SkScalar z = ab.fX * dc.fY - ab.fY * dc.fX; |
| 563 | z = SkScalarInvert(z); |
| 564 | cAvg.fX *= z; |
| 565 | cAvg.fY *= z; |
| 566 | if (sublevel <= kMaxSubdivs) { |
| 567 | SkScalar d0Sqd = SkPointPriv::DistanceToSqd(c0, cAvg); |
| 568 | SkScalar d1Sqd = SkPointPriv::DistanceToSqd(c1, cAvg); |
| 569 | // We need to subdivide if d0 + d1 > tolerance but we have the sqd values. We know |
| 570 | // the distances and tolerance can't be negative. |
| 571 | // (d0 + d1)^2 > toleranceSqd |
| 572 | // d0Sqd + 2*d0*d1 + d1Sqd > toleranceSqd |
| 573 | SkScalar d0d1 = SkScalarSqrt(d0Sqd * d1Sqd); |
| 574 | subdivide = 2 * d0d1 + d0Sqd + d1Sqd > toleranceSqd; |
| 575 | } |
| 576 | } |
| 577 | if (!subdivide) { |
| 578 | SkPoint* pts = quads->push_back_n(3); |
| 579 | pts[0] = p[0]; |
| 580 | pts[1] = cAvg; |
| 581 | pts[2] = p[3]; |
| 582 | return; |
| 583 | } |
| 584 | } |
| 585 | SkPoint choppedPts[7]; |
| 586 | SkChopCubicAtHalf(p, choppedPts); |
| 587 | convert_noninflect_cubic_to_quads_with_constraint( |
| 588 | choppedPts + 0, toleranceSqd, dir, quads, sublevel + 1); |
| 589 | convert_noninflect_cubic_to_quads_with_constraint( |
| 590 | choppedPts + 3, toleranceSqd, dir, quads, sublevel + 1); |
| 591 | } |
| 592 | } // namespace |
| 593 | |
| 594 | void GrPathUtils::convertCubicToQuads(const SkPoint p[4], |
| 595 | SkScalar tolScale, |
| 596 | SkTArray<SkPoint, true>* quads) { |
| 597 | if (!p[0].isFinite() || !p[1].isFinite() || !p[2].isFinite() || !p[3].isFinite()) { |
| 598 | return; |
| 599 | } |
| 600 | if (!SkScalarIsFinite(tolScale)) { |
| 601 | return; |
| 602 | } |
| 603 | SkPoint chopped[10]; |
| 604 | int count = SkChopCubicAtInflections(p, chopped); |
| 605 | |
| 606 | const SkScalar tolSqd = SkScalarSquare(tolScale); |
| 607 | |
| 608 | for (int i = 0; i < count; ++i) { |
| 609 | SkPoint* cubic = chopped + 3*i; |
| 610 | convert_noninflect_cubic_to_quads(cubic, tolSqd, quads); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | void GrPathUtils::convertCubicToQuadsConstrainToTangents(const SkPoint p[4], |
| 615 | SkScalar tolScale, |
| 616 | SkPathPriv::FirstDirection dir, |
| 617 | SkTArray<SkPoint, true>* quads) { |
| 618 | if (!p[0].isFinite() || !p[1].isFinite() || !p[2].isFinite() || !p[3].isFinite()) { |
| 619 | return; |
| 620 | } |
| 621 | if (!SkScalarIsFinite(tolScale)) { |
| 622 | return; |
| 623 | } |
| 624 | SkPoint chopped[10]; |
| 625 | int count = SkChopCubicAtInflections(p, chopped); |
| 626 | |
| 627 | const SkScalar tolSqd = SkScalarSquare(tolScale); |
| 628 | |
| 629 | for (int i = 0; i < count; ++i) { |
| 630 | SkPoint* cubic = chopped + 3*i; |
| 631 | convert_noninflect_cubic_to_quads_with_constraint(cubic, tolSqd, dir, quads); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | //////////////////////////////////////////////////////////////////////////////// |
| 636 | |
| 637 | using ExcludedTerm = GrPathUtils::ExcludedTerm; |
| 638 | |
| 639 | ExcludedTerm GrPathUtils::calcCubicInverseTransposePowerBasisMatrix(const SkPoint p[4], |
| 640 | SkMatrix* out) { |
| 641 | static_assert(SK_SCALAR_IS_FLOAT); |
| 642 | |
| 643 | // First convert the bezier coordinates p[0..3] to power basis coefficients X,Y(,W=[0 0 0 1]). |
| 644 | // M3 is the matrix that does this conversion. The homogeneous equation for the cubic becomes: |
| 645 | // |
| 646 | // | X Y 0 | |
| 647 | // C(t,s) = [t^3 t^2*s t*s^2 s^3] * | . . 0 | |
| 648 | // | . . 0 | |
| 649 | // | . . 1 | |
| 650 | // |
| 651 | const Sk4f M3[3] = {Sk4f(-1, 3, -3, 1), |
| 652 | Sk4f(3, -6, 3, 0), |
| 653 | Sk4f(-3, 3, 0, 0)}; |
| 654 | // 4th col of M3 = Sk4f(1, 0, 0, 0)}; |
| 655 | Sk4f X(p[3].x(), 0, 0, 0); |
| 656 | Sk4f Y(p[3].y(), 0, 0, 0); |
| 657 | for (int i = 2; i >= 0; --i) { |
| 658 | X += M3[i] * p[i].x(); |
| 659 | Y += M3[i] * p[i].y(); |
| 660 | } |
| 661 | |
| 662 | // The matrix is 3x4. In order to invert it, we first need to make it square by throwing out one |
| 663 | // of the middle two rows. We toss the row that leaves us with the largest absolute determinant. |
| 664 | // Since the right column will be [0 0 1], the respective determinants reduce to x0*y2 - y0*x2 |
| 665 | // and x0*y1 - y0*x1. |
| 666 | SkScalar dets[4]; |
| 667 | Sk4f D = SkNx_shuffle<0,0,2,1>(X) * SkNx_shuffle<2,1,0,0>(Y); |
| 668 | D -= SkNx_shuffle<2,3,0,1>(D); |
| 669 | D.store(dets); |
| 670 | ExcludedTerm skipTerm = SkScalarAbs(dets[0]) > SkScalarAbs(dets[1]) ? |
| 671 | ExcludedTerm::kQuadraticTerm : ExcludedTerm::kLinearTerm; |
| 672 | SkScalar det = dets[ExcludedTerm::kQuadraticTerm == skipTerm ? 0 : 1]; |
| 673 | if (0 == det) { |
| 674 | return ExcludedTerm::kNonInvertible; |
| 675 | } |
| 676 | SkScalar rdet = 1 / det; |
| 677 | |
| 678 | // Compute the inverse-transpose of the power basis matrix with the 'skipRow'th row removed. |
| 679 | // Since W=[0 0 0 1], it follows that our corresponding solution will be equal to: |
| 680 | // |
| 681 | // | y1 -x1 x1*y2 - y1*x2 | |
| 682 | // 1/det * | -y0 x0 -x0*y2 + y0*x2 | |
| 683 | // | 0 0 det | |
| 684 | // |
| 685 | SkScalar x[4], y[4], z[4]; |
| 686 | X.store(x); |
| 687 | Y.store(y); |
| 688 | (X * SkNx_shuffle<3,3,3,3>(Y) - Y * SkNx_shuffle<3,3,3,3>(X)).store(z); |
| 689 | |
| 690 | int middleRow = ExcludedTerm::kQuadraticTerm == skipTerm ? 2 : 1; |
| 691 | out->setAll( y[middleRow] * rdet, -x[middleRow] * rdet, z[middleRow] * rdet, |
| 692 | -y[0] * rdet, x[0] * rdet, -z[0] * rdet, |
| 693 | 0, 0, 1); |
| 694 | |
| 695 | return skipTerm; |
| 696 | } |
| 697 | |
| 698 | inline static void calc_serp_kcoeffs(SkScalar tl, SkScalar sl, SkScalar tm, SkScalar sm, |
| 699 | ExcludedTerm skipTerm, SkScalar outCoeffs[3]) { |
| 700 | SkASSERT(ExcludedTerm::kQuadraticTerm == skipTerm || ExcludedTerm::kLinearTerm == skipTerm); |
| 701 | outCoeffs[0] = 0; |
| 702 | outCoeffs[1] = (ExcludedTerm::kLinearTerm == skipTerm) ? sl*sm : -tl*sm - tm*sl; |
| 703 | outCoeffs[2] = tl*tm; |
| 704 | } |
| 705 | |
| 706 | inline static void calc_serp_lmcoeffs(SkScalar t, SkScalar s, ExcludedTerm skipTerm, |
| 707 | SkScalar outCoeffs[3]) { |
| 708 | SkASSERT(ExcludedTerm::kQuadraticTerm == skipTerm || ExcludedTerm::kLinearTerm == skipTerm); |
| 709 | outCoeffs[0] = -s*s*s; |
| 710 | outCoeffs[1] = (ExcludedTerm::kLinearTerm == skipTerm) ? 3*s*s*t : -3*s*t*t; |
| 711 | outCoeffs[2] = t*t*t; |
| 712 | } |
| 713 | |
| 714 | inline static void calc_loop_kcoeffs(SkScalar td, SkScalar sd, SkScalar te, SkScalar se, |
| 715 | SkScalar tdse, SkScalar tesd, ExcludedTerm skipTerm, |
| 716 | SkScalar outCoeffs[3]) { |
| 717 | SkASSERT(ExcludedTerm::kQuadraticTerm == skipTerm || ExcludedTerm::kLinearTerm == skipTerm); |
| 718 | outCoeffs[0] = 0; |
| 719 | outCoeffs[1] = (ExcludedTerm::kLinearTerm == skipTerm) ? sd*se : -tdse - tesd; |
| 720 | outCoeffs[2] = td*te; |
| 721 | } |
| 722 | |
| 723 | inline static void calc_loop_lmcoeffs(SkScalar t2, SkScalar s2, SkScalar t1, SkScalar s1, |
| 724 | SkScalar t2s1, SkScalar t1s2, ExcludedTerm skipTerm, |
| 725 | SkScalar outCoeffs[3]) { |
| 726 | SkASSERT(ExcludedTerm::kQuadraticTerm == skipTerm || ExcludedTerm::kLinearTerm == skipTerm); |
| 727 | outCoeffs[0] = -s2*s2*s1; |
| 728 | outCoeffs[1] = (ExcludedTerm::kLinearTerm == skipTerm) ? s2 * (2*t2s1 + t1s2) |
| 729 | : -t2 * (t2s1 + 2*t1s2); |
| 730 | outCoeffs[2] = t2*t2*t1; |
| 731 | } |
| 732 | |
| 733 | // For the case when a cubic bezier is actually a quadratic. We duplicate k in l so that the |
| 734 | // implicit becomes: |
| 735 | // |
| 736 | // k^3 - l*m == k^3 - l*k == k * (k^2 - l) |
| 737 | // |
| 738 | // In the quadratic case we can simply assign fixed values at each control point: |
| 739 | // |
| 740 | // | ..K.. | | pts[0] pts[1] pts[2] pts[3] | | 0 1/3 2/3 1 | |
| 741 | // | ..L.. | * | . . . . | == | 0 0 1/3 1 | |
| 742 | // | ..K.. | | 1 1 1 1 | | 0 1/3 2/3 1 | |
| 743 | // |
| 744 | static void calc_quadratic_klm(const SkPoint pts[4], double d3, SkMatrix* klm) { |
| 745 | SkMatrix klmAtPts; |
| 746 | klmAtPts.setAll(0, 1.f/3, 1, |
| 747 | 0, 0, 1, |
| 748 | 0, 1.f/3, 1); |
| 749 | |
| 750 | SkMatrix inversePts; |
| 751 | inversePts.setAll(pts[0].x(), pts[1].x(), pts[3].x(), |
| 752 | pts[0].y(), pts[1].y(), pts[3].y(), |
| 753 | 1, 1, 1); |
| 754 | SkAssertResult(inversePts.invert(&inversePts)); |
| 755 | |
| 756 | klm->setConcat(klmAtPts, inversePts); |
| 757 | |
| 758 | // If d3 > 0 we need to flip the orientation of our curve |
| 759 | // This is done by negating the k and l values |
| 760 | if (d3 > 0) { |
| 761 | klm->postScale(-1, -1); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | // For the case when a cubic bezier is actually a line. We set K=0, L=1, M=-line, which results in |
| 766 | // the following implicit: |
| 767 | // |
| 768 | // k^3 - l*m == 0^3 - 1*(-line) == -(-line) == line |
| 769 | // |
| 770 | static void calc_line_klm(const SkPoint pts[4], SkMatrix* klm) { |
| 771 | SkScalar ny = pts[0].x() - pts[3].x(); |
| 772 | SkScalar nx = pts[3].y() - pts[0].y(); |
| 773 | SkScalar k = nx * pts[0].x() + ny * pts[0].y(); |
| 774 | klm->setAll( 0, 0, 0, |
| 775 | 0, 0, 1, |
| 776 | -nx, -ny, k); |
| 777 | } |
| 778 | |
| 779 | SkCubicType GrPathUtils::getCubicKLM(const SkPoint src[4], SkMatrix* klm, double tt[2], |
| 780 | double ss[2]) { |
| 781 | double d[4]; |
| 782 | SkCubicType type = SkClassifyCubic(src, tt, ss, d); |
| 783 | |
| 784 | if (SkCubicType::kLineOrPoint == type) { |
| 785 | calc_line_klm(src, klm); |
| 786 | return SkCubicType::kLineOrPoint; |
| 787 | } |
| 788 | |
| 789 | if (SkCubicType::kQuadratic == type) { |
| 790 | calc_quadratic_klm(src, d[3], klm); |
| 791 | return SkCubicType::kQuadratic; |
| 792 | } |
| 793 | |
| 794 | SkMatrix CIT; |
| 795 | ExcludedTerm skipTerm = calcCubicInverseTransposePowerBasisMatrix(src, &CIT); |
| 796 | if (ExcludedTerm::kNonInvertible == skipTerm) { |
| 797 | // This could technically also happen if the curve were quadratic, but SkClassifyCubic |
| 798 | // should have detected that case already with tolerance. |
| 799 | calc_line_klm(src, klm); |
| 800 | return SkCubicType::kLineOrPoint; |
| 801 | } |
| 802 | |
| 803 | const SkScalar t0 = static_cast<SkScalar>(tt[0]), t1 = static_cast<SkScalar>(tt[1]), |
| 804 | s0 = static_cast<SkScalar>(ss[0]), s1 = static_cast<SkScalar>(ss[1]); |
| 805 | |
| 806 | SkMatrix klmCoeffs; |
| 807 | switch (type) { |
| 808 | case SkCubicType::kCuspAtInfinity: |
| 809 | SkASSERT(1 == t1 && 0 == s1); // Infinity. |
| 810 | [[fallthrough]]; |
| 811 | case SkCubicType::kLocalCusp: |
| 812 | case SkCubicType::kSerpentine: |
| 813 | calc_serp_kcoeffs(t0, s0, t1, s1, skipTerm, &klmCoeffs[0]); |
| 814 | calc_serp_lmcoeffs(t0, s0, skipTerm, &klmCoeffs[3]); |
| 815 | calc_serp_lmcoeffs(t1, s1, skipTerm, &klmCoeffs[6]); |
| 816 | break; |
| 817 | case SkCubicType::kLoop: { |
| 818 | const SkScalar tdse = t0 * s1; |
| 819 | const SkScalar tesd = t1 * s0; |
| 820 | calc_loop_kcoeffs(t0, s0, t1, s1, tdse, tesd, skipTerm, &klmCoeffs[0]); |
| 821 | calc_loop_lmcoeffs(t0, s0, t1, s1, tdse, tesd, skipTerm, &klmCoeffs[3]); |
| 822 | calc_loop_lmcoeffs(t1, s1, t0, s0, tesd, tdse, skipTerm, &klmCoeffs[6]); |
| 823 | break; |
| 824 | } |
| 825 | default: |
| 826 | SK_ABORT("Unexpected cubic type." ); |
| 827 | break; |
| 828 | } |
| 829 | |
| 830 | klm->setConcat(klmCoeffs, CIT); |
| 831 | return type; |
| 832 | } |
| 833 | |
| 834 | int GrPathUtils::chopCubicAtLoopIntersection(const SkPoint src[4], SkPoint dst[10], SkMatrix* klm, |
| 835 | int* loopIndex) { |
| 836 | SkSTArray<2, SkScalar> chops; |
| 837 | *loopIndex = -1; |
| 838 | |
| 839 | double t[2], s[2]; |
| 840 | if (SkCubicType::kLoop == GrPathUtils::getCubicKLM(src, klm, t, s)) { |
| 841 | SkScalar t0 = static_cast<SkScalar>(t[0] / s[0]); |
| 842 | SkScalar t1 = static_cast<SkScalar>(t[1] / s[1]); |
| 843 | SkASSERT(t0 <= t1); // Technically t0 != t1 in a loop, but there may be FP error. |
| 844 | |
| 845 | if (t0 < 1 && t1 > 0) { |
| 846 | *loopIndex = 0; |
| 847 | if (t0 > 0) { |
| 848 | chops.push_back(t0); |
| 849 | *loopIndex = 1; |
| 850 | } |
| 851 | if (t1 < 1) { |
| 852 | chops.push_back(t1); |
| 853 | *loopIndex = chops.count() - 1; |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | SkChopCubicAt(src, dst, chops.begin(), chops.count()); |
| 859 | return chops.count() + 1; |
| 860 | } |
| 861 | |