blob: 6cd9ca2377358a0ad4f14e512a5b2c3cb4f8269c [file] [log] [blame]
reed1119c872014-10-09 14:29:01 -07001/*
2 * Copyright 2014 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 "Benchmark.h"
9#include "SkGeometry.h"
10#include "SkRandom.h"
11#include "SkRect.h"
12
13class GeometryBench : public Benchmark {
14public:
15 GeometryBench(const char suffix[]) : fVolatileInt(0) {
16 fName.printf("geo_%s", suffix);
17 }
18
mtklein36352bf2015-03-25 18:17:31 -070019 const char* onGetName() override {
reed1119c872014-10-09 14:29:01 -070020 return fName.c_str();
21 }
22
mtklein36352bf2015-03-25 18:17:31 -070023 bool isSuitableFor(Backend backend) override {
reed1119c872014-10-09 14:29:01 -070024 return kNonRendering_Backend == backend;
25 }
26
27protected:
28 volatile int fVolatileInt;
29
30 /**
31 * Subclasses can call this to try to defeat the optimizer (with some result of their
32 * inner loop), since it will fool the compiler into assuming that "n" is actually
33 * needed somewhere, and since this method is not const, the member fields cannot
34 * be assumed to be const before and after the call.
35 */
36 virtual void virtualCallToFoilOptimizers(int n) { fVolatileInt += n; }
37
38private:
39 SkString fName;
40};
41
42class GeoRectBench : public GeometryBench {
43public:
44 GeoRectBench(const char suffix[]) : GeometryBench(suffix) {}
45
46protected:
47 SkRect fRects[2048];
48
49 virtual void onPreDraw() {
50 const SkScalar min = -100;
51 const SkScalar max = 100;
52 SkRandom rand;
53 for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) {
54 SkScalar x = rand.nextRangeScalar(min, max);
55 SkScalar y = rand.nextRangeScalar(min, max);
56 SkScalar w = rand.nextRangeScalar(min, max);
57 SkScalar h = rand.nextRangeScalar(min, max);
58 fRects[i].setXYWH(x, y, w, h);
59 }
60 }
61};
62
63class GeoRectBench_intersect : public GeoRectBench {
64public:
65 GeoRectBench_intersect() : GeoRectBench("rect_intersect") {}
66
67protected:
mtklein36352bf2015-03-25 18:17:31 -070068 void onDraw(const int loops, SkCanvas* canvas) override {
reed1119c872014-10-09 14:29:01 -070069 for (int outer = 0; outer < loops; ++outer) {
70 int count = 0;
71 for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) {
72 SkRect r = fRects[0];
73 count += r.intersect(fRects[i]);
74 }
75 this->virtualCallToFoilOptimizers(count);
76 }
77 }
78};
79
80class GeoRectBench_intersect_rect : public GeoRectBench {
81public:
82 GeoRectBench_intersect_rect() : GeoRectBench("rect_intersect_rect") {}
83
84protected:
mtklein36352bf2015-03-25 18:17:31 -070085 void onDraw(const int loops, SkCanvas* canvas) override {
reed1119c872014-10-09 14:29:01 -070086 for (int outer = 0; outer < loops; ++outer) {
87 int count = 0;
88 SkRect r;
89 for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) {
90 count += r.intersect(fRects[0], fRects[i]);
91 }
92 this->virtualCallToFoilOptimizers(count);
93 }
94 }
95};
96
97class GeoRectBench_Intersects : public GeoRectBench {
98public:
99 GeoRectBench_Intersects() : GeoRectBench("rect_Intersects") {}
mtkleindba3e642014-10-20 10:43:55 -0700100
reed1119c872014-10-09 14:29:01 -0700101protected:
mtklein36352bf2015-03-25 18:17:31 -0700102 void onDraw(const int loops, SkCanvas* canvas) override {
reed1119c872014-10-09 14:29:01 -0700103 for (int outer = 0; outer < loops; ++outer) {
104 int count = 0;
105 for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) {
106 count += SkRect::Intersects(fRects[0], fRects[i]);
107 }
108 this->virtualCallToFoilOptimizers(count);
109 }
110 }
111};
112
reed40636a52014-10-10 05:50:15 -0700113class GeoRectBench_sort : public GeoRectBench {
114public:
115 GeoRectBench_sort() : GeoRectBench("rect_sort") {}
mtkleindba3e642014-10-20 10:43:55 -0700116
reed40636a52014-10-10 05:50:15 -0700117protected:
mtklein36352bf2015-03-25 18:17:31 -0700118 void onDraw(const int loops, SkCanvas* canvas) override {
reed40636a52014-10-10 05:50:15 -0700119 for (int outer = 0; outer < loops; ++outer) {
120 for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) {
121 fRects[i].sort();
122 }
123 }
124 }
125};
126
reed1119c872014-10-09 14:29:01 -0700127DEF_BENCH( return new GeoRectBench_intersect; )
128DEF_BENCH( return new GeoRectBench_intersect_rect; )
129DEF_BENCH( return new GeoRectBench_Intersects; )
reed40636a52014-10-10 05:50:15 -0700130
131DEF_BENCH( return new GeoRectBench_sort; )
reed65cb2cd2015-03-19 10:18:47 -0700132
133///////////////////////////////////////////////////////////////////////////////////////////////////
134
reed40b7dd52015-03-20 06:01:08 -0700135class QuadBenchBase : public GeometryBench {
136protected:
reed6b9ef902015-03-24 19:24:34 -0700137 SkPoint fPts[4];
reed65cb2cd2015-03-19 10:18:47 -0700138public:
reed40b7dd52015-03-20 06:01:08 -0700139 QuadBenchBase(const char name[]) : GeometryBench(name) {
reed65cb2cd2015-03-19 10:18:47 -0700140 SkRandom rand;
reed6b9ef902015-03-24 19:24:34 -0700141 for (int i = 0; i < 4; ++i) {
reed65cb2cd2015-03-19 10:18:47 -0700142 fPts[i].set(rand.nextUScalar1(), rand.nextUScalar1());
143 }
144 }
reed40b7dd52015-03-20 06:01:08 -0700145};
146
147class EvalQuadAt0 : public QuadBenchBase {
148public:
149 EvalQuadAt0() : QuadBenchBase("evalquadat0") {}
reed65cb2cd2015-03-19 10:18:47 -0700150protected:
mtklein36352bf2015-03-25 18:17:31 -0700151 void onDraw(const int loops, SkCanvas* canvas) override {
reed65cb2cd2015-03-19 10:18:47 -0700152 SkPoint result;
153 for (int outer = 0; outer < loops; ++outer) {
reed40b7dd52015-03-20 06:01:08 -0700154 SkEvalQuadAt(fPts, 0.5f, &result);
155 SkEvalQuadAt(fPts, 0.5f, &result);
156 SkEvalQuadAt(fPts, 0.5f, &result);
157 SkEvalQuadAt(fPts, 0.5f, &result);
reed65cb2cd2015-03-19 10:18:47 -0700158 }
159 }
160};
161DEF_BENCH( return new EvalQuadAt0; )
162
reed40b7dd52015-03-20 06:01:08 -0700163class EvalQuadAt1 : public QuadBenchBase {
reed65cb2cd2015-03-19 10:18:47 -0700164public:
reed40b7dd52015-03-20 06:01:08 -0700165 EvalQuadAt1() : QuadBenchBase("evalquadat1") {}
reed65cb2cd2015-03-19 10:18:47 -0700166protected:
mtklein36352bf2015-03-25 18:17:31 -0700167 void onDraw(const int loops, SkCanvas* canvas) override {
reed40b7dd52015-03-20 06:01:08 -0700168 SkPoint result;
reed65cb2cd2015-03-19 10:18:47 -0700169 for (int outer = 0; outer < loops; ++outer) {
reed40b7dd52015-03-20 06:01:08 -0700170 result = SkEvalQuadAt(fPts, 0.5f);
171 result = SkEvalQuadAt(fPts, 0.5f);
172 result = SkEvalQuadAt(fPts, 0.5f);
173 result = SkEvalQuadAt(fPts, 0.5f);
reed65cb2cd2015-03-19 10:18:47 -0700174 }
175 }
176};
177DEF_BENCH( return new EvalQuadAt1; )
178
reed40b7dd52015-03-20 06:01:08 -0700179////////
180
181class EvalQuadTangentAt0 : public QuadBenchBase {
182public:
183 EvalQuadTangentAt0() : QuadBenchBase("evalquadtangentat0") {}
184protected:
mtklein36352bf2015-03-25 18:17:31 -0700185 void onDraw(const int loops, SkCanvas* canvas) override {
reed40b7dd52015-03-20 06:01:08 -0700186 SkPoint result;
187 for (int outer = 0; outer < loops; ++outer) {
188 SkEvalQuadAt(fPts, 0.5f, NULL, &result);
189 SkEvalQuadAt(fPts, 0.5f, NULL, &result);
190 SkEvalQuadAt(fPts, 0.5f, NULL, &result);
191 SkEvalQuadAt(fPts, 0.5f, NULL, &result);
192 }
193 }
194};
195DEF_BENCH( return new EvalQuadTangentAt0; )
196
197class EvalQuadTangentAt1 : public QuadBenchBase {
198public:
199 EvalQuadTangentAt1() : QuadBenchBase("evalquadtangentat1") {}
200protected:
mtklein36352bf2015-03-25 18:17:31 -0700201 void onDraw(const int loops, SkCanvas* canvas) override {
reed40b7dd52015-03-20 06:01:08 -0700202 SkPoint result;
203 for (int outer = 0; outer < loops; ++outer) {
204 result = SkEvalQuadTangentAt(fPts, 0.5f);
205 result = SkEvalQuadTangentAt(fPts, 0.5f);
206 result = SkEvalQuadTangentAt(fPts, 0.5f);
207 result = SkEvalQuadTangentAt(fPts, 0.5f);
208 }
209 }
210};
211DEF_BENCH( return new EvalQuadTangentAt1; )
212
213////////
214
reedc08330f2015-03-26 07:26:08 -0700215class ChopQuadAt : public QuadBenchBase {
reed40b7dd52015-03-20 06:01:08 -0700216public:
reedc08330f2015-03-26 07:26:08 -0700217 ChopQuadAt() : QuadBenchBase("chopquadat") {}
reed40b7dd52015-03-20 06:01:08 -0700218protected:
mtklein36352bf2015-03-25 18:17:31 -0700219 void onDraw(const int loops, SkCanvas* canvas) override {
reed40b7dd52015-03-20 06:01:08 -0700220 SkPoint dst[5];
221 for (int outer = 0; outer < loops; ++outer) {
222 SkChopQuadAt(fPts, dst, 0.5f);
223 SkChopQuadAt(fPts, dst, 0.5f);
224 SkChopQuadAt(fPts, dst, 0.5f);
225 SkChopQuadAt(fPts, dst, 0.5f);
226 }
227 }
228};
reedc08330f2015-03-26 07:26:08 -0700229DEF_BENCH( return new ChopQuadAt; )
reed40b7dd52015-03-20 06:01:08 -0700230
reedc08330f2015-03-26 07:26:08 -0700231class ChopCubicAt : public QuadBenchBase {
reed40b7dd52015-03-20 06:01:08 -0700232public:
reedc08330f2015-03-26 07:26:08 -0700233 ChopCubicAt() : QuadBenchBase("chopcubicat0") {}
reed6b9ef902015-03-24 19:24:34 -0700234protected:
mtklein36352bf2015-03-25 18:17:31 -0700235 void onDraw(const int loops, SkCanvas* canvas) override {
reed6b9ef902015-03-24 19:24:34 -0700236 SkPoint dst[7];
237 for (int outer = 0; outer < loops; ++outer) {
238 SkChopCubicAt(fPts, dst, 0.5f);
239 SkChopCubicAt(fPts, dst, 0.5f);
240 SkChopCubicAt(fPts, dst, 0.5f);
241 SkChopCubicAt(fPts, dst, 0.5f);
242 }
243 }
244};
reedc08330f2015-03-26 07:26:08 -0700245DEF_BENCH( return new ChopCubicAt; )
reed6b9ef902015-03-24 19:24:34 -0700246