gfx/skia/trunk/src/gpu/GrPathUtils.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright 2011 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "GrPathUtils.h"
michael@0 9
michael@0 10 #include "GrPoint.h"
michael@0 11 #include "SkGeometry.h"
michael@0 12
michael@0 13 SkScalar GrPathUtils::scaleToleranceToSrc(SkScalar devTol,
michael@0 14 const SkMatrix& viewM,
michael@0 15 const SkRect& pathBounds) {
michael@0 16 // In order to tesselate the path we get a bound on how much the matrix can
michael@0 17 // stretch when mapping to screen coordinates.
michael@0 18 SkScalar stretch = viewM.getMaxStretch();
michael@0 19 SkScalar srcTol = devTol;
michael@0 20
michael@0 21 if (stretch < 0) {
michael@0 22 // take worst case mapRadius amoung four corners.
michael@0 23 // (less than perfect)
michael@0 24 for (int i = 0; i < 4; ++i) {
michael@0 25 SkMatrix mat;
michael@0 26 mat.setTranslate((i % 2) ? pathBounds.fLeft : pathBounds.fRight,
michael@0 27 (i < 2) ? pathBounds.fTop : pathBounds.fBottom);
michael@0 28 mat.postConcat(viewM);
michael@0 29 stretch = SkMaxScalar(stretch, mat.mapRadius(SK_Scalar1));
michael@0 30 }
michael@0 31 }
michael@0 32 srcTol = SkScalarDiv(srcTol, stretch);
michael@0 33 return srcTol;
michael@0 34 }
michael@0 35
michael@0 36 static const int MAX_POINTS_PER_CURVE = 1 << 10;
michael@0 37 static const SkScalar gMinCurveTol = 0.0001f;
michael@0 38
michael@0 39 uint32_t GrPathUtils::quadraticPointCount(const GrPoint points[],
michael@0 40 SkScalar tol) {
michael@0 41 if (tol < gMinCurveTol) {
michael@0 42 tol = gMinCurveTol;
michael@0 43 }
michael@0 44 SkASSERT(tol > 0);
michael@0 45
michael@0 46 SkScalar d = points[1].distanceToLineSegmentBetween(points[0], points[2]);
michael@0 47 if (d <= tol) {
michael@0 48 return 1;
michael@0 49 } else {
michael@0 50 // Each time we subdivide, d should be cut in 4. So we need to
michael@0 51 // subdivide x = log4(d/tol) times. x subdivisions creates 2^(x)
michael@0 52 // points.
michael@0 53 // 2^(log4(x)) = sqrt(x);
michael@0 54 int temp = SkScalarCeilToInt(SkScalarSqrt(SkScalarDiv(d, tol)));
michael@0 55 int pow2 = GrNextPow2(temp);
michael@0 56 // Because of NaNs & INFs we can wind up with a degenerate temp
michael@0 57 // such that pow2 comes out negative. Also, our point generator
michael@0 58 // will always output at least one pt.
michael@0 59 if (pow2 < 1) {
michael@0 60 pow2 = 1;
michael@0 61 }
michael@0 62 return GrMin(pow2, MAX_POINTS_PER_CURVE);
michael@0 63 }
michael@0 64 }
michael@0 65
michael@0 66 uint32_t GrPathUtils::generateQuadraticPoints(const GrPoint& p0,
michael@0 67 const GrPoint& p1,
michael@0 68 const GrPoint& p2,
michael@0 69 SkScalar tolSqd,
michael@0 70 GrPoint** points,
michael@0 71 uint32_t pointsLeft) {
michael@0 72 if (pointsLeft < 2 ||
michael@0 73 (p1.distanceToLineSegmentBetweenSqd(p0, p2)) < tolSqd) {
michael@0 74 (*points)[0] = p2;
michael@0 75 *points += 1;
michael@0 76 return 1;
michael@0 77 }
michael@0 78
michael@0 79 GrPoint q[] = {
michael@0 80 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
michael@0 81 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
michael@0 82 };
michael@0 83 GrPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) };
michael@0 84
michael@0 85 pointsLeft >>= 1;
michael@0 86 uint32_t a = generateQuadraticPoints(p0, q[0], r, tolSqd, points, pointsLeft);
michael@0 87 uint32_t b = generateQuadraticPoints(r, q[1], p2, tolSqd, points, pointsLeft);
michael@0 88 return a + b;
michael@0 89 }
michael@0 90
michael@0 91 uint32_t GrPathUtils::cubicPointCount(const GrPoint points[],
michael@0 92 SkScalar tol) {
michael@0 93 if (tol < gMinCurveTol) {
michael@0 94 tol = gMinCurveTol;
michael@0 95 }
michael@0 96 SkASSERT(tol > 0);
michael@0 97
michael@0 98 SkScalar d = GrMax(
michael@0 99 points[1].distanceToLineSegmentBetweenSqd(points[0], points[3]),
michael@0 100 points[2].distanceToLineSegmentBetweenSqd(points[0], points[3]));
michael@0 101 d = SkScalarSqrt(d);
michael@0 102 if (d <= tol) {
michael@0 103 return 1;
michael@0 104 } else {
michael@0 105 int temp = SkScalarCeilToInt(SkScalarSqrt(SkScalarDiv(d, tol)));
michael@0 106 int pow2 = GrNextPow2(temp);
michael@0 107 // Because of NaNs & INFs we can wind up with a degenerate temp
michael@0 108 // such that pow2 comes out negative. Also, our point generator
michael@0 109 // will always output at least one pt.
michael@0 110 if (pow2 < 1) {
michael@0 111 pow2 = 1;
michael@0 112 }
michael@0 113 return GrMin(pow2, MAX_POINTS_PER_CURVE);
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 uint32_t GrPathUtils::generateCubicPoints(const GrPoint& p0,
michael@0 118 const GrPoint& p1,
michael@0 119 const GrPoint& p2,
michael@0 120 const GrPoint& p3,
michael@0 121 SkScalar tolSqd,
michael@0 122 GrPoint** points,
michael@0 123 uint32_t pointsLeft) {
michael@0 124 if (pointsLeft < 2 ||
michael@0 125 (p1.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd &&
michael@0 126 p2.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd)) {
michael@0 127 (*points)[0] = p3;
michael@0 128 *points += 1;
michael@0 129 return 1;
michael@0 130 }
michael@0 131 GrPoint q[] = {
michael@0 132 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
michael@0 133 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
michael@0 134 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
michael@0 135 };
michael@0 136 GrPoint r[] = {
michael@0 137 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
michael@0 138 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
michael@0 139 };
michael@0 140 GrPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
michael@0 141 pointsLeft >>= 1;
michael@0 142 uint32_t a = generateCubicPoints(p0, q[0], r[0], s, tolSqd, points, pointsLeft);
michael@0 143 uint32_t b = generateCubicPoints(s, r[1], q[2], p3, tolSqd, points, pointsLeft);
michael@0 144 return a + b;
michael@0 145 }
michael@0 146
michael@0 147 int GrPathUtils::worstCasePointCount(const SkPath& path, int* subpaths,
michael@0 148 SkScalar tol) {
michael@0 149 if (tol < gMinCurveTol) {
michael@0 150 tol = gMinCurveTol;
michael@0 151 }
michael@0 152 SkASSERT(tol > 0);
michael@0 153
michael@0 154 int pointCount = 0;
michael@0 155 *subpaths = 1;
michael@0 156
michael@0 157 bool first = true;
michael@0 158
michael@0 159 SkPath::Iter iter(path, false);
michael@0 160 SkPath::Verb verb;
michael@0 161
michael@0 162 GrPoint pts[4];
michael@0 163 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
michael@0 164
michael@0 165 switch (verb) {
michael@0 166 case SkPath::kLine_Verb:
michael@0 167 pointCount += 1;
michael@0 168 break;
michael@0 169 case SkPath::kQuad_Verb:
michael@0 170 pointCount += quadraticPointCount(pts, tol);
michael@0 171 break;
michael@0 172 case SkPath::kCubic_Verb:
michael@0 173 pointCount += cubicPointCount(pts, tol);
michael@0 174 break;
michael@0 175 case SkPath::kMove_Verb:
michael@0 176 pointCount += 1;
michael@0 177 if (!first) {
michael@0 178 ++(*subpaths);
michael@0 179 }
michael@0 180 break;
michael@0 181 default:
michael@0 182 break;
michael@0 183 }
michael@0 184 first = false;
michael@0 185 }
michael@0 186 return pointCount;
michael@0 187 }
michael@0 188
michael@0 189 void GrPathUtils::QuadUVMatrix::set(const GrPoint qPts[3]) {
michael@0 190 SkMatrix m;
michael@0 191 // We want M such that M * xy_pt = uv_pt
michael@0 192 // We know M * control_pts = [0 1/2 1]
michael@0 193 // [0 0 1]
michael@0 194 // [1 1 1]
michael@0 195 // And control_pts = [x0 x1 x2]
michael@0 196 // [y0 y1 y2]
michael@0 197 // [1 1 1 ]
michael@0 198 // We invert the control pt matrix and post concat to both sides to get M.
michael@0 199 // Using the known form of the control point matrix and the result, we can
michael@0 200 // optimize and improve precision.
michael@0 201
michael@0 202 double x0 = qPts[0].fX;
michael@0 203 double y0 = qPts[0].fY;
michael@0 204 double x1 = qPts[1].fX;
michael@0 205 double y1 = qPts[1].fY;
michael@0 206 double x2 = qPts[2].fX;
michael@0 207 double y2 = qPts[2].fY;
michael@0 208 double det = x0*y1 - y0*x1 + x2*y0 - y2*x0 + x1*y2 - y1*x2;
michael@0 209
michael@0 210 if (!sk_float_isfinite(det)
michael@0 211 || SkScalarNearlyZero((float)det, SK_ScalarNearlyZero * SK_ScalarNearlyZero)) {
michael@0 212 // The quad is degenerate. Hopefully this is rare. Find the pts that are
michael@0 213 // farthest apart to compute a line (unless it is really a pt).
michael@0 214 SkScalar maxD = qPts[0].distanceToSqd(qPts[1]);
michael@0 215 int maxEdge = 0;
michael@0 216 SkScalar d = qPts[1].distanceToSqd(qPts[2]);
michael@0 217 if (d > maxD) {
michael@0 218 maxD = d;
michael@0 219 maxEdge = 1;
michael@0 220 }
michael@0 221 d = qPts[2].distanceToSqd(qPts[0]);
michael@0 222 if (d > maxD) {
michael@0 223 maxD = d;
michael@0 224 maxEdge = 2;
michael@0 225 }
michael@0 226 // We could have a tolerance here, not sure if it would improve anything
michael@0 227 if (maxD > 0) {
michael@0 228 // Set the matrix to give (u = 0, v = distance_to_line)
michael@0 229 GrVec lineVec = qPts[(maxEdge + 1)%3] - qPts[maxEdge];
michael@0 230 // when looking from the point 0 down the line we want positive
michael@0 231 // distances to be to the left. This matches the non-degenerate
michael@0 232 // case.
michael@0 233 lineVec.setOrthog(lineVec, GrPoint::kLeft_Side);
michael@0 234 lineVec.dot(qPts[0]);
michael@0 235 // first row
michael@0 236 fM[0] = 0;
michael@0 237 fM[1] = 0;
michael@0 238 fM[2] = 0;
michael@0 239 // second row
michael@0 240 fM[3] = lineVec.fX;
michael@0 241 fM[4] = lineVec.fY;
michael@0 242 fM[5] = -lineVec.dot(qPts[maxEdge]);
michael@0 243 } else {
michael@0 244 // It's a point. It should cover zero area. Just set the matrix such
michael@0 245 // that (u, v) will always be far away from the quad.
michael@0 246 fM[0] = 0; fM[1] = 0; fM[2] = 100.f;
michael@0 247 fM[3] = 0; fM[4] = 0; fM[5] = 100.f;
michael@0 248 }
michael@0 249 } else {
michael@0 250 double scale = 1.0/det;
michael@0 251
michael@0 252 // compute adjugate matrix
michael@0 253 double a0, a1, a2, a3, a4, a5, a6, a7, a8;
michael@0 254 a0 = y1-y2;
michael@0 255 a1 = x2-x1;
michael@0 256 a2 = x1*y2-x2*y1;
michael@0 257
michael@0 258 a3 = y2-y0;
michael@0 259 a4 = x0-x2;
michael@0 260 a5 = x2*y0-x0*y2;
michael@0 261
michael@0 262 a6 = y0-y1;
michael@0 263 a7 = x1-x0;
michael@0 264 a8 = x0*y1-x1*y0;
michael@0 265
michael@0 266 // this performs the uv_pts*adjugate(control_pts) multiply,
michael@0 267 // then does the scale by 1/det afterwards to improve precision
michael@0 268 m[SkMatrix::kMScaleX] = (float)((0.5*a3 + a6)*scale);
michael@0 269 m[SkMatrix::kMSkewX] = (float)((0.5*a4 + a7)*scale);
michael@0 270 m[SkMatrix::kMTransX] = (float)((0.5*a5 + a8)*scale);
michael@0 271
michael@0 272 m[SkMatrix::kMSkewY] = (float)(a6*scale);
michael@0 273 m[SkMatrix::kMScaleY] = (float)(a7*scale);
michael@0 274 m[SkMatrix::kMTransY] = (float)(a8*scale);
michael@0 275
michael@0 276 m[SkMatrix::kMPersp0] = (float)((a0 + a3 + a6)*scale);
michael@0 277 m[SkMatrix::kMPersp1] = (float)((a1 + a4 + a7)*scale);
michael@0 278 m[SkMatrix::kMPersp2] = (float)((a2 + a5 + a8)*scale);
michael@0 279
michael@0 280 // The matrix should not have perspective.
michael@0 281 SkDEBUGCODE(static const SkScalar gTOL = 1.f / 100.f);
michael@0 282 SkASSERT(SkScalarAbs(m.get(SkMatrix::kMPersp0)) < gTOL);
michael@0 283 SkASSERT(SkScalarAbs(m.get(SkMatrix::kMPersp1)) < gTOL);
michael@0 284
michael@0 285 // It may not be normalized to have 1.0 in the bottom right
michael@0 286 float m33 = m.get(SkMatrix::kMPersp2);
michael@0 287 if (1.f != m33) {
michael@0 288 m33 = 1.f / m33;
michael@0 289 fM[0] = m33 * m.get(SkMatrix::kMScaleX);
michael@0 290 fM[1] = m33 * m.get(SkMatrix::kMSkewX);
michael@0 291 fM[2] = m33 * m.get(SkMatrix::kMTransX);
michael@0 292 fM[3] = m33 * m.get(SkMatrix::kMSkewY);
michael@0 293 fM[4] = m33 * m.get(SkMatrix::kMScaleY);
michael@0 294 fM[5] = m33 * m.get(SkMatrix::kMTransY);
michael@0 295 } else {
michael@0 296 fM[0] = m.get(SkMatrix::kMScaleX);
michael@0 297 fM[1] = m.get(SkMatrix::kMSkewX);
michael@0 298 fM[2] = m.get(SkMatrix::kMTransX);
michael@0 299 fM[3] = m.get(SkMatrix::kMSkewY);
michael@0 300 fM[4] = m.get(SkMatrix::kMScaleY);
michael@0 301 fM[5] = m.get(SkMatrix::kMTransY);
michael@0 302 }
michael@0 303 }
michael@0 304 }
michael@0 305
michael@0 306 ////////////////////////////////////////////////////////////////////////////////
michael@0 307
michael@0 308 // k = (y2 - y0, x0 - x2, (x2 - x0)*y0 - (y2 - y0)*x0 )
michael@0 309 // l = (2*w * (y1 - y0), 2*w * (x0 - x1), 2*w * (x1*y0 - x0*y1))
michael@0 310 // m = (2*w * (y2 - y1), 2*w * (x1 - x2), 2*w * (x2*y1 - x1*y2))
michael@0 311 void GrPathUtils::getConicKLM(const SkPoint p[3], const SkScalar weight, SkScalar klm[9]) {
michael@0 312 const SkScalar w2 = 2.f * weight;
michael@0 313 klm[0] = p[2].fY - p[0].fY;
michael@0 314 klm[1] = p[0].fX - p[2].fX;
michael@0 315 klm[2] = (p[2].fX - p[0].fX) * p[0].fY - (p[2].fY - p[0].fY) * p[0].fX;
michael@0 316
michael@0 317 klm[3] = w2 * (p[1].fY - p[0].fY);
michael@0 318 klm[4] = w2 * (p[0].fX - p[1].fX);
michael@0 319 klm[5] = w2 * (p[1].fX * p[0].fY - p[0].fX * p[1].fY);
michael@0 320
michael@0 321 klm[6] = w2 * (p[2].fY - p[1].fY);
michael@0 322 klm[7] = w2 * (p[1].fX - p[2].fX);
michael@0 323 klm[8] = w2 * (p[2].fX * p[1].fY - p[1].fX * p[2].fY);
michael@0 324
michael@0 325 // scale the max absolute value of coeffs to 10
michael@0 326 SkScalar scale = 0.f;
michael@0 327 for (int i = 0; i < 9; ++i) {
michael@0 328 scale = SkMaxScalar(scale, SkScalarAbs(klm[i]));
michael@0 329 }
michael@0 330 SkASSERT(scale > 0.f);
michael@0 331 scale = 10.f / scale;
michael@0 332 for (int i = 0; i < 9; ++i) {
michael@0 333 klm[i] *= scale;
michael@0 334 }
michael@0 335 }
michael@0 336
michael@0 337 ////////////////////////////////////////////////////////////////////////////////
michael@0 338
michael@0 339 namespace {
michael@0 340
michael@0 341 // a is the first control point of the cubic.
michael@0 342 // ab is the vector from a to the second control point.
michael@0 343 // dc is the vector from the fourth to the third control point.
michael@0 344 // d is the fourth control point.
michael@0 345 // p is the candidate quadratic control point.
michael@0 346 // this assumes that the cubic doesn't inflect and is simple
michael@0 347 bool is_point_within_cubic_tangents(const SkPoint& a,
michael@0 348 const SkVector& ab,
michael@0 349 const SkVector& dc,
michael@0 350 const SkPoint& d,
michael@0 351 SkPath::Direction dir,
michael@0 352 const SkPoint p) {
michael@0 353 SkVector ap = p - a;
michael@0 354 SkScalar apXab = ap.cross(ab);
michael@0 355 if (SkPath::kCW_Direction == dir) {
michael@0 356 if (apXab > 0) {
michael@0 357 return false;
michael@0 358 }
michael@0 359 } else {
michael@0 360 SkASSERT(SkPath::kCCW_Direction == dir);
michael@0 361 if (apXab < 0) {
michael@0 362 return false;
michael@0 363 }
michael@0 364 }
michael@0 365
michael@0 366 SkVector dp = p - d;
michael@0 367 SkScalar dpXdc = dp.cross(dc);
michael@0 368 if (SkPath::kCW_Direction == dir) {
michael@0 369 if (dpXdc < 0) {
michael@0 370 return false;
michael@0 371 }
michael@0 372 } else {
michael@0 373 SkASSERT(SkPath::kCCW_Direction == dir);
michael@0 374 if (dpXdc > 0) {
michael@0 375 return false;
michael@0 376 }
michael@0 377 }
michael@0 378 return true;
michael@0 379 }
michael@0 380
michael@0 381 void convert_noninflect_cubic_to_quads(const SkPoint p[4],
michael@0 382 SkScalar toleranceSqd,
michael@0 383 bool constrainWithinTangents,
michael@0 384 SkPath::Direction dir,
michael@0 385 SkTArray<SkPoint, true>* quads,
michael@0 386 int sublevel = 0) {
michael@0 387
michael@0 388 // Notation: Point a is always p[0]. Point b is p[1] unless p[1] == p[0], in which case it is
michael@0 389 // 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].
michael@0 390
michael@0 391 SkVector ab = p[1] - p[0];
michael@0 392 SkVector dc = p[2] - p[3];
michael@0 393
michael@0 394 if (ab.isZero()) {
michael@0 395 if (dc.isZero()) {
michael@0 396 SkPoint* degQuad = quads->push_back_n(3);
michael@0 397 degQuad[0] = p[0];
michael@0 398 degQuad[1] = p[0];
michael@0 399 degQuad[2] = p[3];
michael@0 400 return;
michael@0 401 }
michael@0 402 ab = p[2] - p[0];
michael@0 403 }
michael@0 404 if (dc.isZero()) {
michael@0 405 dc = p[1] - p[3];
michael@0 406 }
michael@0 407
michael@0 408 // When the ab and cd tangents are nearly parallel with vector from d to a the constraint that
michael@0 409 // the quad point falls between the tangents becomes hard to enforce and we are likely to hit
michael@0 410 // the max subdivision count. However, in this case the cubic is approaching a line and the
michael@0 411 // accuracy of the quad point isn't so important. We check if the two middle cubic control
michael@0 412 // points are very close to the baseline vector. If so then we just pick quadratic points on the
michael@0 413 // control polygon.
michael@0 414
michael@0 415 if (constrainWithinTangents) {
michael@0 416 SkVector da = p[0] - p[3];
michael@0 417 SkScalar invDALengthSqd = da.lengthSqd();
michael@0 418 if (invDALengthSqd > SK_ScalarNearlyZero) {
michael@0 419 invDALengthSqd = SkScalarInvert(invDALengthSqd);
michael@0 420 // cross(ab, da)^2/length(da)^2 == sqd distance from b to line from d to a.
michael@0 421 // same goed for point c using vector cd.
michael@0 422 SkScalar detABSqd = ab.cross(da);
michael@0 423 detABSqd = SkScalarSquare(detABSqd);
michael@0 424 SkScalar detDCSqd = dc.cross(da);
michael@0 425 detDCSqd = SkScalarSquare(detDCSqd);
michael@0 426 if (SkScalarMul(detABSqd, invDALengthSqd) < toleranceSqd &&
michael@0 427 SkScalarMul(detDCSqd, invDALengthSqd) < toleranceSqd) {
michael@0 428 SkPoint b = p[0] + ab;
michael@0 429 SkPoint c = p[3] + dc;
michael@0 430 SkPoint mid = b + c;
michael@0 431 mid.scale(SK_ScalarHalf);
michael@0 432 // Insert two quadratics to cover the case when ab points away from d and/or dc
michael@0 433 // points away from a.
michael@0 434 if (SkVector::DotProduct(da, dc) < 0 || SkVector::DotProduct(ab,da) > 0) {
michael@0 435 SkPoint* qpts = quads->push_back_n(6);
michael@0 436 qpts[0] = p[0];
michael@0 437 qpts[1] = b;
michael@0 438 qpts[2] = mid;
michael@0 439 qpts[3] = mid;
michael@0 440 qpts[4] = c;
michael@0 441 qpts[5] = p[3];
michael@0 442 } else {
michael@0 443 SkPoint* qpts = quads->push_back_n(3);
michael@0 444 qpts[0] = p[0];
michael@0 445 qpts[1] = mid;
michael@0 446 qpts[2] = p[3];
michael@0 447 }
michael@0 448 return;
michael@0 449 }
michael@0 450 }
michael@0 451 }
michael@0 452
michael@0 453 static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2;
michael@0 454 static const int kMaxSubdivs = 10;
michael@0 455
michael@0 456 ab.scale(kLengthScale);
michael@0 457 dc.scale(kLengthScale);
michael@0 458
michael@0 459 // e0 and e1 are extrapolations along vectors ab and dc.
michael@0 460 SkVector c0 = p[0];
michael@0 461 c0 += ab;
michael@0 462 SkVector c1 = p[3];
michael@0 463 c1 += dc;
michael@0 464
michael@0 465 SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : c0.distanceToSqd(c1);
michael@0 466 if (dSqd < toleranceSqd) {
michael@0 467 SkPoint cAvg = c0;
michael@0 468 cAvg += c1;
michael@0 469 cAvg.scale(SK_ScalarHalf);
michael@0 470
michael@0 471 bool subdivide = false;
michael@0 472
michael@0 473 if (constrainWithinTangents &&
michael@0 474 !is_point_within_cubic_tangents(p[0], ab, dc, p[3], dir, cAvg)) {
michael@0 475 // choose a new cAvg that is the intersection of the two tangent lines.
michael@0 476 ab.setOrthog(ab);
michael@0 477 SkScalar z0 = -ab.dot(p[0]);
michael@0 478 dc.setOrthog(dc);
michael@0 479 SkScalar z1 = -dc.dot(p[3]);
michael@0 480 cAvg.fX = SkScalarMul(ab.fY, z1) - SkScalarMul(z0, dc.fY);
michael@0 481 cAvg.fY = SkScalarMul(z0, dc.fX) - SkScalarMul(ab.fX, z1);
michael@0 482 SkScalar z = SkScalarMul(ab.fX, dc.fY) - SkScalarMul(ab.fY, dc.fX);
michael@0 483 z = SkScalarInvert(z);
michael@0 484 cAvg.fX *= z;
michael@0 485 cAvg.fY *= z;
michael@0 486 if (sublevel <= kMaxSubdivs) {
michael@0 487 SkScalar d0Sqd = c0.distanceToSqd(cAvg);
michael@0 488 SkScalar d1Sqd = c1.distanceToSqd(cAvg);
michael@0 489 // We need to subdivide if d0 + d1 > tolerance but we have the sqd values. We know
michael@0 490 // the distances and tolerance can't be negative.
michael@0 491 // (d0 + d1)^2 > toleranceSqd
michael@0 492 // d0Sqd + 2*d0*d1 + d1Sqd > toleranceSqd
michael@0 493 SkScalar d0d1 = SkScalarSqrt(SkScalarMul(d0Sqd, d1Sqd));
michael@0 494 subdivide = 2 * d0d1 + d0Sqd + d1Sqd > toleranceSqd;
michael@0 495 }
michael@0 496 }
michael@0 497 if (!subdivide) {
michael@0 498 SkPoint* pts = quads->push_back_n(3);
michael@0 499 pts[0] = p[0];
michael@0 500 pts[1] = cAvg;
michael@0 501 pts[2] = p[3];
michael@0 502 return;
michael@0 503 }
michael@0 504 }
michael@0 505 SkPoint choppedPts[7];
michael@0 506 SkChopCubicAtHalf(p, choppedPts);
michael@0 507 convert_noninflect_cubic_to_quads(choppedPts + 0,
michael@0 508 toleranceSqd,
michael@0 509 constrainWithinTangents,
michael@0 510 dir,
michael@0 511 quads,
michael@0 512 sublevel + 1);
michael@0 513 convert_noninflect_cubic_to_quads(choppedPts + 3,
michael@0 514 toleranceSqd,
michael@0 515 constrainWithinTangents,
michael@0 516 dir,
michael@0 517 quads,
michael@0 518 sublevel + 1);
michael@0 519 }
michael@0 520 }
michael@0 521
michael@0 522 void GrPathUtils::convertCubicToQuads(const GrPoint p[4],
michael@0 523 SkScalar tolScale,
michael@0 524 bool constrainWithinTangents,
michael@0 525 SkPath::Direction dir,
michael@0 526 SkTArray<SkPoint, true>* quads) {
michael@0 527 SkPoint chopped[10];
michael@0 528 int count = SkChopCubicAtInflections(p, chopped);
michael@0 529
michael@0 530 // base tolerance is 1 pixel.
michael@0 531 static const SkScalar kTolerance = SK_Scalar1;
michael@0 532 const SkScalar tolSqd = SkScalarSquare(SkScalarMul(tolScale, kTolerance));
michael@0 533
michael@0 534 for (int i = 0; i < count; ++i) {
michael@0 535 SkPoint* cubic = chopped + 3*i;
michael@0 536 convert_noninflect_cubic_to_quads(cubic, tolSqd, constrainWithinTangents, dir, quads);
michael@0 537 }
michael@0 538
michael@0 539 }
michael@0 540
michael@0 541 ////////////////////////////////////////////////////////////////////////////////
michael@0 542
michael@0 543 enum CubicType {
michael@0 544 kSerpentine_CubicType,
michael@0 545 kCusp_CubicType,
michael@0 546 kLoop_CubicType,
michael@0 547 kQuadratic_CubicType,
michael@0 548 kLine_CubicType,
michael@0 549 kPoint_CubicType
michael@0 550 };
michael@0 551
michael@0 552 // discr(I) = d0^2 * (3*d1^2 - 4*d0*d2)
michael@0 553 // Classification:
michael@0 554 // discr(I) > 0 Serpentine
michael@0 555 // discr(I) = 0 Cusp
michael@0 556 // discr(I) < 0 Loop
michael@0 557 // d0 = d1 = 0 Quadratic
michael@0 558 // d0 = d1 = d2 = 0 Line
michael@0 559 // p0 = p1 = p2 = p3 Point
michael@0 560 static CubicType classify_cubic(const SkPoint p[4], const SkScalar d[3]) {
michael@0 561 if (p[0] == p[1] && p[0] == p[2] && p[0] == p[3]) {
michael@0 562 return kPoint_CubicType;
michael@0 563 }
michael@0 564 const SkScalar discr = d[0] * d[0] * (3.f * d[1] * d[1] - 4.f * d[0] * d[2]);
michael@0 565 if (discr > SK_ScalarNearlyZero) {
michael@0 566 return kSerpentine_CubicType;
michael@0 567 } else if (discr < -SK_ScalarNearlyZero) {
michael@0 568 return kLoop_CubicType;
michael@0 569 } else {
michael@0 570 if (0.f == d[0] && 0.f == d[1]) {
michael@0 571 return (0.f == d[2] ? kLine_CubicType : kQuadratic_CubicType);
michael@0 572 } else {
michael@0 573 return kCusp_CubicType;
michael@0 574 }
michael@0 575 }
michael@0 576 }
michael@0 577
michael@0 578 // Assumes the third component of points is 1.
michael@0 579 // Calcs p0 . (p1 x p2)
michael@0 580 static SkScalar calc_dot_cross_cubic(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) {
michael@0 581 const SkScalar xComp = p0.fX * (p1.fY - p2.fY);
michael@0 582 const SkScalar yComp = p0.fY * (p2.fX - p1.fX);
michael@0 583 const SkScalar wComp = p1.fX * p2.fY - p1.fY * p2.fX;
michael@0 584 return (xComp + yComp + wComp);
michael@0 585 }
michael@0 586
michael@0 587 // Solves linear system to extract klm
michael@0 588 // P.K = k (similarly for l, m)
michael@0 589 // Where P is matrix of control points
michael@0 590 // K is coefficients for the line K
michael@0 591 // k is vector of values of K evaluated at the control points
michael@0 592 // Solving for K, thus K = P^(-1) . k
michael@0 593 static void calc_cubic_klm(const SkPoint p[4], const SkScalar controlK[4],
michael@0 594 const SkScalar controlL[4], const SkScalar controlM[4],
michael@0 595 SkScalar k[3], SkScalar l[3], SkScalar m[3]) {
michael@0 596 SkMatrix matrix;
michael@0 597 matrix.setAll(p[0].fX, p[0].fY, 1.f,
michael@0 598 p[1].fX, p[1].fY, 1.f,
michael@0 599 p[2].fX, p[2].fY, 1.f);
michael@0 600 SkMatrix inverse;
michael@0 601 if (matrix.invert(&inverse)) {
michael@0 602 inverse.mapHomogeneousPoints(k, controlK, 1);
michael@0 603 inverse.mapHomogeneousPoints(l, controlL, 1);
michael@0 604 inverse.mapHomogeneousPoints(m, controlM, 1);
michael@0 605 }
michael@0 606
michael@0 607 }
michael@0 608
michael@0 609 static void set_serp_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) {
michael@0 610 SkScalar tempSqrt = SkScalarSqrt(9.f * d[1] * d[1] - 12.f * d[0] * d[2]);
michael@0 611 SkScalar ls = 3.f * d[1] - tempSqrt;
michael@0 612 SkScalar lt = 6.f * d[0];
michael@0 613 SkScalar ms = 3.f * d[1] + tempSqrt;
michael@0 614 SkScalar mt = 6.f * d[0];
michael@0 615
michael@0 616 k[0] = ls * ms;
michael@0 617 k[1] = (3.f * ls * ms - ls * mt - lt * ms) / 3.f;
michael@0 618 k[2] = (lt * (mt - 2.f * ms) + ls * (3.f * ms - 2.f * mt)) / 3.f;
michael@0 619 k[3] = (lt - ls) * (mt - ms);
michael@0 620
michael@0 621 l[0] = ls * ls * ls;
michael@0 622 const SkScalar lt_ls = lt - ls;
michael@0 623 l[1] = ls * ls * lt_ls * -1.f;
michael@0 624 l[2] = lt_ls * lt_ls * ls;
michael@0 625 l[3] = -1.f * lt_ls * lt_ls * lt_ls;
michael@0 626
michael@0 627 m[0] = ms * ms * ms;
michael@0 628 const SkScalar mt_ms = mt - ms;
michael@0 629 m[1] = ms * ms * mt_ms * -1.f;
michael@0 630 m[2] = mt_ms * mt_ms * ms;
michael@0 631 m[3] = -1.f * mt_ms * mt_ms * mt_ms;
michael@0 632
michael@0 633 // If d0 < 0 we need to flip the orientation of our curve
michael@0 634 // This is done by negating the k and l values
michael@0 635 // We want negative distance values to be on the inside
michael@0 636 if ( d[0] > 0) {
michael@0 637 for (int i = 0; i < 4; ++i) {
michael@0 638 k[i] = -k[i];
michael@0 639 l[i] = -l[i];
michael@0 640 }
michael@0 641 }
michael@0 642 }
michael@0 643
michael@0 644 static void set_loop_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) {
michael@0 645 SkScalar tempSqrt = SkScalarSqrt(4.f * d[0] * d[2] - 3.f * d[1] * d[1]);
michael@0 646 SkScalar ls = d[1] - tempSqrt;
michael@0 647 SkScalar lt = 2.f * d[0];
michael@0 648 SkScalar ms = d[1] + tempSqrt;
michael@0 649 SkScalar mt = 2.f * d[0];
michael@0 650
michael@0 651 k[0] = ls * ms;
michael@0 652 k[1] = (3.f * ls*ms - ls * mt - lt * ms) / 3.f;
michael@0 653 k[2] = (lt * (mt - 2.f * ms) + ls * (3.f * ms - 2.f * mt)) / 3.f;
michael@0 654 k[3] = (lt - ls) * (mt - ms);
michael@0 655
michael@0 656 l[0] = ls * ls * ms;
michael@0 657 l[1] = (ls * (ls * (mt - 3.f * ms) + 2.f * lt * ms))/-3.f;
michael@0 658 l[2] = ((lt - ls) * (ls * (2.f * mt - 3.f * ms) + lt * ms))/3.f;
michael@0 659 l[3] = -1.f * (lt - ls) * (lt - ls) * (mt - ms);
michael@0 660
michael@0 661 m[0] = ls * ms * ms;
michael@0 662 m[1] = (ms * (ls * (2.f * mt - 3.f * ms) + lt * ms))/-3.f;
michael@0 663 m[2] = ((mt - ms) * (ls * (mt - 3.f * ms) + 2.f * lt * ms))/3.f;
michael@0 664 m[3] = -1.f * (lt - ls) * (mt - ms) * (mt - ms);
michael@0 665
michael@0 666
michael@0 667 // If (d0 < 0 && sign(k1) > 0) || (d0 > 0 && sign(k1) < 0),
michael@0 668 // we need to flip the orientation of our curve.
michael@0 669 // This is done by negating the k and l values
michael@0 670 if ( (d[0] < 0 && k[1] > 0) || (d[0] > 0 && k[1] < 0)) {
michael@0 671 for (int i = 0; i < 4; ++i) {
michael@0 672 k[i] = -k[i];
michael@0 673 l[i] = -l[i];
michael@0 674 }
michael@0 675 }
michael@0 676 }
michael@0 677
michael@0 678 static void set_cusp_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) {
michael@0 679 const SkScalar ls = d[2];
michael@0 680 const SkScalar lt = 3.f * d[1];
michael@0 681
michael@0 682 k[0] = ls;
michael@0 683 k[1] = ls - lt / 3.f;
michael@0 684 k[2] = ls - 2.f * lt / 3.f;
michael@0 685 k[3] = ls - lt;
michael@0 686
michael@0 687 l[0] = ls * ls * ls;
michael@0 688 const SkScalar ls_lt = ls - lt;
michael@0 689 l[1] = ls * ls * ls_lt;
michael@0 690 l[2] = ls_lt * ls_lt * ls;
michael@0 691 l[3] = ls_lt * ls_lt * ls_lt;
michael@0 692
michael@0 693 m[0] = 1.f;
michael@0 694 m[1] = 1.f;
michael@0 695 m[2] = 1.f;
michael@0 696 m[3] = 1.f;
michael@0 697 }
michael@0 698
michael@0 699 // For the case when a cubic is actually a quadratic
michael@0 700 // M =
michael@0 701 // 0 0 0
michael@0 702 // 1/3 0 1/3
michael@0 703 // 2/3 1/3 2/3
michael@0 704 // 1 1 1
michael@0 705 static void set_quadratic_klm(const SkScalar d[3], SkScalar k[4], SkScalar l[4], SkScalar m[4]) {
michael@0 706 k[0] = 0.f;
michael@0 707 k[1] = 1.f/3.f;
michael@0 708 k[2] = 2.f/3.f;
michael@0 709 k[3] = 1.f;
michael@0 710
michael@0 711 l[0] = 0.f;
michael@0 712 l[1] = 0.f;
michael@0 713 l[2] = 1.f/3.f;
michael@0 714 l[3] = 1.f;
michael@0 715
michael@0 716 m[0] = 0.f;
michael@0 717 m[1] = 1.f/3.f;
michael@0 718 m[2] = 2.f/3.f;
michael@0 719 m[3] = 1.f;
michael@0 720
michael@0 721 // If d2 < 0 we need to flip the orientation of our curve
michael@0 722 // This is done by negating the k and l values
michael@0 723 if ( d[2] > 0) {
michael@0 724 for (int i = 0; i < 4; ++i) {
michael@0 725 k[i] = -k[i];
michael@0 726 l[i] = -l[i];
michael@0 727 }
michael@0 728 }
michael@0 729 }
michael@0 730
michael@0 731 // Calc coefficients of I(s,t) where roots of I are inflection points of curve
michael@0 732 // I(s,t) = t*(3*d0*s^2 - 3*d1*s*t + d2*t^2)
michael@0 733 // d0 = a1 - 2*a2+3*a3
michael@0 734 // d1 = -a2 + 3*a3
michael@0 735 // d2 = 3*a3
michael@0 736 // a1 = p0 . (p3 x p2)
michael@0 737 // a2 = p1 . (p0 x p3)
michael@0 738 // a3 = p2 . (p1 x p0)
michael@0 739 // Places the values of d1, d2, d3 in array d passed in
michael@0 740 static void calc_cubic_inflection_func(const SkPoint p[4], SkScalar d[3]) {
michael@0 741 SkScalar a1 = calc_dot_cross_cubic(p[0], p[3], p[2]);
michael@0 742 SkScalar a2 = calc_dot_cross_cubic(p[1], p[0], p[3]);
michael@0 743 SkScalar a3 = calc_dot_cross_cubic(p[2], p[1], p[0]);
michael@0 744
michael@0 745 // need to scale a's or values in later calculations will grow to high
michael@0 746 SkScalar max = SkScalarAbs(a1);
michael@0 747 max = SkMaxScalar(max, SkScalarAbs(a2));
michael@0 748 max = SkMaxScalar(max, SkScalarAbs(a3));
michael@0 749 max = 1.f/max;
michael@0 750 a1 = a1 * max;
michael@0 751 a2 = a2 * max;
michael@0 752 a3 = a3 * max;
michael@0 753
michael@0 754 d[2] = 3.f * a3;
michael@0 755 d[1] = d[2] - a2;
michael@0 756 d[0] = d[1] - a2 + a1;
michael@0 757 }
michael@0 758
michael@0 759 int GrPathUtils::chopCubicAtLoopIntersection(const SkPoint src[4], SkPoint dst[10], SkScalar klm[9],
michael@0 760 SkScalar klm_rev[3]) {
michael@0 761 // Variable to store the two parametric values at the loop double point
michael@0 762 SkScalar smallS = 0.f;
michael@0 763 SkScalar largeS = 0.f;
michael@0 764
michael@0 765 SkScalar d[3];
michael@0 766 calc_cubic_inflection_func(src, d);
michael@0 767
michael@0 768 CubicType cType = classify_cubic(src, d);
michael@0 769
michael@0 770 int chop_count = 0;
michael@0 771 if (kLoop_CubicType == cType) {
michael@0 772 SkScalar tempSqrt = SkScalarSqrt(4.f * d[0] * d[2] - 3.f * d[1] * d[1]);
michael@0 773 SkScalar ls = d[1] - tempSqrt;
michael@0 774 SkScalar lt = 2.f * d[0];
michael@0 775 SkScalar ms = d[1] + tempSqrt;
michael@0 776 SkScalar mt = 2.f * d[0];
michael@0 777 ls = ls / lt;
michael@0 778 ms = ms / mt;
michael@0 779 // need to have t values sorted since this is what is expected by SkChopCubicAt
michael@0 780 if (ls <= ms) {
michael@0 781 smallS = ls;
michael@0 782 largeS = ms;
michael@0 783 } else {
michael@0 784 smallS = ms;
michael@0 785 largeS = ls;
michael@0 786 }
michael@0 787
michael@0 788 SkScalar chop_ts[2];
michael@0 789 if (smallS > 0.f && smallS < 1.f) {
michael@0 790 chop_ts[chop_count++] = smallS;
michael@0 791 }
michael@0 792 if (largeS > 0.f && largeS < 1.f) {
michael@0 793 chop_ts[chop_count++] = largeS;
michael@0 794 }
michael@0 795 if(dst) {
michael@0 796 SkChopCubicAt(src, dst, chop_ts, chop_count);
michael@0 797 }
michael@0 798 } else {
michael@0 799 if (dst) {
michael@0 800 memcpy(dst, src, sizeof(SkPoint) * 4);
michael@0 801 }
michael@0 802 }
michael@0 803
michael@0 804 if (klm && klm_rev) {
michael@0 805 // Set klm_rev to to match the sub_section of cubic that needs to have its orientation
michael@0 806 // flipped. This will always be the section that is the "loop"
michael@0 807 if (2 == chop_count) {
michael@0 808 klm_rev[0] = 1.f;
michael@0 809 klm_rev[1] = -1.f;
michael@0 810 klm_rev[2] = 1.f;
michael@0 811 } else if (1 == chop_count) {
michael@0 812 if (smallS < 0.f) {
michael@0 813 klm_rev[0] = -1.f;
michael@0 814 klm_rev[1] = 1.f;
michael@0 815 } else {
michael@0 816 klm_rev[0] = 1.f;
michael@0 817 klm_rev[1] = -1.f;
michael@0 818 }
michael@0 819 } else {
michael@0 820 if (smallS < 0.f && largeS > 1.f) {
michael@0 821 klm_rev[0] = -1.f;
michael@0 822 } else {
michael@0 823 klm_rev[0] = 1.f;
michael@0 824 }
michael@0 825 }
michael@0 826 SkScalar controlK[4];
michael@0 827 SkScalar controlL[4];
michael@0 828 SkScalar controlM[4];
michael@0 829
michael@0 830 if (kSerpentine_CubicType == cType || (kCusp_CubicType == cType && 0.f != d[0])) {
michael@0 831 set_serp_klm(d, controlK, controlL, controlM);
michael@0 832 } else if (kLoop_CubicType == cType) {
michael@0 833 set_loop_klm(d, controlK, controlL, controlM);
michael@0 834 } else if (kCusp_CubicType == cType) {
michael@0 835 SkASSERT(0.f == d[0]);
michael@0 836 set_cusp_klm(d, controlK, controlL, controlM);
michael@0 837 } else if (kQuadratic_CubicType == cType) {
michael@0 838 set_quadratic_klm(d, controlK, controlL, controlM);
michael@0 839 }
michael@0 840
michael@0 841 calc_cubic_klm(src, controlK, controlL, controlM, klm, &klm[3], &klm[6]);
michael@0 842 }
michael@0 843 return chop_count + 1;
michael@0 844 }
michael@0 845
michael@0 846 void GrPathUtils::getCubicKLM(const SkPoint p[4], SkScalar klm[9]) {
michael@0 847 SkScalar d[3];
michael@0 848 calc_cubic_inflection_func(p, d);
michael@0 849
michael@0 850 CubicType cType = classify_cubic(p, d);
michael@0 851
michael@0 852 SkScalar controlK[4];
michael@0 853 SkScalar controlL[4];
michael@0 854 SkScalar controlM[4];
michael@0 855
michael@0 856 if (kSerpentine_CubicType == cType || (kCusp_CubicType == cType && 0.f != d[0])) {
michael@0 857 set_serp_klm(d, controlK, controlL, controlM);
michael@0 858 } else if (kLoop_CubicType == cType) {
michael@0 859 set_loop_klm(d, controlK, controlL, controlM);
michael@0 860 } else if (kCusp_CubicType == cType) {
michael@0 861 SkASSERT(0.f == d[0]);
michael@0 862 set_cusp_klm(d, controlK, controlL, controlM);
michael@0 863 } else if (kQuadratic_CubicType == cType) {
michael@0 864 set_quadratic_klm(d, controlK, controlL, controlM);
michael@0 865 }
michael@0 866
michael@0 867 calc_cubic_klm(p, controlK, controlL, controlM, klm, &klm[3], &klm[6]);
michael@0 868 }

mercurial