2015-03-25 20:43:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2015-02-23 18:04:34 +00:00
|
|
|
#include "Benchmark.h"
|
|
|
|
#include "SkPMFloat.h"
|
2015-03-03 16:03:27 +00:00
|
|
|
|
|
|
|
// Used to prevent the compiler from optimizing away the whole loop.
|
|
|
|
volatile uint32_t blackhole = 0;
|
|
|
|
|
|
|
|
// Not a great random number generator, but it's very fast.
|
|
|
|
// The code we're measuring is quite fast, so low overhead is essential.
|
|
|
|
static uint32_t lcg_rand(uint32_t* seed) {
|
|
|
|
*seed *= 1664525;
|
|
|
|
*seed += 1013904223;
|
|
|
|
return *seed;
|
|
|
|
}
|
2015-02-23 18:04:34 +00:00
|
|
|
|
2015-03-05 19:31:59 +00:00
|
|
|
// I'm having better luck getting these to constant-propagate away as template parameters.
|
|
|
|
template <bool kClamp, bool kWide>
|
2015-03-26 01:13:02 +00:00
|
|
|
struct PMFloatGetSetBench : public Benchmark {
|
|
|
|
PMFloatGetSetBench() {}
|
2015-02-23 18:04:34 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2015-03-05 19:31:59 +00:00
|
|
|
switch (kClamp << 1 | kWide) {
|
|
|
|
case 0: return "SkPMFloat_get_1x";
|
|
|
|
case 1: return "SkPMFloat_get_4x";
|
|
|
|
case 2: return "SkPMFloat_clamp_1x";
|
|
|
|
case 3: return "SkPMFloat_clamp_4x";
|
|
|
|
}
|
|
|
|
SkFAIL("unreachable");
|
|
|
|
return "oh bother";
|
|
|
|
}
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
|
2015-02-23 18:04:34 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(const int loops, SkCanvas* canvas) override {
|
2015-03-03 16:03:27 +00:00
|
|
|
// Unlike blackhole, junk can and probably will be a register.
|
|
|
|
uint32_t junk = 0;
|
|
|
|
uint32_t seed = 0;
|
2015-02-23 18:04:34 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
2015-03-05 19:31:59 +00:00
|
|
|
SkPMColor colors[4];
|
2015-03-03 16:03:27 +00:00
|
|
|
#ifdef SK_DEBUG
|
2015-03-05 19:31:59 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
// Our SkASSERTs will remind us that it's technically required that we premultiply.
|
|
|
|
colors[i] = SkPreMultiplyColor(lcg_rand(&seed));
|
|
|
|
}
|
2015-03-03 16:03:27 +00:00
|
|
|
#else
|
|
|
|
// But it's a lot faster not to, and this code won't really mind the non-PM colors.
|
2015-03-05 19:31:59 +00:00
|
|
|
(void)lcg_rand(&seed);
|
|
|
|
colors[0] = seed + 0;
|
|
|
|
colors[1] = seed + 1;
|
|
|
|
colors[2] = seed + 2;
|
|
|
|
colors[3] = seed + 3;
|
2015-03-03 16:03:27 +00:00
|
|
|
#endif
|
2015-03-05 19:31:59 +00:00
|
|
|
|
2015-03-25 20:43:34 +00:00
|
|
|
SkPMFloat fa,fb,fc,fd;
|
2015-03-05 19:31:59 +00:00
|
|
|
if (kWide) {
|
2015-03-25 20:43:34 +00:00
|
|
|
SkPMFloat::From4PMColors(colors, &fa, &fb, &fc, &fd);
|
2015-03-05 19:31:59 +00:00
|
|
|
} else {
|
2015-03-25 20:43:34 +00:00
|
|
|
fa = SkPMFloat::FromPMColor(colors[0]);
|
|
|
|
fb = SkPMFloat::FromPMColor(colors[1]);
|
|
|
|
fc = SkPMFloat::FromPMColor(colors[2]);
|
|
|
|
fd = SkPMFloat::FromPMColor(colors[3]);
|
2015-03-05 19:31:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkPMColor back[4];
|
|
|
|
switch (kClamp << 1 | kWide) {
|
2015-03-25 20:43:34 +00:00
|
|
|
case 0: {
|
2015-04-03 14:05:20 +00:00
|
|
|
back[0] = fa.round();
|
|
|
|
back[1] = fb.round();
|
|
|
|
back[2] = fc.round();
|
|
|
|
back[3] = fd.round();
|
2015-03-25 20:43:34 +00:00
|
|
|
} break;
|
2015-04-03 14:05:20 +00:00
|
|
|
case 1: SkPMFloat::RoundTo4PMColors(fa, fb, fc, fd, back); break;
|
2015-03-25 20:43:34 +00:00
|
|
|
case 2: {
|
2015-04-03 14:05:20 +00:00
|
|
|
back[0] = fa.roundClamp();
|
|
|
|
back[1] = fb.roundClamp();
|
|
|
|
back[2] = fc.roundClamp();
|
|
|
|
back[3] = fd.roundClamp();
|
2015-03-25 20:43:34 +00:00
|
|
|
} break;
|
2015-04-03 14:05:20 +00:00
|
|
|
case 3: SkPMFloat::RoundClampTo4PMColors(fa, fb, fc, fd, back); break;
|
2015-03-05 19:31:59 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
junk ^= back[i];
|
|
|
|
}
|
2015-02-23 18:04:34 +00:00
|
|
|
}
|
2015-03-03 16:03:27 +00:00
|
|
|
blackhole ^= junk;
|
2015-02-23 18:04:34 +00:00
|
|
|
}
|
|
|
|
};
|
2015-03-05 19:31:59 +00:00
|
|
|
|
|
|
|
// Extra () help DEF_BENCH not get confused by the comma inside the <>.
|
2015-03-26 01:13:02 +00:00
|
|
|
DEF_BENCH(return (new PMFloatGetSetBench< true, true>);)
|
|
|
|
DEF_BENCH(return (new PMFloatGetSetBench<false, true>);)
|
|
|
|
DEF_BENCH(return (new PMFloatGetSetBench< true, false>);)
|
|
|
|
DEF_BENCH(return (new PMFloatGetSetBench<false, false>);)
|
|
|
|
|
|
|
|
struct PMFloatGradientBench : public Benchmark {
|
|
|
|
const char* onGetName() override { return "PMFloat_gradient"; }
|
|
|
|
bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
|
|
|
|
|
|
|
|
SkPMColor fDevice[100];
|
|
|
|
void onDraw(const int loops, SkCanvas*) override {
|
2015-03-31 15:17:00 +00:00
|
|
|
Sk4f c0 = SkPMFloat::FromARGB(255, 255, 0, 0),
|
2015-03-26 01:13:02 +00:00
|
|
|
c1 = SkPMFloat::FromARGB(255, 0, 0, 255),
|
|
|
|
dc = c1 - c0,
|
|
|
|
fx(0.1f),
|
|
|
|
dx(0.002f),
|
|
|
|
dcdx(dc*dx),
|
|
|
|
dcdx4(dcdx+dcdx+dcdx+dcdx);
|
|
|
|
|
|
|
|
for (int n = 0; n < loops; n++) {
|
2015-03-31 15:17:00 +00:00
|
|
|
Sk4f a = c0 + dc*fx + Sk4f(0.5f), // The +0.5f lets us call trunc() instead of get().
|
2015-03-26 01:13:02 +00:00
|
|
|
b = a + dcdx,
|
|
|
|
c = b + dcdx,
|
|
|
|
d = c + dcdx;
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fDevice); i += 4) {
|
2015-03-26 19:32:29 +00:00
|
|
|
fDevice[i+0] = SkPMFloat(a).trunc();
|
|
|
|
fDevice[i+1] = SkPMFloat(b).trunc();
|
|
|
|
fDevice[i+2] = SkPMFloat(c).trunc();
|
|
|
|
fDevice[i+3] = SkPMFloat(d).trunc();
|
2015-03-26 01:13:02 +00:00
|
|
|
a += dcdx4;
|
|
|
|
b += dcdx4;
|
|
|
|
c += dcdx4;
|
|
|
|
d += dcdx4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_BENCH(return new PMFloatGradientBench;)
|