blob: 6c55656bc4dd5fa5463c21f9a2040549c556f132 [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"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000012
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000013class GrGLConvolutionEffect;
14
bsalomon@google.comb505a122012-05-31 18:40:36 +000015/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000016 * A convolution effect. The kernel is specified as an array of 2 * half-width
bsalomon@google.comb505a122012-05-31 18:40:36 +000017 * + 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 */
20class GrConvolutionEffect : public Gr1DKernelEffect {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000021
22public:
23
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +000024 /// Convolve with an arbitrary user-specified kernel
joshualittb0a8a372014-09-23 09:50:21 -070025 static GrFragmentProcessor* Create(GrTexture* tex,
26 Direction dir,
27 int halfWidth,
28 const float* kernel,
29 bool useBounds,
30 float bounds[2]) {
bsalomon55fad7a2014-07-08 07:34:20 -070031 return SkNEW_ARGS(GrConvolutionEffect, (tex,
32 dir,
33 halfWidth,
34 kernel,
35 useBounds,
36 bounds));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000037 }
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +000038
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000039 /// Convolve with a Gaussian kernel
joshualittb0a8a372014-09-23 09:50:21 -070040 static GrFragmentProcessor* CreateGaussian(GrTexture* tex,
41 Direction dir,
42 int halfWidth,
43 float gaussianSigma,
44 bool useBounds,
45 float bounds[2]) {
bsalomon55fad7a2014-07-08 07:34:20 -070046 return SkNEW_ARGS(GrConvolutionEffect, (tex,
47 dir,
48 halfWidth,
49 gaussianSigma,
50 useBounds,
51 bounds));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000052 }
53
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000054 virtual ~GrConvolutionEffect();
55
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000056 const float* kernel() const { return fKernel; }
bsalomon@google.com289efe02012-05-21 20:57:59 +000057
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000058 const float* bounds() const { return fBounds; }
59 bool useBounds() const { return fUseBounds; }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000060
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000061 static const char* Name() { return "Convolution"; }
62
joshualittb0a8a372014-09-23 09:50:21 -070063 typedef GrGLConvolutionEffect GLProcessor;
bsalomon@google.com289efe02012-05-21 20:57:59 +000064
joshualittb0a8a372014-09-23 09:50:21 -070065 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000066
bsalomon@google.comb505a122012-05-31 18:40:36 +000067 enum {
68 // This was decided based on the min allowed value for the max texture
69 // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0
70 // on a blur filter gives a kernel width of 25 while a sigma of 5.0
71 // would exceed a 32 wide kernel.
72 kMaxKernelRadius = 12,
73 // With a C++11 we could have a constexpr version of WidthFromRadius()
74 // and not have to duplicate this calculation.
75 kMaxKernelWidth = 2 * kMaxKernelRadius + 1,
76 };
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000077
78protected:
79
bsalomon@google.comb505a122012-05-31 18:40:36 +000080 float fKernel[kMaxKernelWidth];
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000081 bool fUseBounds;
82 float fBounds[2];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000083
84private:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000085 GrConvolutionEffect(GrTexture*, Direction,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000086 int halfWidth,
87 const float* kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000088 bool useBounds,
89 float bounds[2]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000090
91 /// Convolve with a Gaussian kernel
92 GrConvolutionEffect(GrTexture*, Direction,
93 int halfWidth,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000094 float gaussianSigma,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000095 bool useBounds,
96 float bounds[2]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000097
joshualittb0a8a372014-09-23 09:50:21 -070098 virtual bool onIsEqual(const GrProcessor&) const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000099
egdaniel1a8ecdf2014-10-03 06:24:12 -0700100 virtual void onComputeInvariantOutput(InvariantOutput* inout) const {
101 // If the texture was opaque we could know that the output color if we knew the sum of the
102 // kernel values.
egdanielccb2e382014-10-13 12:53:46 -0700103 inout->mulByUnknownColor();
egdaniel1a8ecdf2014-10-03 06:24:12 -0700104 }
105
joshualittb0a8a372014-09-23 09:50:21 -0700106 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000107
bsalomon@google.comb505a122012-05-31 18:40:36 +0000108 typedef Gr1DKernelEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000109};
110
111#endif