Implement morphology as a custom effect

Review URL: http://codereview.appspot.com/6250073/



git-svn-id: http://skia.googlecode.com/svn/trunk@4102 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/effects/GrConvolutionEffect.h b/src/gpu/effects/GrConvolutionEffect.h
index df164d5..fd6883b 100644
--- a/src/gpu/effects/GrConvolutionEffect.h
+++ b/src/gpu/effects/GrConvolutionEffect.h
@@ -8,40 +8,58 @@
 #ifndef GrConvolutionEffect_DEFINED
 #define GrConvolutionEffect_DEFINED
 
-#include "GrCustomStage.h"
-#include "GrSamplerState.h" // for MAX_KENEL_WIDTH, FilterDirection
+#include "Gr1DKernelEffect.h"
 
 class GrGLConvolutionEffect;
 
-class GrConvolutionEffect : public GrCustomStage {
+/**
+ * A convolution effect. The kernel is specified as an array of 2 * half-width 
+ * + 1 weights. Each texel is multiplied by it's weight and summed to determine
+ * the output color. The output color is modulated by the input color.
+ */
+class GrConvolutionEffect : public Gr1DKernelEffect {
 
 public:
 
-    GrConvolutionEffect(GrSamplerState::FilterDirection direction,
-                        unsigned int kernelWidth, const float* kernel);
+    GrConvolutionEffect(Direction, int halfWidth, const float* kernel = NULL);
     virtual ~GrConvolutionEffect();
 
-    unsigned int width() const { return fKernelWidth; }
+    void setKernel(const float* kernel) {
+        memcpy(fKernel, kernel, this->width());
+    }
+
+    /**
+     * Helper to set the kernel to a Gaussian. Replaces the existing kernel.
+     */
+    void setGaussianKernel(float sigma);
+
     const float* kernel() const { return fKernel; }
-    GrSamplerState::FilterDirection direction() const { return fDirection; }
 
     static const char* Name() { return "Convolution"; }
 
     typedef GrGLConvolutionEffect GLProgramStage;
 
     virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE;
-    virtual bool isEqual(const GrCustomStage *) const SK_OVERRIDE;
+    virtual bool isEqual(const GrCustomStage&) const SK_OVERRIDE;
+
+    enum {
+        // This was decided based on the min allowed value for the max texture
+        // samples per fragment program run in DX9SM2 (32). A sigma param of 4.0
+        // on a blur filter gives a kernel width of 25 while a sigma of 5.0
+        // would exceed a 32 wide kernel.
+        kMaxKernelRadius = 12,
+        // With a C++11 we could have a constexpr version of WidthFromRadius()
+        // and not have to duplicate this calculation.
+        kMaxKernelWidth = 2 * kMaxKernelRadius + 1,
+    };
 
 protected:
 
-    GrSamplerState::FilterDirection fDirection;
-    unsigned int fKernelWidth;
-    float fKernel[MAX_KERNEL_WIDTH];
-
+    float fKernel[kMaxKernelWidth];
 
 private:
 
-    typedef GrCustomStage INHERITED;
+    typedef Gr1DKernelEffect INHERITED;
 };
 
 #endif