Add readPixels() tests for generator backed images

Good news:
Everything seems to work as it is supposed to.  That's why this
CL is just tests.

Bad news:
Picture is a bit strange in that the caching behavior may affect
how the output looks.  Ex: If we choose to cache, we will first
draw into the picture's colorSpace and then convert that to the
dstColorSpace.  If we choose not to cache, we will draw directly
into the dstColorSpace.
And then untagged pictures seem like they really shouldn't work
very well...  We are caching a legacy draw and then drawing that
into the dstColorSpace?  Maybe this isn't the most critical
thing to think about right now though, given Florin's work.

Remaining TODOs:
Color space support for gpu-backed images.
I still plan to clarify conversions that are allowed vs. not
allowed and share that code between all SkImages.

BUG=skia:6021

Change-Id: I9557ca1c00ff6854848fe59c3a67abd2af91bb46
Reviewed-on: https://skia-review.googlesource.com/6853
Reviewed-by: Brian Osman <brianosman@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Matt Sarett <msarett@google.com>
diff --git a/gm/readpixels.cpp b/gm/readpixels.cpp
index 024cb43..df7fb07 100644
--- a/gm/readpixels.cpp
+++ b/gm/readpixels.cpp
@@ -12,6 +12,7 @@
 #include "SkColorSpace_Base.h"
 #include "SkHalf.h"
 #include "SkImage.h"
+#include "SkPictureRecorder.h"
 
 static void clamp_if_necessary(const SkImageInfo& info, void* pixels) {
     if (kRGBA_F16_SkColorType != info.colorType()) {
@@ -62,6 +63,39 @@
     return SkImage::MakeFromBitmap(bitmap);
 }
 
+static sk_sp<SkImage> make_codec_image() {
+    sk_sp<SkData> encoded = GetResourceAsData("randPixels.png");
+    return SkImage::MakeFromEncoded(encoded);
+}
+
+static void draw_contents(SkCanvas* canvas) {
+    SkPaint paint;
+    paint.setStyle(SkPaint::kStroke_Style);
+    paint.setStrokeWidth(20);
+    paint.setColor(0xFF800000);
+    canvas->drawCircle(40, 40, 35, paint);
+    paint.setColor(0xFF008000);
+    canvas->drawCircle(50, 50, 35, paint);
+    paint.setColor(0xFF000080);
+    canvas->drawCircle(60, 60, 35, paint);
+}
+
+static sk_sp<SkImage> make_tagged_picture_image() {
+    SkPictureRecorder recorder;
+    draw_contents(recorder.beginRecording(SkRect::MakeIWH(kWidth, kHeight)));
+    return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
+                                    SkISize::Make(kWidth, kHeight), nullptr, nullptr,
+                                    SkImage::BitDepth::kU8,
+                                    SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named));
+}
+
+static sk_sp<SkImage> make_untagged_picture_image() {
+    SkPictureRecorder recorder;
+    draw_contents(recorder.beginRecording(SkRect::MakeIWH(kWidth, kHeight)));
+    return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
+                                    SkISize::Make(kWidth, kHeight), nullptr, nullptr);
+}
+
 static sk_sp<SkColorSpace> make_srgb_transfer_fn(const SkColorSpacePrimaries& primaries) {
     SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor);
     SkAssertResult(primaries.toXYZD50(&toXYZD50));
@@ -96,13 +130,14 @@
 }
 
 static void draw_image(SkCanvas* canvas, SkImage* image, SkColorType dstColorType,
-                       SkAlphaType dstAlphaType, sk_sp<SkColorSpace> dstColorSpace) {
+                       SkAlphaType dstAlphaType, sk_sp<SkColorSpace> dstColorSpace,
+                       SkImage::CachingHint hint) {
     size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(dstColorType);
     sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
     dstColorSpace = fix_for_colortype(dstColorSpace.get(), dstColorType);
     SkImageInfo dstInfo = SkImageInfo::Make(image->width(), image->height(), dstColorType,
                                             dstAlphaType, dstColorSpace);
-    image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0);
+    image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, hint);
 
     // readPixels() does not always clamp F16.  The drawing code expects pixels in the 0-1 range.
     clamp_if_necessary(dstInfo, data->writable_data());
@@ -156,7 +191,7 @@
                     for (SkColorType dstColorType : colorTypes) {
                         for (SkAlphaType dstAlphaType : alphaTypes) {
                             draw_image(canvas, image.get(), dstColorType, dstAlphaType,
-                                       dstColorSpace);
+                                       dstColorSpace, SkImage::kAllow_CachingHint);
                             canvas->translate((float) kWidth, 0.0f);
                         }
                     }
