gfx/skia/trunk/src/gpu/effects/GrConvolutionEffect.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/gpu/effects/GrConvolutionEffect.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,113 @@
     1.4 +/*
     1.5 + * Copyright 2012 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#ifndef GrConvolutionEffect_DEFINED
    1.12 +#define GrConvolutionEffect_DEFINED
    1.13 +
    1.14 +#include "Gr1DKernelEffect.h"
    1.15 +
    1.16 +class GrGLConvolutionEffect;
    1.17 +
    1.18 +/**
    1.19 + * A convolution effect. The kernel is specified as an array of 2 * half-width
    1.20 + * + 1 weights. Each texel is multiplied by it's weight and summed to determine
    1.21 + * the output color. The output color is modulated by the input color.
    1.22 + */
    1.23 +class GrConvolutionEffect : public Gr1DKernelEffect {
    1.24 +
    1.25 +public:
    1.26 +
    1.27 +    /// Convolve with an arbitrary user-specified kernel
    1.28 +    static GrEffectRef* Create(GrTexture* tex,
    1.29 +                               Direction dir,
    1.30 +                               int halfWidth,
    1.31 +                               const float* kernel,
    1.32 +                               bool useBounds,
    1.33 +                               float bounds[2]) {
    1.34 +        AutoEffectUnref effect(SkNEW_ARGS(GrConvolutionEffect, (tex,
    1.35 +                                                                dir,
    1.36 +                                                                halfWidth,
    1.37 +                                                                kernel,
    1.38 +                                                                useBounds,
    1.39 +                                                                bounds)));
    1.40 +        return CreateEffectRef(effect);
    1.41 +    }
    1.42 +
    1.43 +    /// Convolve with a Gaussian kernel
    1.44 +    static GrEffectRef* CreateGaussian(GrTexture* tex,
    1.45 +                                       Direction dir,
    1.46 +                                       int halfWidth,
    1.47 +                                       float gaussianSigma,
    1.48 +                                       bool useBounds,
    1.49 +                                       float bounds[2]) {
    1.50 +        AutoEffectUnref effect(SkNEW_ARGS(GrConvolutionEffect, (tex,
    1.51 +                                                                dir,
    1.52 +                                                                halfWidth,
    1.53 +                                                                gaussianSigma,
    1.54 +                                                                useBounds,
    1.55 +                                                                bounds)));
    1.56 +        return CreateEffectRef(effect);
    1.57 +    }
    1.58 +
    1.59 +    virtual ~GrConvolutionEffect();
    1.60 +
    1.61 +    const float* kernel() const { return fKernel; }
    1.62 +
    1.63 +    const float* bounds() const { return fBounds; }
    1.64 +    bool useBounds() const { return fUseBounds; }
    1.65 +
    1.66 +    static const char* Name() { return "Convolution"; }
    1.67 +
    1.68 +    typedef GrGLConvolutionEffect GLEffect;
    1.69 +
    1.70 +    virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
    1.71 +
    1.72 +    virtual void getConstantColorComponents(GrColor*, uint32_t* validFlags) const {
    1.73 +        // If the texture was opaque we could know that the output color if we knew the sum of the
    1.74 +        // kernel values.
    1.75 +        *validFlags = 0;
    1.76 +    }
    1.77 +
    1.78 +    enum {
    1.79 +        // This was decided based on the min allowed value for the max texture
    1.80 +        // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0
    1.81 +        // on a blur filter gives a kernel width of 25 while a sigma of 5.0
    1.82 +        // would exceed a 32 wide kernel.
    1.83 +        kMaxKernelRadius = 12,
    1.84 +        // With a C++11 we could have a constexpr version of WidthFromRadius()
    1.85 +        // and not have to duplicate this calculation.
    1.86 +        kMaxKernelWidth = 2 * kMaxKernelRadius + 1,
    1.87 +    };
    1.88 +
    1.89 +protected:
    1.90 +
    1.91 +    float fKernel[kMaxKernelWidth];
    1.92 +    bool fUseBounds;
    1.93 +    float fBounds[2];
    1.94 +
    1.95 +private:
    1.96 +    GrConvolutionEffect(GrTexture*, Direction,
    1.97 +                        int halfWidth,
    1.98 +                        const float* kernel,
    1.99 +                        bool useBounds,
   1.100 +                        float bounds[2]);
   1.101 +
   1.102 +    /// Convolve with a Gaussian kernel
   1.103 +    GrConvolutionEffect(GrTexture*, Direction,
   1.104 +                        int halfWidth,
   1.105 +                        float gaussianSigma,
   1.106 +                        bool useBounds,
   1.107 +                        float bounds[2]);
   1.108 +
   1.109 +    virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
   1.110 +
   1.111 +    GR_DECLARE_EFFECT_TEST;
   1.112 +
   1.113 +    typedef Gr1DKernelEffect INHERITED;
   1.114 +};
   1.115 +
   1.116 +#endif

mercurial