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 GrRRectBlurEffect.fp; do not modify. |
10 | **************************************************************************************************/ |
11 | #include "GrRRectBlurEffect.h" |
12 | |
13 | std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::Make( |
14 | std::unique_ptr<GrFragmentProcessor> inputFP, |
15 | GrRecordingContext* context, |
16 | float sigma, |
17 | float xformedSigma, |
18 | const SkRRect& srcRRect, |
19 | const SkRRect& devRRect) { |
20 | SkASSERT(!SkRRectPriv::IsCircle(devRRect) && |
21 | !devRRect.isRect()); // Should've been caught up-stream |
22 | |
23 | // TODO: loosen this up |
24 | if (!SkRRectPriv::IsSimpleCircular(devRRect)) { |
25 | return nullptr; |
26 | } |
27 | |
28 | // Make sure we can successfully ninepatch this rrect -- the blur sigma has to be |
29 | // sufficiently small relative to both the size of the corner radius and the |
30 | // width (and height) of the rrect. |
31 | SkRRect rrectToDraw; |
32 | SkISize dimensions; |
33 | SkScalar ignored[kSkBlurRRectMaxDivisions]; |
34 | int ignoredSize; |
35 | uint32_t ignored32; |
36 | |
37 | bool ninePatchable = SkComputeBlurredRRectParams( |
38 | srcRRect, devRRect, SkRect::MakeEmpty(), sigma, xformedSigma, &rrectToDraw, &dimensions, |
39 | ignored, ignored, ignored, ignored, &ignoredSize, &ignoredSize, &ignored32); |
40 | if (!ninePatchable) { |
41 | return nullptr; |
42 | } |
43 | |
44 | std::unique_ptr<GrFragmentProcessor> maskFP = |
45 | find_or_create_rrect_blur_mask_fp(context, rrectToDraw, dimensions, xformedSigma); |
46 | if (!maskFP) { |
47 | return nullptr; |
48 | } |
49 | |
50 | return std::unique_ptr<GrFragmentProcessor>( |
51 | new GrRRectBlurEffect(std::move(inputFP), xformedSigma, devRRect.getBounds(), |
52 | SkRRectPriv::GetSimpleRadii(devRRect).fX, std::move(maskFP))); |
53 | } |
54 | #include "src/core/SkUtils.h" |
55 | #include "src/gpu/GrTexture.h" |
56 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
57 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
58 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
59 | #include "src/sksl/SkSLCPP.h" |
60 | #include "src/sksl/SkSLUtil.h" |
61 | class GrGLSLRRectBlurEffect : public GrGLSLFragmentProcessor { |
62 | public: |
63 | GrGLSLRRectBlurEffect() {} |
64 | void emitCode(EmitArgs& args) override { |
65 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
66 | const GrRRectBlurEffect& _outer = args.fFp.cast<GrRRectBlurEffect>(); |
67 | (void)_outer; |
68 | auto sigma = _outer.sigma; |
69 | (void)sigma; |
70 | auto rect = _outer.rect; |
71 | (void)rect; |
72 | auto cornerRadius = _outer.cornerRadius; |
73 | (void)cornerRadius; |
74 | cornerRadiusVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
75 | kHalf_GrSLType, "cornerRadius" ); |
76 | proxyRectVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
77 | kFloat4_GrSLType, "proxyRect" ); |
78 | blurRadiusVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
79 | kHalf_GrSLType, "blurRadius" ); |
80 | fragBuilder->codeAppendf( |
81 | R"SkSL(half2 translatedFragPos = half2(sk_FragCoord.xy - %s.xy); |
82 | half2 proxyCenter = half2((%s.zw - %s.xy) * 0.5); |
83 | half edgeSize = (2.0 * %s + %s) + 0.5; |
84 | translatedFragPos -= proxyCenter; |
85 | half2 fragDirection = sign(translatedFragPos); |
86 | translatedFragPos = abs(translatedFragPos); |
87 | translatedFragPos -= proxyCenter - edgeSize; |
88 | translatedFragPos = max(translatedFragPos, 0.0); |
89 | translatedFragPos *= fragDirection; |
90 | translatedFragPos += half2(edgeSize); |
91 | half2 proxyDims = half2(2.0 * edgeSize); |
92 | half2 texCoord = translatedFragPos / proxyDims;)SkSL" , |
93 | args.fUniformHandler->getUniformCStr(proxyRectVar), |
94 | args.fUniformHandler->getUniformCStr(proxyRectVar), |
95 | args.fUniformHandler->getUniformCStr(proxyRectVar), |
96 | args.fUniformHandler->getUniformCStr(blurRadiusVar), |
97 | args.fUniformHandler->getUniformCStr(cornerRadiusVar)); |
98 | SkString _sample9554 = this->invokeChild(0, args); |
99 | fragBuilder->codeAppendf( |
100 | R"SkSL( |
101 | half4 inputColor = %s;)SkSL" , |
102 | _sample9554.c_str()); |
103 | SkString _coords9602("float2(texCoord)" ); |
104 | SkString _sample9602 = this->invokeChild(1, args, _coords9602.c_str()); |
105 | fragBuilder->codeAppendf( |
106 | R"SkSL( |
107 | %s = inputColor * %s; |
108 | )SkSL" , |
109 | args.fOutputColor, _sample9602.c_str()); |
110 | } |
111 | |
112 | private: |
113 | void onSetData(const GrGLSLProgramDataManager& pdman, |
114 | const GrFragmentProcessor& _proc) override { |
115 | const GrRRectBlurEffect& _outer = _proc.cast<GrRRectBlurEffect>(); |
116 | { pdman.set1f(cornerRadiusVar, (_outer.cornerRadius)); } |
117 | auto sigma = _outer.sigma; |
118 | (void)sigma; |
119 | auto rect = _outer.rect; |
120 | (void)rect; |
121 | UniformHandle& cornerRadius = cornerRadiusVar; |
122 | (void)cornerRadius; |
123 | UniformHandle& proxyRect = proxyRectVar; |
124 | (void)proxyRect; |
125 | UniformHandle& blurRadius = blurRadiusVar; |
126 | (void)blurRadius; |
127 | |
128 | float blurRadiusValue = 3.f * SkScalarCeilToScalar(sigma - 1 / 6.0f); |
129 | pdman.set1f(blurRadius, blurRadiusValue); |
130 | |
131 | SkRect outset = rect; |
132 | outset.outset(blurRadiusValue, blurRadiusValue); |
133 | pdman.set4f(proxyRect, outset.fLeft, outset.fTop, outset.fRight, outset.fBottom); |
134 | } |
135 | UniformHandle proxyRectVar; |
136 | UniformHandle blurRadiusVar; |
137 | UniformHandle cornerRadiusVar; |
138 | }; |
139 | GrGLSLFragmentProcessor* GrRRectBlurEffect::onCreateGLSLInstance() const { |
140 | return new GrGLSLRRectBlurEffect(); |
141 | } |
142 | void GrRRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
143 | GrProcessorKeyBuilder* b) const {} |
144 | bool GrRRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const { |
145 | const GrRRectBlurEffect& that = other.cast<GrRRectBlurEffect>(); |
146 | (void)that; |
147 | if (sigma != that.sigma) return false; |
148 | if (rect != that.rect) return false; |
149 | if (cornerRadius != that.cornerRadius) return false; |
150 | return true; |
151 | } |
152 | GrRRectBlurEffect::GrRRectBlurEffect(const GrRRectBlurEffect& src) |
153 | : INHERITED(kGrRRectBlurEffect_ClassID, src.optimizationFlags()) |
154 | , sigma(src.sigma) |
155 | , rect(src.rect) |
156 | , cornerRadius(src.cornerRadius) { |
157 | this->cloneAndRegisterAllChildProcessors(src); |
158 | } |
159 | std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::clone() const { |
160 | return std::make_unique<GrRRectBlurEffect>(*this); |
161 | } |
162 | #if GR_TEST_UTILS |
163 | SkString GrRRectBlurEffect::onDumpInfo() const { |
164 | return SkStringPrintf("(sigma=%f, rect=float4(%f, %f, %f, %f), cornerRadius=%f)" , sigma, |
165 | rect.left(), rect.top(), rect.right(), rect.bottom(), cornerRadius); |
166 | } |
167 | #endif |
168 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrRRectBlurEffect); |
169 | #if GR_TEST_UTILS |
170 | std::unique_ptr<GrFragmentProcessor> GrRRectBlurEffect::TestCreate(GrProcessorTestData* d) { |
171 | SkScalar w = d->fRandom->nextRangeScalar(100.f, 1000.f); |
172 | SkScalar h = d->fRandom->nextRangeScalar(100.f, 1000.f); |
173 | SkScalar r = d->fRandom->nextRangeF(1.f, 9.f); |
174 | SkScalar sigma = d->fRandom->nextRangeF(1.f, 10.f); |
175 | SkRRect rrect; |
176 | rrect.setRectXY(SkRect::MakeWH(w, h), r, r); |
177 | return GrRRectBlurEffect::Make(d->inputFP(), d->context(), sigma, sigma, rrect, rrect); |
178 | } |
179 | #endif |
180 | |