reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 13 | class GeometryBench : public Benchmark { |
| 14 | public: |
| 15 | GeometryBench(const char suffix[]) : fVolatileInt(0) { |
| 16 | fName.printf("geo_%s", suffix); |
| 17 | } |
| 18 | |
| 19 | virtual const char* onGetName() SK_OVERRIDE { |
| 20 | return fName.c_str(); |
| 21 | } |
| 22 | |
| 23 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 24 | return kNonRendering_Backend == backend; |
| 25 | } |
| 26 | |
| 27 | protected: |
| 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 | |
| 38 | private: |
| 39 | SkString fName; |
| 40 | }; |
| 41 | |
| 42 | class GeoRectBench : public GeometryBench { |
| 43 | public: |
| 44 | GeoRectBench(const char suffix[]) : GeometryBench(suffix) {} |
| 45 | |
| 46 | protected: |
| 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 | |
| 63 | class GeoRectBench_intersect : public GeoRectBench { |
| 64 | public: |
| 65 | GeoRectBench_intersect() : GeoRectBench("rect_intersect") {} |
| 66 | |
| 67 | protected: |
| 68 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
| 69 | 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 | |
| 80 | class GeoRectBench_intersect_rect : public GeoRectBench { |
| 81 | public: |
| 82 | GeoRectBench_intersect_rect() : GeoRectBench("rect_intersect_rect") {} |
| 83 | |
| 84 | protected: |
| 85 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
| 86 | 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 | |
| 97 | class GeoRectBench_Intersects : public GeoRectBench { |
| 98 | public: |
| 99 | GeoRectBench_Intersects() : GeoRectBench("rect_Intersects") {} |
| 100 | |
| 101 | protected: |
| 102 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
| 103 | 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 | |
| 113 | DEF_BENCH( return new GeoRectBench_intersect; ) |
| 114 | DEF_BENCH( return new GeoRectBench_intersect_rect; ) |
| 115 | DEF_BENCH( return new GeoRectBench_Intersects; ) |