| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 #include "gl/builders/GrGLProgramBuilder.h" | 7 #include "gl/builders/GrGLProgramBuilder.h" |
| 8 #include "GrMatrixConvolutionEffect.h" | 8 #include "GrMatrixConvolutionEffect.h" |
| 9 #include "gl/GrGLEffect.h" | 9 #include "gl/GrGLProcessor.h" |
| 10 #include "gl/GrGLSL.h" | 10 #include "gl/GrGLSL.h" |
| 11 #include "gl/GrGLTexture.h" | 11 #include "gl/GrGLTexture.h" |
| 12 #include "GrTBackendEffectFactory.h" | 12 #include "GrTBackendProcessorFactory.h" |
| 13 | 13 |
| 14 class GrGLMatrixConvolutionEffect : public GrGLEffect { | 14 class GrGLMatrixConvolutionEffect : public GrGLFragmentProcessor { |
| 15 public: | 15 public: |
| 16 GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, | 16 GrGLMatrixConvolutionEffect(const GrBackendProcessorFactory& factory, |
| 17 const GrEffect& effect); | 17 const GrProcessor&); |
| 18 virtual void emitCode(GrGLProgramBuilder*, | 18 virtual void emitCode(GrGLProgramBuilder*, |
| 19 const GrEffect&, | 19 const GrFragmentProcessor&, |
| 20 const GrEffectKey&, | 20 const GrProcessorKey&, |
| 21 const char* outputColor, | 21 const char* outputColor, |
| 22 const char* inputColor, | 22 const char* inputColor, |
| 23 const TransformedCoordsArray&, | 23 const TransformedCoordsArray&, |
| 24 const TextureSamplerArray&) SK_OVERRIDE; | 24 const TextureSamplerArray&) SK_OVERRIDE; |
| 25 | 25 |
| 26 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuild
er*); | 26 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKe
yBuilder*); |
| 27 | 27 |
| 28 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVER
RIDE; | 28 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_O
VERRIDE; |
| 29 | 29 |
| 30 private: | 30 private: |
| 31 typedef GrGLProgramDataManager::UniformHandle UniformHandle; | 31 typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
| 32 SkISize fKernelSize; | 32 SkISize fKernelSize; |
| 33 bool fConvolveAlpha; | 33 bool fConvolveAlpha; |
| 34 | 34 |
| 35 UniformHandle fBoundsUni; | 35 UniformHandle fBoundsUni; |
| 36 UniformHandle fKernelUni; | 36 UniformHandle fKernelUni; |
| 37 UniformHandle fImageIncrementUni; | 37 UniformHandle fImageIncrementUni; |
| 38 UniformHandle fKernelOffsetUni; | 38 UniformHandle fKernelOffsetUni; |
| 39 UniformHandle fGainUni; | 39 UniformHandle fGainUni; |
| 40 UniformHandle fBiasUni; | 40 UniformHandle fBiasUni; |
| 41 GrTextureDomain::GLDomain fDomain; | 41 GrTextureDomain::GLDomain fDomain; |
| 42 | 42 |
| 43 typedef GrGLEffect INHERITED; | 43 typedef GrGLFragmentProcessor INHERITED; |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFa
ctory& factory, | 46 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendProcesso
rFactory& factory, |
| 47 const GrEffect& effect) | 47 const GrProcessor& proc
essor) |
| 48 : INHERITED(factory) { | 48 : INHERITED(factory) { |
| 49 const GrMatrixConvolutionEffect& m = effect.cast<GrMatrixConvolutionEffect>(
); | 49 const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffec
t>(); |
| 50 fKernelSize = m.kernelSize(); | 50 fKernelSize = m.kernelSize(); |
| 51 fConvolveAlpha = m.convolveAlpha(); | 51 fConvolveAlpha = m.convolveAlpha(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void GrGLMatrixConvolutionEffect::emitCode(GrGLProgramBuilder* builder, | 54 void GrGLMatrixConvolutionEffect::emitCode(GrGLProgramBuilder* builder, |
| 55 const GrEffect& effect, | 55 const GrFragmentProcessor& fp, |
| 56 const GrEffectKey& key, | 56 const GrProcessorKey& key, |
| 57 const char* outputColor, | 57 const char* outputColor, |
| 58 const char* inputColor, | 58 const char* inputColor, |
| 59 const TransformedCoordsArray& coords, | 59 const TransformedCoordsArray& coords, |
| 60 const TextureSamplerArray& samplers)
{ | 60 const TextureSamplerArray& samplers)
{ |
| 61 sk_ignore_unused_variable(inputColor); | 61 sk_ignore_unused_variable(inputColor); |
| 62 const GrTextureDomain& domain = effect.cast<GrMatrixConvolutionEffect>().dom
ain(); | 62 const GrTextureDomain& domain = fp.cast<GrMatrixConvolutionEffect>().domain(
); |
| 63 | 63 |
| 64 fBoundsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, | 64 fBoundsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 65 kVec4f_GrSLType, "Bounds"); | 65 kVec4f_GrSLType, "Bounds"); |
| 66 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, | 66 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, |
| 67 kVec2f_GrSLType, "ImageIncrement"); | 67 kVec2f_GrSLType, "ImageIncrement"); |
| 68 fKernelUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibili
ty, | 68 fKernelUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibili
ty, |
| 69 kFloat_GrSLType, | 69 kFloat_GrSLType, |
| 70 "Kernel", | 70 "Kernel", |
| 71 fKernelSize.width() * fKernelSize.heig
ht()); | 71 fKernelSize.width() * fKernelSize.heig
ht()); |
| 72 fKernelOffsetUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibil
ity, | 72 fKernelOffsetUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibil
ity, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 fsBuilder->codeAppendf("%s.a = c.a;", outputColor); | 113 fsBuilder->codeAppendf("%s.a = c.a;", outputColor); |
| 114 fsBuilder->codeAppendf("%s.rgb = sum.rgb * %s + %s;", outputColor, gain,
bias); | 114 fsBuilder->codeAppendf("%s.rgb = sum.rgb * %s + %s;", outputColor, gain,
bias); |
| 115 fsBuilder->codeAppendf("%s.rgb *= %s.a;", outputColor, outputColor); | 115 fsBuilder->codeAppendf("%s.rgb *= %s.a;", outputColor, outputColor); |
| 116 } | 116 } |
| 117 | 117 |
| 118 SkString modulate; | 118 SkString modulate; |
| 119 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); | 119 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
| 120 fsBuilder->codeAppend(modulate.c_str()); | 120 fsBuilder->codeAppend(modulate.c_str()); |
| 121 } | 121 } |
| 122 | 122 |
| 123 void GrGLMatrixConvolutionEffect::GenKey(const GrEffect& effect, | 123 void GrGLMatrixConvolutionEffect::GenKey(const GrProcessor& processor, |
| 124 const GrGLCaps&, GrEffectKeyBuilder* b)
{ | 124 const GrGLCaps&, GrProcessorKeyBuilder*
b) { |
| 125 const GrMatrixConvolutionEffect& m = effect.cast<GrMatrixConvolutionEffect>(
); | 125 const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffec
t>(); |
| 126 SkASSERT(m.kernelSize().width() <= 0x7FFF && m.kernelSize().height() <= 0xFF
FF); | 126 SkASSERT(m.kernelSize().width() <= 0x7FFF && m.kernelSize().height() <= 0xFF
FF); |
| 127 uint32_t key = m.kernelSize().width() << 16 | m.kernelSize().height(); | 127 uint32_t key = m.kernelSize().width() << 16 | m.kernelSize().height(); |
| 128 key |= m.convolveAlpha() ? 1 << 31 : 0; | 128 key |= m.convolveAlpha() ? 1 << 31 : 0; |
| 129 b->add32(key); | 129 b->add32(key); |
| 130 b->add32(GrTextureDomain::GLDomain::DomainKey(m.domain())); | 130 b->add32(GrTextureDomain::GLDomain::DomainKey(m.domain())); |
| 131 } | 131 } |
| 132 | 132 |
| 133 void GrGLMatrixConvolutionEffect::setData(const GrGLProgramDataManager& pdman, | 133 void GrGLMatrixConvolutionEffect::setData(const GrGLProgramDataManager& pdman, |
| 134 const GrEffect& effect) { | 134 const GrProcessor& processor) { |
| 135 const GrMatrixConvolutionEffect& conv = effect.cast<GrMatrixConvolutionEffec
t>(); | 135 const GrMatrixConvolutionEffect& conv = processor.cast<GrMatrixConvolutionEf
fect>(); |
| 136 GrTexture& texture = *conv.texture(0); | 136 GrTexture& texture = *conv.texture(0); |
| 137 // the code we generated was for a specific kernel size | 137 // the code we generated was for a specific kernel size |
| 138 SkASSERT(conv.kernelSize() == fKernelSize); | 138 SkASSERT(conv.kernelSize() == fKernelSize); |
| 139 float imageIncrement[2]; | 139 float imageIncrement[2]; |
| 140 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; | 140 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
| 141 imageIncrement[0] = 1.0f / texture.width(); | 141 imageIncrement[0] = 1.0f / texture.width(); |
| 142 imageIncrement[1] = ySign / texture.height(); | 142 imageIncrement[1] = ySign / texture.height(); |
| 143 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); | 143 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
| 144 pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); | 144 pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); |
| 145 pdman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.ke
rnel()); | 145 pdman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.ke
rnel()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 166 for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) { | 166 for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) { |
| 167 fKernel[i] = SkScalarToFloat(kernel[i]); | 167 fKernel[i] = SkScalarToFloat(kernel[i]); |
| 168 } | 168 } |
| 169 fKernelOffset[0] = static_cast<float>(kernelOffset.x()); | 169 fKernelOffset[0] = static_cast<float>(kernelOffset.x()); |
| 170 fKernelOffset[1] = static_cast<float>(kernelOffset.y()); | 170 fKernelOffset[1] = static_cast<float>(kernelOffset.y()); |
| 171 } | 171 } |
| 172 | 172 |
| 173 GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() { | 173 GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() { |
| 174 } | 174 } |
| 175 | 175 |
| 176 const GrBackendEffectFactory& GrMatrixConvolutionEffect::getFactory() const { | 176 const GrBackendFragmentProcessorFactory& GrMatrixConvolutionEffect::getFactory()
const { |
| 177 return GrTBackendEffectFactory<GrMatrixConvolutionEffect>::getInstance(); | 177 return GrTBackendFragmentProcessorFactory<GrMatrixConvolutionEffect>::getIns
tance(); |
| 178 } | 178 } |
| 179 | 179 |
| 180 bool GrMatrixConvolutionEffect::onIsEqual(const GrEffect& sBase) const { | 180 bool GrMatrixConvolutionEffect::onIsEqual(const GrProcessor& sBase) const { |
| 181 const GrMatrixConvolutionEffect& s = sBase.cast<GrMatrixConvolutionEffect>()
; | 181 const GrMatrixConvolutionEffect& s = sBase.cast<GrMatrixConvolutionEffect>()
; |
| 182 return this->texture(0) == s.texture(0) && | 182 return this->texture(0) == s.texture(0) && |
| 183 fKernelSize == s.kernelSize() && | 183 fKernelSize == s.kernelSize() && |
| 184 !memcmp(fKernel, s.kernel(), | 184 !memcmp(fKernel, s.kernel(), |
| 185 fKernelSize.width() * fKernelSize.height() * sizeof(float)) &
& | 185 fKernelSize.width() * fKernelSize.height() * sizeof(float)) &
& |
| 186 fGain == s.gain() && | 186 fGain == s.gain() && |
| 187 fBias == s.bias() && | 187 fBias == s.bias() && |
| 188 fKernelOffset == s.kernelOffset() && | 188 fKernelOffset == s.kernelOffset() && |
| 189 fConvolveAlpha == s.convolveAlpha() && | 189 fConvolveAlpha == s.convolveAlpha() && |
| 190 fDomain == s.domain(); | 190 fDomain == s.domain(); |
| 191 } | 191 } |
| 192 | 192 |
| 193 // Static function to create a 2D convolution | 193 // Static function to create a 2D convolution |
| 194 GrEffect* GrMatrixConvolutionEffect::CreateGaussian(GrTexture* texture, | 194 GrFragmentProcessor* |
| 195 const SkIRect& bounds, | 195 GrMatrixConvolutionEffect::CreateGaussian(GrTexture* texture, |
| 196 const SkISize& kernelSize, | 196 const SkIRect& bounds, |
| 197 SkScalar gain, | 197 const SkISize& kernelSize, |
| 198 SkScalar bias, | 198 SkScalar gain, |
| 199 const SkIPoint& kernelOffset
, | 199 SkScalar bias, |
| 200 GrTextureDomain::Mode tileMo
de, | 200 const SkIPoint& kernelOffset, |
| 201 bool convolveAlpha, | 201 GrTextureDomain::Mode tileMode, |
| 202 SkScalar sigmaX, | 202 bool convolveAlpha, |
| 203 SkScalar sigmaY) { | 203 SkScalar sigmaX, |
| 204 SkScalar sigmaY) { |
| 204 float kernel[MAX_KERNEL_SIZE]; | 205 float kernel[MAX_KERNEL_SIZE]; |
| 205 int width = kernelSize.width(); | 206 int width = kernelSize.width(); |
| 206 int height = kernelSize.height(); | 207 int height = kernelSize.height(); |
| 207 SkASSERT(width * height <= MAX_KERNEL_SIZE); | 208 SkASSERT(width * height <= MAX_KERNEL_SIZE); |
| 208 float sum = 0.0f; | 209 float sum = 0.0f; |
| 209 float sigmaXDenom = 1.0f / (2.0f * SkScalarToFloat(SkScalarSquare(sigmaX))); | 210 float sigmaXDenom = 1.0f / (2.0f * SkScalarToFloat(SkScalarSquare(sigmaX))); |
| 210 float sigmaYDenom = 1.0f / (2.0f * SkScalarToFloat(SkScalarSquare(sigmaY))); | 211 float sigmaYDenom = 1.0f / (2.0f * SkScalarToFloat(SkScalarSquare(sigmaY))); |
| 211 int xRadius = width / 2; | 212 int xRadius = width / 2; |
| 212 int yRadius = height / 2; | 213 int yRadius = height / 2; |
| 213 for (int x = 0; x < width; x++) { | 214 for (int x = 0; x < width; x++) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 231 bounds, | 232 bounds, |
| 232 kernelSize, | 233 kernelSize, |
| 233 kernel, | 234 kernel, |
| 234 gain, | 235 gain, |
| 235 bias, | 236 bias, |
| 236 kernelOffset, | 237 kernelOffset, |
| 237 tileMode, | 238 tileMode, |
| 238 convolveAlpha)); | 239 convolveAlpha)); |
| 239 } | 240 } |
| 240 | 241 |
| 241 GR_DEFINE_EFFECT_TEST(GrMatrixConvolutionEffect); | 242 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMatrixConvolutionEffect); |
| 242 | 243 |
| 243 GrEffect* GrMatrixConvolutionEffect::TestCreate(SkRandom* random, | 244 GrFragmentProcessor* GrMatrixConvolutionEffect::TestCreate(SkRandom* random, |
| 244 GrContext* context, | 245 GrContext* context, |
| 245 const GrDrawTargetCaps&, | 246 const GrDrawTargetCap
s&, |
| 246 GrTexture* textures[]) { | 247 GrTexture* textures[]
) { |
| 247 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : | 248 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 248 GrEffectUnitTest::kAlphaTextureIdx; | 249 GrProcessorUnitTest::kAlphaTextureIdx; |
| 249 int width = random->nextRangeU(1, MAX_KERNEL_SIZE); | 250 int width = random->nextRangeU(1, MAX_KERNEL_SIZE); |
| 250 int height = random->nextRangeU(1, MAX_KERNEL_SIZE / width); | 251 int height = random->nextRangeU(1, MAX_KERNEL_SIZE / width); |
| 251 SkISize kernelSize = SkISize::Make(width, height); | 252 SkISize kernelSize = SkISize::Make(width, height); |
| 252 SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]); | 253 SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]); |
| 253 for (int i = 0; i < width * height; i++) { | 254 for (int i = 0; i < width * height; i++) { |
| 254 kernel.get()[i] = random->nextSScalar1(); | 255 kernel.get()[i] = random->nextSScalar1(); |
| 255 } | 256 } |
| 256 SkScalar gain = random->nextSScalar1(); | 257 SkScalar gain = random->nextSScalar1(); |
| 257 SkScalar bias = random->nextSScalar1(); | 258 SkScalar bias = random->nextSScalar1(); |
| 258 SkIPoint kernelOffset = SkIPoint::Make(random->nextRangeU(0, kernelSize.widt
h()), | 259 SkIPoint kernelOffset = SkIPoint::Make(random->nextRangeU(0, kernelSize.widt
h()), |
| 259 random->nextRangeU(0, kernelSize.heig
ht())); | 260 random->nextRangeU(0, kernelSize.heig
ht())); |
| 260 SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->w
idth()), | 261 SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->w
idth()), |
| 261 random->nextRangeU(0, textures[texIdx]->h
eight()), | 262 random->nextRangeU(0, textures[texIdx]->h
eight()), |
| 262 random->nextRangeU(0, textures[texIdx]->w
idth()), | 263 random->nextRangeU(0, textures[texIdx]->w
idth()), |
| 263 random->nextRangeU(0, textures[texIdx]->h
eight())); | 264 random->nextRangeU(0, textures[texIdx]->h
eight())); |
| 264 GrTextureDomain::Mode tileMode = static_cast<GrTextureDomain::Mode>(random->
nextRangeU(0, 2)); | 265 GrTextureDomain::Mode tileMode = static_cast<GrTextureDomain::Mode>(random->
nextRangeU(0, 2)); |
| 265 bool convolveAlpha = random->nextBool(); | 266 bool convolveAlpha = random->nextBool(); |
| 266 return GrMatrixConvolutionEffect::Create(textures[texIdx], | 267 return GrMatrixConvolutionEffect::Create(textures[texIdx], |
| 267 bounds, | 268 bounds, |
| 268 kernelSize, | 269 kernelSize, |
| 269 kernel.get(), | 270 kernel.get(), |
| 270 gain, | 271 gain, |
| 271 bias, | 272 bias, |
| 272 kernelOffset, | 273 kernelOffset, |
| 273 tileMode, | 274 tileMode, |
| 274 convolveAlpha); | 275 convolveAlpha); |
| 275 } | 276 } |
| OLD | NEW |