Revert "Implement support for expanding crop rects in image filters"

Breaking ImageFilterTests unit test.

TBR=bsalomon@google.com
BUG=skia:

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

git-svn-id: http://skia.googlecode.com/svn/trunk@13806 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/core/SkImageFilter.cpp b/src/core/SkImageFilter.cpp
index 2be66e7..6613f09 100644
--- a/src/core/SkImageFilter.cpp
+++ b/src/core/SkImageFilter.cpp
@@ -8,7 +8,6 @@
 #include "SkImageFilter.h"
 
 #include "SkBitmap.h"
-#include "SkDevice.h"
 #include "SkReadBuffer.h"
 #include "SkWriteBuffer.h"
 #include "SkRect.h"
@@ -156,7 +155,9 @@
     }
     GrTexture* srcTexture = input.getTexture();
     SkIRect bounds;
-    if (!this->applyCropRect(ctx, proxy, input, &srcOffset, &bounds, &input)) {
+    src.getBounds(&bounds);
+    bounds.offset(srcOffset);
+    if (!this->applyCropRect(&bounds, ctx.ctm())) {
         return false;
     }
     SkRect srcRect = SkRect::Make(bounds);
@@ -195,60 +196,18 @@
 #endif
 }
 
-bool SkImageFilter::applyCropRect(const Context& ctx, const SkBitmap& src,
-                                  const SkIPoint& srcOffset, SkIRect* bounds) const {
-    SkIRect srcBounds;
-    src.getBounds(&srcBounds);
-    srcBounds.offset(srcOffset);
+bool SkImageFilter::applyCropRect(SkIRect* rect, const SkMatrix& matrix) const {
     SkRect cropRect;
-    ctx.ctm().mapRect(&cropRect, fCropRect.rect());
+    matrix.mapRect(&cropRect, fCropRect.rect());
     SkIRect cropRectI;
     cropRect.roundOut(&cropRectI);
     uint32_t flags = fCropRect.flags();
-    if (flags & CropRect::kHasLeft_CropEdge) srcBounds.fLeft = cropRectI.fLeft;
-    if (flags & CropRect::kHasTop_CropEdge) srcBounds.fTop = cropRectI.fTop;
-    if (flags & CropRect::kHasRight_CropEdge) srcBounds.fRight = cropRectI.fRight;
-    if (flags & CropRect::kHasBottom_CropEdge) srcBounds.fBottom = cropRectI.fBottom;
-    if (!srcBounds.intersect(ctx.clipBounds())) {
-        return false;
-    }
-    *bounds = srcBounds;
-    return true;
-}
-
-bool SkImageFilter::applyCropRect(const Context& ctx, Proxy* proxy, const SkBitmap& src,
-                                  SkIPoint* srcOffset, SkIRect* bounds, SkBitmap* dst) const {
-    SkIRect srcBounds;
-    src.getBounds(&srcBounds);
-    srcBounds.offset(*srcOffset);
-    SkRect cropRect;
-    ctx.ctm().mapRect(&cropRect, fCropRect.rect());
-    SkIRect cropRectI;
-    cropRect.roundOut(&cropRectI);
-    uint32_t flags = fCropRect.flags();
-    *bounds = srcBounds;
-    if (flags & CropRect::kHasLeft_CropEdge) bounds->fLeft = cropRectI.fLeft;
-    if (flags & CropRect::kHasTop_CropEdge) bounds->fTop = cropRectI.fTop;
-    if (flags & CropRect::kHasRight_CropEdge) bounds->fRight = cropRectI.fRight;
-    if (flags & CropRect::kHasBottom_CropEdge) bounds->fBottom = cropRectI.fBottom;
-    if (!bounds->intersect(ctx.clipBounds())) {
-        return false;
-    }
-    if (srcBounds.contains(*bounds)) {
-        *dst = src;
-        return true;
-    } else {
-        SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds->width(), bounds->height()));
-        if (!device) {
-            return false;
-        }
-        SkCanvas canvas(device);
-        canvas.clear(0x00000000);
-        canvas.drawBitmap(src, srcOffset->x() - bounds->x(), srcOffset->y() - bounds->y());
-        *srcOffset = SkIPoint::Make(bounds->x(), bounds->y());
-        *dst = device->accessBitmap(false);
-        return true;
-    }
+    // If the original crop rect edges were unset, max out the new crop edges
+    if (!(flags & CropRect::kHasLeft_CropEdge)) cropRectI.fLeft = SK_MinS32;
+    if (!(flags & CropRect::kHasTop_CropEdge)) cropRectI.fTop = SK_MinS32;
+    if (!(flags & CropRect::kHasRight_CropEdge)) cropRectI.fRight = SK_MaxS32;
+    if (!(flags & CropRect::kHasBottom_CropEdge)) cropRectI.fBottom = SK_MaxS32;
+    return rect->intersect(cropRectI);
 }
 
 bool SkImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
