OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Benchmark.h" | 8 #include "Benchmark.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkRTree.h" | 10 #include "SkRTree.h" |
11 #include "SkRandom.h" | 11 #include "SkRandom.h" |
12 #include "SkString.h" | 12 #include "SkString.h" |
13 | 13 |
14 // confine rectangles to a smallish area, so queries generally hit something, an
d overlap occurs: | 14 // confine rectangles to a smallish area, so queries generally hit something, an
d overlap occurs: |
15 static const SkScalar GENERATE_EXTENTS = 1000.0f; | 15 static const SkScalar GENERATE_EXTENTS = 1000.0f; |
16 static const int NUM_BUILD_RECTS = 500; | 16 static const int NUM_BUILD_RECTS = 500; |
17 static const int NUM_QUERY_RECTS = 5000; | 17 static const int NUM_QUERY_RECTS = 5000; |
18 static const int GRID_WIDTH = 100; | 18 static const int GRID_WIDTH = 100; |
19 | 19 |
20 typedef SkRect (*MakeRectProc)(SkRandom&, int, int); | 20 typedef SkRect (*MakeRectProc)(SkRandom&, int, int); |
21 | 21 |
22 // Time how long it takes to build an R-Tree either bulk-loaded or not | 22 // Time how long it takes to build an R-Tree. |
23 class RTreeBuildBench : public Benchmark { | 23 class RTreeBuildBench : public Benchmark { |
24 public: | 24 public: |
25 RTreeBuildBench(const char* name, MakeRectProc proc, SkRTree* tree) | 25 RTreeBuildBench(const char* name, MakeRectProc proc) : fProc(proc) { |
26 : fTree(tree) | |
27 , fProc(proc) { | |
28 fName.printf("rtree_%s_build", name); | 26 fName.printf("rtree_%s_build", name); |
29 } | 27 } |
30 | 28 |
31 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { | 29 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
32 return backend == kNonRendering_Backend; | 30 return backend == kNonRendering_Backend; |
33 } | 31 } |
34 | 32 |
35 virtual ~RTreeBuildBench() { | |
36 fTree->unref(); | |
37 } | |
38 protected: | 33 protected: |
39 virtual const char* onGetName() SK_OVERRIDE { | 34 virtual const char* onGetName() SK_OVERRIDE { |
40 return fName.c_str(); | 35 return fName.c_str(); |
41 } | 36 } |
42 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { | 37 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
43 SkRandom rand; | 38 SkRandom rand; |
44 SkAutoTMalloc<SkRect> rects(NUM_BUILD_RECTS); | 39 SkAutoTMalloc<SkRect> rects(NUM_BUILD_RECTS); |
45 for (int i = 0; i < NUM_BUILD_RECTS; ++i) { | 40 for (int i = 0; i < NUM_BUILD_RECTS; ++i) { |
46 rects[i] = fProc(rand, i, NUM_BUILD_RECTS); | 41 rects[i] = fProc(rand, i, NUM_BUILD_RECTS); |
47 } | 42 } |
48 | 43 |
49 for (int i = 0; i < loops; ++i) { | 44 for (int i = 0; i < loops; ++i) { |
50 fTree->insert(&rects, NUM_BUILD_RECTS); | 45 SkRTree tree; |
| 46 tree.insert(&rects, NUM_BUILD_RECTS); |
51 SkASSERT(rects != NULL); // It'd break this bench if the tree took
ownership of rects. | 47 SkASSERT(rects != NULL); // It'd break this bench if the tree took
ownership of rects. |
52 fTree->clear(); | |
53 } | 48 } |
54 } | 49 } |
55 private: | 50 private: |
56 SkRTree* fTree; | |
57 MakeRectProc fProc; | 51 MakeRectProc fProc; |
58 SkString fName; | 52 SkString fName; |
59 typedef Benchmark INHERITED; | 53 typedef Benchmark INHERITED; |
60 }; | 54 }; |
61 | 55 |
62 // Time how long it takes to perform queries on an R-Tree, bulk-loaded or not | 56 // Time how long it takes to perform queries on an R-Tree. |
63 class RTreeQueryBench : public Benchmark { | 57 class RTreeQueryBench : public Benchmark { |
64 public: | 58 public: |
65 RTreeQueryBench(const char* name, MakeRectProc proc, SkRTree* tree) | 59 RTreeQueryBench(const char* name, MakeRectProc proc) : fProc(proc) { |
66 : fTree(tree) | |
67 , fProc(proc) { | |
68 fName.printf("rtree_%s_query", name); | 60 fName.printf("rtree_%s_query", name); |
69 } | 61 } |
70 | 62 |
71 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { | 63 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
72 return backend == kNonRendering_Backend; | 64 return backend == kNonRendering_Backend; |
73 } | 65 } |
74 | |
75 virtual ~RTreeQueryBench() { | |
76 fTree->unref(); | |
77 } | |
78 protected: | 66 protected: |
79 virtual const char* onGetName() SK_OVERRIDE { | 67 virtual const char* onGetName() SK_OVERRIDE { |
80 return fName.c_str(); | 68 return fName.c_str(); |
81 } | 69 } |
82 virtual void onPreDraw() SK_OVERRIDE { | 70 virtual void onPreDraw() SK_OVERRIDE { |
83 SkRandom rand; | 71 SkRandom rand; |
84 SkAutoTMalloc<SkRect> rects(NUM_QUERY_RECTS); | 72 SkAutoTMalloc<SkRect> rects(NUM_QUERY_RECTS); |
85 for (int i = 0; i < NUM_QUERY_RECTS; ++i) { | 73 for (int i = 0; i < NUM_QUERY_RECTS; ++i) { |
86 rects[i] = fProc(rand, i, NUM_QUERY_RECTS); | 74 rects[i] = fProc(rand, i, NUM_QUERY_RECTS); |
87 } | 75 } |
88 fTree->insert(&rects, NUM_QUERY_RECTS); | 76 fTree.insert(&rects, NUM_QUERY_RECTS); |
89 } | 77 } |
90 | 78 |
91 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { | 79 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
92 SkRandom rand; | 80 SkRandom rand; |
93 for (int i = 0; i < loops; ++i) { | 81 for (int i = 0; i < loops; ++i) { |
94 SkTDArray<unsigned> hits; | 82 SkTDArray<unsigned> hits; |
95 SkRect query; | 83 SkRect query; |
96 query.fLeft = rand.nextRangeF(0, GENERATE_EXTENTS); | 84 query.fLeft = rand.nextRangeF(0, GENERATE_EXTENTS); |
97 query.fTop = rand.nextRangeF(0, GENERATE_EXTENTS); | 85 query.fTop = rand.nextRangeF(0, GENERATE_EXTENTS); |
98 query.fRight = query.fLeft + 1 + rand.nextRangeF(0, GENERATE_EXTENT
S/2); | 86 query.fRight = query.fLeft + 1 + rand.nextRangeF(0, GENERATE_EXTENT
S/2); |
99 query.fBottom = query.fTop + 1 + rand.nextRangeF(0, GENERATE_EXTENT
S/2); | 87 query.fBottom = query.fTop + 1 + rand.nextRangeF(0, GENERATE_EXTENT
S/2); |
100 fTree->search(query, &hits); | 88 fTree.search(query, &hits); |
101 } | 89 } |
102 } | 90 } |
103 private: | 91 private: |
104 SkBBoxHierarchy* fTree; | 92 SkRTree fTree; |
105 MakeRectProc fProc; | 93 MakeRectProc fProc; |
106 SkString fName; | 94 SkString fName; |
107 typedef Benchmark INHERITED; | 95 typedef Benchmark INHERITED; |
108 }; | 96 }; |
109 | 97 |
110 static inline SkRect make_concentric_rects_increasing(SkRandom&, int index, int
numRects) { | |
111 SkRect out = SkRect::MakeWH(SkIntToScalar(index+1), SkIntToScalar(index+1)); | |
112 return out; | |
113 } | |
114 | |
115 static inline SkRect make_XYordered_rects(SkRandom& rand, int index, int numRect
s) { | 98 static inline SkRect make_XYordered_rects(SkRandom& rand, int index, int numRect
s) { |
116 SkRect out; | 99 SkRect out; |
117 out.fLeft = SkIntToScalar(index % GRID_WIDTH); | 100 out.fLeft = SkIntToScalar(index % GRID_WIDTH); |
118 out.fTop = SkIntToScalar(index / GRID_WIDTH); | 101 out.fTop = SkIntToScalar(index / GRID_WIDTH); |
119 out.fRight = out.fLeft + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/3); | 102 out.fRight = out.fLeft + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/3); |
120 out.fBottom = out.fTop + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/3); | 103 out.fBottom = out.fTop + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/3); |
121 return out; | 104 return out; |
122 } | 105 } |
123 static inline SkRect make_YXordered_rects(SkRandom& rand, int index, int numRect
s) { | 106 static inline SkRect make_YXordered_rects(SkRandom& rand, int index, int numRect
s) { |
124 SkRect out; | 107 SkRect out; |
125 out.fLeft = SkIntToScalar(index / GRID_WIDTH); | 108 out.fLeft = SkIntToScalar(index / GRID_WIDTH); |
126 out.fTop = SkIntToScalar(index % GRID_WIDTH); | 109 out.fTop = SkIntToScalar(index % GRID_WIDTH); |
127 out.fRight = out.fLeft + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/3); | 110 out.fRight = out.fLeft + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/3); |
128 out.fBottom = out.fTop + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/3); | 111 out.fBottom = out.fTop + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/3); |
129 return out; | 112 return out; |
130 } | 113 } |
131 | 114 |
132 static inline SkRect make_random_rects(SkRandom& rand, int index, int numRects)
{ | 115 static inline SkRect make_random_rects(SkRandom& rand, int index, int numRects)
{ |
133 SkRect out; | 116 SkRect out; |
134 out.fLeft = rand.nextRangeF(0, GENERATE_EXTENTS); | 117 out.fLeft = rand.nextRangeF(0, GENERATE_EXTENTS); |
135 out.fTop = rand.nextRangeF(0, GENERATE_EXTENTS); | 118 out.fTop = rand.nextRangeF(0, GENERATE_EXTENTS); |
136 out.fRight = out.fLeft + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/5); | 119 out.fRight = out.fLeft + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/5); |
137 out.fBottom = out.fTop + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/5); | 120 out.fBottom = out.fTop + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/5); |
138 return out; | 121 return out; |
139 } | 122 } |
140 | 123 |
| 124 static inline SkRect make_concentric_rects(SkRandom&, int index, int numRects) { |
| 125 return SkRect::MakeWH(SkIntToScalar(index+1), SkIntToScalar(index+1)); |
| 126 } |
| 127 |
141 /////////////////////////////////////////////////////////////////////////////// | 128 /////////////////////////////////////////////////////////////////////////////// |
142 | 129 |
143 DEF_BENCH(return SkNEW_ARGS(RTreeBuildBench, | 130 DEF_BENCH(return SkNEW_ARGS(RTreeBuildBench, ("XY", &make_XYordered_rect
s))); |
144 ("XY_sorted", &make_XYordered_rects, SkRTree::Create(5, 16)));) | 131 DEF_BENCH(return SkNEW_ARGS(RTreeBuildBench, ("YX", &make_YXordered_rect
s))); |
145 DEF_BENCH(return SkNEW_ARGS(RTreeBuildBench, | 132 DEF_BENCH(return SkNEW_ARGS(RTreeBuildBench, ("random", &make_random_rects))
); |
146 ("XY_unsorted", &make_XYordered_rects, SkRTree::Create(5, 16, 1, fal
se)));) | 133 DEF_BENCH(return SkNEW_ARGS(RTreeBuildBench, ("concentric", &make_concentric_rec
ts))); |
147 DEF_BENCH(return SkNEW_ARGS(RTreeBuildBench, | |
148 ("YX_sorted", &make_YXordered_rects, SkRTree::Create(5, 16)));) | |
149 DEF_BENCH(return SkNEW_ARGS(RTreeBuildBench, | |
150 ("YX_unsorted", &make_YXordered_rects, SkRTree::Create(5, 16, 1, fal
se)));) | |
151 DEF_BENCH(return SkNEW_ARGS(RTreeBuildBench, | |
152 ("random_sorted", &make_random_rects, SkRTree::Create(5, 16)));) | |
153 DEF_BENCH(return SkNEW_ARGS(RTreeBuildBench, | |
154 ("random_unsorted", &make_random_rects, SkRTree::Create(5, 16, 1, fa
lse)));) | |
155 DEF_BENCH(return SkNEW_ARGS(RTreeBuildBench, | |
156 ("concentric_sorted", &make_concentric_rects_increasing, SkRTree::Cr
eate(5, 16)));) | |
157 DEF_BENCH(return SkNEW_ARGS(RTreeBuildBench, | |
158 ("concentric_unsorted", | |
159 &make_concentric_rects_increasing, | |
160 SkRTree::Create(5, 16, 1, false)));) | |
161 | 134 |
162 DEF_BENCH(return SkNEW_ARGS(RTreeQueryBench, | 135 DEF_BENCH(return SkNEW_ARGS(RTreeQueryBench, ("XY", &make_XYordered_rect
s))); |
163 ("XY_sorted", &make_XYordered_rects, SkRTree::Create(5, 16)));) | 136 DEF_BENCH(return SkNEW_ARGS(RTreeQueryBench, ("YX", &make_YXordered_rect
s))); |
164 DEF_BENCH(return SkNEW_ARGS(RTreeQueryBench, | 137 DEF_BENCH(return SkNEW_ARGS(RTreeQueryBench, ("random", &make_random_rects))
); |
165 ("XY_unsorted", &make_XYordered_rects, SkRTree::Create(5, 16, 1, fal
se)));) | 138 DEF_BENCH(return SkNEW_ARGS(RTreeQueryBench, ("concentric", &make_concentric_rec
ts))); |
166 DEF_BENCH(return SkNEW_ARGS(RTreeQueryBench, | |
167 ("YX_sorted", &make_YXordered_rects, SkRTree::Create(5, 16)));) | |
168 DEF_BENCH(return SkNEW_ARGS(RTreeQueryBench, | |
169 ("YX_unsorted", &make_YXordered_rects, SkRTree::Create(5, 16, 1, fal
se)));) | |
170 DEF_BENCH(return SkNEW_ARGS(RTreeQueryBench, | |
171 ("random_sorted", &make_random_rects, SkRTree::Create(5, 16)));) | |
172 DEF_BENCH(return SkNEW_ARGS(RTreeQueryBench, | |
173 ("random_unsorted", &make_random_rects, SkRTree::Create(5, 16, 1, fa
lse)));) | |
174 DEF_BENCH(return SkNEW_ARGS(RTreeQueryBench, | |
175 ("concentric_sorted", &make_concentric_rects_increasing, SkRTree::Cr
eate(5, 16)));) | |
176 DEF_BENCH(return SkNEW_ARGS(RTreeQueryBench, | |
177 ("concentric_unsorted", | |
178 &make_concentric_rects_increasing, | |
179 SkRTree::Create(5, 16, 1, false)));) | |
OLD | NEW |