tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 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 GrConvolutionEffect_DEFINED |
| 9 | #define GrConvolutionEffect_DEFINED |
| 10 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 11 | #include "Gr1DKernelEffect.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 12 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 13 | class GrGLConvolutionEffect; |
| 14 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 15 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 16 | * A convolution effect. The kernel is specified as an array of 2 * half-width |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 17 | * + 1 weights. Each texel is multiplied by it's weight and summed to determine |
| 18 | * the output color. The output color is modulated by the input color. |
| 19 | */ |
| 20 | class GrConvolutionEffect : public Gr1DKernelEffect { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 21 | |
| 22 | public: |
| 23 | |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 24 | /// Convolve with an arbitrary user-specified kernel |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 25 | static GrFragmentProcessor* Create(GrTexture* tex, |
| 26 | Direction dir, |
| 27 | int halfWidth, |
| 28 | const float* kernel, |
| 29 | bool useBounds, |
| 30 | float bounds[2]) { |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 31 | return SkNEW_ARGS(GrConvolutionEffect, (tex, |
| 32 | dir, |
| 33 | halfWidth, |
| 34 | kernel, |
| 35 | useBounds, |
| 36 | bounds)); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 37 | } |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 38 | |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 39 | /// Convolve with a Gaussian kernel |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 40 | static GrFragmentProcessor* CreateGaussian(GrTexture* tex, |
| 41 | Direction dir, |
| 42 | int halfWidth, |
| 43 | float gaussianSigma, |
| 44 | bool useBounds, |
| 45 | float bounds[2]) { |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 46 | return SkNEW_ARGS(GrConvolutionEffect, (tex, |
| 47 | dir, |
| 48 | halfWidth, |
| 49 | gaussianSigma, |
| 50 | useBounds, |
| 51 | bounds)); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 52 | } |
| 53 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 54 | virtual ~GrConvolutionEffect(); |
| 55 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 56 | const float* kernel() const { return fKernel; } |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 57 | |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 58 | const float* bounds() const { return fBounds; } |
| 59 | bool useBounds() const { return fUseBounds; } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 60 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 61 | static const char* Name() { return "Convolution"; } |
| 62 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 63 | typedef GrGLConvolutionEffect GLProcessor; |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 64 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 65 | virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 66 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 67 | enum { |
| 68 | // This was decided based on the min allowed value for the max texture |
| 69 | // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0 |
| 70 | // on a blur filter gives a kernel width of 25 while a sigma of 5.0 |
| 71 | // would exceed a 32 wide kernel. |
| 72 | kMaxKernelRadius = 12, |
| 73 | // With a C++11 we could have a constexpr version of WidthFromRadius() |
| 74 | // and not have to duplicate this calculation. |
| 75 | kMaxKernelWidth = 2 * kMaxKernelRadius + 1, |
| 76 | }; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 77 | |
| 78 | protected: |
| 79 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 80 | float fKernel[kMaxKernelWidth]; |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 81 | bool fUseBounds; |
| 82 | float fBounds[2]; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 83 | |
| 84 | private: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 85 | GrConvolutionEffect(GrTexture*, Direction, |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 86 | int halfWidth, |
| 87 | const float* kernel, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 88 | bool useBounds, |
| 89 | float bounds[2]); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 90 | |
| 91 | /// Convolve with a Gaussian kernel |
| 92 | GrConvolutionEffect(GrTexture*, Direction, |
| 93 | int halfWidth, |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 94 | float gaussianSigma, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 95 | bool useBounds, |
| 96 | float bounds[2]); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 97 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 98 | virtual bool onIsEqual(const GrProcessor&) const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 99 | |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 100 | virtual void onComputeInvariantOutput(InvariantOutput* inout) const { |
| 101 | // If the texture was opaque we could know that the output color if we knew the sum of the |
| 102 | // kernel values. |
| 103 | inout->fValidFlags = 0; |
| 104 | inout->fIsSingleComponent = false; |
| 105 | } |
| 106 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 107 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 108 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 109 | typedef Gr1DKernelEffect INHERITED; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | #endif |