blob: b7e27a6b1f3fb00b086b131fe0373e7d93c806cb [file] [log] [blame]
Brian Osman964dec32017-01-26 09:32:33 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/effects/GrSRGBEffect.h"
Brian Osman964dec32017-01-26 09:32:33 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrFragmentProcessor.h"
11#include "src/gpu/GrProcessor.h"
12#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
13#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Brian Osman964dec32017-01-26 09:32:33 -050014
15class GrGLSRGBEffect : public GrGLSLFragmentProcessor {
16public:
17 void emitCode(EmitArgs& args) override {
18 const GrSRGBEffect& srgbe = args.fFp.cast<GrSRGBEffect>();
19 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
20
21 SkString srgbFuncName;
Nico Webere50efdf2018-10-01 14:40:44 -040022 const GrShaderVar gSrgbArgs[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040023 GrShaderVar("x", kHalf_GrSLType),
Brian Osman964dec32017-01-26 09:32:33 -050024 };
25 switch (srgbe.mode()) {
26 case GrSRGBEffect::Mode::kLinearToSRGB:
Ethan Nicholasf7b88202017-09-18 14:10:39 -040027 fragBuilder->emitFunction(kHalf_GrSLType,
Brian Osman964dec32017-01-26 09:32:33 -050028 "linear_to_srgb",
29 SK_ARRAY_COUNT(gSrgbArgs),
30 gSrgbArgs,
31 "return (x <= 0.0031308) ? (x * 12.92) "
32 ": (1.055 * pow(x, 0.416666667) - 0.055);",
33 &srgbFuncName);
34 break;
35 case GrSRGBEffect::Mode::kSRGBToLinear:
Ethan Nicholasf7b88202017-09-18 14:10:39 -040036 fragBuilder->emitFunction(kHalf_GrSLType,
Brian Osman964dec32017-01-26 09:32:33 -050037 "srgb_to_linear",
38 SK_ARRAY_COUNT(gSrgbArgs),
39 gSrgbArgs,
40 "return (x <= 0.04045) ? (x / 12.92) "
41 ": pow((x + 0.055) / 1.055, 2.4);",
42 &srgbFuncName);
43 break;
44 }
45
Brian Salomon08c39fc2018-04-03 09:58:37 -040046 // Mali Bifrost uses fp16 for mediump. Making the intermediate color variable highp causes
47 // calculations to be performed with sufficient precision.
Ethan Nicholas71935862019-02-06 09:25:40 -050048 fragBuilder->codeAppendf("float4 color = %s;", args.fInputColor);
Mike Reed98308fb2017-07-07 08:28:13 -040049 if (srgbe.alpha() == GrSRGBEffect::Alpha::kPremul) {
Brian Salomond1bc2402019-06-05 11:47:50 -040050 fragBuilder->codeAppendf("float nonZeroAlpha = max(color.a, 0.0001);");
Ethan Nicholas71935862019-02-06 09:25:40 -050051 fragBuilder->codeAppendf("color = float4(color.rgb / nonZeroAlpha, color.a);");
Mike Reed98308fb2017-07-07 08:28:13 -040052 }
Ethan Nicholas71935862019-02-06 09:25:40 -050053 fragBuilder->codeAppendf("color = float4(%s(half(color.r)), %s(half(color.g)), "
54 "%s(half(color.b)), color.a);",
55 srgbFuncName.c_str(),
56 srgbFuncName.c_str(),
57 srgbFuncName.c_str());
Mike Reed98308fb2017-07-07 08:28:13 -040058 if (srgbe.alpha() == GrSRGBEffect::Alpha::kPremul) {
Ethan Nicholas71935862019-02-06 09:25:40 -050059 fragBuilder->codeAppendf("color = float4(color.rgb, 1) * color.a;");
Mike Reed98308fb2017-07-07 08:28:13 -040060 }
Ethan Nicholas71935862019-02-06 09:25:40 -050061 fragBuilder->codeAppendf("%s = half4(color);", args.fOutputColor);
Brian Osman964dec32017-01-26 09:32:33 -050062 }
63
64 static inline void GenKey(const GrProcessor& processor, const GrShaderCaps&,
65 GrProcessorKeyBuilder* b) {
66 const GrSRGBEffect& srgbe = processor.cast<GrSRGBEffect>();
Mike Reed98308fb2017-07-07 08:28:13 -040067 uint32_t key = static_cast<uint32_t>(srgbe.mode()) |
68 (static_cast<uint32_t>(srgbe.alpha()) << 1);
Brian Osman964dec32017-01-26 09:32:33 -050069 b->add32(key);
70 }
71
72private:
73 typedef GrGLSLFragmentProcessor INHERITED;
74};
75
76///////////////////////////////////////////////////////////////////////////////
77
Mike Reed98308fb2017-07-07 08:28:13 -040078GrSRGBEffect::GrSRGBEffect(Mode mode, Alpha alpha)
Ethan Nicholasabff9562017-10-09 10:54:08 -040079 : INHERITED(kGrSRGBEffect_ClassID, kPreservesOpaqueInput_OptimizationFlag |
Mike Reed98308fb2017-07-07 08:28:13 -040080 kConstantOutputForConstantInput_OptimizationFlag)
81 , fMode(mode)
82 , fAlpha(alpha)
83{
Brian Osman964dec32017-01-26 09:32:33 -050084}
85
Brian Salomonaff329b2017-08-11 09:40:37 -040086std::unique_ptr<GrFragmentProcessor> GrSRGBEffect::clone() const { return Make(fMode, fAlpha); }
Brian Salomon1a2a7ab2017-07-26 13:11:51 -040087
Brian Osman964dec32017-01-26 09:32:33 -050088bool GrSRGBEffect::onIsEqual(const GrFragmentProcessor& s) const {
89 const GrSRGBEffect& other = s.cast<GrSRGBEffect>();
90 return other.fMode == fMode;
91}
92
Brian Salomon587e08f2017-01-27 10:59:27 -050093static inline float srgb_to_linear(float srgb) {
94 return (srgb <= 0.04045f) ? srgb / 12.92f : powf((srgb + 0.055f) / 1.055f, 2.4f);
95}
96static inline float linear_to_srgb(float linear) {
97 return (linear <= 0.0031308) ? linear * 12.92f : 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
98}
99
Brian Osman1d5b5982018-10-01 13:41:39 -0400100SkPMColor4f GrSRGBEffect::constantOutputForConstantInput(const SkPMColor4f& inColor) const {
101 SkColor4f color = inColor.unpremul();
Brian Salomon587e08f2017-01-27 10:59:27 -0500102 switch (fMode) {
103 case Mode::kLinearToSRGB:
Brian Osman1d5b5982018-10-01 13:41:39 -0400104 color = { linear_to_srgb(color.fR), linear_to_srgb(color.fG), linear_to_srgb(color.fB),
105 color.fA };
Mike Reed98308fb2017-07-07 08:28:13 -0400106 break;
Brian Salomon587e08f2017-01-27 10:59:27 -0500107 case Mode::kSRGBToLinear:
Brian Osman1d5b5982018-10-01 13:41:39 -0400108 color = { srgb_to_linear(color.fR), srgb_to_linear(color.fG), srgb_to_linear(color.fB),
109 color.fA };
Mike Reed98308fb2017-07-07 08:28:13 -0400110 break;
Brian Salomon587e08f2017-01-27 10:59:27 -0500111 }
Mike Reed98308fb2017-07-07 08:28:13 -0400112 return color.premul();
Brian Salomon587e08f2017-01-27 10:59:27 -0500113}
114
Brian Osman964dec32017-01-26 09:32:33 -0500115///////////////////////////////////////////////////////////////////////////////
116
117GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSRGBEffect);
118
Hal Canary6f6961e2017-01-31 13:50:44 -0500119#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400120std::unique_ptr<GrFragmentProcessor> GrSRGBEffect::TestCreate(GrProcessorTestData* d) {
Brian Osman964dec32017-01-26 09:32:33 -0500121 Mode testMode = static_cast<Mode>(d->fRandom->nextRangeU(0, 1));
Mike Reed98308fb2017-07-07 08:28:13 -0400122 return GrSRGBEffect::Make(testMode, Alpha::kPremul);
Brian Osman964dec32017-01-26 09:32:33 -0500123}
Hal Canary6f6961e2017-01-31 13:50:44 -0500124#endif
Brian Osman964dec32017-01-26 09:32:33 -0500125
126///////////////////////////////////////////////////////////////////////////////
127
128void GrSRGBEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
129 GrProcessorKeyBuilder* b) const {
130 GrGLSRGBEffect::GenKey(*this, caps, b);
131}
132
133GrGLSLFragmentProcessor* GrSRGBEffect::onCreateGLSLInstance() const {
Mike Reed98308fb2017-07-07 08:28:13 -0400134 return new GrGLSRGBEffect;
Brian Osman964dec32017-01-26 09:32:33 -0500135}
136