Fix mapping from src to dst image space in SkAlphaThresholdFilter

This CL does 3 things:
   It updates the imagealphathreshold GMs so they would've caught this bug
   It updates SkAlphaImageThresholdFilter to fix the bug
   It updates the imagealphathreshold_surface GM to match the imagealphathreshold_crop GM (which it was, presumably, originally written to do)

The bug in question is that the prior mapping from src to dst space was correct as long as the imageOffset was (0, 0).

BUG=675332

Change-Id: I3aa1f463a2234576fb2277797caa2fc4aba2650d
Reviewed-on: https://skia-review.googlesource.com/6291
Reviewed-by: Brian Osman <brianosman@google.com>
Reviewed-by: Stephan White <senorblanco@chromium.org>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/gm/imagealphathreshold.cpp b/gm/imagealphathreshold.cpp
index 1e90f76..302cdee 100644
--- a/gm/imagealphathreshold.cpp
+++ b/gm/imagealphathreshold.cpp
@@ -7,27 +7,25 @@
 
 #include "gm.h"
 #include "SkAlphaThresholdFilter.h"
-#include "SkRandom.h"
+#include "SkOffsetImageFilter.h"
 #include "SkSurface.h"
 
 #define WIDTH 500
 #define HEIGHT 500
 
-namespace {
-
-void draw_rects(SkCanvas* canvas) {
+static void draw_rects(SkCanvas* canvas) {
     SkPaint rectPaint;
-    rectPaint.setColor(0xFF0000FF);
+    rectPaint.setColor(SK_ColorBLUE);
     canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
     rectPaint.setColor(0xBFFF0000);
     canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
     rectPaint.setColor(0x3F00FF00);
     canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
-    rectPaint.setColor(0x00000000);
+    rectPaint.setColor(SK_ColorTRANSPARENT);
     canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
 }
 
-SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) {
+static SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) {
     SkIRect rects[2];
     rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
     rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
@@ -35,18 +33,15 @@
     region.setRects(rects, 2);
 
     SkPaint paint;
-    paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, nullptr, cropRect));
+    sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(25, 25, nullptr));
+    paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, std::move(offset), cropRect));
     return paint;
 }
 
-};
-
-namespace skiagm {
-
-class ImageAlphaThresholdGM : public GM {
+class ImageAlphaThresholdGM : public skiagm::GM {
 public:
     ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(useCropRect) {
-        this->setBGColor(0xFFFFFFFF);
+        this->setBGColor(SK_ColorWHITE);
     }
 
 protected:
@@ -87,8 +82,31 @@
     typedef GM INHERITED;
 };
 
+// Create a 'width' x 'height' SkSurface that matches the colorType of 'canvas' as
+// best we can
+static sk_sp<SkSurface> make_color_matching_surface(SkCanvas* canvas, int width, int height,
+                                                    SkAlphaType alphaType) {
 
-class ImageAlphaThresholdSurfaceGM : public GM {
+    SkColorType ct = canvas->imageInfo().colorType();
+    sk_sp<SkColorSpace> cs(sk_ref_sp(canvas->imageInfo().colorSpace()));
+
+    if (kUnknown_SkColorType == ct) {
+        // For backends that aren't yet color-space aware we just fallback to N32.
+        ct = kN32_SkColorType;
+        cs = nullptr;
+    }
+
+    SkImageInfo info = SkImageInfo::Make(width, height, ct, alphaType, std::move(cs));
+
+    sk_sp<SkSurface> result = canvas->makeSurface(info);
+    if (!result) {
+        result = SkSurface::MakeRaster(info);
+    }
+
+    return result;
+}
+
+class ImageAlphaThresholdSurfaceGM : public skiagm::GM {
 public:
     ImageAlphaThresholdSurfaceGM() {
         this->setBGColor(0xFFFFFFFF);
@@ -104,12 +122,17 @@
     }
 
     void onDraw(SkCanvas* canvas) override {
-        SkImageInfo info = SkImageInfo::MakeS32(WIDTH, HEIGHT, kOpaque_SkAlphaType);
-        auto surface(canvas->makeSurface(info));
-        if (nullptr == surface) {
-            surface = SkSurface::MakeRaster(info);
-        }
-        surface->getCanvas()->clear(SK_ColorWHITE);
+        SkMatrix matrix;
+        matrix.reset();
+        matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
+        matrix.postScale(.8f, .8f);
+
+        canvas->concat(matrix);
+
+        sk_sp<SkSurface> surface(make_color_matching_surface(canvas, WIDTH, HEIGHT,
+                                                             kPremul_SkAlphaType));
+
+        surface->getCanvas()->clear(SK_ColorTRANSPARENT);
         draw_rects(surface->getCanvas());
 
         SkPaint paint = create_filter_paint();
@@ -118,7 +141,7 @@
     }
 
 private:
-    typedef GM INHERITED;
+    typedef skiagm::GM INHERITED;
 };
 
 //////////////////////////////////////////////////////////////////////////////
@@ -126,5 +149,3 @@
 DEF_GM(return new ImageAlphaThresholdGM(true);)
 DEF_GM(return new ImageAlphaThresholdGM(false);)
 DEF_GM(return new ImageAlphaThresholdSurfaceGM();)
-
-}