brianosman | 54f30c1 | 2016-07-18 10:53:52 -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 "GrColorSpaceXform.h" |
| 9 | #include "SkColorSpace.h" |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 10 | #include "SkColorSpacePriv.h" |
brianosman | 9f97882 | 2016-07-27 05:25:26 -0700 | [diff] [blame] | 11 | #include "SkMatrix44.h" |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 12 | #include "glsl/GrGLSLColorSpaceXformHelper.h" |
| 13 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 14 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Brian Osman | 7c2114f | 2016-10-20 15:34:06 -0400 | [diff] [blame] | 15 | |
Brian Osman | 653f34d | 2018-06-14 11:44:02 -0400 | [diff] [blame] | 16 | sk_sp<GrColorSpaceXform> GrColorSpaceXform::Make(SkColorSpace* src, SkColorSpace* dst) { |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 17 | // No transformation is performed in legacy mode, until SkColorSpaceXformCanvas is gone |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 18 | if (!dst) { |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 19 | return nullptr; |
| 20 | } |
| 21 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 22 | // Treat null sources as sRGB (safe because sRGB is a global singleton) |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 23 | if (!src) { |
Brian Osman | 653f34d | 2018-06-14 11:44:02 -0400 | [diff] [blame] | 24 | src = SkColorSpace::MakeSRGB().get(); |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 25 | } |
| 26 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 27 | // TODO: Plumb source alpha type |
| 28 | SkColorSpaceXformSteps steps(src, kPremul_SkAlphaType, dst); |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 29 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 30 | return steps.flags.mask() == 0 ? nullptr /* Noop transform */ |
| 31 | : sk_make_sp<GrColorSpaceXform>(steps); |
| 32 | } |
| 33 | |
| 34 | sk_sp<GrColorSpaceXform> GrColorSpaceXform::MakeUnpremulToUnpremul(SkColorSpace* src, |
| 35 | SkColorSpace* dst) { |
| 36 | // No transformation is performed in legacy mode, until SkColorSpaceXformCanvas is gone |
| 37 | if (!dst) { |
Brian Osman | 653f34d | 2018-06-14 11:44:02 -0400 | [diff] [blame] | 38 | return nullptr; |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 39 | } |
Brian Osman | 653f34d | 2018-06-14 11:44:02 -0400 | [diff] [blame] | 40 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 41 | // Treat null sources as sRGB (safe because sRGB is a global singleton) |
| 42 | if (!src) { |
| 43 | src = SkColorSpace::MakeSRGB().get(); |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 44 | } |
Brian Osman | 7c2114f | 2016-10-20 15:34:06 -0400 | [diff] [blame] | 45 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 46 | SkColorSpaceXformSteps steps = SkColorSpaceXformSteps::UnpremulToUnpremul(src, dst); |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 47 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 48 | return steps.flags.mask() == 0 ? nullptr /* Noop transform */ |
| 49 | : sk_make_sp<GrColorSpaceXform>(steps); |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 50 | } |
brianosman | 5a7ae7e | 2016-09-12 12:07:25 -0700 | [diff] [blame] | 51 | |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 52 | bool GrColorSpaceXform::Equals(const GrColorSpaceXform* a, const GrColorSpaceXform* b) { |
| 53 | if (a == b) { |
| 54 | return true; |
| 55 | } |
| 56 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 57 | if (!a || !b || a->fSteps.flags.mask() != b->fSteps.flags.mask()) { |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 58 | return false; |
| 59 | } |
| 60 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 61 | if (a->fSteps.flags.linearize && |
| 62 | 0 != memcmp(&a->fSteps.srcTF, &b->fSteps.srcTF, sizeof(a->fSteps.srcTF))) { |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 63 | return false; |
| 64 | } |
| 65 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 66 | if (a->fSteps.flags.gamut_transform && |
| 67 | 0 != memcmp(&a->fSteps.src_to_dst_matrix, &b->fSteps.src_to_dst_matrix, |
| 68 | sizeof(a->fSteps.src_to_dst_matrix))) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | if (a->fSteps.flags.encode && |
| 73 | 0 != memcmp(&a->fSteps.dstTFInv, &b->fSteps.dstTFInv, sizeof(a->fSteps.dstTFInv))) { |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | |
| 77 | return true; |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 80 | GrColor4f GrColorSpaceXform::apply(const GrColor4f& srcColor) { |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 81 | GrColor4f result = srcColor; |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 82 | fSteps.apply(result.fRGBA); |
brianosman | 5a7ae7e | 2016-09-12 12:07:25 -0700 | [diff] [blame] | 83 | return result; |
| 84 | } |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 85 | |
| 86 | ////////////////////////////////////////////////////////////////////////////// |
| 87 | |
| 88 | class GrGLColorSpaceXformEffect : public GrGLSLFragmentProcessor { |
| 89 | public: |
| 90 | void emitCode(EmitArgs& args) override { |
| 91 | const GrColorSpaceXformEffect& csxe = args.fFp.cast<GrColorSpaceXformEffect>(); |
| 92 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 93 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 94 | |
| 95 | fColorSpaceHelper.emitCode(uniformHandler, csxe.colorXform()); |
| 96 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 97 | if (this->numChildProcessors()) { |
| 98 | SkString childColor("src_color"); |
| 99 | this->emitChild(0, &childColor, args); |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 100 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 101 | SkString xformedColor; |
| 102 | fragBuilder->appendColorGamutXform(&xformedColor, childColor.c_str(), &fColorSpaceHelper); |
| 103 | fragBuilder->codeAppendf("%s = %s * %s;", args.fOutputColor, xformedColor.c_str(), |
| 104 | args.fInputColor); |
| 105 | } else { |
| 106 | if (nullptr == args.fInputColor) { |
| 107 | args.fInputColor = "half4(1)"; |
| 108 | } |
| 109 | SkString xformedColor; |
| 110 | fragBuilder->appendColorGamutXform(&xformedColor, args.fInputColor, &fColorSpaceHelper); |
| 111 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, xformedColor.c_str()); |
| 112 | } |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | private: |
| 116 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 117 | const GrFragmentProcessor& processor) override { |
| 118 | const GrColorSpaceXformEffect& csxe = processor.cast<GrColorSpaceXformEffect>(); |
Brian Osman | c891b10 | 2018-06-14 14:50:17 -0400 | [diff] [blame] | 119 | fColorSpaceHelper.setData(pdman, csxe.colorXform()); |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | GrGLSLColorSpaceXformHelper fColorSpaceHelper; |
| 123 | |
| 124 | typedef GrGLSLFragmentProcessor INHERITED; |
| 125 | }; |
| 126 | |
| 127 | ////////////////////////////////////////////////////////////////////////////// |
| 128 | |
| 129 | GrColorSpaceXformEffect::GrColorSpaceXformEffect(std::unique_ptr<GrFragmentProcessor> child, |
| 130 | sk_sp<GrColorSpaceXform> colorXform) |
| 131 | : INHERITED(kGrColorSpaceXformEffect_ClassID, OptFlags(child.get())) |
| 132 | , fColorXform(std::move(colorXform)) { |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 133 | if (child) { |
| 134 | this->registerChildProcessor(std::move(child)); |
| 135 | } |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | std::unique_ptr<GrFragmentProcessor> GrColorSpaceXformEffect::clone() const { |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 139 | std::unique_ptr<GrFragmentProcessor> child = |
| 140 | this->numChildProcessors() ? this->childProcessor(0).clone() : nullptr; |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 141 | return std::unique_ptr<GrFragmentProcessor>( |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 142 | new GrColorSpaceXformEffect(std::move(child), fColorXform)); |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | bool GrColorSpaceXformEffect::onIsEqual(const GrFragmentProcessor& s) const { |
| 146 | const GrColorSpaceXformEffect& other = s.cast<GrColorSpaceXformEffect>(); |
| 147 | return GrColorSpaceXform::Equals(fColorXform.get(), other.fColorXform.get()); |
| 148 | } |
| 149 | |
| 150 | void GrColorSpaceXformEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 151 | GrProcessorKeyBuilder* b) const { |
| 152 | b->add32(GrColorSpaceXform::XformKey(fColorXform.get())); |
| 153 | } |
| 154 | |
| 155 | GrGLSLFragmentProcessor* GrColorSpaceXformEffect::onCreateGLSLInstance() const { |
| 156 | return new GrGLColorSpaceXformEffect(); |
| 157 | } |
| 158 | |
| 159 | GrFragmentProcessor::OptimizationFlags GrColorSpaceXformEffect::OptFlags( |
| 160 | const GrFragmentProcessor* child) { |
| 161 | // TODO: Implement constant output for constant input |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 162 | if (child) { |
| 163 | OptimizationFlags flags = kNone_OptimizationFlags; |
| 164 | if (child->compatibleWithCoverageAsAlpha()) { |
| 165 | flags |= kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
| 166 | } |
| 167 | if (child->preservesOpaqueInput()) { |
| 168 | flags |= kPreservesOpaqueInput_OptimizationFlag; |
| 169 | } |
| 170 | return flags; |
| 171 | } else { |
| 172 | return kCompatibleWithCoverageAsAlpha_OptimizationFlag | |
| 173 | kPreservesOpaqueInput_OptimizationFlag; |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 174 | } |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | std::unique_ptr<GrFragmentProcessor> GrColorSpaceXformEffect::Make(SkColorSpace* src, |
| 178 | SkColorSpace* dst) { |
| 179 | auto xform = GrColorSpaceXform::Make(src, dst); |
| 180 | if (!xform) { |
| 181 | return nullptr; |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 182 | } |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 183 | |
| 184 | return std::unique_ptr<GrFragmentProcessor>(new GrColorSpaceXformEffect(nullptr, |
| 185 | std::move(xform))); |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | std::unique_ptr<GrFragmentProcessor> GrColorSpaceXformEffect::Make( |
| 189 | std::unique_ptr<GrFragmentProcessor> child, |
Brian Osman | 653f34d | 2018-06-14 11:44:02 -0400 | [diff] [blame] | 190 | SkColorSpace* src, SkColorSpace* dst) { |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 191 | if (!child) { |
| 192 | return nullptr; |
| 193 | } |
| 194 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 195 | auto xform = GrColorSpaceXform::Make(src, dst); |
| 196 | if (!xform) { |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 197 | return child; |
| 198 | } |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 199 | |
| 200 | return std::unique_ptr<GrFragmentProcessor>(new GrColorSpaceXformEffect(std::move(child), |
| 201 | std::move(xform))); |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 202 | } |