blob: 17a54af56056a472791f0f218ea4a9608fe41c26 [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 Dalton6f5e77a2018-04-23 21:14:42 -060041void GrCCGeometry::lineTo(const SkPoint P[2]) {
Chris Daltonc1e59632017-09-05 00:30:07 -060042 SkASSERT(fBuildingContour);
Chris Dalton6f5e77a2018-04-23 21:14:42 -060043 SkASSERT(P[0] == fPoints.back());
44 Sk2f p0 = Sk2f::Load(P);
45 Sk2f p1 = Sk2f::Load(P+1);
46 this->appendLine(p0, p1);
Chris Dalton7ca3b7b2018-04-10 00:21:19 -060047}
48
Chris Dalton6f5e77a2018-04-23 21:14:42 -060049inline void GrCCGeometry::appendLine(const Sk2f& p0, const Sk2f& p1) {
50 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
51 if ((p0 == p1).allTrue()) {
52 return;
53 }
54 p1.store(&fPoints.push_back());
Chris Daltonc1e59632017-09-05 00:30:07 -060055 fVerbs.push_back(Verb::kLineTo);
56}
57
Chris Dalton419a94d2017-08-28 10:24:22 -060058static inline Sk2f normalize(const Sk2f& n) {
59 Sk2f nn = n*n;
60 return n * (nn + SkNx_shuffle<1,0>(nn)).rsqrt();
61}
62
63static inline float dot(const Sk2f& a, const Sk2f& b) {
64 float product[2];
65 (a * b).store(product);
66 return product[0] + product[1];
67}
68
Chris Daltonb0601a42018-04-10 00:23:45 -060069static inline bool are_collinear(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
Chris Daltond8bae7d2018-04-19 13:13:25 -060070 float tolerance = kFlatnessThreshold) {
Chris Daltonb0601a42018-04-10 00:23:45 -060071 Sk2f l = p2 - p0; // Line from p0 -> p2.
Chris Dalton900cd052017-09-07 10:36:51 -060072
Chris Daltonb0601a42018-04-10 00:23:45 -060073 // lwidth = Manhattan width of l.
74 Sk2f labs = l.abs();
75 float lwidth = labs[0] + labs[1];
Chris Dalton900cd052017-09-07 10:36:51 -060076
Chris Daltonb0601a42018-04-10 00:23:45 -060077 // d = |p1 - p0| dot | l.y|
78 // |-l.x| = distance from p1 to l.
79 Sk2f dd = (p1 - p0) * SkNx_shuffle<1,0>(l);
80 float d = dd[0] - dd[1];
Chris Dalton900cd052017-09-07 10:36:51 -060081
Chris Daltonb0601a42018-04-10 00:23:45 -060082 // We are collinear if a box with radius "tolerance", centered on p1, touches the line l.
83 // To decide this, we check if the distance from p1 to the line is less than the distance from
84 // p1 to the far corner of this imaginary box, along that same normal vector.
85 // The far corner of the box can be found at "p1 + sign(n) * tolerance", where n is normal to l:
86 //
87 // abs(dot(p1 - p0, n)) <= dot(sign(n) * tolerance, n)
88 //
89 // Which reduces to:
90 //
91 // abs(d) <= (n.x * sign(n.x) + n.y * sign(n.y)) * tolerance
92 // abs(d) <= (abs(n.x) + abs(n.y)) * tolerance
93 //
94 // Use "<=" in case l == 0.
95 return std::abs(d) <= lwidth * tolerance;
96}
97
Chris Daltond8bae7d2018-04-19 13:13:25 -060098static inline bool are_collinear(const SkPoint P[4], float tolerance = kFlatnessThreshold) {
Chris Daltonb0601a42018-04-10 00:23:45 -060099 Sk4f Px, Py; // |Px Py| |p0 - p3|
100 Sk4f::Load2(P, &Px, &Py); // |. . | = |p1 - p3|
101 Px -= Px[3]; // |. . | |p2 - p3|
102 Py -= Py[3]; // |. . | | 0 |
103
104 // Find [lx, ly] = the line from p3 to the furthest-away point from p3.
105 Sk4f Pwidth = Px.abs() + Py.abs(); // Pwidth = Manhattan width of each point.
106 int lidx = Pwidth[0] > Pwidth[1] ? 0 : 1;
107 lidx = Pwidth[lidx] > Pwidth[2] ? lidx : 2;
108 float lx = Px[lidx], ly = Py[lidx];
109 float lwidth = Pwidth[lidx]; // lwidth = Manhattan width of [lx, ly].
110
111 // |Px Py|
112 // d = |. . | * | ly| = distances from each point to l (two of the distances will be zero).
113 // |. . | |-lx|
114 // |. . |
115 Sk4f d = Px*ly - Py*lx;
116
117 // We are collinear if boxes with radius "tolerance", centered on all 4 points all touch line l.
118 // (See the rationale for this formula in the above, 3-point version of this function.)
119 // Use "<=" in case l == 0.
120 return (d.abs() <= lwidth * tolerance).allTrue();
Chris Dalton900cd052017-09-07 10:36:51 -0600121}
122
Chris Dalton419a94d2017-08-28 10:24:22 -0600123// Returns whether the (convex) curve segment is monotonic with respect to [endPt - startPt].
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600124static inline bool is_convex_curve_monotonic(const Sk2f& startPt, const Sk2f& tan0,
125 const Sk2f& endPt, const Sk2f& tan1) {
Chris Dalton419a94d2017-08-28 10:24:22 -0600126 Sk2f v = endPt - startPt;
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600127 float dot0 = dot(tan0, v);
128 float dot1 = dot(tan1, v);
Chris Dalton419a94d2017-08-28 10:24:22 -0600129
130 // A small, negative tolerance handles floating-point error in the case when one tangent
131 // approaches 0 length, meaning the (convex) curve segment is effectively a flat line.
132 float tolerance = -std::max(std::abs(dot0), std::abs(dot1)) * SK_ScalarNearlyZero;
133 return dot0 >= tolerance && dot1 >= tolerance;
134}
135
Chris Dalton9f2dab02018-04-18 14:07:03 -0600136template<int N> static inline SkNx<N,float> lerp(const SkNx<N,float>& a, const SkNx<N,float>& b,
137 const SkNx<N,float>& t) {
Chris Dalton419a94d2017-08-28 10:24:22 -0600138 return SkNx_fma(t, b - a, a);
139}
140
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600141void GrCCGeometry::quadraticTo(const SkPoint P[3]) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600142 SkASSERT(fBuildingContour);
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600143 SkASSERT(P[0] == fPoints.back());
144 Sk2f p0 = Sk2f::Load(P);
145 Sk2f p1 = Sk2f::Load(P+1);
146 Sk2f p2 = Sk2f::Load(P+2);
Chris Daltonc1e59632017-09-05 00:30:07 -0600147
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600148 // Don't crunch on the curve if it is nearly flat (or just very small). Flat curves can break
149 // The monotonic chopping math.
150 if (are_collinear(p0, p1, p2)) {
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600151 this->appendLine(p0, p2);
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600152 return;
153 }
Chris Dalton419a94d2017-08-28 10:24:22 -0600154
Chris Daltonb3a69592018-04-18 14:10:22 -0600155 this->appendQuadratics(p0, p1, p2);
Chris Dalton29011a22017-09-28 12:08:33 -0600156}
157
Chris Daltonb3a69592018-04-18 14:10:22 -0600158inline void GrCCGeometry::appendQuadratics(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2) {
Chris Dalton419a94d2017-08-28 10:24:22 -0600159 Sk2f tan0 = p1 - p0;
160 Sk2f tan1 = p2 - p1;
Chris Dalton29011a22017-09-28 12:08:33 -0600161
Chris Dalton419a94d2017-08-28 10:24:22 -0600162 // This should almost always be this case for well-behaved curves in the real world.
Chris Dalton43646532017-12-07 12:47:02 -0700163 if (is_convex_curve_monotonic(p0, tan0, p2, tan1)) {
Chris Daltonb3a69592018-04-18 14:10:22 -0600164 this->appendMonotonicQuadratic(p0, p1, p2);
Chris Daltonc1e59632017-09-05 00:30:07 -0600165 return;
Chris Dalton419a94d2017-08-28 10:24:22 -0600166 }
167
168 // 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 -0600169 // tangent angle is halfway between tan0 and tan1.
Chris Dalton419a94d2017-08-28 10:24:22 -0600170 Sk2f n = normalize(tan0) - normalize(tan1);
171
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600172 // The midtangent can be found where (dQ(t) dot n) = 0:
Chris Dalton419a94d2017-08-28 10:24:22 -0600173 //
174 // 0 = (dQ(t) dot n) = | 2*t 1 | * | p0 - 2*p1 + p2 | * | n |
175 // | -2*p0 + 2*p1 | | . |
176 //
177 // = | 2*t 1 | * | tan1 - tan0 | * | n |
178 // | 2*tan0 | | . |
179 //
180 // = 2*t * ((tan1 - tan0) dot n) + (2*tan0 dot n)
181 //
182 // t = (tan0 dot n) / ((tan0 - tan1) dot n)
183 Sk2f dQ1n = (tan0 - tan1) * n;
184 Sk2f dQ0n = tan0 * n;
185 Sk2f t = (dQ0n + SkNx_shuffle<1,0>(dQ0n)) / (dQ1n + SkNx_shuffle<1,0>(dQ1n));
186 t = Sk2f::Min(Sk2f::Max(t, 0), 1); // Clamp for FP error.
187
188 Sk2f p01 = SkNx_fma(t, tan0, p0);
189 Sk2f p12 = SkNx_fma(t, tan1, p1);
190 Sk2f p012 = lerp(p01, p12, t);
191
Chris Daltonb3a69592018-04-18 14:10:22 -0600192 this->appendMonotonicQuadratic(p0, p01, p012);
193 this->appendMonotonicQuadratic(p012, p12, p2);
Chris Dalton43646532017-12-07 12:47:02 -0700194}
195
Chris Daltonb3a69592018-04-18 14:10:22 -0600196inline void GrCCGeometry::appendMonotonicQuadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2) {
Chris Dalton43646532017-12-07 12:47:02 -0700197 // Don't send curves to the GPU if we know they are nearly flat (or just very small).
198 if (are_collinear(p0, p1, p2)) {
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600199 this->appendLine(p0, p2);
Chris Dalton43646532017-12-07 12:47:02 -0700200 return;
201 }
202
Chris Daltonb3a69592018-04-18 14:10:22 -0600203 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600204 SkASSERT((p0 != p2).anyTrue());
Chris Dalton43646532017-12-07 12:47:02 -0700205 p1.store(&fPoints.push_back());
Chris Daltonc1e59632017-09-05 00:30:07 -0600206 p2.store(&fPoints.push_back());
Chris Dalton43646532017-12-07 12:47:02 -0700207 fVerbs.push_back(Verb::kMonotonicQuadraticTo);
208 ++fCurrContourTallies.fQuadratics;
Chris Daltonc1e59632017-09-05 00:30:07 -0600209}
210
Chris Daltonb3a69592018-04-18 14:10:22 -0600211static inline Sk2f first_unless_nearly_zero(const Sk2f& a, const Sk2f& b) {
212 Sk2f aa = a*a;
213 aa += SkNx_shuffle<1,0>(aa);
214 SkASSERT(aa[0] == aa[1]);
215
216 Sk2f bb = b*b;
217 bb += SkNx_shuffle<1,0>(bb);
218 SkASSERT(bb[0] == bb[1]);
219
220 return (aa > bb * SK_ScalarNearlyZero).thenElse(a, b);
221}
222
223static inline void get_cubic_tangents(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
224 const Sk2f& p3, Sk2f* tan0, Sk2f* tan1) {
225 *tan0 = first_unless_nearly_zero(p1 - p0, p2 - p0);
226 *tan1 = first_unless_nearly_zero(p3 - p2, p3 - p1);
227}
228
229static inline bool is_cubic_nearly_quadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
230 const Sk2f& p3, const Sk2f& tan0, const Sk2f& tan1,
231 Sk2f* c) {
232 Sk2f c1 = SkNx_fma(Sk2f(1.5f), tan0, p0);
233 Sk2f c2 = SkNx_fma(Sk2f(-1.5f), tan1, p3);
234 *c = (c1 + c2) * .5f; // Hopefully optimized out if not used?
235 return ((c1 - c2).abs() <= 1).allTrue();
236}
237
Chris Dalton4229b352018-04-18 14:13:45 -0600238enum class ExcludedTerm : bool {
239 kQuadraticTerm,
240 kLinearTerm
241};
Chris Daltonc1e59632017-09-05 00:30:07 -0600242
Chris Daltonb3a69592018-04-18 14:10:22 -0600243// Finds where to chop a non-loop around its inflection points. The resulting cubic segments will be
244// chopped such that a box of radius 'padRadius', centered at any point along the curve segment, is
245// guaranteed to not cross the tangent lines at the inflection points (a.k.a lines L & M).
Chris Dalton7f578bf2017-09-05 16:46:48 -0600246//
Chris Dalton5450ab12018-04-18 16:49:13 -0600247// 'chops' will be filled with 0, 2, or 4 T values. The segments between T0..T1 and T2..T3 must be
248// drawn with flat lines instead of cubics.
Chris Dalton7f578bf2017-09-05 16:46:48 -0600249//
250// A serpentine cubic has two inflection points, so this method takes Sk2f and computes the padding
251// for both in SIMD.
Chris Dalton5450ab12018-04-18 16:49:13 -0600252static inline void find_chops_around_inflection_points(float padRadius, Sk2f tl, Sk2f sl,
Chris Dalton4229b352018-04-18 14:13:45 -0600253 const Sk2f& C0, const Sk2f& C1,
254 ExcludedTerm skipTerm, float Cdet,
Chris Daltonb3a69592018-04-18 14:10:22 -0600255 SkSTArray<4, float>* chops) {
256 SkASSERT(chops->empty());
Chris Dalton7f578bf2017-09-05 16:46:48 -0600257 SkASSERT(padRadius >= 0);
Chris Daltonc1e59632017-09-05 00:30:07 -0600258
Chris Dalton4229b352018-04-18 14:13:45 -0600259 padRadius /= std::abs(Cdet); // Scale this single value rather than all of C^-1 later on.
260
Chris Dalton5450ab12018-04-18 16:49:13 -0600261 // The homogeneous parametric functions for distance from lines L & M are:
262 //
263 // l(t,s) = (t*sl - s*tl)^3
264 // m(t,s) = (t*sm - s*tm)^3
265 //
266 // See "Resolution Independent Curve Rendering using Programmable Graphics Hardware",
267 // 4.3 Finding klmn:
268 //
269 // https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf
270 //
271 // From here on we use Sk2f with "L" names, but the second lane will be for line M.
272 tl = (sl > 0).thenElse(tl, -tl); // Tl=tl/sl is the triple root of l(t,s). Normalize so s >= 0.
273 sl = sl.abs();
Chris Dalton7f578bf2017-09-05 16:46:48 -0600274
Chris Dalton5450ab12018-04-18 16:49:13 -0600275 // Convert l(t,s), m(t,s) to power-basis form:
276 //
277 // | l3 m3 |
278 // |l(t,s) m(t,s)| = |t^3 t^2*s t*s^2 s^3| * | l2 m2 |
279 // | l1 m1 |
280 // | l0 m0 |
281 //
282 Sk2f l3 = sl*sl*sl;
283 Sk2f l2or1 = (ExcludedTerm::kLinearTerm == skipTerm) ? sl*sl*tl*-3 : sl*tl*tl*3;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600284
Chris Dalton5450ab12018-04-18 16:49:13 -0600285 // The equation for line L can be found as follows:
286 //
287 // L = C^-1 * (l excluding skipTerm)
288 //
289 // (See comments for GrPathUtils::calcCubicInverseTransposePowerBasisMatrix.)
Chris Dalton4229b352018-04-18 14:13:45 -0600290 // We are only interested in the normal to L, so only need the upper 2x2 of C^-1. And rather
291 // than divide by determinant(C) here, we have already performed this divide on padRadius.
292 Sk2f Lx = C1[1]*l3 - C0[1]*l2or1;
293 Sk2f Ly = -C1[0]*l3 + C0[0]*l2or1;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600294
Chris Dalton5450ab12018-04-18 16:49:13 -0600295 // A box of radius "padRadius" is touching line L if "center dot L" is less than the Manhattan
296 // with of L. (See rationale in are_collinear.)
297 Sk2f Lwidth = Lx.abs() + Ly.abs();
298 Sk2f pad = Lwidth * padRadius;
299
300 // Will T=(t + cbrt(pad))/s be greater than 0? No need to solve roots outside T=0..1.
301 Sk2f insideLeftPad = pad + tl*tl*tl;
302
303 // Will T=(t - cbrt(pad))/s be less than 1? No need to solve roots outside T=0..1.
304 Sk2f tms = tl - sl;
305 Sk2f insideRightPad = pad - tms*tms*tms;
306
307 // Solve for the T values where abs(l(T)) = pad.
308 if (insideLeftPad[0] > 0 && insideRightPad[0] > 0) {
309 float padT = cbrtf(pad[0]);
310 Sk2f pts = (tl[0] + Sk2f(-padT, +padT)) / sl[0];
311 pts.store(chops->push_back_n(2));
312 }
313
314 // Solve for the T values where abs(m(T)) = pad.
315 if (insideLeftPad[1] > 0 && insideRightPad[1] > 0) {
316 float padT = cbrtf(pad[1]);
317 Sk2f pts = (tl[1] + Sk2f(-padT, +padT)) / sl[1];
318 pts.store(chops->push_back_n(2));
319 }
Chris Dalton7f578bf2017-09-05 16:46:48 -0600320}
321
322static inline void swap_if_greater(float& a, float& b) {
323 if (a > b) {
324 std::swap(a, b);
325 }
326}
327
Chris Daltonb3a69592018-04-18 14:10:22 -0600328// Finds where to chop a non-loop around its intersection point. The resulting cubic segments will
329// be chopped such that a box of radius 'padRadius', centered at any point along the curve segment,
330// 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 -0600331//
Chris Daltonb3a69592018-04-18 14:10:22 -0600332// 'chops' will be filled with 0, 2, or 4 T values. The segments between T0..T1 and T2..T3 must be
333// drawn with quadratic splines instead of cubics.
Chris Dalton7f578bf2017-09-05 16:46:48 -0600334//
Chris Daltonb3a69592018-04-18 14:10:22 -0600335// A loop intersection falls at two different T values, so this method takes Sk2f and computes the
336// padding for both in SIMD.
Chris Dalton5450ab12018-04-18 16:49:13 -0600337static inline void find_chops_around_loop_intersection(float padRadius, Sk2f t2, Sk2f s2,
Chris Dalton4229b352018-04-18 14:13:45 -0600338 const Sk2f& C0, const Sk2f& C1,
339 ExcludedTerm skipTerm, float Cdet,
Chris Daltonb3a69592018-04-18 14:10:22 -0600340 SkSTArray<4, float>* chops) {
341 SkASSERT(chops->empty());
Chris Dalton7f578bf2017-09-05 16:46:48 -0600342 SkASSERT(padRadius >= 0);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600343
Chris Dalton4229b352018-04-18 14:13:45 -0600344 padRadius /= std::abs(Cdet); // Scale this single value rather than all of C^-1 later on.
345
Chris Dalton5450ab12018-04-18 16:49:13 -0600346 // The parametric functions for distance from lines L & M are:
347 //
348 // l(T) = (T - Td)^2 * (T - Te)
349 // m(T) = (T - Td) * (T - Te)^2
350 //
351 // See "Resolution Independent Curve Rendering using Programmable Graphics Hardware",
352 // 4.3 Finding klmn:
353 //
354 // https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf
355 Sk2f T2 = t2/s2; // T2 is the double root of l(T).
356 Sk2f T1 = SkNx_shuffle<1,0>(T2); // T1 is the other root of l(T).
Chris Dalton7f578bf2017-09-05 16:46:48 -0600357
Chris Dalton5450ab12018-04-18 16:49:13 -0600358 // Convert l(T), m(T) to power-basis form:
359 //
360 // | 1 1 |
361 // |l(T) m(T)| = |T^3 T^2 T 1| * | l2 m2 |
362 // | l1 m1 |
363 // | l0 m0 |
364 //
365 // From here on we use Sk2f with "L" names, but the second lane will be for line M.
366 Sk2f l2 = SkNx_fma(Sk2f(-2), T2, -T1);
367 Sk2f l1 = T2 * SkNx_fma(Sk2f(2), T1, T2);
368 Sk2f l0 = -T2*T2*T1;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600369
Chris Dalton5450ab12018-04-18 16:49:13 -0600370 // The equation for line L can be found as follows:
371 //
372 // L = C^-1 * (l excluding skipTerm)
373 //
374 // (See comments for GrPathUtils::calcCubicInverseTransposePowerBasisMatrix.)
Chris Dalton4229b352018-04-18 14:13:45 -0600375 // We are only interested in the normal to L, so only need the upper 2x2 of C^-1. And rather
376 // than divide by determinant(C) here, we have already performed this divide on padRadius.
Chris Dalton5450ab12018-04-18 16:49:13 -0600377 Sk2f l2or1 = (ExcludedTerm::kLinearTerm == skipTerm) ? l2 : l1;
Chris Dalton4229b352018-04-18 14:13:45 -0600378 Sk2f Lx = -C0[1]*l2or1 + C1[1]; // l3 is always 1.
379 Sk2f Ly = C0[0]*l2or1 - C1[0];
Chris Dalton7f578bf2017-09-05 16:46:48 -0600380
Chris Dalton5450ab12018-04-18 16:49:13 -0600381 // A box of radius "padRadius" is touching line L if "center dot L" is less than the Manhattan
382 // with of L. (See rationale in are_collinear.)
383 Sk2f Lwidth = Lx.abs() + Ly.abs();
384 Sk2f pad = Lwidth * padRadius;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600385
Chris Dalton5450ab12018-04-18 16:49:13 -0600386 // Is l(T=0) outside the padding around line L?
387 Sk2f lT0 = l0; // l(T=0) = |0 0 0 1| dot |1 l2 l1 l0| = l0
388 Sk2f outsideT0 = lT0.abs() - pad;
389
390 // Is l(T=1) outside the padding around line L?
391 Sk2f lT1 = (Sk2f(1) + l2 + l1 + l0).abs(); // l(T=1) = |1 1 1 1| dot |1 l2 l1 l0|
392 Sk2f outsideT1 = lT1.abs() - pad;
393
394 // Values for solving the cubic.
395 Sk2f p, q, qqq, discr, numRoots, D;
396 bool hasDiscr = false;
397
398 // Values for calculating one root (rarely needed).
399 Sk2f R, QQ;
400 bool hasOneRootVals = false;
Chris Daltonc1e59632017-09-05 00:30:07 -0600401
Chris Dalton7f578bf2017-09-05 16:46:48 -0600402 // Values for calculating three roots.
Chris Dalton5450ab12018-04-18 16:49:13 -0600403 Sk2f P, cosTheta3;
404 bool hasThreeRootVals = false;
Chris Daltonc1e59632017-09-05 00:30:07 -0600405
Chris Dalton5450ab12018-04-18 16:49:13 -0600406 // Solve for the T values where l(T) = +pad and m(T) = -pad.
Chris Dalton7f578bf2017-09-05 16:46:48 -0600407 for (int i = 0; i < 2; ++i) {
Chris Dalton5450ab12018-04-18 16:49:13 -0600408 float T = T2[i]; // T is the point we are chopping around.
409 if ((T < 0 && outsideT0[i] >= 0) || (T > 1 && outsideT1[i] >= 0)) {
410 // The padding around T is completely out of range. No point solving for it.
411 continue;
412 }
413
414 if (!hasDiscr) {
415 p = Sk2f(+.5f, -.5f) * pad;
416 q = (1.f/3) * (T2 - T1);
417 qqq = q*q*q;
418 discr = qqq*p*2 + p*p;
419 numRoots = (discr < 0).thenElse(3, 1);
420 D = T2 - q;
421 hasDiscr = true;
422 }
423
Chris Dalton7f578bf2017-09-05 16:46:48 -0600424 if (1 == numRoots[i]) {
Chris Dalton5450ab12018-04-18 16:49:13 -0600425 if (!hasOneRootVals) {
426 Sk2f r = qqq + p;
427 Sk2f s = r.abs() + discr.sqrt();
428 R = (r > 0).thenElse(-s, s);
429 QQ = q*q;
430 hasOneRootVals = true;
431 }
432
433 float A = cbrtf(R[i]);
434 float B = A != 0 ? QQ[i]/A : 0;
435 // 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 -0600436 if (1 == i) {
437 chops->push_back(0);
438 }
Chris Daltonb3a69592018-04-18 14:10:22 -0600439 chops->push_back(A + B + D[i]);
440 if (0 == i) {
441 chops->push_back(1);
442 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600443 continue;
444 }
445
Chris Dalton5450ab12018-04-18 16:49:13 -0600446 if (!hasThreeRootVals) {
447 P = q.abs() * -2;
448 cosTheta3 = (q >= 0).thenElse(1, -1) + p / qqq.abs();
449 hasThreeRootVals = true;
450 }
451
Chris Dalton7f578bf2017-09-05 16:46:48 -0600452 static constexpr float k2PiOver3 = 2 * SK_ScalarPI / 3;
453 float theta = std::acos(cosTheta3[i]) * (1.f/3);
Chris Daltonb3a69592018-04-18 14:10:22 -0600454 float roots[3] = {P[i] * std::cos(theta) + D[i],
455 P[i] * std::cos(theta + k2PiOver3) + D[i],
456 P[i] * std::cos(theta - k2PiOver3) + D[i]};
Chris Daltonc1e59632017-09-05 00:30:07 -0600457
Chris Dalton7f578bf2017-09-05 16:46:48 -0600458 // Sort the three roots.
Chris Daltonb3a69592018-04-18 14:10:22 -0600459 swap_if_greater(roots[0], roots[1]);
460 swap_if_greater(roots[1], roots[2]);
461 swap_if_greater(roots[0], roots[1]);
462
463 // Line L chops around the first 2 roots, line M chops around the second 2.
464 chops->push_back_n(2, &roots[i]);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600465 }
466}
467
Chris Daltonb3a69592018-04-18 14:10:22 -0600468void GrCCGeometry::cubicTo(const SkPoint P[4], float inflectPad, float loopIntersectPad) {
469 SkASSERT(fBuildingContour);
470 SkASSERT(P[0] == fPoints.back());
Chris Dalton29011a22017-09-28 12:08:33 -0600471
Chris Daltonb3a69592018-04-18 14:10:22 -0600472 // Don't crunch on the curve or inflate geometry if it is nearly flat (or just very small).
473 // Flat curves can break the math below.
474 if (are_collinear(P)) {
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600475 Sk2f p0 = Sk2f::Load(P);
476 Sk2f p3 = Sk2f::Load(P+3);
477 this->appendLine(p0, p3);
Chris Daltonb3a69592018-04-18 14:10:22 -0600478 return;
479 }
Chris Dalton29011a22017-09-28 12:08:33 -0600480
Chris Daltonb3a69592018-04-18 14:10:22 -0600481 Sk2f p0 = Sk2f::Load(P);
482 Sk2f p1 = Sk2f::Load(P+1);
483 Sk2f p2 = Sk2f::Load(P+2);
484 Sk2f p3 = Sk2f::Load(P+3);
485
486 // Also detect near-quadratics ahead of time.
487 Sk2f tan0, tan1, c;
488 get_cubic_tangents(p0, p1, p2, p3, &tan0, &tan1);
489 if (is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, &c)) {
490 this->appendQuadratics(p0, c, p3);
491 return;
492 }
493
494 double tt[2], ss[2], D[4];
495 fCurrCubicType = SkClassifyCubic(P, tt, ss, D);
496 SkASSERT(!SkCubicIsDegenerate(fCurrCubicType));
497 Sk2f t = Sk2f(static_cast<float>(tt[0]), static_cast<float>(tt[1]));
498 Sk2f s = Sk2f(static_cast<float>(ss[0]), static_cast<float>(ss[1]));
499
Chris Dalton4229b352018-04-18 14:13:45 -0600500 ExcludedTerm skipTerm = (std::abs(D[2]) > std::abs(D[1]))
501 ? ExcludedTerm::kQuadraticTerm
502 : ExcludedTerm::kLinearTerm;
503 Sk2f C0 = SkNx_fma(Sk2f(3), p1 - p2, p3 - p0);
504 Sk2f C1 = (ExcludedTerm::kLinearTerm == skipTerm
505 ? SkNx_fma(Sk2f(-2), p1, p0 + p2)
506 : p1 - p0) * 3;
507 Sk2f C0x1 = C0 * SkNx_shuffle<1,0>(C1);
508 float Cdet = C0x1[0] - C0x1[1];
Chris Daltonb3a69592018-04-18 14:10:22 -0600509
510 SkSTArray<4, float> chops;
511 if (SkCubicType::kLoop != fCurrCubicType) {
Chris Dalton4229b352018-04-18 14:13:45 -0600512 find_chops_around_inflection_points(inflectPad, t, s, C0, C1, skipTerm, Cdet, &chops);
Chris Daltonb3a69592018-04-18 14:10:22 -0600513 } else {
Chris Dalton4229b352018-04-18 14:13:45 -0600514 find_chops_around_loop_intersection(loopIntersectPad, t, s, C0, C1, skipTerm, Cdet, &chops);
Chris Daltonb3a69592018-04-18 14:10:22 -0600515 }
Chris Dalton5450ab12018-04-18 16:49:13 -0600516 if (4 == chops.count() && chops[1] >= chops[2]) {
Chris Daltonb3a69592018-04-18 14:10:22 -0600517 // This just the means the KLM roots are so close that their paddings overlap. We will
518 // approximate the entire middle section, but still have it chopped midway. For loops this
519 // chop guarantees the append code only sees convex segments. Otherwise, it means we are (at
520 // least almost) a cusp and the chop makes sure we get a sharp point.
521 Sk2f ts = t * SkNx_shuffle<1,0>(s);
522 chops[1] = chops[2] = (ts[0] + ts[1]) / (2*s[0]*s[1]);
523 }
524
525#ifdef SK_DEBUG
526 for (int i = 1; i < chops.count(); ++i) {
527 SkASSERT(chops[i] >= chops[i - 1]);
528 }
529#endif
530 this->appendCubics(AppendCubicMode::kLiteral, p0, p1, p2, p3, chops.begin(), chops.count());
Chris Dalton29011a22017-09-28 12:08:33 -0600531}
532
Chris Daltonb3a69592018-04-18 14:10:22 -0600533static inline void chop_cubic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, const Sk2f& p3,
534 float T, Sk2f* ab, Sk2f* abc, Sk2f* abcd, Sk2f* bcd, Sk2f* cd) {
535 Sk2f TT = T;
536 *ab = lerp(p0, p1, TT);
537 Sk2f bc = lerp(p1, p2, TT);
538 *cd = lerp(p2, p3, TT);
539 *abc = lerp(*ab, bc, TT);
540 *bcd = lerp(bc, *cd, TT);
541 *abcd = lerp(*abc, *bcd, TT);
542}
Chris Dalton29011a22017-09-28 12:08:33 -0600543
Chris Daltonb3a69592018-04-18 14:10:22 -0600544void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f& p1,
545 const Sk2f& p2, const Sk2f& p3, const float chops[], int numChops,
546 float localT0, float localT1) {
547 if (numChops) {
548 SkASSERT(numChops > 0);
549 int midChopIdx = numChops/2;
550 float T = chops[midChopIdx];
551 // Chops alternate between literal and approximate mode.
552 AppendCubicMode rightMode = (AppendCubicMode)((bool)mode ^ (midChopIdx & 1) ^ 1);
Chris Dalton29011a22017-09-28 12:08:33 -0600553
Chris Daltonb3a69592018-04-18 14:10:22 -0600554 if (T <= localT0) {
555 // T is outside 0..1. Append the right side only.
556 this->appendCubics(rightMode, p0, p1, p2, p3, &chops[midChopIdx + 1],
557 numChops - midChopIdx - 1, localT0, localT1);
558 return;
559 }
560
561 if (T >= localT1) {
562 // T is outside 0..1. Append the left side only.
563 this->appendCubics(mode, p0, p1, p2, p3, chops, midChopIdx, localT0, localT1);
564 return;
565 }
566
567 float localT = (T - localT0) / (localT1 - localT0);
568 Sk2f p01, p02, pT, p11, p12;
569 chop_cubic(p0, p1, p2, p3, localT, &p01, &p02, &pT, &p11, &p12);
570 this->appendCubics(mode, p0, p01, p02, pT, chops, midChopIdx, localT0, T);
571 this->appendCubics(rightMode, pT, p11, p12, p3, &chops[midChopIdx + 1],
572 numChops - midChopIdx - 1, T, localT1);
573 return;
574 }
575
576 this->appendCubics(mode, p0, p1, p2, p3);
577}
578
579void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f& p1,
580 const Sk2f& p2, const Sk2f& p3, int maxSubdivisions) {
Chris Daltonb3a69592018-04-18 14:10:22 -0600581 if (SkCubicType::kLoop != fCurrCubicType) {
582 // Serpentines and cusps are always monotonic after chopping around inflection points.
583 SkASSERT(!SkCubicIsDegenerate(fCurrCubicType));
584
585 if (AppendCubicMode::kApproximate == mode) {
586 // This section passes through an inflection point, so we can get away with a flat line.
587 // This can cause some curves to feel slightly more flat when inspected rigorously back
588 // and forth against another renderer, but for now this seems acceptable given the
589 // simplicity.
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600590 this->appendLine(p0, p3);
Chris Daltonb3a69592018-04-18 14:10:22 -0600591 return;
592 }
593 } else {
594 Sk2f tan0, tan1;
595 get_cubic_tangents(p0, p1, p2, p3, &tan0, &tan1);
596
597 if (maxSubdivisions && !is_convex_curve_monotonic(p0, tan0, p3, tan1)) {
598 this->chopAndAppendCubicAtMidTangent(mode, p0, p1, p2, p3, tan0, tan1,
599 maxSubdivisions - 1);
600 return;
601 }
602
603 if (AppendCubicMode::kApproximate == mode) {
604 Sk2f c;
605 if (!is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, &c) && maxSubdivisions) {
606 this->chopAndAppendCubicAtMidTangent(mode, p0, p1, p2, p3, tan0, tan1,
607 maxSubdivisions - 1);
608 return;
609 }
610
611 this->appendMonotonicQuadratic(p0, c, p3);
612 return;
613 }
614 }
615
616 // Don't send curves to the GPU if we know they are nearly flat (or just very small).
617 // Since the cubic segment is known to be convex at this point, our flatness check is simple.
618 if (are_collinear(p0, (p1 + p2) * .5f, p3)) {
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600619 this->appendLine(p0, p3);
Chris Daltonb3a69592018-04-18 14:10:22 -0600620 return;
621 }
622
623 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600624 SkASSERT((p0 != p3).anyTrue());
Chris Daltonb3a69592018-04-18 14:10:22 -0600625 p1.store(&fPoints.push_back());
626 p2.store(&fPoints.push_back());
627 p3.store(&fPoints.push_back());
628 fVerbs.push_back(Verb::kMonotonicCubicTo);
629 ++fCurrContourTallies.fCubics;
Chris Dalton29011a22017-09-28 12:08:33 -0600630}
631
Chris Dalton9f2dab02018-04-18 14:07:03 -0600632// Given a convex curve segment with the following order-2 tangent function:
633//
634// |C2x C2y|
635// tan = some_scale * |dx/dt dy/dt| = |t^2 t 1| * |C1x C1y|
636// |C0x C0y|
637//
638// This function finds the T value whose tangent angle is halfway between the tangents at T=0 and
639// T=1 (tan0 and tan1).
640static inline float find_midtangent(const Sk2f& tan0, const Sk2f& tan1,
Chris Dalton5ed4df32018-07-18 12:41:43 -0600641 const Sk2f& C2, const Sk2f& C1, const Sk2f& C0) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600642 // Tangents point in the direction of increasing T, so tan0 and -tan1 both point toward the
643 // midtangent. 'n' will therefore bisect tan0 and -tan1, giving us the normal to the midtangent.
644 //
645 // n dot midtangent = 0
646 //
647 Sk2f n = normalize(tan0) - normalize(tan1);
648
649 // Find the T value at the midtangent. This is a simple quadratic equation:
650 //
651 // midtangent dot n = 0
652 //
653 // (|t^2 t 1| * C) dot n = 0
654 //
655 // |t^2 t 1| dot C*n = 0
656 //
657 // First find coeffs = C*n.
658 Sk4f C[2];
659 Sk2f::Store4(C, C2, C1, C0, 0);
660 Sk4f coeffs = C[0]*n[0] + C[1]*n[1];
Chris Dalton9f2dab02018-04-18 14:07:03 -0600661
662 // Now solve the quadratic.
663 float a = coeffs[0], b = coeffs[1], c = coeffs[2];
664 float discr = b*b - 4*a*c;
665 if (discr < 0) {
666 return 0; // This will only happen if the curve is a line.
667 }
668
669 // The roots are q/a and c/q. Pick the one closer to T=.5.
670 float q = -.5f * (b + copysignf(std::sqrt(discr), b));
671 float r = .5f*q*a;
672 return std::abs(q*q - r) < std::abs(a*c - r) ? q/a : c/q;
673}
674
Chris Daltonb3a69592018-04-18 14:10:22 -0600675inline void GrCCGeometry::chopAndAppendCubicAtMidTangent(AppendCubicMode mode, const Sk2f& p0,
676 const Sk2f& p1, const Sk2f& p2,
677 const Sk2f& p3, const Sk2f& tan0,
678 const Sk2f& tan1,
679 int maxFutureSubdivisions) {
Chris Dalton5ed4df32018-07-18 12:41:43 -0600680 float midT = find_midtangent(tan0, tan1, p3 + (p1 - p2)*3 - p0,
681 (p0 - p1*2 + p2)*2,
682 p1 - p0);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600683 // Use positive logic since NaN fails comparisons. (However midT should not be NaN since we cull
684 // near-flat cubics in cubicTo().)
685 if (!(midT > 0 && midT < 1)) {
686 // The cubic is flat. Otherwise there would be a real midtangent inside T=0..1.
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600687 this->appendLine(p0, p3);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600688 return;
689 }
690
Chris Daltonb3a69592018-04-18 14:10:22 -0600691 Sk2f p01, p02, pT, p11, p12;
692 chop_cubic(p0, p1, p2, p3, midT, &p01, &p02, &pT, &p11, &p12);
693 this->appendCubics(mode, p0, p01, p02, pT, maxFutureSubdivisions);
694 this->appendCubics(mode, pT, p11, p12, p3, maxFutureSubdivisions);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600695}
696
Chris Dalton9f2dab02018-04-18 14:07:03 -0600697void GrCCGeometry::conicTo(const SkPoint P[3], float w) {
698 SkASSERT(fBuildingContour);
699 SkASSERT(P[0] == fPoints.back());
700 Sk2f p0 = Sk2f::Load(P);
701 Sk2f p1 = Sk2f::Load(P+1);
702 Sk2f p2 = Sk2f::Load(P+2);
703
Chris Dalton9f2dab02018-04-18 14:07:03 -0600704 Sk2f tan0 = p1 - p0;
705 Sk2f tan1 = p2 - p1;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600706
707 if (!is_convex_curve_monotonic(p0, tan0, p2, tan1)) {
Chris Daltond8bae7d2018-04-19 13:13:25 -0600708 // The derivative of a conic has a cumbersome order-4 denominator. However, this isn't
709 // necessary if we are only interested in a vector in the same *direction* as a given
710 // tangent line. Since the denominator scales dx and dy uniformly, we can throw it out
711 // completely after evaluating the derivative with the standard quotient rule. This leaves
712 // us with a simpler quadratic function that we use to find the midtangent.
Chris Dalton5ed4df32018-07-18 12:41:43 -0600713 float midT = find_midtangent(tan0, tan1, (w - 1) * (p2 - p0),
714 (p2 - p0) - 2*w*(p1 - p0),
715 w*(p1 - p0));
Chris Daltond8bae7d2018-04-19 13:13:25 -0600716 // Use positive logic since NaN fails comparisons. (However midT should not be NaN since we
717 // cull near-linear conics above. And while w=0 is flat, it's not a line and has valid
718 // midtangents.)
719 if (!(midT > 0 && midT < 1)) {
720 // The conic is flat. Otherwise there would be a real midtangent inside T=0..1.
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600721 this->appendLine(p0, p2);
Chris Daltond8bae7d2018-04-19 13:13:25 -0600722 return;
723 }
724
Chris Dalton9f2dab02018-04-18 14:07:03 -0600725 // Chop the conic at midtangent to produce two monotonic segments.
Chris Daltond8bae7d2018-04-19 13:13:25 -0600726 Sk4f p3d0 = Sk4f(p0[0], p0[1], 1, 0);
727 Sk4f p3d1 = Sk4f(p1[0], p1[1], 1, 0) * w;
728 Sk4f p3d2 = Sk4f(p2[0], p2[1], 1, 0);
729 Sk4f midT4 = midT;
730
731 Sk4f p3d01 = lerp(p3d0, p3d1, midT4);
732 Sk4f p3d12 = lerp(p3d1, p3d2, midT4);
733 Sk4f p3d012 = lerp(p3d01, p3d12, midT4);
734
735 Sk2f midpoint = Sk2f(p3d012[0], p3d012[1]) / p3d012[2];
Chris Dalton9f2dab02018-04-18 14:07:03 -0600736 Sk2f ww = Sk2f(p3d01[2], p3d12[2]) * Sk2f(p3d012[2]).rsqrt();
Chris Daltond8bae7d2018-04-19 13:13:25 -0600737
Chris Dalton9f2dab02018-04-18 14:07:03 -0600738 this->appendMonotonicConic(p0, Sk2f(p3d01[0], p3d01[1]) / p3d01[2], midpoint, ww[0]);
739 this->appendMonotonicConic(midpoint, Sk2f(p3d12[0], p3d12[1]) / p3d12[2], p2, ww[1]);
740 return;
741 }
742
743 this->appendMonotonicConic(p0, p1, p2, w);
744}
745
746void GrCCGeometry::appendMonotonicConic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, float w) {
Chris Daltond8bae7d2018-04-19 13:13:25 -0600747 SkASSERT(w >= 0);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600748
Chris Daltond8bae7d2018-04-19 13:13:25 -0600749 Sk2f base = p2 - p0;
750 Sk2f baseAbs = base.abs();
751 float baseWidth = baseAbs[0] + baseAbs[1];
752
753 // Find the height of the curve. Max height always occurs at T=.5 for conics.
754 Sk2f d = (p1 - p0) * SkNx_shuffle<1,0>(base);
755 float h1 = std::abs(d[1] - d[0]); // Height of p1 above the base.
756 float ht = h1*w, hs = 1 + w; // Height of the conic = ht/hs.
757
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600758 // i.e. (ht/hs <= baseWidth * kFlatnessThreshold). Use "<=" in case base == 0.
759 if (ht <= (baseWidth*hs) * kFlatnessThreshold) {
Chris Daltond8bae7d2018-04-19 13:13:25 -0600760 // We are flat. (See rationale in are_collinear.)
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600761 this->appendLine(p0, p2);
Chris Daltond8bae7d2018-04-19 13:13:25 -0600762 return;
763 }
764
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600765 // i.e. (w > 1 && h1 - ht/hs < baseWidth).
766 if (w > 1 && h1*hs - ht < baseWidth*hs) {
Chris Daltond8bae7d2018-04-19 13:13:25 -0600767 // If we get within 1px of p1 when w > 1, we will pick up artifacts from the implicit
768 // function's reflection. Chop at max height (T=.5) and draw a triangle instead.
769 Sk2f p1w = p1*w;
770 Sk2f ab = p0 + p1w;
771 Sk2f bc = p1w + p2;
772 Sk2f highpoint = (ab + bc) / (2*(1 + w));
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600773 this->appendLine(p0, highpoint);
774 this->appendLine(highpoint, p2);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600775 return;
776 }
777
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600778 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
779 SkASSERT((p0 != p2).anyTrue());
Chris Dalton9f2dab02018-04-18 14:07:03 -0600780 p1.store(&fPoints.push_back());
781 p2.store(&fPoints.push_back());
782 fConicWeights.push_back(w);
783 fVerbs.push_back(Verb::kMonotonicConicTo);
784 ++fCurrContourTallies.fConics;
785}
786
Chris Dalton383a2ef2018-01-08 17:21:41 -0500787GrCCGeometry::PrimitiveTallies GrCCGeometry::endContour() {
Chris Daltonc1e59632017-09-05 00:30:07 -0600788 SkASSERT(fBuildingContour);
789 SkASSERT(fVerbs.count() >= fCurrContourTallies.fTriangles);
790
791 // The fTriangles field currently contains this contour's starting verb index. We can now
792 // use it to calculate the size of the contour's fan.
793 int fanSize = fVerbs.count() - fCurrContourTallies.fTriangles;
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600794 if (fPoints.back() == fCurrAnchorPoint) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600795 --fanSize;
796 fVerbs.push_back(Verb::kEndClosedContour);
797 } else {
798 fVerbs.push_back(Verb::kEndOpenContour);
799 }
800
801 fCurrContourTallies.fTriangles = SkTMax(fanSize - 2, 0);
802
Chris Dalton383a2ef2018-01-08 17:21:41 -0500803 SkDEBUGCODE(fBuildingContour = false);
Chris Daltonc1e59632017-09-05 00:30:07 -0600804 return fCurrContourTallies;
Chris Dalton419a94d2017-08-28 10:24:22 -0600805}