Add ColorCodecSrc for testing/comparison on color corrected decodes

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1933753002

Review-Url: https://codereview.chromium.org/1933753002
diff --git a/bench/nanobench.cpp b/bench/nanobench.cpp
index 5e26b48..7999ad5 100644
--- a/bench/nanobench.cpp
+++ b/bench/nanobench.cpp
@@ -606,7 +606,7 @@
         fUseMPDs.push_back() = false;
 
         // Prepare the images for decoding
-        if (!CollectImages(&fImages)) {
+        if (!CollectImages(FLAGS_images, &fImages)) {
             exit(1);
         }
 
diff --git a/dm/DM.cpp b/dm/DM.cpp
index fe1432b..9dbd43a 100644
--- a/dm/DM.cpp
+++ b/dm/DM.cpp
@@ -779,7 +779,7 @@
     }
 
     SkTArray<SkString> images;
-    if (!CollectImages(&images)) {
+    if (!CollectImages(FLAGS_images, &images)) {
         return false;
     }
 
@@ -795,6 +795,16 @@
         }
     }
 
+    SkTArray<SkString> colorImages;
+    if (!CollectImages(FLAGS_colorImages, &colorImages)) {
+        return false;
+    }
+
+    for (auto colorImage : colorImages) {
+        ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode);
+        push_src("image", "color_codec_baseline", src);
+    }
+
     return true;
 }
 
diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp
index e801829..f99afe4 100644
--- a/dm/DMSrcSink.cpp
+++ b/dm/DMSrcSink.cpp
@@ -932,6 +932,71 @@
 
 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
+ColorCodecSrc::ColorCodecSrc(Path path, Mode mode)
+    : fPath(path)
+    , fMode(mode)
+{}
+
+bool ColorCodecSrc::veto(SinkFlags flags) const {
+    // Test to direct raster backends (8888 and 565).
+    return flags.type != SinkFlags::kRaster || flags.approach != SinkFlags::kDirect;
+}
+
+Error ColorCodecSrc::draw(SkCanvas* canvas) const {
+    if (kRGB_565_SkColorType == canvas->imageInfo().colorType()) {
+        return Error::Nonfatal("No need to test color correction to 565 backend.");
+    }
+
+    SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
+    if (!encoded) {
+        return SkStringPrintf("Couldn't read %s.", fPath.c_str());
+    }
+
+    SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
+    if (nullptr == codec.get()) {
+        return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
+    }
+
+    SkImageInfo decodeInfo = codec->getInfo().makeColorType(kN32_SkColorType);
+    SkBitmap bitmap;
+    if (!bitmap.tryAllocPixels(decodeInfo)) {
+        return SkStringPrintf("Image(%s) is too large (%d x %d)", fPath.c_str(),
+                              decodeInfo.width(), decodeInfo.height());
+    }
+
+    switch (fMode) {
+        case kBaseline_Mode:
+            switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes())) {
+                case SkCodec::kSuccess:
+                    break;
+                default:
+                    // Everything else is considered a failure.
+                    return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
+            }
+            canvas->drawBitmap(bitmap, 0, 0);
+            break;
+        default:
+            SkASSERT(false);
+            return "Invalid fMode";
+    }
+    return "";
+}
+
+SkISize ColorCodecSrc::size() const {
+    SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
+    SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
+    if (nullptr == codec) {
+        return SkISize::Make(0, 0);
+    }
+    return SkISize::Make(codec->getInfo().width(), codec->getInfo().height());
+}
+
+Name ColorCodecSrc::name() const {
+    return SkOSPath::Basename(fPath.c_str());
+}
+
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
 static const SkRect kSKPViewport = {0,0, 1000,1000};
 
 SKPSrc::SKPSrc(Path path) : fPath(path) {}