diff --git a/src/effects/SkBlurImageFilter.cpp b/src/effects/SkBlurImageFilter.cpp
index ffebe54..adcb28f 100644
--- a/src/effects/SkBlurImageFilter.cpp
+++ b/src/effects/SkBlurImageFilter.cpp
@@ -146,13 +146,15 @@
         return false;
     }
 
-    SkIRect srcBounds, dstBounds;
-    if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &srcBounds, &src)) {
+    SkAutoLockPixels alp(src);
+    if (!src.getPixels()) {
         return false;
     }
 
-    SkAutoLockPixels alp(src);
-    if (!src.getPixels()) {
+    SkIRect srcBounds, dstBounds;
+    src.getBounds(&srcBounds);
+    srcBounds.offset(srcOffset);
+    if (!this->applyCropRect(&srcBounds, ctx.ctm())) {
         return false;
     }
 
@@ -256,11 +258,13 @@
     if (getInput(0) && !getInput(0)->getInputResultGPU(proxy, src, ctx, &input, &srcOffset)) {
         return false;
     }
+    GrTexture* source = input.getTexture();
     SkIRect rect;
-    if (!this->applyCropRect(ctx, proxy, input, &srcOffset, &rect, &input)) {
+    src.getBounds(&rect);
+    rect.offset(srcOffset);
+    if (!this->applyCropRect(&rect, ctx.ctm())) {
         return false;
     }
-    GrTexture* source = input.getTexture();
     SkVector sigma, localSigma = SkVector::Make(fSigma.width(), fSigma.height());
     ctx.ctm().mapVectors(&sigma, &localSigma, 1);
     offset->fX = rect.fLeft;
diff --git a/src/effects/SkColorFilterImageFilter.cpp b/src/effects/SkColorFilterImageFilter.cpp
index 8cdd546..0de7330 100755
--- a/src/effects/SkColorFilterImageFilter.cpp
+++ b/src/effects/SkColorFilterImageFilter.cpp
@@ -109,7 +109,9 @@
     }
 
     SkIRect bounds;
-    if (!this->applyCropRect(ctx, src, srcOffset, &bounds)) {
+    src.getBounds(&bounds);
+    bounds.offset(srcOffset);
+    if (!this->applyCropRect(&bounds, ctx.ctm())) {
         return false;
     }
 
diff --git a/src/effects/SkDisplacementMapEffect.cpp b/src/effects/SkDisplacementMapEffect.cpp
index 4b26a5f..a1c18c6 100644
--- a/src/effects/SkDisplacementMapEffect.cpp
+++ b/src/effects/SkDisplacementMapEffect.cpp
@@ -209,23 +209,26 @@
         (color.colorType() != kPMColor_SkColorType)) {
         return false;
     }
+
+    SkAutoLockPixels alp_displacement(displ), alp_color(color);
+    if (!displ.getPixels() || !color.getPixels()) {
+        return false;
+    }
     SkIRect bounds;
-    // Since computeDisplacement does bounds checking on color pixel access, we don't need to pad
-    // the color bitmap to bounds here.
-    if (!this->applyCropRect(ctx, color, colorOffset, &bounds)) {
+    color.getBounds(&bounds);
+    bounds.offset(colorOffset);
+    if (!this->applyCropRect(&bounds, ctx.ctm())) {
         return false;
     }
     SkIRect displBounds;
-    if (!this->applyCropRect(ctx, proxy, displ, &displOffset, &displBounds, &displ)) {
+    displ.getBounds(&displBounds);
+    displBounds.offset(displOffset);
+    if (!this->applyCropRect(&displBounds, ctx.ctm())) {
         return false;
     }
     if (!bounds.intersect(displBounds)) {
         return false;
     }
-    SkAutoLockPixels alp_displacement(displ), alp_color(color);
-    if (!displ.getPixels() || !color.getPixels()) {
-        return false;
-    }
 
     dst->setConfig(color.config(), bounds.width(), bounds.height());
     if (!dst->allocPixels()) {
@@ -251,18 +254,14 @@
     } else {
         *dst = src;
     }
-    dst->outset(fScale * SK_ScalarHalf, fScale * SK_ScalarHalf);
 }
 
 bool SkDisplacementMapEffect::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
                                    SkIRect* dst) const {
-    SkIRect bounds = src;
-    if (getColorInput() && !getColorInput()->filterBounds(src, ctm, &bounds)) {
-        return false;
+    if (getColorInput()) {
+        return getColorInput()->filterBounds(src, ctm, dst);
     }
-    bounds.outset(SkScalarCeilToInt(fScale * SK_ScalarHalf),
-                  SkScalarCeilToInt(fScale * SK_ScalarHalf));
-    *dst = bounds;
+    *dst = src;
     return true;
 }
 
@@ -357,6 +356,7 @@
                                                                &colorOffset)) {
         return false;
     }
+    GrTexture* color = colorBM.getTexture();
     SkBitmap displacementBM = src;
     SkIPoint displacementOffset = SkIPoint::Make(0, 0);
     if (getDisplacementInput() &&
@@ -364,21 +364,6 @@
                                                    &displacementOffset)) {
         return false;
     }
