Finish supporting decoding opaque to non-opaque

When decoding to 565 or Gray, allow the client to incorrectly ask for premul.

When checking whether it's possible to decode to 565, return whether the
source is opaque.

In DM, allow decoding to 565 or Gray, even if the client also asked for premul.

This fixes a bug introduced in crrev.com/1999593003 when we stopped ever
requesting Opaque, resulting in us not testing 565 or Gray.

BUG=skia:4616
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=1996993003

Review-Url: https://codereview.chromium.org/1996993003
diff --git a/tests/CodecTest.cpp b/tests/CodecTest.cpp
index 5ac19d9..12d6ac8 100644
--- a/tests/CodecTest.cpp
+++ b/tests/CodecTest.cpp
@@ -103,9 +103,49 @@
     {
         // Test decoding to 565
         SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
-        SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ?
-                expectedResult : SkCodec::kInvalidConversion;
-        test_info(r, codec, info565, expected565, nullptr);
+        if (info.alphaType() == kOpaque_SkAlphaType) {
+            // Decoding to 565 should succeed.
+            SkBitmap bm565;
+            bm565.allocPixels(info565);
+            SkAutoLockPixels alp(bm565);
+
+            // This will allow comparison even if the image is incomplete.
+            bm565.eraseColor(SK_ColorBLACK);
+
+            REPORTER_ASSERT(r, expectedResult == codec->getPixels(info565,
+                    bm565.getPixels(), bm565.rowBytes()));
+
+            SkMD5::Digest digest565;
+            md5(bm565, &digest565);
+
+            // A dumb client's request for non-opaque should also succeed.
+            for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
+                info565 = info565.makeAlphaType(alpha);
+                test_info(r, codec, info565, expectedResult, &digest565);
+            }
+        } else {
+            test_info(r, codec, info565, SkCodec::kInvalidConversion, nullptr);
+        }
+    }
+
+    if (codec->getInfo().colorType() == kGray_8_SkColorType) {
+        SkImageInfo grayInfo = codec->getInfo();
+        SkBitmap grayBm;
+        grayBm.allocPixels(grayInfo);
+        SkAutoLockPixels alp(grayBm);
+
+        grayBm.eraseColor(SK_ColorBLACK);
+
+        REPORTER_ASSERT(r, expectedResult == codec->getPixels(grayInfo,
+                grayBm.getPixels(), grayBm.rowBytes()));
+
+        SkMD5::Digest grayDigest;
+        md5(grayBm, &grayDigest);
+
+        for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
+            grayInfo = grayInfo.makeAlphaType(alpha);
+            test_info(r, codec, grayInfo, expectedResult, &grayDigest);
+        }
     }
 
     // Verify that re-decoding gives the same result.  It is interesting to check this after