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