Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
RTreeBench.cpp
Go to the documentation of this file.
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"
12#include "src/base/SkRandom.h"
13#include "src/core/SkRTree.h"
14
15using namespace skia_private;
16
17// confine rectangles to a smallish area, so queries generally hit something, and overlap occurs:
18static const SkScalar GENERATE_EXTENTS = 1000.0f;
19static const int NUM_BUILD_RECTS = 500;
20static const int NUM_QUERY_RECTS = 5000;
21static const int GRID_WIDTH = 100;
22
24
25// Time how long it takes to build an R-Tree.
26class RTreeBuildBench : public Benchmark {
27public:
28 RTreeBuildBench(const char* name, MakeRectProc proc) : fProc(proc) {
29 fName.printf("rtree_%s_build", name);
30 }
31
32 bool isSuitableFor(Backend backend) override {
34 }
35
36protected:
37 const char* onGetName() override {
38 return fName.c_str();
39 }
40 void onDraw(int loops, SkCanvas* canvas) override {
41 SkRandom rand;
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 }
52private:
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.
59class RTreeQueryBench : public Benchmark {
60public:
61 RTreeQueryBench(const char* name, MakeRectProc proc) : fProc(proc) {
62 fName.printf("rtree_%s_query", name);
63 }
64
65 bool isSuitableFor(Backend backend) override {
67 }
68protected:
69 const char* onGetName() override {
70 return fName.c_str();
71 }
72 void onDelayedSetup() override {
73 SkRandom rand;
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 }
93private:
94 SkRTree fTree;
95 MakeRectProc fProc;
96 SkString fName;
97 using INHERITED = Benchmark;
98};
99
100static 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}
108static 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
117static 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
126static inline SkRect make_concentric_rects(SkRandom&, int index, int numRects) {
127 return SkRect::MakeWH(SkIntToScalar(index+1), SkIntToScalar(index+1));
128}
129
130///////////////////////////////////////////////////////////////////////////////
131
136
139DEF_BENCH(return new RTreeQueryBench("random", &make_random_rects));
140DEF_BENCH(return new RTreeQueryBench("concentric", &make_concentric_rects));
#define DEF_BENCH(code)
Definition Benchmark.h:20
const char * backend
static const int NUM_QUERY_RECTS
static const int NUM_BUILD_RECTS
static const int GRID_WIDTH
static SkRect make_YXordered_rects(SkRandom &rand, int index, int numRects)
SkRect(* MakeRectProc)(SkRandom &, int, int)
static SkRect make_XYordered_rects(SkRandom &rand, int index, int numRects)
static SkRect make_random_rects(SkRandom &rand, int index, int numRects)
static const SkScalar GENERATE_EXTENTS
static SkRect make_concentric_rects(SkRandom &, int index, int numRects)
#define SkIntToScalar(x)
Definition SkScalar.h:57
Type::kYUV Type::kRGBA() int(0.7 *637)
const char * onGetName() override
void onDraw(int loops, SkCanvas *canvas) override
bool isSuitableFor(Backend backend) override
RTreeBuildBench(const char *name, MakeRectProc proc)
void onDraw(int loops, SkCanvas *canvas) override
void onDelayedSetup() override
bool isSuitableFor(Backend backend) override
RTreeQueryBench(const char *name, MakeRectProc proc)
const char * onGetName() override
void insert(const SkRect[], int N) override
Definition SkRTree.cpp:15
void search(const SkRect &query, std::vector< int > *results) const override
Definition SkRTree.cpp:147
float nextRangeF(float min, float max)
Definition SkRandom.h:64
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition SkString.cpp:534
const char * c_str() const
Definition SkString.h:133
const T * data() const
float SkScalar
Definition extension.cpp:12
const char * name
Definition fuchsia.cc:50
SkScalar fBottom
larger y-axis bounds
Definition extension.cpp:17
SkScalar fLeft
smaller x-axis bounds
Definition extension.cpp:14
SkScalar fRight
larger x-axis bounds
Definition extension.cpp:16
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
SkScalar fTop
smaller y-axis bounds
Definition extension.cpp:15