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