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
8#ifndef GrMatrixConvolutionEffect_DEFINED
9#define GrMatrixConvolutionEffect_DEFINED
10
11#include "GrSingleTextureEffect.h"
12#include "GrTextureDomain.h"
13
14// A little bit less than the minimum # uniforms required by DX9SM2 (32).
15// Allows for a 5x5 kernel (or 25x1, for that matter).
16#define MAX_KERNEL_SIZE 25
17
18class GrGLMatrixConvolutionEffect;
19
20class GrMatrixConvolutionEffect : public GrSingleTextureEffect {
21public:
22    static GrFragmentProcessor* Create(GrTexture* texture,
23                                       const SkIRect& bounds,
24                                       const SkISize& kernelSize,
25                                       const SkScalar* kernel,
26                                       SkScalar gain,
27                                       SkScalar bias,
28                                       const SkIPoint& kernelOffset,
29                                       GrTextureDomain::Mode tileMode,
30                                       bool convolveAlpha) {
31        return SkNEW_ARGS(GrMatrixConvolutionEffect, (texture,
32                                                      bounds,
33                                                      kernelSize,
34                                                      kernel,
35                                                      gain,
36                                                      bias,
37                                                      kernelOffset,
38                                                      tileMode,
39                                                      convolveAlpha));
40    }
41
42    static GrFragmentProcessor* CreateGaussian(GrTexture* texture,
43                                               const SkIRect& bounds,
44                                               const SkISize& kernelSize,
45                                               SkScalar gain,
46                                               SkScalar bias,
47                                               const SkIPoint& kernelOffset,
48                                               GrTextureDomain::Mode tileMode,
49                                               bool convolveAlpha,
50                                               SkScalar sigmaX,
51                                               SkScalar sigmaY);
52
53    virtual ~GrMatrixConvolutionEffect();
54
55    virtual void getConstantColorComponents(GrColor* color,
56                                            uint32_t* validFlags) const SK_OVERRIDE {
57        // TODO: Try to do better?
58        *validFlags = 0;
59    }
60
61    static const char* Name() { return "MatrixConvolution"; }
62    const SkIRect& bounds() const { return fBounds; }
63    const SkISize& kernelSize() const { return fKernelSize; }
64    const float* kernelOffset() const { return fKernelOffset; }
65    const float* kernel() const { return fKernel; }
66    float gain() const { return fGain; }
67    float bias() const { return fBias; }
68    bool convolveAlpha() const { return fConvolveAlpha; }
69    const GrTextureDomain& domain() const { return fDomain; }
70
71    typedef GrGLMatrixConvolutionEffect GLProcessor;
72
73    virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE;
74
75private:
76    GrMatrixConvolutionEffect(GrTexture*,
77                              const SkIRect& bounds,
78                              const SkISize& kernelSize,
79                              const SkScalar* kernel,
80                              SkScalar gain,
81                              SkScalar bias,
82                              const SkIPoint& kernelOffset,
83                              GrTextureDomain::Mode tileMode,
84                              bool convolveAlpha);
85
86    virtual bool onIsEqual(const GrProcessor&) const SK_OVERRIDE;
87
88    SkIRect         fBounds;
89    SkISize         fKernelSize;
90    float           fKernel[MAX_KERNEL_SIZE];
91    float           fGain;
92    float           fBias;
93    float           fKernelOffset[2];
94    bool            fConvolveAlpha;
95    GrTextureDomain fDomain;
96
97    GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
98
99    typedef GrSingleTextureEffect INHERITED;
100};
101
102#endif
103