aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/GeometryBench.cpp
blob: 6cd9ca2377358a0ad4f14e512a5b2c3cb4f8269c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "Benchmark.h"
#include "SkGeometry.h"
#include "SkRandom.h"
#include "SkRect.h"

class GeometryBench : public Benchmark {
public:
    GeometryBench(const char suffix[]) : fVolatileInt(0) {
        fName.printf("geo_%s", suffix);
    }

    const char* onGetName() override {
        return fName.c_str();
    }

    bool isSuitableFor(Backend backend) override {
        return kNonRendering_Backend == backend;
    }

protected:
    volatile int fVolatileInt;

    /**
     *  Subclasses can call this to try to defeat the optimizer (with some result of their
     *  inner loop), since it will fool the compiler into assuming that "n" is actually
     *  needed somewhere, and since this method is not const, the member fields cannot
     *  be assumed to be const before and after the call.
     */
    virtual void virtualCallToFoilOptimizers(int n) { fVolatileInt += n; }

private:
    SkString fName;
};

class GeoRectBench : public GeometryBench {
public:
    GeoRectBench(const char suffix[]) : GeometryBench(suffix) {}

protected:
    SkRect fRects[2048];

    virtual void onPreDraw() {
        const SkScalar min = -100;
        const SkScalar max = 100;
        SkRandom rand;
        for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) {
            SkScalar x = rand.nextRangeScalar(min, max);
            SkScalar y = rand.nextRangeScalar(min, max);
            SkScalar w = rand.nextRangeScalar(min, max);
            SkScalar h = rand.nextRangeScalar(min, max);
            fRects[i].setXYWH(x, y, w, h);
        }
    }
};

class GeoRectBench_intersect : public GeoRectBench {
public:
    GeoRectBench_intersect() : GeoRectBench("rect_intersect") {}

protected:
    void onDraw(const int loops, SkCanvas* canvas) override {
        for (int outer = 0; outer < loops; ++outer) {
            int count = 0;
            for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) {
                SkRect r = fRects[0];
                count += r.intersect(fRects[i]);
            }
            this->virtualCallToFoilOptimizers(count);
        }
    }
};

class GeoRectBench_intersect_rect : public GeoRectBench {
public:
    GeoRectBench_intersect_rect() : GeoRectBench("rect_intersect_rect") {}

protected:
    void onDraw(const int loops, SkCanvas* canvas) override {
        for (int outer = 0; outer < loops; ++outer) {
            int count = 0;
            SkRect r;
            for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) {
                count += r.intersect(fRects[0], fRects[i]);
            }
            this->virtualCallToFoilOptimizers(count);
        }
    }
};

class GeoRectBench_Intersects : public GeoRectBench {
public:
    GeoRectBench_Intersects() : GeoRectBench("rect_Intersects") {}
    
protected:
    void onDraw(const int loops, SkCanvas* canvas) override {
        for (int outer = 0; outer < loops; ++outer) {
            int count = 0;
            for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) {
                count += SkRect::Intersects(fRects[0], fRects[i]);
            }
            this->virtualCallToFoilOptimizers(count);
        }
    }
};

class GeoRectBench_sort : public GeoRectBench {
public:
    GeoRectBench_sort() : GeoRectBench("rect_sort") {}
    
protected:
    void onDraw(const int loops, SkCanvas* canvas) override {
        for (int outer = 0; outer < loops; ++outer) {
            for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) {
                fRects[i].sort();
            }
        }
    }
};

DEF_BENCH( return new GeoRectBench_intersect; )
DEF_BENCH( return new GeoRectBench_intersect_rect; )
DEF_BENCH( return new GeoRectBench_Intersects; )

DEF_BENCH( return new GeoRectBench_sort; )

///////////////////////////////////////////////////////////////////////////////////////////////////

class QuadBenchBase : public GeometryBench {
protected:
    SkPoint fPts[4];
public:
    QuadBenchBase(const char name[]) : GeometryBench(name) {
        SkRandom rand;
        for (int i = 0; i < 4; ++i) {
            fPts[i].set(rand.nextUScalar1(), rand.nextUScalar1());
        }
    }
};

