blob: b8df437681252ba42f8324f8c693fb595667601f [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:
bungeman06ca8ec2016-06-09 08:01:03 -070021 static sk_sp<GrFragmentProcessor> Make(GrTexture* texture,
22 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) {
30 return sk_sp<GrFragmentProcessor>(
31 new GrMatrixConvolutionEffect(texture, bounds, kernelSize, kernel, gain, bias,
32 kernelOffset, tileMode, convolveAlpha));
joshualittac977922014-07-22 09:52:11 -070033 }
joshualitt5acfea72014-08-11 13:55:34 -070034
bungeman06ca8ec2016-06-09 08:01:03 -070035 static sk_sp<GrFragmentProcessor> MakeGaussian(GrTexture* texture,
36 const SkIRect& bounds,
37 const SkISize& kernelSize,
38 SkScalar gain,
39 SkScalar bias,
40 const SkIPoint& kernelOffset,
41 GrTextureDomain::Mode tileMode,
42 bool convolveAlpha,
43 SkScalar sigmaX,
44 SkScalar sigmaY);
joshualitt5acfea72014-08-11 13:55:34 -070045
joshualittac977922014-07-22 09:52:11 -070046 const SkIRect& bounds() const { return fBounds; }
47 const SkISize& kernelSize() const { return fKernelSize; }
48 const float* kernelOffset() const { return fKernelOffset; }
49 const float* kernel() const { return fKernel; }
50 float gain() const { return fGain; }
51 float bias() const { return fBias; }
joshualittac977922014-07-22 09:52:11 -070052 bool convolveAlpha() const { return fConvolveAlpha; }
joshualitt5ae5fc52014-07-29 12:59:27 -070053 const GrTextureDomain& domain() const { return fDomain; }
joshualittac977922014-07-22 09:52:11 -070054
mtklein36352bf2015-03-25 18:17:31 -070055 const char* name() const override { return "MatrixConvolution"; }
joshualittac977922014-07-22 09:52:11 -070056
joshualittac977922014-07-22 09:52:11 -070057private:
bsalomon4a339522015-10-06 08:40:50 -070058 GrMatrixConvolutionEffect(GrTexture*,
joshualittac977922014-07-22 09:52:11 -070059 const SkIRect& bounds,
60 const SkISize& kernelSize,
61 const SkScalar* kernel,
62 SkScalar gain,
63 SkScalar bias,
64 const SkIPoint& kernelOffset,
joshualitt5ae5fc52014-07-29 12:59:27 -070065 GrTextureDomain::Mode tileMode,
joshualittac977922014-07-22 09:52:11 -070066 bool convolveAlpha);
67
egdaniel57d3b032015-11-13 11:57:27 -080068 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -070069
egdaniel57d3b032015-11-13 11:57:27 -080070 void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -070071
mtklein36352bf2015-03-25 18:17:31 -070072 bool onIsEqual(const GrFragmentProcessor&) const override;
joshualittac977922014-07-22 09:52:11 -070073
mtklein36352bf2015-03-25 18:17:31 -070074 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
egdaniel1a8ecdf2014-10-03 06:24:12 -070075 // TODO: Try to do better?
joshualitt56995b52014-12-11 15:44:02 -080076 inout->mulByUnknownFourComponents();
egdaniel1a8ecdf2014-10-03 06:24:12 -070077 }
78
joshualitt5ae5fc52014-07-29 12:59:27 -070079 SkIRect fBounds;
80 SkISize fKernelSize;
joshualitt5acfea72014-08-11 13:55:34 -070081 float fKernel[MAX_KERNEL_SIZE];
joshualitt5ae5fc52014-07-29 12:59:27 -070082 float fGain;
83 float fBias;
84 float fKernelOffset[2];
85 bool fConvolveAlpha;
86 GrTextureDomain fDomain;
joshualittac977922014-07-22 09:52:11 -070087
joshualittb0a8a372014-09-23 09:50:21 -070088 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
joshualittac977922014-07-22 09:52:11 -070089
90 typedef GrSingleTextureEffect INHERITED;
91};
92
93#endif