OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 | 7 |
8 #include "gl/builders/GrGLProgramBuilder.h" | 8 #include "gl/builders/GrGLProgramBuilder.h" |
9 #include "GrConvolutionEffect.h" | 9 #include "GrConvolutionEffect.h" |
10 #include "gl/GrGLEffect.h" | 10 #include "gl/GrGLEffect.h" |
11 #include "gl/GrGLSL.h" | 11 #include "gl/GrGLSL.h" |
12 #include "gl/GrGLTexture.h" | 12 #include "gl/GrGLTexture.h" |
13 #include "GrTBackendEffectFactory.h" | 13 #include "GrTBackendEffectFactory.h" |
14 | 14 |
15 // For brevity | 15 // For brevity |
16 typedef GrGLProgramDataManager::UniformHandle UniformHandle; | 16 typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
17 | 17 |
18 class GrGLConvolutionEffect : public GrGLEffect { | 18 class GrGLConvolutionEffect : public GrGLEffect { |
19 public: | 19 public: |
20 GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | 20 GrGLConvolutionEffect(const GrBackendEffectFactory&, const GrEffect&); |
21 | 21 |
22 virtual void emitCode(GrGLProgramBuilder*, | 22 virtual void emitCode(GrGLProgramBuilder*, |
23 const GrDrawEffect&, | 23 const GrEffect&, |
24 const GrEffectKey&, | 24 const GrEffectKey&, |
25 const char* outputColor, | 25 const char* outputColor, |
26 const char* inputColor, | 26 const char* inputColor, |
27 const TransformedCoordsArray&, | 27 const TransformedCoordsArray&, |
28 const TextureSamplerArray&) SK_OVERRIDE; | 28 const TextureSamplerArray&) SK_OVERRIDE; |
29 | 29 |
30 virtual void setData(const GrGLProgramDataManager& pdman, const GrDrawEffect
&) SK_OVERRIDE; | 30 virtual void setData(const GrGLProgramDataManager& pdman, const GrEffect&) S
K_OVERRIDE; |
31 | 31 |
32 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyB
uilder*); | 32 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuild
er*); |
33 | 33 |
34 private: | 34 private: |
35 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); } | 35 int width() const { return Gr1DKernelEffect::WidthFromRadius(fRadius); } |
36 bool useBounds() const { return fUseBounds; } | 36 bool useBounds() const { return fUseBounds; } |
37 Gr1DKernelEffect::Direction direction() const { return fDirection; } | 37 Gr1DKernelEffect::Direction direction() const { return fDirection; } |
38 | 38 |
39 int fRadius; | 39 int fRadius; |
40 bool fUseBounds; | 40 bool fUseBounds; |
41 Gr1DKernelEffect::Direction fDirection; | 41 Gr1DKernelEffect::Direction fDirection; |
42 UniformHandle fKernelUni; | 42 UniformHandle fKernelUni; |
43 UniformHandle fImageIncrementUni; | 43 UniformHandle fImageIncrementUni; |
44 UniformHandle fBoundsUni; | 44 UniformHandle fBoundsUni; |
45 | 45 |
46 typedef GrGLEffect INHERITED; | 46 typedef GrGLEffect INHERITED; |
47 }; | 47 }; |
48 | 48 |
49 GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& facto
ry, | 49 GrGLConvolutionEffect::GrGLConvolutionEffect(const GrBackendEffectFactory& facto
ry, |
50 const GrDrawEffect& drawEffect) | 50 const GrEffect& effect) |
51 : INHERITED(factory) { | 51 : INHERITED(factory) { |
52 const GrConvolutionEffect& c = drawEffect.castEffect<GrConvolutionEffect>(); | 52 const GrConvolutionEffect& c = GrEffect::CastEffect<GrConvolutionEffect>(eff
ect); |
53 fRadius = c.radius(); | 53 fRadius = c.radius(); |
54 fUseBounds = c.useBounds(); | 54 fUseBounds = c.useBounds(); |
55 fDirection = c.direction(); | 55 fDirection = c.direction(); |
56 } | 56 } |
57 | 57 |
58 void GrGLConvolutionEffect::emitCode(GrGLProgramBuilder* builder, | 58 void GrGLConvolutionEffect::emitCode(GrGLProgramBuilder* builder, |
59 const GrDrawEffect&, | 59 const GrEffect&, |
60 const GrEffectKey& key, | 60 const GrEffectKey& key, |
61 const char* outputColor, | 61 const char* outputColor, |
62 const char* inputColor, | 62 const char* inputColor, |
63 const TransformedCoordsArray& coords, | 63 const TransformedCoordsArray& coords, |
64 const TextureSamplerArray& samplers) { | 64 const TextureSamplerArray& samplers) { |
65 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, | 65 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, |
66 kVec2f_GrSLType, "ImageIncrement"); | 66 kVec2f_GrSLType, "ImageIncrement"); |
67 if (this->useBounds()) { | 67 if (this->useBounds()) { |
68 fBoundsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibilit
y, | 68 fBoundsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibilit
y, |
69 kVec2f_GrSLType, "Bounds"); | 69 kVec2f_GrSLType, "Bounds"); |
(...skipping 29 matching lines...) Expand all Loading... |
99 fsBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str()); | 99 fsBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str()); |
100 fsBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc); | 100 fsBuilder->codeAppendf("\t\tcoord += %s;\n", imgInc); |
101 } | 101 } |
102 | 102 |
103 SkString modulate; | 103 SkString modulate; |
104 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); | 104 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
105 fsBuilder->codeAppend(modulate.c_str()); | 105 fsBuilder->codeAppend(modulate.c_str()); |
106 } | 106 } |
107 | 107 |
108 void GrGLConvolutionEffect::setData(const GrGLProgramDataManager& pdman, | 108 void GrGLConvolutionEffect::setData(const GrGLProgramDataManager& pdman, |
109 const GrDrawEffect& drawEffect) { | 109 const GrEffect& effect) { |
110 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>
(); | 110 const GrConvolutionEffect& conv = GrEffect::CastEffect<GrConvolutionEffect>(
effect); |
111 GrTexture& texture = *conv.texture(0); | 111 GrTexture& texture = *conv.texture(0); |
112 // the code we generated was for a specific kernel radius | 112 // the code we generated was for a specific kernel radius |
113 SkASSERT(conv.radius() == fRadius); | 113 SkASSERT(conv.radius() == fRadius); |
114 float imageIncrement[2] = { 0 }; | 114 float imageIncrement[2] = { 0 }; |
115 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; | 115 float ySign = texture.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f; |
116 switch (conv.direction()) { | 116 switch (conv.direction()) { |
117 case Gr1DKernelEffect::kX_Direction: | 117 case Gr1DKernelEffect::kX_Direction: |
118 imageIncrement[0] = 1.0f / texture.width(); | 118 imageIncrement[0] = 1.0f / texture.width(); |
119 break; | 119 break; |
120 case Gr1DKernelEffect::kY_Direction: | 120 case Gr1DKernelEffect::kY_Direction: |
121 imageIncrement[1] = ySign / texture.height(); | 121 imageIncrement[1] = ySign / texture.height(); |
122 break; | 122 break; |
123 default: | 123 default: |
124 SkFAIL("Unknown filter direction."); | 124 SkFAIL("Unknown filter direction."); |
125 } | 125 } |
126 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); | 126 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
127 if (conv.useBounds()) { | 127 if (conv.useBounds()) { |
128 const float* bounds = conv.bounds(); | 128 const float* bounds = conv.bounds(); |
129 if (Gr1DKernelEffect::kY_Direction == conv.direction() && | 129 if (Gr1DKernelEffect::kY_Direction == conv.direction() && |
130 texture.origin() != kTopLeft_GrSurfaceOrigin) { | 130 texture.origin() != kTopLeft_GrSurfaceOrigin) { |
131 pdman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]); | 131 pdman.set2f(fBoundsUni, 1.0f - bounds[1], 1.0f - bounds[0]); |
132 } else { | 132 } else { |
133 pdman.set2f(fBoundsUni, bounds[0], bounds[1]); | 133 pdman.set2f(fBoundsUni, bounds[0], bounds[1]); |
134 } | 134 } |
135 } | 135 } |
136 pdman.set1fv(fKernelUni, this->width(), conv.kernel()); | 136 pdman.set1fv(fKernelUni, this->width(), conv.kernel()); |
137 } | 137 } |
138 | 138 |
139 void GrGLConvolutionEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCap
s&, | 139 void GrGLConvolutionEffect::GenKey(const GrEffect& effect, const GrGLCaps&, |
140 GrEffectKeyBuilder* b) { | 140 GrEffectKeyBuilder* b) { |
141 const GrConvolutionEffect& conv = drawEffect.castEffect<GrConvolutionEffect>
(); | 141 const GrConvolutionEffect& conv = GrEffect::CastEffect<GrConvolutionEffect>(
effect); |
142 uint32_t key = conv.radius(); | 142 uint32_t key = conv.radius(); |
143 key <<= 2; | 143 key <<= 2; |
144 if (conv.useBounds()) { | 144 if (conv.useBounds()) { |
145 key |= 0x2; | 145 key |= 0x2; |
146 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0
; | 146 key |= GrConvolutionEffect::kY_Direction == conv.direction() ? 0x1 : 0x0
; |
147 } | 147 } |
148 b->add32(key); | 148 b->add32(key); |
149 } | 149 } |
150 | 150 |
151 /////////////////////////////////////////////////////////////////////////////// | 151 /////////////////////////////////////////////////////////////////////////////// |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 } | 194 } |
195 | 195 |
196 GrConvolutionEffect::~GrConvolutionEffect() { | 196 GrConvolutionEffect::~GrConvolutionEffect() { |
197 } | 197 } |
198 | 198 |
199 const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const { | 199 const GrBackendEffectFactory& GrConvolutionEffect::getFactory() const { |
200 return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance(); | 200 return GrTBackendEffectFactory<GrConvolutionEffect>::getInstance(); |
201 } | 201 } |
202 | 202 |
203 bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const { | 203 bool GrConvolutionEffect::onIsEqual(const GrEffect& sBase) const { |
204 const GrConvolutionEffect& s = CastEffect<GrConvolutionEffect>(sBase); | 204 const GrConvolutionEffect& s = GrEffect::CastEffect<GrConvolutionEffect>(sBa
se); |
205 return (this->texture(0) == s.texture(0) && | 205 return (this->texture(0) == s.texture(0) && |
206 this->radius() == s.radius() && | 206 this->radius() == s.radius() && |
207 this->direction() == s.direction() && | 207 this->direction() == s.direction() && |
208 this->useBounds() == s.useBounds() && | 208 this->useBounds() == s.useBounds() && |
209 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) && | 209 0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) && |
210 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); | 210 0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float))); |
211 } | 211 } |
212 | 212 |
213 /////////////////////////////////////////////////////////////////////////////// | 213 /////////////////////////////////////////////////////////////////////////////// |
214 | 214 |
(...skipping 17 matching lines...) Expand all Loading... |
232 } | 232 } |
233 | 233 |
234 bool useBounds = random->nextBool(); | 234 bool useBounds = random->nextBool(); |
235 return GrConvolutionEffect::Create(textures[texIdx], | 235 return GrConvolutionEffect::Create(textures[texIdx], |
236 dir, | 236 dir, |
237 radius, | 237 radius, |
238 kernel, | 238 kernel, |
239 useBounds, | 239 useBounds, |
240 bounds); | 240 bounds); |
241 } | 241 } |
OLD | NEW |