blob: 066da1975c41d621c55dc3dd0a171203d2613b1d [file] [log] [blame]
tomhudson@google.comd8f856c2012-05-10 12:13:36 +00001/*
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.comb505a122012-05-31 18:40:36 +000011#include "Gr1DKernelEffect.h"
egdaniel605dd0f2014-11-12 08:35:25 -080012#include "GrInvariantOutput.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000013
bsalomon@google.comb505a122012-05-31 18:40:36 +000014/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000015 * A convolution effect. The kernel is specified as an array of 2 * half-width
bsalomon@google.comb505a122012-05-31 18:40:36 +000016 * + 1 weights. Each texel is multiplied by it's weight and summed to determine
17 * the output color. The output color is modulated by the input color.
18 */
19class GrConvolutionEffect : public Gr1DKernelEffect {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000020
21public:
22
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +000023 /// Convolve with an arbitrary user-specified kernel
joshualitt5f10b5c2015-07-09 10:24:35 -070024 static GrFragmentProcessor* Create(GrProcessorDataManager* procDataManager,
25 GrTexture* tex,
joshualittb0a8a372014-09-23 09:50:21 -070026 Direction dir,
27 int halfWidth,
28 const float* kernel,
29 bool useBounds,
30 float bounds[2]) {
joshualitt5f10b5c2015-07-09 10:24:35 -070031 return SkNEW_ARGS(GrConvolutionEffect, (procDataManager,
32 tex,
bsalomon55fad7a2014-07-08 07:34:20 -070033 dir,
34 halfWidth,
35 kernel,
36 useBounds,
37 bounds));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000038 }
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +000039
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000040 /// Convolve with a Gaussian kernel
joshualitt5f10b5c2015-07-09 10:24:35 -070041 static GrFragmentProcessor* CreateGaussian(GrProcessorDataManager* procDataManager,
42 GrTexture* tex,
joshualittb0a8a372014-09-23 09:50:21 -070043 Direction dir,
44 int halfWidth,
45 float gaussianSigma,
46 bool useBounds,
47 float bounds[2]) {
joshualitt5f10b5c2015-07-09 10:24:35 -070048 return SkNEW_ARGS(GrConvolutionEffect, (procDataManager,
49 tex,
bsalomon55fad7a2014-07-08 07:34:20 -070050 dir,
51 halfWidth,
52 gaussianSigma,
53 useBounds,
54 bounds));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000055 }
56
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000057 virtual ~GrConvolutionEffect();
58
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000059 const float* kernel() const { return fKernel; }
bsalomon@google.com289efe02012-05-21 20:57:59 +000060
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000061 const float* bounds() const { return fBounds; }
62 bool useBounds() const { return fUseBounds; }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000063
mtklein36352bf2015-03-25 18:17:31 -070064 const char* name() const override { return "Convolution"; }
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000065
jvanverthcfc18862015-04-28 08:48:20 -070066 void getGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
bsalomon@google.com289efe02012-05-21 20:57:59 +000067
mtklein36352bf2015-03-25 18:17:31 -070068 GrGLFragmentProcessor* createGLInstance() const override;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000069
bsalomon@google.comb505a122012-05-31 18:40:36 +000070 enum {
71 // This was decided based on the min allowed value for the max texture
72 // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0
73 // on a blur filter gives a kernel width of 25 while a sigma of 5.0
74 // would exceed a 32 wide kernel.
75 kMaxKernelRadius = 12,
76 // With a C++11 we could have a constexpr version of WidthFromRadius()
77 // and not have to duplicate this calculation.
78 kMaxKernelWidth = 2 * kMaxKernelRadius + 1,
79 };
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000080
81protected:
82
bsalomon@google.comb505a122012-05-31 18:40:36 +000083 float fKernel[kMaxKernelWidth];
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000084 bool fUseBounds;
85 float fBounds[2];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000086
87private:
joshualitt5f10b5c2015-07-09 10:24:35 -070088 GrConvolutionEffect(GrProcessorDataManager*,
89 GrTexture*, Direction,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000090 int halfWidth,
91 const float* kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000092 bool useBounds,
93 float bounds[2]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000094
95 /// Convolve with a Gaussian kernel
joshualitt5f10b5c2015-07-09 10:24:35 -070096 GrConvolutionEffect(GrProcessorDataManager*,
97 GrTexture*, Direction,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000098 int halfWidth,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000099 float gaussianSigma,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +0000100 bool useBounds,
101 float bounds[2]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000102
mtklein36352bf2015-03-25 18:17:31 -0700103 bool onIsEqual(const GrFragmentProcessor&) const override;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000104
mtklein36352bf2015-03-25 18:17:31 -0700105 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700106 // If the texture was opaque we could know that the output color if we knew the sum of the
107 // kernel values.
joshualitt56995b52014-12-11 15:44:02 -0800108 inout->mulByUnknownFourComponents();
egdaniel1a8ecdf2014-10-03 06:24:12 -0700109 }
110
joshualittb0a8a372014-09-23 09:50:21 -0700111 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000112
bsalomon@google.comb505a122012-05-31 18:40:36 +0000113 typedef Gr1DKernelEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000114};
115
116#endif