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 GrAlphaThresholdFragmentProcessor.fp; do not modify.
10  **************************************************************************************************/
11 #include "GrAlphaThresholdFragmentProcessor.h"
12 
optFlags(float outerThreshold)13 inline GrFragmentProcessor::OptimizationFlags GrAlphaThresholdFragmentProcessor::optFlags(
14         float outerThreshold) {
15     if (outerThreshold >= 1.0) {
16         return kPreservesOpaqueInput_OptimizationFlag |
17                kCompatibleWithCoverageAsAlpha_OptimizationFlag;
18     } else {
19         return kCompatibleWithCoverageAsAlpha_OptimizationFlag;
20     }
21 }
22 #include "glsl/GrGLSLFragmentProcessor.h"
23 #include "glsl/GrGLSLFragmentShaderBuilder.h"
24 #include "glsl/GrGLSLProgramBuilder.h"
25 #include "GrTexture.h"
26 #include "SkSLCPP.h"
27 #include "SkSLUtil.h"
28 class GrGLSLAlphaThresholdFragmentProcessor : public GrGLSLFragmentProcessor {
29 public:
GrGLSLAlphaThresholdFragmentProcessor()30     GrGLSLAlphaThresholdFragmentProcessor() {}
emitCode(EmitArgs & args)31     void emitCode(EmitArgs& args) override {
32         GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
33         const GrAlphaThresholdFragmentProcessor& _outer =
34                 args.fFp.cast<GrAlphaThresholdFragmentProcessor>();
35         (void)_outer;
36         auto innerThreshold = _outer.innerThreshold();
37         (void)innerThreshold;
38         auto outerThreshold = _outer.outerThreshold();
39         (void)outerThreshold;
40         fInnerThresholdVar = args.fUniformHandler->addUniform(
41                 kFragment_GrShaderFlag, kHalf_GrSLType, kDefault_GrSLPrecision, "innerThreshold");
42         fOuterThresholdVar = args.fUniformHandler->addUniform(
43                 kFragment_GrShaderFlag, kHalf_GrSLType, kDefault_GrSLPrecision, "outerThreshold");
44         SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
45         fragBuilder->codeAppendf(
46                 "half4 color = %s;\nhalf4 mask_color = texture(%s, %s).%s;\nif "
47                 "(float(mask_color.w) < 0.5) {\n    if (color.w > %s) {\n        half scale = %s / "
48                 "color.w;\n        color.xyz *= scale;\n        color.w = %s;\n    }\n} else if "
49                 "(color.w < %s) {\n    half scale = float(%s) / max(0.001, float(color.w));\n    "
50                 "color.xyz *= scale;\n    color.w = %s;\n}\n%s = color;\n",
51                 args.fInputColor,
52                 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(),
53                 sk_TransformedCoords2D_0.c_str(),
54                 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(),
55                 args.fUniformHandler->getUniformCStr(fOuterThresholdVar),
56                 args.fUniformHandler->getUniformCStr(fOuterThresholdVar),
57                 args.fUniformHandler->getUniformCStr(fOuterThresholdVar),
58                 args.fUniformHandler->getUniformCStr(fInnerThresholdVar),
59                 args.fUniformHandler->getUniformCStr(fInnerThresholdVar),
60                 args.fUniformHandler->getUniformCStr(fInnerThresholdVar), args.fOutputColor);
61     }
62 
63 private:
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & _proc)64     void onSetData(const GrGLSLProgramDataManager& pdman,
65                    const GrFragmentProcessor& _proc) override {
66         const GrAlphaThresholdFragmentProcessor& _outer =
67                 _proc.cast<GrAlphaThresholdFragmentProcessor>();
68         {
69             pdman.set1f(fInnerThresholdVar, (_outer.innerThreshold()));
70             pdman.set1f(fOuterThresholdVar, (_outer.outerThreshold()));
71         }
72     }
73     UniformHandle fInnerThresholdVar;
74     UniformHandle fOuterThresholdVar;
75 };
onCreateGLSLInstance() const76 GrGLSLFragmentProcessor* GrAlphaThresholdFragmentProcessor::onCreateGLSLInstance() const {
77     return new GrGLSLAlphaThresholdFragmentProcessor();
78 }
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const79 void GrAlphaThresholdFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
80                                                               GrProcessorKeyBuilder* b) const {}
onIsEqual(const GrFragmentProcessor & other) const81 bool GrAlphaThresholdFragmentProcessor::onIsEqual(const GrFragmentProcessor& other) const {
82     const GrAlphaThresholdFragmentProcessor& that = other.cast<GrAlphaThresholdFragmentProcessor>();
83     (void)that;
84     if (fMask != that.fMask) return false;
85     if (fInnerThreshold != that.fInnerThreshold) return false;
86     if (fOuterThreshold != that.fOuterThreshold) return false;
87     return true;
88 }
GrAlphaThresholdFragmentProcessor(const GrAlphaThresholdFragmentProcessor & src)89 GrAlphaThresholdFragmentProcessor::GrAlphaThresholdFragmentProcessor(
90         const GrAlphaThresholdFragmentProcessor& src)
91         : INHERITED(kGrAlphaThresholdFragmentProcessor_ClassID, src.optimizationFlags())
92         , fMask(src.fMask)
93         , fInnerThreshold(src.fInnerThreshold)
94         , fOuterThreshold(src.fOuterThreshold)
95         , fMaskCoordTransform(src.fMaskCoordTransform) {
96     this->setTextureSamplerCnt(1);
97     this->addCoordTransform(&fMaskCoordTransform);
98 }
clone() const99 std::unique_ptr<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::clone() const {
100     return std::unique_ptr<GrFragmentProcessor>(new GrAlphaThresholdFragmentProcessor(*this));
101 }
onTextureSampler(int index) const102 const GrFragmentProcessor::TextureSampler& GrAlphaThresholdFragmentProcessor::onTextureSampler(
103         int index) const {
104     return IthTextureSampler(index, fMask);
105 }
106 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrAlphaThresholdFragmentProcessor);
107 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * testData)108 std::unique_ptr<GrFragmentProcessor> GrAlphaThresholdFragmentProcessor::TestCreate(
109         GrProcessorTestData* testData) {
110     sk_sp<GrTextureProxy> maskProxy = testData->textureProxy(GrProcessorUnitTest::kAlphaTextureIdx);
111     // Make the inner and outer thresholds be in (0, 1) exclusive and be sorted correctly.
112     float innerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f;
113     float outerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f;
114     const int kMaxWidth = 1000;
115     const int kMaxHeight = 1000;
116     uint32_t width = testData->fRandom->nextULessThan(kMaxWidth);
117     uint32_t height = testData->fRandom->nextULessThan(kMaxHeight);
118     uint32_t x = testData->fRandom->nextULessThan(kMaxWidth - width);
119     uint32_t y = testData->fRandom->nextULessThan(kMaxHeight - height);
120     SkIRect bounds = SkIRect::MakeXYWH(x, y, width, height);
121     return GrAlphaThresholdFragmentProcessor::Make(std::move(maskProxy), innerThresh, outerThresh,
122                                                    bounds);
123 }
124 #endif
125