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 GrSweepGradientLayout.fp; do not modify.
10  **************************************************************************************************/
11 #include "GrSweepGradientLayout.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 GrGLSLSweepGradientLayout : public GrGLSLFragmentProcessor {
19 public:
GrGLSLSweepGradientLayout()20     GrGLSLSweepGradientLayout() {}
emitCode(EmitArgs & args)21     void emitCode(EmitArgs& args) override {
22         GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
23         const GrSweepGradientLayout& _outer = args.fFp.cast<GrSweepGradientLayout>();
24         (void)_outer;
25         auto gradientMatrix = _outer.gradientMatrix();
26         (void)gradientMatrix;
27         auto bias = _outer.bias();
28         (void)bias;
29         auto scale = _outer.scale();
30         (void)scale;
31         fBiasVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, "bias");
32         fScaleVar =
33                 args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, "scale");
34         SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
35         fragBuilder->codeAppendf(
36                 "half angle;\nif (sk_Caps.atan2ImplementedAsAtanYOverX) {\n    angle = half(2.0 * "
37                 "atan(-%s.y, length(%s) - %s.x));\n} else {\n    angle = half(atan(-%s.y, "
38                 "-%s.x));\n}\nhalf t = ((angle * 0.15915494309180001 + 0.5) + %s) * %s;\n%s = "
39                 "half4(t, 1.0, 0.0, 0.0);\n",
40                 sk_TransformedCoords2D_0.c_str(), sk_TransformedCoords2D_0.c_str(),
41                 sk_TransformedCoords2D_0.c_str(), sk_TransformedCoords2D_0.c_str(),
42                 sk_TransformedCoords2D_0.c_str(), args.fUniformHandler->getUniformCStr(fBiasVar),
43                 args.fUniformHandler->getUniformCStr(fScaleVar), args.fOutputColor);
44     }
45 
46 private:
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & _proc)47     void onSetData(const GrGLSLProgramDataManager& pdman,
48                    const GrFragmentProcessor& _proc) override {
49         const GrSweepGradientLayout& _outer = _proc.cast<GrSweepGradientLayout>();
50         {
51             float biasValue = _outer.bias();
52             if (fBiasPrev != biasValue) {
53                 fBiasPrev = biasValue;
54                 pdman.set1f(fBiasVar, biasValue);
55             }
56             float scaleValue = _outer.scale();
57             if (fScalePrev != scaleValue) {
58                 fScalePrev = scaleValue;
59                 pdman.set1f(fScaleVar, scaleValue);
60             }
61         }
62     }
63     float fBiasPrev = SK_FloatNaN;
64     float fScalePrev = SK_FloatNaN;
65     UniformHandle fBiasVar;
66     UniformHandle fScaleVar;
67 };
onCreateGLSLInstance() const68 GrGLSLFragmentProcessor* GrSweepGradientLayout::onCreateGLSLInstance() const {
69     return new GrGLSLSweepGradientLayout();
70 }
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const71 void GrSweepGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps,
72                                                   GrProcessorKeyBuilder* b) const {}
onIsEqual(const GrFragmentProcessor & other) const73 bool GrSweepGradientLayout::onIsEqual(const GrFragmentProcessor& other) const {
74     const GrSweepGradientLayout& that = other.cast<GrSweepGradientLayout>();
75     (void)that;
76     if (fGradientMatrix != that.fGradientMatrix) return false;
77     if (fBias != that.fBias) return false;
78     if (fScale != that.fScale) return false;
79     return true;
80 }
GrSweepGradientLayout(const GrSweepGradientLayout & src)81 GrSweepGradientLayout::GrSweepGradientLayout(const GrSweepGradientLayout& src)
82         : INHERITED(kGrSweepGradientLayout_ClassID, src.optimizationFlags())
83         , fGradientMatrix(src.fGradientMatrix)
84         , fBias(src.fBias)
85         , fScale(src.fScale)
86         , fCoordTransform0(src.fCoordTransform0) {
87     this->addCoordTransform(&fCoordTransform0);
88 }
clone() const89 std::unique_ptr<GrFragmentProcessor> GrSweepGradientLayout::clone() const {
90     return std::unique_ptr<GrFragmentProcessor>(new GrSweepGradientLayout(*this));
91 }
92 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSweepGradientLayout);
93 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)94 std::unique_ptr<GrFragmentProcessor> GrSweepGradientLayout::TestCreate(GrProcessorTestData* d) {
95     SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
96     SkPoint center = {d->fRandom->nextRangeScalar(0.0f, scale),
97                       d->fRandom->nextRangeScalar(0.0f, scale)};
98 
99     GrGradientShader::RandomParams params(d->fRandom);
100     auto shader = params.fUseColors4f
101                           ? SkGradientShader::MakeSweep(center.fX, center.fY, params.fColors4f,
102                                                         params.fColorSpace, params.fStops,
103                                                         params.fColorCount)
104                           : SkGradientShader::MakeSweep(center.fX, center.fY, params.fColors,
105                                                         params.fStops, params.fColorCount);
106     GrTest::TestAsFPArgs asFPArgs(d);
107     std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
108     GrAlwaysAssert(fp);
109     return fp;
110 }
111 #endif
112 
Make(const SkSweepGradient & grad,const GrFPArgs & args)113 std::unique_ptr<GrFragmentProcessor> GrSweepGradientLayout::Make(const SkSweepGradient& grad,
114                                                                  const GrFPArgs& args) {
115     SkMatrix matrix;
116     if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
117         return nullptr;
118     }
119     matrix.postConcat(grad.getGradientMatrix());
120     return std::unique_ptr<GrFragmentProcessor>(
121             new GrSweepGradientLayout(matrix, grad.getTBias(), grad.getTScale()));
122 }
123