Reland "Switch SkCodec to use skcms plus fixes""

This reverts commit 83988edfd3256dc822b961362aad7fbc3e0cdabc.

The CTS failure was actually due to another CL.

TBR=brianosman@google.com
TBR=djsollen@google.com

Bug: skia:6839
Bug: skia:8052
Bug: skia:8278
Change-Id: Id9f152ec2c66467d90f49df223cb9b7c168ac2ac
Reviewed-on: https://skia-review.googlesource.com/149483
Reviewed-by: Leon Scroggins <scroggo@google.com>
Commit-Queue: Leon Scroggins <scroggo@google.com>
diff --git a/src/codec/SkHeifCodec.cpp b/src/codec/SkHeifCodec.cpp
index 31057a0..f5785d6 100644
--- a/src/codec/SkHeifCodec.cpp
+++ b/src/codec/SkHeifCodec.cpp
@@ -133,40 +133,37 @@
         return nullptr;
     }
 
-    SkEncodedInfo info = SkEncodedInfo::Make(
-            SkEncodedInfo::kYUV_Color, SkEncodedInfo::kOpaque_Alpha, 8);
+    std::unique_ptr<SkEncodedInfo::ICCProfile> profile = nullptr;
+    if ((frameInfo.mIccSize > 0) && (frameInfo.mIccData != nullptr)) {
+        // FIXME: Would it be possible to use MakeWithoutCopy?
+        auto icc = SkData::MakeWithCopy(frameInfo.mIccData.get(), frameInfo.mIccSize);
+        profile = SkEncodedInfo::ICCProfile::Make(std::move(icc));
+    }
+    if (!profile || profile->profile()->data_color_space != skcms_Signature_RGB) {
+        profile = SkEncodedInfo::ICCProfile::MakeSRGB();
+    }
 
+    SkEncodedInfo info = SkEncodedInfo::Make(frameInfo.mWidth, frameInfo.mHeight,
+            SkEncodedInfo::kYUV_Color, SkEncodedInfo::kOpaque_Alpha, 8, std::move(profile));
     SkEncodedOrigin orientation = get_orientation(frameInfo);
 
-    sk_sp<SkColorSpace> colorSpace = nullptr;
-    if ((frameInfo.mIccSize > 0) && (frameInfo.mIccData != nullptr)) {
-        colorSpace = SkColorSpace::MakeICC(frameInfo.mIccData.get(),
-                                           frameInfo.mIccSize);
-    }
-    if (!colorSpace || colorSpace->type() != SkColorSpace::kRGB_Type) {
-        colorSpace = SkColorSpace::MakeSRGB();
-    }
-
     *result = kSuccess;
-    return std::unique_ptr<SkCodec>(new SkHeifCodec(frameInfo.mWidth, frameInfo.mHeight,
-            info, heifDecoder.release(), std::move(colorSpace), orientation));
+    return std::unique_ptr<SkCodec>(new SkHeifCodec(std::move(info), heifDecoder.release(),
+                                                    orientation));
 }
 
-SkHeifCodec::SkHeifCodec(int width, int height, const SkEncodedInfo& info,
-        HeifDecoder* heifDecoder, sk_sp<SkColorSpace> colorSpace, SkEncodedOrigin origin)
-    : INHERITED(width, height, info, SkColorSpaceXform::kRGBA_8888_ColorFormat,
-            nullptr, std::move(colorSpace), origin)
+SkHeifCodec::SkHeifCodec(SkEncodedInfo&& info, HeifDecoder* heifDecoder, SkEncodedOrigin origin)
+    : INHERITED(std::move(info), skcms_PixelFormat_RGBA_8888, nullptr, origin)
     , fHeifDecoder(heifDecoder)
     , fSwizzleSrcRow(nullptr)
     , fColorXformSrcRow(nullptr)
 {}
 
-/*
- * Checks if the conversion between the input image and the requested output
- * image has been implemented
- * Sets the output color format
- */
-bool SkHeifCodec::setOutputColorFormat(const SkImageInfo& dstInfo) {
+
+bool SkHeifCodec::conversionSupported(const SkImageInfo& dstInfo, SkColorType /*srcColorType*/,
+                                      bool srcIsOpaque, bool needsColorXform) {
+    SkASSERT(srcIsOpaque);
+
     if (kUnknown_SkAlphaType == dstInfo.alphaType()) {
         return false;
     }
@@ -184,14 +181,14 @@
             return fHeifDecoder->setOutputColor(kHeifColorFormat_BGRA_8888);
 
         case kRGB_565_SkColorType:
-            if (this->colorXform()) {
+            if (needsColorXform) {
                 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
             } else {
                 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGB565);
             }
 
         case kRGBA_F16_SkColorType:
-            SkASSERT(this->colorXform());
+            SkASSERT(needsColorXform);
             return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
 
         default:
@@ -240,7 +237,7 @@
         }
 
         if (this->colorXform()) {
-            this->applyColorXform(dst, swizzleDst, dstWidth, kOpaque_SkAlphaType);
+            this->applyColorXform(dst, swizzleDst, dstWidth);
             dst = SkTAddOffset<void>(dst, rowBytes);
         }
 
@@ -265,11 +262,6 @@
         return kUnimplemented;
     }
 
-    // Check if we can decode to the requested destination and set the output color space
-    if (!this->setOutputColorFormat(dstInfo)) {
-        return kInvalidConversion;
-    }
-
     if (!fHeifDecoder->decode(&fFrameInfo)) {
         return kInvalidInput;
     }
@@ -313,15 +305,13 @@
 
 void SkHeifCodec::initializeSwizzler(
         const SkImageInfo& dstInfo, const Options& options) {
-    SkEncodedInfo swizzlerInfo = this->getEncodedInfo();
-
     SkImageInfo swizzlerDstInfo = dstInfo;
     if (this->colorXform()) {
         // The color xform will be expecting RGBA 8888 input.
         swizzlerDstInfo = swizzlerDstInfo.makeColorType(kRGBA_8888_SkColorType);
     }
 
-    fSwizzler.reset(SkSwizzler::CreateSwizzler(swizzlerInfo, nullptr,
+    fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), nullptr,
             swizzlerDstInfo, options, nullptr, true));
     SkASSERT(fSwizzler);
 }
@@ -339,11 +329,6 @@
 
 SkCodec::Result SkHeifCodec::onStartScanlineDecode(
         const SkImageInfo& dstInfo, const Options& options) {
-    // Check if we can decode to the requested destination and set the output color space
-    if (!this->setOutputColorFormat(dstInfo)) {
-        return kInvalidConversion;
-    }
-
     // TODO: For now, just decode the whole thing even when there is a subset.
     // If the heif image has tiles, we could potentially do this much faster,
     // but the tile configuration needs to be retrieved from the metadata.