| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | #include "GrConvolutionEffect.h" |
| bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 9 | #include "gl/GrGLEffect.h" |
| bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 10 | #include "gl/GrGLEffectMatrix.h" |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 11 | #include "gl/GrGLSL.h" |
| 12 | #include "gl/GrGLTexture.h" |
| bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 13 | #include "GrTBackendEffectFactory.h" |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 14 | |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 15 | // For brevity |
| 16 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 17 | static const UniformHandle kInvalidUniformHandle = GrGLUniformManager::kInvalidUniformHandle; |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 18 | |
| bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 19 | class GrGLConvolutionEffect : public GrGLEffect { |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 20 | public: |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 21 | GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 22 | |
| bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 23 | virtual void emitCode(GrGLShaderBuilder*, |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 24 | const GrDrawEffect&, |
| bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 25 | EffectKey, |
| bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 26 | const char* outputColor, |
| 27 | const char* inputColor, |
| 28 | const TextureSamplerArray&) SK_OVERRIDE; |
| bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 29 | |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 30 | virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect&) SK_OVERRIDE; |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 31 | |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 32 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 33 | |
| 34 | private: |
| bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 35 | int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); } |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 36 | bool useCropRect() const { return fUseCropRect; } |
| bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 37 | |
| bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 38 | int fRadius; |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 39 | bool fUseCropRect; |
| bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 40 | UniformHandle fKernelUni; |
| 41 | UniformHandle fImageIncrementUni; |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 42 | UniformHandle fCropRectUni; |
| bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 43 | GrGLEffectMatrix fEffectMatrix; |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 44 | |
| bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 45 | typedef GrGLEffect INHERITED; |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 48 | GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& factory, |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 49 | const GrDrawEffect& drawEffect) |
| bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 50 | : INHERITED(factory) |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 51 | , fKernelUni(kInvalidUniformHandle) |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 52 | , fImageIncrementUni(kInvalidUniformHandle) |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 53 | , fCropRectUni(kInvalidUniformHandle) |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 54 | , fEffectMatrix(drawEffect.castEffect<GrConvolutionEffect>().coordsType()) { |
| 55 | const GrConvolutionEffect& c = drawEffect.castEffect<GrConvolutionEffect>(); |
| bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 56 | fRadius = c.radius(); |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 57 | fUseCropRect = c.useCropRect(); |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 60 | void GrGLConvolutionEffect::emitCode(GrGLShaderBuilder* builder, |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 61 | const GrDrawEffect&, |
| bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 62 | EffectKey key, |
| bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 63 | const char* outputColor, |
| 64 | const char* inputColor, |
| 65 | const TextureSamplerArray& samplers) { |
| bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 66 | const char* coords; |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 67 | fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); |
| bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 68 | fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 69 | kVec2f_GrSLType, "ImageIncrement"); |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 70 | if (this->useCropRect()) { |
| 71 | fCropRectUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 72 | kVec4f_GrSLType, "CropRect"); |
| 73 | } |
| bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 74 | fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType, |
| 75 | kFloat_GrSLType, "Kernel", this->width()); |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 76 | |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 77 | builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 78 | |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 79 | int width = this->width(); |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 80 | const GrGLShaderVar& kernel = builder->getUniformVariable(fKernelUni); |
| 81 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 82 | |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 83 | builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius, imgInc); |
| bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 84 | |
| tomhudson@google.com | f8a2289 | 2012-06-11 12:42:24 +0000 | [diff] [blame] | 85 | // Manually unroll loop because some drivers don't; yields 20-30% speedup. |
| bsalomon@google.com | fc0d23a | 2012-06-15 21:50:15 +0000 | [diff] [blame] | 86 | for (int i = 0; i < width; i++) { |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 87 | SkString index; |
| 88 | SkString kernelIndex; |
| tomhudson@google.com | f8a2289 | 2012-06-11 12:42:24 +0000 | [diff] [blame] | 89 | index.appendS32(i); |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 90 | kernel.appendArrayAccess(index.c_str(), &kernelIndex); |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 91 | builder->fsCodeAppendf("\t\t%s += ", outputColor); |
| 92 | builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, samplers[0], "coord"); |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 93 | if (this->useCropRect()) { |
| 94 | const char* cropRect = builder->getUniformCStr(fCropRectUni); |
| 95 | builder->fsCodeAppendf(" * float(coord.x >= %s.x && coord.x <= %s.y && coord.y >= %s.z && coord.y <= %s.w)", |
| 96 | cropRect, cropRect, cropRect, cropRect); |
| 97 | } |
| robertphillips@google.com | 58c856a | 2013-07-24 13:18:06 +0000 | [diff] [blame] | 98 | builder->fsCodeAppendf(" * %s;\n", kernelIndex.c_str()); |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 99 | builder->fsCodeAppendf("\t\tcoord += %s;\n", imgInc); |
| tomhudson@google.com | f8a2289 | 2012-06-11 12:42:24 +0000 | [diff] [blame] | 100 | } |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 101 | |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 102 | SkString modulate; |
| 103 | GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
| 104 | builder->fsCodeAppend(modulate.c_str()); |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 107 | void GrGLConvolutionEffect::setData(const GrGLUniformManager& uman, |
| 108 | const GrDrawEffect& drawEffect) { |
| 109 | const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>(); |
| bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 110 | GrTexture& texture = *conv.texture(0); |
| bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 111 | // the code we generated was for a specific kernel radius |
| 112 | GrAssert(conv.radius() == fRadius); |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 113 | float imageIncrement[2] = { 0 }; |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 114 | float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
| bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 115 | switch (conv.direction()) { |
| 116 | case Gr1DKernelEffect::kX_Direction: |
| tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 117 | imageIncrement[0] = 1.0f / texture.width(); |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 118 | break; |
| bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 119 | case Gr1DKernelEffect::kY_Direction: |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 120 | imageIncrement[1] = ySign / texture.height(); |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 121 | break; |
| 122 | default: |
| 123 | GrCrash("Unknown filter direction."); |
| 124 | } |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 125 | uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 126 | if (conv.useCropRect()) { |
| 127 | float c[4]; |
| 128 | memcpy(c, conv.cropRect(), sizeof(c)); |
| 129 | if (texture.origin() != kTopLeft_GrSurfaceOrigin) { |
| 130 | float tmp = 1.0f - c[2]; |
| 131 | c[2] = 1.0f - c[3]; |
| 132 | c[3] = tmp; |
| 133 | } |
| 134 | uman.set4fv(fCropRectUni, 0, 1, c); |
| 135 | } |
| bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 136 | uman.set1fv(fKernelUni, 0, this->width(), conv.kernel()); |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 137 | fEffectMatrix.setData(uman, conv.getMatrix(), drawEffect, conv.texture(0)); |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 140 | GrGLEffect::EffectKey GrGLConvolutionEffect::GenKey(const GrDrawEffect& drawEffect, |
| 141 | const GrGLCaps&) { |
| 142 | const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>(); |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 143 | EffectKey key = conv.radius() << 1; |
| 144 | key |= conv.useCropRect() ? 0x1 : 0x0; |
| bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 145 | key <<= GrGLEffectMatrix::kKeyBits; |
| 146 | EffectKey matrixKey = GrGLEffectMatrix::GenKey(conv.getMatrix(), |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 147 | drawEffect, |
| 148 | conv.coordsType(), |
| bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 149 | conv.texture(0)); |
| 150 | return key | matrixKey; |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 153 | /////////////////////////////////////////////////////////////////////////////// |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 154 | |
| tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 155 | GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, |
| 156 | Direction direction, |
| bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 157 | int radius, |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 158 | const float* kernel, |
| 159 | bool useCropRect, |
| 160 | float cropRect[4]) |
| 161 | : Gr1DKernelEffect(texture, direction, radius), fUseCropRect(useCropRect) { |
| bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 162 | GrAssert(radius <= kMaxKernelRadius); |
| bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 163 | GrAssert(NULL != kernel); |
| bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 164 | int width = this->width(); |
| bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 165 | for (int i = 0; i < width; i++) { |
| 166 | fKernel[i] = kernel[i]; |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 167 | } |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 168 | memcpy(fCropRect, cropRect, sizeof(fCropRect)); |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 171 | GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, |
| 172 | Direction direction, |
| 173 | int radius, |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 174 | float gaussianSigma, |
| 175 | bool useCropRect, |
| 176 | float cropRect[4]) |
| 177 | : Gr1DKernelEffect(texture, direction, radius), fUseCropRect(useCropRect) { |
| tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 178 | GrAssert(radius <= kMaxKernelRadius); |
| 179 | int width = this->width(); |
| 180 | |
| 181 | float sum = 0.0f; |
| 182 | float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma); |
| 183 | for (int i = 0; i < width; ++i) { |
| 184 | float x = static_cast<float>(i - this->radius()); |
| 185 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
| 186 | // is dropped here, since we renormalize the kernel below. |
| 187 | fKernel[i] = sk_float_exp(- x * x * denom); |
| 188 | sum += fKernel[i]; |
| 189 | } |
| 190 | // Normalize the kernel |
| 191 | float scale = 1.0f / sum; |
| 192 | for (int i = 0; i < width; ++i) { |
| 193 | fKernel[i] *= scale; |
| 194 | } |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 195 | memcpy(fCropRect, cropRect, sizeof(fCropRect)); |
| tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 198 | GrConvolutionEffect::~GrConvolutionEffect() { |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 201 | const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const { |
| 202 | return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance(); |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 205 | bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const { |
| bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 206 | const GrConvolutionEffect& s = CastEffect<GrConvolutionEffect>(sBase); |
| bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 207 | return (this->texture(0) == s.texture(0) && |
| tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 208 | this->radius() == s.radius() && |
| 209 | this->direction() == s.direction() && |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 210 | 0 == memcmp(fCropRect, s.fCropRect, sizeof(fCropRect)) && |
| tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 211 | 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); |
| tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 212 | } |
| bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 213 | |
| 214 | /////////////////////////////////////////////////////////////////////////////// |
| 215 | |
| bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 216 | GR_DEFINE_EFFECT_TEST(GrConvolutionEffect); |
| bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 217 | |
| bsalomon@google.com | 73a9694 | 2013-02-13 16:31:19 +0000 | [diff] [blame] | 218 | GrEffectRef* GrConvolutionEffect::TestCreate(SkMWCRandom* random, |
| sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 219 | GrContext*, |
| bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 220 | const GrDrawTargetCaps&, |
| bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 221 | GrTexture* textures[]) { |
| bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 222 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 223 | GrEffectUnitTest::kAlphaTextureIdx; |
| bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 224 | Direction dir = random->nextBool() ? kX_Direction : kY_Direction; |
| 225 | int radius = random->nextRangeU(1, kMaxKernelRadius); |
| 226 | float kernel[kMaxKernelRadius]; |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 227 | float cropRect[4]; |
| bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 228 | for (int i = 0; i < kMaxKernelRadius; ++i) { |
| 229 | kernel[i] = random->nextSScalar1(); |
| 230 | } |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 231 | for (int i = 0; i < 4; ++i) { |
| 232 | cropRect[i] = random->nextF(); |
| 233 | } |
| bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 234 | |
| senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 235 | bool useCropRect = random->nextBool(); |
| 236 | return GrConvolutionEffect::Create(textures[texIdx], |
| 237 | dir, |
| 238 | radius, |
| 239 | kernel, |
| 240 | useCropRect, |
| 241 | cropRect); |
| bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 242 | } |