forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtessellator_unittests.cc
More file actions
501 lines (438 loc) · 19.7 KB
/
tessellator_unittests.cc
File metadata and controls
501 lines (438 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/testing/testing.h"
#include "gtest/gtest.h"
#include "impeller/geometry/geometry_asserts.h"
#include "impeller/geometry/path.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/tessellator/tessellator.h"
namespace impeller {
namespace testing {
TEST(TessellatorTest, TessellatorBuilderReturnsCorrectResultStatus) {
// Zero points.
{
// This will hit a DHCECK so the test can only run in non-debug builds.
#ifndef NDEBUG
#else
Tessellator t;
auto path = PathBuilder{}.TakePath(FillType::kOdd);
Tessellator::Result result = t.Tessellate(
path, 1.0f,
[](const float* vertices, size_t vertices_count,
const uint16_t* indices, size_t indices_count) { return true; });
ASSERT_EQ(result, Tessellator::Result::kInputError);
#endif // NDEBUG
}
// One point.
{
Tessellator t;
auto path = PathBuilder{}.LineTo({0, 0}).TakePath(FillType::kOdd);
Tessellator::Result result = t.Tessellate(
path, 1.0f,
[](const float* vertices, size_t vertices_count,
const uint16_t* indices, size_t indices_count) { return true; });
ASSERT_EQ(result, Tessellator::Result::kSuccess);
}
// Two points.
{
Tessellator t;
auto path = PathBuilder{}.AddLine({0, 0}, {0, 1}).TakePath(FillType::kOdd);
Tessellator::Result result = t.Tessellate(
path, 1.0f,
[](const float* vertices, size_t vertices_count,
const uint16_t* indices, size_t indices_count) { return true; });
ASSERT_EQ(result, Tessellator::Result::kSuccess);
}
// Many points.
{
Tessellator t;
PathBuilder builder;
for (int i = 0; i < 1000; i++) {
auto coord = i * 1.0f;
builder.AddLine({coord, coord}, {coord + 1, coord + 1});
}
auto path = builder.TakePath(FillType::kOdd);
Tessellator::Result result = t.Tessellate(
path, 1.0f,
[](const float* vertices, size_t vertices_count,
const uint16_t* indices, size_t indices_count) { return true; });
ASSERT_EQ(result, Tessellator::Result::kSuccess);
}
// Closure fails.
{
Tessellator t;
auto path = PathBuilder{}.AddLine({0, 0}, {0, 1}).TakePath(FillType::kOdd);
Tessellator::Result result = t.Tessellate(
path, 1.0f,
[](const float* vertices, size_t vertices_count,
const uint16_t* indices, size_t indices_count) { return false; });
ASSERT_EQ(result, Tessellator::Result::kInputError);
}
// More than uint16 points, odd fill mode.
{
Tessellator t;
PathBuilder builder = {};
for (auto i = 0; i < 1000; i++) {
builder.AddCircle(Point(i, i), 4);
}
auto path = builder.TakePath(FillType::kOdd);
bool no_indices = false;
size_t count = 0u;
Tessellator::Result result = t.Tessellate(
path, 1.0f,
[&no_indices, &count](const float* vertices, size_t vertices_count,
const uint16_t* indices, size_t indices_count) {
no_indices = indices == nullptr;
count = vertices_count;
return true;
});
ASSERT_TRUE(no_indices);
ASSERT_TRUE(count >= USHRT_MAX);
ASSERT_EQ(result, Tessellator::Result::kSuccess);
}
}
TEST(TessellatorTest, TessellateConvex) {
{
Tessellator t;
// Sanity check simple rectangle.
auto pts = t.TessellateConvex(
PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 10, 10)).TakePath(), 1.0);
std::vector<Point> expected = {{0, 0}, {10, 0}, {0, 10}, {10, 10}};
EXPECT_EQ(pts, expected);
}
{
Tessellator t;
auto pts = t.TessellateConvex(PathBuilder{}
.AddRect(Rect::MakeLTRB(0, 0, 10, 10))
.AddRect(Rect::MakeLTRB(20, 20, 30, 30))
.TakePath(),
1.0);
std::vector<Point> expected = {{0, 0}, {10, 0}, {0, 10}, {10, 10},
{10, 10}, {20, 20}, {20, 20}, {30, 20},
{20, 30}, {30, 30}};
EXPECT_EQ(pts, expected);
}
}
TEST(TessellatorTest, CircleVertexCounts) {
auto tessellator = std::make_shared<Tessellator>();
auto test = [&tessellator](const Matrix& transform, Scalar radius) {
auto generator = tessellator->FilledCircle(transform, {}, radius);
size_t quadrant_divisions = generator.GetVertexCount() / 4;
// Confirm the approximation error is within the currently accepted
// |kCircleTolerance| value advertised by |CircleTessellator|.
// (With an additional 1% tolerance for floating point rounding.)
double angle = kPiOver2 / quadrant_divisions;
Point first = {radius, 0};
Point next = {static_cast<Scalar>(cos(angle) * radius),
static_cast<Scalar>(sin(angle) * radius)};
Point midpoint = (first + next) * 0.5;
EXPECT_GE(midpoint.GetLength(),
radius - Tessellator::kCircleTolerance * 1.01)
<< ", transform = " << transform << ", radius = " << radius
<< ", divisions = " << quadrant_divisions;
};
test({}, 0.0);
test({}, 0.9);
test({}, 1.0);
test({}, 1.9);
test(Matrix::MakeScale(Vector2(2.0, 2.0)), 0.95);
test({}, 2.0);
test(Matrix::MakeScale(Vector2(2.0, 2.0)), 1.0);
test({}, 11.9);
test({}, 12.0);
test({}, 35.9);
for (int i = 36; i < 10000; i += 4) {
test({}, i);
}
}
TEST(TessellatorTest, FilledCircleTessellationVertices) {
auto tessellator = std::make_shared<Tessellator>();
auto test = [&tessellator](const Matrix& transform, const Point& center,
Scalar radius) {
auto generator = tessellator->FilledCircle(transform, center, radius);
EXPECT_EQ(generator.GetTriangleType(), PrimitiveType::kTriangleStrip);
auto vertex_count = generator.GetVertexCount();
auto vertices = std::vector<Point>();
generator.GenerateVertices([&vertices](const Point& p) { //
vertices.push_back(p);
});
EXPECT_EQ(vertices.size(), vertex_count);
ASSERT_EQ(vertex_count % 4, 0u);
auto quadrant_count = vertex_count / 4;
for (size_t i = 0; i < quadrant_count; i++) {
double angle = kPiOver2 * i / (quadrant_count - 1);
double degrees = angle * 180.0 / kPi;
double rsin = sin(angle) * radius;
double rcos = cos(angle) * radius;
EXPECT_POINT_NEAR(vertices[i * 2],
Point(center.x - rcos, center.y + rsin))
<< "vertex " << i << ", angle = " << degrees << std::endl;
EXPECT_POINT_NEAR(vertices[i * 2 + 1],
Point(center.x - rcos, center.y - rsin))
<< "vertex " << i << ", angle = " << degrees << std::endl;
EXPECT_POINT_NEAR(vertices[vertex_count - i * 2 - 1],
Point(center.x + rcos, center.y - rsin))
<< "vertex " << i << ", angle = " << degrees << std::endl;
EXPECT_POINT_NEAR(vertices[vertex_count - i * 2 - 2],
Point(center.x + rcos, center.y + rsin))
<< "vertex " << i << ", angle = " << degrees << std::endl;
}
};
test({}, {}, 2.0);
test({}, {10, 10}, 2.0);
test(Matrix::MakeScale({500.0, 500.0, 0.0}), {}, 2.0);
test(Matrix::MakeScale({0.002, 0.002, 0.0}), {}, 1000.0);
}
TEST(TessellatorTest, StrokedCircleTessellationVertices) {
auto tessellator = std::make_shared<Tessellator>();
auto test = [&tessellator](const Matrix& transform, const Point& center,
Scalar radius, Scalar half_width) {
ASSERT_GT(radius, half_width);
auto generator =
tessellator->StrokedCircle(transform, center, radius, half_width);
EXPECT_EQ(generator.GetTriangleType(), PrimitiveType::kTriangleStrip);
auto vertex_count = generator.GetVertexCount();
auto vertices = std::vector<Point>();
generator.GenerateVertices([&vertices](const Point& p) { //
vertices.push_back(p);
});
EXPECT_EQ(vertices.size(), vertex_count);
ASSERT_EQ(vertex_count % 4, 0u);
auto quadrant_count = vertex_count / 8;
// Test outer points first
for (size_t i = 0; i < quadrant_count; i++) {
double angle = kPiOver2 * i / (quadrant_count - 1);
double degrees = angle * 180.0 / kPi;
double rsin = sin(angle) * (radius + half_width);
double rcos = cos(angle) * (radius + half_width);
EXPECT_POINT_NEAR(vertices[i * 2],
Point(center.x - rcos, center.y - rsin))
<< "vertex " << i << ", angle = " << degrees << std::endl;
EXPECT_POINT_NEAR(vertices[quadrant_count * 2 + i * 2],
Point(center.x + rsin, center.y - rcos))
<< "vertex " << i << ", angle = " << degrees << std::endl;
EXPECT_POINT_NEAR(vertices[quadrant_count * 4 + i * 2],
Point(center.x + rcos, center.y + rsin))
<< "vertex " << i << ", angle = " << degrees << std::endl;
EXPECT_POINT_NEAR(vertices[quadrant_count * 6 + i * 2],
Point(center.x - rsin, center.y + rcos))
<< "vertex " << i << ", angle = " << degrees << std::endl;
}
// Then test innerer points
for (size_t i = 0; i < quadrant_count; i++) {
double angle = kPiOver2 * i / (quadrant_count - 1);
double degrees = angle * 180.0 / kPi;
double rsin = sin(angle) * (radius - half_width);
double rcos = cos(angle) * (radius - half_width);
EXPECT_POINT_NEAR(vertices[i * 2 + 1],
Point(center.x - rcos, center.y - rsin))
<< "vertex " << i << ", angle = " << degrees << std::endl;
EXPECT_POINT_NEAR(vertices[quadrant_count * 2 + i * 2 + 1],
Point(center.x + rsin, center.y - rcos))
<< "vertex " << i << ", angle = " << degrees << std::endl;
EXPECT_POINT_NEAR(vertices[quadrant_count * 4 + i * 2 + 1],
Point(center.x + rcos, center.y + rsin))
<< "vertex " << i << ", angle = " << degrees << std::endl;
EXPECT_POINT_NEAR(vertices[quadrant_count * 6 + i * 2 + 1],
Point(center.x - rsin, center.y + rcos))
<< "vertex " << i << ", angle = " << degrees << std::endl;
}
};
test({}, {}, 2.0, 1.0);
test({}, {}, 2.0, 0.5);
test({}, {10, 10}, 2.0, 1.0);
test(Matrix::MakeScale({500.0, 500.0, 0.0}), {}, 2.0, 1.0);
test(Matrix::MakeScale({0.002, 0.002, 0.0}), {}, 1000.0, 10.0);
}
TEST(TessellatorTest, RoundCapLineTessellationVertices) {
auto tessellator = std::make_shared<Tessellator>();
auto test = [&tessellator](const Matrix& transform, const Point& p0,
const Point& p1, Scalar radius) {
auto generator = tessellator->RoundCapLine(transform, p0, p1, radius);
EXPECT_EQ(generator.GetTriangleType(), PrimitiveType::kTriangleStrip);
auto vertex_count = generator.GetVertexCount();
auto vertices = std::vector<Point>();
generator.GenerateVertices([&vertices](const Point& p) { //
vertices.push_back(p);
});
EXPECT_EQ(vertices.size(), vertex_count);
ASSERT_EQ(vertex_count % 4, 0u);
Point along = p1 - p0;
Scalar length = along.GetLength();
if (length > 0) {
along *= radius / length;
} else {
along = {radius, 0};
}
Point across = {-along.y, along.x};
auto quadrant_count = vertex_count / 4;
for (size_t i = 0; i < quadrant_count; i++) {
double angle = kPiOver2 * i / (quadrant_count - 1);
double degrees = angle * 180.0 / kPi;
Point relative_along = along * cos(angle);
Point relative_across = across * sin(angle);
EXPECT_POINT_NEAR(vertices[i * 2], //
p0 - relative_along + relative_across)
<< "vertex " << i << ", angle = " << degrees << ", " //
<< "line = " << p0 << " => " << p1 << ", " //
<< "radius = " << radius << std::endl;
EXPECT_POINT_NEAR(vertices[i * 2 + 1], //
p0 - relative_along - relative_across)
<< "vertex " << i << ", angle = " << degrees << ", " //
<< "line = " << p0 << " => " << p1 << ", " //
<< "radius = " << radius << std::endl;
EXPECT_POINT_NEAR(vertices[vertex_count - i * 2 - 1], //
p1 + relative_along - relative_across)
<< "vertex " << i << ", angle = " << degrees << ", " //
<< "line = " << p0 << " => " << p1 << ", " //
<< "radius = " << radius << std::endl;
EXPECT_POINT_NEAR(vertices[vertex_count - i * 2 - 2], //
p1 + relative_along + relative_across)
<< "vertex " << i << ", angle = " << degrees << ", " //
<< "line = " << p0 << " => " << p1 << ", " //
<< "radius = " << radius << std::endl;
}
};
// Empty line should actually use the circle generator, but its
// results should match the same math as the round cap generator.
test({}, {0, 0}, {0, 0}, 10);
test({}, {0, 0}, {10, 0}, 2);
test({}, {10, 0}, {0, 0}, 2);
test({}, {0, 0}, {10, 10}, 2);
test(Matrix::MakeScale({500.0, 500.0, 0.0}), {0, 0}, {10, 0}, 2);
test(Matrix::MakeScale({500.0, 500.0, 0.0}), {10, 0}, {0, 0}, 2);
test(Matrix::MakeScale({500.0, 500.0, 0.0}), {0, 0}, {10, 10}, 2);
test(Matrix::MakeScale({0.002, 0.002, 0.0}), {0, 0}, {10, 0}, 2);
test(Matrix::MakeScale({0.002, 0.002, 0.0}), {10, 0}, {0, 0}, 2);
test(Matrix::MakeScale({0.002, 0.002, 0.0}), {0, 0}, {10, 10}, 2);
}
TEST(TessellatorTest, FilledEllipseTessellationVertices) {
auto tessellator = std::make_shared<Tessellator>();
auto test = [&tessellator](const Matrix& transform, const Rect& bounds) {
auto center = bounds.GetCenter();
auto half_size = bounds.GetSize() * 0.5f;
auto generator = tessellator->FilledEllipse(transform, bounds);
EXPECT_EQ(generator.GetTriangleType(), PrimitiveType::kTriangleStrip);
auto vertex_count = generator.GetVertexCount();
auto vertices = std::vector<Point>();
generator.GenerateVertices([&vertices](const Point& p) { //
vertices.push_back(p);
});
EXPECT_EQ(vertices.size(), vertex_count);
ASSERT_EQ(vertex_count % 4, 0u);
auto quadrant_count = vertex_count / 4;
for (size_t i = 0; i < quadrant_count; i++) {
double angle = kPiOver2 * i / (quadrant_count - 1);
double degrees = angle * 180.0 / kPi;
double rcos = cos(angle) * half_size.width;
double rsin = sin(angle) * half_size.height;
EXPECT_POINT_NEAR(vertices[i * 2],
Point(center.x - rcos, center.y + rsin))
<< "vertex " << i << ", angle = " << degrees << ", " //
<< "bounds = " << bounds << std::endl;
EXPECT_POINT_NEAR(vertices[i * 2 + 1],
Point(center.x - rcos, center.y - rsin))
<< "vertex " << i << ", angle = " << degrees << ", " //
<< "bounds = " << bounds << std::endl;
EXPECT_POINT_NEAR(vertices[vertex_count - i * 2 - 1],
Point(center.x + rcos, center.y - rsin))
<< "vertex " << i << ", angle = " << degrees << ", " //
<< "bounds = " << bounds << std::endl;
EXPECT_POINT_NEAR(vertices[vertex_count - i * 2 - 2],
Point(center.x + rcos, center.y + rsin))
<< "vertex " << i << ", angle = " << degrees << ", " //
<< "bounds = " << bounds << std::endl;
}
};
// Square bounds should actually use the circle generator, but its
// results should match the same math as the ellipse generator.
test({}, Rect::MakeXYWH(0, 0, 2, 2));
test({}, Rect::MakeXYWH(0, 0, 2, 3));
test({}, Rect::MakeXYWH(0, 0, 3, 2));
test({}, Rect::MakeXYWH(5, 10, 2, 3));
test({}, Rect::MakeXYWH(16, 7, 3, 2));
test(Matrix::MakeScale({500.0, 500.0, 0.0}), Rect::MakeXYWH(5, 10, 3, 2));
test(Matrix::MakeScale({500.0, 500.0, 0.0}), Rect::MakeXYWH(5, 10, 2, 3));
test(Matrix::MakeScale({0.002, 0.002, 0.0}),
Rect::MakeXYWH(5000, 10000, 3000, 2000));
test(Matrix::MakeScale({0.002, 0.002, 0.0}),
Rect::MakeXYWH(5000, 10000, 2000, 3000));
}
TEST(TessellatorTest, FilledRoundRectTessellationVertices) {
auto tessellator = std::make_shared<Tessellator>();
auto test = [&tessellator](const Matrix& transform, const Rect& bounds,
const Size& radii) {
FML_DCHECK(radii.width * 2 <= bounds.GetWidth()) << radii << bounds;
FML_DCHECK(radii.height * 2 <= bounds.GetHeight()) << radii << bounds;
Scalar middle_left = bounds.GetX() + radii.width;
Scalar middle_top = bounds.GetY() + radii.height;
Scalar middle_right = bounds.GetX() + bounds.GetWidth() - radii.width;
Scalar middle_bottom = bounds.GetY() + bounds.GetHeight() - radii.height;
auto generator = tessellator->FilledRoundRect(transform, bounds, radii);
EXPECT_EQ(generator.GetTriangleType(), PrimitiveType::kTriangleStrip);
auto vertex_count = generator.GetVertexCount();
auto vertices = std::vector<Point>();
generator.GenerateVertices([&vertices](const Point& p) { //
vertices.push_back(p);
});
EXPECT_EQ(vertices.size(), vertex_count);
ASSERT_EQ(vertex_count % 4, 0u);
auto quadrant_count = vertex_count / 4;
for (size_t i = 0; i < quadrant_count; i++) {
double angle = kPiOver2 * i / (quadrant_count - 1);
double degrees = angle * 180.0 / kPi;
double rcos = cos(angle) * radii.width;
double rsin = sin(angle) * radii.height;
EXPECT_POINT_NEAR(vertices[i * 2],
Point(middle_left - rcos, middle_bottom + rsin))
<< "vertex " << i << ", angle = " << degrees << ", " //
<< "bounds = " << bounds << std::endl;
EXPECT_POINT_NEAR(vertices[i * 2 + 1],
Point(middle_left - rcos, middle_top - rsin))
<< "vertex " << i << ", angle = " << degrees << ", " //
<< "bounds = " << bounds << std::endl;
EXPECT_POINT_NEAR(vertices[vertex_count - i * 2 - 1],
Point(middle_right + rcos, middle_top - rsin))
<< "vertex " << i << ", angle = " << degrees << ", " //
<< "bounds = " << bounds << std::endl;
EXPECT_POINT_NEAR(vertices[vertex_count - i * 2 - 2],
Point(middle_right + rcos, middle_bottom + rsin))
<< "vertex " << i << ", angle = " << degrees << ", " //
<< "bounds = " << bounds << std::endl;
}
};
// Both radii spanning the bounds should actually use the circle/ellipse
// generator, but their results should match the same math as the round
// rect generator.
test({}, Rect::MakeXYWH(0, 0, 20, 20), {10, 10});
// One radius spanning the bounds, but not the other will not match the
// round rect math if the generator transfers to circle/ellipse
test({}, Rect::MakeXYWH(0, 0, 20, 20), {10, 5});
test({}, Rect::MakeXYWH(0, 0, 20, 20), {5, 10});
test({}, Rect::MakeXYWH(0, 0, 20, 30), {2, 2});
test({}, Rect::MakeXYWH(0, 0, 30, 20), {2, 2});
test({}, Rect::MakeXYWH(5, 10, 20, 30), {2, 3});
test({}, Rect::MakeXYWH(16, 7, 30, 20), {2, 3});
test(Matrix::MakeScale({500.0, 500.0, 0.0}), Rect::MakeXYWH(5, 10, 30, 20),
{2, 3});
test(Matrix::MakeScale({500.0, 500.0, 0.0}), Rect::MakeXYWH(5, 10, 20, 30),
{2, 3});
test(Matrix::MakeScale({0.002, 0.002, 0.0}),
Rect::MakeXYWH(5000, 10000, 3000, 2000), {50, 70});
test(Matrix::MakeScale({0.002, 0.002, 0.0}),
Rect::MakeXYWH(5000, 10000, 2000, 3000), {50, 70});
}
#if !NDEBUG
TEST(TessellatorTest, ChecksConcurrentPolylineUsage) {
auto tessellator = std::make_shared<Tessellator>();
PathBuilder builder;
builder.AddLine({0, 0}, {100, 100});
auto path = builder.TakePath();
auto polyline = tessellator->CreateTempPolyline(path, 0.1);
EXPECT_DEBUG_DEATH(tessellator->CreateTempPolyline(path, 0.1),
"point_buffer_");
}
#endif // NDEBUG
} // namespace testing
} // namespace impeller