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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 19 | const char* onGetName() override { |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 20 | return fName.c_str(); |
| 21 | } |
| 22 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 23 | bool isSuitableFor(Backend backend) override { |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 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 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 49 | virtual void onDelayedSetup() { |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 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: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 68 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 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: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 85 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 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") {} |
mtklein | dba3e64 | 2014-10-20 10:43:55 -0700 | [diff] [blame] | 100 | |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 101 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 102 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 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 | |
reed | 40636a5 | 2014-10-10 05:50:15 -0700 | [diff] [blame] | 113 | class GeoRectBench_sort : public GeoRectBench { |
| 114 | public: |
| 115 | GeoRectBench_sort() : GeoRectBench("rect_sort") {} |
mtklein | dba3e64 | 2014-10-20 10:43:55 -0700 | [diff] [blame] | 116 | |
reed | 40636a5 | 2014-10-10 05:50:15 -0700 | [diff] [blame] | 117 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 118 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 40636a5 | 2014-10-10 05:50:15 -0700 | [diff] [blame] | 119 | 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 | |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 127 | DEF_BENCH( return new GeoRectBench_intersect; ) |
| 128 | DEF_BENCH( return new GeoRectBench_intersect_rect; ) |
| 129 | DEF_BENCH( return new GeoRectBench_Intersects; ) |
reed | 40636a5 | 2014-10-10 05:50:15 -0700 | [diff] [blame] | 130 | |
| 131 | DEF_BENCH( return new GeoRectBench_sort; ) |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 132 | |
| 133 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 134 | |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 135 | class QuadBenchBase : public GeometryBench { |
| 136 | protected: |
reed | 6b9ef90 | 2015-03-24 19:24:34 -0700 | [diff] [blame] | 137 | SkPoint fPts[4]; |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 138 | public: |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 139 | QuadBenchBase(const char name[]) : GeometryBench(name) { |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 140 | SkRandom rand; |
reed | 6b9ef90 | 2015-03-24 19:24:34 -0700 | [diff] [blame] | 141 | for (int i = 0; i < 4; ++i) { |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 142 | fPts[i].set(rand.nextUScalar1(), rand.nextUScalar1()); |
| 143 | } |
| 144 | } |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | class EvalQuadAt0 : public QuadBenchBase { |
| 148 | public: |
| 149 | EvalQuadAt0() : QuadBenchBase("evalquadat0") {} |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 150 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 151 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 152 | SkPoint result; |
| 153 | for (int outer = 0; outer < loops; ++outer) { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 154 | SkEvalQuadAt(fPts, 0.5f, &result); |
| 155 | SkEvalQuadAt(fPts, 0.5f, &result); |
| 156 | SkEvalQuadAt(fPts, 0.5f, &result); |
| 157 | SkEvalQuadAt(fPts, 0.5f, &result); |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | }; |
| 161 | DEF_BENCH( return new EvalQuadAt0; ) |
| 162 | |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 163 | class EvalQuadAt1 : public QuadBenchBase { |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 164 | public: |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 165 | EvalQuadAt1() : QuadBenchBase("evalquadat1") {} |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 166 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 167 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 168 | SkPoint result; |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 169 | for (int outer = 0; outer < loops; ++outer) { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 170 | result = SkEvalQuadAt(fPts, 0.5f); |
| 171 | result = SkEvalQuadAt(fPts, 0.5f); |
| 172 | result = SkEvalQuadAt(fPts, 0.5f); |
| 173 | result = SkEvalQuadAt(fPts, 0.5f); |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | }; |
| 177 | DEF_BENCH( return new EvalQuadAt1; ) |
| 178 | |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 179 | //////// |
| 180 | |
| 181 | class EvalQuadTangentAt0 : public QuadBenchBase { |
| 182 | public: |
| 183 | EvalQuadTangentAt0() : QuadBenchBase("evalquadtangentat0") {} |
| 184 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 185 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 186 | SkPoint result; |
| 187 | for (int outer = 0; outer < loops; ++outer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 188 | SkEvalQuadAt(fPts, 0.5f, nullptr, &result); |
| 189 | SkEvalQuadAt(fPts, 0.5f, nullptr, &result); |
| 190 | SkEvalQuadAt(fPts, 0.5f, nullptr, &result); |
| 191 | SkEvalQuadAt(fPts, 0.5f, nullptr, &result); |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | }; |
| 195 | DEF_BENCH( return new EvalQuadTangentAt0; ) |
| 196 | |
| 197 | class EvalQuadTangentAt1 : public QuadBenchBase { |
| 198 | public: |
| 199 | EvalQuadTangentAt1() : QuadBenchBase("evalquadtangentat1") {} |
| 200 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 201 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 202 | 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 | }; |
| 211 | DEF_BENCH( return new EvalQuadTangentAt1; ) |
| 212 | |
| 213 | //////// |
| 214 | |
reed | c08330f | 2015-03-26 07:26:08 -0700 | [diff] [blame] | 215 | class ChopQuadAt : public QuadBenchBase { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 216 | public: |
reed | c08330f | 2015-03-26 07:26:08 -0700 | [diff] [blame] | 217 | ChopQuadAt() : QuadBenchBase("chopquadat") {} |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 218 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 219 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 220 | 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 | }; |
reed | c08330f | 2015-03-26 07:26:08 -0700 | [diff] [blame] | 229 | DEF_BENCH( return new ChopQuadAt; ) |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 230 | |
reed | c08330f | 2015-03-26 07:26:08 -0700 | [diff] [blame] | 231 | class ChopCubicAt : public QuadBenchBase { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 232 | public: |
reed | c08330f | 2015-03-26 07:26:08 -0700 | [diff] [blame] | 233 | ChopCubicAt() : QuadBenchBase("chopcubicat0") {} |
reed | 6b9ef90 | 2015-03-24 19:24:34 -0700 | [diff] [blame] | 234 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 235 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 6b9ef90 | 2015-03-24 19:24:34 -0700 | [diff] [blame] | 236 | 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 | }; |
reed | c08330f | 2015-03-26 07:26:08 -0700 | [diff] [blame] | 245 | DEF_BENCH( return new ChopCubicAt; ) |
reed | 6b9ef90 | 2015-03-24 19:24:34 -0700 | [diff] [blame] | 246 | |