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 | d8bae7d | 2018-04-19 13:13:25 -0600 | [diff] [blame] | 21 | static constexpr float kFlatnessThreshold = 1/16.f; // 1/16 of a pixel. |
| 22 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 23 | void GrCCGeometry::beginPath() { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 24 | SkASSERT(!fBuildingContour); |
| 25 | fVerbs.push_back(Verb::kBeginPath); |
| 26 | } |
| 27 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 28 | void GrCCGeometry::beginContour(const SkPoint& pt) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 29 | SkASSERT(!fBuildingContour); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 30 | // 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 Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 32 | fCurrContourTallies = {fVerbs.count(), 0, 0, 0, 0}; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 33 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 34 | fPoints.push_back(pt); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 35 | fVerbs.push_back(Verb::kBeginContour); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 36 | fCurrAnchorPoint = pt; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 37 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 38 | SkDEBUGCODE(fBuildingContour = true); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 39 | } |
| 40 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 41 | void GrCCGeometry::lineTo(const SkPoint& pt) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 42 | SkASSERT(fBuildingContour); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 43 | fPoints.push_back(pt); |
| 44 | fVerbs.push_back(Verb::kLineTo); |
| 45 | } |
| 46 | |
| 47 | void GrCCGeometry::appendLine(const Sk2f& endpt) { |
| 48 | endpt.store(&fPoints.push_back()); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 49 | fVerbs.push_back(Verb::kLineTo); |
| 50 | } |
| 51 | |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 52 | static inline Sk2f normalize(const Sk2f& n) { |
| 53 | Sk2f nn = n*n; |
| 54 | return n * (nn + SkNx_shuffle<1,0>(nn)).rsqrt(); |
| 55 | } |
| 56 | |
| 57 | static 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 Dalton | b0601a4 | 2018-04-10 00:23:45 -0600 | [diff] [blame] | 63 | static inline bool are_collinear(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, |
Chris Dalton | d8bae7d | 2018-04-19 13:13:25 -0600 | [diff] [blame] | 64 | float tolerance = kFlatnessThreshold) { |
Chris Dalton | b0601a4 | 2018-04-10 00:23:45 -0600 | [diff] [blame] | 65 | Sk2f l = p2 - p0; // Line from p0 -> p2. |
Chris Dalton | 900cd05 | 2017-09-07 10:36:51 -0600 | [diff] [blame] | 66 | |
Chris Dalton | b0601a4 | 2018-04-10 00:23:45 -0600 | [diff] [blame] | 67 | // lwidth = Manhattan width of l. |
| 68 | Sk2f labs = l.abs(); |
| 69 | float lwidth = labs[0] + labs[1]; |
Chris Dalton | 900cd05 | 2017-09-07 10:36:51 -0600 | [diff] [blame] | 70 | |
Chris Dalton | b0601a4 | 2018-04-10 00:23:45 -0600 | [diff] [blame] | 71 | // 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 Dalton | 900cd05 | 2017-09-07 10:36:51 -0600 | [diff] [blame] | 75 | |
Chris Dalton | b0601a4 | 2018-04-10 00:23:45 -0600 | [diff] [blame] | 76 | // 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 Dalton | d8bae7d | 2018-04-19 13:13:25 -0600 | [diff] [blame] | 92 | static inline bool are_collinear(const SkPoint P[4], float tolerance = kFlatnessThreshold) { |
Chris Dalton | b0601a4 | 2018-04-10 00:23:45 -0600 | [diff] [blame] | 93 | 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 Dalton | 900cd05 | 2017-09-07 10:36:51 -0600 | [diff] [blame] | 115 | } |
| 116 | |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 117 | // 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] | 118 | static inline bool is_convex_curve_monotonic(const Sk2f& startPt, const Sk2f& tan0, |
| 119 | const Sk2f& endPt, const Sk2f& tan1) { |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 120 | Sk2f v = endPt - startPt; |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 121 | float dot0 = dot(tan0, v); |
| 122 | float dot1 = dot(tan1, v); |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 123 | |
| 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 Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 130 | template<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 Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 132 | return SkNx_fma(t, b - a, a); |
| 133 | } |
| 134 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 135 | void GrCCGeometry::quadraticTo(const SkPoint P[3]) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 136 | SkASSERT(fBuildingContour); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 137 | 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 Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 141 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 142 | // 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 Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 148 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 149 | this->appendQuadratics(p0, p1, p2); |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 150 | } |
| 151 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 152 | 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] | 153 | Sk2f tan0 = p1 - p0; |
| 154 | Sk2f tan1 = p2 - p1; |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 155 | |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 156 | // 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] | 157 | if (is_convex_curve_monotonic(p0, tan0, p2, tan1)) { |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 158 | this->appendMonotonicQuadratic(p0, p1, p2); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 159 | return; |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // 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] | 163 | // tangent angle is halfway between tan0 and tan1. |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 164 | Sk2f n = normalize(tan0) - normalize(tan1); |
| 165 | |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 166 | // The midtangent can be found where (dQ(t) dot n) = 0: |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 167 | // |
| 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 Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 186 | this->appendMonotonicQuadratic(p0, p01, p012); |
| 187 | this->appendMonotonicQuadratic(p012, p12, p2); |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 190 | 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] | 191 | // 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 Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 193 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
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 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 198 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 199 | p1.store(&fPoints.push_back()); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 200 | p2.store(&fPoints.push_back()); |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 201 | fVerbs.push_back(Verb::kMonotonicQuadraticTo); |
| 202 | ++fCurrContourTallies.fQuadratics; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 203 | } |
| 204 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 205 | static 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 | |
| 217 | static 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 | |
| 223 | static 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 Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 232 | using ExcludedTerm = GrPathUtils::ExcludedTerm; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 233 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 234 | // 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 Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 237 | // |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 238 | // 'chops' will be filled with 0, 2, or 4 T values. The segments between T0..T1 and T2..T3 must be |
| 239 | // drawn with flat lines instead of cubics. |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 240 | // |
| 241 | // A serpentine cubic has two inflection points, so this method takes Sk2f and computes the padding |
| 242 | // for both in SIMD. |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 243 | static inline void find_chops_around_inflection_points(float padRadius, Sk2f tl, Sk2f sl, |
| 244 | const SkMatrix& CIT, ExcludedTerm skipTerm, |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 245 | SkSTArray<4, float>* chops) { |
| 246 | SkASSERT(chops->empty()); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 247 | SkASSERT(padRadius >= 0); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 248 | |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 249 | // The homogeneous parametric functions for distance from lines L & M are: |
| 250 | // |
| 251 | // l(t,s) = (t*sl - s*tl)^3 |
| 252 | // m(t,s) = (t*sm - s*tm)^3 |
| 253 | // |
| 254 | // See "Resolution Independent Curve Rendering using Programmable Graphics Hardware", |
| 255 | // 4.3 Finding klmn: |
| 256 | // |
| 257 | // https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf |
| 258 | // |
| 259 | // From here on we use Sk2f with "L" names, but the second lane will be for line M. |
| 260 | tl = (sl > 0).thenElse(tl, -tl); // Tl=tl/sl is the triple root of l(t,s). Normalize so s >= 0. |
| 261 | sl = sl.abs(); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 262 | |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 263 | // Convert l(t,s), m(t,s) to power-basis form: |
| 264 | // |
| 265 | // | l3 m3 | |
| 266 | // |l(t,s) m(t,s)| = |t^3 t^2*s t*s^2 s^3| * | l2 m2 | |
| 267 | // | l1 m1 | |
| 268 | // | l0 m0 | |
| 269 | // |
| 270 | Sk2f l3 = sl*sl*sl; |
| 271 | Sk2f l2or1 = (ExcludedTerm::kLinearTerm == skipTerm) ? sl*sl*tl*-3 : sl*tl*tl*3; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 272 | |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 273 | // The equation for line L can be found as follows: |
| 274 | // |
| 275 | // L = C^-1 * (l excluding skipTerm) |
| 276 | // |
| 277 | // (See comments for GrPathUtils::calcCubicInverseTransposePowerBasisMatrix.) |
| 278 | Sk2f Lx = CIT[0] * l3 + CIT[3] * l2or1; |
| 279 | Sk2f Ly = CIT[1] * l3 + CIT[4] * l2or1; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 280 | |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 281 | // A box of radius "padRadius" is touching line L if "center dot L" is less than the Manhattan |
| 282 | // with of L. (See rationale in are_collinear.) |
| 283 | Sk2f Lwidth = Lx.abs() + Ly.abs(); |
| 284 | Sk2f pad = Lwidth * padRadius; |
| 285 | |
| 286 | // Will T=(t + cbrt(pad))/s be greater than 0? No need to solve roots outside T=0..1. |
| 287 | Sk2f insideLeftPad = pad + tl*tl*tl; |
| 288 | |
| 289 | // Will T=(t - cbrt(pad))/s be less than 1? No need to solve roots outside T=0..1. |
| 290 | Sk2f tms = tl - sl; |
| 291 | Sk2f insideRightPad = pad - tms*tms*tms; |
| 292 | |
| 293 | // Solve for the T values where abs(l(T)) = pad. |
| 294 | if (insideLeftPad[0] > 0 && insideRightPad[0] > 0) { |
| 295 | float padT = cbrtf(pad[0]); |
| 296 | Sk2f pts = (tl[0] + Sk2f(-padT, +padT)) / sl[0]; |
| 297 | pts.store(chops->push_back_n(2)); |
| 298 | } |
| 299 | |
| 300 | // Solve for the T values where abs(m(T)) = pad. |
| 301 | if (insideLeftPad[1] > 0 && insideRightPad[1] > 0) { |
| 302 | float padT = cbrtf(pad[1]); |
| 303 | Sk2f pts = (tl[1] + Sk2f(-padT, +padT)) / sl[1]; |
| 304 | pts.store(chops->push_back_n(2)); |
| 305 | } |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static inline void swap_if_greater(float& a, float& b) { |
| 309 | if (a > b) { |
| 310 | std::swap(a, b); |
| 311 | } |
| 312 | } |
| 313 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 314 | // Finds where to chop a non-loop around its intersection point. The resulting cubic segments will |
| 315 | // be chopped such that a box of radius 'padRadius', centered at any point along the curve segment, |
| 316 | // is guaranteed to not cross the tangent lines at the intersection point (a.k.a lines L & M). |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 317 | // |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 318 | // 'chops' will be filled with 0, 2, or 4 T values. The segments between T0..T1 and T2..T3 must be |
| 319 | // drawn with quadratic splines instead of cubics. |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 320 | // |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 321 | // A loop intersection falls at two different T values, so this method takes Sk2f and computes the |
| 322 | // padding for both in SIMD. |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 323 | static inline void find_chops_around_loop_intersection(float padRadius, Sk2f t2, Sk2f s2, |
| 324 | const SkMatrix& CIT, ExcludedTerm skipTerm, |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 325 | SkSTArray<4, float>* chops) { |
| 326 | SkASSERT(chops->empty()); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 327 | SkASSERT(padRadius >= 0); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 328 | |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 329 | // The parametric functions for distance from lines L & M are: |
| 330 | // |
| 331 | // l(T) = (T - Td)^2 * (T - Te) |
| 332 | // m(T) = (T - Td) * (T - Te)^2 |
| 333 | // |
| 334 | // See "Resolution Independent Curve Rendering using Programmable Graphics Hardware", |
| 335 | // 4.3 Finding klmn: |
| 336 | // |
| 337 | // https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf |
| 338 | Sk2f T2 = t2/s2; // T2 is the double root of l(T). |
| 339 | Sk2f T1 = SkNx_shuffle<1,0>(T2); // T1 is the other root of l(T). |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 340 | |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 341 | // Convert l(T), m(T) to power-basis form: |
| 342 | // |
| 343 | // | 1 1 | |
| 344 | // |l(T) m(T)| = |T^3 T^2 T 1| * | l2 m2 | |
| 345 | // | l1 m1 | |
| 346 | // | l0 m0 | |
| 347 | // |
| 348 | // From here on we use Sk2f with "L" names, but the second lane will be for line M. |
| 349 | Sk2f l2 = SkNx_fma(Sk2f(-2), T2, -T1); |
| 350 | Sk2f l1 = T2 * SkNx_fma(Sk2f(2), T1, T2); |
| 351 | Sk2f l0 = -T2*T2*T1; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 352 | |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 353 | // The equation for line L can be found as follows: |
| 354 | // |
| 355 | // L = C^-1 * (l excluding skipTerm) |
| 356 | // |
| 357 | // (See comments for GrPathUtils::calcCubicInverseTransposePowerBasisMatrix.) |
| 358 | Sk2f l2or1 = (ExcludedTerm::kLinearTerm == skipTerm) ? l2 : l1; |
| 359 | Sk2f Lx = CIT[3] * l2or1 + CIT[0]; // l3 is always 1. |
| 360 | Sk2f Ly = CIT[4] * l2or1 - CIT[1]; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 361 | |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 362 | // A box of radius "padRadius" is touching line L if "center dot L" is less than the Manhattan |
| 363 | // with of L. (See rationale in are_collinear.) |
| 364 | Sk2f Lwidth = Lx.abs() + Ly.abs(); |
| 365 | Sk2f pad = Lwidth * padRadius; |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 366 | |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 367 | // Is l(T=0) outside the padding around line L? |
| 368 | Sk2f lT0 = l0; // l(T=0) = |0 0 0 1| dot |1 l2 l1 l0| = l0 |
| 369 | Sk2f outsideT0 = lT0.abs() - pad; |
| 370 | |
| 371 | // Is l(T=1) outside the padding around line L? |
| 372 | Sk2f lT1 = (Sk2f(1) + l2 + l1 + l0).abs(); // l(T=1) = |1 1 1 1| dot |1 l2 l1 l0| |
| 373 | Sk2f outsideT1 = lT1.abs() - pad; |
| 374 | |
| 375 | // Values for solving the cubic. |
| 376 | Sk2f p, q, qqq, discr, numRoots, D; |
| 377 | bool hasDiscr = false; |
| 378 | |
| 379 | // Values for calculating one root (rarely needed). |
| 380 | Sk2f R, QQ; |
| 381 | bool hasOneRootVals = false; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 382 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 383 | // Values for calculating three roots. |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 384 | Sk2f P, cosTheta3; |
| 385 | bool hasThreeRootVals = false; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 386 | |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 387 | // Solve for the T values where l(T) = +pad and m(T) = -pad. |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 388 | for (int i = 0; i < 2; ++i) { |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 389 | float T = T2[i]; // T is the point we are chopping around. |
| 390 | if ((T < 0 && outsideT0[i] >= 0) || (T > 1 && outsideT1[i] >= 0)) { |
| 391 | // The padding around T is completely out of range. No point solving for it. |
| 392 | continue; |
| 393 | } |
| 394 | |
| 395 | if (!hasDiscr) { |
| 396 | p = Sk2f(+.5f, -.5f) * pad; |
| 397 | q = (1.f/3) * (T2 - T1); |
| 398 | qqq = q*q*q; |
| 399 | discr = qqq*p*2 + p*p; |
| 400 | numRoots = (discr < 0).thenElse(3, 1); |
| 401 | D = T2 - q; |
| 402 | hasDiscr = true; |
| 403 | } |
| 404 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 405 | if (1 == numRoots[i]) { |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 406 | if (!hasOneRootVals) { |
| 407 | Sk2f r = qqq + p; |
| 408 | Sk2f s = r.abs() + discr.sqrt(); |
| 409 | R = (r > 0).thenElse(-s, s); |
| 410 | QQ = q*q; |
| 411 | hasOneRootVals = true; |
| 412 | } |
| 413 | |
| 414 | float A = cbrtf(R[i]); |
| 415 | float B = A != 0 ? QQ[i]/A : 0; |
| 416 | // When there is only one root, ine L chops from root..1, line M chops from 0..root. |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 417 | if (1 == i) { |
| 418 | chops->push_back(0); |
| 419 | } |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 420 | chops->push_back(A + B + D[i]); |
| 421 | if (0 == i) { |
| 422 | chops->push_back(1); |
| 423 | } |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 424 | continue; |
| 425 | } |
| 426 | |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 427 | if (!hasThreeRootVals) { |
| 428 | P = q.abs() * -2; |
| 429 | cosTheta3 = (q >= 0).thenElse(1, -1) + p / qqq.abs(); |
| 430 | hasThreeRootVals = true; |
| 431 | } |
| 432 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 433 | static constexpr float k2PiOver3 = 2 * SK_ScalarPI / 3; |
| 434 | float theta = std::acos(cosTheta3[i]) * (1.f/3); |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 435 | float roots[3] = {P[i] * std::cos(theta) + D[i], |
| 436 | P[i] * std::cos(theta + k2PiOver3) + D[i], |
| 437 | P[i] * std::cos(theta - k2PiOver3) + D[i]}; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 438 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 439 | // Sort the three roots. |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 440 | swap_if_greater(roots[0], roots[1]); |
| 441 | swap_if_greater(roots[1], roots[2]); |
| 442 | swap_if_greater(roots[0], roots[1]); |
| 443 | |
| 444 | // Line L chops around the first 2 roots, line M chops around the second 2. |
| 445 | chops->push_back_n(2, &roots[i]); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 449 | void GrCCGeometry::cubicTo(const SkPoint P[4], float inflectPad, float loopIntersectPad) { |
| 450 | SkASSERT(fBuildingContour); |
| 451 | SkASSERT(P[0] == fPoints.back()); |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 452 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 453 | // Don't crunch on the curve or inflate geometry if it is nearly flat (or just very small). |
| 454 | // Flat curves can break the math below. |
| 455 | if (are_collinear(P)) { |
| 456 | this->lineTo(P[3]); |
| 457 | return; |
| 458 | } |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 459 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 460 | Sk2f p0 = Sk2f::Load(P); |
| 461 | Sk2f p1 = Sk2f::Load(P+1); |
| 462 | Sk2f p2 = Sk2f::Load(P+2); |
| 463 | Sk2f p3 = Sk2f::Load(P+3); |
| 464 | |
| 465 | // Also detect near-quadratics ahead of time. |
| 466 | Sk2f tan0, tan1, c; |
| 467 | get_cubic_tangents(p0, p1, p2, p3, &tan0, &tan1); |
| 468 | if (is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, &c)) { |
| 469 | this->appendQuadratics(p0, c, p3); |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | double tt[2], ss[2], D[4]; |
| 474 | fCurrCubicType = SkClassifyCubic(P, tt, ss, D); |
| 475 | SkASSERT(!SkCubicIsDegenerate(fCurrCubicType)); |
| 476 | Sk2f t = Sk2f(static_cast<float>(tt[0]), static_cast<float>(tt[1])); |
| 477 | Sk2f s = Sk2f(static_cast<float>(ss[0]), static_cast<float>(ss[1])); |
| 478 | |
| 479 | SkMatrix CIT; |
| 480 | ExcludedTerm skipTerm = GrPathUtils::calcCubicInverseTransposePowerBasisMatrix(P, &CIT); |
| 481 | SkASSERT(ExcludedTerm::kNonInvertible != skipTerm); // Should have been caught above. |
| 482 | SkASSERT(0 == CIT[6]); |
| 483 | SkASSERT(0 == CIT[7]); |
| 484 | SkASSERT(1 == CIT[8]); |
| 485 | |
| 486 | SkSTArray<4, float> chops; |
| 487 | if (SkCubicType::kLoop != fCurrCubicType) { |
| 488 | find_chops_around_inflection_points(inflectPad, t, s, CIT, skipTerm, &chops); |
| 489 | } else { |
| 490 | find_chops_around_loop_intersection(loopIntersectPad, t, s, CIT, skipTerm, &chops); |
| 491 | } |
Chris Dalton | 5450ab1 | 2018-04-18 16:49:13 -0600 | [diff] [blame^] | 492 | if (4 == chops.count() && chops[1] >= chops[2]) { |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 493 | // This just the means the KLM roots are so close that their paddings overlap. We will |
| 494 | // approximate the entire middle section, but still have it chopped midway. For loops this |
| 495 | // chop guarantees the append code only sees convex segments. Otherwise, it means we are (at |
| 496 | // least almost) a cusp and the chop makes sure we get a sharp point. |
| 497 | Sk2f ts = t * SkNx_shuffle<1,0>(s); |
| 498 | chops[1] = chops[2] = (ts[0] + ts[1]) / (2*s[0]*s[1]); |
| 499 | } |
| 500 | |
| 501 | #ifdef SK_DEBUG |
| 502 | for (int i = 1; i < chops.count(); ++i) { |
| 503 | SkASSERT(chops[i] >= chops[i - 1]); |
| 504 | } |
| 505 | #endif |
| 506 | this->appendCubics(AppendCubicMode::kLiteral, p0, p1, p2, p3, chops.begin(), chops.count()); |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 507 | } |
| 508 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 509 | static inline void chop_cubic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, const Sk2f& p3, |
| 510 | float T, Sk2f* ab, Sk2f* abc, Sk2f* abcd, Sk2f* bcd, Sk2f* cd) { |
| 511 | Sk2f TT = T; |
| 512 | *ab = lerp(p0, p1, TT); |
| 513 | Sk2f bc = lerp(p1, p2, TT); |
| 514 | *cd = lerp(p2, p3, TT); |
| 515 | *abc = lerp(*ab, bc, TT); |
| 516 | *bcd = lerp(bc, *cd, TT); |
| 517 | *abcd = lerp(*abc, *bcd, TT); |
| 518 | } |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 519 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 520 | void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f& p1, |
| 521 | const Sk2f& p2, const Sk2f& p3, const float chops[], int numChops, |
| 522 | float localT0, float localT1) { |
| 523 | if (numChops) { |
| 524 | SkASSERT(numChops > 0); |
| 525 | int midChopIdx = numChops/2; |
| 526 | float T = chops[midChopIdx]; |
| 527 | // Chops alternate between literal and approximate mode. |
| 528 | AppendCubicMode rightMode = (AppendCubicMode)((bool)mode ^ (midChopIdx & 1) ^ 1); |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 529 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 530 | if (T <= localT0) { |
| 531 | // T is outside 0..1. Append the right side only. |
| 532 | this->appendCubics(rightMode, p0, p1, p2, p3, &chops[midChopIdx + 1], |
| 533 | numChops - midChopIdx - 1, localT0, localT1); |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | if (T >= localT1) { |
| 538 | // T is outside 0..1. Append the left side only. |
| 539 | this->appendCubics(mode, p0, p1, p2, p3, chops, midChopIdx, localT0, localT1); |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | float localT = (T - localT0) / (localT1 - localT0); |
| 544 | Sk2f p01, p02, pT, p11, p12; |
| 545 | chop_cubic(p0, p1, p2, p3, localT, &p01, &p02, &pT, &p11, &p12); |
| 546 | this->appendCubics(mode, p0, p01, p02, pT, chops, midChopIdx, localT0, T); |
| 547 | this->appendCubics(rightMode, pT, p11, p12, p3, &chops[midChopIdx + 1], |
| 548 | numChops - midChopIdx - 1, T, localT1); |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | this->appendCubics(mode, p0, p1, p2, p3); |
| 553 | } |
| 554 | |
| 555 | void GrCCGeometry::appendCubics(AppendCubicMode mode, const Sk2f& p0, const Sk2f& p1, |
| 556 | const Sk2f& p2, const Sk2f& p3, int maxSubdivisions) { |
| 557 | if ((p0 == p3).allTrue()) { |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | if (SkCubicType::kLoop != fCurrCubicType) { |
| 562 | // Serpentines and cusps are always monotonic after chopping around inflection points. |
| 563 | SkASSERT(!SkCubicIsDegenerate(fCurrCubicType)); |
| 564 | |
| 565 | if (AppendCubicMode::kApproximate == mode) { |
| 566 | // This section passes through an inflection point, so we can get away with a flat line. |
| 567 | // This can cause some curves to feel slightly more flat when inspected rigorously back |
| 568 | // and forth against another renderer, but for now this seems acceptable given the |
| 569 | // simplicity. |
| 570 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
| 571 | this->appendLine(p3); |
| 572 | return; |
| 573 | } |
| 574 | } else { |
| 575 | Sk2f tan0, tan1; |
| 576 | get_cubic_tangents(p0, p1, p2, p3, &tan0, &tan1); |
| 577 | |
| 578 | if (maxSubdivisions && !is_convex_curve_monotonic(p0, tan0, p3, tan1)) { |
| 579 | this->chopAndAppendCubicAtMidTangent(mode, p0, p1, p2, p3, tan0, tan1, |
| 580 | maxSubdivisions - 1); |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | if (AppendCubicMode::kApproximate == mode) { |
| 585 | Sk2f c; |
| 586 | if (!is_cubic_nearly_quadratic(p0, p1, p2, p3, tan0, tan1, &c) && maxSubdivisions) { |
| 587 | this->chopAndAppendCubicAtMidTangent(mode, p0, p1, p2, p3, tan0, tan1, |
| 588 | maxSubdivisions - 1); |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | this->appendMonotonicQuadratic(p0, c, p3); |
| 593 | return; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | // Don't send curves to the GPU if we know they are nearly flat (or just very small). |
| 598 | // Since the cubic segment is known to be convex at this point, our flatness check is simple. |
| 599 | if (are_collinear(p0, (p1 + p2) * .5f, p3)) { |
| 600 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
| 601 | this->appendLine(p3); |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
| 606 | p1.store(&fPoints.push_back()); |
| 607 | p2.store(&fPoints.push_back()); |
| 608 | p3.store(&fPoints.push_back()); |
| 609 | fVerbs.push_back(Verb::kMonotonicCubicTo); |
| 610 | ++fCurrContourTallies.fCubics; |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 611 | } |
| 612 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 613 | // Given a convex curve segment with the following order-2 tangent function: |
| 614 | // |
| 615 | // |C2x C2y| |
| 616 | // tan = some_scale * |dx/dt dy/dt| = |t^2 t 1| * |C1x C1y| |
| 617 | // |C0x C0y| |
| 618 | // |
| 619 | // This function finds the T value whose tangent angle is halfway between the tangents at T=0 and |
| 620 | // T=1 (tan0 and tan1). |
| 621 | static inline float find_midtangent(const Sk2f& tan0, const Sk2f& tan1, |
| 622 | float scale2, const Sk2f& C2, |
| 623 | float scale1, const Sk2f& C1, |
| 624 | float scale0, const Sk2f& C0) { |
| 625 | // Tangents point in the direction of increasing T, so tan0 and -tan1 both point toward the |
| 626 | // midtangent. 'n' will therefore bisect tan0 and -tan1, giving us the normal to the midtangent. |
| 627 | // |
| 628 | // n dot midtangent = 0 |
| 629 | // |
| 630 | Sk2f n = normalize(tan0) - normalize(tan1); |
| 631 | |
| 632 | // Find the T value at the midtangent. This is a simple quadratic equation: |
| 633 | // |
| 634 | // midtangent dot n = 0 |
| 635 | // |
| 636 | // (|t^2 t 1| * C) dot n = 0 |
| 637 | // |
| 638 | // |t^2 t 1| dot C*n = 0 |
| 639 | // |
| 640 | // First find coeffs = C*n. |
| 641 | Sk4f C[2]; |
| 642 | Sk2f::Store4(C, C2, C1, C0, 0); |
| 643 | Sk4f coeffs = C[0]*n[0] + C[1]*n[1]; |
| 644 | if (1 != scale2 || 1 != scale1 || 1 != scale0) { |
| 645 | coeffs *= Sk4f(scale2, scale1, scale0, 0); |
| 646 | } |
| 647 | |
| 648 | // Now solve the quadratic. |
| 649 | float a = coeffs[0], b = coeffs[1], c = coeffs[2]; |
| 650 | float discr = b*b - 4*a*c; |
| 651 | if (discr < 0) { |
| 652 | return 0; // This will only happen if the curve is a line. |
| 653 | } |
| 654 | |
| 655 | // The roots are q/a and c/q. Pick the one closer to T=.5. |
| 656 | float q = -.5f * (b + copysignf(std::sqrt(discr), b)); |
| 657 | float r = .5f*q*a; |
| 658 | return std::abs(q*q - r) < std::abs(a*c - r) ? q/a : c/q; |
| 659 | } |
| 660 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 661 | inline void GrCCGeometry::chopAndAppendCubicAtMidTangent(AppendCubicMode mode, const Sk2f& p0, |
| 662 | const Sk2f& p1, const Sk2f& p2, |
| 663 | const Sk2f& p3, const Sk2f& tan0, |
| 664 | const Sk2f& tan1, |
| 665 | int maxFutureSubdivisions) { |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 666 | float midT = find_midtangent(tan0, tan1, 3, p3 + (p1 - p2)*3 - p0, |
| 667 | 6, p0 - p1*2 + p2, |
| 668 | 3, p1 - p0); |
| 669 | // Use positive logic since NaN fails comparisons. (However midT should not be NaN since we cull |
| 670 | // near-flat cubics in cubicTo().) |
| 671 | if (!(midT > 0 && midT < 1)) { |
| 672 | // The cubic is flat. Otherwise there would be a real midtangent inside T=0..1. |
| 673 | this->appendLine(p3); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 674 | return; |
| 675 | } |
| 676 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 677 | Sk2f p01, p02, pT, p11, p12; |
| 678 | chop_cubic(p0, p1, p2, p3, midT, &p01, &p02, &pT, &p11, &p12); |
| 679 | this->appendCubics(mode, p0, p01, p02, pT, maxFutureSubdivisions); |
| 680 | this->appendCubics(mode, pT, p11, p12, p3, maxFutureSubdivisions); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 681 | } |
| 682 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 683 | void GrCCGeometry::conicTo(const SkPoint P[3], float w) { |
| 684 | SkASSERT(fBuildingContour); |
| 685 | SkASSERT(P[0] == fPoints.back()); |
| 686 | Sk2f p0 = Sk2f::Load(P); |
| 687 | Sk2f p1 = Sk2f::Load(P+1); |
| 688 | Sk2f p2 = Sk2f::Load(P+2); |
| 689 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 690 | Sk2f tan0 = p1 - p0; |
| 691 | Sk2f tan1 = p2 - p1; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 692 | |
| 693 | if (!is_convex_curve_monotonic(p0, tan0, p2, tan1)) { |
Chris Dalton | d8bae7d | 2018-04-19 13:13:25 -0600 | [diff] [blame] | 694 | // The derivative of a conic has a cumbersome order-4 denominator. However, this isn't |
| 695 | // necessary if we are only interested in a vector in the same *direction* as a given |
| 696 | // tangent line. Since the denominator scales dx and dy uniformly, we can throw it out |
| 697 | // completely after evaluating the derivative with the standard quotient rule. This leaves |
| 698 | // us with a simpler quadratic function that we use to find the midtangent. |
| 699 | float midT = find_midtangent(tan0, tan1, 1, (w - 1) * (p2 - p0), |
| 700 | 1, (p2 - p0) - 2*w*(p1 - p0), |
| 701 | 1, w*(p1 - p0)); |
| 702 | // Use positive logic since NaN fails comparisons. (However midT should not be NaN since we |
| 703 | // cull near-linear conics above. And while w=0 is flat, it's not a line and has valid |
| 704 | // midtangents.) |
| 705 | if (!(midT > 0 && midT < 1)) { |
| 706 | // The conic is flat. Otherwise there would be a real midtangent inside T=0..1. |
| 707 | this->appendLine(p2); |
| 708 | return; |
| 709 | } |
| 710 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 711 | // Chop the conic at midtangent to produce two monotonic segments. |
Chris Dalton | d8bae7d | 2018-04-19 13:13:25 -0600 | [diff] [blame] | 712 | Sk4f p3d0 = Sk4f(p0[0], p0[1], 1, 0); |
| 713 | Sk4f p3d1 = Sk4f(p1[0], p1[1], 1, 0) * w; |
| 714 | Sk4f p3d2 = Sk4f(p2[0], p2[1], 1, 0); |
| 715 | Sk4f midT4 = midT; |
| 716 | |
| 717 | Sk4f p3d01 = lerp(p3d0, p3d1, midT4); |
| 718 | Sk4f p3d12 = lerp(p3d1, p3d2, midT4); |
| 719 | Sk4f p3d012 = lerp(p3d01, p3d12, midT4); |
| 720 | |
| 721 | Sk2f midpoint = Sk2f(p3d012[0], p3d012[1]) / p3d012[2]; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 722 | Sk2f ww = Sk2f(p3d01[2], p3d12[2]) * Sk2f(p3d012[2]).rsqrt(); |
Chris Dalton | d8bae7d | 2018-04-19 13:13:25 -0600 | [diff] [blame] | 723 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 724 | this->appendMonotonicConic(p0, Sk2f(p3d01[0], p3d01[1]) / p3d01[2], midpoint, ww[0]); |
| 725 | this->appendMonotonicConic(midpoint, Sk2f(p3d12[0], p3d12[1]) / p3d12[2], p2, ww[1]); |
| 726 | return; |
| 727 | } |
| 728 | |
| 729 | this->appendMonotonicConic(p0, p1, p2, w); |
| 730 | } |
| 731 | |
| 732 | void GrCCGeometry::appendMonotonicConic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, float w) { |
Chris Dalton | d8bae7d | 2018-04-19 13:13:25 -0600 | [diff] [blame] | 733 | SkASSERT(w >= 0); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 734 | SkASSERT(fPoints.back() == SkPoint::Make(p0[0], p0[1])); |
| 735 | |
Chris Dalton | d8bae7d | 2018-04-19 13:13:25 -0600 | [diff] [blame] | 736 | Sk2f base = p2 - p0; |
| 737 | Sk2f baseAbs = base.abs(); |
| 738 | float baseWidth = baseAbs[0] + baseAbs[1]; |
| 739 | |
| 740 | // Find the height of the curve. Max height always occurs at T=.5 for conics. |
| 741 | Sk2f d = (p1 - p0) * SkNx_shuffle<1,0>(base); |
| 742 | float h1 = std::abs(d[1] - d[0]); // Height of p1 above the base. |
| 743 | float ht = h1*w, hs = 1 + w; // Height of the conic = ht/hs. |
| 744 | |
| 745 | if (ht < (baseWidth*hs) * kFlatnessThreshold) { // i.e. ht/hs < baseWidth * kFlatnessThreshold |
| 746 | // We are flat. (See rationale in are_collinear.) |
| 747 | this->appendLine(p2); |
| 748 | return; |
| 749 | } |
| 750 | |
| 751 | if (w > 1 && h1*hs - ht < baseWidth*hs) { // i.e. w > 1 && h1 - ht/hs < baseWidth |
| 752 | // If we get within 1px of p1 when w > 1, we will pick up artifacts from the implicit |
| 753 | // function's reflection. Chop at max height (T=.5) and draw a triangle instead. |
| 754 | Sk2f p1w = p1*w; |
| 755 | Sk2f ab = p0 + p1w; |
| 756 | Sk2f bc = p1w + p2; |
| 757 | Sk2f highpoint = (ab + bc) / (2*(1 + w)); |
| 758 | this->appendLine(highpoint); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 759 | this->appendLine(p2); |
| 760 | return; |
| 761 | } |
| 762 | |
| 763 | p1.store(&fPoints.push_back()); |
| 764 | p2.store(&fPoints.push_back()); |
| 765 | fConicWeights.push_back(w); |
| 766 | fVerbs.push_back(Verb::kMonotonicConicTo); |
| 767 | ++fCurrContourTallies.fConics; |
| 768 | } |
| 769 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 770 | GrCCGeometry::PrimitiveTallies GrCCGeometry::endContour() { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 771 | SkASSERT(fBuildingContour); |
| 772 | SkASSERT(fVerbs.count() >= fCurrContourTallies.fTriangles); |
| 773 | |
| 774 | // The fTriangles field currently contains this contour's starting verb index. We can now |
| 775 | // use it to calculate the size of the contour's fan. |
| 776 | int fanSize = fVerbs.count() - fCurrContourTallies.fTriangles; |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 777 | if (fPoints.back() == fCurrAnchorPoint) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 778 | --fanSize; |
| 779 | fVerbs.push_back(Verb::kEndClosedContour); |
| 780 | } else { |
| 781 | fVerbs.push_back(Verb::kEndOpenContour); |
| 782 | } |
| 783 | |
| 784 | fCurrContourTallies.fTriangles = SkTMax(fanSize - 2, 0); |
| 785 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 786 | SkDEBUGCODE(fBuildingContour = false); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 787 | return fCurrContourTallies; |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 788 | } |