blob: ce139c3cbadf39227e7c03dfa26fe9f2ead2b714 [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 Daltonf6bf5162020-05-13 19:18:46 -060024void for_random_matrices(SkRandom* rand, std::function<void(const SkMatrix&)> f) {
25 SkMatrix m;
26 m.setIdentity();
27 f(m);
28
29 for (int i = -10; i <= 30; ++i) {
30 for (int j = -10; j <= 30; ++j) {
31 m.setScaleX(std::ldexp(1 + rand->nextF(), i));
32 m.setSkewX(0);
33 m.setSkewY(0);
34 m.setScaleY(std::ldexp(1 + rand->nextF(), j));
35 f(m);
36
37 m.setScaleX(std::ldexp(1 + rand->nextF(), i));
38 m.setSkewX(std::ldexp(1 + rand->nextF(), (j + i) / 2));
39 m.setSkewY(std::ldexp(1 + rand->nextF(), (j + i) / 2));
40 m.setScaleY(std::ldexp(1 + rand->nextF(), j));
41 f(m);
42 }
43 }
44}
45
46void for_random_beziers(int numPoints, SkRandom* rand, std::function<void(const SkPoint[])> f) {
47 SkASSERT(numPoints <= 4);
48 SkPoint pts[4];
49 for (int i = -10; i <= 30; ++i) {
50 for (int j = 0; j < numPoints; ++j) {
51 pts[j].set(std::ldexp(1 + rand->nextF(), i), std::ldexp(1 + rand->nextF(), i));
52 }
53 f(pts);
54 }
55}
56
57// Ensure the optimized "*_log2" versions return the same value as ceil(std::log2(f)).
58DEF_TEST(WangsFormula_log2, r) {
59 // Constructs a cubic such that the 'length' term in wang's formula == term.
60 //
61 // f = sqrt(k * length(max(abs(p0 - p1*2 + p2),
62 // abs(p1 - p2*2 + p3))));
63 auto setupCubicLengthTerm = [](int seed, SkPoint pts[], float term) {
64 memset(pts, 0, sizeof(SkPoint) * 4);
65
66 SkPoint term2d = (seed & 1) ?
67 SkPoint::Make(term, 0) : SkPoint::Make(.5f, std::sqrt(3)/2) * term;
68 seed >>= 1;
69
70 if (seed & 1) {
71 term2d.fX = -term2d.fX;
72 }
73 seed >>= 1;
74
75 if (seed & 1) {
76 std::swap(term2d.fX, term2d.fY);
77 }
78 seed >>= 1;
79
80 switch (seed % 4) {
81 case 0:
82 pts[0] = term2d;
83 pts[3] = term2d * .75f;
84 return;
85 case 1:
86 pts[1] = term2d * -.5f;
87 return;
88 case 2:
89 pts[1] = term2d * -.5f;
90 return;
91 case 3:
92 pts[3] = term2d;
93 pts[0] = term2d * .75f;
94 return;
95 }
96 };
97
98 // Constructs a quadratic such that the 'length' term in wang's formula == term.
99 //
100 // f = sqrt(k * length(p0 - p1*2 + p2));
101 auto setupQuadraticLengthTerm = [](int seed, SkPoint pts[], float term) {
102 memset(pts, 0, sizeof(SkPoint) * 3);
103
104 SkPoint term2d = (seed & 1) ?
105 SkPoint::Make(term, 0) : SkPoint::Make(.5f, std::sqrt(3)/2) * term;
106 seed >>= 1;
107
108 if (seed & 1) {
109 term2d.fX = -term2d.fX;
110 }
111 seed >>= 1;
112
113 if (seed & 1) {
114 std::swap(term2d.fX, term2d.fY);
115 }
116 seed >>= 1;
117
118 switch (seed % 3) {
119 case 0:
120 pts[0] = term2d;
121 return;
122 case 1:
123 pts[1] = term2d * -.5f;
124 return;
125 case 2:
126 pts[2] = term2d;
127 return;
128 }
129 };
130
131 for (int level = 0; level < 30; ++level) {
132 float epsilon = std::ldexp(SK_ScalarNearlyZero, level * 2);
133 SkPoint pts[4];
134
135 {
136 // Test cubic boundaries.
137 // f = sqrt(k * length(max(abs(p0 - p1*2 + p2),
138 // abs(p1 - p2*2 + p3))));
139 constexpr static float k = (3 * 2) / (8 * (1.f/kIntolerance));
140 float x = std::ldexp(1, level * 2) / k;
141 setupCubicLengthTerm(level << 1, pts, x - epsilon);
142 REPORTER_ASSERT(r,
143 std::ceil(std::log2(GrWangsFormula::cubic(kIntolerance, pts))) == level);
144 REPORTER_ASSERT(r, GrWangsFormula::cubic_log2(kIntolerance, pts) == level);
145 setupCubicLengthTerm(level << 1, pts, x + epsilon);
146 REPORTER_ASSERT(r,
147 std::ceil(std::log2(GrWangsFormula::cubic(kIntolerance, pts))) == level + 1);
148 REPORTER_ASSERT(r, GrWangsFormula::cubic_log2(kIntolerance, pts) == level + 1);
149 }
150
151 {
152 // Test quadratic boundaries.
153 // f = std::sqrt(k * Length(p0 - p1*2 + p2));
154 constexpr static float k = 2 / (8 * (1.f/kIntolerance));
155 float x = std::ldexp(1, level * 2) / k;
156 setupQuadraticLengthTerm(level << 1, pts, x - epsilon);
157 REPORTER_ASSERT(r,
158 std::ceil(std::log2(GrWangsFormula::quadratic(kIntolerance, pts))) == level);
159 REPORTER_ASSERT(r, GrWangsFormula::quadratic_log2(kIntolerance, pts) == level);
160 setupQuadraticLengthTerm(level << 1, pts, x + epsilon);
161 REPORTER_ASSERT(r,
162 std::ceil(std::log2(GrWangsFormula::quadratic(kIntolerance, pts))) == level+1);
163 REPORTER_ASSERT(r, GrWangsFormula::quadratic_log2(kIntolerance, pts) == level + 1);
164 }
165 }
166
167 auto check_cubic_log2 = [&](const SkPoint* pts) {
168 float f = std::max(1.f, GrWangsFormula::cubic(kIntolerance, pts));
169 int f_log2 = GrWangsFormula::cubic_log2(kIntolerance, pts);
170 REPORTER_ASSERT(r, SkScalarCeilToInt(std::log2(f)) == f_log2);
171 };
172
173 auto check_quadratic_log2 = [&](const SkPoint* pts) {
174 float f = std::max(1.f, GrWangsFormula::quadratic(kIntolerance, pts));
175 int f_log2 = GrWangsFormula::quadratic_log2(kIntolerance, pts);
176 REPORTER_ASSERT(r, SkScalarCeilToInt(std::log2(f)) == f_log2);
177 };
178
179 SkRandom rand;
180
181 for_random_matrices(&rand, [&](const SkMatrix& m) {
182 SkPoint pts[4];
183 m.mapPoints(pts, kSerp, 4);
184 check_cubic_log2(pts);
185
186 m.mapPoints(pts, kLoop, 4);
187 check_cubic_log2(pts);
188
189 m.mapPoints(pts, kQuad, 3);
190 check_quadratic_log2(pts);
191 });
192
193 for_random_beziers(4, &rand, [&](const SkPoint pts[]) {
194 check_cubic_log2(pts);
195 });
196
197 for_random_beziers(3, &rand, [&](const SkPoint pts[]) {
198 check_quadratic_log2(pts);
199 });
200}
201
202// Ensure using transformations gives the same result as pre-transforming all points.
203DEF_TEST(WangsFormula_vectorXforms, r) {
204 auto check_cubic_log2_with_transform = [&](const SkPoint* pts, const SkMatrix& m){
205 SkPoint ptsXformed[4];
206 m.mapPoints(ptsXformed, pts, 4);
207 int expected = GrWangsFormula::cubic_log2(kIntolerance, ptsXformed);
208 int actual = GrWangsFormula::cubic_log2(kIntolerance, pts, GrVectorXform(m));
209 REPORTER_ASSERT(r, actual == expected);
210 };
211
212 auto check_quadratic_log2_with_transform = [&](const SkPoint* pts, const SkMatrix& m) {
213 SkPoint ptsXformed[3];
214 m.mapPoints(ptsXformed, pts, 3);
215 int expected = GrWangsFormula::quadratic_log2(kIntolerance, ptsXformed);
216 int actual = GrWangsFormula::quadratic_log2(kIntolerance, pts, GrVectorXform(m));
217 REPORTER_ASSERT(r, actual == expected);
218 };
219
220 SkRandom rand;
221
222 for_random_matrices(&rand, [&](const SkMatrix& m) {
223 check_cubic_log2_with_transform(kSerp, m);
224 check_cubic_log2_with_transform(kLoop, m);
225 check_quadratic_log2_with_transform(kQuad, m);
226
227 for_random_beziers(4, &rand, [&](const SkPoint pts[]) {
228 check_cubic_log2_with_transform(pts, m);
229 });
230
231 for_random_beziers(3, &rand, [&](const SkPoint pts[]) {
232 check_quadratic_log2_with_transform(pts, m);
233 });
234 });
Chris Daltonb96995d2020-06-04 16:44:29 -0600235}
Chris Daltonf6bf5162020-05-13 19:18:46 -0600236
Chris Daltonb96995d2020-06-04 16:44:29 -0600237DEF_TEST(WangsFormula_worst_case_cubic, r) {
238 {
239 SkPoint worstP[] = {{0,0}, {100,100}, {0,0}, {0,0}};
240 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic(kIntolerance, 100, 100) ==
241 GrWangsFormula::cubic(kIntolerance, worstP));
242 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic_log2(kIntolerance, 100, 100) ==
243 GrWangsFormula::cubic_log2(kIntolerance, worstP));
244 }
245 {
246 SkPoint worstP[] = {{100,100}, {100,100}, {200,200}, {100,100}};
247 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic(kIntolerance, 100, 100) ==
248 GrWangsFormula::cubic(kIntolerance, worstP));
249 REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic_log2(kIntolerance, 100, 100) ==
250 GrWangsFormula::cubic_log2(kIntolerance, worstP));
251 }
252 auto check_worst_case_cubic = [&](const SkPoint* pts) {
253 SkRect bbox;
254 bbox.setBoundsNoCheck(pts, 4);
255 float worst = GrWangsFormula::worst_case_cubic(kIntolerance, bbox.width(), bbox.height());
256 int worst_log2 = GrWangsFormula::worst_case_cubic_log2(kIntolerance, bbox.width(),
257 bbox.height());
258 float actual = GrWangsFormula::cubic(kIntolerance, pts);
259 REPORTER_ASSERT(r, worst >= actual);
260 REPORTER_ASSERT(r, std::ceil(std::log2(std::max(1.f, worst))) == worst_log2);
261 SkASSERT(std::ceil(std::log2(std::max(1.f, worst))) == worst_log2);
262 };
263 SkRandom rand;
264 for (int i = 0; i < 100; ++i) {
265 for_random_beziers(4, &rand, [&](const SkPoint pts[]) {
266 check_worst_case_cubic(pts);
267 });
268 }
Chris Daltonf6bf5162020-05-13 19:18:46 -0600269}