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 GrDrawEffect& effect); | 17 const GrEffect& effect); |
18 virtual void emitCode(GrGLProgramBuilder*, | 18 virtual void emitCode(GrGLProgramBuilder*, |
19 const GrDrawEffect&, | 19 const GrEffect&, |
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 GrDrawEffect&, const GrGLCaps&, GrEffectKeyB
uilder*); | 26 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuild
er*); |
27 | 27 |
28 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; | 28 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVER
RIDE; |
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 GrDrawEffect& dra
wEffect) | 47 const GrEffect& effect) |
48 : INHERITED(factory) { | 48 : INHERITED(factory) { |
49 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvoluti
onEffect>(); | 49 const GrMatrixConvolutionEffect& m = GrEffect::CastEffect<GrMatrixConvolutio
nEffect>(effect); |
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 GrDrawEffect& drawEffect, | 55 const GrEffect& effect, |
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 = drawEffect.castEffect<GrMatrixConvolutionEff
ect>().domain(); | 62 const GrTextureDomain& domain = |
| 63 GrEffect::CastEffect<GrMatrixConvolutionEffect>(effect).domain(); |
63 | 64 |
64 fBoundsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, | 65 fBoundsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
65 kVec4f_GrSLType, "Bounds"); | 66 kVec4f_GrSLType, "Bounds"); |
66 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, | 67 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, |
67 kVec2f_GrSLType, "ImageIncrement"); | 68 kVec2f_GrSLType, "ImageIncrement"); |
68 fKernelUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibili
ty, | 69 fKernelUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibili
ty, |
69 kFloat_GrSLType, | 70 kFloat_GrSLType, |
70 "Kernel", | 71 "Kernel", |
71 fKernelSize.width() * fKernelSize.heig
ht()); | 72 fKernelSize.width() * fKernelSize.heig
ht()); |
72 fKernelOffsetUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibil
ity, | 73 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); | 114 fsBuilder->codeAppendf("%s.a = c.a;", outputColor); |
114 fsBuilder->codeAppendf("%s.rgb = sum.rgb * %s + %s;", outputColor, gain,
bias); | 115 fsBuilder->codeAppendf("%s.rgb = sum.rgb * %s + %s;", outputColor, gain,
bias); |
115 fsBuilder->codeAppendf("%s.rgb *= %s.a;", outputColor, outputColor); | 116 fsBuilder->codeAppendf("%s.rgb *= %s.a;", outputColor, outputColor); |
116 } | 117 } |
117 | 118 |
118 SkString modulate; | 119 SkString modulate; |
119 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); | 120 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
120 fsBuilder->codeAppend(modulate.c_str()); | 121 fsBuilder->codeAppend(modulate.c_str()); |
121 } | 122 } |
122 | 123 |
123 void GrGLMatrixConvolutionEffect::GenKey(const GrDrawEffect& drawEffect, | 124 void GrGLMatrixConvolutionEffect::GenKey(const GrEffect& effect, |
124 const GrGLCaps&, GrEffectKeyBuilder* b)
{ | 125 const GrGLCaps&, GrEffectKeyBuilder* b)
{ |
125 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvoluti
onEffect>(); | 126 const GrMatrixConvolutionEffect& m = GrEffect::CastEffect<GrMatrixConvolutio
nEffect>(effect); |
126 SkASSERT(m.kernelSize().width() <= 0x7FFF && m.kernelSize().height() <= 0xFF
FF); | 127 SkASSERT(m.kernelSize().width() <= 0x7FFF && m.kernelSize().height() <= 0xFF
FF); |
127 uint32_t key = m.kernelSize().width() << 16 | m.kernelSize().height(); | 128 uint32_t key = m.kernelSize().width() << 16 | m.kernelSize().height(); |
128 key |= m.convolveAlpha() ? 1 << 31 : 0; | 129 key |= m.convolveAlpha() ? 1 << 31 : 0; |
129 b->add32(key); | 130 b->add32(key); |
130 b->add32(GrTextureDomain::GLDomain::DomainKey(m.domain())); | 131 b->add32(GrTextureDomain::GLDomain::DomainKey(m.domain())); |
131 } | 132 } |
132 | 133 |
133 void GrGLMatrixConvolutionEffect::setData(const GrGLProgramDataManager& pdman, | 134 void GrGLMatrixConvolutionEffect::setData(const GrGLProgramDataManager& pdman, |
134 const GrDrawEffect& drawEffect) { | 135 const GrEffect& effect) { |
135 const GrMatrixConvolutionEffect& conv = drawEffect.castEffect<GrMatrixConvol
utionEffect>(); | 136 const GrMatrixConvolutionEffect& conv = GrEffect::CastEffect<GrMatrixConvolu
tionEffect>(effect); |
136 GrTexture& texture = *conv.texture(0); | 137 GrTexture& texture = *conv.texture(0); |
137 // the code we generated was for a specific kernel size | 138 // the code we generated was for a specific kernel size |
138 SkASSERT(conv.kernelSize() == fKernelSize); | 139 SkASSERT(conv.kernelSize() == fKernelSize); |
139 float imageIncrement[2]; | 140 float imageIncrement[2]; |
140 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; | 141 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
141 imageIncrement[0] = 1.0f / texture.width(); | 142 imageIncrement[0] = 1.0f / texture.width(); |
142 imageIncrement[1] = ySign / texture.height(); | 143 imageIncrement[1] = ySign / texture.height(); |
143 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); | 144 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
144 pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); | 145 pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset()); |
145 pdman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.ke
rnel()); | 146 pdman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.ke
rnel()); |
(...skipping 25 matching lines...) Expand all Loading... |
171 } | 172 } |
172 | 173 |
173 GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() { | 174 GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() { |
174 } | 175 } |
175 | 176 |
176 const GrBackendEffectFactory& GrMatrixConvolutionEffect::getFactory() const { | 177 const GrBackendEffectFactory& GrMatrixConvolutionEffect::getFactory() const { |
177 return GrTBackendEffectFactory<GrMatrixConvolutionEffect>::getInstance(); | 178 return GrTBackendEffectFactory<GrMatrixConvolutionEffect>::getInstance(); |
178 } | 179 } |
179 | 180 |
180 bool GrMatrixConvolutionEffect::onIsEqual(const GrEffect& sBase) const { | 181 bool GrMatrixConvolutionEffect::onIsEqual(const GrEffect& sBase) const { |
181 const GrMatrixConvolutionEffect& s = CastEffect<GrMatrixConvolutionEffect>(s
Base); | 182 const GrMatrixConvolutionEffect& s = GrEffect::CastEffect<GrMatrixConvolutio
nEffect>(sBase); |
182 return this->texture(0) == s.texture(0) && | 183 return this->texture(0) == s.texture(0) && |
183 fKernelSize == s.kernelSize() && | 184 fKernelSize == s.kernelSize() && |
184 !memcmp(fKernel, s.kernel(), | 185 !memcmp(fKernel, s.kernel(), |
185 fKernelSize.width() * fKernelSize.height() * sizeof(float)) &
& | 186 fKernelSize.width() * fKernelSize.height() * sizeof(float)) &
& |
186 fGain == s.gain() && | 187 fGain == s.gain() && |
187 fBias == s.bias() && | 188 fBias == s.bias() && |
188 fKernelOffset == s.kernelOffset() && | 189 fKernelOffset == s.kernelOffset() && |
189 fConvolveAlpha == s.convolveAlpha() && | 190 fConvolveAlpha == s.convolveAlpha() && |
190 fDomain == s.domain(); | 191 fDomain == s.domain(); |
191 } | 192 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |