blob: b701540c83b9fe297305edc8aab0579528f03664 [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 Dalton4dd3c8c2020-10-30 22:45:58 -060024static float wangs_formula_quadratic_reference_impl(float intolerance, const SkPoint p[3]) {
25 float k = (2 * 1) / 8.f * intolerance;
26 return sqrtf(k * (p[0] - p[1]*2 + p[2]).length());
Chris Daltonfc396a82020-09-23 20:11:26 -060027}
28
Chris Dalton4dd3c8c2020-10-30 22:45:58 -060029static float wangs_formula_cubic_reference_impl(float intolerance, const SkPoint p[4]) {
30 float k = (3 * 2) / 8.f * intolerance;
31 return sqrtf(k * std::max((p[0] - p[1]*2 + p[2]).length(),
32 (p[1] - p[2]*2 + p[3]).length()));
Chris Daltonfc396a82020-09-23 20:11:26 -060033}
34
35static void for_random_matrices(SkRandom* rand, std::function<void(const SkMatrix&)> f) {
Chris Daltonf6bf5162020-05-13 19:18:46 -060036 SkMatrix m;
37 m.setIdentity();
38 f(m);
39
40 for (int i = -10; i <= 30; ++i) {
41 for (int j = -10; j <= 30; ++j) {
42 m.setScaleX(std::ldexp(1 + rand->nextF(), i));
43 m.setSkewX(0);
44 m.setSkewY(0);
45 m.setScaleY(std::ldexp(1 + rand->nextF(), j));
46 f(m);
47
48 m.setScaleX(std::ldexp(1 + rand->nextF(), i));
49 m.setSkewX(std::ldexp(1 + rand->nextF(), (j + i) / 2));
50 m.setSkewY(std::ldexp(1 + rand->nextF(), (j + i) / 2));
51 m.setScaleY(std::ldexp(1 + rand->nextF(), j));
52 f(m);
53 }
54 }
55}
56
Chris Daltonfc396a82020-09-23 20:11:26 -060057static void for_random_beziers(int numPoints, SkRandom* rand,
58 std::function<void(const SkPoint[])> f) {
Chris Daltonf6bf5162020-05-13 19:18:46 -060059 SkASSERT(numPoints <= 4);
60 SkPoint pts[4];
61 for (int i = -10; i <= 30; ++i) {
62 for (int j = 0; j < numPoints; ++j) {
63 pts[j].set(std::ldexp(1 + rand->nextF(), i), std::ldexp(1 + rand->nextF(), i));
64 }
65 f(pts);
66 }
67}
68
69// Ensure the optimized "*_log2" versions return the same value as ceil(std::log2(f)).
70DEF_TEST(WangsFormula_log2, r) {
71 // Constructs a cubic such that the 'length' term in wang's formula == term.
72 //
73 // f = sqrt(k * length(max(abs(p0 - p1*2 + p2),
74 // abs(p1 - p2*2 + p3))));
75 auto setupCubicLengthTerm = [](int seed, SkPoint pts[], float term) {
76 memset(pts, 0, sizeof(SkPoint) * 4);
77
78 SkPoint term2d = (seed & 1) ?
79 SkPoint::Make(term, 0) : SkPoint::Make(.5f, std::sqrt(3)/2) * term;
80 seed >>= 1;
81
82 if (seed & 1) {
83 term2d.fX = -term2d.fX;
84 }
85 seed >>= 1;
86
87 if (seed & 1) {
88 std::swap(term2d.fX, term2d.fY);
89 }
90 seed >>= 1;
91
92 switch (seed % 4) {
93 case 0:
94 pts[0] = term2d;
95 pts[3] = term2d * .75f;
96 return;
97 case 1:
98 pts[1] = term2d * -.5f;
99 return;
100 case 2:
101 pts[1] = term2d * -.5f;
102 return;
103 case 3:
104 pts[3] = term2d;
105 pts[0] = term2d * .75f;
106 return;
107 }
108 };
109
110 // Constructs a quadratic such that the 'length' term in wang's formula == term.
111 //
112 // f = sqrt(k * length(p0 - p1*2 + p2));
113 auto setupQuadraticLengthTerm = [](int seed, SkPoint pts[], float term) {
114 memset(pts, 0, sizeof(SkPoint) * 3);
115
116 SkPoint term2d = (seed & 1) ?
117 SkPoint::Make(term, 0) : SkPoint::Make(.5f, std::sqrt(3)/2) * term;
118 seed >>= 1;
119
120 if (seed & 1) {
121 term2d.fX = -term2d.fX;
122 }
123 seed >>= 1;
124
125 if (seed & 1) {
126 std::swap(term2d.fX, term2d.fY);
127 }
128 seed >>= 1;
129
130 switch (seed % 3) {
131 case 0:
132 pts[0] = term2d;
133 return;
134 case 1:
135 pts[1] = term2d * -.5f;
136 return;
137 case 2:
138 pts[2] = term2d;
139 return;
140 }
141 };
142
Chris Daltonfc396a82020-09-23 20:11:26 -0600143 // GrWangsFormula::cubic and ::quadratic both use rsqrt instead of sqrt for speed. Linearization
144 // is all approximate anyway, so as long as we are within ~1/2 tessellation segment of the
145 // reference value we are good enough.
146 constexpr static float kTessellationTolerance = 1/128.f;
147
Chris Daltonf6bf5162020-05-13 19:18:46 -0600148 for (int level = 0; level < 30; ++level) {
149 float epsilon = std::ldexp(SK_ScalarNearlyZero, level * 2);
150 SkPoint pts[4];
151
152 {
153 // Test cubic boundaries.
154 // f = sqrt(k * length(max(abs(p0 - p1*2 + p2),
155 // abs(p1 - p2*2 + p3))));
156 constexpr static float k = (3 * 2) / (8 * (1.f/kIntolerance));
157 float x = std::ldexp(1, level * 2) / k;
158 setupCubicLengthTerm(level << 1, pts, x - epsilon);
Chris Daltonfc396a82020-09-23 20:11:26 -0600159 float referenceValue = wangs_formula_cubic_reference_impl(kIntolerance, pts);
160 REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level);
161 float c = GrWangsFormula::cubic(kIntolerance, pts);
162 REPORTER_ASSERT(r, SkScalarNearlyEqual(c/referenceValue, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600163 REPORTER_ASSERT(r, GrWangsFormula::cubic_log2(kIntolerance, pts) == level);
164 setupCubicLengthTerm(level << 1, pts, x + epsilon);
Chris Daltonfc396a82020-09-23 20:11:26 -0600165 referenceValue = wangs_formula_cubic_reference_impl(kIntolerance, pts);
166 REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level + 1);
167 c = GrWangsFormula::cubic(kIntolerance, pts);
168 REPORTER_ASSERT(r, SkScalarNearlyEqual(c/referenceValue, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600169 REPORTER_ASSERT(r, GrWangsFormula::cubic_log2(kIntolerance, pts) == level + 1);
170 }
171
172 {
173 // Test quadratic boundaries.
174 // f = std::sqrt(k * Length(p0 - p1*2 + p2));
175 constexpr static float k = 2 / (8 * (1.f/kIntolerance));
176 float x = std::ldexp(1, level * 2) / k;
177 setupQuadraticLengthTerm(level << 1, pts, x - epsilon);
Chris Daltonfc396a82020-09-23 20:11:26 -0600178 float referenceValue = wangs_formula_quadratic_reference_impl(kIntolerance, pts);
179 REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level);
180 float q = GrWangsFormula::quadratic(kIntolerance, pts);
181 REPORTER_ASSERT(r, SkScalarNearlyEqual(q/referenceValue, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600182 REPORTER_ASSERT(r, GrWangsFormula::quadratic_log2(kIntolerance, pts) == level);
183 setupQuadraticLengthTerm(level << 1, pts, x + epsilon);
Chris Daltonfc396a82020-09-23 20:11:26 -0600184 referenceValue = wangs_formula_quadratic_reference_impl(kIntolerance, pts);
185 REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level+1);
186 q = GrWangsFormula::quadratic(kIntolerance, pts);
187 REPORTER_ASSERT(r, SkScalarNearlyEqual(q/referenceValue, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600188 REPORTER_ASSERT(r, GrWangsFormula::quadratic_log2(kIntolerance, pts) == level + 1);
189 }
190 }
191
192 auto check_cubic_log2 = [&](const SkPoint* pts) {
Chris Daltonfc396a82020-09-23 20:11:26 -0600193 float f = std::max(1.f, wangs_formula_cubic_reference_impl(kIntolerance, pts));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600194 int f_log2 = GrWangsFormula::cubic_log2(kIntolerance, pts);
195 REPORTER_ASSERT(r, SkScalarCeilToInt(std::log2(f)) == f_log2);
Chris Daltonfc396a82020-09-23 20:11:26 -0600196 float c = std::max(1.f, GrWangsFormula::cubic(kIntolerance, pts));
197 REPORTER_ASSERT(r, SkScalarNearlyEqual(c/f, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600198 };
199
200 auto check_quadratic_log2 = [&](const SkPoint* pts) {
Chris Daltonfc396a82020-09-23 20:11:26 -0600201 float f = std::max(1.f, wangs_formula_quadratic_reference_impl(kIntolerance, pts));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600202 int f_log2 = GrWangsFormula::quadratic_log2(kIntolerance, pts);
203 REPORTER_ASSERT(r, SkScalarCeilToInt(std::log2(f)) == f_log2);
Chris Daltonfc396a82020-09-23 20:11:26 -0600204 float q = std::max(1.f, GrWangsFormula::quadratic(kIntolerance, pts));
205 REPORTER_ASSERT(r, SkScalarNearlyEqual(q/f, 1, kTessellationTolerance));
Chris Daltonf6bf5162020-05-13 19:18:46 -0600206 };
207
208 SkRandom rand;
209
210 for_random_matrices(&rand, [&](const SkMatrix& m) {
211 SkPoint pts[4];
212 m.mapPoints(pts, kSerp, 4);
213 check_cubic_log2(pts);
214
215 m.mapPoints(pts, kLoop, 4);
216 check_cubic_log2(pts);
217
218 m.mapPoints(pts, kQuad, 3);
219 check_quadratic_log2(pts);
220 });
221
222 for_random_beziers(4, &rand, [&](const SkPoint pts[]) {
223 check_cubic_log2(pts);
224 });
225
226 for_random_beziers(3, &rand, [&](const SkPoint pts[]) {
227 check_quadratic_log2(pts);
228 });
229}
230
231// Ensure using transformations gives the same result as pre-transforming all points.
232DEF_TEST(WangsFormula_vectorXforms, r) {
233 auto check_cubic_log2_with_transform = [&](const SkPoint* pts, const SkMatrix& m){
234 SkPoint ptsXformed[4];
235 m.mapPoints(ptsXformed, pts, 4);
236 int expected = GrWangsFormula::cubic_log2(kIntolerance, ptsXformed);
237 int actual = GrWangsFormula::cubic_log2(kIntolerance, pts, GrVectorXform(m));
238 REPORTER_ASSERT(r, actual == expected);
239 };
240
241 auto check_quadratic_log2_with_transform = [&](const SkPoint* pts, const SkMatrix& m) {
242 SkPoint ptsXformed[3];
243 m.mapPoints(ptsXformed, pts, 3);
244 int expected = GrWangsFormula::quadratic_log2(kIntolerance, ptsXformed);
245 int actual = GrWangsFormula::quadratic_log2(kIntolerance, pts, GrVectorXform(m));
246 REPORTER_ASSERT(r, actual == expected);
247 };
248
249 SkRandom rand;
250
251 for_random_matrices(&rand, [&](const SkMatrix& m) {
252 check_cubic_log2_with_transform(kSerp, m);
253 check_cubic_log2_with_transform(kLoop, m);
254 check_quadratic_log2_with_transform(kQuad, m);
255
256 for_random_beziers(4, &rand, [&](const SkPoint pts[]) {
257 check_cubic_log2_with_transform(pts, m);
258 });
259
260 for_random_beziers(3, &rand, [&](const SkPoint pts[]) {
261 check_quadratic_log2_with_transform(pts, m);
262 });
263 });
Chris Daltonb96995d2020-06-04 16:44:29 -0600264}
Chris Daltonf6bf5162020-05-13 19:18:46 -0600265
Chris Daltonb96995d2020-06-04 16:44:29 -0600266DEF_TEST(WangsFormula_worst_case_cubic, r) {
267 {
268 SkPoint worstP[] = {{0,0}, {100,100}, {0,0}, {0,0}};
269 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic(kIntolerance, 100, 100) ==
Chris Daltonfc396a82020-09-23 20:11:26 -0600270 wangs_formula_cubic_reference_impl(kIntolerance, worstP));
Chris Daltonb96995d2020-06-04 16:44:29 -0600271 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic_log2(kIntolerance, 100, 100) ==
272 GrWangsFormula::cubic_log2(kIntolerance, worstP));
273 }
274 {
275 SkPoint worstP[] = {{100,100}, {100,100}, {200,200}, {100,100}};
276 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic(kIntolerance, 100, 100) ==
Chris Daltonfc396a82020-09-23 20:11:26 -0600277 wangs_formula_cubic_reference_impl(kIntolerance, worstP));
Chris Daltonb96995d2020-06-04 16:44:29 -0600278 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic_log2(kIntolerance, 100, 100) ==
279 GrWangsFormula::cubic_log2(kIntolerance, worstP));
280 }
281 auto check_worst_case_cubic = [&](const SkPoint* pts) {
282 SkRect bbox;
283 bbox.setBoundsNoCheck(pts, 4);
284 float worst = GrWangsFormula::worst_case_cubic(kIntolerance, bbox.width(), bbox.height());
285 int worst_log2 = GrWangsFormula::worst_case_cubic_log2(kIntolerance, bbox.width(),
286 bbox.height());
Chris Daltonfc396a82020-09-23 20:11:26 -0600287 float actual = wangs_formula_cubic_reference_impl(kIntolerance, pts);
Chris Daltonb96995d2020-06-04 16:44:29 -0600288 REPORTER_ASSERT(r, worst >= actual);
289 REPORTER_ASSERT(r, std::ceil(std::log2(std::max(1.f, worst))) == worst_log2);
Chris Daltonb96995d2020-06-04 16:44:29 -0600290 };
291 SkRandom rand;
292 for (int i = 0; i < 100; ++i) {
293 for_random_beziers(4, &rand, [&](const SkPoint pts[]) {
294 check_worst_case_cubic(pts);
295 });
296 }
Chris Daltonf6bf5162020-05-13 19:18:46 -0600297}