blob: 6d60609e314af8dff72644116f024c63aa3b45b0 [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:
joshualittb0a8a372014-09-23 09:50:21 -070021 static GrFragmentProcessor* Create(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) {
joshualittac977922014-07-22 09:52:11 -070030 return SkNEW_ARGS(GrMatrixConvolutionEffect, (texture,
31 bounds,
32 kernelSize,
33 kernel,
34 gain,
35 bias,
36 kernelOffset,
37 tileMode,
38 convolveAlpha));
39 }
joshualitt5acfea72014-08-11 13:55:34 -070040
joshualittb0a8a372014-09-23 09:50:21 -070041 static GrFragmentProcessor* CreateGaussian(GrTexture* texture,
42 const SkIRect& bounds,
43 const SkISize& kernelSize,
44 SkScalar gain,
45 SkScalar bias,
46 const SkIPoint& kernelOffset,
47 GrTextureDomain::Mode tileMode,
48 bool convolveAlpha,
49 SkScalar sigmaX,
50 SkScalar sigmaY);
joshualitt5acfea72014-08-11 13:55:34 -070051
joshualittac977922014-07-22 09:52:11 -070052 virtual ~GrMatrixConvolutionEffect();
53
joshualittac977922014-07-22 09:52:11 -070054 const SkIRect& bounds() const { return fBounds; }
55 const SkISize& kernelSize() const { return fKernelSize; }
56 const float* kernelOffset() const { return fKernelOffset; }
57 const float* kernel() const { return fKernel; }
58 float gain() const { return fGain; }
59 float bias() const { return fBias; }
joshualittac977922014-07-22 09:52:11 -070060 bool convolveAlpha() const { return fConvolveAlpha; }
joshualitt5ae5fc52014-07-29 12:59:27 -070061 const GrTextureDomain& domain() const { return fDomain; }
joshualittac977922014-07-22 09:52:11 -070062
joshualitteb2a6762014-12-04 11:35:33 -080063 virtual const char* name() const SK_OVERRIDE { return "MatrixConvolution"; }
joshualittac977922014-07-22 09:52:11 -070064
joshualitteb2a6762014-12-04 11:35:33 -080065 virtual void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) const SK_OVERRIDE;
66
67 virtual GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE;
joshualittac977922014-07-22 09:52:11 -070068
69private:
70 GrMatrixConvolutionEffect(GrTexture*,
71 const SkIRect& bounds,
72 const SkISize& kernelSize,
73 const SkScalar* kernel,
74 SkScalar gain,
75 SkScalar bias,
76 const SkIPoint& kernelOffset,
joshualitt5ae5fc52014-07-29 12:59:27 -070077 GrTextureDomain::Mode tileMode,
joshualittac977922014-07-22 09:52:11 -070078 bool convolveAlpha);
79
bsalomon0e08fc12014-10-15 08:19:04 -070080 virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE;
joshualittac977922014-07-22 09:52:11 -070081
egdaniel605dd0f2014-11-12 08:35:25 -080082 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE {
egdaniel1a8ecdf2014-10-03 06:24:12 -070083 // TODO: Try to do better?
egdanielccb2e382014-10-13 12:53:46 -070084 inout->mulByUnknownColor();
egdaniel1a8ecdf2014-10-03 06:24:12 -070085 }
86
joshualitt5ae5fc52014-07-29 12:59:27 -070087 SkIRect fBounds;
88 SkISize fKernelSize;
joshualitt5acfea72014-08-11 13:55:34 -070089 float fKernel[MAX_KERNEL_SIZE];
joshualitt5ae5fc52014-07-29 12:59:27 -070090 float fGain;
91 float fBias;
92 float fKernelOffset[2];
93 bool fConvolveAlpha;
94 GrTextureDomain fDomain;
joshualittac977922014-07-22 09:52:11 -070095
joshualittb0a8a372014-09-23 09:50:21 -070096 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
joshualittac977922014-07-22 09:52:11 -070097
98 typedef GrSingleTextureEffect INHERITED;
99};
100
101#endif