Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(466)

Side by Side Diff: src/gpu/effects/GrMatrixConvolutionEffect.cpp

Issue 379253003: Initial change to move 2D kernel to its own file (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Correctly wrapping asNewEffect definition with ifdefs Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2014 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 #include "GrMatrixConvolutionEffect.h"
8 #include "gl/GrGLEffect.h"
9 #include "gl/GrGLSL.h"
10 #include "gl/GrGLTexture.h"
11 #include "GrTBackendEffectFactory.h"
12
13 // For brevity
14 typedef GrGLUniformManager::UniformHandle UniformHandle;
15
16 class GrGLMatrixConvolutionEffect : public GrGLEffect {
17 public:
18 GrGLMatrixConvolutionEffect(const GrBackendEffectFactory& factory,
19 const GrDrawEffect& effect);
20 virtual void emitCode(GrGLShaderBuilder*,
21 const GrDrawEffect&,
22 EffectKey,
23 const char* outputColor,
24 const char* inputColor,
25 const TransformedCoordsArray&,
26 const TextureSamplerArray&) SK_OVERRIDE;
27
28 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
29
30 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER RIDE;
31
32 private:
33 typedef GrGLUniformManager::UniformHandle UniformHandle;
34 typedef SkMatrixConvolutionImageFilter::TileMode TileMode;
35 SkISize fKernelSize;
36 TileMode fTileMode;
37 bool fConvolveAlpha;
38
39 UniformHandle fBoundsUni;
40 UniformHandle fKernelUni;
41 UniformHandle fImageIncrementUni;
42 UniformHandle fKernelOffsetUni;
43 UniformHandle fGainUni;
44 UniformHandle fBiasUni;
45
46 typedef GrGLEffect INHERITED;
47 };
48
49 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrBackendEffectFa ctory& factory,
50 const GrDrawEffect& dra wEffect)
51 : INHERITED(factory) {
52 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvoluti onEffect>();
53 fKernelSize = m.kernelSize();
54 fTileMode = m.tileMode();
55 fConvolveAlpha = m.convolveAlpha();
56 }
57
58 static void appendTextureLookup(GrGLShaderBuilder* builder,
59 const GrGLShaderBuilder::TextureSampler& sampler ,
60 const char* coord,
61 const char* bounds,
62 SkMatrixConvolutionImageFilter::TileMode tileMod e) {
63 SkString clampedCoord;
64 switch (tileMode) {
65 case SkMatrixConvolutionImageFilter::kClamp_TileMode:
66 clampedCoord.printf("clamp(%s, %s.xy, %s.zw)", coord, bounds, bounds );
67 coord = clampedCoord.c_str();
68 break;
69 case SkMatrixConvolutionImageFilter::kRepeat_TileMode:
70 clampedCoord.printf("mod(%s - %s.xy, %s.zw - %s.xy) + %s.xy", coord, bounds, bounds, bounds, bounds);
71 coord = clampedCoord.c_str();
72 break;
73 case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode:
74 builder->fsCodeAppendf("clamp(%s, %s.xy, %s.zw) != %s ? vec4(0, 0, 0 , 0) : ", coord, bounds, bounds, coord);
75 break;
76 }
77 builder->fsAppendTextureLookup(sampler, coord);
78 }
79
80 void GrGLMatrixConvolutionEffect::emitCode(GrGLShaderBuilder* builder,
81 const GrDrawEffect&,
82 EffectKey key,
83 const char* outputColor,
84 const char* inputColor,
85 const TransformedCoordsArray& coords,
86 const TextureSamplerArray& samplers) {
87 sk_ignore_unused_variable(inputColor);
88 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
89 fBoundsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
90 kVec4f_GrSLType, "Bounds");
91 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi lity,
92 kVec2f_GrSLType, "ImageIncrement");
93 fKernelUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibilit y,
94 kFloat_GrSLType,
95 "Kernel",
96 fKernelSize.width() * fKernelSize.h eight());
97 fKernelOffsetUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibili ty,
98 kVec2f_GrSLType, "KernelOffset");
99 fGainUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
100 kFloat_GrSLType, "Gain");
101 fBiasUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
102 kFloat_GrSLType, "Bias");
103
104 const char* bounds = builder->getUniformCStr(fBoundsUni);
105 const char* kernelOffset = builder->getUniformCStr(fKernelOffsetUni);
106 const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
107 const char* kernel = builder->getUniformCStr(fKernelUni);
108 const char* gain = builder->getUniformCStr(fGainUni);
109 const char* bias = builder->getUniformCStr(fBiasUni);
110 int kWidth = fKernelSize.width();
111 int kHeight = fKernelSize.height();
112
113 builder->fsCodeAppend("\t\tvec4 sum = vec4(0, 0, 0, 0);\n");
114 builder->fsCodeAppendf("\t\tvec2 coord = %s - %s * %s;\n", coords2D.c_str(), kernelOffset, imgInc);
115 builder->fsCodeAppendf("\t\tfor (int y = 0; y < %d; y++) {\n", kHeight);
116 builder->fsCodeAppendf("\t\t\tfor (int x = 0; x < %d; x++) {\n", kWidth);
117 builder->fsCodeAppendf("\t\t\t\tfloat k = %s[y * %d + x];\n", kernel, kWidth );
118 builder->fsCodeAppendf("\t\t\t\tvec2 coord2 = coord + vec2(x, y) * %s;\n", i mgInc);
119 builder->fsCodeAppend("\t\t\t\tvec4 c = ");
120 appendTextureLookup(builder, samplers[0], "coord2", bounds, fTileMode);
121 builder->fsCodeAppend(";\n");
122 if (!fConvolveAlpha) {
123 builder->fsCodeAppend("\t\t\t\tc.rgb /= c.a;\n");
124 }
125 builder->fsCodeAppend("\t\t\t\tsum += c * k;\n");
126 builder->fsCodeAppend("\t\t\t}\n");
127 builder->fsCodeAppend("\t\t}\n");
128 if (fConvolveAlpha) {
129 builder->fsCodeAppendf("\t\t%s = sum * %s + %s;\n", outputColor, gain, b ias);
130 builder->fsCodeAppendf("\t\t%s.rgb = clamp(%s.rgb, 0.0, %s.a);\n",
131 outputColor, outputColor, outputColor);
132 } else {
133 builder->fsCodeAppend("\t\tvec4 c = ");
134 appendTextureLookup(builder, samplers[0], coords2D.c_str(), bounds, fTil eMode);
135 builder->fsCodeAppend(";\n");
136 builder->fsCodeAppendf("\t\t%s.a = c.a;\n", outputColor);
137 builder->fsCodeAppendf("\t\t%s.rgb = sum.rgb * %s + %s;\n", outputColor, gain, bias);
138 builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor );
139 }
140 }
141
142 namespace {
143
144 int encodeXY(int x, int y) {
145 SkASSERT(x >= 1 && y >= 1 && x * y <= 32);
146 if (y < x)
147 return 0x40 | encodeXY(y, x);
148 else
149 return (0x40 >> x) | (y - x);
150 }
151
152 };
153
154 GrGLEffect::EffectKey GrGLMatrixConvolutionEffect::GenKey(const GrDrawEffect& dr awEffect,
155 const GrGLCaps&) {
156 const GrMatrixConvolutionEffect& m = drawEffect.castEffect<GrMatrixConvoluti onEffect>();
157 EffectKey key = encodeXY(m.kernelSize().width(), m.kernelSize().height());
158 key |= m.tileMode() << 7;
159 key |= m.convolveAlpha() ? 1 << 9 : 0;
160 return key;
161 }
162
163 void GrGLMatrixConvolutionEffect::setData(const GrGLUniformManager& uman,
164 const GrDrawEffect& drawEffect) {
165 const GrMatrixConvolutionEffect& conv = drawEffect.castEffect<GrMatrixConvol utionEffect>();
166 GrTexture& texture = *conv.texture(0);
167 // the code we generated was for a specific kernel size
168 SkASSERT(conv.kernelSize() == fKernelSize);
169 SkASSERT(conv.tileMode() == fTileMode);
170 float imageIncrement[2];
171 float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
172 imageIncrement[0] = 1.0f / texture.width();
173 imageIncrement[1] = ySign / texture.height();
174 uman.set2fv(fImageIncrementUni, 1, imageIncrement);
175 uman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset());
176 uman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.ker nel());
177 uman.set1f(fGainUni, conv.gain());
178 uman.set1f(fBiasUni, conv.bias());
179 const SkIRect& bounds = conv.bounds();
180 float left = (float) bounds.left() / texture.width();
181 float top = (float) bounds.top() / texture.height();
182 float right = (float) bounds.right() / texture.width();
183 float bottom = (float) bounds.bottom() / texture.height();
184 if (texture.origin() == kBottomLeft_GrSurfaceOrigin) {
185 uman.set4f(fBoundsUni, left, 1.0f - bottom, right, 1.0f - top);
186 } else {
187 uman.set4f(fBoundsUni, left, top, right, bottom);
188 }
189 }
190
191 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture,
192 const SkIRect& bounds,
193 const SkISize& kernelSize,
194 const SkScalar* kernel,
195 SkScalar gain,
196 SkScalar bias,
197 const SkIPoint& kernelOffse t,
198 TileMode tileMode,
199 bool convolveAlpha)
200 : INHERITED(texture, MakeDivByTextureWHMatrix(texture)),
201 fBounds(bounds),
202 fKernelSize(kernelSize),
203 fGain(SkScalarToFloat(gain)),
204 fBias(SkScalarToFloat(bias) / 255.0f),
205 fTileMode(tileMode),
206 fConvolveAlpha(convolveAlpha) {
207 fKernel = new float[kernelSize.width() * kernelSize.height()];
208 for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) {
209 fKernel[i] = SkScalarToFloat(kernel[i]);
210 }
211 fKernelOffset[0] = static_cast<float>(kernelOffset.x());
212 fKernelOffset[1] = static_cast<float>(kernelOffset.y());
213 this->setWillNotUseInputColor();
214 }
215
216 GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() {
217 delete[] fKernel;
218 }
219
220 const GrBackendEffectFactory& GrMatrixConvolutionEffect::getFactory() const {
221 return GrTBackendEffectFactory<GrMatrixConvolutionEffect>::getInstance();
222 }
223
224 bool GrMatrixConvolutionEffect::onIsEqual(const GrEffect& sBase) const {
225 const GrMatrixConvolutionEffect& s = CastEffect<GrMatrixConvolutionEffect>(s Base);
226 return this->texture(0) == s.texture(0) &&
227 fKernelSize == s.kernelSize() &&
228 !memcmp(fKernel, s.kernel(),
229 fKernelSize.width() * fKernelSize.height() * sizeof(float)) & &
230 fGain == s.gain() &&
231 fBias == s.bias() &&
232 fKernelOffset == s.kernelOffset() &&
233 fTileMode == s.tileMode() &&
234 fConvolveAlpha == s.convolveAlpha();
235 }
236
237 GR_DEFINE_EFFECT_TEST(GrMatrixConvolutionEffect);
238
239 GrEffect* GrMatrixConvolutionEffect::TestCreate(SkRandom* random,
240 GrContext* context,
241 const GrDrawTargetCaps&,
242 GrTexture* textures[]) {
243 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
244 GrEffectUnitTest::kAlphaTextureIdx;
245 int width = random->nextRangeU(1, MAX_KERNEL_SIZE);
246 int height = random->nextRangeU(1, MAX_KERNEL_SIZE / width);
247 SkISize kernelSize = SkISize::Make(width, height);
248 SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]);
249 for (int i = 0; i < width * height; i++) {
250 kernel.get()[i] = random->nextSScalar1();
251 }
252 SkScalar gain = random->nextSScalar1();
253 SkScalar bias = random->nextSScalar1();
254 SkIPoint kernelOffset = SkIPoint::Make(random->nextRangeU(0, kernelSize.widt h()),
255 random->nextRangeU(0, kernelSize.heig ht()));
256 SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->w idth()),
257 random->nextRangeU(0, textures[texIdx]->h eight()),
258 random->nextRangeU(0, textures[texIdx]->w idth()),
259 random->nextRangeU(0, textures[texIdx]->h eight()));
260 TileMode tileMode = static_cast<TileMode>(random->nextRangeU(0, 2));
261 bool convolveAlpha = random->nextBool();
262 return GrMatrixConvolutionEffect::Create(textures[texIdx],
263 bounds,
264 kernelSize,
265 kernel.get(),
266 gain,
267 bias,
268 kernelOffset,
269 tileMode,
270 convolveAlpha);
271 }
OLDNEW
« src/gpu/effects/GrMatrixConvolutionEffect.h ('K') | « src/gpu/effects/GrMatrixConvolutionEffect.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698