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 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 8 | #include "gl/builders/GrGLProgramBuilder.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 9 | #include "GrConvolutionEffect.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 10 | #include "gl/GrGLEffect.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 |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 16 | typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 18 | class GrGLConvolutionEffect : public GrGLEffect { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 19 | public: |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 20 | GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrEffect&); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 21 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 22 | virtual void emitCode(GrGLProgramBuilder*, |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 23 | const GrEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 24 | const GrEffectKey&, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 25 | const char* outputColor, |
| 26 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 27 | const TransformedCoordsArray&, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 28 | const TextureSamplerArray&) SK_OVERRIDE; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 29 | |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 30 | virtual void setData(const GrGLProgramDataManager& pdman, const GrEffect&) SK_OVERRIDE; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 31 | |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 32 | static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuilder*); |
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 | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 36 | bool useBounds() const { return fUseBounds; } |
| 37 | Gr1DKernelEffect::Direction direction() const { return fDirection; } |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 38 | |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 39 | int fRadius; |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 40 | bool fUseBounds; |
| 41 | Gr1DKernelEffect::Direction fDirection; |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 42 | UniformHandle fKernelUni; |
| 43 | UniformHandle fImageIncrementUni; |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 44 | UniformHandle fBoundsUni; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 45 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 46 | typedef GrGLEffect INHERITED; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 49 | GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& factory, |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 50 | const GrEffect& effect) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 51 | : INHERITED(factory) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 52 | const GrConvolutionEffect& c = effect.cast<GrConvolutionEffect>(); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 53 | fRadius = c.radius(); |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 54 | fUseBounds = c.useBounds(); |
| 55 | fDirection = c.direction(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 56 | } |
| 57 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 58 | void GrGLConvolutionEffect::emitCode(GrGLProgramBuilder* builder, |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 59 | const GrEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 60 | const GrEffectKey& key, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 61 | const char* outputColor, |
| 62 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 63 | const TransformedCoordsArray& coords, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 64 | const TextureSamplerArray& samplers) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 65 | fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 66 | kVec2f_GrSLType, "ImageIncrement"); |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 67 | if (this->useBounds()) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 68 | fBoundsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 69 | kVec2f_GrSLType, "Bounds"); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 70 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 71 | fKernelUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 72 | kFloat_GrSLType, "Kernel", this->width()); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 73 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 74 | GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 75 | SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); |
| 76 | |
| 77 | fsBuilder->codeAppendf("\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 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 83 | fsBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(), 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); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 91 | fsBuilder->codeAppendf("\t\t%s += ", outputColor); |
| 92 | fsBuilder->appendTextureLookup(samplers[0], "coord"); |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 93 | if (this->useBounds()) { |
| 94 | const char* bounds = builder->getUniformCStr(fBoundsUni); |
| 95 | const char* component = this->direction() == Gr1DKernelEffect::kY_Direction ? "y" : "x"; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 96 | fsBuilder->codeAppendf(" * float(coord.%s >= %s.x && coord.%s <= %s.y)", |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 97 | component, bounds, component, bounds); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 98 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 99 | fsBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str()); |
| 100 | fsBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc); |
tomhudson@google.com | f8a2289 | 2012-06-11 12:42:24 +0000 | [diff] [blame] | 101 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 102 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 103 | SkString modulate; |
| 104 | GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 105 | fsBuilder->codeAppend(modulate.c_str()); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 106 | } |
| 107 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 108 | void GrGLConvolutionEffect::setData(const GrGLProgramDataManager& pdman, |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 109 | const GrEffect& effect) { |
| 110 | const GrConvolutionEffect& conv = effect.cast<GrConvolutionEffect>(); |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 111 | GrTexture& texture = *conv.texture(0); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 112 | // the code we generated was for a specific kernel radius |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 113 | SkASSERT(conv.radius() == fRadius); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 114 | float imageIncrement[2] = { 0 }; |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 115 | float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 116 | switch (conv.direction()) { |
| 117 | case Gr1DKernelEffect::kX_Direction: |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 118 | imageIncrement[0] = 1.0f / texture.width(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 119 | break; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 120 | case Gr1DKernelEffect::kY_Direction: |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 121 | imageIncrement[1] = ySign / texture.height(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 122 | break; |
| 123 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 124 | SkFAIL("Unknown filter direction."); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 125 | } |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 126 | pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 127 | if (conv.useBounds()) { |
| 128 | const float* bounds = conv.bounds(); |
| 129 | if (Gr1DKernelEffect::kY_Direction == conv.direction() && |
| 130 | texture.origin() != kTopLeft_GrSurfaceOrigin) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 131 | pdman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]); |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 132 | } else { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 133 | pdman.set2f(fBoundsUni, bounds[0], bounds[1]); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 134 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 135 | } |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 136 | pdman.set1fv(fKernelUni, this->width(), conv.kernel()); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 137 | } |
| 138 | |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 139 | void GrGLConvolutionEffect::GenKey(const GrEffect& effect, const GrGLCaps&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 140 | GrEffectKeyBuilder* b) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 141 | const GrConvolutionEffect& conv = effect.cast<GrConvolutionEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 142 | uint32_t key = conv.radius(); |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 143 | key <<= 2; |
| 144 | if (conv.useBounds()) { |
| 145 | key |= 0x2; |
| 146 | key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0; |
| 147 | } |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 148 | b->add32(key); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 149 | } |
| 150 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 151 | /////////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 152 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 153 | GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, |
| 154 | Direction direction, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 155 | int radius, |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 156 | const float* kernel, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 157 | bool useBounds, |
| 158 | float bounds[2]) |
| 159 | : Gr1DKernelEffect(texture, direction, radius), fUseBounds(useBounds) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 160 | SkASSERT(radius <= kMaxKernelRadius); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 161 | SkASSERT(kernel); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 162 | int width = this->width(); |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 163 | for (int i = 0; i < width; i++) { |
| 164 | fKernel[i] = kernel[i]; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 165 | } |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 166 | memcpy(fBounds, bounds, sizeof(fBounds)); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 167 | } |
| 168 | |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 169 | GrConvolutionEffect::GrConvolutionEffect(GrTexture* texture, |
| 170 | Direction direction, |
| 171 | int radius, |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 172 | float gaussianSigma, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 173 | bool useBounds, |
| 174 | float bounds[2]) |
| 175 | : Gr1DKernelEffect(texture, direction, radius), fUseBounds(useBounds) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 176 | SkASSERT(radius <= kMaxKernelRadius); |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 177 | int width = this->width(); |
| 178 | |
| 179 | float sum = 0.0f; |
| 180 | float denom = 1.0f / (2.0f * gaussianSigma * gaussianSigma); |
| 181 | for (int i = 0; i < width; ++i) { |
| 182 | float x = static_cast<float>(i - this->radius()); |
| 183 | // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
| 184 | // is dropped here, since we renormalize the kernel below. |
| 185 | fKernel[i] = sk_float_exp(- x * x * denom); |
| 186 | sum += fKernel[i]; |
| 187 | } |
| 188 | // Normalize the kernel |
| 189 | float scale = 1.0f / sum; |
| 190 | for (int i = 0; i < width; ++i) { |
| 191 | fKernel[i] *= scale; |
| 192 | } |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 193 | memcpy(fBounds, bounds, sizeof(fBounds)); |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 194 | } |
| 195 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 196 | GrConvolutionEffect::~GrConvolutionEffect() { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 197 | } |
| 198 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 199 | const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const { |
| 200 | return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 201 | } |
| 202 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 203 | bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 204 | const GrConvolutionEffect& s = sBase.cast<GrConvolutionEffect>(); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 205 | return (this->texture(0) == s.texture(0) && |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 206 | this->radius() == s.radius() && |
| 207 | this->direction() == s.direction() && |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 208 | this->useBounds() == s.useBounds() && |
| 209 | 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) && |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 210 | 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 211 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 212 | |
| 213 | /////////////////////////////////////////////////////////////////////////////// |
| 214 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 215 | GR_DEFINE_EFFECT_TEST(GrConvolutionEffect); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 216 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 217 | GrEffect* GrConvolutionEffect::TestCreate(SkRandom* random, |
| 218 | GrContext*, |
| 219 | const GrDrawTargetCaps&, |
| 220 | GrTexture* textures[]) { |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 221 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 222 | GrEffectUnitTest::kAlphaTextureIdx; |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 223 | Direction dir = random->nextBool() ? kX_Direction : kY_Direction; |
| 224 | int radius = random->nextRangeU(1, kMaxKernelRadius); |
bungeman@google.com | 4348663 | 2013-08-20 15:20:34 +0000 | [diff] [blame] | 225 | float kernel[kMaxKernelWidth]; |
| 226 | for (size_t i = 0; i < SK_ARRAY_COUNT(kernel); ++i) { |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 227 | kernel[i] = random->nextSScalar1(); |
| 228 | } |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 229 | float bounds[2]; |
bungeman@google.com | 4348663 | 2013-08-20 15:20:34 +0000 | [diff] [blame] | 230 | for (size_t i = 0; i < SK_ARRAY_COUNT(bounds); ++i) { |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 231 | bounds[i] = random->nextF(); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 232 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 233 | |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 234 | bool useBounds = random->nextBool(); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 235 | return GrConvolutionEffect::Create(textures[texIdx], |
| 236 | dir, |
| 237 | radius, |
| 238 | kernel, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 239 | useBounds, |
| 240 | bounds); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 241 | } |