1 | /* |
2 | * Copyright 2014 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 | #include "src/gpu/effects/GrMatrixConvolutionEffect.h" |
8 | |
9 | #include "include/private/SkHalf.h" |
10 | #include "src/gpu/GrBitmapTextureMaker.h" |
11 | #include "src/gpu/GrContextPriv.h" |
12 | #include "src/gpu/GrProxyProvider.h" |
13 | #include "src/gpu/GrRecordingContextPriv.h" |
14 | #include "src/gpu/GrTexture.h" |
15 | #include "src/gpu/GrTextureProxy.h" |
16 | #include "src/gpu/effects/GrTextureEffect.h" |
17 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
18 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
19 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
20 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
21 | |
22 | class GrGLMatrixConvolutionEffect : public GrGLSLFragmentProcessor { |
23 | public: |
24 | void emitCode(EmitArgs&) override; |
25 | |
26 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
27 | |
28 | protected: |
29 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
30 | |
31 | private: |
32 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
33 | |
34 | void emitKernelBlock(EmitArgs&, SkIPoint); |
35 | |
36 | UniformHandle fKernelUni; |
37 | UniformHandle fKernelOffsetUni; |
38 | UniformHandle fGainUni; |
39 | UniformHandle fBiasUni; |
40 | UniformHandle fKernelBiasUni; |
41 | |
42 | typedef GrGLSLFragmentProcessor INHERITED; |
43 | }; |
44 | |
45 | GrMatrixConvolutionEffect::KernelWrapper::MakeResult |
46 | GrMatrixConvolutionEffect::KernelWrapper::Make(GrRecordingContext* context, |
47 | SkISize size, |
48 | const GrCaps& caps, |
49 | const SkScalar* values) { |
50 | if (!context || !values || size.isEmpty()) { |
51 | return {}; |
52 | } |
53 | const int length = size.area(); |
54 | // Small kernel -> just fill the array. |
55 | KernelWrapper result(size); |
56 | if (length <= kMaxUniformSize) { |
57 | for (int i = 0; i < length; i++) { |
58 | result.fArray[i] = SkScalarToFloat(values[i]); |
59 | } |
60 | return {result, nullptr}; |
61 | } |
62 | |
63 | BiasAndGain& scalableSampler = result.fBiasAndGain; |
64 | bool useA16 = |
65 | context->defaultBackendFormat(kA16_float_SkColorType, GrRenderable::kNo).isValid(); |
66 | SkScalar min = values[0]; |
67 | if (!useA16) { |
68 | // Determine min and max values to figure out inner gain & bias. |
69 | SkScalar max = values[0]; |
70 | for (int i = 1; i < length; i++) { |
71 | if (values[i] < min) { |
72 | min = values[i]; |
73 | } |
74 | if (values[i] > max) { |
75 | max = values[i]; |
76 | } |
77 | } |
78 | // Treat near-0 gain (i.e. box blur) as 1, and let the kernelBias |
79 | // move everything up to the final value. |
80 | const SkScalar computedGain = max - min; |
81 | scalableSampler.fGain = |
82 | SkScalarNearlyZero(computedGain) ? 1.0f : SkScalarToFloat(computedGain); |
83 | // Inner bias is pre-inner-gain so we divide that out. |
84 | scalableSampler.fBias = SkScalarToFloat(min) / scalableSampler.fGain; |
85 | } |
86 | |
87 | // TODO: Pick cache or dont-cache based on observed perf. |
88 | static constexpr bool kCacheKernelTexture = true; |
89 | |
90 | GrUniqueKey key; |
91 | if (kCacheKernelTexture) { |
92 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
93 | GrUniqueKey::Builder builder(&key, kDomain, length, "Matrix Convolution Kernel" ); |
94 | // Texture cache key is the exact content of the kernel. |
95 | static_assert(sizeof(float) == 4); |
96 | for (int i = 0; i < length; i++) { |
97 | builder[i] = *(const uint32_t*)&values[i]; |
98 | } |
99 | builder.finish(); |
100 | } |
101 | |
102 | // Find or create a texture. |
103 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
104 | GrSurfaceProxyView view; |
105 | SkColorType colorType = useA16 ? kA16_float_SkColorType : kAlpha_8_SkColorType; |
106 | sk_sp<GrTextureProxy> cachedKernel; |
107 | if (kCacheKernelTexture && (cachedKernel = proxyProvider->findOrCreateProxyByUniqueKey(key))) { |
108 | GrSwizzle swizzle = |
109 | context->priv().caps()->getReadSwizzle(cachedKernel->backendFormat(), |
110 | SkColorTypeToGrColorType(colorType)); |
111 | view = {std::move(cachedKernel), kTopLeft_GrSurfaceOrigin, swizzle}; |
112 | } else { |
113 | SkBitmap bm; |
114 | auto info = SkImageInfo::Make({length, 1}, colorType, kPremul_SkAlphaType, nullptr); |
115 | if (!bm.tryAllocPixels(info)) { |
116 | return {}; |
117 | } |
118 | for (int i = 0; i < length; i++) { |
119 | if (useA16) { |
120 | *bm.getAddr16(i, 0) = SkFloatToHalf(values[i]); |
121 | } else { |
122 | *bm.getAddr8(i, 0) = |
123 | SkScalarRoundToInt((values[i] - min) / scalableSampler.fGain * 255); |
124 | } |
125 | } |
126 | bm.setImmutable(); |
127 | GrBitmapTextureMaker maker(context, bm, GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
128 | view = maker.view(GrMipmapped::kNo); |
129 | if (!view) { |
130 | return {}; |
131 | } |
132 | if (kCacheKernelTexture) { |
133 | proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy()); |
134 | } |
135 | } |
136 | auto kernelFP = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType); |
137 | return {result, std::move(kernelFP)}; |
138 | } |
139 | |
140 | bool GrMatrixConvolutionEffect::KernelWrapper::operator==(const KernelWrapper& k) const { |
141 | if (fSize != k.fSize) { |
142 | return false; |
143 | } else if (this->isSampled()) { |
144 | return fBiasAndGain == k.fBiasAndGain; |
145 | } else { |
146 | return std::equal(fArray.begin(), fArray.begin() + fSize.area(), k.fArray.begin()); |
147 | } |
148 | } |
149 | |
150 | bool GrMatrixConvolutionEffect::KernelWrapper::BiasAndGain::operator==( |
151 | const BiasAndGain& k) const { |
152 | return fGain == k.fGain && fBias == k.fBias; |
153 | } |
154 | |
155 | // For sampled kernels, emit a for loop that does all the kernel accumulation. |
156 | // For uniform kernels, emit a single iteration. Function is called repeatedly in a for loop. |
157 | // loc is ignored for sampled kernels. |
158 | void GrGLMatrixConvolutionEffect::emitKernelBlock(EmitArgs& args, SkIPoint loc) { |
159 | const GrMatrixConvolutionEffect& mce = args.fFp.cast<GrMatrixConvolutionEffect>(); |
160 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
161 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
162 | int kernelWidth = mce.kernelSize().width(); |
163 | int kernelHeight = mce.kernelSize().height(); |
164 | int kernelArea = kernelWidth * kernelHeight; |
165 | |
166 | if (mce.kernelIsSampled()) { |
167 | fragBuilder->codeAppendf("for (int i = 0; i < %d; ++i)" , (int)kernelArea); |
168 | } |
169 | |
170 | GrGLSLShaderBuilder::ShaderBlock block(fragBuilder); |
171 | |
172 | fragBuilder->codeAppend("half k;" ); |
173 | fragBuilder->codeAppend("half2 sourceOffset;" ); |
174 | if (mce.kernelIsSampled()) { |
175 | const char* kernelBias = uniformHandler->getUniformCStr(fKernelBiasUni); |
176 | SkString kernelCoord = SkStringPrintf("float2(float(i) + 0.5, 0.5)" ); |
177 | SkString kernelSample = this->invokeChild(1, args, kernelCoord.c_str()); |
178 | fragBuilder->codeAppendf("k = %s.w + %s;" , kernelSample.c_str(), kernelBias); |
179 | fragBuilder->codeAppendf("sourceOffset.y = floor(i / %d);" , kernelWidth); |
180 | fragBuilder->codeAppendf("sourceOffset.x = i - sourceOffset.y * %d;" , kernelWidth); |
181 | } else { |
182 | fragBuilder->codeAppendf("sourceOffset = half2(%d, %d);" , loc.x(), loc.y()); |
183 | int offset = loc.y() * kernelWidth + loc.x(); |
184 | static constexpr const char kVecSuffix[][4] = { ".x" , ".y" , ".z" , ".w" }; |
185 | const char* kernel = uniformHandler->getUniformCStr(fKernelUni); |
186 | fragBuilder->codeAppendf("k = %s[%d]%s;" , kernel, offset / 4, |
187 | kVecSuffix[offset & 0x3]); |
188 | } |
189 | |
190 | auto sample = this->invokeChild(0, args, "coord + sourceOffset" ); |
191 | fragBuilder->codeAppendf("half4 c = %s;" , sample.c_str()); |
192 | if (!mce.convolveAlpha()) { |
193 | fragBuilder->codeAppend("c = unpremul(c);" ); |
194 | fragBuilder->codeAppend("c.rgb = saturate(c.rgb);" ); |
195 | } |
196 | fragBuilder->codeAppend("sum += c * k;" ); |
197 | } |
198 | |
199 | void GrGLMatrixConvolutionEffect::emitCode(EmitArgs& args) { |
200 | const GrMatrixConvolutionEffect& mce = args.fFp.cast<GrMatrixConvolutionEffect>(); |
201 | |
202 | int kernelWidth = mce.kernelSize().width(); |
203 | int kernelHeight = mce.kernelSize().height(); |
204 | |
205 | int arrayCount = (kernelWidth * kernelHeight + 3) / 4; |
206 | SkASSERT(4 * arrayCount >= kernelWidth * kernelHeight); |
207 | |
208 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
209 | if (mce.kernelIsSampled()) { |
210 | fKernelBiasUni = uniformHandler->addUniform(&mce, kFragment_GrShaderFlag, |
211 | kHalf_GrSLType, "KernelBias" ); |
212 | } else { |
213 | fKernelUni = uniformHandler->addUniformArray(&mce, kFragment_GrShaderFlag, |
214 | kHalf4_GrSLType, "Kernel" , arrayCount); |
215 | } |
216 | fKernelOffsetUni = uniformHandler->addUniform(&mce, kFragment_GrShaderFlag, kHalf2_GrSLType, |
217 | "KernelOffset" ); |
218 | fGainUni = uniformHandler->addUniform(&mce, kFragment_GrShaderFlag, kHalf_GrSLType, "Gain" ); |
219 | fBiasUni = uniformHandler->addUniform(&mce, kFragment_GrShaderFlag, kHalf_GrSLType, "Bias" ); |
220 | |
221 | const char* kernelOffset = uniformHandler->getUniformCStr(fKernelOffsetUni); |
222 | const char* gain = uniformHandler->getUniformCStr(fGainUni); |
223 | const char* bias = uniformHandler->getUniformCStr(fBiasUni); |
224 | |
225 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
226 | fragBuilder->codeAppend("half4 sum = half4(0, 0, 0, 0);" ); |
227 | fragBuilder->codeAppendf("float2 coord = %s - %s;" , args.fSampleCoord, kernelOffset); |
228 | |
229 | if (mce.kernelIsSampled()) { |
230 | this->emitKernelBlock(args, {}); |
231 | } else { |
232 | for (int x = 0; x < kernelWidth; ++x) { |
233 | for (int y = 0; y < kernelHeight; ++y) { |
234 | this->emitKernelBlock(args, SkIPoint::Make(x, y)); |
235 | } |
236 | } |
237 | } |
238 | |
239 | if (mce.convolveAlpha()) { |
240 | fragBuilder->codeAppendf("%s = sum * %s + %s;" , args.fOutputColor, gain, bias); |
241 | fragBuilder->codeAppendf("%s.a = saturate(%s.a);" , args.fOutputColor, args.fOutputColor); |
242 | fragBuilder->codeAppendf("%s.rgb = clamp(%s.rgb, 0.0, %s.a);" , |
243 | args.fOutputColor, args.fOutputColor, args.fOutputColor); |
244 | } else { |
245 | auto sample = this->invokeChild(0, args); |
246 | fragBuilder->codeAppendf("half4 c = %s;" , sample.c_str()); |
247 | fragBuilder->codeAppendf("%s.a = c.a;" , args.fOutputColor); |
248 | fragBuilder->codeAppendf("%s.rgb = saturate(sum.rgb * %s + %s);" , args.fOutputColor, gain, bias); |
249 | fragBuilder->codeAppendf("%s.rgb *= %s.a;" , args.fOutputColor, args.fOutputColor); |
250 | } |
251 | fragBuilder->codeAppendf("%s *= %s;\n" , args.fOutputColor, args.fInputColor); |
252 | } |
253 | |
254 | void GrGLMatrixConvolutionEffect::GenKey(const GrProcessor& processor, |
255 | const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
256 | const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffect>(); |
257 | SkASSERT(m.kernelSize().width() <= 0x7FFF && m.kernelSize().height() <= 0xFFFF); |
258 | uint32_t key = m.kernelSize().width() << 16 | m.kernelSize().height(); |
259 | key |= m.convolveAlpha() ? 1U << 31 : 0; |
260 | b->add32(key); |
261 | } |
262 | |
263 | void GrGLMatrixConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
264 | const GrFragmentProcessor& processor) { |
265 | const GrMatrixConvolutionEffect& conv = processor.cast<GrMatrixConvolutionEffect>(); |
266 | pdman.set2f(fKernelOffsetUni, conv.kernelOffset().fX, conv.kernelOffset().fY); |
267 | float totalGain = conv.gain(); |
268 | if (conv.kernelIsSampled()) { |
269 | totalGain *= conv.kernelSampleGain(); |
270 | pdman.set1f(fKernelBiasUni, conv.kernelSampleBias()); |
271 | } else { |
272 | int kernelCount = conv.kernelSize().area(); |
273 | int arrayCount = (kernelCount + 3) / 4; |
274 | SkASSERT(4 * arrayCount >= kernelCount); |
275 | pdman.set4fv(fKernelUni, arrayCount, conv.kernel()); |
276 | } |
277 | pdman.set1f(fBiasUni, conv.bias()); |
278 | pdman.set1f(fGainUni, totalGain); |
279 | } |
280 | |
281 | GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(std::unique_ptr<GrFragmentProcessor> child, |
282 | const KernelWrapper& kernel, |
283 | std::unique_ptr<GrFragmentProcessor> kernelFP, |
284 | SkScalar gain, |
285 | SkScalar bias, |
286 | const SkIPoint& kernelOffset, |
287 | bool convolveAlpha) |
288 | // To advertise either the modulation or opaqueness optimizations we'd have to examine the |
289 | // parameters. |
290 | : INHERITED(kGrMatrixConvolutionEffect_ClassID, kNone_OptimizationFlags) |
291 | , fKernel(kernel) |
292 | , fGain(SkScalarToFloat(gain)) |
293 | , fBias(SkScalarToFloat(bias) / 255.0f) |
294 | , fConvolveAlpha(convolveAlpha) { |
295 | this->registerChild(std::move(child), SkSL::SampleUsage::Explicit()); |
296 | this->registerChild(std::move(kernelFP), SkSL::SampleUsage::Explicit()); |
297 | fKernelOffset = {static_cast<float>(kernelOffset.x()), |
298 | static_cast<float>(kernelOffset.y())}; |
299 | this->setUsesSampleCoordsDirectly(); |
300 | } |
301 | |
302 | GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(const GrMatrixConvolutionEffect& that) |
303 | : INHERITED(kGrMatrixConvolutionEffect_ClassID, kNone_OptimizationFlags) |
304 | , fKernel(that.fKernel) |
305 | , fGain(that.fGain) |
306 | , fBias(that.fBias) |
307 | , fKernelOffset(that.fKernelOffset) |
308 | , fConvolveAlpha(that.fConvolveAlpha) { |
309 | this->cloneAndRegisterAllChildProcessors(that); |
310 | this->setUsesSampleCoordsDirectly(); |
311 | } |
312 | |
313 | std::unique_ptr<GrFragmentProcessor> GrMatrixConvolutionEffect::clone() const { |
314 | return std::unique_ptr<GrFragmentProcessor>(new GrMatrixConvolutionEffect(*this)); |
315 | } |
316 | |
317 | void GrMatrixConvolutionEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
318 | GrProcessorKeyBuilder* b) const { |
319 | GrGLMatrixConvolutionEffect::GenKey(*this, caps, b); |
320 | } |
321 | |
322 | GrGLSLFragmentProcessor* GrMatrixConvolutionEffect::onCreateGLSLInstance() const { |
323 | return new GrGLMatrixConvolutionEffect; |
324 | } |
325 | |
326 | bool GrMatrixConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
327 | const GrMatrixConvolutionEffect& s = sBase.cast<GrMatrixConvolutionEffect>(); |
328 | return fKernel == s.fKernel && |
329 | fGain == s.gain() && |
330 | fBias == s.bias() && |
331 | fKernelOffset == s.kernelOffset() && |
332 | fConvolveAlpha == s.convolveAlpha(); |
333 | } |
334 | |
335 | static void fill_in_1D_gaussian_kernel_with_stride(float* kernel, int size, int stride, |
336 | float twoSigmaSqrd) { |
337 | SkASSERT(!SkScalarNearlyZero(twoSigmaSqrd, SK_ScalarNearlyZero)); |
338 | |
339 | const float sigmaDenom = 1.0f / twoSigmaSqrd; |
340 | const int radius = size / 2; |
341 | |
342 | float sum = 0.0f; |
343 | for (int i = 0; i < size; ++i) { |
344 | float term = static_cast<float>(i - radius); |
345 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
346 | // is dropped here, since we renormalize the kernel below. |
347 | kernel[i * stride] = sk_float_exp(-term * term * sigmaDenom); |
348 | sum += kernel[i * stride]; |
349 | } |
350 | // Normalize the kernel |
351 | float scale = 1.0f / sum; |
352 | for (int i = 0; i < size; ++i) { |
353 | kernel[i * stride] *= scale; |
354 | } |
355 | } |
356 | |
357 | static void fill_in_2D_gaussian_kernel(float* kernel, int width, int height, |
358 | SkScalar sigmaX, SkScalar sigmaY) { |
359 | const float twoSigmaSqrdX = 2.0f * SkScalarToFloat(SkScalarSquare(sigmaX)); |
360 | const float twoSigmaSqrdY = 2.0f * SkScalarToFloat(SkScalarSquare(sigmaY)); |
361 | |
362 | // TODO: in all of these degenerate cases we're uploading (and using) a whole lot of zeros. |
363 | if (SkScalarNearlyZero(twoSigmaSqrdX, SK_ScalarNearlyZero) || |
364 | SkScalarNearlyZero(twoSigmaSqrdY, SK_ScalarNearlyZero)) { |
365 | // In this case the 2D Gaussian degenerates to a 1D Gaussian (in X or Y) or a point |
366 | SkASSERT(3 == width || 3 == height); |
367 | std::fill_n(kernel, width*height, 0); |
368 | |
369 | if (SkScalarNearlyZero(twoSigmaSqrdX, SK_ScalarNearlyZero) && |
370 | SkScalarNearlyZero(twoSigmaSqrdY, SK_ScalarNearlyZero)) { |
371 | // A point |
372 | SkASSERT(3 == width && 3 == height); |
373 | kernel[4] = 1.0f; |
374 | } else if (SkScalarNearlyZero(twoSigmaSqrdX, SK_ScalarNearlyZero)) { |
375 | // A 1D Gaussian in Y |
376 | SkASSERT(3 == width); |
377 | // Down the middle column of the kernel with a stride of width |
378 | fill_in_1D_gaussian_kernel_with_stride(&kernel[1], height, width, twoSigmaSqrdY); |
379 | } else { |
380 | // A 1D Gaussian in X |
381 | SkASSERT(SkScalarNearlyZero(twoSigmaSqrdY, SK_ScalarNearlyZero)); |
382 | SkASSERT(3 == height); |
383 | // Down the middle row of the kernel with a stride of 1 |
384 | fill_in_1D_gaussian_kernel_with_stride(&kernel[width], width, 1, twoSigmaSqrdX); |
385 | } |
386 | return; |
387 | } |
388 | |
389 | const float sigmaXDenom = 1.0f / twoSigmaSqrdX; |
390 | const float sigmaYDenom = 1.0f / twoSigmaSqrdY; |
391 | const int xRadius = width / 2; |
392 | const int yRadius = height / 2; |
393 | |
394 | float sum = 0.0f; |
395 | for (int x = 0; x < width; x++) { |
396 | float xTerm = static_cast<float>(x - xRadius); |
397 | xTerm = xTerm * xTerm * sigmaXDenom; |
398 | for (int y = 0; y < height; y++) { |
399 | float yTerm = static_cast<float>(y - yRadius); |
400 | float xyTerm = sk_float_exp(-(xTerm + yTerm * yTerm * sigmaYDenom)); |
401 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
402 | // is dropped here, since we renormalize the kernel below. |
403 | kernel[y * width + x] = xyTerm; |
404 | sum += xyTerm; |
405 | } |
406 | } |
407 | // Normalize the kernel |
408 | float scale = 1.0f / sum; |
409 | for (int i = 0; i < width * height; ++i) { |
410 | kernel[i] *= scale; |
411 | } |
412 | } |
413 | |
414 | std::unique_ptr<GrFragmentProcessor> GrMatrixConvolutionEffect::Make(GrRecordingContext* context, |
415 | GrSurfaceProxyView srcView, |
416 | const SkIRect& srcBounds, |
417 | const SkISize& kernelSize, |
418 | const SkScalar* kernel, |
419 | SkScalar gain, |
420 | SkScalar bias, |
421 | const SkIPoint& kernelOffset, |
422 | GrSamplerState::WrapMode wm, |
423 | bool convolveAlpha, |
424 | const GrCaps& caps) { |
425 | auto [kernelWrapper, kernelFP] = KernelWrapper::Make(context, kernelSize, caps, kernel); |
426 | if (!kernelWrapper.isValid()) { |
427 | return nullptr; |
428 | } |
429 | GrSamplerState sampler(wm, GrSamplerState::Filter::kNearest); |
430 | auto child = GrTextureEffect::MakeSubset(std::move(srcView), kPremul_SkAlphaType, SkMatrix::I(), |
431 | sampler, SkRect::Make(srcBounds), caps); |
432 | return std::unique_ptr<GrFragmentProcessor>( |
433 | new GrMatrixConvolutionEffect(std::move(child), kernelWrapper, std::move(kernelFP), |
434 | gain, bias, kernelOffset, convolveAlpha)); |
435 | } |
436 | |
437 | std::unique_ptr<GrFragmentProcessor> GrMatrixConvolutionEffect::MakeGaussian( |
438 | GrRecordingContext* context, |
439 | GrSurfaceProxyView srcView, |
440 | const SkIRect& srcBounds, |
441 | const SkISize& kernelSize, |
442 | SkScalar gain, |
443 | SkScalar bias, |
444 | const SkIPoint& kernelOffset, |
445 | GrSamplerState::WrapMode wm, |
446 | bool convolveAlpha, |
447 | SkScalar sigmaX, |
448 | SkScalar sigmaY, |
449 | const GrCaps& caps) { |
450 | SkAutoSTMalloc<32, float> kernel(kernelSize.area()); |
451 | fill_in_2D_gaussian_kernel(kernel.get(), kernelSize.width(), kernelSize.height(), |
452 | sigmaX, sigmaY); |
453 | return Make(context, std::move(srcView), srcBounds, kernelSize, kernel.get(), |
454 | gain, bias, kernelOffset, wm, convolveAlpha, caps); |
455 | } |
456 | |
457 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMatrixConvolutionEffect); |
458 | |
459 | #if GR_TEST_UTILS |
460 | std::unique_ptr<GrFragmentProcessor> GrMatrixConvolutionEffect::TestCreate(GrProcessorTestData* d) { |
461 | auto [view, ct, at] = d->randomView(); |
462 | |
463 | static constexpr size_t kMaxTestKernelSize = 2 * kMaxUniformSize; |
464 | int width = d->fRandom->nextRangeU(1, kMaxTestKernelSize); |
465 | int height = d->fRandom->nextRangeU(1, kMaxTestKernelSize / width); |
466 | SkISize kernelSize = SkISize::Make(width, height); |
467 | std::unique_ptr<SkScalar[]> kernel(new SkScalar[width * height]); |
468 | for (int i = 0; i < width * height; i++) { |
469 | kernel.get()[i] = d->fRandom->nextSScalar1(); |
470 | } |
471 | SkScalar gain = d->fRandom->nextSScalar1(); |
472 | SkScalar bias = d->fRandom->nextSScalar1(); |
473 | |
474 | uint32_t kernalOffsetX = d->fRandom->nextRangeU(0, kernelSize.width()); |
475 | uint32_t kernalOffsetY = d->fRandom->nextRangeU(0, kernelSize.height()); |
476 | SkIPoint kernelOffset = SkIPoint::Make(kernalOffsetX, kernalOffsetY); |
477 | |
478 | uint32_t boundsX = d->fRandom->nextRangeU(0, view.width()); |
479 | uint32_t boundsY = d->fRandom->nextRangeU(0, view.height()); |
480 | uint32_t boundsW = d->fRandom->nextRangeU(0, view.width()); |
481 | uint32_t boundsH = d->fRandom->nextRangeU(0, view.height()); |
482 | SkIRect bounds = SkIRect::MakeXYWH(boundsX, boundsY, boundsW, boundsH); |
483 | |
484 | auto wm = static_cast<GrSamplerState::WrapMode>( |
485 | d->fRandom->nextULessThan(GrSamplerState::kWrapModeCount)); |
486 | bool convolveAlpha = d->fRandom->nextBool(); |
487 | return GrMatrixConvolutionEffect::Make(d->context(), |
488 | std::move(view), |
489 | bounds, |
490 | kernelSize, |
491 | kernel.get(), |
492 | gain, |
493 | bias, |
494 | kernelOffset, |
495 | wm, |
496 | convolveAlpha, |
497 | *d->caps()); |
498 | } |
499 | #endif |
500 | |