-    SkIRect bounds;
-    // Since GrDisplacementMapEffect does bounds checking on color pixel access, we don't need to
-    // pad the color bitmap to bounds here.
-    if (!this->applyCropRect(ctx, colorBM, colorOffset, &bounds)) {
-        return false;
-    }
-    SkIRect displBounds;
-    if (!this->applyCropRect(ctx, proxy, displacementBM,
-                             &displacementOffset, &displBounds, &displacementBM)) {
-        return false;
-    }
-    if (!bounds.intersect(displBounds)) {
-        return false;
-    }
-    GrTexture* color = colorBM.getTexture();
     GrTexture* displacement = displacementBM.getTexture();
     GrContext* context = color->getContext();
 
@@ -395,6 +380,21 @@
 
     SkVector scale = SkVector::Make(fScale, fScale);
     ctx.ctm().mapVectors(&scale, 1);
+    SkIRect bounds;
+    colorBM.getBounds(&bounds);
+    bounds.offset(colorOffset);
+    if (!this->applyCropRect(&bounds, ctx.ctm())) {
+        return false;
+    }
+    SkIRect displBounds;
+    displacementBM.getBounds(&displBounds);
+    displBounds.offset(displacementOffset);
+    if (!this->applyCropRect(&displBounds, ctx.ctm())) {
+        return false;
+    }
+    if (!bounds.intersect(displBounds)) {
+        return false;
+    }
 
     GrPaint paint;
     SkMatrix offsetMatrix = GrEffect::MakeDivByTextureWHMatrix(displacement);
