blob: 1d0950bfa58ea401e459c303e5032b2a25042a71 [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
joshualittb0a8a372014-09-23 09:50:21 -070024 static GrFragmentProcessor* Create(GrTexture* tex,
25 Direction dir,
26 int halfWidth,
27 const float* kernel,
28 bool useBounds,
29 float bounds[2]) {
bsalomon55fad7a2014-07-08 07:34:20 -070030 return SkNEW_ARGS(GrConvolutionEffect, (tex,
31 dir,
32 halfWidth,
33 kernel,
34 useBounds,
35 bounds));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000036 }
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +000037
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000038 /// Convolve with a Gaussian kernel
joshualittb0a8a372014-09-23 09:50:21 -070039 static GrFragmentProcessor* CreateGaussian(GrTexture* tex,
40 Direction dir,
41 int halfWidth,
42 float gaussianSigma,
43 bool useBounds,
44 float bounds[2]) {
bsalomon55fad7a2014-07-08 07:34:20 -070045 return SkNEW_ARGS(GrConvolutionEffect, (tex,
46 dir,
47 halfWidth,
48 gaussianSigma,
49 useBounds,
50 bounds));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000051 }
52
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000053 virtual ~GrConvolutionEffect();
54
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000055 const float* kernel() const { return fKernel; }
bsalomon@google.com289efe02012-05-21 20:57:59 +000056
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000057 const float* bounds() const { return fBounds; }
58 bool useBounds() const { return fUseBounds; }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000059
joshualitteb2a6762014-12-04 11:35:33 -080060 virtual const char* name() const SK_OVERRIDE { return "Convolution"; }
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000061
joshualitteb2a6762014-12-04 11:35:33 -080062 virtual void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) const SK_OVERRIDE;
bsalomon@google.com289efe02012-05-21 20:57:59 +000063
joshualitteb2a6762014-12-04 11:35:33 -080064 virtual GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000065
bsalomon@google.comb505a122012-05-31 18:40:36 +000066 enum {
67 // This was decided based on the min allowed value for the max texture
68 // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0
69 // on a blur filter gives a kernel width of 25 while a sigma of 5.0
70 // would exceed a 32 wide kernel.
71 kMaxKernelRadius = 12,
72 // With a C++11 we could have a constexpr version of WidthFromRadius()
73 // and not have to duplicate this calculation.
74 kMaxKernelWidth = 2 * kMaxKernelRadius + 1,
75 };
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000076
77protected:
78
bsalomon@google.comb505a122012-05-31 18:40:36 +000079 float fKernel[kMaxKernelWidth];
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000080 bool fUseBounds;
81 float fBounds[2];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000082
83private:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000084 GrConvolutionEffect(GrTexture*, Direction,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000085 int halfWidth,
86 const float* kernel,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000087 bool useBounds,
88 float bounds[2]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000089
90 /// Convolve with a Gaussian kernel
91 GrConvolutionEffect(GrTexture*, Direction,
92 int halfWidth,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000093 float gaussianSigma,
senorblanco@chromium.orge8232bc2013-07-29 18:45:44 +000094 bool useBounds,
95 float bounds[2]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000096
bsalomon0e08fc12014-10-15 08:19:04 -070097 virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000098
egdaniel605dd0f2014-11-12 08:35:25 -080099 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700100 // If the texture was opaque we could know that the output color if we knew the sum of the
101 // kernel values.
joshualitt56995b52014-12-11 15:44:02 -0800102 inout->mulByUnknownFourComponents();
egdaniel1a8ecdf2014-10-03 06:24:12 -0700103 }
104
joshualittb0a8a372014-09-23 09:50:21 -0700105 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000106
bsalomon@google.comb505a122012-05-31 18:40:36 +0000107 typedef Gr1DKernelEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000108};
109
110#endif