@@ -171,3 +206,132 @@
     typedef skiagm::GM INHERITED;
 };
 DEF_GM( return new ReadPixelsGM; )
+
+class ReadPixelsCodecGM : public skiagm::GM {
+public:
+    ReadPixelsCodecGM() {}
+
+protected:
+    SkString onShortName() override {
+        return SkString("readpixelscodec");
+    }
+
+    SkISize onISize() override {
+        return SkISize::Make(3 * (kEncodedWidth + 1), 12 * (kEncodedHeight + 1));
+    }
+
+    void onDraw(SkCanvas* canvas) override {
+        if (!canvas->imageInfo().colorSpace()) {
+            // This gm is only interesting in color correct modes.
+            return;
+        }
+
+        const SkAlphaType alphaTypes[] = {
+                kUnpremul_SkAlphaType,
+                kPremul_SkAlphaType,
+        };
+        const SkColorType colorTypes[] = {
+                kRGBA_8888_SkColorType,
+                kBGRA_8888_SkColorType,
+                kRGBA_F16_SkColorType,
+        };
+        const sk_sp<SkColorSpace> colorSpaces[] = {
+                make_wide_gamut(),
+                SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named),
+                make_small_gamut(),
+        };
+        const SkImage::CachingHint hints[] = {
+                SkImage::kAllow_CachingHint,
+                SkImage::kDisallow_CachingHint,
+        };
+
+        sk_sp<SkImage> image = make_codec_image();
+        for (sk_sp<SkColorSpace> dstColorSpace : colorSpaces) {
+            canvas->save();
+            for (SkColorType dstColorType : colorTypes) {
+                for (SkAlphaType dstAlphaType : alphaTypes) {
+                    for (SkImage::CachingHint hint : hints) {
+                        draw_image(canvas, image.get(), dstColorType, dstAlphaType, dstColorSpace,
+                                   hint);
+                        canvas->translate(0.0f, (float) kEncodedHeight + 1);
+                    }
+                }
+            }
+            canvas->restore();
+            canvas->translate((float) kEncodedWidth + 1, 0.0f);
+        }
+    }
+
+private:
+    static const int kEncodedWidth = 8;
+    static const int kEncodedHeight = 8;
+
+    typedef skiagm::GM INHERITED;
+};
+DEF_GM( return new ReadPixelsCodecGM; )
+
+class ReadPixelsPictureGM : public skiagm::GM {
+public:
+    ReadPixelsPictureGM() {}
+
+protected:
+    SkString onShortName() override {
+        return SkString("readpixelspicture");
+    }
+
+    SkISize onISize() override {
+        return SkISize::Make(3 * kWidth, 12 * kHeight);
+    }
+
+    void onDraw(SkCanvas* canvas) override {
+        if (!canvas->imageInfo().colorSpace()) {
+            // This gm is only interesting in color correct modes.
+            return;
+        }
+
+        const sk_sp<SkImage> images[] = {
+                make_tagged_picture_image(),
+                make_untagged_picture_image(),
+        };
+        const SkAlphaType alphaTypes[] = {
+                kUnpremul_SkAlphaType,
+                kPremul_SkAlphaType,
+        };
+        const SkColorType colorTypes[] = {
+                kRGBA_8888_SkColorType,
+                kBGRA_8888_SkColorType,
+                kRGBA_F16_SkColorType,
+        };
+        const sk_sp<SkColorSpace> colorSpaces[] = {
+                make_wide_gamut(),
+                SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named),
+                make_small_gamut(),
+        };
+        const SkImage::CachingHint hints[] = {
+                SkImage::kAllow_CachingHint,
+                SkImage::kDisallow_CachingHint,
+        };
+
+        for (sk_sp<SkImage> image : images) {
+            for (sk_sp<SkColorSpace> dstColorSpace : colorSpaces) {
+                canvas->save();
+                for (SkColorType dstColorType : colorTypes) {
+                    for (SkAlphaType dstAlphaType : alphaTypes) {
+                        for (SkImage::CachingHint hint : hints) {
+                            draw_image(canvas, image.get(), dstColorType, dstAlphaType,
+                                       dstColorSpace, hint);
+                            canvas->translate(0.0f, (float) kHeight);
+                        }
+                    }
+                }
+                canvas->restore();
+                canvas->translate((float) kWidth, 0.0f);
+            }
+        }
+    }
+
+private:
+
+    typedef skiagm::GM INHERITED;
+};
+DEF_GM( return new ReadPixelsPictureGM; )