blob: b114abed61a7433bea84c94f9b5334d80d5e621b [file] [log] [blame]
joshualittac977922014-07-22 09:52:11 -07001/*
2 * Copyright 2014 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 GrMatrixConvolutionEffect_DEFINED
9#define GrMatrixConvolutionEffect_DEFINED
10
11#include "GrSingleTextureEffect.h"
egdaniel605dd0f2014-11-12 08:35:25 -080012#include "GrInvariantOutput.h"
joshualitt5ae5fc52014-07-29 12:59:27 -070013#include "GrTextureDomain.h"
joshualittac977922014-07-22 09:52:11 -070014
15// A little bit less than the minimum # uniforms required by DX9SM2 (32).
16// Allows for a 5x5 kernel (or 25x1, for that matter).
17#define MAX_KERNEL_SIZE 25
18
joshualittac977922014-07-22 09:52:11 -070019class GrMatrixConvolutionEffect : public GrSingleTextureEffect {
20public:
bsalomon4a339522015-10-06 08:40:50 -070021 static GrFragmentProcessor* Create(GrTexture* texture,
joshualittb0a8a372014-09-23 09:50:21 -070022 const SkIRect& bounds,
23 const SkISize& kernelSize,
24 const SkScalar* kernel,
25 SkScalar gain,
26 SkScalar bias,
27 const SkIPoint& kernelOffset,
28 GrTextureDomain::Mode tileMode,
29 bool convolveAlpha) {
bsalomon4a339522015-10-06 08:40:50 -070030 return new GrMatrixConvolutionEffect(texture, bounds, kernelSize, kernel, gain, bias,
31 kernelOffset, tileMode, convolveAlpha);
joshualittac977922014-07-22 09:52:11 -070032 }
joshualitt5acfea72014-08-11 13:55:34 -070033
bsalomon4a339522015-10-06 08:40:50 -070034 static GrFragmentProcessor* CreateGaussian(GrTexture* texture,
joshualittb0a8a372014-09-23 09:50:21 -070035 const SkIRect& bounds,
36 const SkISize& kernelSize,
37 SkScalar gain,
38 SkScalar bias,
39 const SkIPoint& kernelOffset,
40 GrTextureDomain::Mode tileMode,
41 bool convolveAlpha,
42 SkScalar sigmaX,
43 SkScalar sigmaY);
joshualitt5acfea72014-08-11 13:55:34 -070044
joshualittac977922014-07-22 09:52:11 -070045 virtual ~GrMatrixConvolutionEffect();
46
joshualittac977922014-07-22 09:52:11 -070047 const SkIRect& bounds() const { return fBounds; }
48 const SkISize& kernelSize() const { return fKernelSize; }
49 const float* kernelOffset() const { return fKernelOffset; }
50 const float* kernel() const { return fKernel; }
51 float gain() const { return fGain; }
52 float bias() const { return fBias; }
joshualittac977922014-07-22 09:52:11 -070053 bool convolveAlpha() const { return fConvolveAlpha; }
joshualitt5ae5fc52014-07-29 12:59:27 -070054 const GrTextureDomain& domain() const { return fDomain; }
joshualittac977922014-07-22 09:52:11 -070055
mtklein36352bf2015-03-25 18:17:31 -070056 const char* name() const override { return "MatrixConvolution"; }
joshualittac977922014-07-22 09:52:11 -070057
joshualittac977922014-07-22 09:52:11 -070058private:
bsalomon4a339522015-10-06 08:40:50 -070059 GrMatrixConvolutionEffect(GrTexture*,
joshualittac977922014-07-22 09:52:11 -070060 const SkIRect& bounds,
61 const SkISize& kernelSize,
62 const SkScalar* kernel,
63 SkScalar gain,
64 SkScalar bias,
65 const SkIPoint& kernelOffset,
joshualitt5ae5fc52014-07-29 12:59:27 -070066 GrTextureDomain::Mode tileMode,
joshualittac977922014-07-22 09:52:11 -070067 bool convolveAlpha);
68
egdaniel57d3b032015-11-13 11:57:27 -080069 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -070070
egdaniel57d3b032015-11-13 11:57:27 -080071 void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -070072
mtklein36352bf2015-03-25 18:17:31 -070073 bool onIsEqual(const GrFragmentProcessor&) const override;
joshualittac977922014-07-22 09:52:11 -070074
mtklein36352bf2015-03-25 18:17:31 -070075 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
egdaniel1a8ecdf2014-10-03 06:24:12 -070076 // TODO: Try to do better?
joshualitt56995b52014-12-11 15:44:02 -080077 inout->mulByUnknownFourComponents();
egdaniel1a8ecdf2014-10-03 06:24:12 -070078 }
79
joshualitt5ae5fc52014-07-29 12:59:27 -070080 SkIRect fBounds;
81 SkISize fKernelSize;
joshualitt5acfea72014-08-11 13:55:34 -070082 float fKernel[MAX_KERNEL_SIZE];
joshualitt5ae5fc52014-07-29 12:59:27 -070083 float fGain;
84 float fBias;
85 float fKernelOffset[2];
86 bool fConvolveAlpha;
87 GrTextureDomain fDomain;
joshualittac977922014-07-22 09:52:11 -070088
joshualittb0a8a372014-09-23 09:50:21 -070089 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
joshualittac977922014-07-22 09:52:11 -070090
91 typedef GrSingleTextureEffect INHERITED;
92};
93
94#endif