1 /* 2 * Copyright 2017 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "Benchmark.h" 9 #include "SkColor.h" 10 #include "SkColorSpaceXformer.h" 11 #include "SkColorSpaceXformSteps.h" 12 #include "SkMakeUnique.h" 13 #include "SkRandom.h" 14 15 enum class Mode { steps, xformer }; 16 17 struct ColorSpaceXformBench : public Benchmark { ColorSpaceXformBenchColorSpaceXformBench18 ColorSpaceXformBench(Mode mode) : fMode(mode) {} 19 20 const Mode fMode; 21 22 std::unique_ptr<SkColorSpaceXformSteps> fSteps; 23 std::unique_ptr<SkColorSpaceXformer> fXformer; 24 onGetNameColorSpaceXformBench25 const char* onGetName() override { 26 switch (fMode) { 27 case Mode::steps : return "ColorSpaceXformBench_steps"; 28 case Mode::xformer: return "ColorSpaceXformBench_xformer"; 29 } 30 return ""; 31 } 32 isSuitableForColorSpaceXformBench33 bool isSuitableFor(Backend backend) override { return kNonRendering_Backend == backend; } 34 onDelayedSetupColorSpaceXformBench35 void onDelayedSetup() override { 36 sk_sp<SkColorSpace> src = SkColorSpace::MakeSRGB(), 37 dst = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, 38 SkNamedGamut::kDCIP3); 39 40 fSteps = skstd::make_unique<SkColorSpaceXformSteps>(src.get(), kOpaque_SkAlphaType, 41 dst.get(), kPremul_SkAlphaType); 42 fXformer = SkColorSpaceXformer::Make(dst); // src is implicitly sRGB, what we want anyway 43 } 44 onDrawColorSpaceXformBench45 void onDraw(int n, SkCanvas* canvas) override { 46 volatile SkColor junk = 0; 47 SkRandom rand; 48 49 for (int i = 0; i < n; i++) { 50 SkColor src = rand.nextU(), 51 dst; 52 switch (fMode) { 53 case Mode::steps: { 54 SkColor4f rgba = SkColor4f::FromColor(src); 55 fSteps->apply(rgba.vec()); 56 dst = rgba.toSkColor(); 57 } break; 58 59 case Mode::xformer: { 60 dst = fXformer->apply(src); 61 } break; 62 } 63 64 if (false && i == 0) { 65 SkDebugf("%x ~~> %x\n", src, dst); 66 } 67 68 junk ^= dst; 69 } 70 } 71 }; 72 73 DEF_BENCH(return new ColorSpaceXformBench{Mode::steps };) 74 DEF_BENCH(return new ColorSpaceXformBench{Mode::xformer};) 75