Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 1 | /* |
| 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 Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 8 | #include "GrCCGeometry.h" |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 9 | |
| 10 | #include "GrTypes.h" |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 11 | #include "GrPathUtils.h" |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 12 | #include <algorithm> |
| 13 | #include <cmath> |
| 14 | #include <cstdlib> |
| 15 | |
| 16 | // We convert between SkPoint and Sk2f freely throughout this file. |
| 17 | GR_STATIC_ASSERT(SK_SCALAR_IS_FLOAT); |
| 18 | GR_STATIC_ASSERT(2 * sizeof(float) == sizeof(SkPoint)); |
| 19 | GR_STATIC_ASSERT(0 == offsetof(SkPoint, fX)); |
| 20 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 21 | void GrCCGeometry::beginPath() { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 22 | SkASSERT(!fBuildingContour); |
| 23 | fVerbs.push_back(Verb::kBeginPath); |
| 24 | } |
| 25 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 26 | void GrCCGeometry::beginContour(const SkPoint& pt) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 27 | SkASSERT(!fBuildingContour); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 28 | // Store the current verb count in the fTriangles field for now. When we close the contour we |
| 29 | // will use this value to calculate the actual number of triangles in its fan. |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 30 | fCurrContourTallies = {fVerbs.count(), 0, 0, 0}; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 31 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 32 | fPoints.push_back(pt); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 33 | fVerbs.push_back(Verb::kBeginContour); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 34 | fCurrAnchorPoint = pt; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 35 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 36 | SkDEBUGCODE(fBuildingContour = true); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 37 | } |
| 38 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 39 | void GrCCGeometry::lineTo(const SkPoint& pt) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 40 | SkASSERT(fBuildingContour); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 41 | fPoints.push_back(pt); |
| 42 | fVerbs.push_back(Verb::kLineTo); |
| 43 | } |
| 44 | |
| 45 | void GrCCGeometry::appendLine(const Sk2f& endpt) { |
| 46 | endpt.store(&fPoints.push_back()); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 47 | fVerbs.push_back(Verb::kLineTo); |
| 48 | } |
| 49 | |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 50 | static inline Sk2f normalize(const Sk2f& n) { |
| 51 | Sk2f nn = n*n; |
| 52 | return n * (nn + SkNx_shuffle<1,0>(nn)).rsqrt(); |
| 53 | } |
| 54 | |
| 55 | static inline float dot(const Sk2f& a, const Sk2f& b) { |
| 56 | float product[2]; |
| 57 | (a * b).store(product); |
| 58 | return product[0] + product[1]; |
| 59 | } |
| 60 | |
Chris Dalton | b0601a4 | 2018-04-10 00:23:45 -0600 | [diff] [blame^] | 61 | static inline bool are_collinear(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, |
| 62 | float tolerance = 1/16.f) { // 1/16 of a pixel. |
| 63 | Sk2f l = p2 - p0; // Line from p0 -> p2. |
Chris Dalton | 900cd05 | 2017-09-07 10:36:51 -0600 | [diff] [blame] | 64 | |
Chris Dalton | b0601a4 | 2018-04-10 00:23:45 -0600 | [diff] [blame^] | 65 | // lwidth = Manhattan width of l. |
| 66 | Sk2f labs = l.abs(); |
| 67 | float lwidth = labs[0] + labs[1]; |
Chris Dalton | 900cd05 | 2017-09-07 10:36:51 -0600 | [diff] [blame] | 68 | |
Chris Dalton | b0601a4 | 2018-04-10 00:23:45 -0600 | [diff] [blame^] | 69 | // d = |p1 - p0| dot | l.y| |
| 70 | // |-l.x| = distance from p1 to l. |
| 71 | Sk2f dd = (p1 - p0) * SkNx_shuffle<1,0>(l); |
| 72 | float d = dd[0] - dd[1]; |
Chris Dalton | 900cd05 | 2017-09-07 10:36:51 -0600 | [diff] [blame] | 73 | |
Chris Dalton | b0601a4 | 2018-04-10 00:23:45 -0600 | [diff] [blame^] | 74 | // We are collinear if a box with radius "tolerance", centered on p1, touches the line l. |
| 75 | // To decide this, we check if the distance from p1 to the line is less than the distance from |
| 76 | // p1 to the far corner of this imaginary box, along that same normal vector. |
| 77 | // The far corner of the box can be found at "p1 + sign(n) * tolerance", where n is normal to l: |
| 78 | // |
| 79 | // abs(dot(p1 - p0, n)) <= dot(sign(n) * tolerance, n) |
| 80 | // |
| 81 | // Which reduces to: |
| 82 | // |
| 83 | // abs(d) <= (n.x * sign(n.x) + n.y * sign(n.y)) * tolerance |
| 84 | // abs(d) <= (abs(n.x) + abs(n.y)) * tolerance |
| 85 | // |
| 86 | // Use "<=" in case l == 0. |
| 87 | return std::abs(d) <= lwidth * tolerance; |
| 88 | } |
| 89 | |
| 90 | static inline bool are_collinear(const SkPoint P[4], float tolerance = 1/16.f) { // 1/16 of a pixel. |
| 91 | Sk4f Px, Py; // |Px Py| |p0 - p3| |
| 92 | Sk4f::Load2(P, &Px, &Py); // |. . | = |p1 - p3| |
| 93 | Px -= Px[3]; // |. . | |p2 - p3| |
| 94 | Py -= Py[3]; // |. . | | 0 | |
| 95 | |
| 96 | // Find [lx, ly] = the line from p3 to the furthest-away point from p3. |
| 97 | Sk4f Pwidth = Px.abs() + Py.abs(); // Pwidth = Manhattan width of each point. |
| 98 | int lidx = Pwidth[0] > Pwidth[1] ? 0 : 1; |
| 99 | lidx = Pwidth[lidx] > Pwidth[2] ? lidx : 2; |
| 100 | float lx = Px[lidx], ly = Py[lidx]; |
| 101 | float lwidth = Pwidth[lidx]; // lwidth = Manhattan width of [lx, ly]. |
| 102 | |
| 103 | // |Px Py| |
| 104 | // d = |. . | * | ly| = distances from each point to l (two of the distances will be zero). |
| 105 | // |. . | |-lx| |
| 106 | // |. . | |
| 107 | Sk4f d = Px*ly - Py*lx; |
| 108 | |
| 109 | // We are collinear if boxes with radius "tolerance", centered on all 4 points all touch line l. |
| 110 | // (See the rationale for this formula in the above, 3-point version of this function.) |
| 111 | // Use "<=" in case l == 0. |
| 112 | return (d.abs() <= lwidth * tolerance).allTrue(); |
Chris Dalton | 900cd05 | 2017-09-07 10:36:51 -0600 | [diff] [blame] | 113 | } |
| 114 | |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 115 | // Returns whether the (convex) curve segment is monotonic with respect to [endPt - startPt]. |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 116 | static inline bool is_convex_curve_monotonic(const Sk2f& startPt, const Sk2f& tan0, |
| 117 | const Sk2f& endPt, const Sk2f& tan1) { |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 118 | Sk2f v = endPt - startPt; |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 119 | float dot0 = dot(tan0, v); |
| 120 | float dot1 = dot(tan1, v); |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 121 | |
| 122 | // A small, negative tolerance handles floating-point error in the case when one tangent |
| 123 | // approaches 0 length, meaning the (convex) curve segment is effectively a flat line. |
| 124 | float tolerance = -std::max(std::abs(dot0), std::abs(dot1)) * SK_ScalarNearlyZero; |
| 125 | return dot0 >= tolerance && dot1 >= tolerance; |
| 126 | } |
| 127 | |
| 128 | static inline Sk2f lerp(const Sk2f& a, const Sk2f& b, const Sk2f& t) { |
| 129 | return SkNx_fma(t, b - a, a); |
| 130 | } |
| 131 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 132 | void GrCCGeometry::quadraticTo(const SkPoint P[3]) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 133 | SkASSERT(fBuildingContour); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 134 | SkASSERT(P[0] == fPoints.back()); |
| 135 | Sk2f p0 = Sk2f::Load(P); |
| 136 | Sk2f p1 = Sk2f::Load(P+1); |
| 137 | Sk2f p2 = Sk2f::Load(P+2); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 138 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 139 | // Don't crunch on the curve if it is nearly flat (or just very small). Flat curves can break |
| 140 | // The monotonic chopping math. |
| 141 | if (are_collinear(p0, p1, p2)) { |
| 142 | this->appendLine(p2); |
| 143 | return; |
| 144 | } |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 145 | |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 146 | this->appendMonotonicQuadratics(p0, p1, p2); |
| 147 | } |
| 148 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 149 | inline void GrCCGeometry::appendMonotonicQuadratics(const Sk2f& p0, const Sk2f& p1, |
| 150 | const Sk2f& p2) { |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 151 | Sk2f tan0 = p1 - p0; |
| 152 | Sk2f tan1 = p2 - p1; |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 153 | |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 154 | // This should almost always be this case for well-behaved curves in the real world. |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 155 | if (is_convex_curve_monotonic(p0, tan0, p2, tan1)) { |
| 156 | this->appendSingleMonotonicQuadratic(p0, p1, p2); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 157 | return; |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Chop the curve into two segments with equal curvature. To do this we find the T value whose |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 161 | // tangent angle is halfway between tan0 and tan1. |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 162 | Sk2f n = normalize(tan0) - normalize(tan1); |
| 163 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 164 | // The midtangent can be found where (dQ(t) dot n) = 0: |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 165 | // |
| 166 | // 0 = (dQ(t) dot n) = | 2*t 1 | * | p0 - 2*p1 + p2 | * | n | |
| 167 | // | -2*p0 + 2*p1 | | . | |
| 168 | // |
| 169 | // = | 2*t 1 | * | tan1 - tan0 | * | n | |
| 170 | // | 2*tan0 | | . | |
| 171 | // |
| 172 | // = 2*t * ((tan1 - tan0) dot n) + (2*tan0 dot n) |
| 173 | // |
| 174 | // t = (tan0 dot n) / ((tan0 - tan1) dot n) |
| 175 | Sk2f dQ1n = (tan0 - tan1) * n; |
| 176 | Sk2f dQ0n = tan0 * n; |
| 177 | Sk2f t = (dQ0n + SkNx_shuffle<1,0>(dQ0n)) / (dQ1n + SkNx_shuffle<1,0>(dQ1n)); |
| 178 | t = Sk2f::Min(Sk2f::Max(t, 0), 1); // Clamp for FP error. |
| 179 | |
| 180 | Sk2f p01 = SkNx_fma(t, tan0, p0); |
| 181 | Sk2f p12 = SkNx_fma(t, tan1, p1); |
| 182 | Sk2f p012 = lerp(p01, p12, t); |
| 183 | |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 184 | this->appendSingleMonotonicQuadratic(p0, p01, p012); |
| 185 | this->appendSingleMonotonicQuadratic(p012, p12, p2); |
| 186 | } |
| 187 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 188 | inline void GrCCGeometry::appendSingleMonotonicQuadratic(const Sk2f& p0, const Sk2f& p1, |
| 189 | const Sk2f& p2) { |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 190 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
| 191 | |
| 192 | // Don't send curves to the GPU if we know they are nearly flat (or just very small). |
| 193 | if (are_collinear(p0, p1, p2)) { |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 194 | this->appendLine(p2); |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | |
| 198 | p1.store(&fPoints.push_back()); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 199 | p2.store(&fPoints.push_back()); |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 200 | fVerbs.push_back(Verb::kMonotonicQuadraticTo); |
| 201 | ++fCurrContourTallies.fQuadratics; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 202 | } |
| 203 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 204 | using ExcludedTerm = GrPathUtils::ExcludedTerm; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 205 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 206 | // Calculates the padding to apply around inflection points, in homogeneous parametric coordinates. |
| 207 | // |
| 208 | // More specifically, if the inflection point lies at C(t/s), then C((t +/- returnValue) / s) will |
| 209 | // be the two points on the curve at which a square box with radius "padRadius" will have a corner |
| 210 | // that touches the inflection point's tangent line. |
| 211 | // |
| 212 | // A serpentine cubic has two inflection points, so this method takes Sk2f and computes the padding |
| 213 | // for both in SIMD. |
| 214 | static inline Sk2f calc_inflect_homogeneous_padding(float padRadius, const Sk2f& t, const Sk2f& s, |
| 215 | const SkMatrix& CIT, ExcludedTerm skipTerm) { |
| 216 | SkASSERT(padRadius >= 0); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 217 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 218 | Sk2f Clx = s*s*s; |
| 219 | Sk2f Cly = (ExcludedTerm::kLinearTerm == skipTerm) ? s*s*t*-3 : s*t*t*3; |
| 220 | |
| 221 | Sk2f Lx = CIT[0] * Clx + CIT[3] * Cly; |
| 222 | Sk2f Ly = CIT[1] * Clx + CIT[4] * Cly; |
| 223 | |
| 224 | float ret[2]; |
| 225 | Sk2f bloat = padRadius * (Lx.abs() + Ly.abs()); |
| 226 | (bloat * s >= 0).thenElse(bloat, -bloat).store(ret); |
| 227 | |
| 228 | ret[0] = cbrtf(ret[0]); |
| 229 | ret[1] = cbrtf(ret[1]); |
| 230 | return Sk2f::Load(ret); |
| 231 | } |
| 232 | |
| 233 | static inline void swap_if_greater(float& a, float& b) { |
| 234 | if (a > b) { |
| 235 | std::swap(a, b); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // Calculates all parameter values for a loop at which points a square box with radius "padRadius" |
| 240 | // will have a corner that touches a tangent line from the intersection. |
| 241 | // |
| 242 | // T2 must contain the lesser parameter value of the loop intersection in its first component, and |
| 243 | // the greater in its second. |
| 244 | // |
| 245 | // roots[0] will be filled with 1 or 3 sorted parameter values, representing the padding points |
| 246 | // around the first tangent. roots[1] will be filled with the padding points for the second tangent. |
| 247 | static inline void calc_loop_intersect_padding_pts(float padRadius, const Sk2f& T2, |
| 248 | const SkMatrix& CIT, ExcludedTerm skipTerm, |
| 249 | SkSTArray<3, float, true> roots[2]) { |
| 250 | SkASSERT(padRadius >= 0); |
| 251 | SkASSERT(T2[0] <= T2[1]); |
| 252 | SkASSERT(roots[0].empty()); |
| 253 | SkASSERT(roots[1].empty()); |
| 254 | |
| 255 | Sk2f T1 = SkNx_shuffle<1,0>(T2); |
| 256 | Sk2f Cl = (ExcludedTerm::kLinearTerm == skipTerm) ? T2*-2 - T1 : T2*T2 + T2*T1*2; |
| 257 | Sk2f Lx = Cl * CIT[3] + CIT[0]; |
| 258 | Sk2f Ly = Cl * CIT[4] + CIT[1]; |
| 259 | |
| 260 | Sk2f bloat = Sk2f(+.5f * padRadius, -.5f * padRadius) * (Lx.abs() + Ly.abs()); |
| 261 | Sk2f q = (1.f/3) * (T2 - T1); |
| 262 | |
| 263 | Sk2f qqq = q*q*q; |
| 264 | Sk2f discr = qqq*bloat*2 + bloat*bloat; |
| 265 | |
| 266 | float numRoots[2], D[2]; |
| 267 | (discr < 0).thenElse(3, 1).store(numRoots); |
| 268 | (T2 - q).store(D); |
| 269 | |
| 270 | // Values for calculating one root. |
| 271 | float R[2], QQ[2]; |
| 272 | if ((discr >= 0).anyTrue()) { |
| 273 | Sk2f r = qqq + bloat; |
| 274 | Sk2f s = r.abs() + discr.sqrt(); |
| 275 | (r > 0).thenElse(-s, s).store(R); |
| 276 | (q*q).store(QQ); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 277 | } |
| 278 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 279 | // Values for calculating three roots. |
| 280 | float P[2], cosTheta3[2]; |
| 281 | if ((discr < 0).anyTrue()) { |
| 282 | (q.abs() * -2).store(P); |
| 283 | ((q >= 0).thenElse(1, -1) + bloat / qqq.abs()).store(cosTheta3); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 284 | } |
| 285 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 286 | for (int i = 0; i < 2; ++i) { |
| 287 | if (1 == numRoots[i]) { |
| 288 | float A = cbrtf(R[i]); |
| 289 | float B = A != 0 ? QQ[i]/A : 0; |
| 290 | roots[i].push_back(A + B + D[i]); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 291 | continue; |
| 292 | } |
| 293 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 294 | static constexpr float k2PiOver3 = 2 * SK_ScalarPI / 3; |
| 295 | float theta = std::acos(cosTheta3[i]) * (1.f/3); |
| 296 | roots[i].push_back(P[i] * std::cos(theta) + D[i]); |
| 297 | roots[i].push_back(P[i] * std::cos(theta + k2PiOver3) + D[i]); |
| 298 | roots[i].push_back(P[i] * std::cos(theta - k2PiOver3) + D[i]); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 299 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 300 | // Sort the three roots. |
| 301 | swap_if_greater(roots[i][0], roots[i][1]); |
| 302 | swap_if_greater(roots[i][1], roots[i][2]); |
| 303 | swap_if_greater(roots[i][0], roots[i][1]); |
| 304 | } |
| 305 | } |
| 306 | |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 307 | static inline Sk2f first_unless_nearly_zero(const Sk2f& a, const Sk2f& b) { |
| 308 | Sk2f aa = a*a; |
| 309 | aa += SkNx_shuffle<1,0>(aa); |
| 310 | SkASSERT(aa[0] == aa[1]); |
| 311 | |
| 312 | Sk2f bb = b*b; |
| 313 | bb += SkNx_shuffle<1,0>(bb); |
| 314 | SkASSERT(bb[0] == bb[1]); |
| 315 | |
| 316 | return (aa > bb * SK_ScalarNearlyZero).thenElse(a, b); |
| 317 | } |
| 318 | |
| 319 | static inline bool is_cubic_nearly_quadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 320 | const Sk2f& p3, Sk2f& tan0, Sk2f& tan1, Sk2f& c) { |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 321 | tan0 = first_unless_nearly_zero(p1 - p0, p2 - p0); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 322 | tan1 = first_unless_nearly_zero(p3 - p2, p3 - p1); |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 323 | |
| 324 | Sk2f c1 = SkNx_fma(Sk2f(1.5f), tan0, p0); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 325 | Sk2f c2 = SkNx_fma(Sk2f(-1.5f), tan1, p3); |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 326 | c = (c1 + c2) * .5f; // Hopefully optimized out if not used? |
| 327 | |
| 328 | return ((c1 - c2).abs() <= 1).allTrue(); |
| 329 | } |
| 330 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 331 | void GrCCGeometry::cubicTo(const SkPoint P[4], float inflectPad, float loopIntersectPad) { |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 332 | SkASSERT(fBuildingContour); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 333 | SkASSERT(P[0] == fPoints.back()); |
Chris Dalton | b0601a4 | 2018-04-10 00:23:45 -0600 | [diff] [blame^] | 334 | |
| 335 | // Don't crunch on the curve or inflate geometry if it is nearly flat (or just very small). |
| 336 | // Flat curves can break the math below. |
| 337 | if (are_collinear(P)) { |
| 338 | this->lineTo(P[3]); |
| 339 | return; |
| 340 | } |
| 341 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 342 | Sk2f p0 = Sk2f::Load(P); |
| 343 | Sk2f p1 = Sk2f::Load(P+1); |
| 344 | Sk2f p2 = Sk2f::Load(P+2); |
| 345 | Sk2f p3 = Sk2f::Load(P+3); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 346 | |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 347 | // Also detect near-quadratics ahead of time. |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 348 | Sk2f tan0, tan1, c; |
| 349 | if (is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, c)) { |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 350 | this->appendMonotonicQuadratics(p0, c, p3); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 354 | double tt[2], ss[2]; |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 355 | fCurrCubicType = SkClassifyCubic(P, tt, ss); |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 356 | SkASSERT(!SkCubicIsDegenerate(fCurrCubicType)); // Should have been caught above. |
| 357 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 358 | SkMatrix CIT; |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 359 | ExcludedTerm skipTerm = GrPathUtils::calcCubicInverseTransposePowerBasisMatrix(P, &CIT); |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 360 | SkASSERT(ExcludedTerm::kNonInvertible != skipTerm); // Should have been caught above. |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 361 | SkASSERT(0 == CIT[6]); |
| 362 | SkASSERT(0 == CIT[7]); |
| 363 | SkASSERT(1 == CIT[8]); |
| 364 | |
| 365 | // Each cubic has five different sections (not always inside t=[0..1]): |
| 366 | // |
| 367 | // 1. The section before the first inflection or loop intersection point, with padding. |
| 368 | // 2. The section that passes through the first inflection/intersection (aka the K,L |
| 369 | // intersection point or T=tt[0]/ss[0]). |
| 370 | // 3. The section between the two inflections/intersections, with padding. |
| 371 | // 4. The section that passes through the second inflection/intersection (aka the K,M |
| 372 | // intersection point or T=tt[1]/ss[1]). |
| 373 | // 5. The section after the second inflection/intersection, with padding. |
| 374 | // |
| 375 | // Sections 1,3,5 can be rendered directly using the CCPR cubic shader. |
| 376 | // |
| 377 | // Sections 2 & 4 must be approximated. For loop intersections we render them with |
| 378 | // quadratic(s), and when passing through an inflection point we use a plain old flat line. |
| 379 | // |
| 380 | // We find T0..T3 below to be the dividing points between these five sections. |
| 381 | float T0, T1, T2, T3; |
| 382 | if (SkCubicType::kLoop != fCurrCubicType) { |
| 383 | Sk2f t = Sk2f(static_cast<float>(tt[0]), static_cast<float>(tt[1])); |
| 384 | Sk2f s = Sk2f(static_cast<float>(ss[0]), static_cast<float>(ss[1])); |
| 385 | Sk2f pad = calc_inflect_homogeneous_padding(inflectPad, t, s, CIT, skipTerm); |
| 386 | |
| 387 | float T[2]; |
| 388 | ((t - pad) / s).store(T); |
| 389 | T0 = T[0]; |
| 390 | T2 = T[1]; |
| 391 | |
| 392 | ((t + pad) / s).store(T); |
| 393 | T1 = T[0]; |
| 394 | T3 = T[1]; |
| 395 | } else { |
| 396 | const float T[2] = {static_cast<float>(tt[0]/ss[0]), static_cast<float>(tt[1]/ss[1])}; |
| 397 | SkSTArray<3, float, true> roots[2]; |
| 398 | calc_loop_intersect_padding_pts(loopIntersectPad, Sk2f::Load(T), CIT, skipTerm, roots); |
| 399 | T0 = roots[0].front(); |
| 400 | if (1 == roots[0].count() || 1 == roots[1].count()) { |
| 401 | // The loop is tighter than our desired padding. Collapse the middle section to a point |
| 402 | // somewhere in the middle-ish of the loop and Sections 2 & 4 will approximate the the |
| 403 | // whole thing with quadratics. |
| 404 | T1 = T2 = (T[0] + T[1]) * .5f; |
| 405 | } else { |
| 406 | T1 = roots[0][1]; |
| 407 | T2 = roots[1][1]; |
| 408 | } |
| 409 | T3 = roots[1].back(); |
| 410 | } |
| 411 | |
| 412 | // Guarantee that T0..T3 are monotonic. |
| 413 | if (T0 > T3) { |
| 414 | // This is not a mathematically valid scenario. The only reason it would happen is if |
| 415 | // padding is very small and we have encountered FP rounding error. |
| 416 | T0 = T1 = T2 = T3 = (T0 + T3) / 2; |
| 417 | } else if (T1 > T2) { |
| 418 | // This just means padding before the middle section overlaps the padding after it. We |
| 419 | // collapse the middle section to a single point that splits the difference between the |
| 420 | // overlap in padding. |
| 421 | T1 = T2 = (T1 + T2) / 2; |
| 422 | } |
| 423 | // Clamp T1 & T2 inside T0..T3. The only reason this would be necessary is if we have |
| 424 | // encountered FP rounding error. |
| 425 | T1 = std::max(T0, std::min(T1, T3)); |
| 426 | T2 = std::max(T0, std::min(T2, T3)); |
| 427 | |
| 428 | // Next we chop the cubic up at all T0..T3 inside 0..1 and store the resulting segments. |
| 429 | if (T1 >= 1) { |
| 430 | // Only sections 1 & 2 can be in 0..1. |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 431 | this->chopCubic<&GrCCGeometry::appendMonotonicCubics, |
| 432 | &GrCCGeometry::appendCubicApproximation>(p0, p1, p2, p3, T0); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 433 | return; |
| 434 | } |
| 435 | |
| 436 | if (T2 <= 0) { |
| 437 | // Only sections 4 & 5 can be in 0..1. |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 438 | this->chopCubic<&GrCCGeometry::appendCubicApproximation, |
| 439 | &GrCCGeometry::appendMonotonicCubics>(p0, p1, p2, p3, T3); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 440 | return; |
| 441 | } |
| 442 | |
| 443 | Sk2f midp0, midp1; // These hold the first two bezier points of the middle section, if needed. |
| 444 | |
| 445 | if (T1 > 0) { |
| 446 | Sk2f T1T1 = Sk2f(T1); |
| 447 | Sk2f ab1 = lerp(p0, p1, T1T1); |
| 448 | Sk2f bc1 = lerp(p1, p2, T1T1); |
| 449 | Sk2f cd1 = lerp(p2, p3, T1T1); |
| 450 | Sk2f abc1 = lerp(ab1, bc1, T1T1); |
| 451 | Sk2f bcd1 = lerp(bc1, cd1, T1T1); |
| 452 | Sk2f abcd1 = lerp(abc1, bcd1, T1T1); |
| 453 | |
| 454 | // Sections 1 & 2. |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 455 | this->chopCubic<&GrCCGeometry::appendMonotonicCubics, |
| 456 | &GrCCGeometry::appendCubicApproximation>(p0, ab1, abc1, abcd1, T0/T1); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 457 | |
| 458 | if (T2 >= 1) { |
| 459 | // The rest of the curve is Section 3 (middle section). |
| 460 | this->appendMonotonicCubics(abcd1, bcd1, cd1, p3); |
| 461 | return; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 462 | } |
| 463 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 464 | // Now calculate the first two bezier points of the middle section. The final two will come |
| 465 | // from when we chop the other side, as that is numerically more stable. |
| 466 | midp0 = abcd1; |
| 467 | midp1 = lerp(abcd1, bcd1, Sk2f((T2 - T1) / (1 - T1))); |
| 468 | } else if (T2 >= 1) { |
| 469 | // The entire cubic is Section 3 (middle section). |
| 470 | this->appendMonotonicCubics(p0, p1, p2, p3); |
| 471 | return; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 472 | } |
| 473 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 474 | SkASSERT(T2 > 0 && T2 < 1); |
| 475 | |
| 476 | Sk2f T2T2 = Sk2f(T2); |
| 477 | Sk2f ab2 = lerp(p0, p1, T2T2); |
| 478 | Sk2f bc2 = lerp(p1, p2, T2T2); |
| 479 | Sk2f cd2 = lerp(p2, p3, T2T2); |
| 480 | Sk2f abc2 = lerp(ab2, bc2, T2T2); |
| 481 | Sk2f bcd2 = lerp(bc2, cd2, T2T2); |
| 482 | Sk2f abcd2 = lerp(abc2, bcd2, T2T2); |
| 483 | |
| 484 | if (T1 <= 0) { |
| 485 | // The curve begins at Section 3 (middle section). |
| 486 | this->appendMonotonicCubics(p0, ab2, abc2, abcd2); |
| 487 | } else if (T2 > T1) { |
| 488 | // Section 3 (middle section). |
| 489 | Sk2f midp2 = lerp(abc2, abcd2, T1/T2); |
| 490 | this->appendMonotonicCubics(midp0, midp1, midp2, abcd2); |
| 491 | } |
| 492 | |
| 493 | // Sections 4 & 5. |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 494 | this->chopCubic<&GrCCGeometry::appendCubicApproximation, |
| 495 | &GrCCGeometry::appendMonotonicCubics>(abcd2, bcd2, cd2, p3, (T3-T2) / (1-T2)); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 496 | } |
| 497 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 498 | template<GrCCGeometry::AppendCubicFn AppendLeftRight> |
| 499 | inline void GrCCGeometry::chopCubicAtMidTangent(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, |
| 500 | const Sk2f& p3, const Sk2f& tan0, |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 501 | const Sk2f& tan1, int maxFutureSubdivisions) { |
| 502 | // Find the T value whose tangent is perpendicular to the vector that bisects tan0 and -tan1. |
| 503 | Sk2f n = normalize(tan0) - normalize(tan1); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 504 | |
| 505 | float a = 3 * dot(p3 + (p1 - p2)*3 - p0, n); |
| 506 | float b = 6 * dot(p0 - p1*2 + p2, n); |
| 507 | float c = 3 * dot(p1 - p0, n); |
| 508 | |
| 509 | float discr = b*b - 4*a*c; |
| 510 | if (discr < 0) { |
| 511 | // If this is the case then the cubic must be nearly flat. |
| 512 | (this->*AppendLeftRight)(p0, p1, p2, p3, maxFutureSubdivisions); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | float q = -.5f * (b + copysignf(std::sqrt(discr), b)); |
| 517 | float m = .5f*q*a; |
| 518 | float T = std::abs(q*q - m) < std::abs(a*c - m) ? q/a : c/q; |
| 519 | |
| 520 | this->chopCubic<AppendLeftRight, AppendLeftRight>(p0, p1, p2, p3, T, maxFutureSubdivisions); |
| 521 | } |
| 522 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 523 | template<GrCCGeometry::AppendCubicFn AppendLeft, GrCCGeometry::AppendCubicFn AppendRight> |
| 524 | inline void GrCCGeometry::chopCubic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, |
| 525 | const Sk2f& p3, float T, int maxFutureSubdivisions) { |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 526 | if (T >= 1) { |
| 527 | (this->*AppendLeft)(p0, p1, p2, p3, maxFutureSubdivisions); |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | if (T <= 0) { |
| 532 | (this->*AppendRight)(p0, p1, p2, p3, maxFutureSubdivisions); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | Sk2f TT = T; |
| 537 | Sk2f ab = lerp(p0, p1, TT); |
| 538 | Sk2f bc = lerp(p1, p2, TT); |
| 539 | Sk2f cd = lerp(p2, p3, TT); |
| 540 | Sk2f abc = lerp(ab, bc, TT); |
| 541 | Sk2f bcd = lerp(bc, cd, TT); |
| 542 | Sk2f abcd = lerp(abc, bcd, TT); |
| 543 | (this->*AppendLeft)(p0, ab, abc, abcd, maxFutureSubdivisions); |
| 544 | (this->*AppendRight)(abcd, bcd, cd, p3, maxFutureSubdivisions); |
| 545 | } |
| 546 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 547 | void GrCCGeometry::appendMonotonicCubics(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, |
| 548 | const Sk2f& p3, int maxSubdivisions) { |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 549 | SkASSERT(maxSubdivisions >= 0); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 550 | if ((p0 == p3).allTrue()) { |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | if (maxSubdivisions) { |
| 555 | Sk2f tan0 = first_unless_nearly_zero(p1 - p0, p2 - p0); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 556 | Sk2f tan1 = first_unless_nearly_zero(p3 - p2, p3 - p1); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 557 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 558 | if (!is_convex_curve_monotonic(p0, tan0, p3, tan1)) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 559 | this->chopCubicAtMidTangent<&GrCCGeometry::appendMonotonicCubics>(p0, p1, p2, p3, |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 560 | tan0, tan1, |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 561 | maxSubdivisions - 1); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 562 | return; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 567 | |
| 568 | // Don't send curves to the GPU if we know they are nearly flat (or just very small). |
| 569 | // Since the cubic segment is known to be convex at this point, our flatness check is simple. |
| 570 | if (are_collinear(p0, (p1 + p2) * .5f, p3)) { |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 571 | this->appendLine(p3); |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 572 | return; |
| 573 | } |
| 574 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 575 | p1.store(&fPoints.push_back()); |
| 576 | p2.store(&fPoints.push_back()); |
| 577 | p3.store(&fPoints.push_back()); |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 578 | fVerbs.push_back(Verb::kMonotonicCubicTo); |
| 579 | ++fCurrContourTallies.fCubics; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 580 | } |
| 581 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 582 | void GrCCGeometry::appendCubicApproximation(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, |
| 583 | const Sk2f& p3, int maxSubdivisions) { |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 584 | SkASSERT(maxSubdivisions >= 0); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 585 | if ((p0 == p3).allTrue()) { |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | if (SkCubicType::kLoop != fCurrCubicType && SkCubicType::kQuadratic != fCurrCubicType) { |
| 590 | // This section passes through an inflection point, so we can get away with a flat line. |
| 591 | // This can cause some curves to feel slightly more flat when inspected rigorously back and |
| 592 | // forth against another renderer, but for now this seems acceptable given the simplicity. |
| 593 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 594 | this->appendLine(p3); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 595 | return; |
| 596 | } |
| 597 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 598 | Sk2f tan0, tan1, c; |
| 599 | if (!is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, c) && maxSubdivisions) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 600 | this->chopCubicAtMidTangent<&GrCCGeometry::appendCubicApproximation>(p0, p1, p2, p3, |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 601 | tan0, tan1, |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 602 | maxSubdivisions - 1); |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 603 | return; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 604 | } |
| 605 | |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 606 | if (maxSubdivisions) { |
| 607 | this->appendMonotonicQuadratics(p0, c, p3); |
| 608 | } else { |
| 609 | this->appendSingleMonotonicQuadratic(p0, c, p3); |
| 610 | } |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 611 | } |
| 612 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 613 | GrCCGeometry::PrimitiveTallies GrCCGeometry::endContour() { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 614 | SkASSERT(fBuildingContour); |
| 615 | SkASSERT(fVerbs.count() >= fCurrContourTallies.fTriangles); |
| 616 | |
| 617 | // The fTriangles field currently contains this contour's starting verb index. We can now |
| 618 | // use it to calculate the size of the contour's fan. |
| 619 | int fanSize = fVerbs.count() - fCurrContourTallies.fTriangles; |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 620 | if (fPoints.back() == fCurrAnchorPoint) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 621 | --fanSize; |
| 622 | fVerbs.push_back(Verb::kEndClosedContour); |
| 623 | } else { |
| 624 | fVerbs.push_back(Verb::kEndOpenContour); |
| 625 | } |
| 626 | |
| 627 | fCurrContourTallies.fTriangles = SkTMax(fanSize - 2, 0); |
| 628 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 629 | SkDEBUGCODE(fBuildingContour = false); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 630 | return fCurrContourTallies; |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 631 | } |