1 /*
2 * Copyright 2018 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 /**************************************************************************************************
9 *** This file was autogenerated from GrDualIntervalGradientColorizer.fp; do not modify.
10 **************************************************************************************************/
11 #include "GrDualIntervalGradientColorizer.h"
12 #include "glsl/GrGLSLFragmentProcessor.h"
13 #include "glsl/GrGLSLFragmentShaderBuilder.h"
14 #include "glsl/GrGLSLProgramBuilder.h"
15 #include "GrTexture.h"
16 #include "SkSLCPP.h"
17 #include "SkSLUtil.h"
18 class GrGLSLDualIntervalGradientColorizer : public GrGLSLFragmentProcessor {
19 public:
GrGLSLDualIntervalGradientColorizer()20 GrGLSLDualIntervalGradientColorizer() {}
emitCode(EmitArgs & args)21 void emitCode(EmitArgs& args) override {
22 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
23 const GrDualIntervalGradientColorizer& _outer =
24 args.fFp.cast<GrDualIntervalGradientColorizer>();
25 (void)_outer;
26 auto scale01 = _outer.scale01();
27 (void)scale01;
28 auto bias01 = _outer.bias01();
29 (void)bias01;
30 auto scale23 = _outer.scale23();
31 (void)scale23;
32 auto bias23 = _outer.bias23();
33 (void)bias23;
34 auto threshold = _outer.threshold();
35 (void)threshold;
36 fScale01Var = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
37 "scale01");
38 fBias01Var = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
39 "bias01");
40 fScale23Var = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
41 "scale23");
42 fBias23Var = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType,
43 "bias23");
44 fThresholdVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
45 "threshold");
46 fragBuilder->codeAppendf(
47 "half t = %s.x;\nfloat4 scale, bias;\nif (t < %s) {\n scale = %s;\n bias = "
48 "%s;\n} else {\n scale = %s;\n bias = %s;\n}\n%s = half4(float(t) * scale + "
49 "bias);\n",
50 args.fInputColor, args.fUniformHandler->getUniformCStr(fThresholdVar),
51 args.fUniformHandler->getUniformCStr(fScale01Var),
52 args.fUniformHandler->getUniformCStr(fBias01Var),
53 args.fUniformHandler->getUniformCStr(fScale23Var),
54 args.fUniformHandler->getUniformCStr(fBias23Var), args.fOutputColor);
55 }
56
57 private:
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & _proc)58 void onSetData(const GrGLSLProgramDataManager& pdman,
59 const GrFragmentProcessor& _proc) override {
60 const GrDualIntervalGradientColorizer& _outer =
61 _proc.cast<GrDualIntervalGradientColorizer>();
62 {
63 const SkPMColor4f& scale01Value = _outer.scale01();
64 if (fScale01Prev != scale01Value) {
65 fScale01Prev = scale01Value;
66 pdman.set4fv(fScale01Var, 1, scale01Value.vec());
67 }
68 const SkPMColor4f& bias01Value = _outer.bias01();
69 if (fBias01Prev != bias01Value) {
70 fBias01Prev = bias01Value;
71 pdman.set4fv(fBias01Var, 1, bias01Value.vec());
72 }
73 const SkPMColor4f& scale23Value = _outer.scale23();
74 if (fScale23Prev != scale23Value) {
75 fScale23Prev = scale23Value;
76 pdman.set4fv(fScale23Var, 1, scale23Value.vec());
77 }
78 const SkPMColor4f& bias23Value = _outer.bias23();
79 if (fBias23Prev != bias23Value) {
80 fBias23Prev = bias23Value;
81 pdman.set4fv(fBias23Var, 1, bias23Value.vec());
82 }
83 float thresholdValue = _outer.threshold();
84 if (fThresholdPrev != thresholdValue) {
85 fThresholdPrev = thresholdValue;
86 pdman.set1f(fThresholdVar, thresholdValue);
87 }
88 }
89 }
90 SkPMColor4f fScale01Prev = {SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN};
91 SkPMColor4f fBias01Prev = {SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN};
92 SkPMColor4f fScale23Prev = {SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN};
93 SkPMColor4f fBias23Prev = {SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN};
94 float fThresholdPrev = SK_FloatNaN;
95 UniformHandle fScale01Var;
96 UniformHandle fBias01Var;
97 UniformHandle fScale23Var;
98 UniformHandle fBias23Var;
99 UniformHandle fThresholdVar;
100 };
onCreateGLSLInstance() const101 GrGLSLFragmentProcessor* GrDualIntervalGradientColorizer::onCreateGLSLInstance() const {
102 return new GrGLSLDualIntervalGradientColorizer();
103 }
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const104 void GrDualIntervalGradientColorizer::onGetGLSLProcessorKey(const GrShaderCaps& caps,
105 GrProcessorKeyBuilder* b) const {}
onIsEqual(const GrFragmentProcessor & other) const106 bool GrDualIntervalGradientColorizer::onIsEqual(const GrFragmentProcessor& other) const {
107 const GrDualIntervalGradientColorizer& that = other.cast<GrDualIntervalGradientColorizer>();
108 (void)that;
109 if (fScale01 != that.fScale01) return false;
110 if (fBias01 != that.fBias01) return false;
111 if (fScale23 != that.fScale23) return false;
112 if (fBias23 != that.fBias23) return false;
113 if (fThreshold != that.fThreshold) return false;
114 return true;
115 }
GrDualIntervalGradientColorizer(const GrDualIntervalGradientColorizer & src)116 GrDualIntervalGradientColorizer::GrDualIntervalGradientColorizer(
117 const GrDualIntervalGradientColorizer& src)
118 : INHERITED(kGrDualIntervalGradientColorizer_ClassID, src.optimizationFlags())
119 , fScale01(src.fScale01)
120 , fBias01(src.fBias01)
121 , fScale23(src.fScale23)
122 , fBias23(src.fBias23)
123 , fThreshold(src.fThreshold) {}
clone() const124 std::unique_ptr<GrFragmentProcessor> GrDualIntervalGradientColorizer::clone() const {
125 return std::unique_ptr<GrFragmentProcessor>(new GrDualIntervalGradientColorizer(*this));
126 }
127
Make(const SkPMColor4f & c0,const SkPMColor4f & c1,const SkPMColor4f & c2,const SkPMColor4f & c3,float threshold)128 std::unique_ptr<GrFragmentProcessor> GrDualIntervalGradientColorizer::Make(const SkPMColor4f& c0,
129 const SkPMColor4f& c1,
130 const SkPMColor4f& c2,
131 const SkPMColor4f& c3,
132 float threshold) {
133 // Derive scale and biases from the 4 colors and threshold
134 auto vc0 = Sk4f::Load(c0.vec());
135 auto vc1 = Sk4f::Load(c1.vec());
136 auto scale01 = (vc1 - vc0) / threshold;
137 // bias01 = c0
138
139 auto vc2 = Sk4f::Load(c2.vec());
140 auto vc3 = Sk4f::Load(c3.vec());
141 auto scale23 = (vc3 - vc2) / (1 - threshold);
142 auto bias23 = vc2 - threshold * scale23;
143
144 return std::unique_ptr<GrFragmentProcessor>(new GrDualIntervalGradientColorizer(
145 {scale01[0], scale01[1], scale01[2], scale01[3]}, c0,
146 {scale23[0], scale23[1], scale23[2], scale23[3]},
147 {bias23[0], bias23[1], bias23[2], bias23[3]}, threshold));
148 }
149