skia2/bench/ColorSpaceXformBench.cpp
Brian Osman 9ba9d7d1cb Use SkColor4f functionality, rather than SkPM4f.h inline functions
Gradient code still uses some of the helpers, otherwise they could
be moved to SkColor.cpp.

Bug: skia:
Change-Id: I2e5076e9cc81669440fbf29a95113e45642b307f
Reviewed-on: https://skia-review.googlesource.com/c/165307
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2018-10-26 19:04:18 +00:00

75 lines
2.3 KiB
C++

/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Benchmark.h"
#include "SkColor.h"
#include "SkColorSpaceXformer.h"
#include "SkColorSpaceXformSteps.h"
#include "SkMakeUnique.h"
#include "SkRandom.h"
enum class Mode { steps, xformer };
struct ColorSpaceXformBench : public Benchmark {
ColorSpaceXformBench(Mode mode) : fMode(mode) {}
const Mode fMode;
std::unique_ptr<SkColorSpaceXformSteps> fSteps;
std::unique_ptr<SkColorSpaceXformer> fXformer;
const char* onGetName() override {
switch (fMode) {
case Mode::steps : return "ColorSpaceXformBench_steps";
case Mode::xformer: return "ColorSpaceXformBench_xformer";
}
return "";
}
bool isSuitableFor(Backend backend) override { return kNonRendering_Backend == backend; }
void onDelayedSetup() override {
sk_sp<SkColorSpace> src = SkColorSpace::MakeSRGB(),
dst = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
SkColorSpace::kDCIP3_D65_Gamut);
fSteps = skstd::make_unique<SkColorSpaceXformSteps>(src.get(), kOpaque_SkAlphaType,
dst.get(), kPremul_SkAlphaType);
fXformer = SkColorSpaceXformer::Make(dst); // src is implicitly sRGB, what we want anyway
}
void onDraw(int n, SkCanvas* canvas) override {
volatile SkColor junk = 0;
SkRandom rand;
for (int i = 0; i < n; i++) {
SkColor src = rand.nextU(),
dst;
switch (fMode) {
case Mode::steps: {
SkColor4f rgba = SkColor4f::FromColor(src);
fSteps->apply(rgba.vec());
dst = rgba.toSkColor();
} break;
case Mode::xformer: {
dst = fXformer->apply(src);
} break;
}
if (false && i == 0) {
SkDebugf("%x ~~> %x\n", src, dst);
}
junk ^= dst;
}
}
};
DEF_BENCH(return new ColorSpaceXformBench{Mode::steps };)
DEF_BENCH(return new ColorSpaceXformBench{Mode::xformer};)