• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "experimental/graphite/src/geom/IntersectionTree.h"
9 #include "include/utils/SkRandom.h"
10 #include "tests/Test.h"
11 
12 namespace skgpu {
13 
14 class SimpleIntersectionTree {
15 public:
add(SkRect rect)16     bool add(SkRect rect) {
17         for (const SkRect& r : fRects) {
18             if (r.intersects(rect)) {
19                 return false;
20             }
21         }
22         fRects.push_back(rect);
23         return true;
24     }
25 
26 private:
27     std::vector<SkRect> fRects;
28 };
29 
30 #define CHECK(A) REPORTER_ASSERT(reporter, A)
31 
DEF_GRAPHITE_TEST(skgpu_IntersectionTree,reporter)32 DEF_GRAPHITE_TEST(skgpu_IntersectionTree, reporter) {
33     SkRandom rand;
34     {
35         SimpleIntersectionTree simpleTree;
36         IntersectionTree tree;
37         for (int i = 0; i < 1000; ++i) {
38             Rect rect = Rect::XYWH(rand.nextRangeF(0, 500),
39                                    rand.nextRangeF(0, 500),
40                                    rand.nextRangeF(0, 70),
41                                    rand.nextRangeF(0, 70));
42             CHECK(tree.add(rect) == simpleTree.add({rect.left(),
43                                                    rect.top(),
44                                                    rect.right(),
45                                                    rect.bot()}));
46         }
47     }
48     {
49         SimpleIntersectionTree simpleTree;
50         IntersectionTree tree;
51         for (int i = 0; i < 100; ++i) {
52             Rect rect = Rect::XYWH(rand.nextRangeF(0, 500),
53                                    rand.nextRangeF(0, 500),
54                                    rand.nextRangeF(0, 200),
55                                    rand.nextRangeF(0, 200));
56             CHECK(tree.add(rect) == simpleTree.add({rect.left(),
57                                                    rect.top(),
58                                                    rect.right(),
59                                                    rect.bot()}));
60         }
61     }
62     {
63         SimpleIntersectionTree simpleTree;
64         IntersectionTree tree;
65         CHECK(tree.add(Rect(float2(-std::numeric_limits<float>::infinity()),
66                                    float2(std::numeric_limits<float>::infinity()))));
67         CHECK(!tree.add(Rect::WH(1,1)));
68         CHECK(!tree.add(Rect::WH(1,std::numeric_limits<float>::infinity())));
69         CHECK(tree.add(Rect::WH(0, 0)));
70         CHECK(tree.add(Rect::WH(-1, 1)));
71         CHECK(tree.add(Rect::WH(1, std::numeric_limits<float>::quiet_NaN())));
72     }
73 }
74 
75 }  // namespace skgpu
76