skia2/bench/ColorSpaceXformBench.cpp
Brian Osman 82ebe04caf Reland "Add SkColorSpace factory from 3x3 row-major gamut and transfer function"
Moved named common transfer functions and gamuts to constexpr values in
SkColorSpace.h, in SkNamedTransferFn and SkNamedGamut namespaces.

Converted nearly all SkColorSpace::MakeRGB calls within Skia to use the
new factory with the named values. Multiple clients want a way to
extract named transfer function and gamut - this still doesn't provide
that, but this may be a better path forward for honestly advertising how
SkColorSpace works internally.

Originally landed as:
https://skia.googlesource.com/skia/+/a9549ab31630fc244094e6f1692371cbaf87f666

Re-landing with a new serialization format, but maintaining ability to
load old serialized color spaces, for SKP compatibility.

Bug: skia:
Change-Id: Ib84a6e1cd5d7d9816175773fdbaff2ca32658667
Reviewed-on: https://skia-review.googlesource.com/c/181176
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2019-01-05 20:06:51 +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(SkNamedTransferFn::kSRGB,
SkNamedGamut::kDCIP3);
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};)