blob: 59d040d73967e4a0c461dddefee80f56641e62e4 [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 Daltonb3a69592018-04-18 14:10:22 -0600238// 'chops' will be filled with 4 T values. The segments between T0..T1 and T2..T3 must be drawn with
239// 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 Daltonb3a69592018-04-18 14:10:22 -0600243static inline void find_chops_around_inflection_points(float padRadius, const Sk2f& t,
244 const Sk2f& s, const SkMatrix& CIT,
245 ExcludedTerm skipTerm,
246 SkSTArray<4, float>* chops) {
247 SkASSERT(chops->empty());
Chris Dalton7f578bf2017-09-05 16:46:48 -0600248 SkASSERT(padRadius >= 0);
Chris Daltonc1e59632017-09-05 00:30:07 -0600249
Chris Dalton7f578bf2017-09-05 16:46:48 -0600250 Sk2f Clx = s*s*s;
251 Sk2f Cly = (ExcludedTerm::kLinearTerm == skipTerm) ? s*s*t*-3 : s*t*t*3;
252
253 Sk2f Lx = CIT[0] * Clx + CIT[3] * Cly;
254 Sk2f Ly = CIT[1] * Clx + CIT[4] * Cly;
255
Chris Daltonb3a69592018-04-18 14:10:22 -0600256 Sk2f pad = padRadius * (Lx.abs() + Ly.abs());
257 pad = (pad * s >= 0).thenElse(pad, -pad);
258 pad = Sk2f(std::cbrt(pad[0]), std::cbrt(pad[1]));
Chris Dalton7f578bf2017-09-05 16:46:48 -0600259
Chris Daltonb3a69592018-04-18 14:10:22 -0600260 Sk2f leftT = (t - pad) / s;
261 Sk2f rightT = (t + pad) / s;
262 Sk2f::Store2(chops->push_back_n(4), leftT, rightT);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600263}
264
265static inline void swap_if_greater(float& a, float& b) {
266 if (a > b) {
267 std::swap(a, b);
268 }
269}
270
Chris Daltonb3a69592018-04-18 14:10:22 -0600271// Finds where to chop a non-loop around its intersection point. The resulting cubic segments will
272// be chopped such that a box of radius 'padRadius', centered at any point along the curve segment,
273// 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 -0600274//
Chris Daltonb3a69592018-04-18 14:10:22 -0600275// 'chops' will be filled with 0, 2, or 4 T values. The segments between T0..T1 and T2..T3 must be
276// drawn with quadratic splines instead of cubics.
Chris Dalton7f578bf2017-09-05 16:46:48 -0600277//
Chris Daltonb3a69592018-04-18 14:10:22 -0600278// A loop intersection falls at two different T values, so this method takes Sk2f and computes the
279// padding for both in SIMD.
280static inline void find_chops_around_loop_intersection(float padRadius, const Sk2f& t,
281 const Sk2f& s, const SkMatrix& CIT,
282 ExcludedTerm skipTerm,
283 SkSTArray<4, float>* chops) {
284 SkASSERT(chops->empty());
Chris Dalton7f578bf2017-09-05 16:46:48 -0600285 SkASSERT(padRadius >= 0);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600286
Chris Daltonb3a69592018-04-18 14:10:22 -0600287 Sk2f T2 = t/s;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600288 Sk2f T1 = SkNx_shuffle<1,0>(T2);
289 Sk2f Cl = (ExcludedTerm::kLinearTerm == skipTerm) ? T2*-2 - T1 : T2*T2 + T2*T1*2;
290 Sk2f Lx = Cl * CIT[3] + CIT[0];
291 Sk2f Ly = Cl * CIT[4] + CIT[1];
292
293 Sk2f bloat = Sk2f(+.5f * padRadius, -.5f * padRadius) * (Lx.abs() + Ly.abs());
294 Sk2f q = (1.f/3) * (T2 - T1);
295
296 Sk2f qqq = q*q*q;
297 Sk2f discr = qqq*bloat*2 + bloat*bloat;
298
299 float numRoots[2], D[2];
300 (discr < 0).thenElse(3, 1).store(numRoots);
301 (T2 - q).store(D);
302
303 // Values for calculating one root.
304 float R[2], QQ[2];
305 if ((discr >= 0).anyTrue()) {
306 Sk2f r = qqq + bloat;
307 Sk2f s = r.abs() + discr.sqrt();
308 (r > 0).thenElse(-s, s).store(R);
309 (q*q).store(QQ);
Chris Daltonc1e59632017-09-05 00:30:07 -0600310 }
311
Chris Dalton7f578bf2017-09-05 16:46:48 -0600312 // Values for calculating three roots.
313 float P[2], cosTheta3[2];
314 if ((discr < 0).anyTrue()) {
315 (q.abs() * -2).store(P);
316 ((q >= 0).thenElse(1, -1) + bloat / qqq.abs()).store(cosTheta3);
Chris Daltonc1e59632017-09-05 00:30:07 -0600317 }
318
Chris Dalton7f578bf2017-09-05 16:46:48 -0600319 for (int i = 0; i < 2; ++i) {
320 if (1 == numRoots[i]) {
Chris Daltonb3a69592018-04-18 14:10:22 -0600321 // When there is only one root, line L chops from root..1, line M chops from 0..root.
322 if (1 == i) {
323 chops->push_back(0);
324 }
Chris Dalton7f578bf2017-09-05 16:46:48 -0600325 float A = cbrtf(R[i]);
326 float B = A != 0 ? QQ[i]/A : 0;
Chris Daltonb3a69592018-04-18 14:10:22 -0600327 chops->push_back(A + B + D[i]);
328 if (0 == i) {
329 chops->push_back(1);
330 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600331 continue;
332 }
333
Chris Dalton7f578bf2017-09-05 16:46:48 -0600334 static constexpr float k2PiOver3 = 2 * SK_ScalarPI / 3;
335 float theta = std::acos(cosTheta3[i]) * (1.f/3);
Chris Daltonb3a69592018-04-18 14:10:22 -0600336 float roots[3] = {P[i] * std::cos(theta) + D[i],
337 P[i] * std::cos(theta + k2PiOver3) + D[i],
338 P[i] * std::cos(theta - k2PiOver3) + D[i]};
Chris Daltonc1e59632017-09-05 00:30:07 -0600339
Chris Dalton7f578bf2017-09-05 16:46:48 -0600340 // Sort the three roots.
Chris Daltonb3a69592018-04-18 14:10:22 -0600341 swap_if_greater(roots[0], roots[1]);
342 swap_if_greater(roots[1], roots[2]);
343 swap_if_greater(roots[0], roots[1]);
344
345 // Line L chops around the first 2 roots, line M chops around the second 2.
346 chops->push_back_n(2, &roots[i]);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600347 }
348}
349
Chris Daltonb3a69592018-04-18 14:10:22 -0600350void GrCCGeometry::cubicTo(const SkPoint P[4], float inflectPad, float loopIntersectPad) {
351 SkASSERT(fBuildingContour);
352 SkASSERT(P[0] == fPoints.back());
Chris Dalton29011a22017-09-28 12:08:33 -0600353
Chris Daltonb3a69592018-04-18 14:10:22 -0600354 // Don't crunch on the curve or inflate geometry if it is nearly flat (or just very small).
355 // Flat curves can break the math below.
356 if (are_collinear(P)) {
357 this->lineTo(P[3]);
358 return;
359 }
Chris Dalton29011a22017-09-28 12:08:33 -0600360
Chris Daltonb3a69592018-04-18 14:10:22 -0600361 Sk2f p0 = Sk2f::Load(P);
362 Sk2f p1 = Sk2f::Load(P+1);
363 Sk2f p2 = Sk2f::Load(P+2);
364 Sk2f p3 = Sk2f::Load(P+3);
365
366 // Also detect near-quadratics ahead of time.
367 Sk2f tan0, tan1, c;
368 get_cubic_tangents(p0, p1, p2, p3, &tan0, &tan1);
369 if (is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, &c)) {
370 this->appendQuadratics(p0, c, p3);
371 return;
372 }
373
374 double tt[2], ss[2], D[4];
375 fCurrCubicType = SkClassifyCubic(P, tt, ss, D);
376 SkASSERT(!SkCubicIsDegenerate(fCurrCubicType));
377 Sk2f t = Sk2f(static_cast<float>(tt[0]), static_cast<float>(tt[1]));
378 Sk2f s = Sk2f(static_cast<float>(ss[0]), static_cast<float>(ss[1]));
379
380 SkMatrix CIT;
381 ExcludedTerm skipTerm = GrPathUtils::calcCubicInverseTransposePowerBasisMatrix(P, &CIT);
382 SkASSERT(ExcludedTerm::kNonInvertible != skipTerm); // Should have been caught above.
383 SkASSERT(0 == CIT[6]);
384 SkASSERT(0 == CIT[7]);
385 SkASSERT(1 == CIT[8]);
386
387 SkSTArray<4, float> chops;
388 if (SkCubicType::kLoop != fCurrCubicType) {
389 find_chops_around_inflection_points(inflectPad, t, s, CIT, skipTerm, &chops);
390 } else {
391 find_chops_around_loop_intersection(loopIntersectPad, t, s, CIT, skipTerm, &chops);
392 }
393 if (chops[1] >= chops[2]) {
394 // This just the means the KLM roots are so close that their paddings overlap. We will
395 // approximate the entire middle section, but still have it chopped midway. For loops this
396 // chop guarantees the append code only sees convex segments. Otherwise, it means we are (at
397 // least almost) a cusp and the chop makes sure we get a sharp point.
398 Sk2f ts = t * SkNx_shuffle<1,0>(s);
399 chops[1] = chops[2] = (ts[0] + ts[1]) / (2*s[0]*s[1]);
400 }
401
402#ifdef SK_DEBUG
403 for (int i = 1; i < chops.count(); ++i) {
404 SkASSERT(chops[i] >= chops[i - 1]);
405 }
406#endif
407 this->appendCubics(AppendCubicMode::kLiteral, p0, p1, p2, p3, chops.begin(), chops.count());
Chris Dalton29011a22017-09-28 12:08:33 -0600408}
409
Chris Daltonb3a69592018-04-18 14:10:22 -0600410static inline void chop_cubic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, const Sk2f& p3,
411 float T, Sk2f* ab, Sk2f* abc, Sk2f* abcd, Sk2f* bcd, Sk2f* cd) {
412 Sk2f TT = T;
413 *ab = lerp(p0, p1, TT);
414 Sk2f bc = lerp(p1, p2, TT);
415 *cd = lerp(p2, p3, TT);
416 *abc = lerp(*ab, bc, TT);
417 *bcd = lerp(bc, *cd, TT);
418 *abcd = lerp(*abc, *bcd, TT);
419}
Chris Dalton29011a22017-09-28 12:08:33 -0600420
Chris Daltonb3a69592018-04-18 14:10:22 -0600421void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f& p1,
422 const Sk2f& p2, const Sk2f& p3, const float chops[], int numChops,
423 float localT0, float localT1) {
424 if (numChops) {
425 SkASSERT(numChops > 0);
426 int midChopIdx = numChops/2;
427 float T = chops[midChopIdx];
428 // Chops alternate between literal and approximate mode.
429 AppendCubicMode rightMode = (AppendCubicMode)((bool)mode ^ (midChopIdx & 1) ^ 1);
Chris Dalton29011a22017-09-28 12:08:33 -0600430
Chris Daltonb3a69592018-04-18 14:10:22 -0600431 if (T <= localT0) {
432 // T is outside 0..1. Append the right side only.
433 this->appendCubics(rightMode, p0, p1, p2, p3, &chops[midChopIdx + 1],
434 numChops - midChopIdx - 1, localT0, localT1);
435 return;
436 }
437
438 if (T >= localT1) {
439 // T is outside 0..1. Append the left side only.
440 this->appendCubics(mode, p0, p1, p2, p3, chops, midChopIdx, localT0, localT1);
441 return;
442 }
443
444 float localT = (T - localT0) / (localT1 - localT0);
445 Sk2f p01, p02, pT, p11, p12;
446 chop_cubic(p0, p1, p2, p3, localT, &p01, &p02, &pT, &p11, &p12);
447 this->appendCubics(mode, p0, p01, p02, pT, chops, midChopIdx, localT0, T);
448 this->appendCubics(rightMode, pT, p11, p12, p3, &chops[midChopIdx + 1],
449 numChops - midChopIdx - 1, T, localT1);
450 return;
451 }
452
453 this->appendCubics(mode, p0, p1, p2, p3);
454}
455
456void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f& p1,
457 const Sk2f& p2, const Sk2f& p3, int maxSubdivisions) {
458 if ((p0 == p3).allTrue()) {
459 return;
460 }
461
462 if (SkCubicType::kLoop != fCurrCubicType) {
463 // Serpentines and cusps are always monotonic after chopping around inflection points.
464 SkASSERT(!SkCubicIsDegenerate(fCurrCubicType));
465
466 if (AppendCubicMode::kApproximate == mode) {
467 // This section passes through an inflection point, so we can get away with a flat line.
468 // This can cause some curves to feel slightly more flat when inspected rigorously back
469 // and forth against another renderer, but for now this seems acceptable given the
470 // simplicity.
471 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
472 this->appendLine(p3);
473 return;
474 }
475 } else {
476 Sk2f tan0, tan1;
477 get_cubic_tangents(p0, p1, p2, p3, &tan0, &tan1);
478
479 if (maxSubdivisions && !is_convex_curve_monotonic(p0, tan0, p3, tan1)) {
480 this->chopAndAppendCubicAtMidTangent(mode, p0, p1, p2, p3, tan0, tan1,
481 maxSubdivisions - 1);
482 return;
483 }
484
485 if (AppendCubicMode::kApproximate == mode) {
486 Sk2f c;
487 if (!is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, &c) && maxSubdivisions) {
488 this->chopAndAppendCubicAtMidTangent(mode, p0, p1, p2, p3, tan0, tan1,
489 maxSubdivisions - 1);
490 return;
491 }
492
493 this->appendMonotonicQuadratic(p0, c, p3);
494 return;
495 }
496 }
497
498 // Don't send curves to the GPU if we know they are nearly flat (or just very small).
499 // Since the cubic segment is known to be convex at this point, our flatness check is simple.
500 if (are_collinear(p0, (p1 + p2) * .5f, p3)) {
501 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
502 this->appendLine(p3);
503 return;
504 }
505
506 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
507 p1.store(&fPoints.push_back());
508 p2.store(&fPoints.push_back());
509 p3.store(&fPoints.push_back());
510 fVerbs.push_back(Verb::kMonotonicCubicTo);
511 ++fCurrContourTallies.fCubics;
Chris Dalton29011a22017-09-28 12:08:33 -0600512}
513
Chris Dalton9f2dab02018-04-18 14:07:03 -0600514// Given a convex curve segment with the following order-2 tangent function:
515//
516// |C2x C2y|
517// tan = some_scale * |dx/dt dy/dt| = |t^2 t 1| * |C1x C1y|
518// |C0x C0y|
519//
520// This function finds the T value whose tangent angle is halfway between the tangents at T=0 and
521// T=1 (tan0 and tan1).
522static inline float find_midtangent(const Sk2f& tan0, const Sk2f& tan1,
523 float scale2, const Sk2f& C2,
524 float scale1, const Sk2f& C1,
525 float scale0, const Sk2f& C0) {
526 // Tangents point in the direction of increasing T, so tan0 and -tan1 both point toward the
527 // midtangent. 'n' will therefore bisect tan0 and -tan1, giving us the normal to the midtangent.
528 //
529 // n dot midtangent = 0
530 //
531 Sk2f n = normalize(tan0) - normalize(tan1);
532
533 // Find the T value at the midtangent. This is a simple quadratic equation:
534 //
535 // midtangent dot n = 0
536 //
537 // (|t^2 t 1| * C) dot n = 0
538 //
539 // |t^2 t 1| dot C*n = 0
540 //
541 // First find coeffs = C*n.
542 Sk4f C[2];
543 Sk2f::Store4(C, C2, C1, C0, 0);
544 Sk4f coeffs = C[0]*n[0] + C[1]*n[1];
545 if (1 != scale2 || 1 != scale1 || 1 != scale0) {
546 coeffs *= Sk4f(scale2, scale1, scale0, 0);
547 }
548
549 // Now solve the quadratic.
550 float a = coeffs[0], b = coeffs[1], c = coeffs[2];
551 float discr = b*b - 4*a*c;
552 if (discr < 0) {
553 return 0; // This will only happen if the curve is a line.
554 }
555
556 // The roots are q/a and c/q. Pick the one closer to T=.5.
557 float q = -.5f * (b + copysignf(std::sqrt(discr), b));
558 float r = .5f*q*a;
559 return std::abs(q*q - r) < std::abs(a*c - r) ? q/a : c/q;
560}
561
Chris Daltonb3a69592018-04-18 14:10:22 -0600562inline void GrCCGeometry::chopAndAppendCubicAtMidTangent(AppendCubicMode mode, const Sk2f& p0,
563 const Sk2f& p1, const Sk2f& p2,
564 const Sk2f& p3, const Sk2f& tan0,
565 const Sk2f& tan1,
566 int maxFutureSubdivisions) {
Chris Dalton9f2dab02018-04-18 14:07:03 -0600567 float midT = find_midtangent(tan0, tan1, 3, p3 + (p1 - p2)*3 - p0,
568 6, p0 - p1*2 + p2,
569 3, p1 - p0);
570 // Use positive logic since NaN fails comparisons. (However midT should not be NaN since we cull
571 // near-flat cubics in cubicTo().)
572 if (!(midT > 0 && midT < 1)) {
573 // The cubic is flat. Otherwise there would be a real midtangent inside T=0..1.
574 this->appendLine(p3);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600575 return;
576 }
577
Chris Daltonb3a69592018-04-18 14:10:22 -0600578 Sk2f p01, p02, pT, p11, p12;
579 chop_cubic(p0, p1, p2, p3, midT, &p01, &p02, &pT, &p11, &p12);
580 this->appendCubics(mode, p0, p01, p02, pT, maxFutureSubdivisions);
581 this->appendCubics(mode, pT, p11, p12, p3, maxFutureSubdivisions);
Chris Dalton7f578bf2017-09-05 16:46:48 -0600582}
583
Chris Dalton9f2dab02018-04-18 14:07:03 -0600584void GrCCGeometry::conicTo(const SkPoint P[3], float w) {
585 SkASSERT(fBuildingContour);
586 SkASSERT(P[0] == fPoints.back());
587 Sk2f p0 = Sk2f::Load(P);
588 Sk2f p1 = Sk2f::Load(P+1);
589 Sk2f p2 = Sk2f::Load(P+2);
590
Chris Dalton9f2dab02018-04-18 14:07:03 -0600591 Sk2f tan0 = p1 - p0;
592 Sk2f tan1 = p2 - p1;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600593
594 if (!is_convex_curve_monotonic(p0, tan0, p2, tan1)) {
Chris Daltond8bae7d2018-04-19 13:13:25 -0600595 // The derivative of a conic has a cumbersome order-4 denominator. However, this isn't
596 // necessary if we are only interested in a vector in the same *direction* as a given
597 // tangent line. Since the denominator scales dx and dy uniformly, we can throw it out
598 // completely after evaluating the derivative with the standard quotient rule. This leaves
599 // us with a simpler quadratic function that we use to find the midtangent.
600 float midT = find_midtangent(tan0, tan1, 1, (w - 1) * (p2 - p0),
601 1, (p2 - p0) - 2*w*(p1 - p0),
602 1, w*(p1 - p0));
603 // Use positive logic since NaN fails comparisons. (However midT should not be NaN since we
604 // cull near-linear conics above. And while w=0 is flat, it's not a line and has valid
605 // midtangents.)
606 if (!(midT > 0 && midT < 1)) {
607 // The conic is flat. Otherwise there would be a real midtangent inside T=0..1.
608 this->appendLine(p2);
609 return;
610 }
611
Chris Dalton9f2dab02018-04-18 14:07:03 -0600612 // Chop the conic at midtangent to produce two monotonic segments.
Chris Daltond8bae7d2018-04-19 13:13:25 -0600613 Sk4f p3d0 = Sk4f(p0[0], p0[1], 1, 0);
614 Sk4f p3d1 = Sk4f(p1[0], p1[1], 1, 0) * w;
615 Sk4f p3d2 = Sk4f(p2[0], p2[1], 1, 0);
616 Sk4f midT4 = midT;
617
618 Sk4f p3d01 = lerp(p3d0, p3d1, midT4);
619 Sk4f p3d12 = lerp(p3d1, p3d2, midT4);
620 Sk4f p3d012 = lerp(p3d01, p3d12, midT4);
621
622 Sk2f midpoint = Sk2f(p3d012[0], p3d012[1]) / p3d012[2];
Chris Dalton9f2dab02018-04-18 14:07:03 -0600623 Sk2f ww = Sk2f(p3d01[2], p3d12[2]) * Sk2f(p3d012[2]).rsqrt();
Chris Daltond8bae7d2018-04-19 13:13:25 -0600624
Chris Dalton9f2dab02018-04-18 14:07:03 -0600625 this->appendMonotonicConic(p0, Sk2f(p3d01[0], p3d01[1]) / p3d01[2], midpoint, ww[0]);
626 this->appendMonotonicConic(midpoint, Sk2f(p3d12[0], p3d12[1]) / p3d12[2], p2, ww[1]);
627 return;
628 }
629
630 this->appendMonotonicConic(p0, p1, p2, w);
631}
632
633void GrCCGeometry::appendMonotonicConic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, float w) {
Chris Daltond8bae7d2018-04-19 13:13:25 -0600634 SkASSERT(w >= 0);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600635 SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1]));
636
Chris Daltond8bae7d2018-04-19 13:13:25 -0600637 Sk2f base = p2 - p0;
638 Sk2f baseAbs = base.abs();
639 float baseWidth = baseAbs[0] + baseAbs[1];
640
641 // Find the height of the curve. Max height always occurs at T=.5 for conics.
642 Sk2f d = (p1 - p0) * SkNx_shuffle<1,0>(base);
643 float h1 = std::abs(d[1] - d[0]); // Height of p1 above the base.
644 float ht = h1*w, hs = 1 + w; // Height of the conic = ht/hs.
645
646 if (ht < (baseWidth*hs) * kFlatnessThreshold) { // i.e. ht/hs < baseWidth * kFlatnessThreshold
647 // We are flat. (See rationale in are_collinear.)
648 this->appendLine(p2);
649 return;
650 }
651
652 if (w > 1 && h1*hs - ht < baseWidth*hs) { // i.e. w > 1 && h1 - ht/hs < baseWidth
653 // If we get within 1px of p1 when w > 1, we will pick up artifacts from the implicit
654 // function's reflection. Chop at max height (T=.5) and draw a triangle instead.
655 Sk2f p1w = p1*w;
656 Sk2f ab = p0 + p1w;
657 Sk2f bc = p1w + p2;
658 Sk2f highpoint = (ab + bc) / (2*(1 + w));
659 this->appendLine(highpoint);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600660 this->appendLine(p2);
661 return;
662 }
663
664 p1.store(&fPoints.push_back());
665 p2.store(&fPoints.push_back());
666 fConicWeights.push_back(w);
667 fVerbs.push_back(Verb::kMonotonicConicTo);
668 ++fCurrContourTallies.fConics;
669}
670
Chris Dalton383a2ef2018-01-08 17:21:41 -0500671GrCCGeometry::PrimitiveTallies GrCCGeometry::endContour() {
Chris Daltonc1e59632017-09-05 00:30:07 -0600672 SkASSERT(fBuildingContour);
673 SkASSERT(fVerbs.count() >= fCurrContourTallies.fTriangles);
674
675 // The fTriangles field currently contains this contour's starting verb index. We can now
676 // use it to calculate the size of the contour's fan.
677 int fanSize = fVerbs.count() - fCurrContourTallies.fTriangles;
Chris Dalton7ca3b7b2018-04-10 00:21:19 -0600678 if (fPoints.back() == fCurrAnchorPoint) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600679 --fanSize;
680 fVerbs.push_back(Verb::kEndClosedContour);
681 } else {
682 fVerbs.push_back(Verb::kEndOpenContour);
683 }
684
685 fCurrContourTallies.fTriangles = SkTMax(fanSize - 2, 0);
686
Chris Dalton383a2ef2018-01-08 17:21:41 -0500687 SkDEBUGCODE(fBuildingContour = false);
Chris Daltonc1e59632017-09-05 00:30:07 -0600688 return fCurrContourTallies;
Chris Dalton419a94d2017-08-28 10:24:22 -0600689}