blob: fd6883b430163726f0d76d417b077ac3559942b4 [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
bsalomon@google.comb505a122012-05-31 18:40:36 +000024 GrConvolutionEffect(Direction, int halfWidth, const float* kernel = NULL);
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000025 virtual ~GrConvolutionEffect();
26
bsalomon@google.comb505a122012-05-31 18:40:36 +000027 void setKernel(const float* kernel) {
28 memcpy(fKernel, kernel, this->width());
29 }
30
31 /**
32 * Helper to set the kernel to a Gaussian. Replaces the existing kernel.
33 */
34 void setGaussianKernel(float sigma);
35
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000036 const float* kernel() const { return fKernel; }
bsalomon@google.com289efe02012-05-21 20:57:59 +000037
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000038 static const char* Name() { return "Convolution"; }
39
40 typedef GrGLConvolutionEffect GLProgramStage;
bsalomon@google.com289efe02012-05-21 20:57:59 +000041
bsalomon@google.comae4f96a2012-05-18 19:54:48 +000042 virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE;
bsalomon@google.comb505a122012-05-31 18:40:36 +000043 virtual bool isEqual(const GrCustomStage&) const SK_OVERRIDE;
44
45 enum {
46 // This was decided based on the min allowed value for the max texture
47 // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0
48 // on a blur filter gives a kernel width of 25 while a sigma of 5.0
49 // would exceed a 32 wide kernel.
50 kMaxKernelRadius = 12,
51 // With a C++11 we could have a constexpr version of WidthFromRadius()
52 // and not have to duplicate this calculation.
53 kMaxKernelWidth = 2 * kMaxKernelRadius + 1,
54 };
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000055
56protected:
57
bsalomon@google.comb505a122012-05-31 18:40:36 +000058 float fKernel[kMaxKernelWidth];
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000059
60private:
61
bsalomon@google.comb505a122012-05-31 18:40:36 +000062 typedef Gr1DKernelEffect INHERITED;
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000063};
64
65#endif