Debian Codegraph @

src/gpu/ganesh/geometry/GrPathUtils.cpp

raw
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/ganesh/geometry/GrPathUtils.h"
9
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkRect.h"
12 #include "include/private/base/SkAssert.h"
13 #include "include/private/base/SkFloatingPoint.h"
14 #include "include/private/base/SkPoint_impl.h"
15 #include "src/core/SkGeometry.h"
16 #include "src/core/SkPathEnums.h"
17 #include "src/core/SkPointPriv.h"
18 #include "src/gpu/tessellate/WangsFormula.h"
19
20 #include <algorithm>
21
22 using namespace skia_private;
23
24 static const SkScalar kMinCurveTol = 0.0001f;
25
26 static float tolerance_to_wangs_precision(float srcTol) {
27 // You should have called scaleToleranceToSrc, which guarantees this
28 SkASSERT(srcTol >= kMinCurveTol);
29
30 // The GrPathUtil API defines tolerance as the max distance the linear segment can be from
31 // the real curve. Wang's formula guarantees the linear segments will be within 1/precision
32 // of the true curve, so precision = 1/srcTol
33 return 1.f / srcTol;
34 }
35
36 uint32_t max_bezier_vertices(uint32_t chopCount) {
37 static constexpr uint32_t kMaxChopsPerCurve = 10;
38 static_assert((1 << kMaxChopsPerCurve) == GrPathUtils::kMaxPointsPerCurve);
39 return 1 << std::min(chopCount, kMaxChopsPerCurve);
40 }
41
42 SkScalar GrPathUtils::scaleToleranceToSrc(SkScalar devTol,
43 const SkMatrix& viewM,
44 const SkRect& pathBounds) {
45 // In order to tesselate the path we get a bound on how much the matrix can
46 // scale when mapping to screen coordinates.
47 SkScalar stretch = viewM.getMaxScale();
48
49 if (stretch < 0) {
50 // take worst case mapRadius amoung four corners.
51 // (less than perfect)
52 for (int i = 0; i < 4; ++i) {
53 SkMatrix mat;
54 mat.setTranslate((i % 2) ? pathBounds.fLeft : pathBounds.fRight,
55 (i < 2) ? pathBounds.fTop : pathBounds.fBottom);
56 mat.postConcat(viewM);
57 stretch = std::max(stretch, mat.mapRadius(SK_Scalar1));
58 }
59 }
60 SkScalar srcTol = 0;
61 if (stretch <= 0) {
62 // We have degenerate bounds or some degenerate matrix. Thus we set the tolerance to be the
63 // max of the path pathBounds width and height.
64 srcTol = std::max(pathBounds.width(), pathBounds.height());
65 } else {
66 srcTol = devTol / stretch;
67 }
68 if (srcTol < kMinCurveTol) {
69 srcTol = kMinCurveTol;
70 }
71 return srcTol;
72 }
73
74 uint32_t GrPathUtils::quadraticPointCount(const SkPoint points[], SkScalar tol) {
75 return max_bezier_vertices(skgpu::wangs_formula::quadratic_log2(
76 tolerance_to_wangs_precision(tol), points));
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 { sk_float_midpoint(p0.fX, p1.fX), sk_float_midpoint(p0.fY, p1.fY) },
94 { sk_float_midpoint(p1.fX, p2.fX), sk_float_midpoint(p1.fY, p2.fY) },
95 };
96 SkPoint r = { sk_float_midpoint(q[0].fX, q[1].fX), sk_float_midpoint(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[], SkScalar tol) {
105 return max_bezier_vertices(skgpu::wangs_formula::cubic_log2(
106 tolerance_to_wangs_precision(tol), points));
107 }
108
109 uint32_t GrPathUtils::generateCubicPoints(const SkPoint& p0,
110 const SkPoint& p1,
111 const SkPoint& p2,
112 const SkPoint& p3,
113 SkScalar tolSqd,
114 SkPoint** points,
115 uint32_t pointsLeft) {
116 if (pointsLeft < 2 ||
117 (SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3) < tolSqd &&
118 SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3) < tolSqd)) {
119 (*points)[0] = p3;
120 *points += 1;
121 return 1;
122 }
123 SkPoint q[] = {
124 { sk_float_midpoint(p0.fX, p1.fX), sk_float_midpoint(p0.fY, p1.fY) },
125 { sk_float_midpoint(p1.fX, p2.fX), sk_float_midpoint(p1.fY, p2.fY) },
126 { sk_float_midpoint(p2.fX, p3.fX), sk_float_midpoint(p2.fY, p3.fY) }
127 };
128 SkPoint r[] = {
129 { sk_float_midpoint(q[0].fX, q[1].fX), sk_float_midpoint(q[0].fY, q[1].fY) },
130 { sk_float_midpoint(q[1].fX, q[2].fX), sk_float_midpoint(q[1].fY, q[2].fY) }
131 };
132 SkPoint s = { sk_float_midpoint(r[0].fX, r[1].fX), sk_float_midpoint(r[0].fY, r[1].fY) };
133 pointsLeft >>= 1;
134 uint32_t a = generateCubicPoints(p0, q[0], r[0], s, tolSqd, points, pointsLeft);
135 uint32_t b = generateCubicPoints(s, r[1], q[2], p3, tolSqd, points, pointsLeft);
136 return a + b;
137 }
138
139 void GrPathUtils::QuadUVMatrix::set(const SkPoint qPts[3]) {
140 // We want M such that M * xy_pt = uv_pt
141 // We know M * control_pts = [0 1/2 1]
142 // [0 0 1]
143 // [1 1 1]
144 // And control_pts = [x0 x1 x2]
145 // [y0 y1 y2]
146 // [1 1 1 ]
147 // We invert the control pt matrix and post concat to both sides to get M.
148 // Using the known form of the control point matrix and the result, we can
149 // optimize and improve precision.
150
151 double x0 = qPts[0].fX;
152 double y0 = qPts[0].fY;
153 double x1 = qPts[1].fX;
154 double y1 = qPts[1].fY;
155 double x2 = qPts[2].fX;
156 double y2 = qPts[2].fY;
157
158 // pre-calculate some adjugate matrix factors for determinant
159 double a2 = x1*y2-x2*y1;
160 double a5 = x2*y0-x0*y2;
161 double a8 = x0*y1-x1*y0;
162 double det = a2 + a5 + a8;
163
164 if (!SkIsFinite(det)
165 || SkScalarNearlyZero((float)det, SK_ScalarNearlyZero * SK_ScalarNearlyZero)) {
166 // The quad is degenerate. Hopefully this is rare. Find the pts that are
167 // farthest apart to compute a line (unless it is really a pt).
168 SkScalar maxD = SkPointPriv::DistanceToSqd(qPts[0], qPts[1]);
169 int maxEdge = 0;
170 SkScalar d = SkPointPriv::DistanceToSqd(qPts[1], qPts[2]);
171 if (d > maxD) {
172 maxD = d;
173 maxEdge = 1;
174 }
175 d = SkPointPriv::DistanceToSqd(qPts[2], qPts[0]);
176 if (d > maxD) {
177 maxD = d;
178 maxEdge = 2;
179 }
180 // We could have a tolerance here, not sure if it would improve anything
181 if (maxD > 0) {
182 // Set the matrix to give (u = 0, v = distance_to_line)
183 SkVector lineVec = qPts[(maxEdge + 1)%3] - qPts[maxEdge];
184 // when looking from the point 0 down the line we want positive
185 // distances to be to the left. This matches the non-degenerate
186 // case.
187 lineVec = SkPointPriv::MakeOrthog(lineVec, SkPointPriv::kLeft_Side);
188 // first row
189 fM[0] = 0;
190 fM[1] = 0;
191 fM[2] = 0;
192 // second row
193 fM[3] = lineVec.fX;
194 fM[4] = lineVec.fY;
195 fM[5] = -lineVec.dot(qPts[maxEdge]);
196 } else {
197 // It's a point. It should cover zero area. Just set the matrix such
198 // that (u, v) will always be far away from the quad.
199 fM[0] = 0; fM[1] = 0; fM[2] = 100.f;
200 fM[3] = 0; fM[4] = 0; fM[5] = 100.f;
201 }
202 } else {
203 double scale = 1.0/det;
204
205 // compute adjugate matrix
206 double a3, a4, a6, a7;
207 a3 = y2-y0;
208 a4 = x0-x2;
209
210 a6 = y0-y1;
211 a7 = x1-x0;
212
213 // this performs the uv_pts*adjugate(control_pts) multiply,
214 // then does the scale by 1/det afterwards to improve precision
215 fM[0] = (float)((0.5*a3 + a6)*scale);
216 fM[1] = (float)((0.5*a4 + a7)*scale);
217 fM[2] = (float)((0.5*a5 + a8)*scale);
218 fM[3] = (float)(a6*scale);
219 fM[4] = (float)(a7*scale);
220 fM[5] = (float)(a8*scale);
221 }
222 }
223
224 ////////////////////////////////////////////////////////////////////////////////
225
226 // k = (y2 - y0, x0 - x2, x2*y0 - x0*y2)
227 // l = (y1 - y0, x0 - x1, x1*y0 - x0*y1) * 2*w
228 // m = (y2 - y1, x1 - x2, x2*y1 - x1*y2) * 2*w
229 void GrPathUtils::getConicKLM(const SkPoint p[3], const SkScalar weight, SkMatrix* out) {
230 SkMatrix& klm = *out;
231 const SkScalar w2 = 2.f * weight;
232 klm[0] = p[2].fY - p[0].fY;
233 klm[1] = p[0].fX - p[2].fX;
234 klm[2] = p[2].fX * p[0].fY - p[0].fX * p[2].fY;
235
236 klm[3] = w2 * (p[1].fY - p[0].fY);
237 klm[4] = w2 * (p[0].fX - p[1].fX);
238 klm[5] = w2 * (p[1].fX * p[0].fY - p[0].fX * p[1].fY);
239
240 klm[6] = w2 * (p[2].fY - p[1].fY);
241 klm[7] = w2 * (p[1].fX - p[2].fX);
242 klm[8] = w2 * (p[2].fX * p[1].fY - p[1].fX * p[2].fY);
243
244 // scale the max absolute value of coeffs to 10
245 SkScalar scale = 0.f;
246 for (int i = 0; i < 9; ++i) {
247 scale = std::max(scale, SkScalarAbs(klm[i]));
248 }
249 SkASSERT(scale > 0.f);
250 scale = 10.f / scale;
251 for (int i = 0; i < 9; ++i) {
252 klm[i] *= scale;
253 }
254 }
255
256 ////////////////////////////////////////////////////////////////////////////////
257
258 namespace {
259
260 // a is the first control point of the cubic.
261 // ab is the vector from a to the second control point.
262 // dc is the vector from the fourth to the third control point.
263 // d is the fourth control point.
264 // p is the candidate quadratic control point.
265 // this assumes that the cubic doesn't inflect and is simple
266 bool is_point_within_cubic_tangents(const SkPoint& a,
267 const SkVector& ab,
268 const SkVector& dc,
269 const SkPoint& d,
270 SkPathFirstDirection dir,
271 const SkPoint p) {
272 SkVector ap = p - a;
273 SkScalar apXab = ap.cross(ab);
274 if (SkPathFirstDirection::kCW == dir) {
275 if (apXab > 0) {
276 return false;
277 }
278 } else {
279 SkASSERT(SkPathFirstDirection::kCCW == dir);
280 if (apXab < 0) {
281 return false;
282 }
283 }
284
285 SkVector dp = p - d;
286 SkScalar dpXdc = dp.cross(dc);
287 if (SkPathFirstDirection::kCW == dir) {
288 if (dpXdc < 0) {
289 return false;
290 }
291 } else {
292 SkASSERT(SkPathFirstDirection::kCCW == dir);
293 if (dpXdc > 0) {
294 return false;
295 }
296 }
297 return true;
298 }
299
300 void convert_noninflect_cubic_to_quads(const SkPoint p[4],
301 SkScalar toleranceSqd,
302 TArray<SkPoint, true>* quads,
303 int sublevel = 0,
304 bool preserveFirstTangent = true,
305 bool preserveLastTangent = true) {
306 // Notation: Point a is always p[0]. Point b is p[1] unless p[1] == p[0], in which case it is
307 // 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].
308 SkVector ab = p[1] - p[0];
309 SkVector dc = p[2] - p[3];
310
311 if (SkPointPriv::LengthSqd(ab) < SK_ScalarNearlyZero) {
312 if (SkPointPriv::LengthSqd(dc) < SK_ScalarNearlyZero) {
313 SkPoint* degQuad = quads->push_back_n(3);
314 degQuad[0] = p[0];
315 degQuad[1] = p[0];
316 degQuad[2] = p[3];
317 return;
318 }
319 ab = p[2] - p[0];
320 }
321 if (SkPointPriv::LengthSqd(dc) < SK_ScalarNearlyZero) {
322 dc = p[1] - p[3];
323 }
324
325 static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2;
326 static const int kMaxSubdivs = 10;
327
328 ab.scale(kLengthScale);
329 dc.scale(kLengthScale);
330
331 // c0 and c1 are extrapolations along vectors ab and dc.
332 SkPoint c0 = p[0] + ab;
333 SkPoint c1 = p[3] + dc;
334
335 SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : SkPointPriv::DistanceToSqd(c0, c1);
336 if (dSqd < toleranceSqd) {
337 SkPoint newC;
338 if (preserveFirstTangent == preserveLastTangent) {
339 // We used to force a split when both tangents need to be preserved and c0 != c1.
340 // This introduced a large performance regression for tiny paths for no noticeable
341 // quality improvement. However, we aren't quite fulfilling our contract of guaranteeing
342 // the two tangent vectors and this could introduce a missed pixel in
343 // AAHairlinePathRenderer.
344 newC = (c0 + c1) * 0.5f;
345 } else if (preserveFirstTangent) {
346 newC = c0;
347 } else {
348 newC = c1;
349 }
350
351 SkPoint* pts = quads->push_back_n(3);
352 pts[0] = p[0];
353 pts[1] = newC;
354 pts[2] = p[3];
355 return;
356 }
357 SkPoint choppedPts[7];
358 SkChopCubicAtHalf(p, choppedPts);
359 convert_noninflect_cubic_to_quads(
360 choppedPts + 0, toleranceSqd, quads, sublevel + 1, preserveFirstTangent, false);
361 convert_noninflect_cubic_to_quads(
362 choppedPts + 3, toleranceSqd, quads, sublevel + 1, false, preserveLastTangent);
363 }
364
365 void convert_noninflect_cubic_to_quads_with_constraint(const SkPoint p[4],
366 SkScalar toleranceSqd,
367 SkPathFirstDirection dir,
368 TArray<SkPoint, true>* quads,
369 int sublevel = 0) {
370 // Notation: Point a is always p[0]. Point b is p[1] unless p[1] == p[0], in which case it is
371 // 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].
372
373 SkVector ab = p[1] - p[0];
374 SkVector dc = p[2] - p[3];
375
376 if (SkPointPriv::LengthSqd(ab) < SK_ScalarNearlyZero) {
377 if (SkPointPriv::LengthSqd(dc) < SK_ScalarNearlyZero) {
378 SkPoint* degQuad = quads->push_back_n(3);
379 degQuad[0] = p[0];
380 degQuad[1] = p[0];
381 degQuad[2] = p[3];
382 return;
383 }
384 ab = p[2] - p[0];
385 }
386 if (SkPointPriv::LengthSqd(dc) < SK_ScalarNearlyZero) {
387 dc = p[1] - p[3];
388 }
389
390 // When the ab and cd tangents are degenerate or nearly parallel with vector from d to a the
391 // constraint that the quad point falls between the tangents becomes hard to enforce and we are
392 // likely to hit the max subdivision count. However, in this case the cubic is approaching a
393 // line and the accuracy of the quad point isn't so important. We check if the two middle cubic
394 // control points are very close to the baseline vector. If so then we just pick quadratic
395 // points on the control polygon.
396
397 SkVector da = p[0] - p[3];
398 bool doQuads = SkPointPriv::LengthSqd(dc) < SK_ScalarNearlyZero ||
399 SkPointPriv::LengthSqd(ab) < SK_ScalarNearlyZero;
400 if (!doQuads) {
401 SkScalar invDALengthSqd = SkPointPriv::LengthSqd(da);
402 if (invDALengthSqd > SK_ScalarNearlyZero) {
403 invDALengthSqd = SkScalarInvert(invDALengthSqd);
404 // cross(ab, da)^2/length(da)^2 == sqd distance from b to line from d to a.
405 // same goes for point c using vector cd.
406 SkScalar detABSqd = ab.cross(da);
407 detABSqd = SkScalarSquare(detABSqd);
408 SkScalar detDCSqd = dc.cross(da);
409 detDCSqd = SkScalarSquare(detDCSqd);
410 if (detABSqd * invDALengthSqd < toleranceSqd &&
411 detDCSqd * invDALengthSqd < toleranceSqd) {
412 doQuads = true;
413 }
414 }
415 }
416 if (doQuads) {
417 SkPoint b = p[0] + ab;
418 SkPoint c = p[3] + dc;
419 SkPoint mid = b + c;
420 mid.scale(SK_ScalarHalf);
421 // Insert two quadratics to cover the case when ab points away from d and/or dc
422 // points away from a.
423 if (SkVector::DotProduct(da, dc) < 0 || SkVector::DotProduct(ab, da) > 0) {
424 SkPoint* qpts = quads->push_back_n(6);
425 qpts[0] = p[0];
426 qpts[1] = b;
427 qpts[2] = mid;
428 qpts[3] = mid;
429 qpts[4] = c;
430 qpts[5] = p[3];
431 } else {
432 SkPoint* qpts = quads->push_back_n(3);
433 qpts[0] = p[0];
434 qpts[1] = mid;
435 qpts[2] = p[3];
436 }
437 return;
438 }
439
440 static const SkScalar kLengthScale = 3 * SK_Scalar1 / 2;
441 static const int kMaxSubdivs = 10;
442
443 ab.scale(kLengthScale);
444 dc.scale(kLengthScale);
445
446 // c0 and c1 are extrapolations along vectors ab and dc.
447 SkVector c0 = p[0] + ab;
448 SkVector c1 = p[3] + dc;
449
450 SkScalar dSqd = sublevel > kMaxSubdivs ? 0 : SkPointPriv::DistanceToSqd(c0, c1);
451 if (dSqd < toleranceSqd) {
452 SkPoint cAvg = (c0 + c1) * 0.5f;
453 bool subdivide = false;
454
455 if (!is_point_within_cubic_tangents(p[0], ab, dc, p[3], dir, cAvg)) {
456 // choose a new cAvg that is the intersection of the two tangent lines.
457 ab = SkPointPriv::MakeOrthog(ab);
458 SkScalar z0 = -ab.dot(p[0]);
459 dc = SkPointPriv::MakeOrthog(dc);
460 SkScalar z1 = -dc.dot(p[3]);
461 cAvg.fX = ab.fY * z1 - z0 * dc.fY;
462 cAvg.fY = z0 * dc.fX - ab.fX * z1;
463 SkScalar z = ab.fX * dc.fY - ab.fY * dc.fX;
464 z = sk_ieee_float_divide(1.0f, z);
465 cAvg.fX *= z;
466 cAvg.fY *= z;
467 if (sublevel <= kMaxSubdivs) {
468 SkScalar d0Sqd = SkPointPriv::DistanceToSqd(c0, cAvg);
469 SkScalar d1Sqd = SkPointPriv::DistanceToSqd(c1, cAvg);
470 // We need to subdivide if d0 + d1 > tolerance but we have the sqd values. We know
471 // the distances and tolerance can't be negative.
472 // (d0 + d1)^2 > toleranceSqd
473 // d0Sqd + 2*d0*d1 + d1Sqd > toleranceSqd
474 SkScalar d0d1 = SkScalarSqrt(d0Sqd * d1Sqd);
475 subdivide = 2 * d0d1 + d0Sqd + d1Sqd > toleranceSqd;
476 }
477 }
478 if (!subdivide) {
479 SkPoint* pts = quads->push_back_n(3);
480 pts[0] = p[0];
481 pts[1] = cAvg;
482 pts[2] = p[3];
483 return;
484 }
485 }
486 SkPoint choppedPts[7];
487 SkChopCubicAtHalf(p, choppedPts);
488 convert_noninflect_cubic_to_quads_with_constraint(
489 choppedPts + 0, toleranceSqd, dir, quads, sublevel + 1);
490 convert_noninflect_cubic_to_quads_with_constraint(
491 choppedPts + 3, toleranceSqd, dir, quads, sublevel + 1);
492 }
493 } // namespace
494
495 void GrPathUtils::convertCubicToQuads(const SkPoint p[4],
496 SkScalar tolScale,
497 TArray<SkPoint, true>* quads) {
498 if (!p[0].isFinite() || !p[1].isFinite() || !p[2].isFinite() || !p[3].isFinite()) {
499 return;
500 }
501 if (!SkIsFinite(tolScale)) {
502 return;
503 }
504 SkPoint chopped[10];
505 int count = SkChopCubicAtInflections(p, chopped);
506
507 const SkScalar tolSqd = SkScalarSquare(tolScale);
508
509 for (int i = 0; i < count; ++i) {
510 SkPoint* cubic = chopped + 3*i;
511 convert_noninflect_cubic_to_quads(cubic, tolSqd, quads);
512 }
513 }
514
515 void GrPathUtils::convertCubicToQuadsConstrainToTangents(const SkPoint p[4],
516 SkScalar tolScale,
517 SkPathFirstDirection dir,
518 TArray<SkPoint, true>* quads) {
519 if (!p[0].isFinite() || !p[1].isFinite() || !p[2].isFinite() || !p[3].isFinite()) {
520 return;
521 }
522 if (!SkIsFinite(tolScale)) {
523 return;
524 }
525 SkPoint chopped[10];
526 int count = SkChopCubicAtInflections(p, chopped);
527
528 const SkScalar tolSqd = SkScalarSquare(tolScale);
529
530 for (int i = 0; i < count; ++i) {
531 SkPoint* cubic = chopped + 3*i;
532 convert_noninflect_cubic_to_quads_with_constraint(cubic, tolSqd, dir, quads);
533 }
534 }