mtklein | 15391ee | 2015-03-25 13:43:34 -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 | |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 8 | #include "Benchmark.h" |
| 9 | #include "SkPMFloat.h" |
mtklein | 60ff458 | 2015-03-03 08:03:27 -0800 | [diff] [blame] | 10 | |
| 11 | // Used to prevent the compiler from optimizing away the whole loop. |
| 12 | volatile uint32_t blackhole = 0; |
| 13 | |
| 14 | // Not a great random number generator, but it's very fast. |
| 15 | // The code we're measuring is quite fast, so low overhead is essential. |
| 16 | static uint32_t lcg_rand(uint32_t* seed) { |
| 17 | *seed *= 1664525; |
| 18 | *seed += 1013904223; |
| 19 | return *seed; |
| 20 | } |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 21 | |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 22 | // I'm having better luck getting these to constant-propagate away as template parameters. |
mtklein | e9a3e3c | 2015-06-25 08:56:28 -0700 | [diff] [blame^] | 23 | struct PMFloatRoundtripBench : public Benchmark { |
| 24 | PMFloatRoundtripBench() {} |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 25 | |
mtklein | e9a3e3c | 2015-06-25 08:56:28 -0700 | [diff] [blame^] | 26 | const char* onGetName() override { return "SkPMFloat_roundtrip"; } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | void onDraw(const int loops, SkCanvas* canvas) override { |
mtklein | 60ff458 | 2015-03-03 08:03:27 -0800 | [diff] [blame] | 30 | // Unlike blackhole, junk can and probably will be a register. |
| 31 | uint32_t junk = 0; |
| 32 | uint32_t seed = 0; |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 33 | for (int i = 0; i < loops; i++) { |
mtklein | e9a3e3c | 2015-06-25 08:56:28 -0700 | [diff] [blame^] | 34 | SkPMColor color; |
mtklein | 60ff458 | 2015-03-03 08:03:27 -0800 | [diff] [blame] | 35 | #ifdef SK_DEBUG |
mtklein | e9a3e3c | 2015-06-25 08:56:28 -0700 | [diff] [blame^] | 36 | // Our SkASSERTs will remind us that it's technically required that we premultiply. |
| 37 | color = SkPreMultiplyColor(lcg_rand(&seed)); |
mtklein | 60ff458 | 2015-03-03 08:03:27 -0800 | [diff] [blame] | 38 | #else |
| 39 | // But it's a lot faster not to, and this code won't really mind the non-PM colors. |
mtklein | e9a3e3c | 2015-06-25 08:56:28 -0700 | [diff] [blame^] | 40 | color = lcg_rand(&seed); |
mtklein | 60ff458 | 2015-03-03 08:03:27 -0800 | [diff] [blame] | 41 | #endif |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 42 | |
mtklein | e9a3e3c | 2015-06-25 08:56:28 -0700 | [diff] [blame^] | 43 | auto f = SkPMFloat::FromPMColor(color); |
| 44 | SkPMColor back = f.round(); |
| 45 | junk ^= back; |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 46 | } |
mtklein | 60ff458 | 2015-03-03 08:03:27 -0800 | [diff] [blame] | 47 | blackhole ^= junk; |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 48 | } |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 49 | }; |
mtklein | e9a3e3c | 2015-06-25 08:56:28 -0700 | [diff] [blame^] | 50 | DEF_BENCH(return new PMFloatRoundtripBench;) |
mtklein | 02fd592 | 2015-03-25 18:13:02 -0700 | [diff] [blame] | 51 | |
| 52 | struct PMFloatGradientBench : public Benchmark { |
| 53 | const char* onGetName() override { return "PMFloat_gradient"; } |
| 54 | bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } |
| 55 | |
| 56 | SkPMColor fDevice[100]; |
| 57 | void onDraw(const int loops, SkCanvas*) override { |
mtklein | e9a3e3c | 2015-06-25 08:56:28 -0700 | [diff] [blame^] | 58 | Sk4f c0 = SkPMFloat::FromARGB(1, 1, 0, 0), |
| 59 | c1 = SkPMFloat::FromARGB(1, 0, 0, 1), |
mtklein | 02fd592 | 2015-03-25 18:13:02 -0700 | [diff] [blame] | 60 | dc = c1 - c0, |
| 61 | fx(0.1f), |
| 62 | dx(0.002f), |
| 63 | dcdx(dc*dx), |
| 64 | dcdx4(dcdx+dcdx+dcdx+dcdx); |
| 65 | |
| 66 | for (int n = 0; n < loops; n++) { |
mtklein | e9a3e3c | 2015-06-25 08:56:28 -0700 | [diff] [blame^] | 67 | Sk4f a = c0 + dc*fx, |
mtklein | 02fd592 | 2015-03-25 18:13:02 -0700 | [diff] [blame] | 68 | b = a + dcdx, |
| 69 | c = b + dcdx, |
| 70 | d = c + dcdx; |
| 71 | for (size_t i = 0; i < SK_ARRAY_COUNT(fDevice); i += 4) { |
mtklein | e9a3e3c | 2015-06-25 08:56:28 -0700 | [diff] [blame^] | 72 | fDevice[i+0] = SkPMFloat(a).round(); |
| 73 | fDevice[i+1] = SkPMFloat(b).round(); |
| 74 | fDevice[i+2] = SkPMFloat(c).round(); |
| 75 | fDevice[i+3] = SkPMFloat(d).round(); |
mtklein | f2fe0e0 | 2015-06-10 08:57:28 -0700 | [diff] [blame] | 76 | a = a + dcdx4; |
| 77 | b = b + dcdx4; |
| 78 | c = c + dcdx4; |
| 79 | d = d + dcdx4; |
mtklein | 02fd592 | 2015-03-25 18:13:02 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | DEF_BENCH(return new PMFloatGradientBench;) |