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 | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 30 | fCurrContourTallies = {fVerbs.count(), 0, 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 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 128 | template<int N> static inline SkNx<N,float> lerp(const SkNx<N,float>& a, const SkNx<N,float>& b, |
| 129 | const SkNx<N,float>& t) { |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 130 | return SkNx_fma(t, b - a, a); |
| 131 | } |
| 132 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 133 | void GrCCGeometry::quadraticTo(const SkPoint P[3]) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 134 | SkASSERT(fBuildingContour); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 135 | SkASSERT(P[0] == fPoints.back()); |
| 136 | Sk2f p0 = Sk2f::Load(P); |
| 137 | Sk2f p1 = Sk2f::Load(P+1); |
| 138 | Sk2f p2 = Sk2f::Load(P+2); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 139 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 140 | // Don't crunch on the curve if it is nearly flat (or just very small). Flat curves can break |
| 141 | // The monotonic chopping math. |
| 142 | if (are_collinear(p0, p1, p2)) { |
| 143 | this->appendLine(p2); |
| 144 | return; |
| 145 | } |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 146 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 147 | this->appendQuadratics(p0, p1, p2); |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 148 | } |
| 149 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 150 | inline void GrCCGeometry::appendQuadratics(const Sk2f& p0, const Sk2f& p1, 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)) { |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 156 | this->appendMonotonicQuadratic(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 | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 184 | this->appendMonotonicQuadratic(p0, p01, p012); |
| 185 | this->appendMonotonicQuadratic(p012, p12, p2); |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 188 | inline void GrCCGeometry::appendMonotonicQuadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2) { |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 189 | // Don't send curves to the GPU if we know they are nearly flat (or just very small). |
| 190 | if (are_collinear(p0, p1, p2)) { |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 191 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 192 | this->appendLine(p2); |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 196 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 197 | p1.store(&fPoints.push_back()); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 198 | p2.store(&fPoints.push_back()); |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 199 | fVerbs.push_back(Verb::kMonotonicQuadraticTo); |
| 200 | ++fCurrContourTallies.fQuadratics; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 201 | } |
| 202 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 203 | static inline Sk2f first_unless_nearly_zero(const Sk2f& a, const Sk2f& b) { |
| 204 | Sk2f aa = a*a; |
| 205 | aa += SkNx_shuffle<1,0>(aa); |
| 206 | SkASSERT(aa[0] == aa[1]); |
| 207 | |
| 208 | Sk2f bb = b*b; |
| 209 | bb += SkNx_shuffle<1,0>(bb); |
| 210 | SkASSERT(bb[0] == bb[1]); |
| 211 | |
| 212 | return (aa > bb * SK_ScalarNearlyZero).thenElse(a, b); |
| 213 | } |
| 214 | |
| 215 | static inline void get_cubic_tangents(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, |
| 216 | const Sk2f& p3, Sk2f* tan0, Sk2f* tan1) { |
| 217 | *tan0 = first_unless_nearly_zero(p1 - p0, p2 - p0); |
| 218 | *tan1 = first_unless_nearly_zero(p3 - p2, p3 - p1); |
| 219 | } |
| 220 | |
| 221 | static inline bool is_cubic_nearly_quadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, |
| 222 | const Sk2f& p3, const Sk2f& tan0, const Sk2f& tan1, |
| 223 | Sk2f* c) { |
| 224 | Sk2f c1 = SkNx_fma(Sk2f(1.5f), tan0, p0); |
| 225 | Sk2f c2 = SkNx_fma(Sk2f(-1.5f), tan1, p3); |
| 226 | *c = (c1 + c2) * .5f; // Hopefully optimized out if not used? |
| 227 | return ((c1 - c2).abs() <= 1).allTrue(); |
| 228 | } |
| 229 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 230 | using ExcludedTerm = GrPathUtils::ExcludedTerm; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 231 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 232 | // Finds where to chop a non-loop around its inflection points. The resulting cubic segments will be |
| 233 | // chopped such that a box of radius 'padRadius', centered at any point along the curve segment, is |
| 234 | // guaranteed to not cross the tangent lines at the inflection points (a.k.a lines L & M). |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 235 | // |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 236 | // 'chops' will be filled with 4 T values. The segments between T0..T1 and T2..T3 must be drawn with |
| 237 | // flat lines instead of cubics. |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 238 | // |
| 239 | // A serpentine cubic has two inflection points, so this method takes Sk2f and computes the padding |
| 240 | // for both in SIMD. |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 241 | static inline void find_chops_around_inflection_points(float padRadius, const Sk2f& t, |
| 242 | const Sk2f& s, const SkMatrix& CIT, |
| 243 | ExcludedTerm skipTerm, |
| 244 | SkSTArray<4, float>* chops) { |
| 245 | SkASSERT(chops->empty()); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 246 | SkASSERT(padRadius >= 0); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 247 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 248 | Sk2f Clx = s*s*s; |
| 249 | Sk2f Cly = (ExcludedTerm::kLinearTerm == skipTerm) ? s*s*t*-3 : s*t*t*3; |
| 250 | |
| 251 | Sk2f Lx = CIT[0] * Clx + CIT[3] * Cly; |
| 252 | Sk2f Ly = CIT[1] * Clx + CIT[4] * Cly; |
| 253 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 254 | Sk2f pad = padRadius * (Lx.abs() + Ly.abs()); |
| 255 | pad = (pad * s >= 0).thenElse(pad, -pad); |
| 256 | pad = Sk2f(std::cbrt(pad[0]), std::cbrt(pad[1])); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 257 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 258 | Sk2f leftT = (t - pad) / s; |
| 259 | Sk2f rightT = (t + pad) / s; |
| 260 | Sk2f::Store2(chops->push_back_n(4), leftT, rightT); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static inline void swap_if_greater(float& a, float& b) { |
| 264 | if (a > b) { |
| 265 | std::swap(a, b); |
| 266 | } |
| 267 | } |
| 268 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 269 | // Finds where to chop a non-loop around its intersection point. The resulting cubic segments will |
| 270 | // be chopped such that a box of radius 'padRadius', centered at any point along the curve segment, |
| 271 | // is guaranteed to not cross the tangent lines at the intersection point (a.k.a lines L & M). |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 272 | // |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 273 | // 'chops' will be filled with 0, 2, or 4 T values. The segments between T0..T1 and T2..T3 must be |
| 274 | // drawn with quadratic splines instead of cubics. |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 275 | // |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 276 | // A loop intersection falls at two different T values, so this method takes Sk2f and computes the |
| 277 | // padding for both in SIMD. |
| 278 | static inline void find_chops_around_loop_intersection(float padRadius, const Sk2f& t, |
| 279 | const Sk2f& s, const SkMatrix& CIT, |
| 280 | ExcludedTerm skipTerm, |
| 281 | SkSTArray<4, float>* chops) { |
| 282 | SkASSERT(chops->empty()); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 283 | SkASSERT(padRadius >= 0); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 284 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 285 | Sk2f T2 = t/s; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 286 | Sk2f T1 = SkNx_shuffle<1,0>(T2); |
| 287 | Sk2f Cl = (ExcludedTerm::kLinearTerm == skipTerm) ? T2*-2 - T1 : T2*T2 + T2*T1*2; |
| 288 | Sk2f Lx = Cl * CIT[3] + CIT[0]; |
| 289 | Sk2f Ly = Cl * CIT[4] + CIT[1]; |
| 290 | |
| 291 | Sk2f bloat = Sk2f(+.5f * padRadius, -.5f * padRadius) * (Lx.abs() + Ly.abs()); |
| 292 | Sk2f q = (1.f/3) * (T2 - T1); |
| 293 | |
| 294 | Sk2f qqq = q*q*q; |
| 295 | Sk2f discr = qqq*bloat*2 + bloat*bloat; |
| 296 | |
| 297 | float numRoots[2], D[2]; |
| 298 | (discr < 0).thenElse(3, 1).store(numRoots); |
| 299 | (T2 - q).store(D); |
| 300 | |
| 301 | // Values for calculating one root. |
| 302 | float R[2], QQ[2]; |
| 303 | if ((discr >= 0).anyTrue()) { |
| 304 | Sk2f r = qqq + bloat; |
| 305 | Sk2f s = r.abs() + discr.sqrt(); |
| 306 | (r > 0).thenElse(-s, s).store(R); |
| 307 | (q*q).store(QQ); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 308 | } |
| 309 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 310 | // Values for calculating three roots. |
| 311 | float P[2], cosTheta3[2]; |
| 312 | if ((discr < 0).anyTrue()) { |
| 313 | (q.abs() * -2).store(P); |
| 314 | ((q >= 0).thenElse(1, -1) + bloat / qqq.abs()).store(cosTheta3); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 315 | } |
| 316 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 317 | for (int i = 0; i < 2; ++i) { |
| 318 | if (1 == numRoots[i]) { |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 319 | // When there is only one root, line L chops from root..1, line M chops from 0..root. |
| 320 | if (1 == i) { |
| 321 | chops->push_back(0); |
| 322 | } |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 323 | float A = cbrtf(R[i]); |
| 324 | float B = A != 0 ? QQ[i]/A : 0; |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 325 | chops->push_back(A + B + D[i]); |
| 326 | if (0 == i) { |
| 327 | chops->push_back(1); |
| 328 | } |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 329 | continue; |
| 330 | } |
| 331 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 332 | static constexpr float k2PiOver3 = 2 * SK_ScalarPI / 3; |
| 333 | float theta = std::acos(cosTheta3[i]) * (1.f/3); |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 334 | float roots[3] = {P[i] * std::cos(theta) + D[i], |
| 335 | P[i] * std::cos(theta + k2PiOver3) + D[i], |
| 336 | P[i] * std::cos(theta - k2PiOver3) + D[i]}; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 337 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 338 | // Sort the three roots. |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 339 | swap_if_greater(roots[0], roots[1]); |
| 340 | swap_if_greater(roots[1], roots[2]); |
| 341 | swap_if_greater(roots[0], roots[1]); |
| 342 | |
| 343 | // Line L chops around the first 2 roots, line M chops around the second 2. |
| 344 | chops->push_back_n(2, &roots[i]); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 348 | void GrCCGeometry::cubicTo(const SkPoint P[4], float inflectPad, float loopIntersectPad) { |
| 349 | SkASSERT(fBuildingContour); |
| 350 | SkASSERT(P[0] == fPoints.back()); |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 351 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 352 | // Don't crunch on the curve or inflate geometry if it is nearly flat (or just very small). |
| 353 | // Flat curves can break the math below. |
| 354 | if (are_collinear(P)) { |
| 355 | this->lineTo(P[3]); |
| 356 | return; |
| 357 | } |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 358 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 359 | Sk2f p0 = Sk2f::Load(P); |
| 360 | Sk2f p1 = Sk2f::Load(P+1); |
| 361 | Sk2f p2 = Sk2f::Load(P+2); |
| 362 | Sk2f p3 = Sk2f::Load(P+3); |
| 363 | |
| 364 | // Also detect near-quadratics ahead of time. |
| 365 | Sk2f tan0, tan1, c; |
| 366 | get_cubic_tangents(p0, p1, p2, p3, &tan0, &tan1); |
| 367 | if (is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, &c)) { |
| 368 | this->appendQuadratics(p0, c, p3); |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | double tt[2], ss[2], D[4]; |
| 373 | fCurrCubicType = SkClassifyCubic(P, tt, ss, D); |
| 374 | SkASSERT(!SkCubicIsDegenerate(fCurrCubicType)); |
| 375 | Sk2f t = Sk2f(static_cast<float>(tt[0]), static_cast<float>(tt[1])); |
| 376 | Sk2f s = Sk2f(static_cast<float>(ss[0]), static_cast<float>(ss[1])); |
| 377 | |
| 378 | SkMatrix CIT; |
| 379 | ExcludedTerm skipTerm = GrPathUtils::calcCubicInverseTransposePowerBasisMatrix(P, &CIT); |
| 380 | SkASSERT(ExcludedTerm::kNonInvertible != skipTerm); // Should have been caught above. |
| 381 | SkASSERT(0 == CIT[6]); |
| 382 | SkASSERT(0 == CIT[7]); |
| 383 | SkASSERT(1 == CIT[8]); |
| 384 | |
| 385 | SkSTArray<4, float> chops; |
| 386 | if (SkCubicType::kLoop != fCurrCubicType) { |
| 387 | find_chops_around_inflection_points(inflectPad, t, s, CIT, skipTerm, &chops); |
| 388 | } else { |
| 389 | find_chops_around_loop_intersection(loopIntersectPad, t, s, CIT, skipTerm, &chops); |
| 390 | } |
| 391 | if (chops[1] >= chops[2]) { |
| 392 | // This just the means the KLM roots are so close that their paddings overlap. We will |
| 393 | // approximate the entire middle section, but still have it chopped midway. For loops this |
| 394 | // chop guarantees the append code only sees convex segments. Otherwise, it means we are (at |
| 395 | // least almost) a cusp and the chop makes sure we get a sharp point. |
| 396 | Sk2f ts = t * SkNx_shuffle<1,0>(s); |
| 397 | chops[1] = chops[2] = (ts[0] + ts[1]) / (2*s[0]*s[1]); |
| 398 | } |
| 399 | |
| 400 | #ifdef SK_DEBUG |
| 401 | for (int i = 1; i < chops.count(); ++i) { |
| 402 | SkASSERT(chops[i] >= chops[i - 1]); |
| 403 | } |
| 404 | #endif |
| 405 | this->appendCubics(AppendCubicMode::kLiteral, p0, p1, p2, p3, chops.begin(), chops.count()); |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 406 | } |
| 407 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 408 | static inline void chop_cubic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, const Sk2f& p3, |
| 409 | float T, Sk2f* ab, Sk2f* abc, Sk2f* abcd, Sk2f* bcd, Sk2f* cd) { |
| 410 | Sk2f TT = T; |
| 411 | *ab = lerp(p0, p1, TT); |
| 412 | Sk2f bc = lerp(p1, p2, TT); |
| 413 | *cd = lerp(p2, p3, TT); |
| 414 | *abc = lerp(*ab, bc, TT); |
| 415 | *bcd = lerp(bc, *cd, TT); |
| 416 | *abcd = lerp(*abc, *bcd, TT); |
| 417 | } |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 418 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 419 | void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f& p1, |
| 420 | const Sk2f& p2, const Sk2f& p3, const float chops[], int numChops, |
| 421 | float localT0, float localT1) { |
| 422 | if (numChops) { |
| 423 | SkASSERT(numChops > 0); |
| 424 | int midChopIdx = numChops/2; |
| 425 | float T = chops[midChopIdx]; |
| 426 | // Chops alternate between literal and approximate mode. |
| 427 | AppendCubicMode rightMode = (AppendCubicMode)((bool)mode ^ (midChopIdx & 1) ^ 1); |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 428 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 429 | if (T <= localT0) { |
| 430 | // T is outside 0..1. Append the right side only. |
| 431 | this->appendCubics(rightMode, p0, p1, p2, p3, &chops[midChopIdx + 1], |
| 432 | numChops - midChopIdx - 1, localT0, localT1); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | if (T >= localT1) { |
| 437 | // T is outside 0..1. Append the left side only. |
| 438 | this->appendCubics(mode, p0, p1, p2, p3, chops, midChopIdx, localT0, localT1); |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | float localT = (T - localT0) / (localT1 - localT0); |
| 443 | Sk2f p01, p02, pT, p11, p12; |
| 444 | chop_cubic(p0, p1, p2, p3, localT, &p01, &p02, &pT, &p11, &p12); |
| 445 | this->appendCubics(mode, p0, p01, p02, pT, chops, midChopIdx, localT0, T); |
| 446 | this->appendCubics(rightMode, pT, p11, p12, p3, &chops[midChopIdx + 1], |
| 447 | numChops - midChopIdx - 1, T, localT1); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | this->appendCubics(mode, p0, p1, p2, p3); |
| 452 | } |
| 453 | |
| 454 | void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f& p1, |
| 455 | const Sk2f& p2, const Sk2f& p3, int maxSubdivisions) { |
| 456 | if ((p0 == p3).allTrue()) { |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | if (SkCubicType::kLoop != fCurrCubicType) { |
| 461 | // Serpentines and cusps are always monotonic after chopping around inflection points. |
| 462 | SkASSERT(!SkCubicIsDegenerate(fCurrCubicType)); |
| 463 | |
| 464 | if (AppendCubicMode::kApproximate == mode) { |
| 465 | // This section passes through an inflection point, so we can get away with a flat line. |
| 466 | // This can cause some curves to feel slightly more flat when inspected rigorously back |
| 467 | // and forth against another renderer, but for now this seems acceptable given the |
| 468 | // simplicity. |
| 469 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
| 470 | this->appendLine(p3); |
| 471 | return; |
| 472 | } |
| 473 | } else { |
| 474 | Sk2f tan0, tan1; |
| 475 | get_cubic_tangents(p0, p1, p2, p3, &tan0, &tan1); |
| 476 | |
| 477 | if (maxSubdivisions && !is_convex_curve_monotonic(p0, tan0, p3, tan1)) { |
| 478 | this->chopAndAppendCubicAtMidTangent(mode, p0, p1, p2, p3, tan0, tan1, |
| 479 | maxSubdivisions - 1); |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | if (AppendCubicMode::kApproximate == mode) { |
| 484 | Sk2f c; |
| 485 | if (!is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, &c) && maxSubdivisions) { |
| 486 | this->chopAndAppendCubicAtMidTangent(mode, p0, p1, p2, p3, tan0, tan1, |
| 487 | maxSubdivisions - 1); |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | this->appendMonotonicQuadratic(p0, c, p3); |
| 492 | return; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | // Don't send curves to the GPU if we know they are nearly flat (or just very small). |
| 497 | // Since the cubic segment is known to be convex at this point, our flatness check is simple. |
| 498 | if (are_collinear(p0, (p1 + p2) * .5f, p3)) { |
| 499 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
| 500 | this->appendLine(p3); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
| 505 | p1.store(&fPoints.push_back()); |
| 506 | p2.store(&fPoints.push_back()); |
| 507 | p3.store(&fPoints.push_back()); |
| 508 | fVerbs.push_back(Verb::kMonotonicCubicTo); |
| 509 | ++fCurrContourTallies.fCubics; |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 510 | } |
| 511 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 512 | // Given a convex curve segment with the following order-2 tangent function: |
| 513 | // |
| 514 | // |C2x C2y| |
| 515 | // tan = some_scale * |dx/dt dy/dt| = |t^2 t 1| * |C1x C1y| |
| 516 | // |C0x C0y| |
| 517 | // |
| 518 | // This function finds the T value whose tangent angle is halfway between the tangents at T=0 and |
| 519 | // T=1 (tan0 and tan1). |
| 520 | static inline float find_midtangent(const Sk2f& tan0, const Sk2f& tan1, |
| 521 | float scale2, const Sk2f& C2, |
| 522 | float scale1, const Sk2f& C1, |
| 523 | float scale0, const Sk2f& C0) { |
| 524 | // Tangents point in the direction of increasing T, so tan0 and -tan1 both point toward the |
| 525 | // midtangent. 'n' will therefore bisect tan0 and -tan1, giving us the normal to the midtangent. |
| 526 | // |
| 527 | // n dot midtangent = 0 |
| 528 | // |
| 529 | Sk2f n = normalize(tan0) - normalize(tan1); |
| 530 | |
| 531 | // Find the T value at the midtangent. This is a simple quadratic equation: |
| 532 | // |
| 533 | // midtangent dot n = 0 |
| 534 | // |
| 535 | // (|t^2 t 1| * C) dot n = 0 |
| 536 | // |
| 537 | // |t^2 t 1| dot C*n = 0 |
| 538 | // |
| 539 | // First find coeffs = C*n. |
| 540 | Sk4f C[2]; |
| 541 | Sk2f::Store4(C, C2, C1, C0, 0); |
| 542 | Sk4f coeffs = C[0]*n[0] + C[1]*n[1]; |
| 543 | if (1 != scale2 || 1 != scale1 || 1 != scale0) { |
| 544 | coeffs *= Sk4f(scale2, scale1, scale0, 0); |
| 545 | } |
| 546 | |
| 547 | // Now solve the quadratic. |
| 548 | float a = coeffs[0], b = coeffs[1], c = coeffs[2]; |
| 549 | float discr = b*b - 4*a*c; |
| 550 | if (discr < 0) { |
| 551 | return 0; // This will only happen if the curve is a line. |
| 552 | } |
| 553 | |
| 554 | // The roots are q/a and c/q. Pick the one closer to T=.5. |
| 555 | float q = -.5f * (b + copysignf(std::sqrt(discr), b)); |
| 556 | float r = .5f*q*a; |
| 557 | return std::abs(q*q - r) < std::abs(a*c - r) ? q/a : c/q; |
| 558 | } |
| 559 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 560 | inline void GrCCGeometry::chopAndAppendCubicAtMidTangent(AppendCubicMode mode, const Sk2f& p0, |
| 561 | const Sk2f& p1, const Sk2f& p2, |
| 562 | const Sk2f& p3, const Sk2f& tan0, |
| 563 | const Sk2f& tan1, |
| 564 | int maxFutureSubdivisions) { |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 565 | float midT = find_midtangent(tan0, tan1, 3, p3 + (p1 - p2)*3 - p0, |
| 566 | 6, p0 - p1*2 + p2, |
| 567 | 3, p1 - p0); |
| 568 | // Use positive logic since NaN fails comparisons. (However midT should not be NaN since we cull |
| 569 | // near-flat cubics in cubicTo().) |
| 570 | if (!(midT > 0 && midT < 1)) { |
| 571 | // The cubic is flat. Otherwise there would be a real midtangent inside T=0..1. |
| 572 | this->appendLine(p3); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 573 | return; |
| 574 | } |
| 575 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame^] | 576 | Sk2f p01, p02, pT, p11, p12; |
| 577 | chop_cubic(p0, p1, p2, p3, midT, &p01, &p02, &pT, &p11, &p12); |
| 578 | this->appendCubics(mode, p0, p01, p02, pT, maxFutureSubdivisions); |
| 579 | this->appendCubics(mode, pT, p11, p12, p3, maxFutureSubdivisions); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 580 | } |
| 581 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 582 | void GrCCGeometry::conicTo(const SkPoint P[3], float w) { |
| 583 | SkASSERT(fBuildingContour); |
| 584 | SkASSERT(P[0] == fPoints.back()); |
| 585 | Sk2f p0 = Sk2f::Load(P); |
| 586 | Sk2f p1 = Sk2f::Load(P+1); |
| 587 | Sk2f p2 = Sk2f::Load(P+2); |
| 588 | |
| 589 | // Don't crunch on the curve if it is nearly flat (or just very small). Collinear control points |
| 590 | // can break the midtangent-finding math below. |
| 591 | if (are_collinear(p0, p1, p2)) { |
| 592 | this->appendLine(p2); |
| 593 | return; |
| 594 | } |
| 595 | |
| 596 | Sk2f tan0 = p1 - p0; |
| 597 | Sk2f tan1 = p2 - p1; |
| 598 | // The derivative of a conic has a cumbersome order-4 denominator. However, this isn't necessary |
| 599 | // if we are only interested in a vector in the same *direction* as a given tangent line. Since |
| 600 | // the denominator scales dx and dy uniformly, we can throw it out completely after evaluating |
| 601 | // the derivative with the standard quotient rule. This leaves us with a simpler quadratic |
| 602 | // function that we use to find the midtangent. |
| 603 | float midT = find_midtangent(tan0, tan1, 1, (w - 1) * (p2 - p0), |
| 604 | 1, (p2 - p0) - 2*w*(p1 - p0), |
| 605 | 1, w*(p1 - p0)); |
| 606 | // Use positive logic since NaN fails comparisons. (However midT should not be NaN since we cull |
| 607 | // near-linear conics above. And while w=0 is flat, it's not a line and has valid midtangents.) |
| 608 | if (!(midT > 0 && midT < 1)) { |
| 609 | // The conic is flat. Otherwise there would be a real midtangent inside T=0..1. |
| 610 | this->appendLine(p2); |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | // Evaluate the conic at midT. |
| 615 | Sk4f p3d0 = Sk4f(p0[0], p0[1], 1, 0); |
| 616 | Sk4f p3d1 = Sk4f(p1[0], p1[1], 1, 0) * w; |
| 617 | Sk4f p3d2 = Sk4f(p2[0], p2[1], 1, 0); |
| 618 | Sk4f midT4 = midT; |
| 619 | |
| 620 | Sk4f p3d01 = lerp(p3d0, p3d1, midT4); |
| 621 | Sk4f p3d12 = lerp(p3d1, p3d2, midT4); |
| 622 | Sk4f p3d012 = lerp(p3d01, p3d12, midT4); |
| 623 | |
| 624 | Sk2f midpoint = Sk2f(p3d012[0], p3d012[1]) / p3d012[2]; |
| 625 | |
| 626 | if (are_collinear(p0, midpoint, p2, 1) || // Check if the curve is within one pixel of flat. |
| 627 | ((midpoint - p1).abs() < 1).allTrue()) { // Check if the curve is almost a triangle. |
| 628 | // Draw the conic as a triangle instead. Our AA approximation won't do well if the curve |
| 629 | // gets wrapped too tightly, and if we get too close to p1 we will pick up artifacts from |
| 630 | // the implicit function's reflection. |
| 631 | this->appendLine(midpoint); |
| 632 | this->appendLine(p2); |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | if (!is_convex_curve_monotonic(p0, tan0, p2, tan1)) { |
| 637 | // Chop the conic at midtangent to produce two monotonic segments. |
| 638 | Sk2f ww = Sk2f(p3d01[2], p3d12[2]) * Sk2f(p3d012[2]).rsqrt(); |
| 639 | this->appendMonotonicConic(p0, Sk2f(p3d01[0], p3d01[1]) / p3d01[2], midpoint, ww[0]); |
| 640 | this->appendMonotonicConic(midpoint, Sk2f(p3d12[0], p3d12[1]) / p3d12[2], p2, ww[1]); |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | this->appendMonotonicConic(p0, p1, p2, w); |
| 645 | } |
| 646 | |
| 647 | void GrCCGeometry::appendMonotonicConic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, float w) { |
| 648 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
| 649 | |
| 650 | // Don't send curves to the GPU if we know they are nearly flat (or just very small). |
| 651 | if (are_collinear(p0, p1, p2)) { |
| 652 | this->appendLine(p2); |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | p1.store(&fPoints.push_back()); |
| 657 | p2.store(&fPoints.push_back()); |
| 658 | fConicWeights.push_back(w); |
| 659 | fVerbs.push_back(Verb::kMonotonicConicTo); |
| 660 | ++fCurrContourTallies.fConics; |
| 661 | } |
| 662 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 663 | GrCCGeometry::PrimitiveTallies GrCCGeometry::endContour() { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 664 | SkASSERT(fBuildingContour); |
| 665 | SkASSERT(fVerbs.count() >= fCurrContourTallies.fTriangles); |
| 666 | |
| 667 | // The fTriangles field currently contains this contour's starting verb index. We can now |
| 668 | // use it to calculate the size of the contour's fan. |
| 669 | int fanSize = fVerbs.count() - fCurrContourTallies.fTriangles; |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 670 | if (fPoints.back() == fCurrAnchorPoint) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 671 | --fanSize; |
| 672 | fVerbs.push_back(Verb::kEndClosedContour); |
| 673 | } else { |
| 674 | fVerbs.push_back(Verb::kEndOpenContour); |
| 675 | } |
| 676 | |
| 677 | fCurrContourTallies.fTriangles = SkTMax(fanSize - 2, 0); |
| 678 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 679 | SkDEBUGCODE(fBuildingContour = false); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 680 | return fCurrContourTallies; |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 681 | } |