Adjust the alpha type for pixelRefs.

Move SkBitmap's validate_alphaType to SkImageInfo, with the new
name SkColorTypeValidateAlphaType. Use it in SkPixelRef's constructors,
as well as in SkDecodingImageGenerator. This fixes a bug where an
SkPixelRef's SkAlphaType could get out of sync with its SkBitmap,
when both were assigned the same SkAlphaType.

R=reed@google.com, halcanary@google.com

Author: scroggo@google.com

Review URL: https://codereview.chromium.org/346593003
diff --git a/src/core/SkImageInfo.cpp b/src/core/SkImageInfo.cpp
index 27c4573..e61cd7d 100644
--- a/src/core/SkImageInfo.cpp
+++ b/src/core/SkImageInfo.cpp
@@ -38,3 +38,34 @@
     uint32_t packed = (fAlphaType << 8) | fColorType;
     buffer.write32(packed);
 }
+
+bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
+                                  SkAlphaType* canonical) {
+    switch (colorType) {
+        case kUnknown_SkColorType:
+            alphaType = kIgnore_SkAlphaType;
+            break;
+        case kAlpha_8_SkColorType:
+            if (kUnpremul_SkAlphaType == alphaType) {
+                alphaType = kPremul_SkAlphaType;
+            }
+            // fall-through
+        case kIndex_8_SkColorType:
+        case kARGB_4444_SkColorType:
+        case kRGBA_8888_SkColorType:
+        case kBGRA_8888_SkColorType:
+            if (kIgnore_SkAlphaType == alphaType) {
+                return false;
+            }
+            break;
+        case kRGB_565_SkColorType:
+            alphaType = kOpaque_SkAlphaType;
+            break;
+        default:
+            return false;
+    }
+    if (canonical) {
+        *canonical = alphaType;
+    }
+    return true;
+}