Line data Source code
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 "GrSRGBEffect.h"
9 :
10 : #include "GrFragmentProcessor.h"
11 : #include "GrProcessor.h"
12 : #include "glsl/GrGLSLFragmentProcessor.h"
13 : #include "glsl/GrGLSLFragmentShaderBuilder.h"
14 :
15 0 : class GrGLSRGBEffect : public GrGLSLFragmentProcessor {
16 : public:
17 0 : void emitCode(EmitArgs& args) override {
18 0 : const GrSRGBEffect& srgbe = args.fFp.cast<GrSRGBEffect>();
19 0 : GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
20 :
21 0 : SkString srgbFuncName;
22 : static const GrShaderVar gSrgbArgs[] = {
23 : GrShaderVar("x", kFloat_GrSLType),
24 0 : };
25 0 : switch (srgbe.mode()) {
26 : case GrSRGBEffect::Mode::kLinearToSRGB:
27 0 : fragBuilder->emitFunction(kFloat_GrSLType,
28 : "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 0 : &srgbFuncName);
34 0 : break;
35 : case GrSRGBEffect::Mode::kSRGBToLinear:
36 0 : fragBuilder->emitFunction(kFloat_GrSLType,
37 : "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 0 : &srgbFuncName);
43 0 : break;
44 : }
45 :
46 0 : if (nullptr == args.fInputColor) {
47 0 : args.fInputColor = "vec4(1)";
48 : }
49 :
50 0 : fragBuilder->codeAppendf("%s = vec4(%s(%s.r), %s(%s.g), %s(%s.b), %s.a);",
51 : args.fOutputColor,
52 : srgbFuncName.c_str(), args.fInputColor,
53 : srgbFuncName.c_str(), args.fInputColor,
54 : srgbFuncName.c_str(), args.fInputColor,
55 0 : args.fInputColor);
56 0 : }
57 :
58 0 : static inline void GenKey(const GrProcessor& processor, const GrShaderCaps&,
59 : GrProcessorKeyBuilder* b) {
60 0 : const GrSRGBEffect& srgbe = processor.cast<GrSRGBEffect>();
61 0 : uint32_t key = static_cast<uint32_t>(srgbe.mode());
62 0 : b->add32(key);
63 0 : }
64 :
65 : private:
66 : typedef GrGLSLFragmentProcessor INHERITED;
67 : };
68 :
69 : ///////////////////////////////////////////////////////////////////////////////
70 :
71 0 : GrSRGBEffect::GrSRGBEffect(Mode mode)
72 : : INHERITED(kPreservesOpaqueInput_OptimizationFlag |
73 : kConstantOutputForConstantInput_OptimizationFlag)
74 0 : , fMode(mode) {
75 0 : this->initClassID<GrSRGBEffect>();
76 0 : }
77 :
78 0 : bool GrSRGBEffect::onIsEqual(const GrFragmentProcessor& s) const {
79 0 : const GrSRGBEffect& other = s.cast<GrSRGBEffect>();
80 0 : return other.fMode == fMode;
81 : }
82 :
83 0 : static inline float srgb_to_linear(float srgb) {
84 0 : return (srgb <= 0.04045f) ? srgb / 12.92f : powf((srgb + 0.055f) / 1.055f, 2.4f);
85 : }
86 0 : static inline float linear_to_srgb(float linear) {
87 0 : return (linear <= 0.0031308) ? linear * 12.92f : 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
88 : }
89 :
90 0 : GrColor4f GrSRGBEffect::constantOutputForConstantInput(GrColor4f input) const {
91 0 : switch (fMode) {
92 : case Mode::kLinearToSRGB:
93 0 : return GrColor4f(linear_to_srgb(input.fRGBA[0]), linear_to_srgb(input.fRGBA[1]),
94 0 : linear_to_srgb(input.fRGBA[2]), input.fRGBA[3]);
95 : case Mode::kSRGBToLinear:
96 0 : return GrColor4f(srgb_to_linear(input.fRGBA[0]), srgb_to_linear(input.fRGBA[1]),
97 0 : srgb_to_linear(input.fRGBA[2]), input.fRGBA[3]);
98 : }
99 0 : SkFAIL("Unexpected mode");
100 0 : return GrColor4f::TransparentBlack();
101 : }
102 :
103 : ///////////////////////////////////////////////////////////////////////////////
104 :
105 : GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSRGBEffect);
106 :
107 : #if GR_TEST_UTILS
108 0 : sk_sp<GrFragmentProcessor> GrSRGBEffect::TestCreate(GrProcessorTestData* d) {
109 0 : Mode testMode = static_cast<Mode>(d->fRandom->nextRangeU(0, 1));
110 0 : return sk_sp<GrFragmentProcessor>(new GrSRGBEffect(testMode));
111 : }
112 : #endif
113 :
114 : ///////////////////////////////////////////////////////////////////////////////
115 :
116 0 : void GrSRGBEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
117 : GrProcessorKeyBuilder* b) const {
118 0 : GrGLSRGBEffect::GenKey(*this, caps, b);
119 0 : }
120 :
121 0 : GrGLSLFragmentProcessor* GrSRGBEffect::onCreateGLSLInstance() const {
122 0 : return new GrGLSRGBEffect();
123 : }
124 :
125 0 : sk_sp<GrFragmentProcessor> GrSRGBEffect::Make(Mode mode) {
126 0 : return sk_sp<GrFragmentProcessor>(new GrSRGBEffect(mode));
127 : }
|