C++11 override should now be supported by all of {bots,Chrome,Android,Mozilla}
[platform/upstream/libSkiaSharp.git] / src / gpu / effects / GrMatrixConvolutionEffect.cpp
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 GrGLCaps&, 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
42 GrGLMatrixConvolutionEffect::GrGLMatrixConvolutionEffect(const GrProcessor& processor) {
43     const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffect>();
44     fKernelSize = m.kernelSize();
45     fConvolveAlpha = m.convolveAlpha();
46 }
47
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     GrGLFPFragmentBuilder* 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             }
94             fsBuilder->codeAppend("sum += c * k;");
95         }
96     }
97     if (fConvolveAlpha) {
98         fsBuilder->codeAppendf("%s = sum * %s + %s;", outputColor, gain, bias);
99         fsBuilder->codeAppendf("%s.rgb = clamp(%s.rgb, 0.0, %s.a);",
100                                outputColor, outputColor, outputColor);
101     } else {
102         fDomain.sampleTexture(fsBuilder, domain, "c", coords2D, samplers[0]);
103         fsBuilder->codeAppendf("%s.a = c.a;", outputColor);
104         fsBuilder->codeAppendf("%s.rgb = sum.rgb * %s + %s;", outputColor, gain, bias);
105         fsBuilder->codeAppendf("%s.rgb *= %s.a;", outputColor, outputColor);
106     }
107
108     SkString modulate;
109     GrGLSLMulVarBy4f(&modulate, outputColor, inputColor);
110     fsBuilder->codeAppend(modulate.c_str());
111 }
112
113 void GrGLMatrixConvolutionEffect::GenKey(const GrProcessor& processor,
114                                          const GrGLCaps&, GrProcessorKeyBuilder* b) {
115     const GrMatrixConvolutionEffect& m = processor.cast<GrMatrixConvolutionEffect>();
116     SkASSERT(m.kernelSize().width() <= 0x7FFF && m.kernelSize().height() <= 0xFFFF);
117     uint32_t key = m.kernelSize().width() << 16 | m.kernelSize().height();
118     key |= m.convolveAlpha() ? 1 << 31 : 0;
119     b->add32(key);
120     b->add32(GrTextureDomain::GLDomain::DomainKey(m.domain()));
121 }
122
123 void GrGLMatrixConvolutionEffect::setData(const GrGLProgramDataManager& pdman,
124                                           const GrProcessor& processor) {
125     const GrMatrixConvolutionEffect& conv = processor.cast<GrMatrixConvolutionEffect>();
126     GrTexture& texture = *conv.texture(0);
127     // the code we generated was for a specific kernel size
128     SkASSERT(conv.kernelSize() == fKernelSize);
129     float imageIncrement[2];
130     float ySign = texture.origin() == kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
131     imageIncrement[0] = 1.0f / texture.width();
132     imageIncrement[1] = ySign / texture.height();
133     pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
134     pdman.set2fv(fKernelOffsetUni, 1, conv.kernelOffset());
135     pdman.set1fv(fKernelUni, fKernelSize.width() * fKernelSize.height(), conv.kernel());
136     pdman.set1f(fGainUni, conv.gain());
137     pdman.set1f(fBiasUni, conv.bias());
138     fDomain.setData(pdman, conv.domain(), texture.origin());
139 }
140
141 GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture,
142                                                      const SkIRect& bounds,
143                                                      const SkISize& kernelSize,
144                                                      const SkScalar* kernel,
145                                                      SkScalar gain,
146                                                      SkScalar bias,
147                                                      const SkIPoint& kernelOffset,
148                                                      GrTextureDomain::Mode tileMode,
149                                                      bool convolveAlpha)
150   : INHERITED(texture, GrCoordTransform::MakeDivByTextureWHMatrix(texture)),
151     fKernelSize(kernelSize),
152     fGain(SkScalarToFloat(gain)),
153     fBias(SkScalarToFloat(bias) / 255.0f),
154     fConvolveAlpha(convolveAlpha),
155     fDomain(GrTextureDomain::MakeTexelDomain(texture, bounds), tileMode) {
156     this->initClassID<GrMatrixConvolutionEffect>();
157     for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) {
158         fKernel[i] = SkScalarToFloat(kernel[i]);
159     }
160     fKernelOffset[0] = static_cast<float>(kernelOffset.x());
161     fKernelOffset[1] = static_cast<float>(kernelOffset.y());
162 }
163
164 GrMatrixConvolutionEffect::~GrMatrixConvolutionEffect() {
165 }
166
167 void GrMatrixConvolutionEffect::getGLProcessorKey(const GrGLCaps& caps,
168                                                   GrProcessorKeyBuilder* b) const {
169     GrGLMatrixConvolutionEffect::GenKey(*this, caps, b);
170 }
171
172 GrGLFragmentProcessor* GrMatrixConvolutionEffect::createGLInstance() const  {
173     return SkNEW_ARGS(GrGLMatrixConvolutionEffect, (*this));
174 }
175
176 bool GrMatrixConvolutionEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
177     const GrMatrixConvolutionEffect& s = sBase.cast<GrMatrixConvolutionEffect>();
178     return fKernelSize == s.kernelSize() &&
179            !memcmp(fKernel, s.kernel(),
180                    fKernelSize.width() * fKernelSize.height() * sizeof(float)) &&
181            fGain == s.gain() &&
182            fBias == s.bias() &&
183            fKernelOffset == s.kernelOffset() &&
184            fConvolveAlpha == s.convolveAlpha() &&
185            fDomain == s.domain();
186 }
187
188 // Static function to create a 2D convolution
189 GrFragmentProcessor*
190 GrMatrixConvolutionEffect::CreateGaussian(GrTexture* texture,
191                                           const SkIRect& bounds,
192                                           const SkISize& kernelSize,
193                                           SkScalar gain,
194                                           SkScalar bias,
195                                           const SkIPoint& kernelOffset,
196                                           GrTextureDomain::Mode tileMode,
197                                           bool convolveAlpha,
198                                           SkScalar sigmaX,
199                                           SkScalar sigmaY) {
200     float kernel[MAX_KERNEL_SIZE];
201     int width = kernelSize.width();
202     int height = kernelSize.height();
203     SkASSERT(width * height <= MAX_KERNEL_SIZE);
204     float sum = 0.0f;
205     float sigmaXDenom = 1.0f / (2.0f * SkScalarToFloat(SkScalarSquare(sigmaX)));
206     float sigmaYDenom = 1.0f / (2.0f * SkScalarToFloat(SkScalarSquare(sigmaY)));
207     int xRadius = width / 2;
208     int yRadius = height / 2;
209     for (int x = 0; x < width; x++) {
210       float xTerm = static_cast<float>(x - xRadius);
211       xTerm = xTerm * xTerm * sigmaXDenom;
212       for (int y = 0; y < height; y++) {
213         float yTerm = static_cast<float>(y - yRadius);
214         float xyTerm = sk_float_exp(-(xTerm + yTerm * yTerm * sigmaYDenom));
215         // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
216        // is dropped here, since we renormalize the kernel below.
217         kernel[y * width + x] = xyTerm;
218         sum += xyTerm;
219       }
220     }
221     // Normalize the kernel
222     float scale = 1.0f / sum;
223     for (int i = 0; i < width * height; ++i) {
224         kernel[i] *= scale;
225     }
226     return SkNEW_ARGS(GrMatrixConvolutionEffect, (texture,
227                                                   bounds,
228                                                   kernelSize,
229                                                   kernel,
230                                                   gain,
231                                                   bias,
232                                                   kernelOffset,
233                                                   tileMode,
234                                                   convolveAlpha));
235 }
236
237 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMatrixConvolutionEffect);
238
239 GrFragmentProcessor* GrMatrixConvolutionEffect::TestCreate(SkRandom* random,
240                                                            GrContext* context,
241                                                            const GrDrawTargetCaps&,
242                                                            GrTexture* textures[]) {
243     int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
244                                       GrProcessorUnitTest::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.width()),
255                                            random->nextRangeU(0, kernelSize.height()));
256     SkIRect bounds = SkIRect::MakeXYWH(random->nextRangeU(0, textures[texIdx]->width()),
257                                        random->nextRangeU(0, textures[texIdx]->height()),
258                                        random->nextRangeU(0, textures[texIdx]->width()),
259                                        random->nextRangeU(0, textures[texIdx]->height()));
260     GrTextureDomain::Mode tileMode = static_cast<GrTextureDomain::Mode>(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 }