Implement crop rect for the dilate and erode (morphology) filters. This provoked some cleanup on the GPU side: apply_morphology() now deals with SkBitmaps, rather than GrTextures. There's still a clear opportunity for more refactoring between the two filters.

Note: this adds some test cases to the morphology GM, so it will require a rebaseline.

R=bsalomon@google.com, reed@google.com

Review URL: https://codereview.chromium.org/23892011

git-svn-id: http://skia.googlecode.com/svn/trunk@11313 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gm/morphology.cpp b/gm/morphology.cpp
index 2d29fcd..2150689 100644
--- a/gm/morphology.cpp
+++ b/gm/morphology.cpp
@@ -8,8 +8,8 @@
 #include "gm.h"
 #include "SkMorphologyImageFilter.h"
 
-#define WIDTH 640
-#define HEIGHT 480
+#define WIDTH 700
+#define HEIGHT 560
 
 namespace skiagm {
 
@@ -44,6 +44,16 @@
     virtual SkISize onISize() {
         return make_isize(WIDTH, HEIGHT);
     }
+
+    void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
+        canvas->save();
+        canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
+        canvas->clipRect(SkRect::MakeWH(
+          SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
+        canvas->drawBitmap(fBitmap, 0, 0, &paint);
+        canvas->restore();
+    }
+
     virtual void onDraw(SkCanvas* canvas) {
         if (!fOnce) {
             make_bitmap();
@@ -60,26 +70,25 @@
             {  24,  24,  25,  25 },
         };
         SkPaint paint;
-        for (unsigned j = 0; j < 2; ++j) {
+        SkIRect cropRect = SkIRect::MakeXYWH(25, 20, 100, 80);
+
+        for (unsigned j = 0; j < 4; ++j) {
             for (unsigned i = 0; i < SK_ARRAY_COUNT(samples); ++i) {
-                SkScalar x = SkIntToScalar(i * 140), y = SkIntToScalar(j * 140);
-                if (j) {
+                const SkIRect* cr = j & 0x02 ? &cropRect : NULL;
+                if (j & 0x01) {
                     paint.setImageFilter(new SkErodeImageFilter(
                         samples[i].fRadiusX,
-                        samples[i].fRadiusY))->unref();
+                        samples[i].fRadiusY,
+                        NULL,
+                        cr))->unref();
                 } else {
                     paint.setImageFilter(new SkDilateImageFilter(
                         samples[i].fRadiusX,
-                        samples[i].fRadiusY))->unref();
+                        samples[i].fRadiusY,
+                        NULL,
+                        cr))->unref();
                 }
-                SkRect bounds = SkRect::MakeXYWH(
-                    x,
-                    y,
-                    SkIntToScalar(samples[i].fWidth),
-                    SkIntToScalar(samples[i].fHeight));
-                canvas->saveLayer(&bounds, &paint);
-                canvas->drawBitmap(fBitmap, x, y);
-                canvas->restore();
+                drawClippedBitmap(canvas, paint, i * 140, j * 140);
             }
         }
     }