| 1 |
/* |
| 2 |
* Copyright 2012 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 "bench/Benchmark.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkString.h" |
| 11 |
#include "include/private/base/SkTemplates.h" |
| 12 |
#include "src/base/SkRandom.h" |
| 13 |
#include "src/core/SkRTree.h" |
| 14 |
|
| 15 |
using namespace skia_private; |
| 16 |
|
| 17 |
// confine rectangles to a smallish area, so queries generally hit something, and overlap occurs: |
| 18 |
static const SkScalar GENERATE_EXTENTS = 1000.0f; |
| 19 |
static const int NUM_BUILD_RECTS = 500; |
| 20 |
static const int NUM_QUERY_RECTS = 5000; |
| 21 |
static const int GRID_WIDTH = 100; |
| 22 |
|
| 23 |
typedef SkRect (*MakeRectProc)(SkRandom&, int, int); |
| 24 |
|
| 25 |
// Time how long it takes to build an R-Tree. |
| 26 |
class RTreeBuildBench : public Benchmark { |
| 27 |
public: |
| 28 |
RTreeBuildBench(const char* name, MakeRectProc proc) : fProc(proc) { |
| 29 |
fName.printf("rtree_%s_build", name); |
| 30 |
} |
| 31 |
|
| 32 |
bool isSuitableFor(Backend backend) override { |
| 33 |
return backend == Backend::kNonRendering; |
| 34 |
} |
| 35 |
|
| 36 |
protected: |
| 37 |
const char* onGetName() override { |
| 38 |
return fName.c_str(); |
| 39 |
} |
| 40 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 41 |
SkRandom rand; |
| 42 |
AutoTArray<SkRect> rects(NUM_BUILD_RECTS); |
| 43 |
for (int i = 0; i < NUM_BUILD_RECTS; ++i) { |
| 44 |
rects[i] = fProc(rand, i, NUM_BUILD_RECTS); |
| 45 |
} |
| 46 |
|
| 47 |
for (int i = 0; i < loops; ++i) { |
| 48 |
SkRTree tree; |
| 49 |
tree.insert(rects.data(), NUM_BUILD_RECTS); |
| 50 |
} |
| 51 |
} |
| 52 |
private: |
| 53 |
MakeRectProc fProc; |
| 54 |
SkString fName; |
| 55 |
using INHERITED = Benchmark; |
| 56 |
}; |
| 57 |
|
| 58 |
// Time how long it takes to perform queries on an R-Tree. |
| 59 |
class RTreeQueryBench : public Benchmark { |
| 60 |
public: |
| 61 |
RTreeQueryBench(const char* name, MakeRectProc proc) : fProc(proc) { |
| 62 |
fName.printf("rtree_%s_query", name); |
| 63 |
} |
| 64 |
|
| 65 |
bool isSuitableFor(Backend backend) override { |
| 66 |
return backend == Backend::kNonRendering; |
| 67 |
} |
| 68 |
protected: |
| 69 |
const char* onGetName() override { |
| 70 |
return fName.c_str(); |
| 71 |
} |
| 72 |
void onDelayedSetup() override { |
| 73 |
SkRandom rand; |
| 74 |
AutoTArray<SkRect> rects(NUM_QUERY_RECTS); |
| 75 |
for (int i = 0; i < NUM_QUERY_RECTS; ++i) { |
| 76 |
rects[i] = fProc(rand, i, NUM_QUERY_RECTS); |
| 77 |
} |
| 78 |
fTree.insert(rects.data(), NUM_QUERY_RECTS); |
| 79 |
} |
| 80 |
|
| 81 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 82 |
SkRandom rand; |
| 83 |
for (int i = 0; i < loops; ++i) { |
| 84 |
std::vector<int> hits; |
| 85 |
SkRect query; |
| 86 |
query.fLeft = rand.nextRangeF(0, GENERATE_EXTENTS); |
| 87 |
query.fTop = rand.nextRangeF(0, GENERATE_EXTENTS); |
| 88 |
query.fRight = query.fLeft + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/2); |
| 89 |
query.fBottom = query.fTop + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/2); |
| 90 |
fTree.search(query, &hits); |
| 91 |
} |
| 92 |
} |
| 93 |
private: |
| 94 |
SkRTree fTree; |
| 95 |
MakeRectProc fProc; |
| 96 |
SkString fName; |
| 97 |
using INHERITED = Benchmark; |
| 98 |
}; |
| 99 |
|
| 100 |
static inline SkRect make_XYordered_rects(SkRandom& rand, int index, int numRects) { |
| 101 |
SkRect out; |
| 102 |
out.fLeft = SkIntToScalar(index % GRID_WIDTH); |
| 103 |
out.fTop = SkIntToScalar(index / GRID_WIDTH); |
| 104 |
out.fRight = out.fLeft + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/3); |
| 105 |
out.fBottom = out.fTop + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/3); |
| 106 |
return out; |
| 107 |
} |
| 108 |
static inline SkRect make_YXordered_rects(SkRandom& rand, int index, int numRects) { |
| 109 |
SkRect out; |
| 110 |
out.fLeft = SkIntToScalar(index / GRID_WIDTH); |
| 111 |
out.fTop = SkIntToScalar(index % GRID_WIDTH); |
| 112 |
out.fRight = out.fLeft + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/3); |
| 113 |
out.fBottom = out.fTop + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/3); |
| 114 |
return out; |
| 115 |
} |
| 116 |
|
| 117 |
static inline SkRect make_random_rects(SkRandom& rand, int index, int numRects) { |
| 118 |
SkRect out; |
| 119 |
out.fLeft = rand.nextRangeF(0, GENERATE_EXTENTS); |
| 120 |
out.fTop = rand.nextRangeF(0, GENERATE_EXTENTS); |
| 121 |
out.fRight = out.fLeft + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/5); |
| 122 |
out.fBottom = out.fTop + 1 + rand.nextRangeF(0, GENERATE_EXTENTS/5); |
| 123 |
return out; |
| 124 |
} |
| 125 |
|
| 126 |
static inline SkRect make_concentric_rects(SkRandom&, int index, int numRects) { |
| 127 |
return SkRect::MakeWH(SkIntToScalar(index+1), SkIntToScalar(index+1)); |
| 128 |
} |
| 129 |
|
| 130 |
/////////////////////////////////////////////////////////////////////////////// |
| 131 |
|
| 132 |
DEF_BENCH(return new RTreeBuildBench("XY", &make_XYordered_rects)) |
| 133 |
DEF_BENCH(return new RTreeBuildBench("YX", &make_YXordered_rects)) |
| 134 |
DEF_BENCH(return new RTreeBuildBench("random", &make_random_rects)) |
| 135 |
DEF_BENCH(return new RTreeBuildBench("concentric", &make_concentric_rects)) |
| 136 |
|
| 137 |
DEF_BENCH(return new RTreeQueryBench("XY", &make_XYordered_rects)) |
| 138 |
DEF_BENCH(return new RTreeQueryBench("YX", &make_YXordered_rects)) |
| 139 |
DEF_BENCH(return new RTreeQueryBench("random", &make_random_rects)) |
| 140 |
DEF_BENCH(return new RTreeQueryBench("concentric", &make_concentric_rects)) |