blob: 58cad83b535670cf08227f33ae30cb692f2a8750 [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/**
16 * A convolution effect. The kernel is specified as an array of 2 * half-width
17 * + 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.comd0c1a062012-07-12 17:23:52 +000024 GrConvolutionEffect(GrTexture*, Direction,
25 int halfWidth, const float* kernel = NULL);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000026 virtual ~GrConvolutionEffect();
27
bsalomon@google.comb505a122012-05-31 18:40:36 +000028 void setKernel(const float* kernel) {
29 memcpy(fKernel, kernel, this->width());
30 }
31
32 /**
33 * Helper to set the kernel to a Gaussian. Replaces the existing kernel.
34 */
35 void setGaussianKernel(float sigma);
36
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000037 const float* kernel() const { return fKernel; }
bsalomon@google.com289efe02012-05-21 20:57:59 +000038
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000039 static const char* Name() { return "Convolution"; }
40
41 typedef GrGLConvolutionEffect GLProgramStage;
bsalomon@google.com289efe02012-05-21 20:57:59 +000042
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000043 virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE;
bsalomon@google.comb505a122012-05-31 18:40:36 +000044 virtual bool isEqual(const GrCustomStage&) const SK_OVERRIDE;
45
46 enum {
47 // This was decided based on the min allowed value for the max texture
48 // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0
49 // on a blur filter gives a kernel width of 25 while a sigma of 5.0
50 // would exceed a 32 wide kernel.
51 kMaxKernelRadius = 12,
52 // With a C++11 we could have a constexpr version of WidthFromRadius()
53 // and not have to duplicate this calculation.
54 kMaxKernelWidth = 2 * kMaxKernelRadius + 1,
55 };
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000056
57protected:
58
bsalomon@google.comb505a122012-05-31 18:40:36 +000059 float fKernel[kMaxKernelWidth];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000060
61private:
62
bsalomon@google.comb505a122012-05-31 18:40:36 +000063 typedef Gr1DKernelEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000064};
65
66#endif