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