Fix blending bug in animated webp decodes

Bug: webp:490

Depending on the encoded image, SkWebpCodec may need to blend the
the output from libwebp with the prior frame. It does so using the
method blend_line, which expects the libwebp output to be unpremul.

Prior to this commit, SkWebpCodec sometimes told libwebp to premultiply,
and then passed that premultiplied data to blend_line, which
premultiplied again.

Use webpInfo's alphaType to decide whether to premultiply. Consolidate
choosing its alphaType into one block. The functional difference is that
if (blendWithPrevFrame), we no longer premul if the dst is kPremul.

Move declaration of webDst to where it's used.

Add a test.

Change-Id: Ic0cfb4d918c2ab434c6787ed5a532c4d1e67fa17
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/342618
Reviewed-by: Derek Sollenberger <djsollen@google.com>
Commit-Queue: Derek Sollenberger <djsollen@google.com>
Auto-Submit: Leon Scroggins <scroggo@google.com>
diff --git a/src/codec/SkWebpCodec.cpp b/src/codec/SkWebpCodec.cpp
index e8b1bb4..2f7a2f0 100644
--- a/src/codec/SkWebpCodec.cpp
+++ b/src/codec/SkWebpCodec.cpp
@@ -445,10 +445,12 @@
     const bool blendWithPrevFrame = !independent && frame.blend_method == WEBP_MUX_BLEND
         && frame.has_alpha;
 
-    SkBitmap webpDst;
     auto webpInfo = dstInfo;
     if (!frame.has_alpha) {
         webpInfo = webpInfo.makeAlphaType(kOpaque_SkAlphaType);
+    } else if (this->colorXform() || blendWithPrevFrame) {
+        // the colorXform and blend_line expect unpremul.
+        webpInfo = webpInfo.makeAlphaType(kUnpremul_SkAlphaType);
     }
     if (this->colorXform()) {
         // Swizzling between RGBA and BGRA is zero cost in a color transform.  So when we have a
@@ -457,12 +459,9 @@
         // Lossy webp is encoded as YUV (so RGBA and BGRA are the same cost).  Lossless webp is
         // encoded as BGRA. This means decoding to BGRA is either faster or the same cost as RGBA.
         webpInfo = webpInfo.makeColorType(kBGRA_8888_SkColorType);
-
-        if (webpInfo.alphaType() == kPremul_SkAlphaType) {
-            webpInfo = webpInfo.makeAlphaType(kUnpremul_SkAlphaType);
-        }
     }
 
+    SkBitmap webpDst;
     if ((this->colorXform() && !is_8888(dstInfo.colorType())) || blendWithPrevFrame) {
         // We will decode the entire image and then perform the color transform.  libwebp
         // does not provide a row-by-row API.  This is a shame particularly when we do not want
@@ -474,7 +473,7 @@
     }
 
     config.output.colorspace = webp_decode_mode(webpInfo.colorType(),
-            frame.has_alpha && dstInfo.alphaType() == kPremul_SkAlphaType && !this->colorXform());
+            webpInfo.alphaType() == kPremul_SkAlphaType);
     config.output.is_external_memory = 1;
 
     config.output.u.RGBA.rgba = reinterpret_cast<uint8_t*>(webpDst.getAddr(dstX, dstY));