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 "src/gpu/effects/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: Make(sk_sp<GrTextureProxy> srcProxy,const SkIRect & srcBounds,const SkISize & kernelSize,const SkScalar * kernel,SkScalar gain,SkScalar bias,const SkIPoint & kernelOffset,GrTextureDomain::Mode tileMode,bool convolveAlpha)19 static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> srcProxy, 20 const SkIRect& srcBounds, 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(srcProxy), srcBounds, kernelSize, kernel, 30 gain, bias, kernelOffset, tileMode, convolveAlpha)); 31 } 32 33 static std::unique_ptr<GrFragmentProcessor> MakeGaussian(sk_sp<GrTextureProxy> srcProxy, 34 const SkIRect& srcBounds, 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 bounds()44 const SkIRect& bounds() const { return fBounds; } kernelSize()45 const SkISize& kernelSize() const { return fKernelSize; } kernelOffset()46 const float* kernelOffset() const { return fKernelOffset; } kernel()47 const float* kernel() const { return fKernel; } gain()48 float gain() const { return fGain; } bias()49 float bias() const { return fBias; } convolveAlpha()50 bool convolveAlpha() const { return fConvolveAlpha; } domain()51 const GrTextureDomain& domain() const { return fDomain; } 52 name()53 const char* name() const override { return "MatrixConvolution"; } 54 55 std::unique_ptr<GrFragmentProcessor> clone() const override; 56 57 private: 58 // srcProxy is the texture that is going to be convolved 59 // srcBounds is the subset of 'srcProxy' that will be used (e.g., for clamp mode) 60 GrMatrixConvolutionEffect(sk_sp<GrTextureProxy> srcProxy, 61 const SkIRect& srcBounds, 62 const SkISize& kernelSize, 63 const SkScalar* kernel, 64 SkScalar gain, 65 SkScalar bias, 66 const SkIPoint& kernelOffset, 67 GrTextureDomain::Mode tileMode, 68 bool convolveAlpha); 69 70 GrMatrixConvolutionEffect(const GrMatrixConvolutionEffect&); 71 72 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; 73 74 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; 75 76 bool onIsEqual(const GrFragmentProcessor&) const override; 77 onTextureSampler(int i)78 const TextureSampler& onTextureSampler(int i) const override { return fTextureSampler; } 79 80 GrCoordTransform fCoordTransform; 81 GrTextureDomain fDomain; 82 TextureSampler fTextureSampler; 83 SkIRect fBounds; 84 SkISize fKernelSize; 85 float fKernel[MAX_KERNEL_SIZE]; 86 float fGain; 87 float fBias; 88 float fKernelOffset[2]; 89 bool fConvolveAlpha; 90 91 GR_DECLARE_FRAGMENT_PROCESSOR_TEST 92 93 typedef GrFragmentProcessor INHERITED; 94 }; 95 96 #endif 97