| 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 "src/gpu/GrTexture.h" | 
|---|
| 10 | #include "src/gpu/GrTextureProxy.h" | 
|---|
| 11 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" | 
|---|
| 12 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" | 
|---|
| 13 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" | 
|---|
| 14 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" | 
|---|
| 15 |  | 
|---|
| 16 | class GrGLMatrixConvolutionEffect : public GrGLSLFragmentProcessor { | 
|---|
| 17 | public: | 
|---|
| 18 | void emitCode(EmitArgs&) override; | 
|---|
| 19 |  | 
|---|
| 20 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); | 
|---|
| 21 |  | 
|---|
| 22 | protected: | 
|---|
| 23 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; | 
|---|
| 24 |  | 
|---|
| 25 | private: | 
|---|
| 26 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; | 
|---|
| 27 |  | 
|---|
| 28 | UniformHandle               fKernelUni; | 
|---|
| 29 | UniformHandle               fImageIncrementUni; | 
|---|
| 30 | UniformHandle               fKernelOffsetUni; | 
|---|
| 31 | UniformHandle               fGainUni; | 
|---|
| 32 | UniformHandle               fBiasUni; | 
|---|
| 33 | GrTextureDomain::GLDomain   fDomain; | 
|---|
| 34 |  | 
|---|
| 35 | typedef GrGLSLFragmentProcessor INHERITED; | 
|---|
| 36 | }; | 
|---|
| 37 |  | 
|---|
| 38 | void GrGLMatrixConvolutionEffect::emitCode(EmitArgs& args) { | 
|---|
| 39 | const GrMatrixConvolutionEffect& mce = args.fFp.cast<GrMatrixConvolutionEffect>(); | 
|---|
| 40 | const GrTextureDomain& domain = mce.domain(); | 
|---|
| 41 |  | 
|---|
| 42 | int kWidth = mce.kernelSize().width(); | 
|---|
| 43 | int kHeight = mce.kernelSize().height(); | 
|---|
| 44 |  | 
|---|
| 45 | int arrayCount = (kWidth * kHeight + 3) / 4; | 
|---|
| 46 | SkASSERT(4 * arrayCount >= kWidth * kHeight); | 
|---|
| 47 |  | 
|---|
| 48 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 
|---|
| 49 | fImageIncrementUni = uniformHandler->addUniform(&mce, kFragment_GrShaderFlag, kHalf2_GrSLType, | 
|---|
| 50 | "ImageIncrement"); | 
|---|
| 51 | fKernelUni = uniformHandler->addUniformArray(&mce, kFragment_GrShaderFlag, kHalf4_GrSLType, | 
|---|
| 52 | "Kernel", | 
|---|
| 53 | arrayCount); | 
|---|
| 54 | fKernelOffsetUni = uniformHandler->addUniform(&mce, kFragment_GrShaderFlag, kHalf2_GrSLType, | 
|---|
| 55 | "KernelOffset"); | 
|---|
| 56 | fGainUni = uniformHandler->addUniform(&mce, kFragment_GrShaderFlag, kHalf_GrSLType, "Gain"); | 
|---|
| 57 | fBiasUni = uniformHandler->addUniform(&mce, kFragment_GrShaderFlag, kHalf_GrSLType, "Bias"); | 
|---|
| 58 |  | 
|---|
| 59 | const char* kernelOffset = uniformHandler->getUniformCStr(fKernelOffsetUni); | 
|---|
| 60 | const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni); | 
|---|
| 61 | const char* kernel = uniformHandler->getUniformCStr(fKernelUni); | 
|---|
| 62 | const char* gain = uniformHandler->getUniformCStr(fGainUni); | 
|---|
| 63 | const char* bias = uniformHandler->getUniformCStr(fBiasUni); | 
|---|
| 64 |  | 
|---|
| 65 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; | 
|---|
| 66 | SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint); | 
|---|
| 67 | fragBuilder->codeAppend( "half4 sum = half4(0, 0, 0, 0);"); | 
|---|
| 68 | fragBuilder->codeAppendf( "float2 coord = %s - %s * %s;", coords2D.c_str(), kernelOffset, imgInc); | 
|---|
| 69 | fragBuilder->codeAppend( "half4 c;"); | 
|---|
| 70 |  | 
|---|
| 71 | const char* kVecSuffix[4] = { ".x", ".y", ".z", ".w"}; | 
|---|
| 72 | for (int y = 0; y < kHeight; y++) { | 
|---|
| 73 | for (int x = 0; x < kWidth; x++) { | 
|---|
| 74 | GrGLSLShaderBuilder::ShaderBlock block(fragBuilder); | 
|---|
| 75 | int offset = y*kWidth + x; | 
|---|
| 76 |  | 
|---|
| 77 | fragBuilder->codeAppendf( "half k = %s[%d]%s;", kernel, offset / 4, | 
|---|
| 78 | kVecSuffix[offset & 0x3]); | 
|---|
| 79 | SkString coord; | 
|---|
| 80 | coord.printf( "coord + half2(%d, %d) * %s", x, y, imgInc); | 
|---|
| 81 | fDomain.sampleTexture(&mce, | 
|---|
| 82 | fragBuilder, | 
|---|
| 83 | uniformHandler, | 
|---|
| 84 | args.fShaderCaps, | 
|---|
| 85 | domain, | 
|---|
| 86 | "c", | 
|---|
| 87 | coord, | 
|---|
| 88 | args.fTexSamplers[0]); | 
|---|
| 89 | if (!mce.convolveAlpha()) { | 
|---|
| 90 | fragBuilder->codeAppend( "c.rgb /= c.a;"); | 
|---|
| 91 | fragBuilder->codeAppend( "c.rgb = saturate(c.rgb);"); | 
|---|
| 92 | } | 
|---|
| 93 | fragBuilder->codeAppend( "sum += c * k;"); | 
|---|
| 94 | } | 
|---|
| 95 | } | 
|---|
| 96 | if (mce.convolveAlpha()) { | 
|---|
| 97 | fragBuilder->codeAppendf( "%s = sum * %s + %s;", args.fOutputColor, gain, bias); | 
|---|
| 98 | fragBuilder->codeAppendf( "%s.a = saturate(%s.a);", args.fOutputColor, args.fOutputColor); | 
|---|
| 99 | fragBuilder->codeAppendf( "%s.rgb = clamp(%s.rgb, 0.0, %s.a);", | 
|---|
| 100 | args.fOutputColor, args.fOutputColor, args.fOutputColor); | 
|---|
| 101 | } else { | 
|---|
| 102 | fDomain.sampleTexture(&mce, | 
|---|
| 103 | fragBuilder, | 
|---|
| 104 | uniformHandler, | 
|---|
| 105 | args.fShaderCaps, | 
|---|
| 106 | domain, | 
|---|
| 107 | "c", | 
|---|
| 108 | coords2D, | 
|---|
| 109 | args.fTexSamplers[0]); | 
|---|
| 110 | fragBuilder->codeAppendf( "%s.a = c.a;", args.fOutputColor); | 
|---|
| 111 | fragBuilder->codeAppendf( "%s.rgb = saturate(sum.rgb * %s + %s);", args.fOutputColor, gain, bias); | 
|---|
| 112 | fragBuilder->codeAppendf( "%s.rgb *= %s.a;", args.fOutputColor, args.fOutputColor); | 
|---|
| 113 | } | 
|---|
| 114 | fragBuilder->codeAppendf( "%s *= %s;\n", args.fOutputColor, args.fInputColor); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | void GrGLMatrixConvolutionEffect::GenKey(const GrProcessor& processor, | 
|---|
| 118 | const GrShaderCaps&, GrProcessorKeyBuilder* b) { | 
|---|
| 119 | const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffect>(); | 
|---|
| 120 | SkASSERT(m.kernelSize().width() <= 0x7FFF && m.kernelSize().height() <= 0xFFFF); | 
|---|
| 121 | uint32_t key = m.kernelSize().width() << 16 | m.kernelSize().height(); | 
|---|
| 122 | key |= m.convolveAlpha() ? 1U << 31 : 0; | 
|---|
| 123 | b->add32(key); | 
|---|
| 124 | b->add32(GrTextureDomain::GLDomain::DomainKey(m.domain())); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | void GrGLMatrixConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman, | 
|---|
| 128 | const GrFragmentProcessor& processor) { | 
|---|
| 129 | const GrMatrixConvolutionEffect& conv = processor.cast<GrMatrixConvolutionEffect>(); | 
|---|
| 130 | const auto& view = conv.textureSampler(0).view(); | 
|---|
| 131 | SkISize textureDims = view.proxy()->backingStoreDimensions(); | 
|---|
| 132 |  | 
|---|
| 133 | float imageIncrement[2]; | 
|---|
| 134 | float ySign = view.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; | 
|---|
| 135 | imageIncrement[0] = 1.0f / textureDims.width(); | 
|---|
| 136 | imageIncrement[1] = ySign / textureDims.height(); | 
|---|
| 137 | pdman.set2fv(fImageIncrementUni, 1, imageIncrement); | 
|---|
| 138 | pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); | 
|---|
| 139 | int kernelCount = conv.kernelSize().width() * conv.kernelSize().height(); | 
|---|
| 140 | int arrayCount = (kernelCount + 3) / 4; | 
|---|
| 141 | SkASSERT(4 * arrayCount >= kernelCount); | 
|---|
| 142 | pdman.set4fv(fKernelUni, arrayCount, conv.kernel()); | 
|---|
| 143 | pdman.set1f(fGainUni, conv.gain()); | 
|---|
| 144 | pdman.set1f(fBiasUni, conv.bias()); | 
|---|
| 145 | fDomain.setData(pdman, conv.domain(), view, conv.textureSampler(0).samplerState()); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrSurfaceProxyView srcView, | 
|---|
| 149 | const SkIRect& srcBounds, | 
|---|
| 150 | const SkISize& kernelSize, | 
|---|
| 151 | const SkScalar* kernel, | 
|---|
| 152 | SkScalar gain, | 
|---|
| 153 | SkScalar bias, | 
|---|
| 154 | const SkIPoint& kernelOffset, | 
|---|
| 155 | GrTextureDomain::Mode tileMode, | 
|---|
| 156 | bool convolveAlpha) | 
|---|
| 157 | // To advertise either the modulation or opaqueness optimizations we'd have to examine the | 
|---|
| 158 | // parameters. | 
|---|
| 159 | : INHERITED(kGrMatrixConvolutionEffect_ClassID, kNone_OptimizationFlags) | 
|---|
| 160 | , fCoordTransform(srcView.proxy(), srcView.origin()) | 
|---|
| 161 | , fDomain(srcView.proxy(), GrTextureDomain::MakeTexelDomain(srcBounds, tileMode), | 
|---|
| 162 | tileMode, tileMode) | 
|---|
| 163 | , fTextureSampler(std::move(srcView)) | 
|---|
| 164 | , fKernelSize(kernelSize) | 
|---|
| 165 | , fGain(SkScalarToFloat(gain)) | 
|---|
| 166 | , fBias(SkScalarToFloat(bias) / 255.0f) | 
|---|
| 167 | , fConvolveAlpha(convolveAlpha) { | 
|---|
| 168 | this->addCoordTransform(&fCoordTransform); | 
|---|
| 169 | this->setTextureSamplerCnt(1); | 
|---|
| 170 | for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) { | 
|---|
| 171 | fKernel[i] = SkScalarToFloat(kernel[i]); | 
|---|
| 172 | } | 
|---|
| 173 | fKernelOffset[0] = static_cast<float>(kernelOffset.x()); | 
|---|
| 174 | fKernelOffset[1] = static_cast<float>(kernelOffset.y()); | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(const GrMatrixConvolutionEffect& that) | 
|---|
| 178 | : INHERITED(kGrMatrixConvolutionEffect_ClassID, kNone_OptimizationFlags) | 
|---|
| 179 | , fCoordTransform(that.fCoordTransform) | 
|---|
| 180 | , fDomain(that.fDomain) | 
|---|
| 181 | , fTextureSampler(that.fTextureSampler) | 
|---|
| 182 | , fKernelSize(that.fKernelSize) | 
|---|
| 183 | , fGain(that.fGain) | 
|---|
| 184 | , fBias(that.fBias) | 
|---|
| 185 | , fConvolveAlpha(that.fConvolveAlpha) { | 
|---|
| 186 | this->addCoordTransform(&fCoordTransform); | 
|---|
| 187 | this->setTextureSamplerCnt(1); | 
|---|
| 188 | memcpy(fKernel, that.fKernel, sizeof(float) * fKernelSize.width() * fKernelSize.height()); | 
|---|
| 189 | memcpy(fKernelOffset, that.fKernelOffset, sizeof(fKernelOffset)); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | std::unique_ptr<GrFragmentProcessor> GrMatrixConvolutionEffect::clone() const { | 
|---|
| 193 | return std::unique_ptr<GrFragmentProcessor>(new GrMatrixConvolutionEffect(*this)); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | void GrMatrixConvolutionEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, | 
|---|
| 197 | GrProcessorKeyBuilder* b) const { | 
|---|
| 198 | GrGLMatrixConvolutionEffect::GenKey(*this, caps, b); | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | GrGLSLFragmentProcessor* GrMatrixConvolutionEffect::onCreateGLSLInstance() const  { | 
|---|
| 202 | return new GrGLMatrixConvolutionEffect; | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | bool GrMatrixConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const { | 
|---|
| 206 | const GrMatrixConvolutionEffect& s = sBase.cast<GrMatrixConvolutionEffect>(); | 
|---|
| 207 | return fKernelSize == s.kernelSize() && | 
|---|
| 208 | !memcmp(fKernel, s.kernel(), | 
|---|
| 209 | fKernelSize.width() * fKernelSize.height() * sizeof(float)) && | 
|---|
| 210 | fGain == s.gain() && | 
|---|
| 211 | fBias == s.bias() && | 
|---|
| 212 | !memcmp(fKernelOffset, s.kernelOffset(), sizeof(fKernelOffset)) && | 
|---|
| 213 | fConvolveAlpha == s.convolveAlpha() && | 
|---|
| 214 | fDomain == s.domain(); | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | static void fill_in_1D_gaussian_kernel_with_stride(float* kernel, int size, int stride, | 
|---|
| 218 | float twoSigmaSqrd) { | 
|---|
| 219 | SkASSERT(!SkScalarNearlyZero(twoSigmaSqrd, SK_ScalarNearlyZero)); | 
|---|
| 220 |  | 
|---|
| 221 | const float sigmaDenom = 1.0f / twoSigmaSqrd; | 
|---|
| 222 | const int radius = size / 2; | 
|---|
| 223 |  | 
|---|
| 224 | float sum = 0.0f; | 
|---|
| 225 | for (int i = 0; i < size; ++i) { | 
|---|
| 226 | float term = static_cast<float>(i - radius); | 
|---|
| 227 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian | 
|---|
| 228 | // is dropped here, since we renormalize the kernel below. | 
|---|
| 229 | kernel[i * stride] = sk_float_exp(-term * term * sigmaDenom); | 
|---|
| 230 | sum += kernel[i * stride]; | 
|---|
| 231 | } | 
|---|
| 232 | // Normalize the kernel | 
|---|
| 233 | float scale = 1.0f / sum; | 
|---|
| 234 | for (int i = 0; i < size; ++i) { | 
|---|
| 235 | kernel[i * stride] *= scale; | 
|---|
| 236 | } | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | static void fill_in_2D_gaussian_kernel(float* kernel, int width, int height, | 
|---|
| 240 | SkScalar sigmaX, SkScalar sigmaY) { | 
|---|
| 241 | SkASSERT(width * height <= MAX_KERNEL_SIZE); | 
|---|
| 242 | const float twoSigmaSqrdX = 2.0f * SkScalarToFloat(SkScalarSquare(sigmaX)); | 
|---|
| 243 | const float twoSigmaSqrdY = 2.0f * SkScalarToFloat(SkScalarSquare(sigmaY)); | 
|---|
| 244 |  | 
|---|
| 245 | // TODO: in all of these degenerate cases we're uploading (and using) a whole lot of zeros. | 
|---|
| 246 | if (SkScalarNearlyZero(twoSigmaSqrdX, SK_ScalarNearlyZero) || | 
|---|
| 247 | SkScalarNearlyZero(twoSigmaSqrdY, SK_ScalarNearlyZero)) { | 
|---|
| 248 | // In this case the 2D Gaussian degenerates to a 1D Gaussian (in X or Y) or a point | 
|---|
| 249 | SkASSERT(3 == width || 3 == height); | 
|---|
| 250 | memset(kernel, 0, width*height*sizeof(float)); | 
|---|
| 251 |  | 
|---|
| 252 | if (SkScalarNearlyZero(twoSigmaSqrdX, SK_ScalarNearlyZero) && | 
|---|
| 253 | SkScalarNearlyZero(twoSigmaSqrdY, SK_ScalarNearlyZero)) { | 
|---|
| 254 | // A point | 
|---|
| 255 | SkASSERT(3 == width && 3 == height); | 
|---|
| 256 | kernel[4] = 1.0f; | 
|---|
| 257 | } else if (SkScalarNearlyZero(twoSigmaSqrdX, SK_ScalarNearlyZero)) { | 
|---|
| 258 | // A 1D Gaussian in Y | 
|---|
| 259 | SkASSERT(3 == width); | 
|---|
| 260 | // Down the middle column of the kernel with a stride of width | 
|---|
| 261 | fill_in_1D_gaussian_kernel_with_stride(&kernel[1], height, width, twoSigmaSqrdY); | 
|---|
| 262 | } else { | 
|---|
| 263 | // A 1D Gaussian in X | 
|---|
| 264 | SkASSERT(SkScalarNearlyZero(twoSigmaSqrdY, SK_ScalarNearlyZero)); | 
|---|
| 265 | SkASSERT(3 == height); | 
|---|
| 266 | // Down the middle row of the kernel with a stride of 1 | 
|---|
| 267 | fill_in_1D_gaussian_kernel_with_stride(&kernel[width], width, 1, twoSigmaSqrdX); | 
|---|
| 268 | } | 
|---|
| 269 | return; | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | const float sigmaXDenom = 1.0f / twoSigmaSqrdX; | 
|---|
| 273 | const float sigmaYDenom = 1.0f / twoSigmaSqrdY; | 
|---|
| 274 | const int xRadius = width / 2; | 
|---|
| 275 | const int yRadius = height / 2; | 
|---|
| 276 |  | 
|---|
| 277 | float sum = 0.0f; | 
|---|
| 278 | for (int x = 0; x < width; x++) { | 
|---|
| 279 | float xTerm = static_cast<float>(x - xRadius); | 
|---|
| 280 | xTerm = xTerm * xTerm * sigmaXDenom; | 
|---|
| 281 | for (int y = 0; y < height; y++) { | 
|---|
| 282 | float yTerm = static_cast<float>(y - yRadius); | 
|---|
| 283 | float xyTerm = sk_float_exp(-(xTerm + yTerm * yTerm * sigmaYDenom)); | 
|---|
| 284 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian | 
|---|
| 285 | // is dropped here, since we renormalize the kernel below. | 
|---|
| 286 | kernel[y * width + x] = xyTerm; | 
|---|
| 287 | sum += xyTerm; | 
|---|
| 288 | } | 
|---|
| 289 | } | 
|---|
| 290 | // Normalize the kernel | 
|---|
| 291 | float scale = 1.0f / sum; | 
|---|
| 292 | for (int i = 0; i < width * height; ++i) { | 
|---|
| 293 | kernel[i] *= scale; | 
|---|
| 294 | } | 
|---|
| 295 | } | 
|---|
| 296 |  | 
|---|
| 297 | // Static function to create a 2D convolution | 
|---|
| 298 | std::unique_ptr<GrFragmentProcessor> GrMatrixConvolutionEffect::MakeGaussian( | 
|---|
| 299 | GrSurfaceProxyView srcView, | 
|---|
| 300 | const SkIRect& srcBounds, | 
|---|
| 301 | const SkISize& kernelSize, | 
|---|
| 302 | SkScalar gain, | 
|---|
| 303 | SkScalar bias, | 
|---|
| 304 | const SkIPoint& kernelOffset, | 
|---|
| 305 | GrTextureDomain::Mode tileMode, | 
|---|
| 306 | bool convolveAlpha, | 
|---|
| 307 | SkScalar sigmaX, | 
|---|
| 308 | SkScalar sigmaY) { | 
|---|
| 309 | // SkGpuBlurUtils is not as aggressive as it once was about avoiding texture domains. | 
|---|
| 310 | // Check for a trivial case here where the domain can be avoided. TODO: Use GrTextureEffect | 
|---|
| 311 | // here which includes this and more. | 
|---|
| 312 | if (tileMode == GrTextureDomain::kClamp_Mode && !srcView.proxy()->isFullyLazy() && | 
|---|
| 313 | srcBounds.contains(SkIRect::MakeSize(srcView.proxy()->backingStoreDimensions()))) { | 
|---|
| 314 | tileMode = GrTextureDomain::kIgnore_Mode; | 
|---|
| 315 | } | 
|---|
| 316 | float kernel[MAX_KERNEL_SIZE]; | 
|---|
| 317 |  | 
|---|
| 318 | fill_in_2D_gaussian_kernel(kernel, kernelSize.width(), kernelSize.height(), sigmaX, sigmaY); | 
|---|
| 319 |  | 
|---|
| 320 | return std::unique_ptr<GrFragmentProcessor>( | 
|---|
| 321 | new GrMatrixConvolutionEffect(std::move(srcView), srcBounds, kernelSize, kernel, | 
|---|
| 322 | gain, bias, kernelOffset, tileMode, convolveAlpha)); | 
|---|
| 323 | } | 
|---|
| 324 |  | 
|---|
| 325 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMatrixConvolutionEffect); | 
|---|
| 326 |  | 
|---|
| 327 | #if GR_TEST_UTILS | 
|---|
| 328 | std::unique_ptr<GrFragmentProcessor> GrMatrixConvolutionEffect::TestCreate(GrProcessorTestData* d) { | 
|---|
| 329 | auto [view, ct, at] = d->randomView(); | 
|---|
| 330 |  | 
|---|
| 331 | int width = d->fRandom->nextRangeU(1, MAX_KERNEL_SIZE); | 
|---|
| 332 | int height = d->fRandom->nextRangeU(1, MAX_KERNEL_SIZE / width); | 
|---|
| 333 | SkISize kernelSize = SkISize::Make(width, height); | 
|---|
| 334 | std::unique_ptr<SkScalar[]> kernel(new SkScalar[width * height]); | 
|---|
| 335 | for (int i = 0; i < width * height; i++) { | 
|---|
| 336 | kernel.get()[i] = d->fRandom->nextSScalar1(); | 
|---|
| 337 | } | 
|---|
| 338 | SkScalar gain = d->fRandom->nextSScalar1(); | 
|---|
| 339 | SkScalar bias = d->fRandom->nextSScalar1(); | 
|---|
| 340 |  | 
|---|
| 341 | uint32_t kernalOffsetX = d->fRandom->nextRangeU(0, kernelSize.width()); | 
|---|
| 342 | uint32_t kernalOffsetY = d->fRandom->nextRangeU(0, kernelSize.height()); | 
|---|
| 343 | SkIPoint kernelOffset = SkIPoint::Make(kernalOffsetX, kernalOffsetY); | 
|---|
| 344 |  | 
|---|
| 345 | uint32_t boundsX = d->fRandom->nextRangeU(0, view.width()); | 
|---|
| 346 | uint32_t boundsY = d->fRandom->nextRangeU(0, view.height()); | 
|---|
| 347 | uint32_t boundsW = d->fRandom->nextRangeU(0, view.width()); | 
|---|
| 348 | uint32_t boundsH = d->fRandom->nextRangeU(0, view.height()); | 
|---|
| 349 | SkIRect bounds = SkIRect::MakeXYWH(boundsX, boundsY, boundsW, boundsH); | 
|---|
| 350 |  | 
|---|
| 351 | GrTextureDomain::Mode tileMode = | 
|---|
| 352 | static_cast<GrTextureDomain::Mode>(d->fRandom->nextRangeU(0, 2)); | 
|---|
| 353 | bool convolveAlpha = d->fRandom->nextBool(); | 
|---|
| 354 |  | 
|---|
| 355 | return GrMatrixConvolutionEffect::Make(std::move(view), | 
|---|
| 356 | bounds, | 
|---|
| 357 | kernelSize, | 
|---|
| 358 | kernel.get(), | 
|---|
| 359 | gain, | 
|---|
| 360 | bias, | 
|---|
| 361 | kernelOffset, | 
|---|
| 362 | tileMode, | 
|---|
| 363 | convolveAlpha); | 
|---|
| 364 | } | 
|---|
| 365 | #endif | 
|---|
| 366 |  | 
|---|