Support decoding images to multiple formats, depending on usage

Our codec generator will now preserve any asked-for color space, and
convert the encoded data to that representation. Cacherator now
allows decoding an image to both legacy (nullptr color space), and
color-correct formats. In color-correct mode, we choose the best
decoded format, based on the original properties, and our backend's
capabilities. Preference is given to the native format, when it's
already texturable (sRGB 8888 or F16 linear). Otherwise, we prefer
linear F16, and fall back to sRGB when that's not an option.

Re-land (and fix) of:
https://skia-review.googlesource.com/c/4438/
https://skia-review.googlesource.com/c/4796/

BUG=skia:5907

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=4838

Change-Id: I20ff972ffe1c7e6535ddc501e2a8ab8c246e4061
Reviewed-on: https://skia-review.googlesource.com/4838
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Matt Sarett <msarett@google.com>
diff --git a/src/image/SkImage_Generator.cpp b/src/image/SkImage_Generator.cpp
index 82f395f..efee134 100644
--- a/src/image/SkImage_Generator.cpp
+++ b/src/image/SkImage_Generator.cpp
@@ -30,9 +30,9 @@
     SkImageCacherator* peekCacherator() const override { return &fCache; }
     SkData* onRefEncoded(GrContext*) const override;
     sk_sp<SkImage> onMakeSubset(const SkIRect&) const override;
-    bool getROPixels(SkBitmap*, CachingHint) const override;
+    bool getROPixels(SkBitmap*, SkDestinationSurfaceColorMode, CachingHint) const override;
     GrTexture* asTextureRef(GrContext*, const GrSamplerParams&,
-                            SkDestinationSurfaceColorMode) const override;
+                            SkDestinationSurfaceColorMode, sk_sp<SkColorSpace>*) const override;
     bool onIsLazyGenerated() const override { return true; }
 
 private:
@@ -45,9 +45,13 @@
 
 bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
                                      int srcX, int srcY, CachingHint chint) const {
+    SkDestinationSurfaceColorMode decodeColorMode = dstInfo.colorSpace()
+        ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
+        : SkDestinationSurfaceColorMode::kLegacy;
     SkBitmap bm;
     if (kDisallow_CachingHint == chint) {
-        if (fCache.lockAsBitmapOnlyIfAlreadyCached(&bm)) {
+        SkImageCacherator::CachedFormat cacheFormat = fCache.chooseCacheFormat(decodeColorMode);
+        if (fCache.lockAsBitmapOnlyIfAlreadyCached(&bm, cacheFormat)) {
             return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
         } else {
             // Try passing the caller's buffer directly down to the generator. If this fails we
@@ -60,7 +64,7 @@
         }
     }
 
-    if (this->getROPixels(&bm, chint)) {
+    if (this->getROPixels(&bm, decodeColorMode, chint)) {
         return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
     }
     return false;
@@ -70,13 +74,15 @@
     return fCache.refEncoded(ctx);
 }
 
-bool SkImage_Generator::getROPixels(SkBitmap* bitmap, CachingHint chint) const {
-    return fCache.lockAsBitmap(bitmap, this, chint);
+bool SkImage_Generator::getROPixels(SkBitmap* bitmap, SkDestinationSurfaceColorMode colorMode,
+                                    CachingHint chint) const {
+    return fCache.lockAsBitmap(bitmap, this, colorMode, chint);
 }
 
 GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, const GrSamplerParams& params,
-                                           SkDestinationSurfaceColorMode colorMode) const {
-    return fCache.lockAsTexture(ctx, params, colorMode, this);
+                                           SkDestinationSurfaceColorMode colorMode,
+                                           sk_sp<SkColorSpace>* texColorSpace) const {
+    return fCache.lockAsTexture(ctx, params, colorMode, texColorSpace, this);
 }
 
 sk_sp<SkImage> SkImage_Generator::onMakeSubset(const SkIRect& subset) const {