1 /*
2 * Copyright 2021 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 "tests/Test.h"
9
10 #include "include/utils/SkRandom.h"
11 #include "src/gpu/tessellate/CullTest.h"
12
13 namespace skgpu {
14
15 const SkMatrix gMatrices[] = {
16 SkMatrix::I(),
17 SkMatrix::Translate(25, -1000),
18 SkMatrix::Scale(.5f, 1000.1f),
19 SkMatrix::MakeAll(1000.1f, .0f, -100,
20 0.0f, .5f, -3000,
21 0.0f, .0f, 1),
22 SkMatrix::MakeAll(0, 1, 0,
23 1, 0, 0,
24 0, 0, 1),
25 SkMatrix::MakeAll( 2, 7.0f, -100,
26 -8000, .5f, 2000,
27 0, .0f, 1),
28 };
29
DEF_TEST(CullTestTest,reporter)30 DEF_TEST(CullTestTest, reporter) {
31 SkRandom rand;
32 float l=10, t=2000, r=100, b=2064;
33 SkRect viewportRect{l, t, r, b};
34 float valuesL[4] = {l-20, l-10, l+10, l+20};
35 float valuesT[4] = {t-20, t-10, t+10, t+20};
36 float valuesR[4] = {r+20, r+10, r-10, r-20};
37 float valuesB[4] = {b+20, b+10, b-10, b-20};
38 for (SkMatrix m : gMatrices) {
39 CullTest cullTest(viewportRect, m);
40 SkMatrix inverse;
41 SkAssertResult(m.invert(&inverse));
42 for (const float* y : {valuesT, valuesB}) {
43 for (const float* x : {valuesL, valuesR}) {
44 for (int i = 0; i < 500; ++i) {
45 int mask = rand.nextU();
46 const SkPoint devPts[4] = {{x[(mask >> 0) & 3], y[(mask >> 2) & 3]},
47 {x[(mask >> 4) & 3], y[(mask >> 6) & 3]},
48 {x[(mask >> 8) & 3], y[(mask >> 10) & 3]},
49 {x[(mask >> 12) & 3], y[(mask >> 14) & 3]}};
50
51 SkPoint localPts[4];
52 inverse.mapPoints(localPts, devPts, 4);
53
54 REPORTER_ASSERT(reporter,
55 cullTest.isVisible(localPts[0]) ==
56 viewportRect.contains(devPts[0].fX, devPts[0].fY));
57
58 {
59 SkRect devBounds3;
60 devBounds3.setBounds(devPts, 3);
61 // Outset devBounds because SkRect::intersects returns false on empty, which is NOT
62 // the behavior we want.
63 devBounds3.outset(1e-3f, 1e-3f);
64 REPORTER_ASSERT(reporter,
65 cullTest.areVisible3(localPts) == viewportRect.intersects(devBounds3));
66 }
67
68 {
69 SkRect devBounds4;
70 devBounds4.setBounds(devPts, 4);
71 // Outset devBounds because SkRect::intersects returns false on empty, which is NOT
72 // the behavior we want.
73 devBounds4.outset(1e-3f, 1e-3f);
74 REPORTER_ASSERT(reporter,
75 cullTest.areVisible4(localPts) == viewportRect.intersects(devBounds4));
76 }
77 }}}
78 }
79 }
80
81 } // namespace skgpu
82