Home | History | Annotate | Download | only in effects
      1 /*
      2  * Copyright 2012 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 GrGaussianConvolutionFragmentProcessor_DEFINED
      9 #define GrGaussianConvolutionFragmentProcessor_DEFINED
     10 
     11 #include "Gr1DKernelEffect.h"
     12 #include "GrTextureDomain.h"
     13 
     14 /**
     15  * A 1D Gaussian convolution effect. The kernel is computed as an array of 2 * half-width weights.
     16  * Each texel is multiplied by it's weight and summed to determine the filtered color. The output
     17  * color is set to a modulation of the filtered and input colors.
     18  */
     19 class GrGaussianConvolutionFragmentProcessor : public Gr1DKernelEffect {
     20 public:
     21     /// Convolve with a Gaussian kernel
     22     static sk_sp<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
     23                                            Direction dir,
     24                                            int halfWidth,
     25                                            float gaussianSigma,
     26                                            GrTextureDomain::Mode mode,
     27                                            int* bounds) {
     28         return sk_sp<GrFragmentProcessor>(new GrGaussianConvolutionFragmentProcessor(
     29             std::move(proxy), dir, halfWidth, gaussianSigma, mode, bounds));
     30     }
     31 
     32     ~GrGaussianConvolutionFragmentProcessor() override;
     33 
     34     const float* kernel() const { return fKernel; }
     35 
     36     const int* bounds() const { return fBounds; }
     37     bool useBounds() const { return fMode != GrTextureDomain::kIgnore_Mode; }
     38 
     39     GrTextureDomain::Mode mode() const { return fMode; }
     40 
     41     const char* name() const override { return "GaussianConvolution"; }
     42 
     43     // This was decided based on the min allowed value for the max texture
     44     // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0
     45     // on a blur filter gives a kernel width of 25 while a sigma of 5.0
     46     // would exceed a 32 wide kernel.
     47     static const int kMaxKernelRadius = 12;
     48     // With a C++11 we could have a constexpr version of WidthFromRadius()
     49     // and not have to duplicate this calculation.
     50     static const int kMaxKernelWidth = 2 * kMaxKernelRadius + 1;
     51 
     52 private:
     53     /// Convolve with a Gaussian kernel
     54     GrGaussianConvolutionFragmentProcessor(sk_sp<GrTextureProxy>, Direction,
     55                                            int halfWidth, float gaussianSigma,
     56                                            GrTextureDomain::Mode mode, int bounds[2]);
     57 
     58     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
     59 
     60     void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
     61 
     62     bool onIsEqual(const GrFragmentProcessor&) const override;
     63 
     64     GR_DECLARE_FRAGMENT_PROCESSOR_TEST
     65 
     66     // TODO: Inline the kernel constants into the generated shader code. This may involve pulling
     67     // some of the logic from SkGpuBlurUtils into this class related to radius/sigma calculations.
     68     float fKernel[kMaxKernelWidth];
     69     int   fBounds[2];
     70     GrTextureDomain::Mode fMode;
     71 
     72     typedef Gr1DKernelEffect INHERITED;
     73 };
     74 
     75 #endif
     76