blob: 066da6568140fd02049ca0fc6b32df55905159e5 [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 const SkIRect& bounds() const { return fBounds; }
46 const SkISize& kernelSize() const { return fKernelSize; }
47 const float* kernelOffset() const { return fKernelOffset; }
48 const float* kernel() const { return fKernel; }
49 float gain() const { return fGain; }
50 float bias() const { return fBias; }
joshualittac977922014-07-22 09:52:11 -070051 bool convolveAlpha() const { return fConvolveAlpha; }
joshualitt5ae5fc52014-07-29 12:59:27 -070052 const GrTextureDomain& domain() const { return fDomain; }
joshualittac977922014-07-22 09:52:11 -070053
mtklein36352bf2015-03-25 18:17:31 -070054 const char* name() const override { return "MatrixConvolution"; }
joshualittac977922014-07-22 09:52:11 -070055
joshualittac977922014-07-22 09:52:11 -070056private:
bsalomon4a339522015-10-06 08:40:50 -070057 GrMatrixConvolutionEffect(GrTexture*,
joshualittac977922014-07-22 09:52:11 -070058 const SkIRect& bounds,
59 const SkISize& kernelSize,
60 const SkScalar* kernel,
61 SkScalar gain,
62 SkScalar bias,
63 const SkIPoint& kernelOffset,
joshualitt5ae5fc52014-07-29 12:59:27 -070064 GrTextureDomain::Mode tileMode,
joshualittac977922014-07-22 09:52:11 -070065 bool convolveAlpha);
66
egdaniel57d3b032015-11-13 11:57:27 -080067 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -070068
egdaniel57d3b032015-11-13 11:57:27 -080069 void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -070070
mtklein36352bf2015-03-25 18:17:31 -070071 bool onIsEqual(const GrFragmentProcessor&) const override;
joshualittac977922014-07-22 09:52:11 -070072
mtklein36352bf2015-03-25 18:17:31 -070073 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
egdaniel1a8ecdf2014-10-03 06:24:12 -070074 // TODO: Try to do better?
joshualitt56995b52014-12-11 15:44:02 -080075 inout->mulByUnknownFourComponents();
egdaniel1a8ecdf2014-10-03 06:24:12 -070076 }
77
joshualitt5ae5fc52014-07-29 12:59:27 -070078 SkIRect fBounds;
79 SkISize fKernelSize;
joshualitt5acfea72014-08-11 13:55:34 -070080 float fKernel[MAX_KERNEL_SIZE];
joshualitt5ae5fc52014-07-29 12:59:27 -070081 float fGain;
82 float fBias;
83 float fKernelOffset[2];
84 bool fConvolveAlpha;
85 GrTextureDomain fDomain;
joshualittac977922014-07-22 09:52:11 -070086
joshualittb0a8a372014-09-23 09:50:21 -070087 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
joshualittac977922014-07-22 09:52:11 -070088
89 typedef GrSingleTextureEffect INHERITED;
90};
91
92#endif