• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/gpu/GrTexture.h"
23 #include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
24 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
25 #include "src/gpu/glsl/GrGLSLProgramBuilder.h"
26 #include "src/sksl/SkSLCPP.h"
27 #include "src/sksl/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         innerThresholdVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
41                                                              "innerThreshold");
42         outerThresholdVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
43                                                              "outerThreshold");
44         SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
45         fragBuilder->codeAppendf(
46                 "half4 color = %s;\nhalf4 mask_color = sample(%s, %s).%s;\nif (mask_color.w < 0.5) "
47                 "{\n    if (color.w > %s) {\n        half scale = %s / color.w;\n        color.xyz "
48                 "*= scale;\n        color.w = %s;\n    }\n} else if (color.w < %s) {\n    half "
49                 "scale = %s / max(0.0010000000474974513, color.w);\n    color.xyz *= scale;\n    "
50                 "color.w = %s;\n}\n%s = color;\n",
51                 args.fInputColor,
52                 fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]),
53                 sk_TransformedCoords2D_0.c_str(),
54                 fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(),
55                 args.fUniformHandler->getUniformCStr(outerThresholdVar),
56                 args.fUniformHandler->getUniformCStr(outerThresholdVar),
57                 args.fUniformHandler->getUniformCStr(outerThresholdVar),
58                 args.fUniformHandler->getUniformCStr(innerThresholdVar),
59                 args.fUniformHandler->getUniformCStr(innerThresholdVar),
60                 args.fUniformHandler->getUniformCStr(innerThresholdVar), 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(innerThresholdVar, (_outer.innerThreshold));
70             pdman.set1f(outerThresholdVar, (_outer.outerThreshold));
71         }
72     }
73     UniformHandle innerThresholdVar;
74     UniformHandle outerThresholdVar;
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 (mask != that.mask) return false;
85     if (innerThreshold != that.innerThreshold) return false;
86     if (outerThreshold != that.outerThreshold) return false;
87     return true;
88 }
GrAlphaThresholdFragmentProcessor(const GrAlphaThresholdFragmentProcessor & src)89 GrAlphaThresholdFragmentProcessor::GrAlphaThresholdFragmentProcessor(
90         const GrAlphaThresholdFragmentProcessor& src)
91         : INHERITED(kGrAlphaThresholdFragmentProcessor_ClassID, src.optimizationFlags())
92         , maskCoordTransform(src.maskCoordTransform)
93         , mask(src.mask)
94         , innerThreshold(src.innerThreshold)
95         , outerThreshold(src.outerThreshold) {
96     this->setTextureSamplerCnt(1);
97     this->addCoordTransform(&maskCoordTransform);
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, mask);
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