diff --git a/src/effects/SkDropShadowImageFilter.cpp b/src/effects/SkDropShadowImageFilter.cpp
index 40ab8cf..94055ce 100644
--- a/src/effects/SkDropShadowImageFilter.cpp
+++ b/src/effects/SkDropShadowImageFilter.cpp
@@ -66,7 +66,9 @@
         return false;
 
     SkIRect bounds;
-    if (!this->applyCropRect(ctx, src, srcOffset, &bounds)) {
+    src.getBounds(&bounds);
+    bounds.offset(srcOffset);
+    if (!this->applyCropRect(&bounds, ctx.ctm())) {
         return false;
     }
 
@@ -88,8 +90,7 @@
     paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
     SkVector offsetVec, localOffsetVec = SkVector::Make(fDx, fDy);
     ctx.ctm().mapVectors(&offsetVec, &localOffsetVec, 1);
-    canvas.translate(SkIntToScalar(srcOffset.fX - bounds.fLeft),
-                     SkIntToScalar(srcOffset.fY - bounds.fTop));
+    canvas.translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop));
     canvas.drawBitmap(src, offsetVec.fX, offsetVec.fY, &paint);
     canvas.drawBitmap(src, 0, 0);
     *result = device->accessBitmap(false);
diff --git a/src/effects/SkLightingImageFilter.cpp b/src/effects/SkLightingImageFilter.cpp
index 54bb1c8..206be1b 100644
--- a/src/effects/SkLightingImageFilter.cpp
+++ b/src/effects/SkLightingImageFilter.cpp
@@ -946,8 +946,15 @@
     if (src.colorType() != kPMColor_SkColorType) {
         return false;
     }
+    SkAutoLockPixels alp(src);
+    if (!src.getPixels()) {
+        return false;
+    }
+
     SkIRect bounds;
-    if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &bounds, &src)) {
+    src.getBounds(&bounds);
+    bounds.offset(srcOffset);
+    if (!this->applyCropRect(&bounds, ctx.ctm())) {
         return false;
     }
 
@@ -955,11 +962,6 @@
         return false;
     }
 
-    SkAutoLockPixels alp(src);
-    if (!src.getPixels()) {
-        return false;
-    }
-
     dst->setConfig(src.config(), bounds.width(), bounds.height());
     if (!dst->allocPixels()) {
         return false;
@@ -1037,9 +1039,15 @@
     if (src.colorType() != kPMColor_SkColorType) {
         return false;
     }
+    SkAutoLockPixels alp(src);
+    if (!src.getPixels()) {
+        return false;
+    }
 
     SkIRect bounds;
-    if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &bounds, &src)) {
+    src.getBounds(&bounds);
+    bounds.offset(srcOffset);
+    if (!this->applyCropRect(&bounds, ctx.ctm())) {
         return false;
     }
 
@@ -1047,11 +1055,6 @@
         return false;
     }
 
-    SkAutoLockPixels alp(src);
-    if (!src.getPixels()) {
-        return false;
-    }
-
     dst->setConfig(src.config(), bounds.width(), bounds.height());
     dst->allocPixels();
     if (!dst->getPixels()) {
diff --git a/src/effects/SkMatrixConvolutionImageFilter.cpp b/src/effects/SkMatrixConvolutionImageFilter.cpp
index 7b9812d..1df1ff9 100644
--- a/src/effects/SkMatrixConvolutionImageFilter.cpp
+++ b/src/effects/SkMatrixConvolutionImageFilter.cpp
@@ -265,7 +265,9 @@
     }
 
     SkIRect bounds;
