joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 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" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 12 | #include "GrInvariantOutput.h" |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 13 | #include "GrTextureDomain.h" |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 14 | |
| 15 | // A little bit less than the minimum # uniforms required by DX9SM2 (32). |
| 16 | // Allows for a 5x5 kernel (or 25x1, for that matter). |
| 17 | #define MAX_KERNEL_SIZE 25 |
| 18 | |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 19 | class GrMatrixConvolutionEffect : public GrSingleTextureEffect { |
| 20 | public: |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 21 | static GrFragmentProcessor* Create(GrTexture* texture, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 22 | const SkIRect& bounds, |
| 23 | const SkISize& kernelSize, |
| 24 | const SkScalar* kernel, |
| 25 | SkScalar gain, |
| 26 | SkScalar bias, |
| 27 | const SkIPoint& kernelOffset, |
| 28 | GrTextureDomain::Mode tileMode, |
| 29 | bool convolveAlpha) { |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 30 | return new GrMatrixConvolutionEffect(texture, bounds, kernelSize, kernel, gain, bias, |
| 31 | kernelOffset, tileMode, convolveAlpha); |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 32 | } |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 33 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 34 | static GrFragmentProcessor* CreateGaussian(GrTexture* texture, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 35 | const SkIRect& bounds, |
| 36 | const SkISize& kernelSize, |
| 37 | SkScalar gain, |
| 38 | SkScalar bias, |
| 39 | const SkIPoint& kernelOffset, |
| 40 | GrTextureDomain::Mode tileMode, |
| 41 | bool convolveAlpha, |
| 42 | SkScalar sigmaX, |
| 43 | SkScalar sigmaY); |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 44 | |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 45 | virtual ~GrMatrixConvolutionEffect(); |
| 46 | |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 47 | const SkIRect& bounds() const { return fBounds; } |
| 48 | const SkISize& kernelSize() const { return fKernelSize; } |
| 49 | const float* kernelOffset() const { return fKernelOffset; } |
| 50 | const float* kernel() const { return fKernel; } |
| 51 | float gain() const { return fGain; } |
| 52 | float bias() const { return fBias; } |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 53 | bool convolveAlpha() const { return fConvolveAlpha; } |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 54 | const GrTextureDomain& domain() const { return fDomain; } |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 55 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 56 | const char* name() const override { return "MatrixConvolution"; } |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 57 | |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 58 | private: |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 59 | GrMatrixConvolutionEffect(GrTexture*, |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 60 | const SkIRect& bounds, |
| 61 | const SkISize& kernelSize, |
| 62 | const SkScalar* kernel, |
| 63 | SkScalar gain, |
| 64 | SkScalar bias, |
| 65 | const SkIPoint& kernelOffset, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 66 | GrTextureDomain::Mode tileMode, |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 67 | bool convolveAlpha); |
| 68 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 69 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 70 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 71 | void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 72 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 73 | bool onIsEqual(const GrFragmentProcessor&) const override; |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 74 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 75 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 76 | // TODO: Try to do better? |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 77 | inout->mulByUnknownFourComponents(); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 78 | } |
| 79 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 80 | SkIRect fBounds; |
| 81 | SkISize fKernelSize; |
joshualitt | 5acfea7 | 2014-08-11 13:55:34 -0700 | [diff] [blame] | 82 | float fKernel[MAX_KERNEL_SIZE]; |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 83 | float fGain; |
| 84 | float fBias; |
| 85 | float fKernelOffset[2]; |
| 86 | bool fConvolveAlpha; |
| 87 | GrTextureDomain fDomain; |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 88 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 89 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
joshualitt | ac97792 | 2014-07-22 09:52:11 -0700 | [diff] [blame] | 90 | |
| 91 | typedef GrSingleTextureEffect INHERITED; |
| 92 | }; |
| 93 | |
| 94 | #endif |