Home | History | Annotate | Download | only in effects
      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 "GrTextureDomain.h"
     12 
     13 // A little bit less than the minimum # uniforms required by DX9SM2 (32).
     14 // Allows for a 5x5 kernel (or 25x1, for that matter).
     15 #define MAX_KERNEL_SIZE 25
     16 
     17 class GrMatrixConvolutionEffect : public GrFragmentProcessor {
     18 public:
     19     static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
     20                                                      const SkIRect& bounds,
     21                                                      const SkISize& kernelSize,
     22                                                      const SkScalar* kernel,
     23                                                      SkScalar gain,
     24                                                      SkScalar bias,
     25                                                      const SkIPoint& kernelOffset,
     26                                                      GrTextureDomain::Mode tileMode,
     27                                                      bool convolveAlpha) {
     28         return std::unique_ptr<GrFragmentProcessor>(
     29                 new GrMatrixConvolutionEffect(std::move(proxy), bounds, kernelSize, kernel, gain,
     30                                               bias, kernelOffset, tileMode, convolveAlpha));
     31     }
     32 
     33     static std::unique_ptr<GrFragmentProcessor> MakeGaussian(sk_sp<GrTextureProxy> proxy,
     34                                                              const SkIRect& bounds,
     35                                                              const SkISize& kernelSize,
     36                                                              SkScalar gain,
     37                                                              SkScalar bias,
     38                                                              const SkIPoint& kernelOffset,
     39                                                              GrTextureDomain::Mode tileMode,
     40                                                              bool convolveAlpha,
     41                                                              SkScalar sigmaX,
     42                                                              SkScalar sigmaY);
     43 
     44     const SkIRect& bounds() const { return fBounds; }
     45     const SkISize& kernelSize() const { return fKernelSize; }
     46     const float* kernelOffset() const { return fKernelOffset; }
     47     const float* kernel() const { return fKernel; }
     48     float gain() const { return fGain; }
     49     float bias() const { return fBias; }
     50     bool convolveAlpha() const { return fConvolveAlpha; }
     51     const GrTextureDomain& domain() const { return fDomain; }
     52 
     53     const char* name() const override { return "MatrixConvolution"; }
     54 
     55     std::unique_ptr<GrFragmentProcessor> clone() const override;
     56 
     57 private:
     58     GrMatrixConvolutionEffect(sk_sp<GrTextureProxy> proxy,
     59                               const SkIRect& bounds,
     60                               const SkISize& kernelSize,
     61                               const SkScalar* kernel,
     62                               SkScalar gain,
     63                               SkScalar bias,
     64                               const SkIPoint& kernelOffset,
     65                               GrTextureDomain::Mode tileMode,
     66                               bool convolveAlpha);
     67 
     68     GrMatrixConvolutionEffect(const GrMatrixConvolutionEffect&);
     69 
     70     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
     71 
     72     void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
     73 
     74     bool onIsEqual(const GrFragmentProcessor&) const override;
     75 
     76     GrCoordTransform fCoordTransform;
     77     GrTextureDomain  fDomain;
     78     TextureSampler   fTextureSampler;
     79     SkIRect          fBounds;
     80     SkISize          fKernelSize;
     81     float            fKernel[MAX_KERNEL_SIZE];
     82     float            fGain;
     83     float            fBias;
     84     float            fKernelOffset[2];
     85     bool             fConvolveAlpha;
     86 
     87     GR_DECLARE_FRAGMENT_PROCESSOR_TEST
     88 
     89     typedef GrFragmentProcessor INHERITED;
     90 };
     91 
     92 #endif
     93