-    if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &bounds, &src)) {
+    src.getBounds(&bounds);
+    bounds.offset(srcOffset);
+    if (!this->applyCropRect(&bounds, ctx.ctm())) {
         return false;
     }
 
diff --git a/src/effects/SkMergeImageFilter.cpp b/src/effects/SkMergeImageFilter.cpp
index adf9afe..ff36e5b 100755
--- a/src/effects/SkMergeImageFilter.cpp
+++ b/src/effects/SkMergeImageFilter.cpp
@@ -73,7 +73,8 @@
     }
 
     SkIRect bounds;
-    if (!this->applyCropRect(ctx, src, SkIPoint::Make(0, 0), &bounds)) {
+    src.getBounds(&bounds);
+    if (!this->applyCropRect(&bounds, ctx.ctm())) {
         return false;
     }
 
diff --git a/src/effects/SkMorphologyImageFilter.cpp b/src/effects/SkMorphologyImageFilter.cpp
index fb69758..90940b9 100644
--- a/src/effects/SkMorphologyImageFilter.cpp
+++ b/src/effects/SkMorphologyImageFilter.cpp
@@ -154,7 +154,9 @@
     }
 
     SkIRect bounds;
-    if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &bounds, &src)) {
+    src.getBounds(&bounds);
+    bounds.offset(srcOffset);
+    if (!this->applyCropRect(&bounds, ctx.ctm())) {
         return false;
     }
 
@@ -545,7 +547,9 @@
         return false;
     }
     SkIRect bounds;
-    if (!this->applyCropRect(ctx, proxy, input, &srcOffset, &bounds, &input)) {
+    input.getBounds(&bounds);
+    bounds.offset(srcOffset);
+    if (!this->applyCropRect(&bounds, ctx.ctm())) {
         return false;
     }
     SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()),
diff --git a/src/effects/SkOffsetImageFilter.cpp b/src/effects/SkOffsetImageFilter.cpp
index ae35a4c..98eb05b 100644
--- a/src/effects/SkOffsetImageFilter.cpp
+++ b/src/effects/SkOffsetImageFilter.cpp
@@ -42,7 +42,10 @@
         }
 
         SkIRect bounds;
-        if (!this->applyCropRect(ctx, src, srcOffset, &bounds)) {
+        src.getBounds(&bounds);
+        bounds.offset(srcOffset);
+
+        if (!applyCropRect(&bounds, ctx.ctm())) {
             return false;
         }
 
diff --git a/src/effects/SkRectShaderImageFilter.cpp b/src/effects/SkRectShaderImageFilter.cpp
index 13e59c2..dad0e02 100644
--- a/src/effects/SkRectShaderImageFilter.cpp
+++ b/src/effects/SkRectShaderImageFilter.cpp
@@ -56,7 +56,8 @@
                                             SkBitmap* result,
                                             SkIPoint* offset) const {
     SkIRect bounds;
-    if (!this->applyCropRect(ctx, source, SkIPoint::Make(0, 0), &bounds)) {
+    source.getBounds(&bounds);
+    if (!this->applyCropRect(&bounds, ctx.ctm())) {
         return false;
     }
 
diff --git a/src/effects/SkXfermodeImageFilter.cpp b/src/effects/SkXfermodeImageFilter.cpp
index 901353f..ebfd16c 100644
--- a/src/effects/SkXfermodeImageFilter.cpp
+++ b/src/effects/SkXfermodeImageFilter.cpp
@@ -62,13 +62,14 @@
     }
 
     SkIRect bounds, foregroundBounds;
-    if (!applyCropRect(ctx, foreground, foregroundOffset, &foregroundBounds)) {
-        return false;
-    }
-    if (!applyCropRect(ctx, background, backgroundOffset, &bounds)) {
-        return false;
-    }
+    background.getBounds(&bounds);
+    bounds.offset(backgroundOffset);
+    foreground.getBounds(&foregroundBounds);
+    foregroundBounds.offset(foregroundOffset);
     bounds.join(foregroundBounds);
+    if (!applyCropRect(&bounds, ctx.ctm())) {
+        return false;
+    }
 
     SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height()));
     if (NULL == device.get()) {