blob: 9fbf3e61f80c2a56129ad657d827aad98a7adcac [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
Chris Dalton383a2ef2018-01-08 17:21:41 -05008#include "GrCCGeometry.h"
Chris Dalton419a94d2017-08-28 10:24:22 -06009
10#include "GrTypes.h"
Chris Dalton4229b352018-04-18 14:13:45 -060011#include "SkGeometry.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 Daltond8bae7d2018-04-19 13:13:25 -060021static constexpr float kFlatnessThreshold = 1/16.f; // 1/16 of a pixel.
22
Chris Dalton383a2ef2018-01-08 17:21:41 -050023void GrCCGeometry::beginPath() {
Chris Daltonc1e59632017-09-05 00:30:07 -060024 SkASSERT(!fBuildingContour);
25 fVerbs.push_back(Verb::kBeginPath);
26}
27
Chris Dalton7ca3b7b2018-04-10 00:21:19 -060028void GrCCGeometry::beginContour(const SkPoint& pt) {
Chris Daltonc1e59632017-09-05 00:30:07 -060029 SkASSERT(!fBuildingContour);
Chris Daltonc1e59632017-09-05 00:30:07 -060030 // Store the current verb count in the fTriangles field for now. When we close the contour we
31 // will use this value to calculate the actual number of triangles in its fan.
Chris Dalton9f2dab02018-04-18 14:07:03 -060032 fCurrContourTallies = {fVerbs.count(), 0, 0, 0, 0};
Chris Daltonc1e59632017-09-05 00:30:07 -060033
Chris Dalton7ca3b7b2018-04-10 00:21:19 -060034 fPoints.push_back(pt);
Chris Daltonc1e59632017-09-05 00:30:07 -060035 fVerbs.push_back(Verb::kBeginContour);
Chris Dalton7ca3b7b2018-04-10 00:21:19 -060036 fCurrAnchorPoint = pt;
Chris Daltonc1e59632017-09-05 00:30:07 -060037
Chris Dalton383a2ef2018-01-08 17:21:41 -050038 SkDEBUGCODE(fBuildingContour = true);
Chris Daltonc1e59632017-09-05 00:30:07 -060039}
40
Chris Dalton7ca3b7b2018-04-10 00:21:19 -060041void GrCCGeometry::lineTo(const SkPoint& pt) {
Chris Daltonc1e59632017-09-05 00:30:07 -060042 SkASSERT(fBuildingContour);
Chris Dalton7ca3b7b2018-04-10 00:21:19 -060043 fPoints.push_back(pt);
44 fVerbs.push_back(Verb::kLineTo);
45}
46
47void GrCCGeometry::appendLine(const Sk2f& endpt) {
48 endpt.store(&fPoints.push_back());
Chris Daltonc1e59632017-09-05 00:30:07 -060049 fVerbs.push_back(Verb::kLineTo);
50}
51
Chris Dalton419a94d2017-08-28 10:24:22 -060052static inline Sk2f normalize(const Sk2f& n) {
53 Sk2f nn = n*n;
54 return n * (nn + SkNx_shuffle<1,0>(nn)).rsqrt();
55}
56
57static inline float dot(const Sk2f& a, const Sk2f& b) {
58 float product[2];
59 (a * b).store(product);
60 return product[0] + product[1];
61}
62
Chris Daltonb0601a42018-04-10 00:23:45 -060063static inline bool are_collinear(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
Chris Daltond8bae7d2018-04-19 13:13:25 -060064 float tolerance = kFlatnessThreshold) {
Chris Daltonb0601a42018-04-10 00:23:45 -060065 Sk2f l = p2 - p0; // Line from p0 -> p2.
Chris Dalton900cd052017-09-07 10:36:51 -060066
Chris Daltonb0601a42018-04-10 00:23:45 -060067 // lwidth = Manhattan width of l.
68 Sk2f labs = l.abs();
69 float lwidth = labs[0] + labs[1];
Chris Dalton900cd052017-09-07 10:36:51 -060070
Chris Daltonb0601a42018-04-10 00:23:45 -060071 // d = |p1 - p0| dot | l.y|
72 // |-l.x| = distance from p1 to l.
73 Sk2f dd = (p1 - p0) * SkNx_shuffle<1,0>(l);
74 float d = dd[0] - dd[1];
Chris Dalton900cd052017-09-07 10:36:51 -060075
Chris Daltonb0601a42018-04-10 00:23:45 -060076 // We are collinear if a box with radius "tolerance", centered on p1, touches the line l.
77 // To decide this, we check if the distance from p1 to the line is less than the distance from
78 // p1 to the far corner of this imaginary box, along that same normal vector.
79 // The far corner of the box can be found at "p1 + sign(n) * tolerance", where n is normal to l:
80 //
81 // abs(dot(p1 - p0, n)) <= dot(sign(n) * tolerance, n)
82 //
83 // Which reduces to:
84 //
85 // abs(d) <= (n.x * sign(n.x) + n.y * sign(n.y)) * tolerance
86 // abs(d) <= (abs(n.x) + abs(n.y)) * tolerance
87 //
88 // Use "<=" in case l == 0.
89 return std::abs(d) <= lwidth * tolerance;
90}
91
Chris Daltond8bae7d2018-04-19 13:13:25 -060092static inline bool are_collinear(const SkPoint P[4], float tolerance = kFlatnessThreshold) {
Chris Daltonb0601a42018-04-10 00:23:45 -060093 Sk4f Px, Py; // |Px Py| |p0 - p3|
94 Sk4f::Load2(P, &Px, &Py); // |. . | = |p1 - p3|
95 Px -= Px[3]; // |. . | |p2 - p3|
96 Py -= Py[3]; // |. . | | 0 |
97
98 // Find [lx, ly] = the line from p3 to the furthest-away point from p3.
99 Sk4f Pwidth = Px.abs() + Py.abs(); // Pwidth = Manhattan width of each point.
100 int lidx = Pwidth[0] > Pwidth[1] ? 0 : 1;
101 lidx = Pwidth[lidx] > Pwidth[2] ? lidx : 2;
102 float lx = Px[lidx], ly = Py[lidx];
103 float lwidth = Pwidth[lidx]; // lwidth = Manhattan width of [lx, ly].
104
105 // |Px Py|
106 // d = |. . | * | ly| = distances from each point to l (two of the distances will be zero).
107 // |. . | |-lx|
108 // |. . |
109 Sk4f d = Px*ly - Py*lx;
110
111 // We are collinear if boxes with radius "tolerance", centered on all 4 points all touch line l.
112 // (See the rationale for this formula in the above, 3-point version of this function.)
113 // Use "<=" in case l == 0.
114 return (d.abs() <= lwidth * tolerance).allTrue();
Chris Dalton900cd052017-09-07 10:36:51 -0600115}
116
Chris Dalton419a94d2017-08-28 10:24:22 -0600117// Returns whether the (convex) curve segment is monotonic with respect to [endPt - startPt].
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600118static inline bool is_convex_curve_monotonic(const Sk2f& startPt, const Sk2f& tan0,
119 const Sk2f& endPt, const Sk2f& tan1) {
Chris Dalton419a94d2017-08-28 10:24:22 -0600120 Sk2f v = endPt - startPt;
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600121 float dot0 = dot(tan0, v);
122 float dot1 = dot(tan1, v);
Chris Dalton419a94d2017-08-28 10:24:22 -0600123
124 // A small, negative tolerance handles floating-point error in the case when one tangent
125 // approaches 0 length, meaning the (convex) curve segment is effectively a flat line.
126 float tolerance = -std::max(std::abs(dot0), std::abs(dot1)) * SK_ScalarNearlyZero;
127 return dot0 >= tolerance && dot1 >= tolerance;
128}
129
Chris Dalton9f2dab02018-04-18 14:07:03 -0600130template<int N> static inline SkNx<N,float> lerp(const SkNx<N,float>& a, const SkNx<N,float>& b,
131 const SkNx<N,float>& t) {
Chris Dalton419a94d2017-08-28 10:24:22 -0600132 return SkNx_fma(t, b - a, a);
133}
134
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600135void GrCCGeometry::quadraticTo(const SkPoint P[3]) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600136 SkASSERT(fBuildingContour);
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600137 SkASSERT(P[0] == fPoints.back());
138 Sk2f p0 = Sk2f::Load(P);
139 Sk2f p1 = Sk2f::Load(P+1);
140 Sk2f p2 = Sk2f::Load(P+2);
Chris Daltonc1e59632017-09-05 00:30:07 -0600141
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600142 // Don't crunch on the curve if it is nearly flat (or just very small). Flat curves can break
143 // The monotonic chopping math.
144 if (are_collinear(p0, p1, p2)) {
145 this->appendLine(p2);
146 return;
147 }
Chris Dalton419a94d2017-08-28 10:24:22 -0600148
Chris Daltonb3a69592018-04-18 14:10:22 -0600149 this->appendQuadratics(p0, p1, p2);
Chris Dalton29011a22017-09-28 12:08:33 -0600150}
151
Chris Daltonb3a69592018-04-18 14:10:22 -0600152inline void GrCCGeometry::appendQuadratics(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2) {
Chris Dalton419a94d2017-08-28 10:24:22 -0600153 Sk2f tan0 = p1 - p0;
154 Sk2f tan1 = p2 - p1;
Chris Dalton29011a22017-09-28 12:08:33 -0600155
Chris Dalton419a94d2017-08-28 10:24:22 -0600156 // This should almost always be this case for well-behaved curves in the real world.
Chris Dalton43646532017-12-07 12:47:02 -0700157 if (is_convex_curve_monotonic(p0, tan0, p2, tan1)) {
Chris Daltonb3a69592018-04-18 14:10:22 -0600158 this->appendMonotonicQuadratic(p0, p1, p2);
Chris Daltonc1e59632017-09-05 00:30:07 -0600159 return;
Chris Dalton419a94d2017-08-28 10:24:22 -0600160 }
161
162 // Chop the curve into two segments with equal curvature. To do this we find the T value whose
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600163 // tangent angle is halfway between tan0 and tan1.
Chris Dalton419a94d2017-08-28 10:24:22 -0600164 Sk2f n = normalize(tan0) - normalize(tan1);
165
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600166 // The midtangent can be found where (dQ(t) dot n) = 0:
Chris Dalton419a94d2017-08-28 10:24:22 -0600167 //
168 // 0 = (dQ(t) dot n) = | 2*t 1 | * | p0 - 2*p1 + p2 | * | n |
169 // | -2*p0 + 2*p1 | | . |
170 //
171 // = | 2*t 1 | * | tan1 - tan0 | * | n |
172 // | 2*tan0 | | . |
173 //
174 // = 2*t * ((tan1 - tan0) dot n) + (2*tan0 dot n)
175 //
176 // t = (tan0 dot n) / ((tan0 - tan1) dot n)
177 Sk2f dQ1n = (tan0 - tan1) * n;
178 Sk2f dQ0n = tan0 * n;
179 Sk2f t = (dQ0n + SkNx_shuffle<1,0>(dQ0n)) / (dQ1n + SkNx_shuffle<1,0>(dQ1n));
180 t = Sk2f::Min(Sk2f::Max(t, 0), 1); // Clamp for FP error.
181
182 Sk2f p01 = SkNx_fma(t, tan0, p0);
183 Sk2f p12 = SkNx_fma(t, tan1, p1);
184 Sk2f p012 = lerp(p01, p12, t);
185
Chris Daltonb3a69592018-04-18 14:10:22 -0600186 this->appendMonotonicQuadratic(p0, p01, p012);
187 this->appendMonotonicQuadratic(p012, p12, p2);
Chris Dalton43646532017-12-07 12:47:02 -0700188}
189
Chris Daltonb3a69592018-04-18 14:10:22 -0600190inline void GrCCGeometry::appendMonotonicQuadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2) {
Chris Dalton43646532017-12-07 12:47:02 -0700191 // Don't send curves to the GPU if we know they are nearly flat (or just very small).
192 if (are_collinear(p0, p1, p2)) {
Chris Daltonb3a69592018-04-18 14:10:22 -0600193 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600194 this->appendLine(p2);
Chris Dalton43646532017-12-07 12:47:02 -0700195 return;
196 }
197
Chris Daltonb3a69592018-04-18 14:10:22 -0600198 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
Chris Dalton43646532017-12-07 12:47:02 -0700199 p1.store(&fPoints.push_back());
Chris Daltonc1e59632017-09-05 00:30:07 -0600200 p2.store(&fPoints.push_back());
Chris Dalton43646532017-12-07 12:47:02 -0700201 fVerbs.push_back(Verb::kMonotonicQuadraticTo);
202 ++fCurrContourTallies.fQuadratics;
Chris Daltonc1e59632017-09-05 00:30:07 -0600203}
204
Chris Daltonb3a69592018-04-18 14:10:22 -0600205static inline Sk2f first_unless_nearly_zero(const Sk2f& a, const Sk2f& b) {
206 Sk2f aa = a*a;
207 aa += SkNx_shuffle<1,0>(aa);
208 SkASSERT(aa[0] == aa[1]);
209
210 Sk2f bb = b*b;
211 bb += SkNx_shuffle<1,0>(bb);
212 SkASSERT(bb[0] == bb[1]);
213
214 return (aa > bb * SK_ScalarNearlyZero).thenElse(a, b);
215}
216
217static inline void get_cubic_tangents(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
218 const Sk2f& p3, Sk2f* tan0, Sk2f* tan1) {
219 *tan0 = first_unless_nearly_zero(p1 - p0, p2 - p0);
220 *tan1 = first_unless_nearly_zero(p3 - p2, p3 - p1);
221}
222
223static inline bool is_cubic_nearly_quadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
224 const Sk2f& p3, const Sk2f& tan0, const Sk2f& tan1,
225 Sk2f* c) {
226 Sk2f c1 = SkNx_fma(Sk2f(1.5f), tan0, p0);
227 Sk2f c2 = SkNx_fma(Sk2f(-1.5f), tan1, p3);
228 *c = (c1 + c2) * .5f; // Hopefully optimized out if not used?
229 return ((c1 - c2).abs() <= 1).allTrue();
230}
231
Chris Dalton4229b352018-04-18 14:13:45 -0600232enum class ExcludedTerm : bool {
233 kQuadraticTerm,
234 kLinearTerm
235};
Chris Daltonc1e59632017-09-05 00:30:07 -0600236
Chris Daltonb3a69592018-04-18 14:10:22 -0600237// Finds where to chop a non-loop around its inflection points. The resulting cubic segments will be
238// chopped such that a box of radius 'padRadius', centered at any point along the curve segment, is
239// guaranteed to not cross the tangent lines at the inflection points (a.k.a lines L & M).
Chris Dalton7f578bf2017-09-05 16:46:48 -0600240//
Chris Dalton5450ab12018-04-18 16:49:13 -0600241// 'chops' will be filled with 0, 2, or 4 T values. The segments between T0..T1 and T2..T3 must be
242// drawn with flat lines instead of cubics.
Chris Dalton7f578bf2017-09-05 16:46:48 -0600243//
244// A serpentine cubic has two inflection points, so this method takes Sk2f and computes the padding
245// for both in SIMD.
Chris Dalton5450ab12018-04-18 16:49:13 -0600246static inline void find_chops_around_inflection_points(float padRadius, Sk2f tl, Sk2f sl,
Chris Dalton4229b352018-04-18 14:13:45 -0600247 const Sk2f& C0, const Sk2f& C1,
248 ExcludedTerm skipTerm, float Cdet,
Chris Daltonb3a69592018-04-18 14:10:22 -0600249 SkSTArray<4, float>* chops) {
250 SkASSERT(chops->empty());
Chris Dalton7f578bf2017-09-05 16:46:48 -0600251 SkASSERT(padRadius >= 0);
Chris Daltonc1e59632017-09-05 00:30:07 -0600252
Chris Dalton4229b352018-04-18 14:13:45 -0600253 padRadius /= std::abs(Cdet); // Scale this single value rather than all of C^-1 later on.
254
Chris Dalton5450ab12018-04-18 16:49:13 -0600255 // The homogeneous parametric functions for distance from lines L & M are:
256 //
257 // l(t,s) = (t*sl - s*tl)^3
258 // m(t,s) = (t*sm - s*tm)^3
259 //
260 // See "Resolution Independent Curve Rendering using Programmable Graphics Hardware",
261 // 4.3 Finding klmn:
262 //
263 // https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf
264 //
265 // From here on we use Sk2f with "L" names, but the second lane will be for line M.
266 tl = (sl > 0).thenElse(tl, -tl); // Tl=tl/sl is the triple root of l(t,s). Normalize so s >= 0.
267 sl = sl.abs();
Chris Dalton7f578bf2017-09-05 16:46:48 -0600268
Chris Dalton5450ab12018-04-18 16:49:13 -0600269 // Convert l(t,s), m(t,s) to power-basis form:
270 //
271 // | l3 m3 |
272 // |l(t,s) m(t,s)| = |t^3 t^2*s t*s^2 s^3| * | l2 m2 |
273 // | l1 m1 |
274 // | l0 m0 |
275 //
276 Sk2f l3 = sl*sl*sl;
277 Sk2f l2or1 = (ExcludedTerm::kLinearTerm == skipTerm) ? sl*sl*tl*-3 : sl*tl*tl*3;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600278
Chris Dalton5450ab12018-04-18 16:49:13 -0600279 // The equation for line L can be found as follows:
280 //
281 // L = C^-1 * (l excluding skipTerm)
282 //
283 // (See comments for GrPathUtils::calcCubicInverseTransposePowerBasisMatrix.)
Chris Dalton4229b352018-04-18 14:13:45 -0600284 // We are only interested in the normal to L, so only need the upper 2x2 of C^-1. And rather
285 // than divide by determinant(C) here, we have already performed this divide on padRadius.
286 Sk2f Lx = C1[1]*l3 - C0[1]*l2or1;
287 Sk2f Ly = -C1[0]*l3 + C0[0]*l2or1;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600288
Chris Dalton5450ab12018-04-18 16:49:13 -0600289 // A box of radius "padRadius" is touching line L if "center dot L" is less than the Manhattan
290 // with of L. (See rationale in are_collinear.)
291 Sk2f Lwidth = Lx.abs() + Ly.abs();
292 Sk2f pad = Lwidth * padRadius;
293
294 // Will T=(t + cbrt(pad))/s be greater than 0? No need to solve roots outside T=0..1.
295 Sk2f insideLeftPad = pad + tl*tl*tl;
296
297 // Will T=(t - cbrt(pad))/s be less than 1? No need to solve roots outside T=0..1.
298 Sk2f tms = tl - sl;
299 Sk2f insideRightPad = pad - tms*tms*tms;
300
301 // Solve for the T values where abs(l(T)) = pad.
302 if (insideLeftPad[0] > 0 && insideRightPad[0] > 0) {
303 float padT = cbrtf(pad[0]);
304 Sk2f pts = (tl[0] + Sk2f(-padT, +padT)) / sl[0];
305 pts.store(chops->push_back_n(2));
306 }
307
308 // Solve for the T values where abs(m(T)) = pad.
309 if (insideLeftPad[1] > 0 && insideRightPad[1] > 0) {
310 float padT = cbrtf(pad[1]);
311 Sk2f pts = (tl[1] + Sk2f(-padT, +padT)) / sl[1];
312 pts.store(chops->push_back_n(2));
313 }
Chris Dalton7f578bf2017-09-05 16:46:48 -0600314}
315
316static inline void swap_if_greater(float& a, float& b) {
317 if (a > b) {
318 std::swap(a, b);
319 }
320}
321
Chris Daltonb3a69592018-04-18 14:10:22 -0600322// Finds where to chop a non-loop around its intersection point. The resulting cubic segments will
323// be chopped such that a box of radius 'padRadius', centered at any point along the curve segment,
324// is guaranteed to not cross the tangent lines at the intersection point (a.k.a lines L & M).
Chris Dalton7f578bf2017-09-05 16:46:48 -0600325//
Chris Daltonb3a69592018-04-18 14:10:22 -0600326// 'chops' will be filled with 0, 2, or 4 T values. The segments between T0..T1 and T2..T3 must be
327// drawn with quadratic splines instead of cubics.
Chris Dalton7f578bf2017-09-05 16:46:48 -0600328//
Chris Daltonb3a69592018-04-18 14:10:22 -0600329// A loop intersection falls at two different T values, so this method takes Sk2f and computes the
330// padding for both in SIMD.
Chris Dalton5450ab12018-04-18 16:49:13 -0600331static inline void find_chops_around_loop_intersection(float padRadius, Sk2f t2, Sk2f s2,
Chris Dalton4229b352018-04-18 14:13:45 -0600332 const Sk2f& C0, const Sk2f& C1,
333 ExcludedTerm skipTerm, float Cdet,
Chris Daltonb3a69592018-04-18 14:10:22 -0600334 SkSTArray<4, float>* chops) {
335 SkASSERT(chops->empty());
Chris Dalton7f578bf2017-09-05 16:46:48 -0600336 SkASSERT(padRadius >= 0);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600337
Chris Dalton4229b352018-04-18 14:13:45 -0600338 padRadius /= std::abs(Cdet); // Scale this single value rather than all of C^-1 later on.
339
Chris Dalton5450ab12018-04-18 16:49:13 -0600340 // The parametric functions for distance from lines L & M are:
341 //
342 // l(T) = (T - Td)^2 * (T - Te)
343 // m(T) = (T - Td) * (T - Te)^2
344 //
345 // See "Resolution Independent Curve Rendering using Programmable Graphics Hardware",
346 // 4.3 Finding klmn:
347 //
348 // https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf
349 Sk2f T2 = t2/s2; // T2 is the double root of l(T).
350 Sk2f T1 = SkNx_shuffle<1,0>(T2); // T1 is the other root of l(T).
Chris Dalton7f578bf2017-09-05 16:46:48 -0600351
Chris Dalton5450ab12018-04-18 16:49:13 -0600352 // Convert l(T), m(T) to power-basis form:
353 //
354 // | 1 1 |
355 // |l(T) m(T)| = |T^3 T^2 T 1| * | l2 m2 |
356 // | l1 m1 |
357 // | l0 m0 |
358 //
359 // From here on we use Sk2f with "L" names, but the second lane will be for line M.
360 Sk2f l2 = SkNx_fma(Sk2f(-2), T2, -T1);
361 Sk2f l1 = T2 * SkNx_fma(Sk2f(2), T1, T2);
362 Sk2f l0 = -T2*T2*T1;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600363
Chris Dalton5450ab12018-04-18 16:49:13 -0600364 // The equation for line L can be found as follows:
365 //
366 // L = C^-1 * (l excluding skipTerm)
367 //
368 // (See comments for GrPathUtils::calcCubicInverseTransposePowerBasisMatrix.)
Chris Dalton4229b352018-04-18 14:13:45 -0600369 // We are only interested in the normal to L, so only need the upper 2x2 of C^-1. And rather
370 // than divide by determinant(C) here, we have already performed this divide on padRadius.
Chris Dalton5450ab12018-04-18 16:49:13 -0600371 Sk2f l2or1 = (ExcludedTerm::kLinearTerm == skipTerm) ? l2 : l1;
Chris Dalton4229b352018-04-18 14:13:45 -0600372 Sk2f Lx = -C0[1]*l2or1 + C1[1]; // l3 is always 1.
373 Sk2f Ly = C0[0]*l2or1 - C1[0];
Chris Dalton7f578bf2017-09-05 16:46:48 -0600374
Chris Dalton5450ab12018-04-18 16:49:13 -0600375 // A box of radius "padRadius" is touching line L if "center dot L" is less than the Manhattan
376 // with of L. (See rationale in are_collinear.)
377 Sk2f Lwidth = Lx.abs() + Ly.abs();
378 Sk2f pad = Lwidth * padRadius;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600379
Chris Dalton5450ab12018-04-18 16:49:13 -0600380 // Is l(T=0) outside the padding around line L?
381 Sk2f lT0 = l0; // l(T=0) = |0 0 0 1| dot |1 l2 l1 l0| = l0
382 Sk2f outsideT0 = lT0.abs() - pad;
383
384 // Is l(T=1) outside the padding around line L?
385 Sk2f lT1 = (Sk2f(1) + l2 + l1 + l0).abs(); // l(T=1) = |1 1 1 1| dot |1 l2 l1 l0|
386 Sk2f outsideT1 = lT1.abs() - pad;
387
388 // Values for solving the cubic.
389 Sk2f p, q, qqq, discr, numRoots, D;
390 bool hasDiscr = false;
391
392 // Values for calculating one root (rarely needed).
393 Sk2f R, QQ;
394 bool hasOneRootVals = false;
Chris Daltonc1e59632017-09-05 00:30:07 -0600395
Chris Dalton7f578bf2017-09-05 16:46:48 -0600396 // Values for calculating three roots.
Chris Dalton5450ab12018-04-18 16:49:13 -0600397 Sk2f P, cosTheta3;
398 bool hasThreeRootVals = false;
Chris Daltonc1e59632017-09-05 00:30:07 -0600399
Chris Dalton5450ab12018-04-18 16:49:13 -0600400 // Solve for the T values where l(T) = +pad and m(T) = -pad.
Chris Dalton7f578bf2017-09-05 16:46:48 -0600401 for (int i = 0; i < 2; ++i) {
Chris Dalton5450ab12018-04-18 16:49:13 -0600402 float T = T2[i]; // T is the point we are chopping around.
403 if ((T < 0 && outsideT0[i] >= 0) || (T > 1 && outsideT1[i] >= 0)) {
404 // The padding around T is completely out of range. No point solving for it.
405 continue;
406 }
407
408 if (!hasDiscr) {
409 p = Sk2f(+.5f, -.5f) * pad;
410 q = (1.f/3) * (T2 - T1);
411 qqq = q*q*q;
412 discr = qqq*p*2 + p*p;
413 numRoots = (discr < 0).thenElse(3, 1);
414 D = T2 - q;
415 hasDiscr = true;
416 }
417
Chris Dalton7f578bf2017-09-05 16:46:48 -0600418 if (1 == numRoots[i]) {
Chris Dalton5450ab12018-04-18 16:49:13 -0600419 if (!hasOneRootVals) {
420 Sk2f r = qqq + p;
421 Sk2f s = r.abs() + discr.sqrt();
422 R = (r > 0).thenElse(-s, s);
423 QQ = q*q;
424 hasOneRootVals = true;
425 }
426
427 float A = cbrtf(R[i]);
428 float B = A != 0 ? QQ[i]/A : 0;
429 // When there is only one root, ine L chops from root..1, line M chops from 0..root.
Chris Daltonb3a69592018-04-18 14:10:22 -0600430 if (1 == i) {
431 chops->push_back(0);
432 }
Chris Daltonb3a69592018-04-18 14:10:22 -0600433 chops->push_back(A + B + D[i]);
434 if (0 == i) {
435 chops->push_back(1);
436 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600437 continue;
438 }
439
Chris Dalton5450ab12018-04-18 16:49:13 -0600440 if (!hasThreeRootVals) {
441 P = q.abs() * -2;
442 cosTheta3 = (q >= 0).thenElse(1, -1) + p / qqq.abs();
443 hasThreeRootVals = true;
444 }
445
Chris Dalton7f578bf2017-09-05 16:46:48 -0600446 static constexpr float k2PiOver3 = 2 * SK_ScalarPI / 3;
447 float theta = std::acos(cosTheta3[i]) * (1.f/3);
Chris Daltonb3a69592018-04-18 14:10:22 -0600448 float roots[3] = {P[i] * std::cos(theta) + D[i],
449 P[i] * std::cos(theta + k2PiOver3) + D[i],
450 P[i] * std::cos(theta - k2PiOver3) + D[i]};
Chris Daltonc1e59632017-09-05 00:30:07 -0600451
Chris Dalton7f578bf2017-09-05 16:46:48 -0600452 // Sort the three roots.
Chris Daltonb3a69592018-04-18 14:10:22 -0600453 swap_if_greater(roots[0], roots[1]);
454 swap_if_greater(roots[1], roots[2]);
455 swap_if_greater(roots[0], roots[1]);
456
457 // Line L chops around the first 2 roots, line M chops around the second 2.
458 chops->push_back_n(2, &roots[i]);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600459 }
460}
461
Chris Daltonb3a69592018-04-18 14:10:22 -0600462void GrCCGeometry::cubicTo(const SkPoint P[4], float inflectPad, float loopIntersectPad) {
463 SkASSERT(fBuildingContour);
464 SkASSERT(P[0] == fPoints.back());
Chris Dalton29011a22017-09-28 12:08:33 -0600465
Chris Daltonb3a69592018-04-18 14:10:22 -0600466 // Don't crunch on the curve or inflate geometry if it is nearly flat (or just very small).
467 // Flat curves can break the math below.
468 if (are_collinear(P)) {
469 this->lineTo(P[3]);
470 return;
471 }
Chris Dalton29011a22017-09-28 12:08:33 -0600472
Chris Daltonb3a69592018-04-18 14:10:22 -0600473 Sk2f p0 = Sk2f::Load(P);
474 Sk2f p1 = Sk2f::Load(P+1);
475 Sk2f p2 = Sk2f::Load(P+2);
476 Sk2f p3 = Sk2f::Load(P+3);
477
478 // Also detect near-quadratics ahead of time.
479 Sk2f tan0, tan1, c;
480 get_cubic_tangents(p0, p1, p2, p3, &tan0, &tan1);
481 if (is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, &c)) {
482 this->appendQuadratics(p0, c, p3);
483 return;
484 }
485
486 double tt[2], ss[2], D[4];
487 fCurrCubicType = SkClassifyCubic(P, tt, ss, D);
488 SkASSERT(!SkCubicIsDegenerate(fCurrCubicType));
489 Sk2f t = Sk2f(static_cast<float>(tt[0]), static_cast<float>(tt[1]));
490 Sk2f s = Sk2f(static_cast<float>(ss[0]), static_cast<float>(ss[1]));
491
Chris Dalton4229b352018-04-18 14:13:45 -0600492 ExcludedTerm skipTerm = (std::abs(D[2]) > std::abs(D[1]))
493 ? ExcludedTerm::kQuadraticTerm
494 : ExcludedTerm::kLinearTerm;
495 Sk2f C0 = SkNx_fma(Sk2f(3), p1 - p2, p3 - p0);
496 Sk2f C1 = (ExcludedTerm::kLinearTerm == skipTerm
497 ? SkNx_fma(Sk2f(-2), p1, p0 + p2)
498 : p1 - p0) * 3;
499 Sk2f C0x1 = C0 * SkNx_shuffle<1,0>(C1);
500 float Cdet = C0x1[0] - C0x1[1];
Chris Daltonb3a69592018-04-18 14:10:22 -0600501
502 SkSTArray<4, float> chops;
503 if (SkCubicType::kLoop != fCurrCubicType) {
Chris Dalton4229b352018-04-18 14:13:45 -0600504 find_chops_around_inflection_points(inflectPad, t, s, C0, C1, skipTerm, Cdet, &chops);
Chris Daltonb3a69592018-04-18 14:10:22 -0600505 } else {
Chris Dalton4229b352018-04-18 14:13:45 -0600506 find_chops_around_loop_intersection(loopIntersectPad, t, s, C0, C1, skipTerm, Cdet, &chops);
Chris Daltonb3a69592018-04-18 14:10:22 -0600507 }
Chris Dalton5450ab12018-04-18 16:49:13 -0600508 if (4 == chops.count() && chops[1] >= chops[2]) {
Chris Daltonb3a69592018-04-18 14:10:22 -0600509 // This just the means the KLM roots are so close that their paddings overlap. We will
510 // approximate the entire middle section, but still have it chopped midway. For loops this
511 // chop guarantees the append code only sees convex segments. Otherwise, it means we are (at
512 // least almost) a cusp and the chop makes sure we get a sharp point.
513 Sk2f ts = t * SkNx_shuffle<1,0>(s);
514 chops[1] = chops[2] = (ts[0] + ts[1]) / (2*s[0]*s[1]);
515 }
516
517#ifdef SK_DEBUG
518 for (int i = 1; i < chops.count(); ++i) {
519 SkASSERT(chops[i] >= chops[i - 1]);
520 }
521#endif
522 this->appendCubics(AppendCubicMode::kLiteral, p0, p1, p2, p3, chops.begin(), chops.count());
Chris Dalton29011a22017-09-28 12:08:33 -0600523}
524
Chris Daltonb3a69592018-04-18 14:10:22 -0600525static inline void chop_cubic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, const Sk2f& p3,
526 float T, Sk2f* ab, Sk2f* abc, Sk2f* abcd, Sk2f* bcd, Sk2f* cd) {
527 Sk2f TT = T;
528 *ab = lerp(p0, p1, TT);
529 Sk2f bc = lerp(p1, p2, TT);
530 *cd = lerp(p2, p3, TT);
531 *abc = lerp(*ab, bc, TT);
532 *bcd = lerp(bc, *cd, TT);
533 *abcd = lerp(*abc, *bcd, TT);
534}
Chris Dalton29011a22017-09-28 12:08:33 -0600535
Chris Daltonb3a69592018-04-18 14:10:22 -0600536void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f& p1,
537 const Sk2f& p2, const Sk2f& p3, const float chops[], int numChops,
538 float localT0, float localT1) {
539 if (numChops) {
540 SkASSERT(numChops > 0);
541 int midChopIdx = numChops/2;
542 float T = chops[midChopIdx];
543 // Chops alternate between literal and approximate mode.
544 AppendCubicMode rightMode = (AppendCubicMode)((bool)mode ^ (midChopIdx & 1) ^ 1);
Chris Dalton29011a22017-09-28 12:08:33 -0600545
Chris Daltonb3a69592018-04-18 14:10:22 -0600546 if (T <= localT0) {
547 // T is outside 0..1. Append the right side only.
548 this->appendCubics(rightMode, p0, p1, p2, p3, &chops[midChopIdx + 1],
549 numChops - midChopIdx - 1, localT0, localT1);
550 return;
551 }
552
553 if (T >= localT1) {
554 // T is outside 0..1. Append the left side only.
555 this->appendCubics(mode, p0, p1, p2, p3, chops, midChopIdx, localT0, localT1);
556 return;
557 }
558
559 float localT = (T - localT0) / (localT1 - localT0);
560 Sk2f p01, p02, pT, p11, p12;
561 chop_cubic(p0, p1, p2, p3, localT, &p01, &p02, &pT, &p11, &p12);
562 this->appendCubics(mode, p0, p01, p02, pT, chops, midChopIdx, localT0, T);
563 this->appendCubics(rightMode, pT, p11, p12, p3, &chops[midChopIdx + 1],
564 numChops - midChopIdx - 1, T, localT1);
565 return;
566 }
567
568 this->appendCubics(mode, p0, p1, p2, p3);
569}
570
571void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f& p1,
572 const Sk2f& p2, const Sk2f& p3, int maxSubdivisions) {
573 if ((p0 == p3).allTrue()) {
574 return;
575 }
576
577 if (SkCubicType::kLoop != fCurrCubicType) {
578 // Serpentines and cusps are always monotonic after chopping around inflection points.
579 SkASSERT(!SkCubicIsDegenerate(fCurrCubicType));
580
581 if (AppendCubicMode::kApproximate == mode) {
582 // This section passes through an inflection point, so we can get away with a flat line.
583 // This can cause some curves to feel slightly more flat when inspected rigorously back
584 // and forth against another renderer, but for now this seems acceptable given the
585 // simplicity.
586 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
587 this->appendLine(p3);
588 return;
589 }
590 } else {
591 Sk2f tan0, tan1;
592 get_cubic_tangents(p0, p1, p2, p3, &tan0, &tan1);
593
594 if (maxSubdivisions && !is_convex_curve_monotonic(p0, tan0, p3, tan1)) {
595 this->chopAndAppendCubicAtMidTangent(mode, p0, p1, p2, p3, tan0, tan1,
596 maxSubdivisions - 1);
597 return;
598 }
599
600 if (AppendCubicMode::kApproximate == mode) {
601 Sk2f c;
602 if (!is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, &c) && maxSubdivisions) {
603 this->chopAndAppendCubicAtMidTangent(mode, p0, p1, p2, p3, tan0, tan1,
604 maxSubdivisions - 1);
605 return;
606 }
607
608 this->appendMonotonicQuadratic(p0, c, p3);
609 return;
610 }
611 }
612
613 // Don't send curves to the GPU if we know they are nearly flat (or just very small).
614 // Since the cubic segment is known to be convex at this point, our flatness check is simple.
615 if (are_collinear(p0, (p1 + p2) * .5f, p3)) {
616 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
617 this->appendLine(p3);
618 return;
619 }
620
621 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
622 p1.store(&fPoints.push_back());
623 p2.store(&fPoints.push_back());
624 p3.store(&fPoints.push_back());
625 fVerbs.push_back(Verb::kMonotonicCubicTo);
626 ++fCurrContourTallies.fCubics;
Chris Dalton29011a22017-09-28 12:08:33 -0600627}
628
Chris Dalton9f2dab02018-04-18 14:07:03 -0600629// Given a convex curve segment with the following order-2 tangent function:
630//
631// |C2x C2y|
632// tan = some_scale * |dx/dt dy/dt| = |t^2 t 1| * |C1x C1y|
633// |C0x C0y|
634//
635// This function finds the T value whose tangent angle is halfway between the tangents at T=0 and
636// T=1 (tan0 and tan1).
637static inline float find_midtangent(const Sk2f& tan0, const Sk2f& tan1,
638 float scale2, const Sk2f& C2,
639 float scale1, const Sk2f& C1,
640 float scale0, const Sk2f& C0) {
641 // Tangents point in the direction of increasing T, so tan0 and -tan1 both point toward the
642 // midtangent. 'n' will therefore bisect tan0 and -tan1, giving us the normal to the midtangent.
643 //
644 // n dot midtangent = 0
645 //
646 Sk2f n = normalize(tan0) - normalize(tan1);
647
648 // Find the T value at the midtangent. This is a simple quadratic equation:
649 //
650 // midtangent dot n = 0
651 //
652 // (|t^2 t 1| * C) dot n = 0
653 //
654 // |t^2 t 1| dot C*n = 0
655 //
656 // First find coeffs = C*n.
657 Sk4f C[2];
658 Sk2f::Store4(C, C2, C1, C0, 0);
659 Sk4f coeffs = C[0]*n[0] + C[1]*n[1];
660 if (1 != scale2 || 1 != scale1 || 1 != scale0) {
661 coeffs *= Sk4f(scale2, scale1, scale0, 0);
662 }
663
664 // Now solve the quadratic.
665 float a = coeffs[0], b = coeffs[1], c = coeffs[2];
666 float discr = b*b - 4*a*c;
667 if (discr < 0) {
668 return 0; // This will only happen if the curve is a line.
669 }
670
671 // The roots are q/a and c/q. Pick the one closer to T=.5.
672 float q = -.5f * (b + copysignf(std::sqrt(discr), b));
673 float r = .5f*q*a;
674 return std::abs(q*q - r) < std::abs(a*c - r) ? q/a : c/q;
675}
676
Chris Daltonb3a69592018-04-18 14:10:22 -0600677inline void GrCCGeometry::chopAndAppendCubicAtMidTangent(AppendCubicMode mode, const Sk2f& p0,
678 const Sk2f& p1, const Sk2f& p2,
679 const Sk2f& p3, const Sk2f& tan0,
680 const Sk2f& tan1,
681 int maxFutureSubdivisions) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600682 float midT = find_midtangent(tan0, tan1, 3, p3 + (p1 - p2)*3 - p0,
683 6, p0 - p1*2 + p2,
684 3, p1 - p0);
685 // Use positive logic since NaN fails comparisons. (However midT should not be NaN since we cull
686 // near-flat cubics in cubicTo().)
687 if (!(midT > 0 && midT < 1)) {
688 // The cubic is flat. Otherwise there would be a real midtangent inside T=0..1.
689 this->appendLine(p3);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600690 return;
691 }
692
Chris Daltonb3a69592018-04-18 14:10:22 -0600693 Sk2f p01, p02, pT, p11, p12;
694 chop_cubic(p0, p1, p2, p3, midT, &p01, &p02, &pT, &p11, &p12);
695 this->appendCubics(mode, p0, p01, p02, pT, maxFutureSubdivisions);
696 this->appendCubics(mode, pT, p11, p12, p3, maxFutureSubdivisions);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600697}
698
Chris Dalton9f2dab02018-04-18 14:07:03 -0600699void GrCCGeometry::conicTo(const SkPoint P[3], float w) {
700 SkASSERT(fBuildingContour);
701 SkASSERT(P[0] == fPoints.back());
702 Sk2f p0 = Sk2f::Load(P);
703 Sk2f p1 = Sk2f::Load(P+1);
704 Sk2f p2 = Sk2f::Load(P+2);
705
Chris Dalton9f2dab02018-04-18 14:07:03 -0600706 Sk2f tan0 = p1 - p0;
707 Sk2f tan1 = p2 - p1;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600708
709 if (!is_convex_curve_monotonic(p0, tan0, p2, tan1)) {
Chris Daltond8bae7d2018-04-19 13:13:25 -0600710 // The derivative of a conic has a cumbersome order-4 denominator. However, this isn't
711 // necessary if we are only interested in a vector in the same *direction* as a given
712 // tangent line. Since the denominator scales dx and dy uniformly, we can throw it out
713 // completely after evaluating the derivative with the standard quotient rule. This leaves
714 // us with a simpler quadratic function that we use to find the midtangent.
715 float midT = find_midtangent(tan0, tan1, 1, (w - 1) * (p2 - p0),
716 1, (p2 - p0) - 2*w*(p1 - p0),
717 1, w*(p1 - p0));
718 // Use positive logic since NaN fails comparisons. (However midT should not be NaN since we
719 // cull near-linear conics above. And while w=0 is flat, it's not a line and has valid
720 // midtangents.)
721 if (!(midT > 0 && midT < 1)) {
722 // The conic is flat. Otherwise there would be a real midtangent inside T=0..1.
723 this->appendLine(p2);
724 return;
725 }
726
Chris Dalton9f2dab02018-04-18 14:07:03 -0600727 // Chop the conic at midtangent to produce two monotonic segments.
Chris Daltond8bae7d2018-04-19 13:13:25 -0600728 Sk4f p3d0 = Sk4f(p0[0], p0[1], 1, 0);
729 Sk4f p3d1 = Sk4f(p1[0], p1[1], 1, 0) * w;
730 Sk4f p3d2 = Sk4f(p2[0], p2[1], 1, 0);
731 Sk4f midT4 = midT;
732
733 Sk4f p3d01 = lerp(p3d0, p3d1, midT4);
734 Sk4f p3d12 = lerp(p3d1, p3d2, midT4);
735 Sk4f p3d012 = lerp(p3d01, p3d12, midT4);
736
737 Sk2f midpoint = Sk2f(p3d012[0], p3d012[1]) / p3d012[2];
Chris Dalton9f2dab02018-04-18 14:07:03 -0600738 Sk2f ww = Sk2f(p3d01[2], p3d12[2]) * Sk2f(p3d012[2]).rsqrt();
Chris Daltond8bae7d2018-04-19 13:13:25 -0600739
Chris Dalton9f2dab02018-04-18 14:07:03 -0600740 this->appendMonotonicConic(p0, Sk2f(p3d01[0], p3d01[1]) / p3d01[2], midpoint, ww[0]);
741 this->appendMonotonicConic(midpoint, Sk2f(p3d12[0], p3d12[1]) / p3d12[2], p2, ww[1]);
742 return;
743 }
744
745 this->appendMonotonicConic(p0, p1, p2, w);
746}
747
748void GrCCGeometry::appendMonotonicConic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, float w) {
Chris Daltond8bae7d2018-04-19 13:13:25 -0600749 SkASSERT(w >= 0);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600750 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
751
Chris Daltond8bae7d2018-04-19 13:13:25 -0600752 Sk2f base = p2 - p0;
753 Sk2f baseAbs = base.abs();
754 float baseWidth = baseAbs[0] + baseAbs[1];
755
756 // Find the height of the curve. Max height always occurs at T=.5 for conics.
757 Sk2f d = (p1 - p0) * SkNx_shuffle<1,0>(base);
758 float h1 = std::abs(d[1] - d[0]); // Height of p1 above the base.
759 float ht = h1*w, hs = 1 + w; // Height of the conic = ht/hs.
760
761 if (ht < (baseWidth*hs) * kFlatnessThreshold) { // i.e. ht/hs < baseWidth * kFlatnessThreshold
762 // We are flat. (See rationale in are_collinear.)
763 this->appendLine(p2);
764 return;
765 }
766
767 if (w > 1 && h1*hs - ht < baseWidth*hs) { // i.e. w > 1 && h1 - ht/hs < baseWidth
768 // If we get within 1px of p1 when w > 1, we will pick up artifacts from the implicit
769 // function's reflection. Chop at max height (T=.5) and draw a triangle instead.
770 Sk2f p1w = p1*w;
771 Sk2f ab = p0 + p1w;
772 Sk2f bc = p1w + p2;
773 Sk2f highpoint = (ab + bc) / (2*(1 + w));
774 this->appendLine(highpoint);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600775 this->appendLine(p2);
776 return;
777 }
778
779 p1.store(&fPoints.push_back());
780 p2.store(&fPoints.push_back());
781 fConicWeights.push_back(w);
782 fVerbs.push_back(Verb::kMonotonicConicTo);
783 ++fCurrContourTallies.fConics;
784}
785
Chris Dalton383a2ef2018-01-08 17:21:41 -0500786GrCCGeometry::PrimitiveTallies GrCCGeometry::endContour() {
Chris Daltonc1e59632017-09-05 00:30:07 -0600787 SkASSERT(fBuildingContour);
788 SkASSERT(fVerbs.count() >= fCurrContourTallies.fTriangles);
789
790 // The fTriangles field currently contains this contour's starting verb index. We can now
791 // use it to calculate the size of the contour's fan.
792 int fanSize = fVerbs.count() - fCurrContourTallies.fTriangles;
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600793 if (fPoints.back() == fCurrAnchorPoint) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600794 --fanSize;
795 fVerbs.push_back(Verb::kEndClosedContour);
796 } else {
797 fVerbs.push_back(Verb::kEndOpenContour);
798 }
799
800 fCurrContourTallies.fTriangles = SkTMax(fanSize - 2, 0);
801
Chris Dalton383a2ef2018-01-08 17:21:41 -0500802 SkDEBUGCODE(fBuildingContour = false);
Chris Daltonc1e59632017-09-05 00:30:07 -0600803 return fCurrContourTallies;
Chris Dalton419a94d2017-08-28 10:24:22 -0600804}