blob: ff661b2adb4a916620cc12237988c3c038470171 [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
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000025 static GrEffectRef* Create(GrTexture* tex, Direction dir, int halfWidth, const float* kernel) {
26 SkAutoTUnref<GrEffect> effect(SkNEW_ARGS(GrConvolutionEffect, (tex,
27 dir,
28 halfWidth,
29 kernel)));
30 return CreateEffectPtr(effect);
31 }
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +000032
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000033 /// Convolve with a Gaussian kernel
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000034 static GrEffectRef* Create(GrTexture* tex,
35 Direction dir,
36 int halfWidth,
37 float gaussianSigma) {
38 SkAutoTUnref<GrEffect> effect(SkNEW_ARGS(GrConvolutionEffect, (tex,
39 dir,
40 halfWidth,
41 gaussianSigma)));
42 return CreateEffectPtr(effect);
43 }
44
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000045 virtual ~GrConvolutionEffect();
46
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000047 const float* kernel() const { return fKernel; }
bsalomon@google.com289efe02012-05-21 20:57:59 +000048
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000049 static const char* Name() { return "Convolution"; }
50
bsalomon@google.com422e81a2012-10-25 14:11:03 +000051 typedef GrGLConvolutionEffect GLEffect;
bsalomon@google.com289efe02012-05-21 20:57:59 +000052
bsalomon@google.com396e61f2012-10-25 19:00:29 +000053 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
bsalomon@google.coma469c282012-10-24 18:28:34 +000054 virtual bool isEqual(const GrEffect&) const SK_OVERRIDE;
bsalomon@google.comb505a122012-05-31 18:40:36 +000055
56 enum {
57 // This was decided based on the min allowed value for the max texture
58 // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0
59 // on a blur filter gives a kernel width of 25 while a sigma of 5.0
60 // would exceed a 32 wide kernel.
61 kMaxKernelRadius = 12,
62 // With a C++11 we could have a constexpr version of WidthFromRadius()
63 // and not have to duplicate this calculation.
64 kMaxKernelWidth = 2 * kMaxKernelRadius + 1,
65 };
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000066
67protected:
68
bsalomon@google.comb505a122012-05-31 18:40:36 +000069 float fKernel[kMaxKernelWidth];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000070
71private:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000072 GrConvolutionEffect(GrTexture*, Direction,
73 int halfWidth, const float* kernel);
74
75 /// Convolve with a Gaussian kernel
76 GrConvolutionEffect(GrTexture*, Direction,
77 int halfWidth,
78 float gaussianSigma);
79
bsalomon@google.comf271cc72012-10-24 19:35:13 +000080 GR_DECLARE_EFFECT_TEST;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000081
bsalomon@google.comb505a122012-05-31 18:40:36 +000082 typedef Gr1DKernelEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000083};
84
85#endif