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