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