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