Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [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 GrGaussianConvolutionFragmentProcessor_DEFINED |
| 9 | #define GrGaussianConvolutionFragmentProcessor_DEFINED |
| 10 | |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 11 | #include "GrCoordTransform.h" |
| 12 | #include "GrFragmentProcessor.h" |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 13 | #include "GrTextureDomain.h" |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * A 1D Gaussian convolution effect. The kernel is computed as an array of 2 * half-width weights. |
| 17 | * Each texel is multiplied by it's weight and summed to determine the filtered color. The output |
| 18 | * color is set to a modulation of the filtered and input colors. |
| 19 | */ |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 20 | class GrGaussianConvolutionFragmentProcessor : public GrFragmentProcessor { |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 21 | public: |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 22 | enum class Direction { kX, kY }; |
| 23 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 24 | /// Convolve with a Gaussian kernel |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 25 | static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy, |
| 26 | Direction dir, |
| 27 | int halfWidth, |
| 28 | float gaussianSigma, |
| 29 | GrTextureDomain::Mode mode, |
| 30 | int* bounds) { |
| 31 | return std::unique_ptr<GrFragmentProcessor>(new GrGaussianConvolutionFragmentProcessor( |
| 32 | std::move(proxy), dir, halfWidth, gaussianSigma, mode, bounds)); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 33 | } |
| 34 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 35 | const float* kernel() const { return fKernel; } |
| 36 | |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 37 | const int* bounds() const { return fBounds; } |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 38 | bool useBounds() const { return fMode != GrTextureDomain::kIgnore_Mode; } |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 39 | int radius() const { return fRadius; } |
| 40 | int width() const { return 2 * fRadius + 1; } |
| 41 | Direction direction() const { return fDirection; } |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 42 | |
| 43 | GrTextureDomain::Mode mode() const { return fMode; } |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 44 | |
| 45 | const char* name() const override { return "GaussianConvolution"; } |
| 46 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 47 | #ifdef SK_DEBUG |
Robert Phillips | ce3c28f | 2018-07-18 13:52:40 -0400 | [diff] [blame] | 48 | SkString dumpInfo() const override { |
| 49 | SkString str; |
| 50 | str.appendf("dir: %s radius: %d bounds: [%d %d]", |
| 51 | Direction::kX == fDirection ? "X" : "Y", |
| 52 | fRadius, |
| 53 | fBounds[0], fBounds[1]); |
| 54 | return str; |
| 55 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 56 | #endif |
Robert Phillips | ce3c28f | 2018-07-18 13:52:40 -0400 | [diff] [blame] | 57 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 58 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 59 | return std::unique_ptr<GrFragmentProcessor>( |
| 60 | new GrGaussianConvolutionFragmentProcessor(*this)); |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 61 | } |
| 62 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 63 | // This was decided based on the min allowed value for the max texture |
| 64 | // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0 |
| 65 | // on a blur filter gives a kernel width of 25 while a sigma of 5.0 |
| 66 | // would exceed a 32 wide kernel. |
| 67 | static const int kMaxKernelRadius = 12; |
| 68 | // With a C++11 we could have a constexpr version of WidthFromRadius() |
| 69 | // and not have to duplicate this calculation. |
| 70 | static const int kMaxKernelWidth = 2 * kMaxKernelRadius + 1; |
| 71 | |
| 72 | private: |
| 73 | /// Convolve with a Gaussian kernel |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 74 | GrGaussianConvolutionFragmentProcessor(sk_sp<GrTextureProxy>, Direction, |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 75 | int halfWidth, float gaussianSigma, |
| 76 | GrTextureDomain::Mode mode, int bounds[2]); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 77 | |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 78 | explicit GrGaussianConvolutionFragmentProcessor(const GrGaussianConvolutionFragmentProcessor&); |
| 79 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 80 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 81 | |
| 82 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
| 83 | |
| 84 | bool onIsEqual(const GrFragmentProcessor&) const override; |
| 85 | |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 86 | const TextureSampler& onTextureSampler(int) const override { return fTextureSampler; } |
| 87 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 88 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 89 | |
Robert Phillips | ce3c28f | 2018-07-18 13:52:40 -0400 | [diff] [blame] | 90 | GrCoordTransform fCoordTransform; |
| 91 | TextureSampler fTextureSampler; |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 92 | // TODO: Inline the kernel constants into the generated shader code. This may involve pulling |
| 93 | // some of the logic from SkGpuBlurUtils into this class related to radius/sigma calculations. |
Robert Phillips | ce3c28f | 2018-07-18 13:52:40 -0400 | [diff] [blame] | 94 | float fKernel[kMaxKernelWidth]; |
| 95 | int fBounds[2]; |
| 96 | int fRadius; |
| 97 | Direction fDirection; |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 98 | GrTextureDomain::Mode fMode; |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 99 | |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 100 | typedef GrFragmentProcessor INHERITED; |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | #endif |