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 | /** |
| 16 | * A convolution effect. The kernel is specified as an array of 2 * half-width |
| 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 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame^] | 24 | GrConvolutionEffect(Direction, int halfWidth, const float* kernel = NULL); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 25 | virtual ~GrConvolutionEffect(); |
| 26 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame^] | 27 | void setKernel(const float* kernel) { |
| 28 | memcpy(fKernel, kernel, this->width()); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Helper to set the kernel to a Gaussian. Replaces the existing kernel. |
| 33 | */ |
| 34 | void setGaussianKernel(float sigma); |
| 35 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 36 | const float* kernel() const { return fKernel; } |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 37 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 38 | static const char* Name() { return "Convolution"; } |
| 39 | |
| 40 | typedef GrGLConvolutionEffect GLProgramStage; |
bsalomon@google.com | 289efe0 | 2012-05-21 20:57:59 +0000 | [diff] [blame] | 41 | |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 42 | virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame^] | 43 | virtual bool isEqual(const GrCustomStage&) const SK_OVERRIDE; |
| 44 | |
| 45 | enum { |
| 46 | // This was decided based on the min allowed value for the max texture |
| 47 | // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0 |
| 48 | // on a blur filter gives a kernel width of 25 while a sigma of 5.0 |
| 49 | // would exceed a 32 wide kernel. |
| 50 | kMaxKernelRadius = 12, |
| 51 | // With a C++11 we could have a constexpr version of WidthFromRadius() |
| 52 | // and not have to duplicate this calculation. |
| 53 | kMaxKernelWidth = 2 * kMaxKernelRadius + 1, |
| 54 | }; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 55 | |
| 56 | protected: |
| 57 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame^] | 58 | float fKernel[kMaxKernelWidth]; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame^] | 62 | typedef Gr1DKernelEffect INHERITED; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | #endif |