blob: bbf5e4fc0fc8af777d697342456d77101b727577 [file] [log] [blame]
Chris Dalton419a94d2017-08-28 10:24:22 -06001/*
2 * Copyright 2017 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 "GrCCPRGeometry.h"
9
10#include "GrTypes.h"
Chris Dalton7f578bf2017-09-05 16:46:48 -060011#include "GrPathUtils.h"
Chris Dalton419a94d2017-08-28 10:24:22 -060012#include <algorithm>
13#include <cmath>
14#include <cstdlib>
15
16// We convert between SkPoint and Sk2f freely throughout this file.
17GR_STATIC_ASSERT(SK_SCALAR_IS_FLOAT);
18GR_STATIC_ASSERT(2 * sizeof(float) == sizeof(SkPoint));
19GR_STATIC_ASSERT(0 == offsetof(SkPoint, fX));
20
Chris Daltonc1e59632017-09-05 00:30:07 -060021void GrCCPRGeometry::beginPath() {
22 SkASSERT(!fBuildingContour);
23 fVerbs.push_back(Verb::kBeginPath);
24}
25
26void GrCCPRGeometry::beginContour(const SkPoint& devPt) {
27 SkASSERT(!fBuildingContour);
28
29 fCurrFanPoint = fCurrAnchorPoint = devPt;
30
31 // Store the current verb count in the fTriangles field for now. When we close the contour we
32 // will use this value to calculate the actual number of triangles in its fan.
33 fCurrContourTallies = {fVerbs.count(), 0, 0, 0};
34
35 fPoints.push_back(devPt);
36 fVerbs.push_back(Verb::kBeginContour);
37
38 SkDEBUGCODE(fBuildingContour = true;)
39}
40
41void GrCCPRGeometry::lineTo(const SkPoint& devPt) {
42 SkASSERT(fBuildingContour);
Chris Dalton900cd052017-09-07 10:36:51 -060043 SkASSERT(fCurrFanPoint == fPoints.back());
Chris Daltonc1e59632017-09-05 00:30:07 -060044 fCurrFanPoint = devPt;
45 fPoints.push_back(devPt);
46 fVerbs.push_back(Verb::kLineTo);
47}
48
Chris Dalton419a94d2017-08-28 10:24:22 -060049static inline Sk2f normalize(const Sk2f& n) {
50 Sk2f nn = n*n;
51 return n * (nn + SkNx_shuffle<1,0>(nn)).rsqrt();
52}
53
54static inline float dot(const Sk2f& a, const Sk2f& b) {
55 float product[2];
56 (a * b).store(product);
57 return product[0] + product[1];
58}
59
Chris Dalton900cd052017-09-07 10:36:51 -060060static inline bool are_collinear(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2) {
61 static constexpr float kFlatnessTolerance = 16; // 1/16 of a pixel.
62
63 // Area (times 2) of the triangle.
64 Sk2f a = (p0 - p1) * SkNx_shuffle<1,0>(p1 - p2);
65 a = (a - SkNx_shuffle<1,0>(a)).abs();
66
67 // Bounding box of the triangle.
68 Sk2f bbox0 = Sk2f::Min(Sk2f::Min(p0, p1), p2);
69 Sk2f bbox1 = Sk2f::Max(Sk2f::Max(p0, p1), p2);
70
71 // The triangle is linear if its area is within a fraction of the largest bounding box
72 // dimension, or else if its area is within a fraction of a pixel.
73 return (a * (kFlatnessTolerance/2) < Sk2f::Max(bbox1 - bbox0, 1)).anyTrue();
74}
75
Chris Dalton419a94d2017-08-28 10:24:22 -060076// Returns whether the (convex) curve segment is monotonic with respect to [endPt - startPt].
77static inline bool is_convex_curve_monotonic(const Sk2f& startPt, const Sk2f& startTan,
78 const Sk2f& endPt, const Sk2f& endTan) {
79 Sk2f v = endPt - startPt;
80 float dot0 = dot(startTan, v);
81 float dot1 = dot(endTan, v);
82
83 // A small, negative tolerance handles floating-point error in the case when one tangent
84 // approaches 0 length, meaning the (convex) curve segment is effectively a flat line.
85 float tolerance = -std::max(std::abs(dot0), std::abs(dot1)) * SK_ScalarNearlyZero;
86 return dot0 >= tolerance && dot1 >= tolerance;
87}
88
89static inline Sk2f lerp(const Sk2f& a, const Sk2f& b, const Sk2f& t) {
90 return SkNx_fma(t, b - a, a);
91}
92
Chris Daltonc1e59632017-09-05 00:30:07 -060093void GrCCPRGeometry::quadraticTo(const SkPoint& devP0, const SkPoint& devP1) {
94 SkASSERT(fBuildingContour);
Chris Dalton900cd052017-09-07 10:36:51 -060095 SkASSERT(fCurrFanPoint == fPoints.back());
Chris Daltonc1e59632017-09-05 00:30:07 -060096
97 Sk2f p0 = Sk2f::Load(&fCurrFanPoint);
98 Sk2f p1 = Sk2f::Load(&devP0);
99 Sk2f p2 = Sk2f::Load(&devP1);
100 fCurrFanPoint = devP1;
Chris Dalton419a94d2017-08-28 10:24:22 -0600101
Chris Dalton900cd052017-09-07 10:36:51 -0600102 // Don't send curves to the GPU if we know they are flat (or just very small).
103 if (are_collinear(p0, p1, p2)) {
104 p2.store(&fPoints.push_back());
105 fVerbs.push_back(Verb::kLineTo);
106 return;
107 }
108
Chris Dalton29011a22017-09-28 12:08:33 -0600109 this->appendMonotonicQuadratics(p0, p1, p2);
110}
111
112inline void GrCCPRGeometry::appendMonotonicQuadratics(const Sk2f& p0, const Sk2f& p1,
113 const Sk2f& p2, bool allowChop) {
114 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
Chris Dalton419a94d2017-08-28 10:24:22 -0600115 Sk2f tan0 = p1 - p0;
116 Sk2f tan1 = p2 - p1;
Chris Dalton29011a22017-09-28 12:08:33 -0600117
Chris Dalton419a94d2017-08-28 10:24:22 -0600118 // This should almost always be this case for well-behaved curves in the real world.
Chris Dalton29011a22017-09-28 12:08:33 -0600119 if (!allowChop || is_convex_curve_monotonic(p0, tan0, p2, tan1)) {
120 p1.store(&fPoints.push_back());
121 p2.store(&fPoints.push_back());
122 fVerbs.push_back(Verb::kMonotonicQuadraticTo);
123 ++fCurrContourTallies.fQuadratics;
Chris Daltonc1e59632017-09-05 00:30:07 -0600124 return;
Chris Dalton419a94d2017-08-28 10:24:22 -0600125 }
126
127 // Chop the curve into two segments with equal curvature. To do this we find the T value whose
128 // tangent is perpendicular to the vector that bisects tan0 and -tan1.
129 Sk2f n = normalize(tan0) - normalize(tan1);
130
131 // This tangent can be found where (dQ(t) dot n) = 0:
132 //
133 // 0 = (dQ(t) dot n) = | 2*t 1 | * | p0 - 2*p1 + p2 | * | n |
134 // | -2*p0 + 2*p1 | | . |
135 //
136 // = | 2*t 1 | * | tan1 - tan0 | * | n |
137 // | 2*tan0 | | . |
138 //
139 // = 2*t * ((tan1 - tan0) dot n) + (2*tan0 dot n)
140 //
141 // t = (tan0 dot n) / ((tan0 - tan1) dot n)
142 Sk2f dQ1n = (tan0 - tan1) * n;
143 Sk2f dQ0n = tan0 * n;
144 Sk2f t = (dQ0n + SkNx_shuffle<1,0>(dQ0n)) / (dQ1n + SkNx_shuffle<1,0>(dQ1n));
145 t = Sk2f::Min(Sk2f::Max(t, 0), 1); // Clamp for FP error.
146
147 Sk2f p01 = SkNx_fma(t, tan0, p0);
148 Sk2f p12 = SkNx_fma(t, tan1, p1);
149 Sk2f p012 = lerp(p01, p12, t);
150
Chris Dalton29011a22017-09-28 12:08:33 -0600151 p01.store(&fPoints.push_back());
152 p012.store(&fPoints.push_back());
153 p12.store(&fPoints.push_back());
Chris Daltonc1e59632017-09-05 00:30:07 -0600154 p2.store(&fPoints.push_back());
Chris Dalton29011a22017-09-28 12:08:33 -0600155 fVerbs.push_back_n(2, Verb::kMonotonicQuadraticTo);
156 fCurrContourTallies.fQuadratics += 2;
Chris Daltonc1e59632017-09-05 00:30:07 -0600157}
158
Chris Dalton7f578bf2017-09-05 16:46:48 -0600159using ExcludedTerm = GrPathUtils::ExcludedTerm;
Chris Daltonc1e59632017-09-05 00:30:07 -0600160
Chris Dalton7f578bf2017-09-05 16:46:48 -0600161// Calculates the padding to apply around inflection points, in homogeneous parametric coordinates.
162//
163// More specifically, if the inflection point lies at C(t/s), then C((t +/- returnValue) / s) will
164// be the two points on the curve at which a square box with radius "padRadius" will have a corner
165// that touches the inflection point's tangent line.
166//
167// A serpentine cubic has two inflection points, so this method takes Sk2f and computes the padding
168// for both in SIMD.
169static inline Sk2f calc_inflect_homogeneous_padding(float padRadius, const Sk2f& t, const Sk2f& s,
170 const SkMatrix& CIT, ExcludedTerm skipTerm) {
171 SkASSERT(padRadius >= 0);
Chris Daltonc1e59632017-09-05 00:30:07 -0600172
Chris Dalton7f578bf2017-09-05 16:46:48 -0600173 Sk2f Clx = s*s*s;
174 Sk2f Cly = (ExcludedTerm::kLinearTerm == skipTerm) ? s*s*t*-3 : s*t*t*3;
175
176 Sk2f Lx = CIT[0] * Clx + CIT[3] * Cly;
177 Sk2f Ly = CIT[1] * Clx + CIT[4] * Cly;
178
179 float ret[2];
180 Sk2f bloat = padRadius * (Lx.abs() + Ly.abs());
181 (bloat * s >= 0).thenElse(bloat, -bloat).store(ret);
182
183 ret[0] = cbrtf(ret[0]);
184 ret[1] = cbrtf(ret[1]);
185 return Sk2f::Load(ret);
186}
187
188static inline void swap_if_greater(float& a, float& b) {
189 if (a > b) {
190 std::swap(a, b);
191 }
192}
193
194// Calculates all parameter values for a loop at which points a square box with radius "padRadius"
195// will have a corner that touches a tangent line from the intersection.
196//
197// T2 must contain the lesser parameter value of the loop intersection in its first component, and
198// the greater in its second.
199//
200// roots[0] will be filled with 1 or 3 sorted parameter values, representing the padding points
201// around the first tangent. roots[1] will be filled with the padding points for the second tangent.
202static inline void calc_loop_intersect_padding_pts(float padRadius, const Sk2f& T2,
203 const SkMatrix& CIT, ExcludedTerm skipTerm,
204 SkSTArray<3, float, true> roots[2]) {
205 SkASSERT(padRadius >= 0);
206 SkASSERT(T2[0] <= T2[1]);
207 SkASSERT(roots[0].empty());
208 SkASSERT(roots[1].empty());
209
210 Sk2f T1 = SkNx_shuffle<1,0>(T2);
211 Sk2f Cl = (ExcludedTerm::kLinearTerm == skipTerm) ? T2*-2 - T1 : T2*T2 + T2*T1*2;
212 Sk2f Lx = Cl * CIT[3] + CIT[0];
213 Sk2f Ly = Cl * CIT[4] + CIT[1];
214
215 Sk2f bloat = Sk2f(+.5f * padRadius, -.5f * padRadius) * (Lx.abs() + Ly.abs());
216 Sk2f q = (1.f/3) * (T2 - T1);
217
218 Sk2f qqq = q*q*q;
219 Sk2f discr = qqq*bloat*2 + bloat*bloat;
220
221 float numRoots[2], D[2];
222 (discr < 0).thenElse(3, 1).store(numRoots);
223 (T2 - q).store(D);
224
225 // Values for calculating one root.
226 float R[2], QQ[2];
227 if ((discr >= 0).anyTrue()) {
228 Sk2f r = qqq + bloat;
229 Sk2f s = r.abs() + discr.sqrt();
230 (r > 0).thenElse(-s, s).store(R);
231 (q*q).store(QQ);
Chris Daltonc1e59632017-09-05 00:30:07 -0600232 }
233
Chris Dalton7f578bf2017-09-05 16:46:48 -0600234 // Values for calculating three roots.
235 float P[2], cosTheta3[2];
236 if ((discr < 0).anyTrue()) {
237 (q.abs() * -2).store(P);
238 ((q >= 0).thenElse(1, -1) + bloat / qqq.abs()).store(cosTheta3);
Chris Daltonc1e59632017-09-05 00:30:07 -0600239 }
240
Chris Dalton7f578bf2017-09-05 16:46:48 -0600241 for (int i = 0; i < 2; ++i) {
242 if (1 == numRoots[i]) {
243 float A = cbrtf(R[i]);
244 float B = A != 0 ? QQ[i]/A : 0;
245 roots[i].push_back(A + B + D[i]);
Chris Daltonc1e59632017-09-05 00:30:07 -0600246 continue;
247 }
248
Chris Dalton7f578bf2017-09-05 16:46:48 -0600249 static constexpr float k2PiOver3 = 2 * SK_ScalarPI / 3;
250 float theta = std::acos(cosTheta3[i]) * (1.f/3);
251 roots[i].push_back(P[i] * std::cos(theta) + D[i]);
252 roots[i].push_back(P[i] * std::cos(theta + k2PiOver3) + D[i]);
253 roots[i].push_back(P[i] * std::cos(theta - k2PiOver3) + D[i]);
Chris Daltonc1e59632017-09-05 00:30:07 -0600254
Chris Dalton7f578bf2017-09-05 16:46:48 -0600255 // Sort the three roots.
256 swap_if_greater(roots[i][0], roots[i][1]);
257 swap_if_greater(roots[i][1], roots[i][2]);
258 swap_if_greater(roots[i][0], roots[i][1]);
259 }
260}
261
Chris Dalton29011a22017-09-28 12:08:33 -0600262static inline Sk2f first_unless_nearly_zero(const Sk2f& a, const Sk2f& b) {
263 Sk2f aa = a*a;
264 aa += SkNx_shuffle<1,0>(aa);
265 SkASSERT(aa[0] == aa[1]);
266
267 Sk2f bb = b*b;
268 bb += SkNx_shuffle<1,0>(bb);
269 SkASSERT(bb[0] == bb[1]);
270
271 return (aa > bb * SK_ScalarNearlyZero).thenElse(a, b);
272}
273
274static inline bool is_cubic_nearly_quadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
275 const Sk2f& p3, Sk2f& tan0, Sk2f& tan3, Sk2f& c) {
276 tan0 = first_unless_nearly_zero(p1 - p0, p2 - p0);
277 tan3 = first_unless_nearly_zero(p3 - p2, p3 - p1);
278
279 Sk2f c1 = SkNx_fma(Sk2f(1.5f), tan0, p0);
280 Sk2f c2 = SkNx_fma(Sk2f(-1.5f), tan3, p3);
281 c = (c1 + c2) * .5f; // Hopefully optimized out if not used?
282
283 return ((c1 - c2).abs() <= 1).allTrue();
284}
285
Chris Dalton7f578bf2017-09-05 16:46:48 -0600286void GrCCPRGeometry::cubicTo(const SkPoint& devP1, const SkPoint& devP2, const SkPoint& devP3,
287 float inflectPad, float loopIntersectPad) {
288 SkASSERT(fBuildingContour);
Chris Dalton900cd052017-09-07 10:36:51 -0600289 SkASSERT(fCurrFanPoint == fPoints.back());
Chris Dalton7f578bf2017-09-05 16:46:48 -0600290
291 SkPoint devPts[4] = {fCurrFanPoint, devP1, devP2, devP3};
292 Sk2f p0 = Sk2f::Load(&fCurrFanPoint);
293 Sk2f p1 = Sk2f::Load(&devP1);
294 Sk2f p2 = Sk2f::Load(&devP2);
295 Sk2f p3 = Sk2f::Load(&devP3);
296 fCurrFanPoint = devP3;
297
Chris Dalton900cd052017-09-07 10:36:51 -0600298 // Don't crunch on the curve and inflate geometry if it is already flat (or just very small).
299 if (are_collinear(p0, p1, p2) &&
300 are_collinear(p1, p2, p3) &&
301 are_collinear(p0, (p1 + p2) * .5f, p3)) {
302 p3.store(&fPoints.push_back());
303 fVerbs.push_back(Verb::kLineTo);
304 return;
305 }
306
Chris Dalton29011a22017-09-28 12:08:33 -0600307 // Also detect near-quadratics ahead of time.
308 Sk2f tan0, tan3, c;
309 if (is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan3, c)) {
310 this->appendMonotonicQuadratics(p0, c, p3);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600311 return;
312 }
313
Chris Dalton29011a22017-09-28 12:08:33 -0600314 double tt[2], ss[2];
315 fCurrCubicType = SkClassifyCubic(devPts, tt, ss);
316 SkASSERT(!SkCubicIsDegenerate(fCurrCubicType)); // Should have been caught above.
317
Chris Dalton7f578bf2017-09-05 16:46:48 -0600318 SkMatrix CIT;
319 ExcludedTerm skipTerm = GrPathUtils::calcCubicInverseTransposePowerBasisMatrix(devPts, &CIT);
Chris Dalton29011a22017-09-28 12:08:33 -0600320 SkASSERT(ExcludedTerm::kNonInvertible != skipTerm); // Should have been caught above.
Chris Dalton7f578bf2017-09-05 16:46:48 -0600321 SkASSERT(0 == CIT[6]);
322 SkASSERT(0 == CIT[7]);
323 SkASSERT(1 == CIT[8]);
324
325 // Each cubic has five different sections (not always inside t=[0..1]):
326 //
327 // 1. The section before the first inflection or loop intersection point, with padding.
328 // 2. The section that passes through the first inflection/intersection (aka the K,L
329 // intersection point or T=tt[0]/ss[0]).
330 // 3. The section between the two inflections/intersections, with padding.
331 // 4. The section that passes through the second inflection/intersection (aka the K,M
332 // intersection point or T=tt[1]/ss[1]).
333 // 5. The section after the second inflection/intersection, with padding.
334 //
335 // Sections 1,3,5 can be rendered directly using the CCPR cubic shader.
336 //
337 // Sections 2 & 4 must be approximated. For loop intersections we render them with
338 // quadratic(s), and when passing through an inflection point we use a plain old flat line.
339 //
340 // We find T0..T3 below to be the dividing points between these five sections.
341 float T0, T1, T2, T3;
342 if (SkCubicType::kLoop != fCurrCubicType) {
343 Sk2f t = Sk2f(static_cast<float>(tt[0]), static_cast<float>(tt[1]));
344 Sk2f s = Sk2f(static_cast<float>(ss[0]), static_cast<float>(ss[1]));
345 Sk2f pad = calc_inflect_homogeneous_padding(inflectPad, t, s, CIT, skipTerm);
346
347 float T[2];
348 ((t - pad) / s).store(T);
349 T0 = T[0];
350 T2 = T[1];
351
352 ((t + pad) / s).store(T);
353 T1 = T[0];
354 T3 = T[1];
355 } else {
356 const float T[2] = {static_cast<float>(tt[0]/ss[0]), static_cast<float>(tt[1]/ss[1])};
357 SkSTArray<3, float, true> roots[2];
358 calc_loop_intersect_padding_pts(loopIntersectPad, Sk2f::Load(T), CIT, skipTerm, roots);
359 T0 = roots[0].front();
360 if (1 == roots[0].count() || 1 == roots[1].count()) {
361 // The loop is tighter than our desired padding. Collapse the middle section to a point
362 // somewhere in the middle-ish of the loop and Sections 2 & 4 will approximate the the
363 // whole thing with quadratics.
364 T1 = T2 = (T[0] + T[1]) * .5f;
365 } else {
366 T1 = roots[0][1];
367 T2 = roots[1][1];
368 }
369 T3 = roots[1].back();
370 }
371
372 // Guarantee that T0..T3 are monotonic.
373 if (T0 > T3) {
374 // This is not a mathematically valid scenario. The only reason it would happen is if
375 // padding is very small and we have encountered FP rounding error.
376 T0 = T1 = T2 = T3 = (T0 + T3) / 2;
377 } else if (T1 > T2) {
378 // This just means padding before the middle section overlaps the padding after it. We
379 // collapse the middle section to a single point that splits the difference between the
380 // overlap in padding.
381 T1 = T2 = (T1 + T2) / 2;
382 }
383 // Clamp T1 & T2 inside T0..T3. The only reason this would be necessary is if we have
384 // encountered FP rounding error.
385 T1 = std::max(T0, std::min(T1, T3));
386 T2 = std::max(T0, std::min(T2, T3));
387
388 // Next we chop the cubic up at all T0..T3 inside 0..1 and store the resulting segments.
389 if (T1 >= 1) {
390 // Only sections 1 & 2 can be in 0..1.
391 this->chopCubic<&GrCCPRGeometry::appendMonotonicCubics,
392 &GrCCPRGeometry::appendCubicApproximation>(p0, p1, p2, p3, T0);
393 return;
394 }
395
396 if (T2 <= 0) {
397 // Only sections 4 & 5 can be in 0..1.
398 this->chopCubic<&GrCCPRGeometry::appendCubicApproximation,
399 &GrCCPRGeometry::appendMonotonicCubics>(p0, p1, p2, p3, T3);
400 return;
401 }
402
403 Sk2f midp0, midp1; // These hold the first two bezier points of the middle section, if needed.
404
405 if (T1 > 0) {
406 Sk2f T1T1 = Sk2f(T1);
407 Sk2f ab1 = lerp(p0, p1, T1T1);
408 Sk2f bc1 = lerp(p1, p2, T1T1);
409 Sk2f cd1 = lerp(p2, p3, T1T1);
410 Sk2f abc1 = lerp(ab1, bc1, T1T1);
411 Sk2f bcd1 = lerp(bc1, cd1, T1T1);
412 Sk2f abcd1 = lerp(abc1, bcd1, T1T1);
413
414 // Sections 1 & 2.
415 this->chopCubic<&GrCCPRGeometry::appendMonotonicCubics,
416 &GrCCPRGeometry::appendCubicApproximation>(p0, ab1, abc1, abcd1, T0/T1);
417
418 if (T2 >= 1) {
419 // The rest of the curve is Section 3 (middle section).
420 this->appendMonotonicCubics(abcd1, bcd1, cd1, p3);
421 return;
Chris Daltonc1e59632017-09-05 00:30:07 -0600422 }
423
Chris Dalton7f578bf2017-09-05 16:46:48 -0600424 // Now calculate the first two bezier points of the middle section. The final two will come
425 // from when we chop the other side, as that is numerically more stable.
426 midp0 = abcd1;
427 midp1 = lerp(abcd1, bcd1, Sk2f((T2 - T1) / (1 - T1)));
428 } else if (T2 >= 1) {
429 // The entire cubic is Section 3 (middle section).
430 this->appendMonotonicCubics(p0, p1, p2, p3);
431 return;
Chris Daltonc1e59632017-09-05 00:30:07 -0600432 }
433
Chris Dalton7f578bf2017-09-05 16:46:48 -0600434 SkASSERT(T2 > 0 && T2 < 1);
435
436 Sk2f T2T2 = Sk2f(T2);
437 Sk2f ab2 = lerp(p0, p1, T2T2);
438 Sk2f bc2 = lerp(p1, p2, T2T2);
439 Sk2f cd2 = lerp(p2, p3, T2T2);
440 Sk2f abc2 = lerp(ab2, bc2, T2T2);
441 Sk2f bcd2 = lerp(bc2, cd2, T2T2);
442 Sk2f abcd2 = lerp(abc2, bcd2, T2T2);
443
444 if (T1 <= 0) {
445 // The curve begins at Section 3 (middle section).
446 this->appendMonotonicCubics(p0, ab2, abc2, abcd2);
447 } else if (T2 > T1) {
448 // Section 3 (middle section).
449 Sk2f midp2 = lerp(abc2, abcd2, T1/T2);
450 this->appendMonotonicCubics(midp0, midp1, midp2, abcd2);
451 }
452
453 // Sections 4 & 5.
454 this->chopCubic<&GrCCPRGeometry::appendCubicApproximation,
455 &GrCCPRGeometry::appendMonotonicCubics>(abcd2, bcd2, cd2, p3, (T3-T2) / (1-T2));
Chris Daltonc1e59632017-09-05 00:30:07 -0600456}
457
Chris Dalton7f578bf2017-09-05 16:46:48 -0600458template<GrCCPRGeometry::AppendCubicFn AppendLeftRight>
459inline void GrCCPRGeometry::chopCubicAtMidTangent(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
460 const Sk2f& p3, const Sk2f& tan0,
461 const Sk2f& tan3, int maxFutureSubdivisions) {
462 // Find the T value whose tangent is perpendicular to the vector that bisects tan0 and -tan3.
463 Sk2f n = normalize(tan0) - normalize(tan3);
464
465 float a = 3 * dot(p3 + (p1 - p2)*3 - p0, n);
466 float b = 6 * dot(p0 - p1*2 + p2, n);
467 float c = 3 * dot(p1 - p0, n);
468
469 float discr = b*b - 4*a*c;
470 if (discr < 0) {
471 // If this is the case then the cubic must be nearly flat.
472 (this->*AppendLeftRight)(p0, p1, p2, p3, maxFutureSubdivisions);
473 return;
474 }
475
476 float q = -.5f * (b + copysignf(std::sqrt(discr), b));
477 float m = .5f*q*a;
478 float T = std::abs(q*q - m) < std::abs(a*c - m) ? q/a : c/q;
479
480 this->chopCubic<AppendLeftRight, AppendLeftRight>(p0, p1, p2, p3, T, maxFutureSubdivisions);
481}
482
483template<GrCCPRGeometry::AppendCubicFn AppendLeft, GrCCPRGeometry::AppendCubicFn AppendRight>
484inline void GrCCPRGeometry::chopCubic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
485 const Sk2f& p3, float T, int maxFutureSubdivisions) {
486 if (T >= 1) {
487 (this->*AppendLeft)(p0, p1, p2, p3, maxFutureSubdivisions);
488 return;
489 }
490
491 if (T <= 0) {
492 (this->*AppendRight)(p0, p1, p2, p3, maxFutureSubdivisions);
493 return;
494 }
495
496 Sk2f TT = T;
497 Sk2f ab = lerp(p0, p1, TT);
498 Sk2f bc = lerp(p1, p2, TT);
499 Sk2f cd = lerp(p2, p3, TT);
500 Sk2f abc = lerp(ab, bc, TT);
501 Sk2f bcd = lerp(bc, cd, TT);
502 Sk2f abcd = lerp(abc, bcd, TT);
503 (this->*AppendLeft)(p0, ab, abc, abcd, maxFutureSubdivisions);
504 (this->*AppendRight)(abcd, bcd, cd, p3, maxFutureSubdivisions);
505}
506
507void GrCCPRGeometry::appendMonotonicCubics(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
508 const Sk2f& p3, int maxSubdivisions) {
Chris Dalton29011a22017-09-28 12:08:33 -0600509 SkASSERT(maxSubdivisions >= 0);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600510 if ((p0 == p3).allTrue()) {
511 return;
512 }
513
514 if (maxSubdivisions) {
515 Sk2f tan0 = first_unless_nearly_zero(p1 - p0, p2 - p0);
516 Sk2f tan3 = first_unless_nearly_zero(p3 - p2, p3 - p1);
517
518 if (!is_convex_curve_monotonic(p0, tan0, p3, tan3)) {
519 this->chopCubicAtMidTangent<&GrCCPRGeometry::appendMonotonicCubics>(p0, p1, p2, p3,
520 tan0, tan3,
521 maxSubdivisions-1);
522 return;
523 }
524 }
525
526 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
527 p1.store(&fPoints.push_back());
528 p2.store(&fPoints.push_back());
529 p3.store(&fPoints.push_back());
530 if (SkCubicType::kLoop != fCurrCubicType) {
531 fVerbs.push_back(Verb::kMonotonicSerpentineTo);
Chris Daltonc1e59632017-09-05 00:30:07 -0600532 ++fCurrContourTallies.fSerpentines;
533 } else {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600534 fVerbs.push_back(Verb::kMonotonicLoopTo);
Chris Daltonc1e59632017-09-05 00:30:07 -0600535 ++fCurrContourTallies.fLoops;
536 }
537}
538
Chris Dalton7f578bf2017-09-05 16:46:48 -0600539void GrCCPRGeometry::appendCubicApproximation(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
540 const Sk2f& p3, int maxSubdivisions) {
Chris Dalton29011a22017-09-28 12:08:33 -0600541 SkASSERT(maxSubdivisions >= 0);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600542 if ((p0 == p3).allTrue()) {
543 return;
544 }
545
546 if (SkCubicType::kLoop != fCurrCubicType && SkCubicType::kQuadratic != fCurrCubicType) {
547 // This section passes through an inflection point, so we can get away with a flat line.
548 // This can cause some curves to feel slightly more flat when inspected rigorously back and
549 // forth against another renderer, but for now this seems acceptable given the simplicity.
550 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
551 p3.store(&fPoints.push_back());
552 fVerbs.push_back(Verb::kLineTo);
553 return;
554 }
555
Chris Dalton29011a22017-09-28 12:08:33 -0600556 Sk2f tan0, tan3, c;
557 if (!is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan3, c) && maxSubdivisions) {
558 this->chopCubicAtMidTangent<&GrCCPRGeometry::appendCubicApproximation>(p0, p1, p2, p3,
559 tan0, tan3,
560 maxSubdivisions - 1);
561 return;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600562 }
563
Chris Dalton29011a22017-09-28 12:08:33 -0600564 this->appendMonotonicQuadratics(p0, c, p3, SkToBool(maxSubdivisions));
Chris Dalton7f578bf2017-09-05 16:46:48 -0600565}
566
Chris Daltonc1e59632017-09-05 00:30:07 -0600567GrCCPRGeometry::PrimitiveTallies GrCCPRGeometry::endContour() {
568 SkASSERT(fBuildingContour);
569 SkASSERT(fVerbs.count() >= fCurrContourTallies.fTriangles);
570
571 // The fTriangles field currently contains this contour's starting verb index. We can now
572 // use it to calculate the size of the contour's fan.
573 int fanSize = fVerbs.count() - fCurrContourTallies.fTriangles;
574 if (fCurrFanPoint == fCurrAnchorPoint) {
575 --fanSize;
576 fVerbs.push_back(Verb::kEndClosedContour);
577 } else {
578 fVerbs.push_back(Verb::kEndOpenContour);
579 }
580
581 fCurrContourTallies.fTriangles = SkTMax(fanSize - 2, 0);
582
583 SkDEBUGCODE(fBuildingContour = false;)
584 return fCurrContourTallies;
Chris Dalton419a94d2017-08-28 10:24:22 -0600585}