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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/Benchmark.h" |
| 9 | #include "include/core/SkRect.h" |
| 10 | #include "include/utils/SkRandom.h" |
| 11 | #include "src/core/SkGeometry.h" |
Mike Reed | 6052c0b | 2020-08-28 11:49:56 -0400 | [diff] [blame] | 12 | #include "src/core/SkPathPriv.h" |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 13 | |
| 14 | class GeometryBench : public Benchmark { |
| 15 | public: |
| 16 | GeometryBench(const char suffix[]) : fVolatileInt(0) { |
| 17 | fName.printf("geo_%s", suffix); |
| 18 | } |
| 19 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 20 | const char* onGetName() override { |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 21 | return fName.c_str(); |
| 22 | } |
| 23 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 24 | bool isSuitableFor(Backend backend) override { |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 25 | return kNonRendering_Backend == backend; |
| 26 | } |
| 27 | |
| 28 | protected: |
| 29 | volatile int fVolatileInt; |
| 30 | |
| 31 | /** |
| 32 | * Subclasses can call this to try to defeat the optimizer (with some result of their |
| 33 | * inner loop), since it will fool the compiler into assuming that "n" is actually |
| 34 | * needed somewhere, and since this method is not const, the member fields cannot |
| 35 | * be assumed to be const before and after the call. |
| 36 | */ |
| 37 | virtual void virtualCallToFoilOptimizers(int n) { fVolatileInt += n; } |
| 38 | |
| 39 | private: |
| 40 | SkString fName; |
| 41 | }; |
| 42 | |
| 43 | class GeoRectBench : public GeometryBench { |
| 44 | public: |
| 45 | GeoRectBench(const char suffix[]) : GeometryBench(suffix) {} |
| 46 | |
| 47 | protected: |
| 48 | SkRect fRects[2048]; |
| 49 | |
Brian Salomon | d007281 | 2020-07-21 17:03:56 -0400 | [diff] [blame] | 50 | void onDelayedSetup() override { |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 51 | const SkScalar min = -100; |
| 52 | const SkScalar max = 100; |
| 53 | SkRandom rand; |
| 54 | for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) { |
| 55 | SkScalar x = rand.nextRangeScalar(min, max); |
| 56 | SkScalar y = rand.nextRangeScalar(min, max); |
| 57 | SkScalar w = rand.nextRangeScalar(min, max); |
| 58 | SkScalar h = rand.nextRangeScalar(min, max); |
| 59 | fRects[i].setXYWH(x, y, w, h); |
| 60 | } |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | class GeoRectBench_intersect : public GeoRectBench { |
| 65 | public: |
| 66 | GeoRectBench_intersect() : GeoRectBench("rect_intersect") {} |
| 67 | |
| 68 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 69 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 70 | for (int outer = 0; outer < loops; ++outer) { |
| 71 | int count = 0; |
| 72 | for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) { |
| 73 | SkRect r = fRects[0]; |
| 74 | count += r.intersect(fRects[i]); |
| 75 | } |
| 76 | this->virtualCallToFoilOptimizers(count); |
| 77 | } |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | class GeoRectBench_intersect_rect : public GeoRectBench { |
| 82 | public: |
| 83 | GeoRectBench_intersect_rect() : GeoRectBench("rect_intersect_rect") {} |
| 84 | |
| 85 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 86 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 87 | for (int outer = 0; outer < loops; ++outer) { |
| 88 | int count = 0; |
| 89 | SkRect r; |
| 90 | for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) { |
| 91 | count += r.intersect(fRects[0], fRects[i]); |
| 92 | } |
| 93 | this->virtualCallToFoilOptimizers(count); |
| 94 | } |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | class GeoRectBench_Intersects : public GeoRectBench { |
| 99 | public: |
| 100 | GeoRectBench_Intersects() : GeoRectBench("rect_Intersects") {} |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 101 | |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 102 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 103 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 104 | for (int outer = 0; outer < loops; ++outer) { |
| 105 | int count = 0; |
| 106 | for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) { |
| 107 | count += SkRect::Intersects(fRects[0], fRects[i]); |
| 108 | } |
| 109 | this->virtualCallToFoilOptimizers(count); |
| 110 | } |
| 111 | } |
| 112 | }; |
| 113 | |
reed | 40636a5 | 2014-10-10 05:50:15 -0700 | [diff] [blame] | 114 | class GeoRectBench_sort : public GeoRectBench { |
| 115 | public: |
| 116 | GeoRectBench_sort() : GeoRectBench("rect_sort") {} |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 117 | |
reed | 40636a5 | 2014-10-10 05:50:15 -0700 | [diff] [blame] | 118 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 119 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 40636a5 | 2014-10-10 05:50:15 -0700 | [diff] [blame] | 120 | for (int outer = 0; outer < loops; ++outer) { |
| 121 | for (size_t i = 0; i < SK_ARRAY_COUNT(fRects); ++i) { |
| 122 | fRects[i].sort(); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | }; |
| 127 | |
reed | 1119c87 | 2014-10-09 14:29:01 -0700 | [diff] [blame] | 128 | DEF_BENCH( return new GeoRectBench_intersect; ) |
| 129 | DEF_BENCH( return new GeoRectBench_intersect_rect; ) |
| 130 | DEF_BENCH( return new GeoRectBench_Intersects; ) |
reed | 40636a5 | 2014-10-10 05:50:15 -0700 | [diff] [blame] | 131 | |
| 132 | DEF_BENCH( return new GeoRectBench_sort; ) |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 133 | |
| 134 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 135 | |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 136 | class QuadBenchBase : public GeometryBench { |
| 137 | protected: |
reed | 6b9ef90 | 2015-03-24 19:24:34 -0700 | [diff] [blame] | 138 | SkPoint fPts[4]; |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 139 | public: |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 140 | QuadBenchBase(const char name[]) : GeometryBench(name) { |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 141 | SkRandom rand; |
reed | 6b9ef90 | 2015-03-24 19:24:34 -0700 | [diff] [blame] | 142 | for (int i = 0; i < 4; ++i) { |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 143 | fPts[i].set(rand.nextUScalar1(), rand.nextUScalar1()); |
| 144 | } |
| 145 | } |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | class EvalQuadAt0 : public QuadBenchBase { |
| 149 | public: |
| 150 | EvalQuadAt0() : QuadBenchBase("evalquadat0") {} |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 151 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 152 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 153 | SkPoint result; |
| 154 | for (int outer = 0; outer < loops; ++outer) { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 155 | SkEvalQuadAt(fPts, 0.5f, &result); |
| 156 | SkEvalQuadAt(fPts, 0.5f, &result); |
| 157 | SkEvalQuadAt(fPts, 0.5f, &result); |
| 158 | SkEvalQuadAt(fPts, 0.5f, &result); |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | }; |
| 162 | DEF_BENCH( return new EvalQuadAt0; ) |
| 163 | |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 164 | class EvalQuadAt1 : public QuadBenchBase { |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 165 | public: |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 166 | EvalQuadAt1() : QuadBenchBase("evalquadat1") {} |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 167 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 168 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 169 | SkPoint result; |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 170 | for (int outer = 0; outer < loops; ++outer) { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 171 | result = SkEvalQuadAt(fPts, 0.5f); |
| 172 | result = SkEvalQuadAt(fPts, 0.5f); |
| 173 | result = SkEvalQuadAt(fPts, 0.5f); |
| 174 | result = SkEvalQuadAt(fPts, 0.5f); |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | }; |
| 178 | DEF_BENCH( return new EvalQuadAt1; ) |
| 179 | |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 180 | //////// |
| 181 | |
| 182 | class EvalQuadTangentAt0 : public QuadBenchBase { |
| 183 | public: |
| 184 | EvalQuadTangentAt0() : QuadBenchBase("evalquadtangentat0") {} |
| 185 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 186 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 187 | SkPoint result; |
| 188 | for (int outer = 0; outer < loops; ++outer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 189 | SkEvalQuadAt(fPts, 0.5f, nullptr, &result); |
| 190 | SkEvalQuadAt(fPts, 0.5f, nullptr, &result); |
| 191 | SkEvalQuadAt(fPts, 0.5f, nullptr, &result); |
| 192 | SkEvalQuadAt(fPts, 0.5f, nullptr, &result); |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | }; |
| 196 | DEF_BENCH( return new EvalQuadTangentAt0; ) |
| 197 | |
| 198 | class EvalQuadTangentAt1 : public QuadBenchBase { |
| 199 | public: |
| 200 | EvalQuadTangentAt1() : QuadBenchBase("evalquadtangentat1") {} |
| 201 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 202 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 203 | SkPoint result; |
| 204 | for (int outer = 0; outer < loops; ++outer) { |
| 205 | result = SkEvalQuadTangentAt(fPts, 0.5f); |
| 206 | result = SkEvalQuadTangentAt(fPts, 0.5f); |
| 207 | result = SkEvalQuadTangentAt(fPts, 0.5f); |
| 208 | result = SkEvalQuadTangentAt(fPts, 0.5f); |
| 209 | } |
| 210 | } |
| 211 | }; |
| 212 | DEF_BENCH( return new EvalQuadTangentAt1; ) |
| 213 | |
| 214 | //////// |
| 215 | |
reed | c08330f | 2015-03-26 07:26:08 -0700 | [diff] [blame] | 216 | class ChopQuadAt : public QuadBenchBase { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 217 | public: |
reed | c08330f | 2015-03-26 07:26:08 -0700 | [diff] [blame] | 218 | ChopQuadAt() : QuadBenchBase("chopquadat") {} |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 219 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 220 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 221 | SkPoint dst[5]; |
| 222 | for (int outer = 0; outer < loops; ++outer) { |
| 223 | SkChopQuadAt(fPts, dst, 0.5f); |
| 224 | SkChopQuadAt(fPts, dst, 0.5f); |
| 225 | SkChopQuadAt(fPts, dst, 0.5f); |
| 226 | SkChopQuadAt(fPts, dst, 0.5f); |
| 227 | } |
| 228 | } |
| 229 | }; |
reed | c08330f | 2015-03-26 07:26:08 -0700 | [diff] [blame] | 230 | DEF_BENCH( return new ChopQuadAt; ) |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 231 | |
reed | c08330f | 2015-03-26 07:26:08 -0700 | [diff] [blame] | 232 | class ChopCubicAt : public QuadBenchBase { |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 233 | public: |
reed | c08330f | 2015-03-26 07:26:08 -0700 | [diff] [blame] | 234 | ChopCubicAt() : QuadBenchBase("chopcubicat0") {} |
reed | 6b9ef90 | 2015-03-24 19:24:34 -0700 | [diff] [blame] | 235 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 236 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | 6b9ef90 | 2015-03-24 19:24:34 -0700 | [diff] [blame] | 237 | SkPoint dst[7]; |
| 238 | for (int outer = 0; outer < loops; ++outer) { |
| 239 | SkChopCubicAt(fPts, dst, 0.5f); |
| 240 | SkChopCubicAt(fPts, dst, 0.5f); |
| 241 | SkChopCubicAt(fPts, dst, 0.5f); |
| 242 | SkChopCubicAt(fPts, dst, 0.5f); |
| 243 | } |
| 244 | } |
| 245 | }; |
reed | c08330f | 2015-03-26 07:26:08 -0700 | [diff] [blame] | 246 | DEF_BENCH( return new ChopCubicAt; ) |
Mike Reed | ef4047d | 2018-11-28 11:45:44 -0500 | [diff] [blame] | 247 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 248 | #include "include/core/SkPath.h" |
Mike Reed | ef4047d | 2018-11-28 11:45:44 -0500 | [diff] [blame] | 249 | |
| 250 | class ConvexityBench : public Benchmark { |
| 251 | SkPath fPath; |
| 252 | |
| 253 | public: |
| 254 | ConvexityBench(const char suffix[]) { |
| 255 | fName.printf("convexity_%s", suffix); |
| 256 | } |
| 257 | |
| 258 | const char* onGetName() override { |
| 259 | return fName.c_str(); |
| 260 | } |
| 261 | |
| 262 | bool isSuitableFor(Backend backend) override { |
| 263 | return kNonRendering_Backend == backend; |
| 264 | } |
| 265 | |
| 266 | virtual void preparePath(SkPath*) = 0; |
| 267 | |
| 268 | protected: |
| 269 | void onPreDraw(SkCanvas*) override { |
| 270 | this->preparePath(&fPath); |
| 271 | } |
| 272 | |
| 273 | void onDraw(int loops, SkCanvas* canvas) override { |
| 274 | for (int i = 0; i < loops; ++i) { |
Mike Reed | 6052c0b | 2020-08-28 11:49:56 -0400 | [diff] [blame] | 275 | SkPathPriv::ForceComputeConvexity(fPath); |
Mike Reed | ef4047d | 2018-11-28 11:45:44 -0500 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | private: |
| 280 | SkString fName; |
| 281 | }; |
| 282 | |
| 283 | class RRectConvexityBench : public ConvexityBench { |
| 284 | public: |
| 285 | RRectConvexityBench() : ConvexityBench("rrect") {} |
| 286 | |
| 287 | void preparePath(SkPath* path) override { |
| 288 | SkRRect rr; |
| 289 | rr.setRectXY({0, 0, 100, 100}, 20, 30); |
| 290 | path->addRRect(rr); |
| 291 | } |
| 292 | }; |
| 293 | DEF_BENCH( return new RRectConvexityBench; ) |
| 294 | |