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. |
| 23 | template <bool kClamp, bool kWide> |
mtklein | 02fd592 | 2015-03-25 18:13:02 -0700 | [diff] [blame] | 24 | struct PMFloatGetSetBench : public Benchmark { |
| 25 | PMFloatGetSetBench() {} |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 26 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | const char* onGetName() override { |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 28 | switch (kClamp << 1 | kWide) { |
| 29 | case 0: return "SkPMFloat_get_1x"; |
| 30 | case 1: return "SkPMFloat_get_4x"; |
| 31 | case 2: return "SkPMFloat_clamp_1x"; |
| 32 | case 3: return "SkPMFloat_clamp_4x"; |
| 33 | } |
| 34 | SkFAIL("unreachable"); |
| 35 | return "oh bother"; |
| 36 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 37 | bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | void onDraw(const int loops, SkCanvas* canvas) override { |
mtklein | 60ff458 | 2015-03-03 08:03:27 -0800 | [diff] [blame] | 40 | // Unlike blackhole, junk can and probably will be a register. |
| 41 | uint32_t junk = 0; |
| 42 | uint32_t seed = 0; |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 43 | for (int i = 0; i < loops; i++) { |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 44 | SkPMColor colors[4]; |
mtklein | 60ff458 | 2015-03-03 08:03:27 -0800 | [diff] [blame] | 45 | #ifdef SK_DEBUG |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 46 | for (int i = 0; i < 4; i++) { |
| 47 | // Our SkASSERTs will remind us that it's technically required that we premultiply. |
| 48 | colors[i] = SkPreMultiplyColor(lcg_rand(&seed)); |
| 49 | } |
mtklein | 60ff458 | 2015-03-03 08:03:27 -0800 | [diff] [blame] | 50 | #else |
| 51 | // But it's a lot faster not to, and this code won't really mind the non-PM colors. |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 52 | (void)lcg_rand(&seed); |
| 53 | colors[0] = seed + 0; |
| 54 | colors[1] = seed + 1; |
| 55 | colors[2] = seed + 2; |
| 56 | colors[3] = seed + 3; |
mtklein | 60ff458 | 2015-03-03 08:03:27 -0800 | [diff] [blame] | 57 | #endif |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 58 | |
mtklein | 15391ee | 2015-03-25 13:43:34 -0700 | [diff] [blame] | 59 | SkPMFloat fa,fb,fc,fd; |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 60 | if (kWide) { |
mtklein | 15391ee | 2015-03-25 13:43:34 -0700 | [diff] [blame] | 61 | SkPMFloat::From4PMColors(colors, &fa, &fb, &fc, &fd); |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 62 | } else { |
mtklein | 15391ee | 2015-03-25 13:43:34 -0700 | [diff] [blame] | 63 | fa = SkPMFloat::FromPMColor(colors[0]); |
| 64 | fb = SkPMFloat::FromPMColor(colors[1]); |
| 65 | fc = SkPMFloat::FromPMColor(colors[2]); |
| 66 | fd = SkPMFloat::FromPMColor(colors[3]); |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | SkPMColor back[4]; |
| 70 | switch (kClamp << 1 | kWide) { |
mtklein | 15391ee | 2015-03-25 13:43:34 -0700 | [diff] [blame] | 71 | case 0: { |
mtklein | 3d62683 | 2015-04-03 07:05:20 -0700 | [diff] [blame] | 72 | back[0] = fa.round(); |
| 73 | back[1] = fb.round(); |
| 74 | back[2] = fc.round(); |
| 75 | back[3] = fd.round(); |
mtklein | 15391ee | 2015-03-25 13:43:34 -0700 | [diff] [blame] | 76 | } break; |
mtklein | 3d62683 | 2015-04-03 07:05:20 -0700 | [diff] [blame] | 77 | case 1: SkPMFloat::RoundTo4PMColors(fa, fb, fc, fd, back); break; |
mtklein | 15391ee | 2015-03-25 13:43:34 -0700 | [diff] [blame] | 78 | case 2: { |
mtklein | 3d62683 | 2015-04-03 07:05:20 -0700 | [diff] [blame] | 79 | back[0] = fa.roundClamp(); |
| 80 | back[1] = fb.roundClamp(); |
| 81 | back[2] = fc.roundClamp(); |
| 82 | back[3] = fd.roundClamp(); |
mtklein | 15391ee | 2015-03-25 13:43:34 -0700 | [diff] [blame] | 83 | } break; |
mtklein | 3d62683 | 2015-04-03 07:05:20 -0700 | [diff] [blame] | 84 | case 3: SkPMFloat::RoundClampTo4PMColors(fa, fb, fc, fd, back); break; |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 85 | } |
| 86 | for (int i = 0; i < 4; i++) { |
| 87 | junk ^= back[i]; |
| 88 | } |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 89 | } |
mtklein | 60ff458 | 2015-03-03 08:03:27 -0800 | [diff] [blame] | 90 | blackhole ^= junk; |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 91 | } |
mtklein | a2f4be7 | 2015-02-23 10:04:34 -0800 | [diff] [blame] | 92 | }; |
mtklein | 548bf38 | 2015-03-05 11:31:59 -0800 | [diff] [blame] | 93 | |
| 94 | // Extra () help DEF_BENCH not get confused by the comma inside the <>. |
mtklein | 02fd592 | 2015-03-25 18:13:02 -0700 | [diff] [blame] | 95 | DEF_BENCH(return (new PMFloatGetSetBench< true, true>);) |
| 96 | DEF_BENCH(return (new PMFloatGetSetBench<false, true>);) |
| 97 | DEF_BENCH(return (new PMFloatGetSetBench< true, false>);) |
| 98 | DEF_BENCH(return (new PMFloatGetSetBench<false, false>);) |
| 99 | |
| 100 | struct PMFloatGradientBench : public Benchmark { |
| 101 | const char* onGetName() override { return "PMFloat_gradient"; } |
| 102 | bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } |
| 103 | |
| 104 | SkPMColor fDevice[100]; |
| 105 | void onDraw(const int loops, SkCanvas*) override { |
mtklein | 0340df5 | 2015-03-31 08:17:00 -0700 | [diff] [blame] | 106 | Sk4f c0 = SkPMFloat::FromARGB(255, 255, 0, 0), |
mtklein | 02fd592 | 2015-03-25 18:13:02 -0700 | [diff] [blame] | 107 | c1 = SkPMFloat::FromARGB(255, 0, 0, 255), |
| 108 | dc = c1 - c0, |
| 109 | fx(0.1f), |
| 110 | dx(0.002f), |
| 111 | dcdx(dc*dx), |
| 112 | dcdx4(dcdx+dcdx+dcdx+dcdx); |
| 113 | |
| 114 | for (int n = 0; n < loops; n++) { |
mtklein | 0340df5 | 2015-03-31 08:17:00 -0700 | [diff] [blame] | 115 | Sk4f a = c0 + dc*fx + Sk4f(0.5f), // The +0.5f lets us call trunc() instead of get(). |
mtklein | 02fd592 | 2015-03-25 18:13:02 -0700 | [diff] [blame] | 116 | b = a + dcdx, |
| 117 | c = b + dcdx, |
| 118 | d = c + dcdx; |
| 119 | for (size_t i = 0; i < SK_ARRAY_COUNT(fDevice); i += 4) { |
mtklein | 3d4c4a5 | 2015-03-26 12:32:29 -0700 | [diff] [blame] | 120 | fDevice[i+0] = SkPMFloat(a).trunc(); |
| 121 | fDevice[i+1] = SkPMFloat(b).trunc(); |
| 122 | fDevice[i+2] = SkPMFloat(c).trunc(); |
| 123 | fDevice[i+3] = SkPMFloat(d).trunc(); |
mtklein | 02fd592 | 2015-03-25 18:13:02 -0700 | [diff] [blame] | 124 | a += dcdx4; |
| 125 | b += dcdx4; |
| 126 | c += dcdx4; |
| 127 | d += dcdx4; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | DEF_BENCH(return new PMFloatGradientBench;) |