OLD | NEW |
1 #include "Benchmark.h" | 1 #include "Benchmark.h" |
2 #include "SkPMFloat.h" | 2 #include "SkPMFloat.h" |
3 #include "SkRandom.h" | 3 |
| 4 // Used to prevent the compiler from optimizing away the whole loop. |
| 5 volatile 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. |
| 9 static uint32_t lcg_rand(uint32_t* seed) { |
| 10 *seed *= 1664525; |
| 11 *seed += 1013904223; |
| 12 return *seed; |
| 13 } |
4 | 14 |
5 struct PMFloatBench : public Benchmark { | 15 struct PMFloatBench : public Benchmark { |
6 explicit PMFloatBench(bool clamp) : fClamp(clamp) {} | 16 explicit PMFloatBench(bool clamp) : fClamp(clamp) {} |
7 | 17 |
8 const char* onGetName() SK_OVERRIDE { return fClamp ? "SkPMFloat_clamp" : "S
kPMFloat_get"; } | 18 const char* onGetName() SK_OVERRIDE { return fClamp ? "SkPMFloat_clamp" : "S
kPMFloat_get"; } |
9 bool isSuitableFor(Backend backend) SK_OVERRIDE { return backend == kNonRend
ering_Backend; } | 19 bool isSuitableFor(Backend backend) SK_OVERRIDE { return backend == kNonRend
ering_Backend; } |
10 | 20 |
11 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { | 21 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
12 SkRandom rand; | 22 // Unlike blackhole, junk can and probably will be a register. |
| 23 uint32_t junk = 0; |
| 24 uint32_t seed = 0; |
13 for (int i = 0; i < loops; i++) { | 25 for (int i = 0; i < loops; i++) { |
14 SkPMColor c = SkPreMultiplyColor(rand.nextU()); | 26 #ifdef SK_DEBUG |
| 27 // Our SkASSERTs will remind us that it's technically required that
we premultiply. |
| 28 SkPMColor c = SkPreMultiplyColor(lcg_rand(&seed)); |
| 29 #else |
| 30 // But it's a lot faster not to, and this code won't really mind the
non-PM colors. |
| 31 SkPMColor c = lcg_rand(&seed); |
| 32 #endif |
15 SkPMFloat pmf; | 33 SkPMFloat pmf; |
16 pmf.set(c); | 34 pmf.set(c); |
17 SkPMColor back = fClamp ? pmf.clamped() : pmf.get(); | 35 SkPMColor back = fClamp ? pmf.clamped() : pmf.get(); |
18 if (c != back) { SkFAIL("no joy"); } // This conditional makes this
not compile away. | 36 junk ^= back; |
19 } | 37 } |
| 38 blackhole ^= junk; |
20 } | 39 } |
21 | 40 |
22 bool fClamp; | 41 bool fClamp; |
23 }; | 42 }; |
24 DEF_BENCH(return new PMFloatBench( true);) | 43 DEF_BENCH(return new PMFloatBench( true);) |
25 DEF_BENCH(return new PMFloatBench(false);) | 44 DEF_BENCH(return new PMFloatBench(false);) |
OLD | NEW |