blob: e99e520cf06bb3289be5fa33b1a85f19ed8ddc5b [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.comae4f96a2012-05-18 19:54:48 +000014class GrGLConvolutionEffect;
15
bsalomon@google.comb505a122012-05-31 18:40:36 +000016/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000017 * A convolution effect. The kernel is specified as an array of 2 * half-width
bsalomon@google.comb505a122012-05-31 18:40:36 +000018 * + 1 weights. Each texel is multiplied by it's weight and summed to determine
19 * the output color. The output color is modulated by the input color.
20 */
21class GrConvolutionEffect : public Gr1DKernelEffect {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000022
23public:
24
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +000025 /// Convolve with an arbitrary user-specified kernel
joshualittb0a8a372014-09-23 09:50:21 -070026 static GrFragmentProcessor* Create(GrTexture* tex,
27 Direction dir,
28 int halfWidth,
29 const float* kernel,
30 bool useBounds,
31 float bounds[2]) {
bsalomon55fad7a2014-07-08 07:34:20 -070032 return SkNEW_ARGS(GrConvolutionEffect, (tex,
33 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
joshualittb0a8a372014-09-23 09:50:21 -070041 static GrFragmentProcessor* CreateGaussian(GrTexture* tex,
42 Direction dir,
43 int halfWidth,
44 float gaussianSigma,
45 bool useBounds,
46 float bounds[2]) {
bsalomon55fad7a2014-07-08 07:34:20 -070047 return SkNEW_ARGS(GrConvolutionEffect, (tex,
48 dir,
49 halfWidth,
50 gaussianSigma,
51 useBounds,
52 bounds));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000053 }
54
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000055 virtual ~GrConvolutionEffect();
56
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000057 const float* kernel() const { return fKernel; }
bsalomon@google.com289efe02012-05-21 20:57:59 +000058
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000059 const float* bounds() const { return fBounds; }
60 bool useBounds() const { return fUseBounds; }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000061
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000062 static const char* Name() { return "Convolution"; }
63
joshualittb0a8a372014-09-23 09:50:21 -070064 typedef GrGLConvolutionEffect GLProcessor;
bsalomon@google.com289efe02012-05-21 20:57:59 +000065
joshualittb0a8a372014-09-23 09:50:21 -070066 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000067
bsalomon@google.comb505a122012-05-31 18:40:36 +000068 enum {
69 // This was decided based on the min allowed value for the max texture
70 // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0
71 // on a blur filter gives a kernel width of 25 while a sigma of 5.0
72 // would exceed a 32 wide kernel.
73 kMaxKernelRadius = 12,
74 // With a C++11 we could have a constexpr version of WidthFromRadius()
75 // and not have to duplicate this calculation.
76 kMaxKernelWidth = 2 * kMaxKernelRadius + 1,
77 };
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000078
79protected:
80
bsalomon@google.comb505a122012-05-31 18:40:36 +000081 float fKernel[kMaxKernelWidth];
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000082 bool fUseBounds;
83 float fBounds[2];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000084
85private:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000086 GrConvolutionEffect(GrTexture*, Direction,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000087 int halfWidth,
88 const float* kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000089 bool useBounds,
90 float bounds[2]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000091
92 /// Convolve with a Gaussian kernel
93 GrConvolutionEffect(GrTexture*, Direction,
94 int halfWidth,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000095 float gaussianSigma,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000096 bool useBounds,
97 float bounds[2]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000098
bsalomon0e08fc12014-10-15 08:19:04 -070099 virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000100
egdaniel605dd0f2014-11-12 08:35:25 -0800101 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700102 // If the texture was opaque we could know that the output color if we knew the sum of the
103 // kernel values.
egdanielccb2e382014-10-13 12:53:46 -0700104 inout->mulByUnknownColor();
egdaniel1a8ecdf2014-10-03 06:24:12 -0700105 }
106
joshualittb0a8a372014-09-23 09:50:21 -0700107 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000108
bsalomon@google.comb505a122012-05-31 18:40:36 +0000109 typedef Gr1DKernelEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000110};
111
112#endif