/src/skia/src/gpu/tessellate/Tessellation.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2021 Google LLC. |
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 | | #include "src/gpu/tessellate/Tessellation.h" |
8 | | |
9 | | #include "include/core/SkPath.h" |
10 | | #include "include/core/SkPathTypes.h" |
11 | | #include "include/core/SkRect.h" |
12 | | #include "include/private/base/SkFloatingPoint.h" |
13 | | #include "include/private/base/SkTArray.h" |
14 | | #include "src/base/SkUtils.h" |
15 | | #include "src/base/SkVx.h" |
16 | | #include "src/core/SkGeometry.h" |
17 | | #include "src/core/SkPathPriv.h" |
18 | | #include "src/gpu/tessellate/CullTest.h" |
19 | | #include "src/gpu/tessellate/WangsFormula.h" |
20 | | |
21 | | using namespace skia_private; |
22 | | |
23 | | namespace skgpu::tess { |
24 | | |
25 | | namespace { |
26 | | |
27 | | using float2 = skvx::float2; |
28 | | using float4 = skvx::float4; |
29 | | |
30 | | // This value only protects us against getting stuck in infinite recursion due to fp32 precision |
31 | | // issues. Mathematically, every curve should reduce to manageable visible sections in O(log N) |
32 | | // chops, where N is the the magnitude of its control points. |
33 | | // |
34 | | // But, to define a protective upper bound, a cubic can enter or exit the viewport as many as 6 |
35 | | // times. So we may need to refine the curve (via binary search chopping at T=.5) up to 6 times. |
36 | | // |
37 | | // Furthermore, chopping a cubic at T=.5 may only reduce its length by 1/8 (.5^3), so we may require |
38 | | // up to 6 chops in order to reduce the length by 1/2. |
39 | | constexpr static int kMaxChopsPerCurve = 128/*magnitude of +fp32_max - -fp32_max*/ * |
40 | | 6/*max number of chops to reduce the length by half*/ * |
41 | | 6/*max number of viewport boundary crosses*/; |
42 | | |
43 | | // Writes a new path, chopping as necessary so no verbs require more segments than |
44 | | // kMaxTessellationSegmentsPerCurve. Curves completely outside the viewport are flattened into |
45 | | // lines. |
46 | | class PathChopper { |
47 | | public: |
48 | | PathChopper(float tessellationPrecision, const SkMatrix& matrix, const SkRect& viewport) |
49 | | : fTessellationPrecision(tessellationPrecision) |
50 | | , fCullTest(viewport, matrix) |
51 | 0 | , fVectorXform(matrix) { |
52 | 0 | fPath.setIsVolatile(true); |
53 | 0 | } |
54 | | |
55 | 0 | SkPath path() const { return fPath; } |
56 | | |
57 | 0 | void moveTo(SkPoint p) { fPath.moveTo(p); } |
58 | 0 | void lineTo(const SkPoint p[2]) { fPath.lineTo(p[1]); } |
59 | 0 | void close() { fPath.close(); } |
60 | | |
61 | 0 | void quadTo(const SkPoint quad[3]) { |
62 | 0 | SkASSERT(fPointStack.empty()); |
63 | | // Use a heap stack to recursively chop the quad into manageable, on-screen segments. |
64 | 0 | fPointStack.push_back_n(3, quad); |
65 | 0 | int numChops = 0; |
66 | 0 | while (!fPointStack.empty()) { |
67 | 0 | const SkPoint* p = fPointStack.end() - 3; |
68 | 0 | if (!fCullTest.areVisible3(p)) { |
69 | 0 | fPath.lineTo(p[2]); |
70 | 0 | } else { |
71 | 0 | float n4 = wangs_formula::quadratic_p4(fTessellationPrecision, p, fVectorXform); |
72 | 0 | if (n4 > kMaxSegmentsPerCurve_p4 && numChops < kMaxChopsPerCurve) { |
73 | 0 | SkPoint chops[5]; |
74 | 0 | SkChopQuadAtHalf(p, chops); |
75 | 0 | fPointStack.pop_back_n(3); |
76 | 0 | fPointStack.push_back_n(3, chops+2); |
77 | 0 | fPointStack.push_back_n(3, chops); |
78 | 0 | ++numChops; |
79 | 0 | continue; |
80 | 0 | } |
81 | 0 | fPath.quadTo(p[1], p[2]); |
82 | 0 | } |
83 | 0 | fPointStack.pop_back_n(3); |
84 | 0 | } |
85 | 0 | } Unexecuted instantiation: Tessellation.cpp:skgpu::tess::(anonymous namespace)::PathChopper::quadTo(SkPoint const*) Unexecuted instantiation: Tessellation.cpp:skgpu::tess::(anonymous namespace)::PathChopper::quadTo(SkPoint const*) |
86 | | |
87 | 0 | void conicTo(const SkPoint conic[3], float weight) { |
88 | 0 | SkASSERT(fPointStack.empty()); |
89 | 0 | SkASSERT(fWeightStack.empty()); |
90 | | // Use a heap stack to recursively chop the conic into manageable, on-screen segments. |
91 | 0 | fPointStack.push_back_n(3, conic); |
92 | 0 | fWeightStack.push_back(weight); |
93 | 0 | int numChops = 0; |
94 | 0 | while (!fPointStack.empty()) { |
95 | 0 | const SkPoint* p = fPointStack.end() - 3; |
96 | 0 | float w = fWeightStack.back(); |
97 | 0 | if (!fCullTest.areVisible3(p)) { |
98 | 0 | fPath.lineTo(p[2]); |
99 | 0 | } else { |
100 | 0 | float n2 = wangs_formula::conic_p2(fTessellationPrecision, p, w, fVectorXform); |
101 | 0 | if (n2 > kMaxSegmentsPerCurve_p2 && numChops < kMaxChopsPerCurve) { |
102 | 0 | SkConic chops[2]; |
103 | 0 | if (!SkConic(p,w).chopAt(.5, chops)) { |
104 | 0 | SkPoint line[2] = {p[0], p[2]}; |
105 | 0 | this->lineTo(line); |
106 | 0 | continue; |
107 | 0 | } |
108 | 0 | fPointStack.pop_back_n(3); |
109 | 0 | fWeightStack.pop_back(); |
110 | 0 | fPointStack.push_back_n(3, chops[1].fPts); |
111 | 0 | fWeightStack.push_back(chops[1].fW); |
112 | 0 | fPointStack.push_back_n(3, chops[0].fPts); |
113 | 0 | fWeightStack.push_back(chops[0].fW); |
114 | 0 | ++numChops; |
115 | 0 | continue; |
116 | 0 | } |
117 | 0 | fPath.conicTo(p[1], p[2], w); |
118 | 0 | } |
119 | 0 | fPointStack.pop_back_n(3); |
120 | 0 | fWeightStack.pop_back(); |
121 | 0 | } |
122 | 0 | SkASSERT(fWeightStack.empty()); |
123 | 0 | } Unexecuted instantiation: Tessellation.cpp:skgpu::tess::(anonymous namespace)::PathChopper::conicTo(SkPoint const*, float) Unexecuted instantiation: Tessellation.cpp:skgpu::tess::(anonymous namespace)::PathChopper::conicTo(SkPoint const*, float) |
124 | | |
125 | 0 | void cubicTo(const SkPoint cubic[4]) { |
126 | 0 | SkASSERT(fPointStack.empty()); |
127 | | // Use a heap stack to recursively chop the cubic into manageable, on-screen segments. |
128 | 0 | fPointStack.push_back_n(4, cubic); |
129 | 0 | int numChops = 0; |
130 | 0 | while (!fPointStack.empty()) { |
131 | 0 | SkPoint* p = fPointStack.end() - 4; |
132 | 0 | if (!fCullTest.areVisible4(p)) { |
133 | 0 | fPath.lineTo(p[3]); |
134 | 0 | } else { |
135 | 0 | float n4 = wangs_formula::cubic_p4(fTessellationPrecision, p, fVectorXform); |
136 | 0 | if (n4 > kMaxSegmentsPerCurve_p4 && numChops < kMaxChopsPerCurve) { |
137 | 0 | SkPoint chops[7]; |
138 | 0 | SkChopCubicAtHalf(p, chops); |
139 | 0 | fPointStack.pop_back_n(4); |
140 | 0 | fPointStack.push_back_n(4, chops+3); |
141 | 0 | fPointStack.push_back_n(4, chops); |
142 | 0 | ++numChops; |
143 | 0 | continue; |
144 | 0 | } |
145 | 0 | fPath.cubicTo(p[1], p[2], p[3]); |
146 | 0 | } |
147 | 0 | fPointStack.pop_back_n(4); |
148 | 0 | } |
149 | 0 | } Unexecuted instantiation: Tessellation.cpp:skgpu::tess::(anonymous namespace)::PathChopper::cubicTo(SkPoint const*) Unexecuted instantiation: Tessellation.cpp:skgpu::tess::(anonymous namespace)::PathChopper::cubicTo(SkPoint const*) |
150 | | |
151 | | private: |
152 | | const float fTessellationPrecision; |
153 | | const CullTest fCullTest; |
154 | | const wangs_formula::VectorXform fVectorXform; |
155 | | SkPath fPath; |
156 | | |
157 | | // Used for stack-based recursion (instead of using the runtime stack). |
158 | | STArray<8, SkPoint> fPointStack; |
159 | | STArray<2, float> fWeightStack; |
160 | | }; |
161 | | |
162 | | } // namespace |
163 | | |
164 | | SkPath PreChopPathCurves(float tessellationPrecision, |
165 | | const SkPath& path, |
166 | | const SkMatrix& matrix, |
167 | 0 | const SkRect& viewport) { |
168 | | // If the viewport is exceptionally large, we could end up blowing out memory with an unbounded |
169 | | // number of of chops. Therefore, we require that the viewport is manageable enough that a fully |
170 | | // contained curve can be tessellated in kMaxTessellationSegmentsPerCurve or fewer. (Any larger |
171 | | // and that amount of pixels wouldn't fit in memory anyway.) |
172 | 0 | SkASSERT(wangs_formula::worst_case_cubic( |
173 | 0 | tessellationPrecision, |
174 | 0 | viewport.width(), |
175 | 0 | viewport.height()) <= kMaxSegmentsPerCurve); |
176 | 0 | PathChopper chopper(tessellationPrecision, matrix, viewport); |
177 | 0 | for (auto [verb, p, w] : SkPathPriv::Iterate(path)) { |
178 | 0 | switch (verb) { |
179 | 0 | case SkPathVerb::kMove: |
180 | 0 | chopper.moveTo(p[0]); |
181 | 0 | break; |
182 | 0 | case SkPathVerb::kLine: |
183 | 0 | chopper.lineTo(p); |
184 | 0 | break; |
185 | 0 | case SkPathVerb::kQuad: |
186 | 0 | chopper.quadTo(p); |
187 | 0 | break; |
188 | 0 | case SkPathVerb::kConic: |
189 | 0 | chopper.conicTo(p, *w); |
190 | 0 | break; |
191 | 0 | case SkPathVerb::kCubic: |
192 | 0 | chopper.cubicTo(p); |
193 | 0 | break; |
194 | 0 | case SkPathVerb::kClose: |
195 | 0 | chopper.close(); |
196 | 0 | break; |
197 | 0 | } |
198 | 0 | } |
199 | | // Must preserve the input path's fill type (see crbug.com/1472747) |
200 | 0 | SkPath chopped = chopper.path(); |
201 | 0 | chopped.setFillType(path.getFillType()); |
202 | 0 | return chopped; |
203 | 0 | } Unexecuted instantiation: skgpu::tess::PreChopPathCurves(float, SkPath const&, SkMatrix const&, SkRect const&) Unexecuted instantiation: skgpu::tess::PreChopPathCurves(float, SkPath const&, SkMatrix const&, SkRect const&) |
204 | | |
205 | 0 | int FindCubicConvex180Chops(const SkPoint pts[], float T[2], bool* areCusps) { |
206 | 0 | SkASSERT(pts); |
207 | 0 | SkASSERT(T); |
208 | 0 | SkASSERT(areCusps); |
209 | | |
210 | | // If a chop falls within a distance of "kEpsilon" from 0 or 1, throw it out. Tangents become |
211 | | // unstable when we chop too close to the boundary. This works out because the tessellation |
212 | | // shaders don't allow more than 2^10 parametric segments, and they snap the beginning and |
213 | | // ending edges at 0 and 1. So if we overstep an inflection or point of 180-degree rotation by a |
214 | | // fraction of a tessellation segment, it just gets snapped. |
215 | 0 | constexpr static float kEpsilon = 1.f / (1 << 11); |
216 | | // Floating-point representation of "1 - 2*kEpsilon". |
217 | 0 | constexpr static uint32_t kIEEE_one_minus_2_epsilon = (127 << 23) - 2 * (1 << (24 - 11)); |
218 | | // Unfortunately we don't have a way to static_assert this, but we can runtime assert that the |
219 | | // kIEEE_one_minus_2_epsilon bits are correct. |
220 | 0 | SkASSERT(sk_bit_cast<float>(kIEEE_one_minus_2_epsilon) == 1 - 2*kEpsilon); |
221 | |
|
222 | 0 | float2 p0 = sk_bit_cast<float2>(pts[0]); |
223 | 0 | float2 p1 = sk_bit_cast<float2>(pts[1]); |
224 | 0 | float2 p2 = sk_bit_cast<float2>(pts[2]); |
225 | 0 | float2 p3 = sk_bit_cast<float2>(pts[3]); |
226 | | |
227 | | // Find the cubic's power basis coefficients. These define the bezier curve as: |
228 | | // |
229 | | // |T^3| |
230 | | // Cubic(T) = x,y = |A 3B 3C| * |T^2| + P0 |
231 | | // |. . .| |T | |
232 | | // |
233 | | // And the tangent direction (scaled by a uniform 1/3) will be: |
234 | | // |
235 | | // |T^2| |
236 | | // Tangent_Direction(T) = dx,dy = |A 2B C| * |T | |
237 | | // |. . .| |1 | |
238 | | // |
239 | 0 | float2 C = p1 - p0; |
240 | 0 | float2 D = p2 - p1; |
241 | 0 | float2 E = p3 - p0; |
242 | 0 | float2 B = D - C; |
243 | 0 | float2 A = -3*D + E; |
244 | | |
245 | | // Now find the cubic's inflection function. There are inflections where F' x F'' == 0. |
246 | | // We formulate this as a quadratic equation: F' x F'' == aT^2 + bT + c == 0. |
247 | | // See: https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf |
248 | | // NOTE: We only need the roots, so a uniform scale factor does not affect the solution. |
249 | 0 | float a = cross(A,B); |
250 | 0 | float b = cross(A,C); |
251 | 0 | float c = cross(B,C); |
252 | 0 | float b_over_minus_2 = -.5f * b; |
253 | 0 | float discr_over_4 = b_over_minus_2*b_over_minus_2 - a*c; |
254 | | |
255 | | // If -cuspThreshold <= discr_over_4 <= cuspThreshold, it means the two roots are within |
256 | | // kEpsilon of one another (in parametric space). This is close enough for our purposes to |
257 | | // consider them a single cusp. |
258 | 0 | float cuspThreshold = a * (kEpsilon/2); |
259 | 0 | cuspThreshold *= cuspThreshold; |
260 | |
|
261 | 0 | if (discr_over_4 < -cuspThreshold) { |
262 | | // The curve does not inflect or cusp. This means it might rotate more than 180 degrees |
263 | | // instead. Chop were rotation == 180 deg. (This is the 2nd root where the tangent is |
264 | | // parallel to tan0.) |
265 | | // |
266 | | // Tangent_Direction(T) x tan0 == 0 |
267 | | // (AT^2 x tan0) + (2BT x tan0) + (C x tan0) == 0 |
268 | | // (A x C)T^2 + (2B x C)T + (C x C) == 0 [[because tan0 == P1 - P0 == C]] |
269 | | // bT^2 + 2cT + 0 == 0 [[because A x C == b, B x C == c]] |
270 | | // T = [0, -2c/b] |
271 | | // |
272 | | // NOTE: if C == 0, then C != tan0. But this is fine because the curve is definitely |
273 | | // convex-180 if any points are colocated, and T[0] will equal NaN which returns 0 chops. |
274 | 0 | *areCusps = false; |
275 | 0 | float root = sk_ieee_float_divide(c, b_over_minus_2); |
276 | | // Is "root" inside the range [kEpsilon, 1 - kEpsilon)? |
277 | 0 | if (sk_bit_cast<uint32_t>(root - kEpsilon) < kIEEE_one_minus_2_epsilon) { |
278 | 0 | T[0] = root; |
279 | 0 | return 1; |
280 | 0 | } |
281 | 0 | return 0; |
282 | 0 | } |
283 | | |
284 | 0 | *areCusps = (discr_over_4 <= cuspThreshold); |
285 | 0 | if (*areCusps) { |
286 | | // The two roots are close enough that we can consider them a single cusp. |
287 | 0 | if (a != 0 || b_over_minus_2 != 0 || c != 0) { |
288 | | // Pick the average of both roots. |
289 | 0 | float root = sk_ieee_float_divide(b_over_minus_2, a); |
290 | | // Is "root" inside the range [kEpsilon, 1 - kEpsilon)? |
291 | 0 | if (sk_bit_cast<uint32_t>(root - kEpsilon) < kIEEE_one_minus_2_epsilon) { |
292 | 0 | T[0] = root; |
293 | 0 | return 1; |
294 | 0 | } |
295 | 0 | return 0; |
296 | 0 | } |
297 | | |
298 | | // The curve is a flat line. The standard inflection function doesn't detect cusps from flat |
299 | | // lines. Find cusps by searching instead for points where the tangent is perpendicular to |
300 | | // tan0. This will find any cusp point. |
301 | | // |
302 | | // dot(tan0, Tangent_Direction(T)) == 0 |
303 | | // |
304 | | // |T^2| |
305 | | // tan0 * |A 2B C| * |T | == 0 |
306 | | // |. . .| |1 | |
307 | | // |
308 | 0 | float2 tan0 = skvx::if_then_else(C != 0, C, p2 - p0); |
309 | 0 | a = dot(tan0, A); |
310 | 0 | b_over_minus_2 = -dot(tan0, B); |
311 | 0 | c = dot(tan0, C); |
312 | 0 | discr_over_4 = std::max(b_over_minus_2*b_over_minus_2 - a*c, 0.f); |
313 | 0 | } |
314 | | |
315 | | // Solve our quadratic equation to find where to chop. See the quadratic formula from |
316 | | // Numerical Recipes in C. |
317 | 0 | float q = sqrtf(discr_over_4); |
318 | 0 | q = copysignf(q, b_over_minus_2); |
319 | 0 | q = q + b_over_minus_2; |
320 | 0 | float2 roots = float2{q,c} / float2{a,q}; |
321 | |
|
322 | 0 | auto inside = (roots > kEpsilon) & (roots < (1 - kEpsilon)); |
323 | 0 | if (inside[0]) { |
324 | 0 | if (inside[1] && roots[0] != roots[1]) { |
325 | 0 | if (roots[0] > roots[1]) { |
326 | 0 | roots = skvx::shuffle<1,0>(roots); // Sort. |
327 | 0 | } |
328 | 0 | roots.store(T); |
329 | 0 | return 2; |
330 | 0 | } |
331 | 0 | T[0] = roots[0]; |
332 | 0 | return 1; |
333 | 0 | } |
334 | 0 | if (inside[1]) { |
335 | 0 | T[0] = roots[1]; |
336 | 0 | return 1; |
337 | 0 | } |
338 | 0 | return 0; |
339 | 0 | } Unexecuted instantiation: skgpu::tess::FindCubicConvex180Chops(SkPoint const*, float*, bool*) Unexecuted instantiation: skgpu::tess::FindCubicConvex180Chops(SkPoint const*, float*, bool*) |
340 | | |
341 | | } // namespace skgpu::tess |