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