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 GrAARectEffect.fp; do not modify.
10 **************************************************************************************************/
11 #include "GrAARectEffect.h"
12 #if SK_SUPPORT_GPU
13 #include "glsl/GrGLSLFragmentProcessor.h"
14 #include "glsl/GrGLSLFragmentShaderBuilder.h"
15 #include "glsl/GrGLSLProgramBuilder.h"
16 #include "GrTexture.h"
17 #include "SkSLCPP.h"
18 #include "SkSLUtil.h"
19 class GrGLSLAARectEffect : public GrGLSLFragmentProcessor {
20 public:
GrGLSLAARectEffect()21 GrGLSLAARectEffect() {}
emitCode(EmitArgs & args)22 void emitCode(EmitArgs& args) override {
23 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
24 const GrAARectEffect& _outer = args.fFp.cast<GrAARectEffect>();
25 (void)_outer;
26 auto edgeType = _outer.edgeType();
27 (void)edgeType;
28 auto rect = _outer.rect();
29 (void)rect;
30 prevRect = float4(-1.0);
31 fRectUniformVar = args.fUniformHandler->addUniform(
32 kFragment_GrShaderFlag, kFloat4_GrSLType, kDefault_GrSLPrecision, "rectUniform");
33 fragBuilder->codeAppendf(
34 "float4 prevRect = float4(%f, %f, %f, %f);\nhalf alpha;\n@switch (%d) {\n case "
35 "0:\n case 2:\n alpha = half(all(greaterThan(float4(sk_FragCoord.xy, "
36 "%s.zw), float4(%s.xy, sk_FragCoord.xy))) ? 1 : 0);\n break;\n "
37 "default:\n half xSub, ySub;\n xSub = half(min(sk_FragCoord.x - "
38 "%s.x, 0.0));\n xSub += half(min(%s.z - sk_FragCoord.x, 0.0));\n "
39 "ySub = half(min(sk_FragCoord.y - %s.y, 0.0));\n ySub += half(min(%s.w - "
40 "sk_FragCoord.y, 0.0));\n alpha = half((1",
41 prevRect.left(),
42 prevRect.top(),
43 prevRect.right(),
44 prevRect.bottom(),
45 (int)_outer.edgeType(),
46 args.fUniformHandler->getUniformCStr(fRectUniformVar),
47 args.fUniformHandler->getUniformCStr(fRectUniformVar),
48 args.fUniformHandler->getUniformCStr(fRectUniformVar),
49 args.fUniformHandler->getUniformCStr(fRectUniformVar),
50 args.fUniformHandler->getUniformCStr(fRectUniformVar),
51 args.fUniformHandler->getUniformCStr(fRectUniformVar));
52 fragBuilder->codeAppendf(
53 ".0 + max(float(xSub), -1.0)) * (1.0 + max(float(ySub), -1.0)));\n}\n@if (%d == 2 "
54 "|| %d == 3) {\n alpha = half(1.0 - float(alpha));\n}\n%s = %s * alpha;\n",
55 (int)_outer.edgeType(),
56 (int)_outer.edgeType(),
57 args.fOutputColor,
58 args.fInputColor ? args.fInputColor : "half4(1)");
59 }
60
61 private:
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & _proc)62 void onSetData(const GrGLSLProgramDataManager& pdman,
63 const GrFragmentProcessor& _proc) override {
64 const GrAARectEffect& _outer = _proc.cast<GrAARectEffect>();
65 auto edgeType = _outer.edgeType();
66 (void)edgeType;
67 auto rect = _outer.rect();
68 (void)rect;
69 UniformHandle& rectUniform = fRectUniformVar;
70 (void)rectUniform;
71
72 const SkRect& newRect = GrProcessorEdgeTypeIsAA(edgeType) ? rect.makeInset(.5f, .5f) : rect;
73 if (newRect != prevRect) {
74 pdman.set4f(rectUniform, newRect.fLeft, newRect.fTop, newRect.fRight, newRect.fBottom);
75 prevRect = newRect;
76 }
77 }
78 SkRect prevRect = float4(0);
79 UniformHandle fRectUniformVar;
80 };
onCreateGLSLInstance() const81 GrGLSLFragmentProcessor* GrAARectEffect::onCreateGLSLInstance() const {
82 return new GrGLSLAARectEffect();
83 }
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const84 void GrAARectEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
85 GrProcessorKeyBuilder* b) const {
86 b->add32((int32_t)fEdgeType);
87 }
onIsEqual(const GrFragmentProcessor & other) const88 bool GrAARectEffect::onIsEqual(const GrFragmentProcessor& other) const {
89 const GrAARectEffect& that = other.cast<GrAARectEffect>();
90 (void)that;
91 if (fEdgeType != that.fEdgeType) return false;
92 if (fRect != that.fRect) return false;
93 return true;
94 }
GrAARectEffect(const GrAARectEffect & src)95 GrAARectEffect::GrAARectEffect(const GrAARectEffect& src)
96 : INHERITED(kGrAARectEffect_ClassID, src.optimizationFlags())
97 , fEdgeType(src.fEdgeType)
98 , fRect(src.fRect) {}
clone() const99 std::unique_ptr<GrFragmentProcessor> GrAARectEffect::clone() const {
100 return std::unique_ptr<GrFragmentProcessor>(new GrAARectEffect(*this));
101 }
102 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrAARectEffect);
103 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)104 std::unique_ptr<GrFragmentProcessor> GrAARectEffect::TestCreate(GrProcessorTestData* d) {
105 SkRect rect = SkRect::MakeLTRB(d->fRandom->nextSScalar1(),
106 d->fRandom->nextSScalar1(),
107 d->fRandom->nextSScalar1(),
108 d->fRandom->nextSScalar1());
109 std::unique_ptr<GrFragmentProcessor> fp;
110 do {
111 GrClipEdgeType edgeType =
112 static_cast<GrClipEdgeType>(d->fRandom->nextULessThan(kGrClipEdgeTypeCnt));
113
114 fp = GrAARectEffect::Make(edgeType, rect);
115 } while (nullptr == fp);
116 return fp;
117 }
118 #endif
119 #endif
120