class EvalQuadAt0 : public QuadBenchBase {
public:
    EvalQuadAt0() : QuadBenchBase("evalquadat0") {}
protected:
    void onDraw(const int loops, SkCanvas* canvas) override {
        SkPoint result;
        for (int outer = 0; outer < loops; ++outer) {
            SkEvalQuadAt(fPts, 0.5f, &result);
            SkEvalQuadAt(fPts, 0.5f, &result);
            SkEvalQuadAt(fPts, 0.5f, &result);
            SkEvalQuadAt(fPts, 0.5f, &result);
        }
    }
};
DEF_BENCH( return new EvalQuadAt0; )

class EvalQuadAt1 : public QuadBenchBase {
public:
    EvalQuadAt1() : QuadBenchBase("evalquadat1") {}
protected:
    void onDraw(const int loops, SkCanvas* canvas) override {
        SkPoint result;
        for (int outer = 0; outer < loops; ++outer) {
            result = SkEvalQuadAt(fPts, 0.5f);
            result = SkEvalQuadAt(fPts, 0.5f);
            result = SkEvalQuadAt(fPts, 0.5f);
            result = SkEvalQuadAt(fPts, 0.5f);
        }
    }
};
DEF_BENCH( return new EvalQuadAt1; )

////////

class EvalQuadTangentAt0 : public QuadBenchBase {
public:
    EvalQuadTangentAt0() : QuadBenchBase("evalquadtangentat0") {}
protected:
    void onDraw(const int loops, SkCanvas* canvas) override {
        SkPoint result;
        for (int outer = 0; outer < loops; ++outer) {
            SkEvalQuadAt(fPts, 0.5f, NULL, &result);
            SkEvalQuadAt(fPts, 0.5f, NULL, &result);
            SkEvalQuadAt(fPts, 0.5f, NULL, &result);
            SkEvalQuadAt(fPts, 0.5f, NULL, &result);
        }
    }
};
DEF_BENCH( return new EvalQuadTangentAt0; )

class EvalQuadTangentAt1 : public QuadBenchBase {
public:
    EvalQuadTangentAt1() : QuadBenchBase("evalquadtangentat1") {}
protected:
    void onDraw(const int loops, SkCanvas* canvas) override {
        SkPoint result;
        for (int outer = 0; outer < loops; ++outer) {
            result = SkEvalQuadTangentAt(fPts, 0.5f);
            result = SkEvalQuadTangentAt(fPts, 0.5f);
            result = SkEvalQuadTangentAt(fPts, 0.5f);
            result = SkEvalQuadTangentAt(fPts, 0.5f);
        }
    }
};
DEF_BENCH( return new EvalQuadTangentAt1; )

////////

class ChopQuadAt : public QuadBenchBase {
public:
    ChopQuadAt() : QuadBenchBase("chopquadat") {}
protected:
    void onDraw(const int loops, SkCanvas* canvas) override {
        SkPoint dst[5];
        for (int outer = 0; outer < loops; ++outer) {
            SkChopQuadAt(fPts, dst, 0.5f);
            SkChopQuadAt(fPts, dst, 0.5f);
            SkChopQuadAt(fPts, dst, 0.5f);
            SkChopQuadAt(fPts, dst, 0.5f);
        }
    }
};
DEF_BENCH( return new ChopQuadAt; )

class ChopCubicAt : public QuadBenchBase {
public:
    ChopCubicAt() : QuadBenchBase("chopcubicat0") {}
protected:
    void onDraw(const int loops, SkCanvas* canvas) override {
        SkPoint dst[7];
        for (int outer = 0; outer < loops; ++outer) {
            SkChopCubicAt(fPts, dst, 0.5f);
            SkChopCubicAt(fPts, dst, 0.5f);
            SkChopCubicAt(fPts, dst, 0.5f);
            SkChopCubicAt(fPts, dst, 0.5f);
        }
    }
};
DEF_BENCH( return new ChopCubicAt; )