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/GrGLProcessor.h"
9 #include "gl/GrGLSL.h"
10 #include "gl/GrGLTexture.h"
11 #include "gl/builders/GrGLProgramBuilder.h"
12 
13 class GrGLMatrixConvolutionEffect : public GrGLFragmentProcessor {
14 public:
15     GrGLMatrixConvolutionEffect(const GrProcessor&);
16     virtual void emitCode(GrGLFPBuilder*,
17                           const GrFragmentProcessor&,
18                           const char* outputColor,
19                           const char* inputColor,
20                           const TransformedCoordsArray&,
21                           const TextureSamplerArray&) override;
22 
23     static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
24 
25     void setData(const GrGLProgramDataManager&, const GrProcessor&) override;
26 
27 private:
28     typedef GrGLProgramDataManager::UniformHandle UniformHandle;
29     SkISize                     fKernelSize;
30     bool                        fConvolveAlpha;
31 
32     UniformHandle               fKernelUni;
33     UniformHandle               fImageIncrementUni;
34     UniformHandle               fKernelOffsetUni;
35     UniformHandle               fGainUni;
36     UniformHandle               fBiasUni;
37     GrTextureDomain::GLDomain   fDomain;
38 
39     typedef GrGLFragmentProcessor INHERITED;
40 };
41 
GrGLMatrixConvolutionEffect(const GrProcessor & processor)42 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrProcessor& processor) {
43     const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffect>();
44     fKernelSize = m.kernelSize();
45     fConvolveAlpha = m.convolveAlpha();
46 }
47 
emitCode(GrGLFPBuilder * builder,const GrFragmentProcessor & fp,const char * outputColor,const char * inputColor,const TransformedCoordsArray & coords,const TextureSamplerArray & samplers)48 void GrGLMatrixConvolutionEffect::emitCode(GrGLFPBuilder* builder,
49                                            const GrFragmentProcessor& fp,
50                                            const char* outputColor,
51                                            const char* inputColor,
52                                            const TransformedCoordsArray& coords,
53                                            const TextureSamplerArray& samplers) {
54     const GrTextureDomain& domain = fp.cast<GrMatrixConvolutionEffect>().domain();
55     fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
56                                              kVec2f_GrSLType, kDefault_GrSLPrecision,
57                                              "ImageIncrement");
58     fKernelUni = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility,
59                                           kFloat_GrSLType, kDefault_GrSLPrecision,
60                                           "Kernel",
61                                           fKernelSize.width() * fKernelSize.height());
62     fKernelOffsetUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
63                                            kVec2f_GrSLType, kDefault_GrSLPrecision, "KernelOffset");
64     fGainUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
65                                    kFloat_GrSLType, kDefault_GrSLPrecision, "Gain");
66     fBiasUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
67                                    kFloat_GrSLType, kDefault_GrSLPrecision, "Bias");
68 
69     const char* kernelOffset = builder->getUniformCStr(fKernelOffsetUni);
70     const char* imgInc = builder->getUniformCStr(fImageIncrementUni);
71     const char* kernel = builder->getUniformCStr(fKernelUni);
72     const char* gain = builder->getUniformCStr(fGainUni);
73     const char* bias = builder->getUniformCStr(fBiasUni);
74     int kWidth = fKernelSize.width();
75     int kHeight = fKernelSize.height();
76 
77     GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
78     SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0);
79     fsBuilder->codeAppend("vec4 sum = vec4(0, 0, 0, 0);");
80     fsBuilder->codeAppendf("vec2 coord = %s - %s * %s;", coords2D.c_str(), kernelOffset,
81                            imgInc);
82     fsBuilder->codeAppend("vec4 c;");
83 
84     for (int y = 0; y < kHeight; y++) {
85         for (int x = 0; x < kWidth; x++) {
86             GrGLShaderBuilder::ShaderBlock block(fsBuilder);
87             fsBuilder->codeAppendf("float k = %s[%d * %d + %d];", kernel, y, kWidth, x);
88             SkString coord;
89             coord.printf("coord + vec2(%d, %d) * %s", x, y, imgInc);
90             fDomain.sampleTexture(fsBuilder, domain, "c", coord, samplers[0]);
91             if (!fConvolveAlpha) {
92                 fsBuilder->codeAppend("c.rgb /= c.a;");
93                 fsBuilder->codeAppend("c.rgb = clamp(c.rgb, 0.0, 1.0);");
94             }
95             fsBuilder->codeAppend("sum += c * k;");
96         }
97     }
98     if (fConvolveAlpha) {
99         fsBuilder->codeAppendf("%s = sum * %s + %s;", outputColor, gain, bias);
100         fsBuilder->codeAppendf("%s.rgb = clamp(%s.rgb, 0.0, %s.a);",
101                                outputColor, outputColor, outputColor);
102     } else {
103         fDomain.sampleTexture(fsBuilder, domain, "c", coords2D, samplers[0]);
104         fsBuilder->codeAppendf("%s.a = c.a;", outputColor);
105         fsBuilder->codeAppendf("%s.rgb = sum.rgb * %s + %s;", outputColor, gain, bias);
106         fsBuilder->codeAppendf("%s.rgb *= %s.a;", outputColor, outputColor);
107     }
108 
109     SkString modulate;
110     GrGLSLMulVarBy4f(&modulate, outputColor, inputColor);
111     fsBuilder->codeAppend(modulate.c_str());
112 }
113 
GenKey(const GrProcessor & processor,const GrGLSLCaps &,GrProcessorKeyBuilder * b)114 void GrGLMatrixConvolutionEffect::GenKey(const GrProcessor& processor,
115                                          const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
116     const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffect>();
117     SkASSERT(m.kernelSize().width() <= 0x7FFF && m.kernelSize().height() <= 0xFFFF);
118     uint32_t key = m.kernelSize().width() << 16 | m.kernelSize().height();
119     key |= m.convolveAlpha() ? 1 << 31 : 0;
120     b->add32(key);
121     b->add32(GrTextureDomain::GLDomain::DomainKey(m.domain()));
122 }
123 
setData(const GrGLProgramDataManager & pdman,const GrProcessor & processor)124 void GrGLMatrixConvolutionEffect::setData(const GrGLProgramDataManager& pdman,
125                                           const GrProcessor& processor) {
126     const GrMatrixConvolutionEffect& conv = processor.cast<GrMatrixConvolutionEffect>();
127     GrTexture& texture = *conv.texture(0);
128     // the code we generated was for a specific kernel size
129     SkASSERT(conv.kernelSize() == fKernelSize);
130     float imageIncrement[2];
131     float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
132     imageIncrement[0] = 1.0f / texture.width();
133     imageIncrement[1] = ySign / texture.height();
134     pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
135     pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset());
136     pdman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.kernel());
137     pdman.set1f(fGainUni, conv.gain());
138     pdman.set1f(fBiasUni, conv.bias());
139     fDomain.setData(pdman, conv.domain(), texture.origin());
140 }
141 
GrMatrixConvolutionEffect(GrTexture * texture,const SkIRect & bounds,const SkISize & kernelSize,const SkScalar * kernel,SkScalar gain,SkScalar bias,const SkIPoint & kernelOffset,GrTextureDomain::Mode tileMode,bool convolveAlpha)142 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture,
143                                                      const SkIRect& bounds,
144                                                      const SkISize& kernelSize,
145                                                      const SkScalar* kernel,
146                                                      SkScalar gain,
147                                                      SkScalar bias,
148                                                      const SkIPoint& kernelOffset,
149                                                      GrTextureDomain::Mode tileMode,
150                                                      bool convolveAlpha)
151   : INHERITED(texture, GrCoordTransform::MakeDivByTextureWHMatrix(texture)),
152     fKernelSize(kernelSize),
153     fGain(SkScalarToFloat(gain)),
154     fBias(SkScalarToFloat(bias) / 255.0f),
155     fConvolveAlpha(convolveAlpha),
156     fDomain(GrTextureDomain::MakeTexelDomainForMode(texture, bounds, tileMode), tileMode) {
157     this->initClassID<GrMatrixConvolutionEffect>();
158     for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) {
159         fKernel[i] = SkScalarToFloat(kernel[i]);
160     }
161     fKernelOffset[0] = static_cast<float>(kernelOffset.x());
162     fKernelOffset[1] = static_cast<float>(kernelOffset.y());
163 }
164 
~GrMatrixConvolutionEffect()165 GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() {
166 }
167 
getGLProcessorKey(const GrGLSLCaps & caps,GrProcessorKeyBuilder * b) const168 void GrMatrixConvolutionEffect::getGLProcessorKey(const GrGLSLCaps& caps,
169                                                   GrProcessorKeyBuilder* b) const {
170     GrGLMatrixConvolutionEffect::GenKey(*this, caps, b);
171 }
172 
createGLInstance() const173 GrGLFragmentProcessor* GrMatrixConvolutionEffect::createGLInstance() const  {
174     return SkNEW_ARGS(GrGLMatrixConvolutionEffect, (*this));
175 }
176 
onIsEqual(const GrFragmentProcessor & sBase) const177 bool GrMatrixConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
178     const GrMatrixConvolutionEffect& s = sBase.cast<GrMatrixConvolutionEffect>();
179     return fKernelSize == s.kernelSize() &&
180            !memcmp(fKernel, s.kernel(),
181                    fKernelSize.width() * fKernelSize.height() * sizeof(float)) &&
182            fGain == s.gain() &&
183            fBias == s.bias() &&
184            fKernelOffset == s.kernelOffset() &&
185            fConvolveAlpha == s.convolveAlpha() &&
186            fDomain == s.domain();
187 }
188 
189 // Static function to create a 2D convolution
190 GrFragmentProcessor*
CreateGaussian(GrTexture * texture,const SkIRect & bounds,const SkISize & kernelSize,SkScalar gain,SkScalar bias,const SkIPoint & kernelOffset,GrTextureDomain::Mode tileMode,bool convolveAlpha,SkScalar sigmaX,SkScalar sigmaY)191 GrMatrixConvolutionEffect::CreateGaussian(GrTexture* texture,
192                                           const SkIRect& bounds,
193                                           const SkISize& kernelSize,
194                                           SkScalar gain,
195                                           SkScalar bias,
196                                           const SkIPoint& kernelOffset,
197                                           GrTextureDomain::Mode tileMode,
198                                           bool convolveAlpha,
199                                           SkScalar sigmaX,
200                                           SkScalar sigmaY) {
201     float kernel[MAX_KERNEL_SIZE];
202     int width = kernelSize.width();
203     int height = kernelSize.height();
204     SkASSERT(width * height <= MAX_KERNEL_SIZE);
205     float sum = 0.0f;
206     float sigmaXDenom = 1.0f / (2.0f * SkScalarToFloat(SkScalarSquare(sigmaX)));
207     float sigmaYDenom = 1.0f / (2.0f * SkScalarToFloat(SkScalarSquare(sigmaY)));
208     int xRadius = width / 2;
209     int yRadius = height / 2;
210     for (int x = 0; x < width; x++) {
211       float xTerm = static_cast<float>(x - xRadius);
212       xTerm = xTerm * xTerm * sigmaXDenom;
213       for (int y = 0; y < height; y++) {
214         float yTerm = static_cast<float>(y - yRadius);
215         float xyTerm = sk_float_exp(-(xTerm + yTerm * yTerm * sigmaYDenom));
216         // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
217        // is dropped here, since we renormalize the kernel below.
218         kernel[y * width + x] = xyTerm;
219         sum += xyTerm;
220       }
221     }
222     // Normalize the kernel
223     float scale = 1.0f / sum;
224     for (int i = 0; i < width * height; ++i) {
225         kernel[i] *= scale;
226     }
227     return SkNEW_ARGS(GrMatrixConvolutionEffect, (texture,
228                                                   bounds,
229                                                   kernelSize,
230                                                   kernel,
231                                                   gain,
232                                                   bias,
233                                                   kernelOffset,
234                                                   tileMode,
235                                                   convolveAlpha));
236 }
237 
238 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMatrixConvolutionEffect);
239 
TestCreate(SkRandom * random,GrContext * context,const GrDrawTargetCaps &,GrTexture * textures[])240 GrFragmentProcessor* GrMatrixConvolutionEffect::TestCreate(SkRandom* random,
241                                                            GrContext* context,
242                                                            const GrDrawTargetCaps&,
243                                                            GrTexture* textures[]) {
244     int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
245                                       GrProcessorUnitTest::kAlphaTextureIdx;
246     int width = random->nextRangeU(1, MAX_KERNEL_SIZE);
247     int height = random->nextRangeU(1, MAX_KERNEL_SIZE / width);
248     SkISize kernelSize = SkISize::Make(width, height);
249     SkAutoTDeleteArray<SkScalar> kernel(new SkScalar[width * height]);
250     for (int i = 0; i < width * height; i++) {
251         kernel.get()[i] = random->nextSScalar1();
252     }
253     SkScalar gain = random->nextSScalar1();
254     SkScalar bias = random->nextSScalar1();
255     SkIPoint kernelOffset = SkIPoint::Make(random->nextRangeU(0, kernelSize.width()),
256                                            random->nextRangeU(0, kernelSize.height()));
257     SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->width()),
258                                        random->nextRangeU(0, textures[texIdx]->height()),
259                                        random->nextRangeU(0, textures[texIdx]->width()),
260                                        random->nextRangeU(0, textures[texIdx]->height()));
261     GrTextureDomain::Mode tileMode = static_cast<GrTextureDomain::Mode>(random->nextRangeU(0, 2));
262     bool convolveAlpha = random->nextBool();
263     return GrMatrixConvolutionEffect::Create(textures[texIdx],
264                                              bounds,
265                                              kernelSize,
266                                              kernel.get(),
267                                              gain,
268                                              bias,
269                                              kernelOffset,
270                                              tileMode,
271                                              convolveAlpha);
272 }
273