blob: 4ba4f54c63a40f8b748afe790b438e086567da2e [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);
43 fCurrFanPoint = devPt;
44 fPoints.push_back(devPt);
45 fVerbs.push_back(Verb::kLineTo);
46}
47
Chris Dalton419a94d2017-08-28 10:24:22 -060048static inline Sk2f normalize(const Sk2f& n) {
49 Sk2f nn = n*n;
50 return n * (nn + SkNx_shuffle<1,0>(nn)).rsqrt();
51}
52
53static inline float dot(const Sk2f& a, const Sk2f& b) {
54 float product[2];
55 (a * b).store(product);
56 return product[0] + product[1];
57}
58
59// Returns whether the (convex) curve segment is monotonic with respect to [endPt - startPt].
60static inline bool is_convex_curve_monotonic(const Sk2f& startPt, const Sk2f& startTan,
61 const Sk2f& endPt, const Sk2f& endTan) {
62 Sk2f v = endPt - startPt;
63 float dot0 = dot(startTan, v);
64 float dot1 = dot(endTan, v);
65
66 // A small, negative tolerance handles floating-point error in the case when one tangent
67 // approaches 0 length, meaning the (convex) curve segment is effectively a flat line.
68 float tolerance = -std::max(std::abs(dot0), std::abs(dot1)) * SK_ScalarNearlyZero;
69 return dot0 >= tolerance && dot1 >= tolerance;
70}
71
72static inline Sk2f lerp(const Sk2f& a, const Sk2f& b, const Sk2f& t) {
73 return SkNx_fma(t, b - a, a);
74}
75
Chris Daltonc1e59632017-09-05 00:30:07 -060076void GrCCPRGeometry::quadraticTo(const SkPoint& devP0, const SkPoint& devP1) {
77 SkASSERT(fBuildingContour);
78
79 Sk2f p0 = Sk2f::Load(&fCurrFanPoint);
80 Sk2f p1 = Sk2f::Load(&devP0);
81 Sk2f p2 = Sk2f::Load(&devP1);
82 fCurrFanPoint = devP1;
Chris Dalton419a94d2017-08-28 10:24:22 -060083
84 Sk2f tan0 = p1 - p0;
85 Sk2f tan1 = p2 - p1;
86 // This should almost always be this case for well-behaved curves in the real world.
87 if (is_convex_curve_monotonic(p0, tan0, p2, tan1)) {
Chris Daltonc1e59632017-09-05 00:30:07 -060088 this->appendMonotonicQuadratic(p1, p2);
89 return;
Chris Dalton419a94d2017-08-28 10:24:22 -060090 }
91
92 // Chop the curve into two segments with equal curvature. To do this we find the T value whose
93 // tangent is perpendicular to the vector that bisects tan0 and -tan1.
94 Sk2f n = normalize(tan0) - normalize(tan1);
95
96 // This tangent can be found where (dQ(t) dot n) = 0:
97 //
98 // 0 = (dQ(t) dot n) = | 2*t 1 | * | p0 - 2*p1 + p2 | * | n |
99 // | -2*p0 + 2*p1 | | . |
100 //
101 // = | 2*t 1 | * | tan1 - tan0 | * | n |
102 // | 2*tan0 | | . |
103 //
104 // = 2*t * ((tan1 - tan0) dot n) + (2*tan0 dot n)
105 //
106 // t = (tan0 dot n) / ((tan0 - tan1) dot n)
107 Sk2f dQ1n = (tan0 - tan1) * n;
108 Sk2f dQ0n = tan0 * n;
109 Sk2f t = (dQ0n + SkNx_shuffle<1,0>(dQ0n)) / (dQ1n + SkNx_shuffle<1,0>(dQ1n));
110 t = Sk2f::Min(Sk2f::Max(t, 0), 1); // Clamp for FP error.
111
112 Sk2f p01 = SkNx_fma(t, tan0, p0);
113 Sk2f p12 = SkNx_fma(t, tan1, p1);
114 Sk2f p012 = lerp(p01, p12, t);
115
Chris Daltonc1e59632017-09-05 00:30:07 -0600116 this->appendMonotonicQuadratic(p01, p012);
117 this->appendMonotonicQuadratic(p12, p2);
118}
Chris Dalton419a94d2017-08-28 10:24:22 -0600119
Chris Daltonc1e59632017-09-05 00:30:07 -0600120inline void GrCCPRGeometry::appendMonotonicQuadratic(const Sk2f& p1, const Sk2f& p2) {
121 p1.store(&fPoints.push_back());
122 p2.store(&fPoints.push_back());
123 fVerbs.push_back(Verb::kMonotonicQuadraticTo);
124 ++fCurrContourTallies.fQuadratics;
125}
126
Chris Dalton7f578bf2017-09-05 16:46:48 -0600127using ExcludedTerm = GrPathUtils::ExcludedTerm;
Chris Daltonc1e59632017-09-05 00:30:07 -0600128
Chris Dalton7f578bf2017-09-05 16:46:48 -0600129// Calculates the padding to apply around inflection points, in homogeneous parametric coordinates.
130//
131// More specifically, if the inflection point lies at C(t/s), then C((t +/- returnValue) / s) will
132// be the two points on the curve at which a square box with radius "padRadius" will have a corner
133// that touches the inflection point's tangent line.
134//
135// A serpentine cubic has two inflection points, so this method takes Sk2f and computes the padding
136// for both in SIMD.
137static inline Sk2f calc_inflect_homogeneous_padding(float padRadius, const Sk2f& t, const Sk2f& s,
138 const SkMatrix& CIT, ExcludedTerm skipTerm) {
139 SkASSERT(padRadius >= 0);
Chris Daltonc1e59632017-09-05 00:30:07 -0600140
Chris Dalton7f578bf2017-09-05 16:46:48 -0600141 Sk2f Clx = s*s*s;
142 Sk2f Cly = (ExcludedTerm::kLinearTerm == skipTerm) ? s*s*t*-3 : s*t*t*3;
143
144 Sk2f Lx = CIT[0] * Clx + CIT[3] * Cly;
145 Sk2f Ly = CIT[1] * Clx + CIT[4] * Cly;
146
147 float ret[2];
148 Sk2f bloat = padRadius * (Lx.abs() + Ly.abs());
149 (bloat * s >= 0).thenElse(bloat, -bloat).store(ret);
150
151 ret[0] = cbrtf(ret[0]);
152 ret[1] = cbrtf(ret[1]);
153 return Sk2f::Load(ret);
154}
155
156static inline void swap_if_greater(float& a, float& b) {
157 if (a > b) {
158 std::swap(a, b);
159 }
160}
161
162// Calculates all parameter values for a loop at which points a square box with radius "padRadius"
163// will have a corner that touches a tangent line from the intersection.
164//
165// T2 must contain the lesser parameter value of the loop intersection in its first component, and
166// the greater in its second.
167//
168// roots[0] will be filled with 1 or 3 sorted parameter values, representing the padding points
169// around the first tangent. roots[1] will be filled with the padding points for the second tangent.
170static inline void calc_loop_intersect_padding_pts(float padRadius, const Sk2f& T2,
171 const SkMatrix& CIT, ExcludedTerm skipTerm,
172 SkSTArray<3, float, true> roots[2]) {
173 SkASSERT(padRadius >= 0);
174 SkASSERT(T2[0] <= T2[1]);
175 SkASSERT(roots[0].empty());
176 SkASSERT(roots[1].empty());
177
178 Sk2f T1 = SkNx_shuffle<1,0>(T2);
179 Sk2f Cl = (ExcludedTerm::kLinearTerm == skipTerm) ? T2*-2 - T1 : T2*T2 + T2*T1*2;
180 Sk2f Lx = Cl * CIT[3] + CIT[0];
181 Sk2f Ly = Cl * CIT[4] + CIT[1];
182
183 Sk2f bloat = Sk2f(+.5f * padRadius, -.5f * padRadius) * (Lx.abs() + Ly.abs());
184 Sk2f q = (1.f/3) * (T2 - T1);
185
186 Sk2f qqq = q*q*q;
187 Sk2f discr = qqq*bloat*2 + bloat*bloat;
188
189 float numRoots[2], D[2];
190 (discr < 0).thenElse(3, 1).store(numRoots);
191 (T2 - q).store(D);
192
193 // Values for calculating one root.
194 float R[2], QQ[2];
195 if ((discr >= 0).anyTrue()) {
196 Sk2f r = qqq + bloat;
197 Sk2f s = r.abs() + discr.sqrt();
198 (r > 0).thenElse(-s, s).store(R);
199 (q*q).store(QQ);
Chris Daltonc1e59632017-09-05 00:30:07 -0600200 }
201
Chris Dalton7f578bf2017-09-05 16:46:48 -0600202 // Values for calculating three roots.
203 float P[2], cosTheta3[2];
204 if ((discr < 0).anyTrue()) {
205 (q.abs() * -2).store(P);
206 ((q >= 0).thenElse(1, -1) + bloat / qqq.abs()).store(cosTheta3);
Chris Daltonc1e59632017-09-05 00:30:07 -0600207 }
208
Chris Dalton7f578bf2017-09-05 16:46:48 -0600209 for (int i = 0; i < 2; ++i) {
210 if (1 == numRoots[i]) {
211 float A = cbrtf(R[i]);
212 float B = A != 0 ? QQ[i]/A : 0;
213 roots[i].push_back(A + B + D[i]);
Chris Daltonc1e59632017-09-05 00:30:07 -0600214 continue;
215 }
216
Chris Dalton7f578bf2017-09-05 16:46:48 -0600217 static constexpr float k2PiOver3 = 2 * SK_ScalarPI / 3;
218 float theta = std::acos(cosTheta3[i]) * (1.f/3);
219 roots[i].push_back(P[i] * std::cos(theta) + D[i]);
220 roots[i].push_back(P[i] * std::cos(theta + k2PiOver3) + D[i]);
221 roots[i].push_back(P[i] * std::cos(theta - k2PiOver3) + D[i]);
Chris Daltonc1e59632017-09-05 00:30:07 -0600222
Chris Dalton7f578bf2017-09-05 16:46:48 -0600223 // Sort the three roots.
224 swap_if_greater(roots[i][0], roots[i][1]);
225 swap_if_greater(roots[i][1], roots[i][2]);
226 swap_if_greater(roots[i][0], roots[i][1]);
227 }
228}
229
230void GrCCPRGeometry::cubicTo(const SkPoint& devP1, const SkPoint& devP2, const SkPoint& devP3,
231 float inflectPad, float loopIntersectPad) {
232 SkASSERT(fBuildingContour);
233
234 SkPoint devPts[4] = {fCurrFanPoint, devP1, devP2, devP3};
235 Sk2f p0 = Sk2f::Load(&fCurrFanPoint);
236 Sk2f p1 = Sk2f::Load(&devP1);
237 Sk2f p2 = Sk2f::Load(&devP2);
238 Sk2f p3 = Sk2f::Load(&devP3);
239 fCurrFanPoint = devP3;
240
241 double tt[2], ss[2];
242 fCurrCubicType = SkClassifyCubic(devPts, tt, ss);
243 if (SkCubicIsDegenerate(fCurrCubicType)) {
244 // Allow one subdivision in case the curve is quadratic, but not monotonic.
245 this->appendCubicApproximation(p0, p1, p2, p3, /*maxSubdivisions=*/1);
246 return;
247 }
248
249 SkMatrix CIT;
250 ExcludedTerm skipTerm = GrPathUtils::calcCubicInverseTransposePowerBasisMatrix(devPts, &CIT);
251 if (ExcludedTerm::kNonInvertible == skipTerm) {
252 // This could technically also happen if the curve were a quadratic, but SkClassifyCubic
253 // should have detected that case already with tolerance.
254 fCurrCubicType = SkCubicType::kLineOrPoint;
255 this->appendCubicApproximation(p0, p1, p2, p3, /*maxSubdivisions=*/0);
256 return;
257 }
258 SkASSERT(0 == CIT[6]);
259 SkASSERT(0 == CIT[7]);
260 SkASSERT(1 == CIT[8]);
261
262 // Each cubic has five different sections (not always inside t=[0..1]):
263 //
264 // 1. The section before the first inflection or loop intersection point, with padding.
265 // 2. The section that passes through the first inflection/intersection (aka the K,L
266 // intersection point or T=tt[0]/ss[0]).
267 // 3. The section between the two inflections/intersections, with padding.
268 // 4. The section that passes through the second inflection/intersection (aka the K,M
269 // intersection point or T=tt[1]/ss[1]).
270 // 5. The section after the second inflection/intersection, with padding.
271 //
272 // Sections 1,3,5 can be rendered directly using the CCPR cubic shader.
273 //
274 // Sections 2 & 4 must be approximated. For loop intersections we render them with
275 // quadratic(s), and when passing through an inflection point we use a plain old flat line.
276 //
277 // We find T0..T3 below to be the dividing points between these five sections.
278 float T0, T1, T2, T3;
279 if (SkCubicType::kLoop != fCurrCubicType) {
280 Sk2f t = Sk2f(static_cast<float>(tt[0]), static_cast<float>(tt[1]));
281 Sk2f s = Sk2f(static_cast<float>(ss[0]), static_cast<float>(ss[1]));
282 Sk2f pad = calc_inflect_homogeneous_padding(inflectPad, t, s, CIT, skipTerm);
283
284 float T[2];
285 ((t - pad) / s).store(T);
286 T0 = T[0];
287 T2 = T[1];
288
289 ((t + pad) / s).store(T);
290 T1 = T[0];
291 T3 = T[1];
292 } else {
293 const float T[2] = {static_cast<float>(tt[0]/ss[0]), static_cast<float>(tt[1]/ss[1])};
294 SkSTArray<3, float, true> roots[2];
295 calc_loop_intersect_padding_pts(loopIntersectPad, Sk2f::Load(T), CIT, skipTerm, roots);
296 T0 = roots[0].front();
297 if (1 == roots[0].count() || 1 == roots[1].count()) {
298 // The loop is tighter than our desired padding. Collapse the middle section to a point
299 // somewhere in the middle-ish of the loop and Sections 2 & 4 will approximate the the
300 // whole thing with quadratics.
301 T1 = T2 = (T[0] + T[1]) * .5f;
302 } else {
303 T1 = roots[0][1];
304 T2 = roots[1][1];
305 }
306 T3 = roots[1].back();
307 }
308
309 // Guarantee that T0..T3 are monotonic.
310 if (T0 > T3) {
311 // This is not a mathematically valid scenario. The only reason it would happen is if
312 // padding is very small and we have encountered FP rounding error.
313 T0 = T1 = T2 = T3 = (T0 + T3) / 2;
314 } else if (T1 > T2) {
315 // This just means padding before the middle section overlaps the padding after it. We
316 // collapse the middle section to a single point that splits the difference between the
317 // overlap in padding.
318 T1 = T2 = (T1 + T2) / 2;
319 }
320 // Clamp T1 & T2 inside T0..T3. The only reason this would be necessary is if we have
321 // encountered FP rounding error.
322 T1 = std::max(T0, std::min(T1, T3));
323 T2 = std::max(T0, std::min(T2, T3));
324
325 // Next we chop the cubic up at all T0..T3 inside 0..1 and store the resulting segments.
326 if (T1 >= 1) {
327 // Only sections 1 & 2 can be in 0..1.
328 this->chopCubic<&GrCCPRGeometry::appendMonotonicCubics,
329 &GrCCPRGeometry::appendCubicApproximation>(p0, p1, p2, p3, T0);
330 return;
331 }
332
333 if (T2 <= 0) {
334 // Only sections 4 & 5 can be in 0..1.
335 this->chopCubic<&GrCCPRGeometry::appendCubicApproximation,
336 &GrCCPRGeometry::appendMonotonicCubics>(p0, p1, p2, p3, T3);
337 return;
338 }
339
340 Sk2f midp0, midp1; // These hold the first two bezier points of the middle section, if needed.
341
342 if (T1 > 0) {
343 Sk2f T1T1 = Sk2f(T1);
344 Sk2f ab1 = lerp(p0, p1, T1T1);
345 Sk2f bc1 = lerp(p1, p2, T1T1);
346 Sk2f cd1 = lerp(p2, p3, T1T1);
347 Sk2f abc1 = lerp(ab1, bc1, T1T1);
348 Sk2f bcd1 = lerp(bc1, cd1, T1T1);
349 Sk2f abcd1 = lerp(abc1, bcd1, T1T1);
350
351 // Sections 1 & 2.
352 this->chopCubic<&GrCCPRGeometry::appendMonotonicCubics,
353 &GrCCPRGeometry::appendCubicApproximation>(p0, ab1, abc1, abcd1, T0/T1);
354
355 if (T2 >= 1) {
356 // The rest of the curve is Section 3 (middle section).
357 this->appendMonotonicCubics(abcd1, bcd1, cd1, p3);
358 return;
Chris Daltonc1e59632017-09-05 00:30:07 -0600359 }
360
Chris Dalton7f578bf2017-09-05 16:46:48 -0600361 // Now calculate the first two bezier points of the middle section. The final two will come
362 // from when we chop the other side, as that is numerically more stable.
363 midp0 = abcd1;
364 midp1 = lerp(abcd1, bcd1, Sk2f((T2 - T1) / (1 - T1)));
365 } else if (T2 >= 1) {
366 // The entire cubic is Section 3 (middle section).
367 this->appendMonotonicCubics(p0, p1, p2, p3);
368 return;
Chris Daltonc1e59632017-09-05 00:30:07 -0600369 }
370
Chris Dalton7f578bf2017-09-05 16:46:48 -0600371 SkASSERT(T2 > 0 && T2 < 1);
372
373 Sk2f T2T2 = Sk2f(T2);
374 Sk2f ab2 = lerp(p0, p1, T2T2);
375 Sk2f bc2 = lerp(p1, p2, T2T2);
376 Sk2f cd2 = lerp(p2, p3, T2T2);
377 Sk2f abc2 = lerp(ab2, bc2, T2T2);
378 Sk2f bcd2 = lerp(bc2, cd2, T2T2);
379 Sk2f abcd2 = lerp(abc2, bcd2, T2T2);
380
381 if (T1 <= 0) {
382 // The curve begins at Section 3 (middle section).
383 this->appendMonotonicCubics(p0, ab2, abc2, abcd2);
384 } else if (T2 > T1) {
385 // Section 3 (middle section).
386 Sk2f midp2 = lerp(abc2, abcd2, T1/T2);
387 this->appendMonotonicCubics(midp0, midp1, midp2, abcd2);
388 }
389
390 // Sections 4 & 5.
391 this->chopCubic<&GrCCPRGeometry::appendCubicApproximation,
392 &GrCCPRGeometry::appendMonotonicCubics>(abcd2, bcd2, cd2, p3, (T3-T2) / (1-T2));
Chris Daltonc1e59632017-09-05 00:30:07 -0600393}
394
Chris Dalton7f578bf2017-09-05 16:46:48 -0600395static inline Sk2f first_unless_nearly_zero(const Sk2f& a, const Sk2f& b) {
396 Sk2f aa = a*a;
397 aa += SkNx_shuffle<1,0>(aa);
398 SkASSERT(aa[0] == aa[1]);
399
400 Sk2f bb = b*b;
401 bb += SkNx_shuffle<1,0>(bb);
402 SkASSERT(bb[0] == bb[1]);
403
404 return (aa > bb * SK_ScalarNearlyZero).thenElse(a, b);
Chris Daltonc1e59632017-09-05 00:30:07 -0600405}
406
Chris Dalton7f578bf2017-09-05 16:46:48 -0600407template<GrCCPRGeometry::AppendCubicFn AppendLeftRight>
408inline void GrCCPRGeometry::chopCubicAtMidTangent(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
409 const Sk2f& p3, const Sk2f& tan0,
410 const Sk2f& tan3, int maxFutureSubdivisions) {
411 // Find the T value whose tangent is perpendicular to the vector that bisects tan0 and -tan3.
412 Sk2f n = normalize(tan0) - normalize(tan3);
413
414 float a = 3 * dot(p3 + (p1 - p2)*3 - p0, n);
415 float b = 6 * dot(p0 - p1*2 + p2, n);
416 float c = 3 * dot(p1 - p0, n);
417
418 float discr = b*b - 4*a*c;
419 if (discr < 0) {
420 // If this is the case then the cubic must be nearly flat.
421 (this->*AppendLeftRight)(p0, p1, p2, p3, maxFutureSubdivisions);
422 return;
423 }
424
425 float q = -.5f * (b + copysignf(std::sqrt(discr), b));
426 float m = .5f*q*a;
427 float T = std::abs(q*q - m) < std::abs(a*c - m) ? q/a : c/q;
428
429 this->chopCubic<AppendLeftRight, AppendLeftRight>(p0, p1, p2, p3, T, maxFutureSubdivisions);
430}
431
432template<GrCCPRGeometry::AppendCubicFn AppendLeft, GrCCPRGeometry::AppendCubicFn AppendRight>
433inline void GrCCPRGeometry::chopCubic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
434 const Sk2f& p3, float T, int maxFutureSubdivisions) {
435 if (T >= 1) {
436 (this->*AppendLeft)(p0, p1, p2, p3, maxFutureSubdivisions);
437 return;
438 }
439
440 if (T <= 0) {
441 (this->*AppendRight)(p0, p1, p2, p3, maxFutureSubdivisions);
442 return;
443 }
444
445 Sk2f TT = T;
446 Sk2f ab = lerp(p0, p1, TT);
447 Sk2f bc = lerp(p1, p2, TT);
448 Sk2f cd = lerp(p2, p3, TT);
449 Sk2f abc = lerp(ab, bc, TT);
450 Sk2f bcd = lerp(bc, cd, TT);
451 Sk2f abcd = lerp(abc, bcd, TT);
452 (this->*AppendLeft)(p0, ab, abc, abcd, maxFutureSubdivisions);
453 (this->*AppendRight)(abcd, bcd, cd, p3, maxFutureSubdivisions);
454}
455
456void GrCCPRGeometry::appendMonotonicCubics(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
457 const Sk2f& p3, int maxSubdivisions) {
458 if ((p0 == p3).allTrue()) {
459 return;
460 }
461
462 if (maxSubdivisions) {
463 Sk2f tan0 = first_unless_nearly_zero(p1 - p0, p2 - p0);
464 Sk2f tan3 = first_unless_nearly_zero(p3 - p2, p3 - p1);
465
466 if (!is_convex_curve_monotonic(p0, tan0, p3, tan3)) {
467 this->chopCubicAtMidTangent<&GrCCPRGeometry::appendMonotonicCubics>(p0, p1, p2, p3,
468 tan0, tan3,
469 maxSubdivisions-1);
470 return;
471 }
472 }
473
474 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
475 p1.store(&fPoints.push_back());
476 p2.store(&fPoints.push_back());
477 p3.store(&fPoints.push_back());
478 if (SkCubicType::kLoop != fCurrCubicType) {
479 fVerbs.push_back(Verb::kMonotonicSerpentineTo);
Chris Daltonc1e59632017-09-05 00:30:07 -0600480 ++fCurrContourTallies.fSerpentines;
481 } else {
Chris Dalton7f578bf2017-09-05 16:46:48 -0600482 fVerbs.push_back(Verb::kMonotonicLoopTo);
Chris Daltonc1e59632017-09-05 00:30:07 -0600483 ++fCurrContourTallies.fLoops;
484 }
485}
486
Chris Dalton7f578bf2017-09-05 16:46:48 -0600487void GrCCPRGeometry::appendCubicApproximation(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
488 const Sk2f& p3, int maxSubdivisions) {
489 if ((p0 == p3).allTrue()) {
490 return;
491 }
492
493 if (SkCubicType::kLoop != fCurrCubicType && SkCubicType::kQuadratic != fCurrCubicType) {
494 // This section passes through an inflection point, so we can get away with a flat line.
495 // This can cause some curves to feel slightly more flat when inspected rigorously back and
496 // forth against another renderer, but for now this seems acceptable given the simplicity.
497 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
498 p3.store(&fPoints.push_back());
499 fVerbs.push_back(Verb::kLineTo);
500 return;
501 }
502
503 Sk2f tan0 = first_unless_nearly_zero(p1 - p0, p2 - p0);
504 Sk2f tan3 = first_unless_nearly_zero(p3 - p2, p3 - p1);
505
506 Sk2f c1 = SkNx_fma(Sk2f(1.5f), tan0, p0);
507 Sk2f c2 = SkNx_fma(Sk2f(-1.5f), tan3, p3);
508
509 if (maxSubdivisions) {
510 bool nearlyQuadratic = ((c1 - c2).abs() <= 1).allTrue();
511
512 if (!nearlyQuadratic || !is_convex_curve_monotonic(p0, tan0, p3, tan3)) {
513 this->chopCubicAtMidTangent<&GrCCPRGeometry::appendCubicApproximation>(p0, p1, p2, p3,
514 tan0, tan3,
515 maxSubdivisions-1);
516 return;
517 }
518 }
519
520 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
521 this->appendMonotonicQuadratic((c1 + c2) * .5f, p3);
522}
523
Chris Daltonc1e59632017-09-05 00:30:07 -0600524GrCCPRGeometry::PrimitiveTallies GrCCPRGeometry::endContour() {
525 SkASSERT(fBuildingContour);
526 SkASSERT(fVerbs.count() >= fCurrContourTallies.fTriangles);
527
528 // The fTriangles field currently contains this contour's starting verb index. We can now
529 // use it to calculate the size of the contour's fan.
530 int fanSize = fVerbs.count() - fCurrContourTallies.fTriangles;
531 if (fCurrFanPoint == fCurrAnchorPoint) {
532 --fanSize;
533 fVerbs.push_back(Verb::kEndClosedContour);
534 } else {
535 fVerbs.push_back(Verb::kEndOpenContour);
536 }
537
538 fCurrContourTallies.fTriangles = SkTMax(fanSize - 2, 0);
539
540 SkDEBUGCODE(fBuildingContour = false;)
541 return fCurrContourTallies;
Chris Dalton419a94d2017-08-28 10:24:22 -0600542}