1 /*
2 * Copyright 2016 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 "ColorCodecBench.h"
9 #include "Resources.h"
10 #include "SkCodec.h"
11 #include "SkCodecPriv.h"
12 #include "SkColorSpace_XYZ.h"
13 #include "SkColorSpaceXform.h"
14 #include "SkCommandLineFlags.h"
15
16 DEFINE_bool(xform_only, false, "Only time the color xform, do not include the decode time");
17 DEFINE_bool(srgb, false, "Convert to srgb dst space");
18 DEFINE_bool(nonstd, false, "Convert to non-standard dst space");
19 DEFINE_bool(half, false, "Convert to half floats");
20
ColorCodecBench(const char * name,sk_sp<SkData> encoded)21 ColorCodecBench::ColorCodecBench(const char* name, sk_sp<SkData> encoded)
22 : fEncoded(std::move(encoded))
23 {
24 fName.appendf("Color%s", FLAGS_xform_only ? "Xform" : "Codec");
25 fName.appendf("_%s", name);
26 }
27
onGetName()28 const char* ColorCodecBench::onGetName() {
29 return fName.c_str();
30 }
31
isSuitableFor(Backend backend)32 bool ColorCodecBench::isSuitableFor(Backend backend) {
33 return kNonRendering_Backend == backend;
34 }
35
decodeAndXform()36 void ColorCodecBench::decodeAndXform() {
37 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(fEncoded));
38 SkASSERT(codec);
39
40 #ifdef SK_DEBUG
41 SkCodec::Result result =
42 #endif
43 codec->getPixels(fDstInfo, fDst.get(), fDstInfo.minRowBytes());
44 SkASSERT(SkCodec::kSuccess == result);
45 }
46
xformOnly()47 void ColorCodecBench::xformOnly() {
48 std::unique_ptr<SkColorSpaceXform> xform = SkColorSpaceXform::New(fSrcSpace.get(),
49 fDstSpace.get());
50 SkASSERT(xform);
51
52 void* dst = fDst.get();
53 void* src = fSrc.get();
54 for (int y = 0; y < fSrcInfo.height(); y++) {
55 SkAssertResult(xform->apply(select_xform_format(fDstInfo.colorType()), dst,
56 SkColorSpaceXform::kRGBA_8888_ColorFormat, src,
57 fSrcInfo.width(), fDstInfo.alphaType()));
58 dst = SkTAddOffset<void>(dst, fDstInfo.minRowBytes());
59 src = SkTAddOffset<void>(src, fSrcInfo.minRowBytes());
60 }
61 }
62
onDelayedSetup()63 void ColorCodecBench::onDelayedSetup() {
64 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(fEncoded));
65 fSrcInfo = codec->getInfo().makeColorType(kRGBA_8888_SkColorType);
66 fDstInfo = fSrcInfo;
67
68 fDstSpace = nullptr;
69 if (FLAGS_srgb) {
70 fDstSpace = SkColorSpace::MakeSRGB();
71 } else if (FLAGS_nonstd) {
72 SkColorSpaceTransferFn gamma;
73 gamma.fA = 1.0f;
74 gamma.fB = gamma.fC = gamma.fD = gamma.fE = gamma.fF = 0.0f;
75 gamma.fG = 4.0f;
76 SkMatrix44 matrix = SkMatrix44(SkMatrix44::kUninitialized_Constructor);
77 matrix.set3x3(0.30f, 0.31f, 0.28f, 0.32f, 0.33f, 0.29f, 0.27f, 0.30f, 0.30f);
78 fDstSpace = SkColorSpace::MakeRGB(gamma, matrix);
79 } else {
80 sk_sp<SkData> dstData = SkData::MakeFromFileName(
81 GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
82 SkASSERT(dstData);
83 fDstSpace = SkColorSpace::MakeICC(dstData->data(), dstData->size());
84 }
85 SkASSERT(fDstSpace);
86 fDstInfo = fDstInfo.makeColorSpace(fDstSpace);
87
88 if (FLAGS_half) {
89 fDstInfo = fDstInfo.makeColorType(kRGBA_F16_SkColorType);
90 SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(fDstSpace)->type());
91 fDstSpace = static_cast<SkColorSpace_XYZ*>(fDstSpace.get())->makeLinearGamma();
92 }
93
94 fDst.reset(fDstInfo.getSafeSize(fDstInfo.minRowBytes()));
95
96 if (FLAGS_xform_only) {
97 fSrc.reset(fSrcInfo.getSafeSize(fSrcInfo.minRowBytes()));
98 fSrcSpace = codec->getInfo().refColorSpace();
99 codec->getPixels(fSrcInfo, fSrc.get(), fSrcInfo.minRowBytes());
100 }
101 }
102
onDraw(int n,SkCanvas *)103 void ColorCodecBench::onDraw(int n, SkCanvas*) {
104 for (int i = 0; i < n; i++) {
105 if (FLAGS_xform_only) {
106 this->xformOnly();
107 } else {
108 this->decodeAndXform();
109 }
110 }
111 }
112