blob: 1da667f4430ff317ef4e261167def3e1db1570ce [file] [log] [blame]
mtkleina2f4be72015-02-23 10:04:34 -08001#include "Benchmark.h"
2#include "SkPMFloat.h"
mtklein60ff4582015-03-03 08:03:27 -08003
4// Used to prevent the compiler from optimizing away the whole loop.
5volatile uint32_t blackhole = 0;
6
7// Not a great random number generator, but it's very fast.
8// The code we're measuring is quite fast, so low overhead is essential.
9static uint32_t lcg_rand(uint32_t* seed) {
10 *seed *= 1664525;
11 *seed += 1013904223;
12 return *seed;
13}
mtkleina2f4be72015-02-23 10:04:34 -080014
mtklein548bf382015-03-05 11:31:59 -080015// I'm having better luck getting these to constant-propagate away as template parameters.
16template <bool kClamp, bool kWide>
mtkleina2f4be72015-02-23 10:04:34 -080017struct PMFloatBench : public Benchmark {
mtklein548bf382015-03-05 11:31:59 -080018 PMFloatBench() {}
mtkleina2f4be72015-02-23 10:04:34 -080019
mtklein548bf382015-03-05 11:31:59 -080020 const char* onGetName() SK_OVERRIDE {
21 switch (kClamp << 1 | kWide) {
22 case 0: return "SkPMFloat_get_1x";
23 case 1: return "SkPMFloat_get_4x";
24 case 2: return "SkPMFloat_clamp_1x";
25 case 3: return "SkPMFloat_clamp_4x";
26 }
27 SkFAIL("unreachable");
28 return "oh bother";
29 }
mtkleina2f4be72015-02-23 10:04:34 -080030 bool isSuitableFor(Backend backend) SK_OVERRIDE { return backend == kNonRendering_Backend; }
31
32 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
mtklein60ff4582015-03-03 08:03:27 -080033 // Unlike blackhole, junk can and probably will be a register.
34 uint32_t junk = 0;
35 uint32_t seed = 0;
mtkleina2f4be72015-02-23 10:04:34 -080036 for (int i = 0; i < loops; i++) {
mtklein548bf382015-03-05 11:31:59 -080037 SkPMColor colors[4];
mtklein60ff4582015-03-03 08:03:27 -080038 #ifdef SK_DEBUG
mtklein548bf382015-03-05 11:31:59 -080039 for (int i = 0; i < 4; i++) {
40 // Our SkASSERTs will remind us that it's technically required that we premultiply.
41 colors[i] = SkPreMultiplyColor(lcg_rand(&seed));
42 }
mtklein60ff4582015-03-03 08:03:27 -080043 #else
44 // But it's a lot faster not to, and this code won't really mind the non-PM colors.
mtklein548bf382015-03-05 11:31:59 -080045 (void)lcg_rand(&seed);
46 colors[0] = seed + 0;
47 colors[1] = seed + 1;
48 colors[2] = seed + 2;
49 colors[3] = seed + 3;
mtklein60ff4582015-03-03 08:03:27 -080050 #endif
mtklein548bf382015-03-05 11:31:59 -080051
52 SkPMFloat floats[4];
53 if (kWide) {
54 SkPMFloat::From4PMColors(floats, colors);
55 } else {
56 for (int i = 0; i < 4; i++) {
57 floats[i] = SkPMFloat::FromPMColor(colors[i]);
58 }
59 }
60
61 SkPMColor back[4];
62 switch (kClamp << 1 | kWide) {
63 case 0: for (int i = 0; i < 4; i++) { back[i] = floats[i].get(); } break;
64 case 1: SkPMFloat::To4PMColors(back, floats); break;
65 case 2: for (int i = 0; i < 4; i++) { back[i] = floats[i].clamped(); } break;
66 case 3: SkPMFloat::ClampTo4PMColors(back, floats); break;
67 }
68 for (int i = 0; i < 4; i++) {
69 junk ^= back[i];
70 }
mtkleina2f4be72015-02-23 10:04:34 -080071 }
mtklein60ff4582015-03-03 08:03:27 -080072 blackhole ^= junk;
mtkleina2f4be72015-02-23 10:04:34 -080073 }
mtkleina2f4be72015-02-23 10:04:34 -080074};
mtklein548bf382015-03-05 11:31:59 -080075
76// Extra () help DEF_BENCH not get confused by the comma inside the <>.
77DEF_BENCH(return (new PMFloatBench< true, true>);)
78DEF_BENCH(return (new PMFloatBench<false, true>);)
79DEF_BENCH(return (new PMFloatBench< true, false>);)
80DEF_BENCH(return (new PMFloatBench<false, false>);)