2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkGeometry.h"
13 class GeometryBench : public Benchmark {
15 GeometryBench(const char suffix[]) : fVolatileInt(0) {
16 fName.printf("geo_%s", suffix);
19 const char* onGetName() SK_OVERRIDE {
23 bool isSuitableFor(Backend backend) SK_OVERRIDE {
24 return kNonRendering_Backend == backend;
28 volatile int fVolatileInt;
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.
36 virtual void virtualCallToFoilOptimizers(int n) { fVolatileInt += n; }
42 class GeoRectBench : public GeometryBench {
44 GeoRectBench(const char suffix[]) : GeometryBench(suffix) {}
49 virtual void onPreDraw() {
50 const SkScalar min = -100;
51 const SkScalar max = 100;
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);
63 class GeoRectBench_intersect : public GeoRectBench {
65 GeoRectBench_intersect() : GeoRectBench("rect_intersect") {}
68 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
69 for (int outer = 0; outer < loops; ++outer) {
71 for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) {
73 count += r.intersect(fRects[i]);
75 this->virtualCallToFoilOptimizers(count);
80 class GeoRectBench_intersect_rect : public GeoRectBench {
82 GeoRectBench_intersect_rect() : GeoRectBench("rect_intersect_rect") {}
85 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
86 for (int outer = 0; outer < loops; ++outer) {
89 for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) {
90 count += r.intersect(fRects[0], fRects[i]);
92 this->virtualCallToFoilOptimizers(count);
97 class GeoRectBench_Intersects : public GeoRectBench {
99 GeoRectBench_Intersects() : GeoRectBench("rect_Intersects") {}
102 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
103 for (int outer = 0; outer < loops; ++outer) {
105 for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) {
106 count += SkRect::Intersects(fRects[0], fRects[i]);
108 this->virtualCallToFoilOptimizers(count);
113 class GeoRectBench_sort : public GeoRectBench {
115 GeoRectBench_sort() : GeoRectBench("rect_sort") {}
118 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
119 for (int outer = 0; outer < loops; ++outer) {
120 for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) {
127 DEF_BENCH( return new GeoRectBench_intersect; )
128 DEF_BENCH( return new GeoRectBench_intersect_rect; )
129 DEF_BENCH( return new GeoRectBench_Intersects; )
131 DEF_BENCH( return new GeoRectBench_sort; )
133 ///////////////////////////////////////////////////////////////////////////////////////////////////
135 class QuadBenchBase : public GeometryBench {
139 QuadBenchBase(const char name[]) : GeometryBench(name) {
141 for (int i = 0; i < 4; ++i) {
142 fPts[i].set(rand.nextUScalar1(), rand.nextUScalar1());
147 class EvalQuadAt0 : public QuadBenchBase {
149 EvalQuadAt0() : QuadBenchBase("evalquadat0") {}
151 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
153 for (int outer = 0; outer < loops; ++outer) {
154 SkEvalQuadAt(fPts, 0.5f, &result);
155 SkEvalQuadAt(fPts, 0.5f, &result);
156 SkEvalQuadAt(fPts, 0.5f, &result);
157 SkEvalQuadAt(fPts, 0.5f, &result);
161 DEF_BENCH( return new EvalQuadAt0; )
163 class EvalQuadAt1 : public QuadBenchBase {
165 EvalQuadAt1() : QuadBenchBase("evalquadat1") {}
167 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
169 for (int outer = 0; outer < loops; ++outer) {
170 result = SkEvalQuadAt(fPts, 0.5f);
171 result = SkEvalQuadAt(fPts, 0.5f);
172 result = SkEvalQuadAt(fPts, 0.5f);
173 result = SkEvalQuadAt(fPts, 0.5f);
177 DEF_BENCH( return new EvalQuadAt1; )
181 class EvalQuadTangentAt0 : public QuadBenchBase {
183 EvalQuadTangentAt0() : QuadBenchBase("evalquadtangentat0") {}
185 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
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);
195 DEF_BENCH( return new EvalQuadTangentAt0; )
197 class EvalQuadTangentAt1 : public QuadBenchBase {
199 EvalQuadTangentAt1() : QuadBenchBase("evalquadtangentat1") {}
201 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
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);
211 DEF_BENCH( return new EvalQuadTangentAt1; )
215 class ChopQuadAt0 : public QuadBenchBase {
217 ChopQuadAt0() : QuadBenchBase("chopquadat0") {}
219 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
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);
229 DEF_BENCH( return new ChopQuadAt0; )
231 class ChopQuadAt1 : public QuadBenchBase {
233 ChopQuadAt1() : QuadBenchBase("chopquadat1") {}
235 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
237 for (int outer = 0; outer < loops; ++outer) {
238 SkChopQuadAt2(fPts, dst, 0.5f);
239 SkChopQuadAt2(fPts, dst, 0.5f);
240 SkChopQuadAt2(fPts, dst, 0.5f);
241 SkChopQuadAt2(fPts, dst, 0.5f);
245 DEF_BENCH( return new ChopQuadAt1; )
247 class ChopCubicAt0 : public QuadBenchBase {
249 ChopCubicAt0() : QuadBenchBase("chopcubicat0") {}
251 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
253 for (int outer = 0; outer < loops; ++outer) {
254 SkChopCubicAt(fPts, dst, 0.5f);
255 SkChopCubicAt(fPts, dst, 0.5f);
256 SkChopCubicAt(fPts, dst, 0.5f);
257 SkChopCubicAt(fPts, dst, 0.5f);
261 DEF_BENCH( return new ChopCubicAt0; )
263 class ChopCubicAt1 : public QuadBenchBase {
265 ChopCubicAt1() : QuadBenchBase("chopcubicat1") {}
267 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
269 for (int outer = 0; outer < loops; ++outer) {
270 SkChopCubicAt2(fPts, dst, 0.5f);
271 SkChopCubicAt2(fPts, dst, 0.5f);
272 SkChopCubicAt2(fPts, dst, 0.5f);
273 SkChopCubicAt2(fPts, dst, 0.5f);
277 DEF_BENCH( return new ChopCubicAt1; )