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 |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 25 | static GrEffectRef* Create(GrTexture* tex, |
| 26 | Direction dir, |
| 27 | int halfWidth, |
| 28 | const float* kernel, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 29 | bool useBounds, |
| 30 | float bounds[2]) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 31 | AutoEffectUnref effect(SkNEW_ARGS(GrConvolutionEffect, (tex, |
| 32 | dir, |
| 33 | halfWidth, |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 34 | kernel, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 35 | useBounds, |
| 36 | bounds))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 37 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 38 | } |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 39 | |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 40 | /// Convolve with a Gaussian kernel |
bsalomon@google.com | c5fae9e | 2013-01-24 19:00:02 +0000 | [diff] [blame] | 41 | static GrEffectRef* CreateGaussian(GrTexture* tex, |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 42 | Direction dir, |
| 43 | int halfWidth, |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 44 | float gaussianSigma, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 45 | bool useBounds, |
| 46 | float bounds[2]) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 47 | AutoEffectUnref effect(SkNEW_ARGS(GrConvolutionEffect, (tex, |
| 48 | dir, |
| 49 | halfWidth, |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 50 | gaussianSigma, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 51 | useBounds, |
| 52 | bounds))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 53 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 54 | } |
| 55 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 56 | virtual ~GrConvolutionEffect(); |
| 57 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 58 | const float* kernel() const { return fKernel; } |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 59 | |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 60 | const float* bounds() const { return fBounds; } |
| 61 | bool useBounds() const { return fUseBounds; } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 62 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 63 | static const char* Name() { return "Convolution"; } |
| 64 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 65 | typedef GrGLConvolutionEffect GLEffect; |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 66 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 67 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 68 | |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 69 | virtual void getConstantColorComponents(GrColor*, uint32_t* validFlags) const { |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 70 | // If the texture was opaque we could know that the output color if we knew the sum of the |
| 71 | // kernel values. |
| 72 | *validFlags = 0; |
| 73 | } |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 74 | |
| 75 | enum { |
| 76 | // This was decided based on the min allowed value for the max texture |
| 77 | // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0 |
| 78 | // on a blur filter gives a kernel width of 25 while a sigma of 5.0 |
| 79 | // would exceed a 32 wide kernel. |
| 80 | kMaxKernelRadius = 12, |
| 81 | // With a C++11 we could have a constexpr version of WidthFromRadius() |
| 82 | // and not have to duplicate this calculation. |
| 83 | kMaxKernelWidth = 2 * kMaxKernelRadius + 1, |
| 84 | }; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 85 | |
| 86 | protected: |
| 87 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 88 | float fKernel[kMaxKernelWidth]; |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 89 | bool fUseBounds; |
| 90 | float fBounds[2]; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 91 | |
| 92 | private: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 93 | GrConvolutionEffect(GrTexture*, Direction, |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 94 | int halfWidth, |
| 95 | const float* kernel, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 96 | bool useBounds, |
| 97 | float bounds[2]); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 98 | |
| 99 | /// Convolve with a Gaussian kernel |
| 100 | GrConvolutionEffect(GrTexture*, Direction, |
| 101 | int halfWidth, |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 102 | float gaussianSigma, |
senorblanco@chromium.org | e8232bc | 2013-07-29 18:45:44 +0000 | [diff] [blame] | 103 | bool useBounds, |
| 104 | float bounds[2]); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 105 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 106 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 107 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 108 | GR_DECLARE_EFFECT_TEST; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 109 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 110 | typedef Gr1DKernelEffect INHERITED; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | #endif |