blob: ecf2359a1d0e079327f613bf1631daac4ab23d75 [file] [log] [blame]
Chris Daltonf6bf5162020-05-13 19:18:46 -06001/*
2 * Copyright 2020 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
8#include "include/utils/SkRandom.h"
9#include "src/core/SkGeometry.h"
10#include "src/gpu/tessellate/GrWangsFormula.h"
11#include "tests/Test.h"
12
13constexpr static int kIntolerance = 4; // 1/4 pixel max error.
14
15const SkPoint kSerp[4] = {
16 {285.625f, 499.687f}, {411.625f, 808.188f}, {1064.62f, 135.688f}, {1042.63f, 585.187f}};
17
18const SkPoint kLoop[4] = {
19 {635.625f, 614.687f}, {171.625f, 236.188f}, {1064.62f, 135.688f}, {516.625f, 570.187f}};
20
21const SkPoint kQuad[4] = {
22 {460.625f, 557.187f}, {707.121f, 209.688f}, {779.628f, 577.687f}};
23
Chris Daltonfc396a82020-09-23 20:11:26 -060024static float length(const Sk2f& v) {
25 Sk2f vv = v*v;
26 return SkScalarSqrt(vv[0] + vv[1]);
27}
28
29static float wangs_formula_quadratic_reference_impl(float intolerance, const SkPoint pts[4]) {
30 Sk2f p0 = Sk2f::Load(pts);
31 Sk2f p1 = Sk2f::Load(pts + 1);
32 Sk2f p2 = Sk2f::Load(pts + 2);
33 float k = GrWangsFormula::quadratic_k(intolerance);
34 return SkScalarSqrt(k * length(p0 - p1*2 + p2));
35}
36
37static float wangs_formula_cubic_reference_impl(float intolerance, const SkPoint pts[4]) {
38 Sk2f p0 = Sk2f::Load(pts);
39 Sk2f p1 = Sk2f::Load(pts + 1);
40 Sk2f p2 = Sk2f::Load(pts + 2);
41 Sk2f p3 = Sk2f::Load(pts + 3);
42 float k = GrWangsFormula::cubic_k(intolerance);
43 return SkScalarSqrt(k * length(Sk2f::Max((p0 - p1*2 + p2).abs(),
44 (p1 - p2*2 + p3).abs())));
45}
46
47static void for_random_matrices(SkRandom* rand, std::function<void(const SkMatrix&)> f) {
Chris Daltonf6bf5162020-05-13 19:18:46 -060048 SkMatrix m;
49 m.setIdentity();
50 f(m);
51
52 for (int i = -10; i <= 30; ++i) {
53 for (int j = -10; j <= 30; ++j) {
54 m.setScaleX(std::ldexp(1 + rand->nextF(), i));
55 m.setSkewX(0);
56 m.setSkewY(0);
57 m.setScaleY(std::ldexp(1 + rand->nextF(), j));
58 f(m);
59
60 m.setScaleX(std::ldexp(1 + rand->nextF(), i));
61 m.setSkewX(std::ldexp(1 + rand->nextF(), (j + i) / 2));
62 m.setSkewY(std::ldexp(1 + rand->nextF(), (j + i) / 2));
63 m.setScaleY(std::ldexp(1 + rand->nextF(), j));
64 f(m);
65 }
66 }
67}
68
Chris Daltonfc396a82020-09-23 20:11:26 -060069static void for_random_beziers(int numPoints, SkRandom* rand,
70 std::function<void(const SkPoint[])> f) {
Chris Daltonf6bf5162020-05-13 19:18:46 -060071 SkASSERT(numPoints <= 4);
72 SkPoint pts[4];
73 for (int i = -10; i <= 30; ++i) {
74 for (int j = 0; j < numPoints; ++j) {
75 pts[j].set(std::ldexp(1 + rand->nextF(), i), std::ldexp(1 + rand->nextF(), i));
76 }
77 f(pts);
78 }
79}
80
81// Ensure the optimized "*_log2" versions return the same value as ceil(std::log2(f)).
82DEF_TEST(WangsFormula_log2, r) {
83 // Constructs a cubic such that the 'length' term in wang's formula == term.
84 //
85 // f = sqrt(k * length(max(abs(p0 - p1*2 + p2),
86 // abs(p1 - p2*2 + p3))));
87 auto setupCubicLengthTerm = [](int seed, SkPoint pts[], float term) {
88 memset(pts, 0, sizeof(SkPoint) * 4);
89
90 SkPoint term2d = (seed & 1) ?
91 SkPoint::Make(term, 0) : SkPoint::Make(.5f, std::sqrt(3)/2) * term;
92 seed >>= 1;
93
94 if (seed & 1) {
95 term2d.fX = -term2d.fX;
96 }
97 seed >>= 1;
98
99 if (seed & 1) {
100 std::swap(term2d.fX, term2d.fY);
101 }
102 seed >>= 1;
103
104 switch (seed % 4) {
105 case 0:
106 pts[0] = term2d;
107 pts[3] = term2d * .75f;
108 return;
109 case 1:
110 pts[1] = term2d * -.5f;
111 return;
112 case 2:
113 pts[1] = term2d * -.5f;
114 return;
115 case 3:
116 pts[3] = term2d;
117 pts[0] = term2d * .75f;
118 return;
119 }
120 };
121
122 // Constructs a quadratic such that the 'length' term in wang's formula == term.
123 //
124 // f = sqrt(k * length(p0 - p1*2 + p2));
125 auto setupQuadraticLengthTerm = [](int seed, SkPoint pts[], float term) {
126 memset(pts, 0, sizeof(SkPoint) * 3);
127
128 SkPoint term2d = (seed & 1) ?
129 SkPoint::Make(term, 0) : SkPoint::Make(.5f, std::sqrt(3)/2) * term;
130 seed >>= 1;
131
132 if (seed & 1) {
133 term2d.fX = -term2d.fX;
134 }
135 seed >>= 1;
136
137 if (seed & 1) {
138 std::swap(term2d.fX, term2d.fY);
139 }
140 seed >>= 1;
141
142 switch (seed % 3) {
143 case 0:
144 pts[0] = term2d;
145 return;
146 case 1:
147 pts[1] = term2d * -.5f;
148 return;
149 case 2:
150 pts[2] = term2d;
151 return;
152 }
153 };
154
Chris Daltonfc396a82020-09-23 20:11:26 -0600155 // GrWangsFormula::cubic and ::quadratic both use rsqrt instead of sqrt for speed. Linearization
156 // is all approximate anyway, so as long as we are within ~1/2 tessellation segment of the
157 // reference value we are good enough.
158 constexpr static float kTessellationTolerance = 1/128.f;
159
Chris Daltonf6bf5162020-05-13 19:18:46 -0600160 for (int level = 0; level < 30; ++level) {
161 float epsilon = std::ldexp(SK_ScalarNearlyZero, level * 2);
162 SkPoint pts[4];
163
164 {
165 // Test cubic boundaries.
166 // f = sqrt(k * length(max(abs(p0 - p1*2 + p2),
167 // abs(p1 - p2*2 + p3))));
168 constexpr static float k = (3 * 2) / (8 * (1.f/kIntolerance));
169 float x = std::ldexp(1, level * 2) / k;
170 setupCubicLengthTerm(level << 1, pts, x - epsilon);
Chris Daltonfc396a82020-09-23 20:11:26 -0600171 float referenceValue = wangs_formula_cubic_reference_impl(kIntolerance, pts);
172 REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level);
173 float c = GrWangsFormula::cubic(kIntolerance, pts);
174 REPORTER_ASSERT(r, SkScalarNearlyEqual(c/referenceValue, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600175 REPORTER_ASSERT(r, GrWangsFormula::cubic_log2(kIntolerance, pts) == level);
176 setupCubicLengthTerm(level << 1, pts, x + epsilon);
Chris Daltonfc396a82020-09-23 20:11:26 -0600177 referenceValue = wangs_formula_cubic_reference_impl(kIntolerance, pts);
178 REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level + 1);
179 c = GrWangsFormula::cubic(kIntolerance, pts);
180 REPORTER_ASSERT(r, SkScalarNearlyEqual(c/referenceValue, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600181 REPORTER_ASSERT(r, GrWangsFormula::cubic_log2(kIntolerance, pts) == level + 1);
182 }
183
184 {
185 // Test quadratic boundaries.
186 // f = std::sqrt(k * Length(p0 - p1*2 + p2));
187 constexpr static float k = 2 / (8 * (1.f/kIntolerance));
188 float x = std::ldexp(1, level * 2) / k;
189 setupQuadraticLengthTerm(level << 1, pts, x - epsilon);
Chris Daltonfc396a82020-09-23 20:11:26 -0600190 float referenceValue = wangs_formula_quadratic_reference_impl(kIntolerance, pts);
191 REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level);
192 float q = GrWangsFormula::quadratic(kIntolerance, pts);
193 REPORTER_ASSERT(r, SkScalarNearlyEqual(q/referenceValue, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600194 REPORTER_ASSERT(r, GrWangsFormula::quadratic_log2(kIntolerance, pts) == level);
195 setupQuadraticLengthTerm(level << 1, pts, x + epsilon);
Chris Daltonfc396a82020-09-23 20:11:26 -0600196 referenceValue = wangs_formula_quadratic_reference_impl(kIntolerance, pts);
197 REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level+1);
198 q = GrWangsFormula::quadratic(kIntolerance, pts);
199 REPORTER_ASSERT(r, SkScalarNearlyEqual(q/referenceValue, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600200 REPORTER_ASSERT(r, GrWangsFormula::quadratic_log2(kIntolerance, pts) == level + 1);
201 }
202 }
203
204 auto check_cubic_log2 = [&](const SkPoint* pts) {
Chris Daltonfc396a82020-09-23 20:11:26 -0600205 float f = std::max(1.f, wangs_formula_cubic_reference_impl(kIntolerance, pts));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600206 int f_log2 = GrWangsFormula::cubic_log2(kIntolerance, pts);
207 REPORTER_ASSERT(r, SkScalarCeilToInt(std::log2(f)) == f_log2);
Chris Daltonfc396a82020-09-23 20:11:26 -0600208 float c = std::max(1.f, GrWangsFormula::cubic(kIntolerance, pts));
209 REPORTER_ASSERT(r, SkScalarNearlyEqual(c/f, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600210 };
211
212 auto check_quadratic_log2 = [&](const SkPoint* pts) {
Chris Daltonfc396a82020-09-23 20:11:26 -0600213 float f = std::max(1.f, wangs_formula_quadratic_reference_impl(kIntolerance, pts));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600214 int f_log2 = GrWangsFormula::quadratic_log2(kIntolerance, pts);
215 REPORTER_ASSERT(r, SkScalarCeilToInt(std::log2(f)) == f_log2);
Chris Daltonfc396a82020-09-23 20:11:26 -0600216 float q = std::max(1.f, GrWangsFormula::quadratic(kIntolerance, pts));
217 REPORTER_ASSERT(r, SkScalarNearlyEqual(q/f, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600218 };
219
220 SkRandom rand;
221
222 for_random_matrices(&rand, [&](const SkMatrix& m) {
223 SkPoint pts[4];
224 m.mapPoints(pts, kSerp, 4);
225 check_cubic_log2(pts);
226
227 m.mapPoints(pts, kLoop, 4);
228 check_cubic_log2(pts);
229
230 m.mapPoints(pts, kQuad, 3);
231 check_quadratic_log2(pts);
232 });
233
234 for_random_beziers(4, &rand, [&](const SkPoint pts[]) {
235 check_cubic_log2(pts);
236 });
237
238 for_random_beziers(3, &rand, [&](const SkPoint pts[]) {
239 check_quadratic_log2(pts);
240 });
241}
242
243// Ensure using transformations gives the same result as pre-transforming all points.
244DEF_TEST(WangsFormula_vectorXforms, r) {
245 auto check_cubic_log2_with_transform = [&](const SkPoint* pts, const SkMatrix& m){
246 SkPoint ptsXformed[4];
247 m.mapPoints(ptsXformed, pts, 4);
248 int expected = GrWangsFormula::cubic_log2(kIntolerance, ptsXformed);
249 int actual = GrWangsFormula::cubic_log2(kIntolerance, pts, GrVectorXform(m));
250 REPORTER_ASSERT(r, actual == expected);
251 };
252
253 auto check_quadratic_log2_with_transform = [&](const SkPoint* pts, const SkMatrix& m) {
254 SkPoint ptsXformed[3];
255 m.mapPoints(ptsXformed, pts, 3);
256 int expected = GrWangsFormula::quadratic_log2(kIntolerance, ptsXformed);
257 int actual = GrWangsFormula::quadratic_log2(kIntolerance, pts, GrVectorXform(m));
258 REPORTER_ASSERT(r, actual == expected);
259 };
260
261 SkRandom rand;
262
263 for_random_matrices(&rand, [&](const SkMatrix& m) {
264 check_cubic_log2_with_transform(kSerp, m);
265 check_cubic_log2_with_transform(kLoop, m);
266 check_quadratic_log2_with_transform(kQuad, m);
267
268 for_random_beziers(4, &rand, [&](const SkPoint pts[]) {
269 check_cubic_log2_with_transform(pts, m);
270 });
271
272 for_random_beziers(3, &rand, [&](const SkPoint pts[]) {
273 check_quadratic_log2_with_transform(pts, m);
274 });
275 });
Chris Daltonb96995d2020-06-04 16:44:29 -0600276}
Chris Daltonf6bf5162020-05-13 19:18:46 -0600277
Chris Daltonb96995d2020-06-04 16:44:29 -0600278DEF_TEST(WangsFormula_worst_case_cubic, r) {
279 {
280 SkPoint worstP[] = {{0,0}, {100,100}, {0,0}, {0,0}};
281 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic(kIntolerance, 100, 100) ==
Chris Daltonfc396a82020-09-23 20:11:26 -0600282 wangs_formula_cubic_reference_impl(kIntolerance, worstP));
Chris Daltonb96995d2020-06-04 16:44:29 -0600283 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic_log2(kIntolerance, 100, 100) ==
284 GrWangsFormula::cubic_log2(kIntolerance, worstP));
285 }
286 {
287 SkPoint worstP[] = {{100,100}, {100,100}, {200,200}, {100,100}};
288 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic(kIntolerance, 100, 100) ==
Chris Daltonfc396a82020-09-23 20:11:26 -0600289 wangs_formula_cubic_reference_impl(kIntolerance, worstP));
Chris Daltonb96995d2020-06-04 16:44:29 -0600290 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic_log2(kIntolerance, 100, 100) ==
291 GrWangsFormula::cubic_log2(kIntolerance, worstP));
292 }
293 auto check_worst_case_cubic = [&](const SkPoint* pts) {
294 SkRect bbox;
295 bbox.setBoundsNoCheck(pts, 4);
296 float worst = GrWangsFormula::worst_case_cubic(kIntolerance, bbox.width(), bbox.height());
297 int worst_log2 = GrWangsFormula::worst_case_cubic_log2(kIntolerance, bbox.width(),
298 bbox.height());
Chris Daltonfc396a82020-09-23 20:11:26 -0600299 float actual = wangs_formula_cubic_reference_impl(kIntolerance, pts);
Chris Daltonb96995d2020-06-04 16:44:29 -0600300 REPORTER_ASSERT(r, worst >= actual);
301 REPORTER_ASSERT(r, std::ceil(std::log2(std::max(1.f, worst))) == worst_log2);
Chris Daltonb96995d2020-06-04 16:44:29 -0600302 };
303 SkRandom rand;
304 for (int i = 0; i < 100; ++i) {
305 for_random_beziers(4, &rand, [&](const SkPoint pts[]) {
306 check_worst_case_cubic(pts);
307 });
308 }
Chris Daltonf6bf5162020-05-13 19:18:46 -0600309}