joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 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 "GrMatrixConvolutionEffect.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 8 | #include "gl/GrGLProcessor.h" |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 9 | #include "gl/GrGLSL.h" |
| 10 | #include "gl/GrGLTexture.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 11 | #include "gl/builders/GrGLProgramBuilder.h" |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 12 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 13 | class GrGLMatrixConvolutionEffect : public GrGLFragmentProcessor { |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 14 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 15 | GrGLMatrixConvolutionEffect(const GrProcessor&); |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 16 | virtual void emitCode(GrGLFPBuilder*, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 17 | const GrFragmentProcessor&, |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 18 | const char* outputColor, |
| 19 | const char* inputColor, |
| 20 | const TransformedCoordsArray&, |
| 21 | const TextureSamplerArray&) SK_OVERRIDE; |
| 22 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 23 | static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 24 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 25 | virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE; |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 26 | |
| 27 | private: |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 28 | typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 29 | SkISize fKernelSize; |
| 30 | bool fConvolveAlpha; |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 31 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 32 | UniformHandle fBoundsUni; |
| 33 | UniformHandle fKernelUni; |
| 34 | UniformHandle fImageIncrementUni; |
| 35 | UniformHandle fKernelOffsetUni; |
| 36 | UniformHandle fGainUni; |
| 37 | UniformHandle fBiasUni; |
| 38 | GrTextureDomain::GLDomain fDomain; |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 39 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 40 | typedef GrGLFragmentProcessor INHERITED; |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 43 | GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrProcessor& processor) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 44 | const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffect>(); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 45 | fKernelSize = m.kernelSize(); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 46 | fConvolveAlpha = m.convolveAlpha(); |
| 47 | } |
| 48 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 49 | void GrGLMatrixConvolutionEffect::emitCode(GrGLFPBuilder* builder, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 50 | const GrFragmentProcessor& fp, |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 51 | const char* outputColor, |
| 52 | const char* inputColor, |
| 53 | const TransformedCoordsArray& coords, |
| 54 | const TextureSamplerArray& samplers) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 55 | const GrTextureDomain& domain = fp.cast<GrMatrixConvolutionEffect>().domain(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 56 | |
| 57 | fBoundsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame^] | 58 | kVec4f_GrSLType, kDefault_GrSLPrecision, "Bounds"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 59 | fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame^] | 60 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 61 | "ImageIncrement"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 62 | fKernelUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame^] | 63 | kFloat_GrSLType, kDefault_GrSLPrecision, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 64 | "Kernel", |
| 65 | fKernelSize.width() * fKernelSize.height()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 66 | fKernelOffsetUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame^] | 67 | kVec2f_GrSLType, kDefault_GrSLPrecision, "KernelOffset"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 68 | fGainUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame^] | 69 | kFloat_GrSLType, kDefault_GrSLPrecision, "Gain"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 70 | fBiasUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame^] | 71 | kFloat_GrSLType, kDefault_GrSLPrecision, "Bias"); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 72 | |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 73 | const char* kernelOffset = builder->getUniformCStr(fKernelOffsetUni); |
| 74 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 75 | const char* kernel = builder->getUniformCStr(fKernelUni); |
| 76 | const char* gain = builder->getUniformCStr(fGainUni); |
| 77 | const char* bias = builder->getUniformCStr(fBiasUni); |
| 78 | int kWidth = fKernelSize.width(); |
| 79 | int kHeight = fKernelSize.height(); |
| 80 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 81 | GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 82 | SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); |
| 83 | fsBuilder->codeAppend("vec4 sum = vec4(0, 0, 0, 0);"); |
| 84 | fsBuilder->codeAppendf("vec2 coord = %s - %s * %s;", coords2D.c_str(), kernelOffset, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 85 | imgInc); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 86 | fsBuilder->codeAppend("vec4 c;"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 87 | |
| 88 | for (int y = 0; y < kHeight; y++) { |
| 89 | for (int x = 0; x < kWidth; x++) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 90 | GrGLShaderBuilder::ShaderBlock block(fsBuilder); |
| 91 | fsBuilder->codeAppendf("float k = %s[%d * %d + %d];", kernel, y, kWidth, x); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 92 | SkString coord; |
| 93 | coord.printf("coord + vec2(%d, %d) * %s", x, y, imgInc); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 94 | fDomain.sampleTexture(fsBuilder, domain, "c", coord, samplers[0]); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 95 | if (!fConvolveAlpha) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 96 | fsBuilder->codeAppend("c.rgb /= c.a;"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 97 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 98 | fsBuilder->codeAppend("sum += c * k;"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 99 | } |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 100 | } |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 101 | if (fConvolveAlpha) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 102 | fsBuilder->codeAppendf("%s = sum * %s + %s;", outputColor, gain, bias); |
| 103 | fsBuilder->codeAppendf("%s.rgb = clamp(%s.rgb, 0.0, %s.a);", |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 104 | outputColor, outputColor, outputColor); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 105 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 106 | fDomain.sampleTexture(fsBuilder, domain, "c", coords2D, samplers[0]); |
| 107 | fsBuilder->codeAppendf("%s.a = c.a;", outputColor); |
| 108 | fsBuilder->codeAppendf("%s.rgb = sum.rgb * %s + %s;", outputColor, gain, bias); |
| 109 | fsBuilder->codeAppendf("%s.rgb *= %s.a;", outputColor, outputColor); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 110 | } |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 111 | |
| 112 | SkString modulate; |
egdaniel | 089f8de | 2014-10-09 10:34:58 -0700 | [diff] [blame] | 113 | GrGLSLMulVarBy4f(&modulate, outputColor, inputColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 114 | fsBuilder->codeAppend(modulate.c_str()); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 115 | } |
| 116 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 117 | void GrGLMatrixConvolutionEffect::GenKey(const GrProcessor& processor, |
| 118 | const GrGLCaps&, GrProcessorKeyBuilder* b) { |
| 119 | const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffect>(); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 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() ? 1 << 31 : 0; |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 123 | b->add32(key); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 124 | b->add32(GrTextureDomain::GLDomain::DomainKey(m.domain())); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 125 | } |
| 126 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 127 | void GrGLMatrixConvolutionEffect::setData(const GrGLProgramDataManager& pdman, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 128 | const GrProcessor& processor) { |
| 129 | const GrMatrixConvolutionEffect& conv = processor.cast<GrMatrixConvolutionEffect>(); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 130 | GrTexture& texture = *conv.texture(0); |
| 131 | // the code we generated was for a specific kernel size |
| 132 | SkASSERT(conv.kernelSize() == fKernelSize); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 133 | float imageIncrement[2]; |
| 134 | float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
| 135 | imageIncrement[0] = 1.0f / texture.width(); |
| 136 | imageIncrement[1] = ySign / texture.height(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 137 | pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
| 138 | pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); |
| 139 | pdman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.kernel()); |
| 140 | pdman.set1f(fGainUni, conv.gain()); |
| 141 | pdman.set1f(fBiasUni, conv.bias()); |
| 142 | fDomain.setData(pdman, conv.domain(), texture.origin()); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture, |
| 146 | const SkIRect& bounds, |
| 147 | const SkISize& kernelSize, |
| 148 | const SkScalar* kernel, |
| 149 | SkScalar gain, |
| 150 | SkScalar bias, |
| 151 | const SkIPoint& kernelOffset, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 152 | GrTextureDomain::Mode tileMode, |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 153 | bool convolveAlpha) |
bsalomon | 6267f81 | 2014-08-29 15:05:53 -0700 | [diff] [blame] | 154 | : INHERITED(texture, GrCoordTransform::MakeDivByTextureWHMatrix(texture)), |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 155 | fKernelSize(kernelSize), |
| 156 | fGain(SkScalarToFloat(gain)), |
| 157 | fBias(SkScalarToFloat(bias) / 255.0f), |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 158 | fConvolveAlpha(convolveAlpha), |
| 159 | fDomain(GrTextureDomain::MakeTexelDomain(texture, bounds), tileMode) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 160 | this->initClassID<GrMatrixConvolutionEffect>(); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 161 | for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) { |
| 162 | fKernel[i] = SkScalarToFloat(kernel[i]); |
| 163 | } |
| 164 | fKernelOffset[0] = static_cast<float>(kernelOffset.x()); |
| 165 | fKernelOffset[1] = static_cast<float>(kernelOffset.y()); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() { |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 169 | } |
| 170 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 171 | void GrMatrixConvolutionEffect::getGLProcessorKey(const GrGLCaps& caps, |
| 172 | GrProcessorKeyBuilder* b) const { |
| 173 | GrGLMatrixConvolutionEffect::GenKey(*this, caps, b); |
| 174 | } |
| 175 | |
| 176 | GrGLFragmentProcessor* GrMatrixConvolutionEffect::createGLInstance() const { |
| 177 | return SkNEW_ARGS(GrGLMatrixConvolutionEffect, (*this)); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 178 | } |
| 179 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 180 | bool GrMatrixConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 181 | const GrMatrixConvolutionEffect& s = sBase.cast<GrMatrixConvolutionEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 182 | return fKernelSize == s.kernelSize() && |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 183 | !memcmp(fKernel, s.kernel(), |
| 184 | fKernelSize.width() * fKernelSize.height() * sizeof(float)) && |
| 185 | fGain == s.gain() && |
| 186 | fBias == s.bias() && |
| 187 | fKernelOffset == s.kernelOffset() && |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 188 | fConvolveAlpha == s.convolveAlpha() && |
| 189 | fDomain == s.domain(); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 190 | } |
| 191 | |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 192 | // Static function to create a 2D convolution |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 193 | GrFragmentProcessor* |
| 194 | GrMatrixConvolutionEffect::CreateGaussian(GrTexture* texture, |
| 195 | const SkIRect& bounds, |
| 196 | const SkISize& kernelSize, |
| 197 | SkScalar gain, |
| 198 | SkScalar bias, |
| 199 | const SkIPoint& kernelOffset, |
| 200 | GrTextureDomain::Mode tileMode, |
| 201 | bool convolveAlpha, |
| 202 | SkScalar sigmaX, |
| 203 | SkScalar sigmaY) { |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 204 | float kernel[MAX_KERNEL_SIZE]; |
| 205 | int width = kernelSize.width(); |
| 206 | int height = kernelSize.height(); |
| 207 | SkASSERT(width * height <= MAX_KERNEL_SIZE); |
| 208 | float sum = 0.0f; |
| 209 | float sigmaXDenom = 1.0f / (2.0f * SkScalarToFloat(SkScalarSquare(sigmaX))); |
| 210 | float sigmaYDenom = 1.0f / (2.0f * SkScalarToFloat(SkScalarSquare(sigmaY))); |
| 211 | int xRadius = width / 2; |
| 212 | int yRadius = height / 2; |
| 213 | for (int x = 0; x < width; x++) { |
| 214 | float xTerm = static_cast<float>(x - xRadius); |
| 215 | xTerm = xTerm * xTerm * sigmaXDenom; |
| 216 | for (int y = 0; y < height; y++) { |
| 217 | float yTerm = static_cast<float>(y - yRadius); |
| 218 | float xyTerm = sk_float_exp(-(xTerm + yTerm * yTerm * sigmaYDenom)); |
| 219 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
| 220 | // is dropped here, since we renormalize the kernel below. |
| 221 | kernel[y * width + x] = xyTerm; |
| 222 | sum += xyTerm; |
| 223 | } |
| 224 | } |
| 225 | // Normalize the kernel |
| 226 | float scale = 1.0f / sum; |
| 227 | for (int i = 0; i < width * height; ++i) { |
| 228 | kernel[i] *= scale; |
| 229 | } |
| 230 | return SkNEW_ARGS(GrMatrixConvolutionEffect, (texture, |
| 231 | bounds, |
| 232 | kernelSize, |
| 233 | kernel, |
| 234 | gain, |
| 235 | bias, |
| 236 | kernelOffset, |
| 237 | tileMode, |
| 238 | convolveAlpha)); |
| 239 | } |
| 240 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 241 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMatrixConvolutionEffect); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 242 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 243 | GrFragmentProcessor* GrMatrixConvolutionEffect::TestCreate(SkRandom* random, |
| 244 | GrContext* context, |
| 245 | const GrDrawTargetCaps&, |
| 246 | GrTexture* textures[]) { |
| 247 | int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 248 | GrProcessorUnitTest::kAlphaTextureIdx; |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 249 | int width = random->nextRangeU(1, MAX_KERNEL_SIZE); |
| 250 | int height = random->nextRangeU(1, MAX_KERNEL_SIZE / width); |
| 251 | SkISize kernelSize = SkISize::Make(width, height); |
| 252 | SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]); |
| 253 | for (int i = 0; i < width * height; i++) { |
| 254 | kernel.get()[i] = random->nextSScalar1(); |
| 255 | } |
| 256 | SkScalar gain = random->nextSScalar1(); |
| 257 | SkScalar bias = random->nextSScalar1(); |
| 258 | SkIPoint kernelOffset = SkIPoint::Make(random->nextRangeU(0, kernelSize.width()), |
| 259 | random->nextRangeU(0, kernelSize.height())); |
| 260 | SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->width()), |
| 261 | random->nextRangeU(0, textures[texIdx]->height()), |
| 262 | random->nextRangeU(0, textures[texIdx]->width()), |
| 263 | random->nextRangeU(0, textures[texIdx]->height())); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 264 | GrTextureDomain::Mode tileMode = static_cast<GrTextureDomain::Mode>(random->nextRangeU(0, 2)); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 265 | bool convolveAlpha = random->nextBool(); |
| 266 | return GrMatrixConvolutionEffect::Create(textures[texIdx], |
| 267 | bounds, |
| 268 | kernelSize, |
| 269 | kernel.get(), |
| 270 | gain, |
| 271 | bias, |
| 272 | kernelOffset, |
| 273 | tileMode, |
| 274 | convolveAlpha); |
| 275 | } |