| 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/GrGLEffect.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 "GrTBackendEffectFactory.h" |
| 13 | 13 |
| 14 class GrGLMatrixConvolutionEffect : public GrGLEffect { | 14 class GrGLMatrixConvolutionEffect : public GrGLEffect { |
| 15 public: | 15 public: |
| 16 GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, | 16 GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory, |
| 17 const GrEffect& effect); | 17 const GrDrawEffect& effect); |
| 18 virtual void emitCode(GrGLProgramBuilder*, | 18 virtual void emitCode(GrGLProgramBuilder*, |
| 19 const GrEffect&, | 19 const GrDrawEffect&, |
| 20 const GrEffectKey&, | 20 const GrEffectKey&, |
| 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 GrDrawEffect&, const GrGLCaps&, GrEffectKeyB
uilder*); |
| 27 | 27 |
| 28 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVER
RIDE; | 28 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; |
| 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 GrGLEffect INHERITED; |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFa
ctory& factory, | 46 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFa
ctory& factory, |
| 47 const GrEffect& effect) | 47 const GrDrawEffect& dra
wEffect) |
| 48 : INHERITED(factory) { | 48 : INHERITED(factory) { |
| 49 const GrMatrixConvolutionEffect& m = effect.cast<GrMatrixConvolutionEffect>(
); | 49 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvoluti
onEffect>(); |
| 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 GrDrawEffect& drawEffect, |
| 56 const GrEffectKey& key, | 56 const GrEffectKey& 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 = drawEffect.castEffect<GrMatrixConvolutionEff
ect>().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 GrDrawEffect& drawEffect, |
| 124 const GrGLCaps&, GrEffectKeyBuilder* b)
{ | 124 const GrGLCaps&, GrEffectKeyBuilder* b)
{ |
| 125 const GrMatrixConvolutionEffect& m = effect.cast<GrMatrixConvolutionEffect>(
); | 125 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvoluti
onEffect>(); |
| 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 GrDrawEffect& drawEffect) { |
| 135 const GrMatrixConvolutionEffect& conv = effect.cast<GrMatrixConvolutionEffec
t>(); | 135 const GrMatrixConvolutionEffect& conv = drawEffect.castEffect<GrMatrixConvol
utionEffect>(); |
| 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 25 matching lines...) Expand all Loading... |
| 171 } | 171 } |
| 172 | 172 |
| 173 GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() { | 173 GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() { |
| 174 } | 174 } |
| 175 | 175 |
| 176 const GrBackendEffectFactory& GrMatrixConvolutionEffect::getFactory() const { | 176 const GrBackendEffectFactory& GrMatrixConvolutionEffect::getFactory() const { |
| 177 return GrTBackendEffectFactory<GrMatrixConvolutionEffect>::getInstance(); | 177 return GrTBackendEffectFactory<GrMatrixConvolutionEffect>::getInstance(); |
| 178 } | 178 } |
| 179 | 179 |
| 180 bool GrMatrixConvolutionEffect::onIsEqual(const GrEffect& sBase) const { | 180 bool GrMatrixConvolutionEffect::onIsEqual(const GrEffect& sBase) const { |
| 181 const GrMatrixConvolutionEffect& s = sBase.cast<GrMatrixConvolutionEffect>()
; | 181 const GrMatrixConvolutionEffect& s = CastEffect<GrMatrixConvolutionEffect>(s
Base); |
| 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 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 return GrMatrixConvolutionEffect::Create(textures[texIdx], | 266 return GrMatrixConvolutionEffect::Create(textures[texIdx], |
| 267 bounds, | 267 bounds, |
| 268 kernelSize, | 268 kernelSize, |
| 269 kernel.get(), | 269 kernel.get(), |
| 270 gain, | 270 gain, |
| 271 bias, | 271 bias, |
| 272 kernelOffset, | 272 kernelOffset, |
| 273 tileMode, | 273 tileMode, |
| 274 convolveAlpha); | 274 convolveAlpha); |
| 275 } | 275 } |
| OLD | NEW |