diff --git a/dm/DMSrcSink.h b/dm/DMSrcSink.h
index 5f60dc5..5a734a6 100644
--- a/dm/DMSrcSink.h
+++ b/dm/DMSrcSink.h
@@ -208,6 +208,24 @@
     bool        fRunSerially;
 };
 
+class ColorCodecSrc : public Src {
+public:
+    enum Mode {
+        // Mimic legacy behavior and apply no color correction.
+        kBaseline_Mode,
+    };
+
+    ColorCodecSrc(Path, Mode);
+
+    Error draw(SkCanvas*) const override;
+    SkISize size() const override;
+    Name name() const override;
+    bool veto(SinkFlags) const override;
+private:
+    Path                    fPath;
+    Mode                    fMode;
+};
+
 class SKPSrc : public Src {
 public:
     explicit SKPSrc(Path path);
diff --git a/tools/flags/SkCommonFlags.cpp b/tools/flags/SkCommonFlags.cpp
index eb2075c..1caffd5 100644
--- a/tools/flags/SkCommonFlags.cpp
+++ b/tools/flags/SkCommonFlags.cpp
@@ -18,6 +18,9 @@
 DEFINE_string(images, "", "List of images and/or directories to decode. A directory with no images"
                           " is treated as a fatal error.");
 
+DEFINE_string(colorImages, "", "List of images and/or directories to decode with color correction. "
+                               "A directory with no images is treated as a fatal error.");
+
 DEFINE_string2(match, m, nullptr,
                "[~][^]substring[$] [...] of GM name to run.\n"
                "Multiple matches may be separated by spaces.\n"
@@ -55,7 +58,7 @@
               "Space-separated key/value pairs to add to JSON identifying this run.");
 DEFINE_bool2(pre_log, p, false, "Log before running each test. May be incomprehensible when threading");
 
-bool CollectImages(SkTArray<SkString>* output) {
+bool CollectImages(SkCommandLineFlags::StringArray images, SkTArray<SkString>* output) {
     SkASSERT(output);
 
     static const char* const exts[] = {
@@ -67,8 +70,8 @@
 #endif
     };
 
-    for (int i = 0; i < FLAGS_images.count(); ++i) {
-        const char* flag = FLAGS_images[i];
+    for (int i = 0; i < images.count(); ++i) {
+        const char* flag = images[i];
         if (!sk_exists(flag)) {
             SkDebugf("%s does not exist!\n", flag);
             return false;
diff --git a/tools/flags/SkCommonFlags.h b/tools/flags/SkCommonFlags.h
index c6cbde4..ddd0fc8 100644
--- a/tools/flags/SkCommonFlags.h
+++ b/tools/flags/SkCommonFlags.h
@@ -16,6 +16,7 @@
 DECLARE_bool(dryRun);
 DECLARE_bool(gpu);
 DECLARE_string(images);
+DECLARE_string(colorImages);
 DECLARE_string(match);
 DECLARE_bool(quiet);
 DECLARE_bool(resetGpuContext);
@@ -34,17 +35,16 @@
 DECLARE_string(properties);
 
 /**
- *  Helper to assist in collecting image paths from --images.
+ *  Helper to assist in collecting image paths from |dir| specified through a command line flag.
  *
- *  Populates an array of strings with paths to images to test.
+ *  Populates |output|, an array of strings with paths to images to test.
  *
- *  Returns true if each argument to --images is meaningful:
+ *  Returns true if each argument to the images flag is meaningful:
  *  - If the file/directory does not exist, return false.
- *  - If a directory passed to --images does not have any supported images (based on file
- *  type), return false.
- *  - If a file is passed to --images, assume the user is deliberately testing this image,
- *  regardless of file type.
+ *  - If |dir| does not have any supported images (based on file type), return false.
+ *  - If |dir| is a single file, assume the user is deliberately testing this image,
+ *    regardless of file type.
  */
-bool CollectImages(SkTArray<SkString>*);
+bool CollectImages(SkCommandLineFlags::StringArray dir, SkTArray<SkString>* output);
 
 #endif