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