blob: f4dfd3c6545cf829e951287d5fb12180766ad967 [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
8#include "GrSRGBEffect.h"
9
Brian Osman964dec32017-01-26 09:32:33 -050010#include "GrFragmentProcessor.h"
Brian Osman964dec32017-01-26 09:32:33 -050011#include "GrProcessor.h"
12#include "glsl/GrGLSLFragmentProcessor.h"
13#include "glsl/GrGLSLFragmentShaderBuilder.h"
14
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;
22 static 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.
48 fragBuilder->codeAppendf("float4 color = %s;", args.fInputColor);
Mike Reed98308fb2017-07-07 08:28:13 -040049 if (srgbe.alpha() == GrSRGBEffect::Alpha::kPremul) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040050 fragBuilder->codeAppendf("half nonZeroAlpha = max(color.a, 0.00001);");
51 fragBuilder->codeAppendf("color = half4(color.rgb / nonZeroAlpha, color.a);");
Mike Reed98308fb2017-07-07 08:28:13 -040052 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -040053 fragBuilder->codeAppendf("color = half4(%s(color.r), %s(color.g), %s(color.b), color.a);",
Mike Reed98308fb2017-07-07 08:28:13 -040054 srgbFuncName.c_str(),
55 srgbFuncName.c_str(),
56 srgbFuncName.c_str());
57 if (srgbe.alpha() == GrSRGBEffect::Alpha::kPremul) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040058 fragBuilder->codeAppendf("color = half4(color.rgb, 1) * color.a;");
Mike Reed98308fb2017-07-07 08:28:13 -040059 }
60 fragBuilder->codeAppendf("%s = color;", args.fOutputColor);
Brian Osman964dec32017-01-26 09:32:33 -050061 }
62
63 static inline void GenKey(const GrProcessor& processor, const GrShaderCaps&,
64 GrProcessorKeyBuilder* b) {
65 const GrSRGBEffect& srgbe = processor.cast<GrSRGBEffect>();
Mike Reed98308fb2017-07-07 08:28:13 -040066 uint32_t key = static_cast<uint32_t>(srgbe.mode()) |
67 (static_cast<uint32_t>(srgbe.alpha()) << 1);
Brian Osman964dec32017-01-26 09:32:33 -050068 b->add32(key);
69 }
70
71private:
72 typedef GrGLSLFragmentProcessor INHERITED;
73};
74
75///////////////////////////////////////////////////////////////////////////////
76
Mike Reed98308fb2017-07-07 08:28:13 -040077GrSRGBEffect::GrSRGBEffect(Mode mode, Alpha alpha)
Ethan Nicholasabff9562017-10-09 10:54:08 -040078 : INHERITED(kGrSRGBEffect_ClassID, kPreservesOpaqueInput_OptimizationFlag |
Mike Reed98308fb2017-07-07 08:28:13 -040079 kConstantOutputForConstantInput_OptimizationFlag)
80 , fMode(mode)
81 , fAlpha(alpha)
82{
Brian Osman964dec32017-01-26 09:32:33 -050083}
84
Brian Salomonaff329b2017-08-11 09:40:37 -040085std::unique_ptr<GrFragmentProcessor> GrSRGBEffect::clone() const { return Make(fMode, fAlpha); }
Brian Salomon1a2a7ab2017-07-26 13:11:51 -040086
Brian Osman964dec32017-01-26 09:32:33 -050087bool GrSRGBEffect::onIsEqual(const GrFragmentProcessor& s) const {
88 const GrSRGBEffect& other = s.cast<GrSRGBEffect>();
89 return other.fMode == fMode;
90}
91
Brian Salomon587e08f2017-01-27 10:59:27 -050092static inline float srgb_to_linear(float srgb) {
93 return (srgb <= 0.04045f) ? srgb / 12.92f : powf((srgb + 0.055f) / 1.055f, 2.4f);
94}
95static inline float linear_to_srgb(float linear) {
96 return (linear <= 0.0031308) ? linear * 12.92f : 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
97}
98
Brian Osman1d5b5982018-10-01 13:41:39 -040099SkPMColor4f GrSRGBEffect::constantOutputForConstantInput(const SkPMColor4f& inColor) const {
100 SkColor4f color = inColor.unpremul();
Brian Salomon587e08f2017-01-27 10:59:27 -0500101 switch (fMode) {
102 case Mode::kLinearToSRGB:
Brian Osman1d5b5982018-10-01 13:41:39 -0400103 color = { linear_to_srgb(color.fR), linear_to_srgb(color.fG), linear_to_srgb(color.fB),
104 color.fA };
Mike Reed98308fb2017-07-07 08:28:13 -0400105 break;
Brian Salomon587e08f2017-01-27 10:59:27 -0500106 case Mode::kSRGBToLinear:
Brian Osman1d5b5982018-10-01 13:41:39 -0400107 color = { srgb_to_linear(color.fR), srgb_to_linear(color.fG), srgb_to_linear(color.fB),
108 color.fA };
Mike Reed98308fb2017-07-07 08:28:13 -0400109 break;
Brian Salomon587e08f2017-01-27 10:59:27 -0500110 }
Mike Reed98308fb2017-07-07 08:28:13 -0400111 return color.premul();
Brian Salomon587e08f2017-01-27 10:59:27 -0500112}
113
Brian Osman964dec32017-01-26 09:32:33 -0500114///////////////////////////////////////////////////////////////////////////////
115
116GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSRGBEffect);
117
Hal Canary6f6961e2017-01-31 13:50:44 -0500118#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400119std::unique_ptr<GrFragmentProcessor> GrSRGBEffect::TestCreate(GrProcessorTestData* d) {
Brian Osman964dec32017-01-26 09:32:33 -0500120 Mode testMode = static_cast<Mode>(d->fRandom->nextRangeU(0, 1));
Mike Reed98308fb2017-07-07 08:28:13 -0400121 return GrSRGBEffect::Make(testMode, Alpha::kPremul);
Brian Osman964dec32017-01-26 09:32:33 -0500122}
Hal Canary6f6961e2017-01-31 13:50:44 -0500123#endif
Brian Osman964dec32017-01-26 09:32:33 -0500124
125///////////////////////////////////////////////////////////////////////////////
126
127void GrSRGBEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
128 GrProcessorKeyBuilder* b) const {
129 GrGLSRGBEffect::GenKey(*this, caps, b);
130}
131
132GrGLSLFragmentProcessor* GrSRGBEffect::onCreateGLSLInstance() const {
Mike Reed98308fb2017-07-07 08:28:13 -0400133 return new GrGLSRGBEffect;
Brian Osman964dec32017-01-26 09:32:33 -0500134}
135