blob: 265150a92c2504359744200dbb4190b002bf289a [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"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000012
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000013class GrGLConvolutionEffect;
14
bsalomon@google.comb505a122012-05-31 18:40:36 +000015/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000016 * A convolution effect. The kernel is specified as an array of 2 * half-width
bsalomon@google.comb505a122012-05-31 18:40:36 +000017 * + 1 weights. Each texel is multiplied by it's weight and summed to determine
18 * the output color. The output color is modulated by the input color.
19 */
20class GrConvolutionEffect : public Gr1DKernelEffect {
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000021
22public:
23
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +000024 /// Convolve with an arbitrary user-specified kernel
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000025 static GrEffectRef* Create(GrTexture* tex,
26 Direction dir,
27 int halfWidth,
28 const float* kernel,
29 bool useCropRect,
30 float cropRect[4]) {
bsalomon@google.com6340a412013-01-22 19:55:59 +000031 AutoEffectUnref effect(SkNEW_ARGS(GrConvolutionEffect, (tex,
32 dir,
33 halfWidth,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000034 kernel,
35 useCropRect,
36 cropRect)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +000037 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000038 }
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +000039
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000040 /// Convolve with a Gaussian kernel
bsalomon@google.comc5fae9e2013-01-24 19:00:02 +000041 static GrEffectRef* CreateGaussian(GrTexture* tex,
bsalomon@google.com6340a412013-01-22 19:55:59 +000042 Direction dir,
43 int halfWidth,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000044 float gaussianSigma,
45 bool useCropRect,
46 float cropRect[4]) {
bsalomon@google.com6340a412013-01-22 19:55:59 +000047 AutoEffectUnref effect(SkNEW_ARGS(GrConvolutionEffect, (tex,
48 dir,
49 halfWidth,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000050 gaussianSigma,
51 useCropRect,
52 cropRect)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +000053 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000054 }
55
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000056 virtual ~GrConvolutionEffect();
57
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000058 const float* kernel() const { return fKernel; }
bsalomon@google.com289efe02012-05-21 20:57:59 +000059
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000060 const float* cropRect() const { return fCropRect; }
61 bool useCropRect() const { return fUseCropRect; }
62
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000063 static const char* Name() { return "Convolution"; }
64
bsalomon@google.com422e81a2012-10-25 14:11:03 +000065 typedef GrGLConvolutionEffect GLEffect;
bsalomon@google.com289efe02012-05-21 20:57:59 +000066
bsalomon@google.com396e61f2012-10-25 19:00:29 +000067 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000068
sugoi@google.come0e385c2013-03-11 18:50:03 +000069 virtual void getConstantColorComponents(GrColor*, uint32_t* validFlags) const {
bsalomon@google.com68b58c92013-01-17 16:50:08 +000070 // If the texture was opaque we could know that the output color if we knew the sum of the
71 // kernel values.
72 *validFlags = 0;
73 }
bsalomon@google.comb505a122012-05-31 18:40:36 +000074
75 enum {
76 // This was decided based on the min allowed value for the max texture
77 // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0
78 // on a blur filter gives a kernel width of 25 while a sigma of 5.0
79 // would exceed a 32 wide kernel.
80 kMaxKernelRadius = 12,
81 // With a C++11 we could have a constexpr version of WidthFromRadius()
82 // and not have to duplicate this calculation.
83 kMaxKernelWidth = 2 * kMaxKernelRadius + 1,
84 };
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000085
86protected:
87
bsalomon@google.comb505a122012-05-31 18:40:36 +000088 float fKernel[kMaxKernelWidth];
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000089 bool fUseCropRect;
90 float fCropRect[4];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000091
92private:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000093 GrConvolutionEffect(GrTexture*, Direction,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000094 int halfWidth,
95 const float* kernel,
96 bool useCropRect,
97 float cropRect[4]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000098
99 /// Convolve with a Gaussian kernel
100 GrConvolutionEffect(GrTexture*, Direction,
101 int halfWidth,
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000102 float gaussianSigma,
103 bool useCropRect,
104 float cropRect[4]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000105
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000106 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000107
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000108 GR_DECLARE_EFFECT_TEST;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000109
bsalomon@google.comb505a122012-05-31 18:40:36 +0000110 typedef Gr1DKernelEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000111};
112
113#endif