blob: 9f44049ab58958c21a71fac46895c590cad9d977 [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 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 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 Dalton7f578bf2017-09-05 16:46:48 -0600232using ExcludedTerm = GrPathUtils::ExcludedTerm;
Chris Daltonc1e59632017-09-05 00:30:07 -0600233
Chris Daltonb3a69592018-04-18 14:10:22 -0600234// Finds where to chop a non-loop around its inflection points. The resulting cubic segments will be
235// chopped such that a box of radius 'padRadius', centered at any point along the curve segment, is
236// guaranteed to not cross the tangent lines at the inflection points (a.k.a lines L & M).
Chris Dalton7f578bf2017-09-05 16:46:48 -0600237//
Chris Dalton5450ab12018-04-18 16:49:13 -0600238// 'chops' will be filled with 0, 2, or 4 T values. The segments between T0..T1 and T2..T3 must be
239// drawn with flat lines instead of cubics.
Chris Dalton7f578bf2017-09-05 16:46:48 -0600240//
241// A serpentine cubic has two inflection points, so this method takes Sk2f and computes the padding
242// for both in SIMD.
Chris Dalton5450ab12018-04-18 16:49:13 -0600243static inline void find_chops_around_inflection_points(float padRadius, Sk2f tl, Sk2f sl,
244 const SkMatrix& CIT, ExcludedTerm skipTerm,
Chris Daltonb3a69592018-04-18 14:10:22 -0600245 SkSTArray<4, float>* chops) {
246 SkASSERT(chops->empty());
Chris Dalton7f578bf2017-09-05 16:46:48 -0600247 SkASSERT(padRadius >= 0);
Chris Daltonc1e59632017-09-05 00:30:07 -0600248
Chris Dalton5450ab12018-04-18 16:49:13 -0600249 // The homogeneous parametric functions for distance from lines L & M are:
250 //
251 // l(t,s) = (t*sl - s*tl)^3
252 // m(t,s) = (t*sm - s*tm)^3
253 //
254 // See "Resolution Independent Curve Rendering using Programmable Graphics Hardware",
255 // 4.3 Finding klmn:
256 //
257 // https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf
258 //
259 // From here on we use Sk2f with "L" names, but the second lane will be for line M.
260 tl = (sl > 0).thenElse(tl, -tl); // Tl=tl/sl is the triple root of l(t,s). Normalize so s >= 0.
261 sl = sl.abs();
Chris Dalton7f578bf2017-09-05 16:46:48 -0600262
Chris Dalton5450ab12018-04-18 16:49:13 -0600263 // Convert l(t,s), m(t,s) to power-basis form:
264 //
265 // | l3 m3 |
266 // |l(t,s) m(t,s)| = |t^3 t^2*s t*s^2 s^3| * | l2 m2 |
267 // | l1 m1 |
268 // | l0 m0 |
269 //
270 Sk2f l3 = sl*sl*sl;
271 Sk2f l2or1 = (ExcludedTerm::kLinearTerm == skipTerm) ? sl*sl*tl*-3 : sl*tl*tl*3;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600272
Chris Dalton5450ab12018-04-18 16:49:13 -0600273 // The equation for line L can be found as follows:
274 //
275 // L = C^-1 * (l excluding skipTerm)
276 //
277 // (See comments for GrPathUtils::calcCubicInverseTransposePowerBasisMatrix.)
278 Sk2f Lx = CIT[0] * l3 + CIT[3] * l2or1;
279 Sk2f Ly = CIT[1] * l3 + CIT[4] * l2or1;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600280
Chris Dalton5450ab12018-04-18 16:49:13 -0600281 // A box of radius "padRadius" is touching line L if "center dot L" is less than the Manhattan
282 // with of L. (See rationale in are_collinear.)
283 Sk2f Lwidth = Lx.abs() + Ly.abs();
284 Sk2f pad = Lwidth * padRadius;
285
286 // Will T=(t + cbrt(pad))/s be greater than 0? No need to solve roots outside T=0..1.
287 Sk2f insideLeftPad = pad + tl*tl*tl;
288
289 // Will T=(t - cbrt(pad))/s be less than 1? No need to solve roots outside T=0..1.
290 Sk2f tms = tl - sl;
291 Sk2f insideRightPad = pad - tms*tms*tms;
292
293 // Solve for the T values where abs(l(T)) = pad.
294 if (insideLeftPad[0] > 0 && insideRightPad[0] > 0) {
295 float padT = cbrtf(pad[0]);
296 Sk2f pts = (tl[0] + Sk2f(-padT, +padT)) / sl[0];
297 pts.store(chops->push_back_n(2));
298 }
299
300 // Solve for the T values where abs(m(T)) = pad.
301 if (insideLeftPad[1] > 0 && insideRightPad[1] > 0) {
302 float padT = cbrtf(pad[1]);
303 Sk2f pts = (tl[1] + Sk2f(-padT, +padT)) / sl[1];
304 pts.store(chops->push_back_n(2));
305 }
Chris Dalton7f578bf2017-09-05 16:46:48 -0600306}
307
308static inline void swap_if_greater(float& a, float& b) {
309 if (a > b) {
310 std::swap(a, b);
311 }
312}
313
Chris Daltonb3a69592018-04-18 14:10:22 -0600314// Finds where to chop a non-loop around its intersection point. The resulting cubic segments will
315// be chopped such that a box of radius 'padRadius', centered at any point along the curve segment,
316// 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 -0600317//
Chris Daltonb3a69592018-04-18 14:10:22 -0600318// 'chops' will be filled with 0, 2, or 4 T values. The segments between T0..T1 and T2..T3 must be
319// drawn with quadratic splines instead of cubics.
Chris Dalton7f578bf2017-09-05 16:46:48 -0600320//
Chris Daltonb3a69592018-04-18 14:10:22 -0600321// A loop intersection falls at two different T values, so this method takes Sk2f and computes the
322// padding for both in SIMD.
Chris Dalton5450ab12018-04-18 16:49:13 -0600323static inline void find_chops_around_loop_intersection(float padRadius, Sk2f t2, Sk2f s2,
324 const SkMatrix& CIT, ExcludedTerm skipTerm,
Chris Daltonb3a69592018-04-18 14:10:22 -0600325 SkSTArray<4, float>* chops) {
326 SkASSERT(chops->empty());
Chris Dalton7f578bf2017-09-05 16:46:48 -0600327 SkASSERT(padRadius >= 0);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600328
Chris Dalton5450ab12018-04-18 16:49:13 -0600329 // The parametric functions for distance from lines L & M are:
330 //
331 // l(T) = (T - Td)^2 * (T - Te)
332 // m(T) = (T - Td) * (T - Te)^2
333 //
334 // See "Resolution Independent Curve Rendering using Programmable Graphics Hardware",
335 // 4.3 Finding klmn:
336 //
337 // https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf
338 Sk2f T2 = t2/s2; // T2 is the double root of l(T).
339 Sk2f T1 = SkNx_shuffle<1,0>(T2); // T1 is the other root of l(T).
Chris Dalton7f578bf2017-09-05 16:46:48 -0600340
Chris Dalton5450ab12018-04-18 16:49:13 -0600341 // Convert l(T), m(T) to power-basis form:
342 //
343 // | 1 1 |
344 // |l(T) m(T)| = |T^3 T^2 T 1| * | l2 m2 |
345 // | l1 m1 |
346 // | l0 m0 |
347 //
348 // From here on we use Sk2f with "L" names, but the second lane will be for line M.
349 Sk2f l2 = SkNx_fma(Sk2f(-2), T2, -T1);
350 Sk2f l1 = T2 * SkNx_fma(Sk2f(2), T1, T2);
351 Sk2f l0 = -T2*T2*T1;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600352
Chris Dalton5450ab12018-04-18 16:49:13 -0600353 // The equation for line L can be found as follows:
354 //
355 // L = C^-1 * (l excluding skipTerm)
356 //
357 // (See comments for GrPathUtils::calcCubicInverseTransposePowerBasisMatrix.)
358 Sk2f l2or1 = (ExcludedTerm::kLinearTerm == skipTerm) ? l2 : l1;
359 Sk2f Lx = CIT[3] * l2or1 + CIT[0]; // l3 is always 1.
360 Sk2f Ly = CIT[4] * l2or1 - CIT[1];
Chris Dalton7f578bf2017-09-05 16:46:48 -0600361
Chris Dalton5450ab12018-04-18 16:49:13 -0600362 // A box of radius "padRadius" is touching line L if "center dot L" is less than the Manhattan
363 // with of L. (See rationale in are_collinear.)
364 Sk2f Lwidth = Lx.abs() + Ly.abs();
365 Sk2f pad = Lwidth * padRadius;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600366
Chris Dalton5450ab12018-04-18 16:49:13 -0600367 // Is l(T=0) outside the padding around line L?
368 Sk2f lT0 = l0; // l(T=0) = |0 0 0 1| dot |1 l2 l1 l0| = l0
369 Sk2f outsideT0 = lT0.abs() - pad;
370
371 // Is l(T=1) outside the padding around line L?
372 Sk2f lT1 = (Sk2f(1) + l2 + l1 + l0).abs(); // l(T=1) = |1 1 1 1| dot |1 l2 l1 l0|
373 Sk2f outsideT1 = lT1.abs() - pad;
374
375 // Values for solving the cubic.
376 Sk2f p, q, qqq, discr, numRoots, D;
377 bool hasDiscr = false;
378
379 // Values for calculating one root (rarely needed).
380 Sk2f R, QQ;
381 bool hasOneRootVals = false;
Chris Daltonc1e59632017-09-05 00:30:07 -0600382
Chris Dalton7f578bf2017-09-05 16:46:48 -0600383 // Values for calculating three roots.
Chris Dalton5450ab12018-04-18 16:49:13 -0600384 Sk2f P, cosTheta3;
385 bool hasThreeRootVals = false;
Chris Daltonc1e59632017-09-05 00:30:07 -0600386
Chris Dalton5450ab12018-04-18 16:49:13 -0600387 // Solve for the T values where l(T) = +pad and m(T) = -pad.
Chris Dalton7f578bf2017-09-05 16:46:48 -0600388 for (int i = 0; i < 2; ++i) {
Chris Dalton5450ab12018-04-18 16:49:13 -0600389 float T = T2[i]; // T is the point we are chopping around.
390 if ((T < 0 && outsideT0[i] >= 0) || (T > 1 && outsideT1[i] >= 0)) {
391 // The padding around T is completely out of range. No point solving for it.
392 continue;
393 }
394
395 if (!hasDiscr) {
396 p = Sk2f(+.5f, -.5f) * pad;
397 q = (1.f/3) * (T2 - T1);
398 qqq = q*q*q;
399 discr = qqq*p*2 + p*p;
400 numRoots = (discr < 0).thenElse(3, 1);
401 D = T2 - q;
402 hasDiscr = true;
403 }
404
Chris Dalton7f578bf2017-09-05 16:46:48 -0600405 if (1 == numRoots[i]) {
Chris Dalton5450ab12018-04-18 16:49:13 -0600406 if (!hasOneRootVals) {
407 Sk2f r = qqq + p;
408 Sk2f s = r.abs() + discr.sqrt();
409 R = (r > 0).thenElse(-s, s);
410 QQ = q*q;
411 hasOneRootVals = true;
412 }
413
414 float A = cbrtf(R[i]);
415 float B = A != 0 ? QQ[i]/A : 0;
416 // 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 -0600417 if (1 == i) {
418 chops->push_back(0);
419 }
Chris Daltonb3a69592018-04-18 14:10:22 -0600420 chops->push_back(A + B + D[i]);
421 if (0 == i) {
422 chops->push_back(1);
423 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600424 continue;
425 }
426
Chris Dalton5450ab12018-04-18 16:49:13 -0600427 if (!hasThreeRootVals) {
428 P = q.abs() * -2;
429 cosTheta3 = (q >= 0).thenElse(1, -1) + p / qqq.abs();
430 hasThreeRootVals = true;
431 }
432
Chris Dalton7f578bf2017-09-05 16:46:48 -0600433 static constexpr float k2PiOver3 = 2 * SK_ScalarPI / 3;
434 float theta = std::acos(cosTheta3[i]) * (1.f/3);
Chris Daltonb3a69592018-04-18 14:10:22 -0600435 float roots[3] = {P[i] * std::cos(theta) + D[i],
436 P[i] * std::cos(theta + k2PiOver3) + D[i],
437 P[i] * std::cos(theta - k2PiOver3) + D[i]};
Chris Daltonc1e59632017-09-05 00:30:07 -0600438
Chris Dalton7f578bf2017-09-05 16:46:48 -0600439 // Sort the three roots.
Chris Daltonb3a69592018-04-18 14:10:22 -0600440 swap_if_greater(roots[0], roots[1]);
441 swap_if_greater(roots[1], roots[2]);
442 swap_if_greater(roots[0], roots[1]);
443
444 // Line L chops around the first 2 roots, line M chops around the second 2.
445 chops->push_back_n(2, &roots[i]);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600446 }
447}
448
Chris Daltonb3a69592018-04-18 14:10:22 -0600449void GrCCGeometry::cubicTo(const SkPoint P[4], float inflectPad, float loopIntersectPad) {
450 SkASSERT(fBuildingContour);
451 SkASSERT(P[0] == fPoints.back());
Chris Dalton29011a22017-09-28 12:08:33 -0600452
Chris Daltonb3a69592018-04-18 14:10:22 -0600453 // Don't crunch on the curve or inflate geometry if it is nearly flat (or just very small).
454 // Flat curves can break the math below.
455 if (are_collinear(P)) {
456 this->lineTo(P[3]);
457 return;
458 }
Chris Dalton29011a22017-09-28 12:08:33 -0600459
Chris Daltonb3a69592018-04-18 14:10:22 -0600460 Sk2f p0 = Sk2f::Load(P);
461 Sk2f p1 = Sk2f::Load(P+1);
462 Sk2f p2 = Sk2f::Load(P+2);
463 Sk2f p3 = Sk2f::Load(P+3);
464
465 // Also detect near-quadratics ahead of time.
466 Sk2f tan0, tan1, c;
467 get_cubic_tangents(p0, p1, p2, p3, &tan0, &tan1);
468 if (is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, &c)) {
469 this->appendQuadratics(p0, c, p3);
470 return;
471 }
472
473 double tt[2], ss[2], D[4];
474 fCurrCubicType = SkClassifyCubic(P, tt, ss, D);
475 SkASSERT(!SkCubicIsDegenerate(fCurrCubicType));
476 Sk2f t = Sk2f(static_cast<float>(tt[0]), static_cast<float>(tt[1]));
477 Sk2f s = Sk2f(static_cast<float>(ss[0]), static_cast<float>(ss[1]));
478
479 SkMatrix CIT;
480 ExcludedTerm skipTerm = GrPathUtils::calcCubicInverseTransposePowerBasisMatrix(P, &CIT);
481 SkASSERT(ExcludedTerm::kNonInvertible != skipTerm); // Should have been caught above.
482 SkASSERT(0 == CIT[6]);
483 SkASSERT(0 == CIT[7]);
484 SkASSERT(1 == CIT[8]);
485
486 SkSTArray<4, float> chops;
487 if (SkCubicType::kLoop != fCurrCubicType) {
488 find_chops_around_inflection_points(inflectPad, t, s, CIT, skipTerm, &chops);
489 } else {
490 find_chops_around_loop_intersection(loopIntersectPad, t, s, CIT, skipTerm, &chops);
491 }
Chris Dalton5450ab12018-04-18 16:49:13 -0600492 if (4 == chops.count() && chops[1] >= chops[2]) {
Chris Daltonb3a69592018-04-18 14:10:22 -0600493 // This just the means the KLM roots are so close that their paddings overlap. We will
494 // approximate the entire middle section, but still have it chopped midway. For loops this
495 // chop guarantees the append code only sees convex segments. Otherwise, it means we are (at
496 // least almost) a cusp and the chop makes sure we get a sharp point.
497 Sk2f ts = t * SkNx_shuffle<1,0>(s);
498 chops[1] = chops[2] = (ts[0] + ts[1]) / (2*s[0]*s[1]);
499 }
500
501#ifdef SK_DEBUG
502 for (int i = 1; i < chops.count(); ++i) {
503 SkASSERT(chops[i] >= chops[i - 1]);
504 }
505#endif
506 this->appendCubics(AppendCubicMode::kLiteral, p0, p1, p2, p3, chops.begin(), chops.count());
Chris Dalton29011a22017-09-28 12:08:33 -0600507}
508
Chris Daltonb3a69592018-04-18 14:10:22 -0600509static inline void chop_cubic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, const Sk2f& p3,
510 float T, Sk2f* ab, Sk2f* abc, Sk2f* abcd, Sk2f* bcd, Sk2f* cd) {
511 Sk2f TT = T;
512 *ab = lerp(p0, p1, TT);
513 Sk2f bc = lerp(p1, p2, TT);
514 *cd = lerp(p2, p3, TT);
515 *abc = lerp(*ab, bc, TT);
516 *bcd = lerp(bc, *cd, TT);
517 *abcd = lerp(*abc, *bcd, TT);
518}
Chris Dalton29011a22017-09-28 12:08:33 -0600519
Chris Daltonb3a69592018-04-18 14:10:22 -0600520void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f& p1,
521 const Sk2f& p2, const Sk2f& p3, const float chops[], int numChops,
522 float localT0, float localT1) {
523 if (numChops) {
524 SkASSERT(numChops > 0);
525 int midChopIdx = numChops/2;
526 float T = chops[midChopIdx];
527 // Chops alternate between literal and approximate mode.
528 AppendCubicMode rightMode = (AppendCubicMode)((bool)mode ^ (midChopIdx & 1) ^ 1);
Chris Dalton29011a22017-09-28 12:08:33 -0600529
Chris Daltonb3a69592018-04-18 14:10:22 -0600530 if (T <= localT0) {
531 // T is outside 0..1. Append the right side only.
532 this->appendCubics(rightMode, p0, p1, p2, p3, &chops[midChopIdx + 1],
533 numChops - midChopIdx - 1, localT0, localT1);
534 return;
535 }
536
537 if (T >= localT1) {
538 // T is outside 0..1. Append the left side only.
539 this->appendCubics(mode, p0, p1, p2, p3, chops, midChopIdx, localT0, localT1);
540 return;
541 }
542
543 float localT = (T - localT0) / (localT1 - localT0);
544 Sk2f p01, p02, pT, p11, p12;
545 chop_cubic(p0, p1, p2, p3, localT, &p01, &p02, &pT, &p11, &p12);
546 this->appendCubics(mode, p0, p01, p02, pT, chops, midChopIdx, localT0, T);
547 this->appendCubics(rightMode, pT, p11, p12, p3, &chops[midChopIdx + 1],
548 numChops - midChopIdx - 1, T, localT1);
549 return;
550 }
551
552 this->appendCubics(mode, p0, p1, p2, p3);
553}
554
555void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f& p1,
556 const Sk2f& p2, const Sk2f& p3, int maxSubdivisions) {
557 if ((p0 == p3).allTrue()) {
558 return;
559 }
560
561 if (SkCubicType::kLoop != fCurrCubicType) {
562 // Serpentines and cusps are always monotonic after chopping around inflection points.
563 SkASSERT(!SkCubicIsDegenerate(fCurrCubicType));
564
565 if (AppendCubicMode::kApproximate == mode) {
566 // This section passes through an inflection point, so we can get away with a flat line.
567 // This can cause some curves to feel slightly more flat when inspected rigorously back
568 // and forth against another renderer, but for now this seems acceptable given the
569 // simplicity.
570 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
571 this->appendLine(p3);
572 return;
573 }
574 } else {
575 Sk2f tan0, tan1;
576 get_cubic_tangents(p0, p1, p2, p3, &tan0, &tan1);
577
578 if (maxSubdivisions && !is_convex_curve_monotonic(p0, tan0, p3, tan1)) {
579 this->chopAndAppendCubicAtMidTangent(mode, p0, p1, p2, p3, tan0, tan1,
580 maxSubdivisions - 1);
581 return;
582 }
583
584 if (AppendCubicMode::kApproximate == mode) {
585 Sk2f c;
586 if (!is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, &c) && maxSubdivisions) {
587 this->chopAndAppendCubicAtMidTangent(mode, p0, p1, p2, p3, tan0, tan1,
588 maxSubdivisions - 1);
589 return;
590 }
591
592 this->appendMonotonicQuadratic(p0, c, p3);
593 return;
594 }
595 }
596
597 // Don't send curves to the GPU if we know they are nearly flat (or just very small).
598 // Since the cubic segment is known to be convex at this point, our flatness check is simple.
599 if (are_collinear(p0, (p1 + p2) * .5f, p3)) {
600 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
601 this->appendLine(p3);
602 return;
603 }
604
605 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
606 p1.store(&fPoints.push_back());
607 p2.store(&fPoints.push_back());
608 p3.store(&fPoints.push_back());
609 fVerbs.push_back(Verb::kMonotonicCubicTo);
610 ++fCurrContourTallies.fCubics;
Chris Dalton29011a22017-09-28 12:08:33 -0600611}
612
Chris Dalton9f2dab02018-04-18 14:07:03 -0600613// Given a convex curve segment with the following order-2 tangent function:
614//
615// |C2x C2y|
616// tan = some_scale * |dx/dt dy/dt| = |t^2 t 1| * |C1x C1y|
617// |C0x C0y|
618//
619// This function finds the T value whose tangent angle is halfway between the tangents at T=0 and
620// T=1 (tan0 and tan1).
621static inline float find_midtangent(const Sk2f& tan0, const Sk2f& tan1,
622 float scale2, const Sk2f& C2,
623 float scale1, const Sk2f& C1,
624 float scale0, const Sk2f& C0) {
625 // Tangents point in the direction of increasing T, so tan0 and -tan1 both point toward the
626 // midtangent. 'n' will therefore bisect tan0 and -tan1, giving us the normal to the midtangent.
627 //
628 // n dot midtangent = 0
629 //
630 Sk2f n = normalize(tan0) - normalize(tan1);
631
632 // Find the T value at the midtangent. This is a simple quadratic equation:
633 //
634 // midtangent dot n = 0
635 //
636 // (|t^2 t 1| * C) dot n = 0
637 //
638 // |t^2 t 1| dot C*n = 0
639 //
640 // First find coeffs = C*n.
641 Sk4f C[2];
642 Sk2f::Store4(C, C2, C1, C0, 0);
643 Sk4f coeffs = C[0]*n[0] + C[1]*n[1];
644 if (1 != scale2 || 1 != scale1 || 1 != scale0) {
645 coeffs *= Sk4f(scale2, scale1, scale0, 0);
646 }
647
648 // Now solve the quadratic.
649 float a = coeffs[0], b = coeffs[1], c = coeffs[2];
650 float discr = b*b - 4*a*c;
651 if (discr < 0) {
652 return 0; // This will only happen if the curve is a line.
653 }
654
655 // The roots are q/a and c/q. Pick the one closer to T=.5.
656 float q = -.5f * (b + copysignf(std::sqrt(discr), b));
657 float r = .5f*q*a;
658 return std::abs(q*q - r) < std::abs(a*c - r) ? q/a : c/q;
659}
660
Chris Daltonb3a69592018-04-18 14:10:22 -0600661inline void GrCCGeometry::chopAndAppendCubicAtMidTangent(AppendCubicMode mode, const Sk2f& p0,
662 const Sk2f& p1, const Sk2f& p2,
663 const Sk2f& p3, const Sk2f& tan0,
664 const Sk2f& tan1,
665 int maxFutureSubdivisions) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600666 float midT = find_midtangent(tan0, tan1, 3, p3 + (p1 - p2)*3 - p0,
667 6, p0 - p1*2 + p2,
668 3, p1 - p0);
669 // Use positive logic since NaN fails comparisons. (However midT should not be NaN since we cull
670 // near-flat cubics in cubicTo().)
671 if (!(midT > 0 && midT < 1)) {
672 // The cubic is flat. Otherwise there would be a real midtangent inside T=0..1.
673 this->appendLine(p3);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600674 return;
675 }
676
Chris Daltonb3a69592018-04-18 14:10:22 -0600677 Sk2f p01, p02, pT, p11, p12;
678 chop_cubic(p0, p1, p2, p3, midT, &p01, &p02, &pT, &p11, &p12);
679 this->appendCubics(mode, p0, p01, p02, pT, maxFutureSubdivisions);
680 this->appendCubics(mode, pT, p11, p12, p3, maxFutureSubdivisions);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600681}
682
Chris Dalton9f2dab02018-04-18 14:07:03 -0600683void GrCCGeometry::conicTo(const SkPoint P[3], float w) {
684 SkASSERT(fBuildingContour);
685 SkASSERT(P[0] == fPoints.back());
686 Sk2f p0 = Sk2f::Load(P);
687 Sk2f p1 = Sk2f::Load(P+1);
688 Sk2f p2 = Sk2f::Load(P+2);
689
Chris Dalton9f2dab02018-04-18 14:07:03 -0600690 Sk2f tan0 = p1 - p0;
691 Sk2f tan1 = p2 - p1;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600692
693 if (!is_convex_curve_monotonic(p0, tan0, p2, tan1)) {
Chris Daltond8bae7d2018-04-19 13:13:25 -0600694 // The derivative of a conic has a cumbersome order-4 denominator. However, this isn't
695 // necessary if we are only interested in a vector in the same *direction* as a given
696 // tangent line. Since the denominator scales dx and dy uniformly, we can throw it out
697 // completely after evaluating the derivative with the standard quotient rule. This leaves
698 // us with a simpler quadratic function that we use to find the midtangent.
699 float midT = find_midtangent(tan0, tan1, 1, (w - 1) * (p2 - p0),
700 1, (p2 - p0) - 2*w*(p1 - p0),
701 1, w*(p1 - p0));
702 // Use positive logic since NaN fails comparisons. (However midT should not be NaN since we
703 // cull near-linear conics above. And while w=0 is flat, it's not a line and has valid
704 // midtangents.)
705 if (!(midT > 0 && midT < 1)) {
706 // The conic is flat. Otherwise there would be a real midtangent inside T=0..1.
707 this->appendLine(p2);
708 return;
709 }
710
Chris Dalton9f2dab02018-04-18 14:07:03 -0600711 // Chop the conic at midtangent to produce two monotonic segments.
Chris Daltond8bae7d2018-04-19 13:13:25 -0600712 Sk4f p3d0 = Sk4f(p0[0], p0[1], 1, 0);
713 Sk4f p3d1 = Sk4f(p1[0], p1[1], 1, 0) * w;
714 Sk4f p3d2 = Sk4f(p2[0], p2[1], 1, 0);
715 Sk4f midT4 = midT;
716
717 Sk4f p3d01 = lerp(p3d0, p3d1, midT4);
718 Sk4f p3d12 = lerp(p3d1, p3d2, midT4);
719 Sk4f p3d012 = lerp(p3d01, p3d12, midT4);
720
721 Sk2f midpoint = Sk2f(p3d012[0], p3d012[1]) / p3d012[2];
Chris Dalton9f2dab02018-04-18 14:07:03 -0600722 Sk2f ww = Sk2f(p3d01[2], p3d12[2]) * Sk2f(p3d012[2]).rsqrt();
Chris Daltond8bae7d2018-04-19 13:13:25 -0600723
Chris Dalton9f2dab02018-04-18 14:07:03 -0600724 this->appendMonotonicConic(p0, Sk2f(p3d01[0], p3d01[1]) / p3d01[2], midpoint, ww[0]);
725 this->appendMonotonicConic(midpoint, Sk2f(p3d12[0], p3d12[1]) / p3d12[2], p2, ww[1]);
726 return;
727 }
728
729 this->appendMonotonicConic(p0, p1, p2, w);
730}
731
732void GrCCGeometry::appendMonotonicConic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, float w) {
Chris Daltond8bae7d2018-04-19 13:13:25 -0600733 SkASSERT(w >= 0);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600734 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
735
Chris Daltond8bae7d2018-04-19 13:13:25 -0600736 Sk2f base = p2 - p0;
737 Sk2f baseAbs = base.abs();
738 float baseWidth = baseAbs[0] + baseAbs[1];
739
740 // Find the height of the curve. Max height always occurs at T=.5 for conics.
741 Sk2f d = (p1 - p0) * SkNx_shuffle<1,0>(base);
742 float h1 = std::abs(d[1] - d[0]); // Height of p1 above the base.
743 float ht = h1*w, hs = 1 + w; // Height of the conic = ht/hs.
744
745 if (ht < (baseWidth*hs) * kFlatnessThreshold) { // i.e. ht/hs < baseWidth * kFlatnessThreshold
746 // We are flat. (See rationale in are_collinear.)
747 this->appendLine(p2);
748 return;
749 }
750
751 if (w > 1 && h1*hs - ht < baseWidth*hs) { // i.e. w > 1 && h1 - ht/hs < baseWidth
752 // If we get within 1px of p1 when w > 1, we will pick up artifacts from the implicit
753 // function's reflection. Chop at max height (T=.5) and draw a triangle instead.
754 Sk2f p1w = p1*w;
755 Sk2f ab = p0 + p1w;
756 Sk2f bc = p1w + p2;
757 Sk2f highpoint = (ab + bc) / (2*(1 + w));
758 this->appendLine(highpoint);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600759 this->appendLine(p2);
760 return;
761 }
762
763 p1.store(&fPoints.push_back());
764 p2.store(&fPoints.push_back());
765 fConicWeights.push_back(w);
766 fVerbs.push_back(Verb::kMonotonicConicTo);
767 ++fCurrContourTallies.fConics;
768}
769
Chris Dalton383a2ef2018-01-08 17:21:41 -0500770GrCCGeometry::PrimitiveTallies GrCCGeometry::endContour() {
Chris Daltonc1e59632017-09-05 00:30:07 -0600771 SkASSERT(fBuildingContour);
772 SkASSERT(fVerbs.count() >= fCurrContourTallies.fTriangles);
773
774 // The fTriangles field currently contains this contour's starting verb index. We can now
775 // use it to calculate the size of the contour's fan.
776 int fanSize = fVerbs.count() - fCurrContourTallies.fTriangles;
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600777 if (fPoints.back() == fCurrAnchorPoint) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600778 --fanSize;
779 fVerbs.push_back(Verb::kEndClosedContour);
780 } else {
781 fVerbs.push_back(Verb::kEndOpenContour);
782 }
783
784 fCurrContourTallies.fTriangles = SkTMax(fanSize - 2, 0);
785
Chris Dalton383a2ef2018-01-08 17:21:41 -0500786 SkDEBUGCODE(fBuildingContour = false);
Chris Daltonc1e59632017-09-05 00:30:07 -0600787 return fCurrContourTallies;
Chris Dalton419a94d2017-08-28 10:24:22 -0600788}