mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "SkColor.h" |
| 10 | #include "SkNx.h" |
| 11 | |
mtklein | 77aa62c | 2016-02-09 11:03:40 -0800 | [diff] [blame] | 12 | // Writing into this array prevents the loops from being compiled away. |
| 13 | static volatile float blackhole[4]; |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 14 | |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame] | 15 | template <typename T> |
| 16 | struct Sk4fRoundtripBench : public Benchmark { |
| 17 | Sk4fRoundtripBench() {} |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 18 | |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame] | 19 | const char* onGetName() override { |
| 20 | switch (sizeof(T)) { |
| 21 | case 1: return "Sk4f_roundtrip_u8"; |
| 22 | case 2: return "Sk4f_roundtrip_u16"; |
mtklein | 77aa62c | 2016-02-09 11:03:40 -0800 | [diff] [blame] | 23 | case 4: return "Sk4f_roundtrip_int"; |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame] | 24 | } |
| 25 | SkASSERT(false); |
| 26 | return ""; |
| 27 | } |
| 28 | |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 29 | bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } |
| 30 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 31 | void onDraw(int loops, SkCanvas* canvas) override { |
mtklein | 77aa62c | 2016-02-09 11:03:40 -0800 | [diff] [blame] | 32 | Sk4f fs(1,2,3,4); |
| 33 | while (loops --> 0) { |
| 34 | fs = SkNx_cast<float>(SkNx_cast<T>(fs)); |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 35 | } |
mtklein | 77aa62c | 2016-02-09 11:03:40 -0800 | [diff] [blame] | 36 | fs.store((float*)blackhole); |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 37 | } |
| 38 | }; |
mtklein | 2d340f2 | 2016-02-06 19:38:39 -0800 | [diff] [blame] | 39 | DEF_BENCH(return new Sk4fRoundtripBench<uint8_t>;) |
| 40 | DEF_BENCH(return new Sk4fRoundtripBench<uint16_t>;) |
mtklein | 77aa62c | 2016-02-09 11:03:40 -0800 | [diff] [blame] | 41 | DEF_BENCH(return new Sk4fRoundtripBench<int>;) |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 42 | |
mtklein | 126626e | 2016-02-09 15:41:36 -0800 | [diff] [blame^] | 43 | struct Sk4fFloorBench : public Benchmark { |
| 44 | Sk4fFloorBench() {} |
| 45 | |
| 46 | const char* onGetName() override { return "Sk4f_floor"; } |
| 47 | bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } |
| 48 | |
| 49 | void onDraw(int loops, SkCanvas* canvas) override { |
| 50 | Sk4f fs(1,2,3,4); |
| 51 | while (loops --> 0) { |
| 52 | fs = fs.floor(); |
| 53 | } |
| 54 | fs.store((float*)blackhole); |
| 55 | } |
| 56 | }; |
| 57 | DEF_BENCH(return new Sk4fFloorBench;) |
| 58 | |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 59 | struct Sk4fGradientBench : public Benchmark { |
| 60 | const char* onGetName() override { return "Sk4f_gradient"; } |
| 61 | bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } |
| 62 | |
| 63 | SkPMColor fDevice[100]; |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 64 | void onDraw(int loops, SkCanvas*) override { |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 65 | Sk4f c0(0,0,255,255), |
| 66 | c1(255,0,0,255), |
| 67 | dc = c1 - c0, |
| 68 | fx(0.1f), |
| 69 | dx(0.002f), |
| 70 | dcdx(dc*dx), |
| 71 | dcdx4(dcdx+dcdx+dcdx+dcdx); |
| 72 | |
| 73 | for (int n = 0; n < loops; n++) { |
| 74 | Sk4f a = c0 + dc*fx + Sk4f(0.5f), // add an extra 0.5f to get rounding for free. |
| 75 | b = a + dcdx, |
| 76 | c = b + dcdx, |
| 77 | d = c + dcdx; |
| 78 | for (size_t i = 0; i < SK_ARRAY_COUNT(fDevice); i += 4) { |
mtklein | 6f37b4a | 2015-12-14 11:25:18 -0800 | [diff] [blame] | 79 | Sk4f_ToBytes((uint8_t*)(fDevice+i), a, b, c, d); |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 80 | a = a + dcdx4; |
| 81 | b = b + dcdx4; |
| 82 | c = c + dcdx4; |
| 83 | d = d + dcdx4; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | }; |
| 88 | DEF_BENCH(return new Sk4fGradientBench;) |