OLD | NEW |
1 | |
2 /* | 1 /* |
3 * Copyright 2012 Google Inc. | 2 * Copyright 2014 Google Inc. |
4 * | 3 * |
5 * 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 |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
8 | 7 |
9 #include "SkBenchmark.h" | 8 #include "SkBenchmark.h" |
10 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
11 #include "SkRTree.h" | 10 #include "SkQuadTree.h" |
12 #include "SkRandom.h" | 11 #include "SkRandom.h" |
13 #include "SkString.h" | 12 #include "SkString.h" |
14 | 13 |
15 // 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: |
16 static const int GENERATE_EXTENTS = 1000; | 15 static const int GENERATE_EXTENTS = 1000; |
17 static const int NUM_BUILD_RECTS = 500; | 16 static const int NUM_BUILD_RECTS = 500; |
18 static const int NUM_QUERY_RECTS = 5000; | 17 static const int NUM_QUERY_RECTS = 5000; |
19 static const int GRID_WIDTH = 100; | 18 static const int GRID_WIDTH = 100; |
| 19 static const SkIRect QUAD_TREE_BOUNDS = SkIRect::MakeLTRB( |
| 20 -GENERATE_EXTENTS, -GENERATE_EXTENTS, 2 * GENERATE_EXTENTS, 2 * GENERATE_EXT
ENTS); |
20 | 21 |
21 typedef SkIRect (*MakeRectProc)(SkRandom&, int, int); | 22 typedef SkIRect (*MakeRectProc)(SkRandom&, int, int); |
22 | 23 |
23 // Time how long it takes to build an R-Tree either bulk-loaded or not | 24 // Time how long it takes to build an QuadTree |
24 class BBoxBuildBench : public SkBenchmark { | 25 class BBoxBuildBench : public SkBenchmark { |
25 public: | 26 public: |
26 BBoxBuildBench(const char* name, MakeRectProc proc, bool bulkLoad, | 27 BBoxBuildBench(const char* name, MakeRectProc proc, SkBBoxHierarchy* tree) |
27 SkBBoxHierarchy* tree) | |
28 : fTree(tree) | 28 : fTree(tree) |
29 , fProc(proc) | 29 , fProc(proc) { |
30 , fBulkLoad(bulkLoad) { | 30 fName.append("quadtree_"); |
31 fName.append("rtree_"); | |
32 fName.append(name); | 31 fName.append(name); |
33 fName.append("_build"); | 32 fName.append("_build"); |
34 if (fBulkLoad) { | |
35 fName.append("_bulk"); | |
36 } | |
37 } | 33 } |
38 | 34 |
39 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { | 35 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
40 return backend == kNonRendering_Backend; | 36 return backend == kNonRendering_Backend; |
41 } | 37 } |
42 | 38 |
43 virtual ~BBoxBuildBench() { | 39 virtual ~BBoxBuildBench() { |
44 fTree->unref(); | 40 fTree->unref(); |
45 } | 41 } |
46 protected: | 42 protected: |
47 virtual const char* onGetName() SK_OVERRIDE { | 43 virtual const char* onGetName() SK_OVERRIDE { |
48 return fName.c_str(); | 44 return fName.c_str(); |
49 } | 45 } |
50 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { | 46 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
51 SkRandom rand; | 47 SkRandom rand; |
52 for (int i = 0; i < loops; ++i) { | 48 for (int i = 0; i < loops; ++i) { |
53 for (int j = 0; j < NUM_BUILD_RECTS; ++j) { | 49 for (int j = 0; j < NUM_BUILD_RECTS; ++j) { |
54 fTree->insert(reinterpret_cast<void*>(j), fProc(rand, j, NUM_BUI
LD_RECTS), | 50 fTree->insert(reinterpret_cast<void*>(j), fProc(rand, j, NUM_BUI
LD_RECTS), |
55 fBulkLoad); | 51 false); |
56 } | 52 } |
57 fTree->flushDeferredInserts(); | |
58 fTree->clear(); | 53 fTree->clear(); |
59 } | 54 } |
60 } | 55 } |
61 private: | 56 private: |
62 SkBBoxHierarchy* fTree; | 57 SkBBoxHierarchy* fTree; |
63 MakeRectProc fProc; | 58 MakeRectProc fProc; |
64 SkString fName; | 59 SkString fName; |
65 bool fBulkLoad; | |
66 typedef SkBenchmark INHERITED; | 60 typedef SkBenchmark INHERITED; |
67 }; | 61 }; |
68 | 62 |
69 // Time how long it takes to perform queries on an R-Tree, bulk-loaded or not | 63 // Time how long it takes to perform queries on an QuadTree |
70 class BBoxQueryBench : public SkBenchmark { | 64 class BBoxQueryBench : public SkBenchmark { |
71 public: | 65 public: |
72 enum QueryType { | 66 enum QueryType { |
73 kSmall_QueryType, // small queries | 67 kSmall_QueryType, // small queries |
74 kLarge_QueryType, // large queries | 68 kLarge_QueryType, // large queries |
75 kRandom_QueryType,// randomly sized queries | 69 kRandom_QueryType,// randomly sized queries |
76 kFull_QueryType // queries that cover everything | 70 kFull_QueryType // queries that cover everything |
77 }; | 71 }; |
78 | 72 |
79 BBoxQueryBench(const char* name, MakeRectProc proc, bool bulkLoad, | 73 BBoxQueryBench(const char* name, MakeRectProc proc, |
80 QueryType q, SkBBoxHierarchy* tree) | 74 QueryType q, SkBBoxHierarchy* tree) |
81 : fTree(tree) | 75 : fTree(tree) |
82 , fProc(proc) | 76 , fProc(proc) |
83 , fBulkLoad(bulkLoad) | |
84 , fQuery(q) { | 77 , fQuery(q) { |
85 fName.append("rtree_"); | 78 fName.append("quadtree_"); |
86 fName.append(name); | 79 fName.append(name); |
87 fName.append("_query"); | 80 fName.append("_query"); |
88 if (fBulkLoad) { | |
89 fName.append("_bulk"); | |
90 } | |
91 } | 81 } |
92 | 82 |
93 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { | 83 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
94 return backend == kNonRendering_Backend; | 84 return backend == kNonRendering_Backend; |
95 } | 85 } |
96 | 86 |
97 virtual ~BBoxQueryBench() { | 87 virtual ~BBoxQueryBench() { |
98 fTree->unref(); | 88 fTree->unref(); |
99 } | 89 } |
100 protected: | 90 protected: |
101 virtual const char* onGetName() SK_OVERRIDE { | 91 virtual const char* onGetName() SK_OVERRIDE { |
102 return fName.c_str(); | 92 return fName.c_str(); |
103 } | 93 } |
104 virtual void onPreDraw() SK_OVERRIDE { | 94 virtual void onPreDraw() SK_OVERRIDE { |
105 SkRandom rand; | 95 SkRandom rand; |
106 for (int j = 0; j < NUM_QUERY_RECTS; ++j) { | 96 for (int j = 0; j < NUM_QUERY_RECTS; ++j) { |
107 fTree->insert(reinterpret_cast<void*>(j), | 97 fTree->insert(reinterpret_cast<void*>(j), |
108 fProc(rand, j, NUM_QUERY_RECTS), | 98 fProc(rand, j, NUM_QUERY_RECTS), |
109 fBulkLoad); | 99 false); |
110 } | 100 } |
111 fTree->flushDeferredInserts(); | 101 fTree->flushDeferredInserts(); |
112 } | 102 } |
113 | 103 |
114 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { | 104 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
115 SkRandom rand; | 105 SkRandom rand; |
116 for (int i = 0; i < loops; ++i) { | 106 for (int i = 0; i < loops; ++i) { |
117 SkTDArray<void*> hits; | 107 SkTDArray<void*> hits; |
118 SkIRect query; | 108 SkIRect query; |
119 switch(fQuery) { | 109 switch(fQuery) { |
(...skipping 23 matching lines...) Expand all Loading... |
143 query.fBottom = query.fTop + 1 + rand.nextU() % (GENERATE_EX
TENTS / 2); | 133 query.fBottom = query.fTop + 1 + rand.nextU() % (GENERATE_EX
TENTS / 2); |
144 break; | 134 break; |
145 }; | 135 }; |
146 fTree->search(query, &hits); | 136 fTree->search(query, &hits); |
147 } | 137 } |
148 } | 138 } |
149 private: | 139 private: |
150 SkBBoxHierarchy* fTree; | 140 SkBBoxHierarchy* fTree; |
151 MakeRectProc fProc; | 141 MakeRectProc fProc; |
152 SkString fName; | 142 SkString fName; |
153 bool fBulkLoad; | |
154 QueryType fQuery; | 143 QueryType fQuery; |
155 typedef SkBenchmark INHERITED; | 144 typedef SkBenchmark INHERITED; |
156 }; | 145 }; |
157 | 146 |
158 static inline SkIRect make_concentric_rects_increasing(SkRandom&, int index, int
numRects) { | 147 static inline SkIRect make_concentric_rects_increasing(SkRandom&, int index, int
numRects) { |
159 SkIRect out = {0, 0, index + 1, index + 1}; | 148 SkIRect out = {0, 0, index + 1, index + 1}; |
160 return out; | 149 return out; |
161 } | 150 } |
162 | 151 |
163 static inline SkIRect make_XYordered_rects(SkRandom& rand, int index, int numRec
ts) { | 152 static inline SkIRect make_XYordered_rects(SkRandom& rand, int index, int numRec
ts) { |
164 SkIRect out; | 153 SkIRect out; |
165 out.fLeft = index % GRID_WIDTH; | 154 out.fLeft = index % GRID_WIDTH; |
166 out.fTop = index / GRID_WIDTH; | 155 out.fTop = index / GRID_WIDTH; |
167 out.fRight = out.fLeft + 1 + rand.nextU() % (GENERATE_EXTENTS / 3); | 156 out.fRight = out.fLeft + 1 + rand.nextU() % (GENERATE_EXTENTS / 3); |
168 out.fBottom = out.fTop + 1 + rand.nextU() % (GENERATE_EXTENTS / 3); | 157 out.fBottom = out.fTop + 1 + rand.nextU() % (GENERATE_EXTENTS / 3); |
169 return out; | 158 return out; |
170 } | 159 } |
| 160 |
171 static inline SkIRect make_YXordered_rects(SkRandom& rand, int index, int numRec
ts) { | 161 static inline SkIRect make_YXordered_rects(SkRandom& rand, int index, int numRec
ts) { |
172 SkIRect out; | 162 SkIRect out; |
173 out.fLeft = index / GRID_WIDTH; | 163 out.fLeft = index / GRID_WIDTH; |
174 out.fTop = index % GRID_WIDTH; | 164 out.fTop = index % GRID_WIDTH; |
175 out.fRight = out.fLeft + 1 + rand.nextU() % (GENERATE_EXTENTS / 3); | 165 out.fRight = out.fLeft + 1 + rand.nextU() % (GENERATE_EXTENTS / 3); |
176 out.fBottom = out.fTop + 1 + rand.nextU() % (GENERATE_EXTENTS / 3); | 166 out.fBottom = out.fTop + 1 + rand.nextU() % (GENERATE_EXTENTS / 3); |
177 return out; | 167 return out; |
178 } | 168 } |
179 | 169 |
180 static inline SkIRect make_random_rects(SkRandom& rand, int index, int numRects)
{ | 170 static inline SkIRect make_random_rects(SkRandom& rand, int index, int numRects)
{ |
181 SkIRect out; | 171 SkIRect out; |
182 out.fLeft = rand.nextS() % GENERATE_EXTENTS; | 172 out.fLeft = rand.nextS() % GENERATE_EXTENTS; |
183 out.fTop = rand.nextS() % GENERATE_EXTENTS; | 173 out.fTop = rand.nextS() % GENERATE_EXTENTS; |
184 out.fRight = out.fLeft + 1 + rand.nextU() % (GENERATE_EXTENTS / 5); | 174 out.fRight = out.fLeft + 1 + rand.nextU() % (GENERATE_EXTENTS / 5); |
185 out.fBottom = out.fTop + 1 + rand.nextU() % (GENERATE_EXTENTS / 5); | 175 out.fBottom = out.fTop + 1 + rand.nextU() % (GENERATE_EXTENTS / 5); |
186 return out; | 176 return out; |
187 } | 177 } |
188 | 178 |
189 /////////////////////////////////////////////////////////////////////////////// | 179 /////////////////////////////////////////////////////////////////////////////// |
190 | 180 |
191 DEF_BENCH( | 181 DEF_BENCH( |
192 return SkNEW_ARGS(BBoxBuildBench, ("XYordered", &make_XYordered_rects, false
, | 182 return SkNEW_ARGS(BBoxBuildBench, ("XYordered", &make_XYordered_rects, |
193 SkRTree::Create(5, 16))); | 183 SkQuadTree::Create(QUAD_TREE_BOUNDS))); |
194 ) | 184 ) |
195 DEF_BENCH( | 185 DEF_BENCH( |
196 return SkNEW_ARGS(BBoxBuildBench, ("XYordered", &make_XYordered_rects, true, | 186 return SkNEW_ARGS(BBoxQueryBench, ("XYordered", &make_XYordered_rects, |
197 SkRTree::Create(5, 16))); | 187 BBoxQueryBench::kRandom_QueryType, SkQuadTree::Create(QUAD
_TREE_BOUNDS))); |
198 ) | 188 ) |
199 DEF_BENCH( | 189 DEF_BENCH( |
200 return SkNEW_ARGS(BBoxBuildBench, ("(unsorted)XYordered", &make_XYordered_re
cts, true, | 190 return SkNEW_ARGS(BBoxBuildBench, ("YXordered", &make_YXordered_rects, |
201 SkRTree::Create(5, 16, 1, false))); | 191 SkQuadTree::Create(QUAD_TREE_BOUNDS))); |
202 ) | 192 ) |
203 DEF_BENCH( | 193 DEF_BENCH( |
204 return SkNEW_ARGS(BBoxQueryBench, ("XYordered", &make_XYordered_rects, true, | 194 return SkNEW_ARGS(BBoxQueryBench, ("YXordered", &make_YXordered_rects, |
205 BBoxQueryBench::kRandom_QueryType, SkRTree::Create(5, 16))
); | 195 BBoxQueryBench::kRandom_QueryType, SkQuadTree::Create(QUAD
_TREE_BOUNDS))); |
206 ) | 196 ) |
207 DEF_BENCH( | 197 DEF_BENCH( |
208 return SkNEW_ARGS(BBoxQueryBench, ("(unsorted)XYordered", &make_XYordered_re
cts, true, | 198 return SkNEW_ARGS(BBoxBuildBench, ("random", &make_random_rects, |
209 BBoxQueryBench::kRandom_QueryType, SkRTree::Create(5, 16,
1, false))); | 199 SkQuadTree::Create(QUAD_TREE_BOUNDS))); |
210 ) | |
211 | |
212 DEF_BENCH( | |
213 return SkNEW_ARGS(BBoxBuildBench, ("YXordered", &make_YXordered_rects, false
, | |
214 SkRTree::Create(5, 16))); | |
215 ) | 200 ) |
216 DEF_BENCH( | 201 DEF_BENCH( |
217 return SkNEW_ARGS(BBoxBuildBench, ("YXordered", &make_YXordered_rects, true, | 202 return SkNEW_ARGS(BBoxQueryBench, ("random", &make_random_rects, |
218 SkRTree::Create(5, 16))); | 203 BBoxQueryBench::kRandom_QueryType, SkQuadTree::Create(QUAD
_TREE_BOUNDS))); |
219 ) | 204 ) |
220 DEF_BENCH( | 205 DEF_BENCH( |
221 return SkNEW_ARGS(BBoxBuildBench, ("(unsorted)YXordered", &make_YXordered_re
cts, true, | 206 return SkNEW_ARGS(BBoxBuildBench, ("concentric", &make_concentric_rects_incr
easing, |
222 SkRTree::Create(5, 16, 1, false))); | 207 SkQuadTree::Create(QUAD_TREE_BOUNDS))); |
223 ) | 208 ) |
224 DEF_BENCH( | 209 DEF_BENCH( |
225 return SkNEW_ARGS(BBoxQueryBench, ("YXordered", &make_YXordered_rects, true, | 210 return SkNEW_ARGS(BBoxQueryBench, ("concentric", &make_concentric_rects_incr
easing, |
226 BBoxQueryBench::kRandom_QueryType, SkRTree::Create(5, 16))
); | 211 BBoxQueryBench::kRandom_QueryType, SkQuadTree::Create(QUAD
_TREE_BOUNDS))); |
227 ) | 212 ) |
228 DEF_BENCH( | |
229 return SkNEW_ARGS(BBoxQueryBench, ("(unsorted)YXordered", &make_YXordered_re
cts, true, | |
230 BBoxQueryBench::kRandom_QueryType, SkRTree::Create(5, 16,
1, false))); | |
231 ) | |
232 | |
233 DEF_BENCH( | |
234 return SkNEW_ARGS(BBoxBuildBench, ("random", &make_random_rects, false, | |
235 SkRTree::Create(5, 16))); | |
236 ) | |
237 DEF_BENCH( | |
238 return SkNEW_ARGS(BBoxBuildBench, ("random", &make_random_rects, true, | |
239 SkRTree::Create(5, 16))); | |
240 ) | |
241 DEF_BENCH( | |
242 return SkNEW_ARGS(BBoxBuildBench, ("(unsorted)random", &make_random_rects, t
rue, | |
243 SkRTree::Create(5, 16, 1, false))); | |
244 ) | |
245 DEF_BENCH( | |
246 return SkNEW_ARGS(BBoxQueryBench, ("random", &make_random_rects, true, | |
247 BBoxQueryBench::kRandom_QueryType, SkRTree::Create(5, 16))
); | |
248 ) | |
249 DEF_BENCH( | |
250 return SkNEW_ARGS(BBoxQueryBench, ("(unsorted)random", &make_random_rects, t
rue, | |
251 BBoxQueryBench::kRandom_QueryType, SkRTree::Create(5, 16,
1, false))); | |
252 ) | |
253 | |
254 DEF_BENCH( | |
255 return SkNEW_ARGS(BBoxBuildBench, ("concentric", | |
256 &make_concentric_rects_increasing, true, SkRTree::Create(5
, 16))); | |
257 ) | |
258 DEF_BENCH( | |
259 return SkNEW_ARGS(BBoxBuildBench, ("(unsorted)concentric", | |
260 &make_concentric_rects_increasing, true, SkRTree::Create(5
, 16, 1, false))); | |
261 ) | |
262 DEF_BENCH( | |
263 return SkNEW_ARGS(BBoxQueryBench, ("concentric", &make_concentric_rects_incr
easing, true, | |
264 BBoxQueryBench::kRandom_QueryType, SkRTree::Create(5, 16))
); | |
265 ) | |
266 DEF_BENCH( | |
267 return SkNEW_ARGS(BBoxQueryBench, ("(unsorted)concentric", &make_concentric_
rects_increasing, true, | |
268 BBoxQueryBench::kRandom_QueryType, SkRTree::Create(5, 16,
1, false))); | |
269 ) | |
OLD | NEW |