move a bunch of helpers from SkImageInfo.h into priv

Bug: skia:
Change-Id: I8c91cfdb89e4f22448d1201d391556fe43d86dca
Reviewed-on: https://skia-review.googlesource.com/105289
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Mike Klein <mtklein@chromium.org>
Reviewed-by: Cary Clark <caryclark@google.com>
diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp
index f2efbad..0dd35ff 100644
--- a/dm/DMSrcSink.cpp
+++ b/dm/DMSrcSink.cpp
@@ -26,6 +26,7 @@
 #include "SkImageGenerator.h"
 #include "SkImageGeneratorCG.h"
 #include "SkImageGeneratorWIC.h"
+#include "SkImageInfoPriv.h"
 #include "SkLiteDL.h"
 #include "SkLiteRecorder.h"
 #include "SkMallocPixelRef.h"
@@ -455,7 +456,7 @@
     }
     decodeInfo = decodeInfo.makeWH(size.width(), size.height());
 
-    const int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType());
+    const int bpp = decodeInfo.bytesPerPixel();
     const size_t rowBytes = size.width() * bpp;
     const size_t safeSize = decodeInfo.computeByteSize(rowBytes);
     SkAutoMalloc pixels(safeSize);
@@ -851,7 +852,7 @@
     }
     decodeInfo = decodeInfo.makeWH(size.width(), size.height());
 
-    int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType());
+    int bpp = decodeInfo.bytesPerPixel();
     size_t rowBytes = size.width() * bpp;
     SkAutoMalloc pixels(size.height() * rowBytes);
 
@@ -977,7 +978,7 @@
     options.fBehavior = canvas->imageInfo().colorSpace() ?
             SkTransferFunctionBehavior::kRespect : SkTransferFunctionBehavior::kIgnore;
 
-    int bpp = SkColorTypeBytesPerPixel(decodeInfo.colorType());
+    int bpp = decodeInfo.bytesPerPixel();
     size_t rowBytes = decodeInfo.width() * bpp;
     SkAutoMalloc pixels(decodeInfo.height() * rowBytes);
     if (!gen->getPixels(decodeInfo, pixels.get(), rowBytes, &options)) {
diff --git a/gm/encode-alpha-jpeg.cpp b/gm/encode-alpha-jpeg.cpp
index dc07bf3..6d26d7a 100644
--- a/gm/encode-alpha-jpeg.cpp
+++ b/gm/encode-alpha-jpeg.cpp
@@ -7,6 +7,7 @@
 
 #include "gm.h"
 #include "SkImage.h"
+#include "SkImageInfoPriv.h"
 #include "SkJpegEncoder.h"
 
 #include "Resources.h"
diff --git a/gm/readpixels.cpp b/gm/readpixels.cpp
index 254b8fb..c112314 100644
--- a/gm/readpixels.cpp
+++ b/gm/readpixels.cpp
@@ -13,6 +13,7 @@
 #include "SkColorSpaceXformPriv.h"
 #include "SkHalf.h"
 #include "SkImage.h"
+#include "SkImageInfoPriv.h"
 #include "SkPictureRecorder.h"
 
 static void clamp_if_necessary(const SkImageInfo& info, void* pixels) {
diff --git a/include/core/SkImageInfo.h b/include/core/SkImageInfo.h
index 6876c8b..c21c3df 100644
--- a/include/core/SkImageInfo.h
+++ b/include/core/SkImageInfo.h
@@ -52,10 +52,6 @@
     return kOpaque_SkAlphaType == at;
 }
 
-static inline bool SkAlphaTypeIsValid(unsigned value) {
-    return value <= kLastEnum_SkAlphaType;
-}
-
 ///////////////////////////////////////////////////////////////////////////////
 
 /** Temporary macro that allows us to add new color types without breaking Chrome compile. */
@@ -92,64 +88,32 @@
 #endif
 };
 
-static int SkColorTypeBytesPerPixel(SkColorType ct) {
-    switch (ct) {
-        case kUnknown_SkColorType:      return 0;
-        case kAlpha_8_SkColorType:      return 1;
-        case kRGB_565_SkColorType:      return 2;
-        case kARGB_4444_SkColorType:    return 2;
-        case kRGBA_8888_SkColorType:    return 4;
-        case kBGRA_8888_SkColorType:    return 4;
-        case kRGB_888x_SkColorType:     return 4;
-        case kRGBA_1010102_SkColorType: return 4;
-        case kRGB_101010x_SkColorType:  return 4;
-        case kGray_8_SkColorType:       return 1;
-        case kRGBA_F16_SkColorType:     return 8;
-    }
-    return 0;
-}
-
-static int SkColorTypeShiftPerPixel(SkColorType ct) {
-    switch (ct) {
-        case kUnknown_SkColorType:      return 0;
-        case kAlpha_8_SkColorType:      return 0;
-        case kRGB_565_SkColorType:      return 1;
-        case kARGB_4444_SkColorType:    return 1;
-        case kRGBA_8888_SkColorType:    return 2;
-        case kRGB_888x_SkColorType:     return 2;
-        case kBGRA_8888_SkColorType:    return 2;
-        case kRGBA_1010102_SkColorType: return 2;
-        case kRGB_101010x_SkColorType:  return 2;
-        case kGray_8_SkColorType:       return 0;
-        case kRGBA_F16_SkColorType:     return 3;
-    }
-    return 0;
-}
-
-static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
-    return width * SkColorTypeBytesPerPixel(ct);
-}
-
-static inline bool SkColorTypeIsValid(unsigned value) {
-    return value <= kLastEnum_SkColorType;
-}
-
-static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) {
-    if (kUnknown_SkColorType == ct) {
-        return 0;
-    }
-    return y * rowBytes + (x << SkColorTypeShiftPerPixel(ct));
-}
-
-///////////////////////////////////////////////////////////////////////////////
+/**
+ *  Returns the number of bytes-per-pixel for the specified colortype, or 0 if invalid.
+ */
+SK_API int SkColorTypeBytesPerPixel(SkColorType ct);
 
 /**
- *  Return true if alphaType is supported by colorType. If there is a canonical
- *  alphaType for this colorType, return it in canonical.
+ *  Tries to validate the colortype, alphatype pair. In all cases if it returns true, it
+ *  will set canonical to the "canonical" answer if it is non-null, and ignore the parameter if
+ *  it is set to null.
+ *
+ *  If the specified colortype has only 1 valid alphatype (e.g. 565 must always be opaque) then
+ *  canonical will be set to that valid alphatype.
+ *
+ *  If the specified colortype treats more than one alphatype the same (e.g. Alpha_8 colortype
+ *  treates Premul and Unpremul the same) and the specified alphatype is one of those,
+ *  then canonical will be set to the "canonical" answer (Premul in the case of Alpha_8 colortype).
+ *
+ *  If the colortype supports multiple alphatypes, and the specified alphatype is one of them,
+ *  then canonical will be set to the specified alphatype. If the specified alphatype is not
+ *  one of them (e.g. kUnknown_SkAlphaType is not valid for any colortype except
+ *  kUnknown_SkColorType), then the function returns false, and canonical's value is undefined.
  */
 bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
                                   SkAlphaType* canonical = nullptr);
 
+
 ///////////////////////////////////////////////////////////////////////////////
 
 /**
@@ -270,9 +234,8 @@
         return Make(fWidth, fHeight, fColorType, fAlphaType, std::move(cs));
     }
 
-    int bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }
-
-    int shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); }
+    int bytesPerPixel() const;
+    int shiftPerPixel() const;
 
     uint64_t minRowBytes64() const {
         return sk_64_mul(fWidth, this->bytesPerPixel());
@@ -286,11 +249,7 @@
         return sk_64_asS32(minRowBytes);
     }
 
-    size_t computeOffset(int x, int y, size_t rowBytes) const {
-        SkASSERT((unsigned)x < (unsigned)fWidth);
-        SkASSERT((unsigned)y < (unsigned)fHeight);
-        return SkColorTypeComputeOffset(fColorType, x, y, rowBytes);
-    }
+    size_t computeOffset(int x, int y, size_t rowBytes) const;
 
     bool operator==(const SkImageInfo& other) const {
         return fWidth == other.fWidth && fHeight == other.fHeight &&
diff --git a/include/core/SkPixmap.h b/include/core/SkPixmap.h
index f7fe710..c8b8300 100644
--- a/include/core/SkPixmap.h
+++ b/include/core/SkPixmap.h
@@ -279,7 +279,7 @@
         @return  readable unsigned 8-bit pointer to pixels
     */
     const uint8_t* addr8() const {
-        SkASSERT(1 == SkColorTypeBytesPerPixel(fInfo.colorType()));
+        SkASSERT(1 == fInfo.bytesPerPixel());
         return reinterpret_cast<const uint8_t*>(fPixels);
     }
 
@@ -292,7 +292,7 @@
         @return  readable unsigned 16-bit pointer to pixels
     */
     const uint16_t* addr16() const {
-        SkASSERT(2 == SkColorTypeBytesPerPixel(fInfo.colorType()));
+        SkASSERT(2 == fInfo.bytesPerPixel());
         return reinterpret_cast<const uint16_t*>(fPixels);
     }
 
@@ -305,7 +305,7 @@
         @return  readable unsigned 32-bit pointer to pixels
     */
     const uint32_t* addr32() const {
-        SkASSERT(4 == SkColorTypeBytesPerPixel(fInfo.colorType()));
+        SkASSERT(4 == fInfo.bytesPerPixel());
         return reinterpret_cast<const uint32_t*>(fPixels);
     }
 
@@ -318,7 +318,7 @@
         @return  readable unsigned 64-bit pointer to pixels
     */
     const uint64_t* addr64() const {
-        SkASSERT(8 == SkColorTypeBytesPerPixel(fInfo.colorType()));
+        SkASSERT(8 == fInfo.bytesPerPixel());
         return reinterpret_cast<const uint64_t*>(fPixels);
     }
 
@@ -332,7 +332,7 @@
         @return  readable unsigned 16-bit pointer to first component of pixels
     */
     const uint16_t* addrF16() const {
-        SkASSERT(8 == SkColorTypeBytesPerPixel(fInfo.colorType()));
+        SkASSERT(8 == fInfo.bytesPerPixel());
         SkASSERT(kRGBA_F16_SkColorType == fInfo.colorType());
         return reinterpret_cast<const uint16_t*>(fPixels);
     }
diff --git a/src/codec/SkCodec.cpp b/src/codec/SkCodec.cpp
index b2902ca..53fa6f4 100644
--- a/src/codec/SkCodec.cpp
+++ b/src/codec/SkCodec.cpp
@@ -214,7 +214,7 @@
         return;
     }
     const auto info = dstInfo.makeWH(frameRect.width(), frameRect.height());
-    const size_t bpp = SkColorTypeBytesPerPixel(dstInfo.colorType());
+    const size_t bpp = dstInfo.bytesPerPixel();
     const size_t offset = frameRect.x() * bpp + frameRect.y() * rowBytes;
     auto* eraseDst = SkTAddOffset<void>(pixels, offset);
     SkSampler::Fill(info, eraseDst, rowBytes, 0, SkCodec::kNo_ZeroInitialized);
diff --git a/src/codec/SkGifCodec.cpp b/src/codec/SkGifCodec.cpp
index dff8136..1118ed0 100644
--- a/src/codec/SkGifCodec.cpp
+++ b/src/codec/SkGifCodec.cpp
@@ -541,7 +541,7 @@
 
     // Tell the frame to copy the row data if need be.
     if (repeatCount > 1) {
-        const size_t bytesPerPixel = SkColorTypeBytesPerPixel(this->dstInfo().colorType());
+        const size_t bytesPerPixel = this->dstInfo().bytesPerPixel();
         const size_t bytesToCopy = fSwizzler->swizzleWidth() * bytesPerPixel;
         void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetBytes());
         void* dst = copiedLine;
diff --git a/src/codec/SkSwizzler.cpp b/src/codec/SkSwizzler.cpp
index f3d97af..4b350c4 100644
--- a/src/codec/SkSwizzler.cpp
+++ b/src/codec/SkSwizzler.cpp
@@ -786,7 +786,7 @@
     RowProc fastProc = nullptr;
     RowProc proc = nullptr;
     int srcBPP;
-    const int dstBPP = SkColorTypeBytesPerPixel(dstInfo.colorType());
+    const int dstBPP = dstInfo.bytesPerPixel();
     if (skipFormatConversion) {
         switch (encodedInfo.color()) {
             case SkEncodedInfo::kGray_Color:
diff --git a/src/codec/SkWebpCodec.cpp b/src/codec/SkWebpCodec.cpp
index 6618072..95ae9b9 100644
--- a/src/codec/SkWebpCodec.cpp
+++ b/src/codec/SkWebpCodec.cpp
@@ -617,7 +617,7 @@
     const bool needsSrgbToLinear = dstInfo.gammaCloseToSRGB() &&
             options.fPremulBehavior == SkTransferFunctionBehavior::kRespect;
 
-    const size_t dstBpp = SkColorTypeBytesPerPixel(dstInfo.colorType());
+    const size_t dstBpp = dstInfo.bytesPerPixel();
     dst = SkTAddOffset<void>(dst, dstBpp * dstX + rowBytes * dstY);
     const size_t srcRowBytes = config.output.u.RGBA.stride;
 
diff --git a/src/core/SkImageInfo.cpp b/src/core/SkImageInfo.cpp
index a3d352f..b684cdf 100644
--- a/src/core/SkImageInfo.cpp
+++ b/src/core/SkImageInfo.cpp
@@ -5,11 +5,28 @@
  * found in the LICENSE file.
  */
 
-#include "SkImageInfo.h"
+#include "SkImageInfoPriv.h"
 #include "SkSafeMath.h"
 #include "SkReadBuffer.h"
 #include "SkWriteBuffer.h"
 
+int SkColorTypeBytesPerPixel(SkColorType ct) {
+    switch (ct) {
+        case kUnknown_SkColorType:      return 0;
+        case kAlpha_8_SkColorType:      return 1;
+        case kRGB_565_SkColorType:      return 2;
+        case kARGB_4444_SkColorType:    return 2;
+        case kRGBA_8888_SkColorType:    return 4;
+        case kBGRA_8888_SkColorType:    return 4;
+        case kRGB_888x_SkColorType:     return 4;
+        case kRGBA_1010102_SkColorType: return 4;
+        case kRGB_101010x_SkColorType:  return 4;
+        case kGray_8_SkColorType:       return 1;
+        case kRGBA_F16_SkColorType:     return 8;
+    }
+    return 0;
+}
+
 // These values must be constant over revisions, though they can be renamed to reflect if/when
 // they are deprecated.
 enum Stored_SkColorType {
@@ -64,6 +81,16 @@
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
+int SkImageInfo::bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }
+
+int SkImageInfo::shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); }
+
+size_t SkImageInfo::computeOffset(int x, int y, size_t rowBytes) const {
+    SkASSERT((unsigned)x < (unsigned)fWidth);
+    SkASSERT((unsigned)y < (unsigned)fHeight);
+    return SkColorTypeComputeOffset(fColorType, x, y, rowBytes);
+}
+
 size_t SkImageInfo::computeByteSize(size_t rowBytes) const {
     if (0 == fHeight) {
         return 0;
diff --git a/src/core/SkImageInfoPriv.h b/src/core/SkImageInfoPriv.h
index 716c35d..bd0b990 100644
--- a/src/core/SkImageInfoPriv.h
+++ b/src/core/SkImageInfoPriv.h
@@ -10,6 +10,42 @@
 
 #include "SkImageInfo.h"
 
+static inline bool SkAlphaTypeIsValid(unsigned value) {
+    return value <= kLastEnum_SkAlphaType;
+}
+
+static int SkColorTypeShiftPerPixel(SkColorType ct) {
+    switch (ct) {
+        case kUnknown_SkColorType:      return 0;
+        case kAlpha_8_SkColorType:      return 0;
+        case kRGB_565_SkColorType:      return 1;
+        case kARGB_4444_SkColorType:    return 1;
+        case kRGBA_8888_SkColorType:    return 2;
+        case kRGB_888x_SkColorType:     return 2;
+        case kBGRA_8888_SkColorType:    return 2;
+        case kRGBA_1010102_SkColorType: return 2;
+        case kRGB_101010x_SkColorType:  return 2;
+        case kGray_8_SkColorType:       return 0;
+        case kRGBA_F16_SkColorType:     return 3;
+    }
+    return 0;
+}
+
+static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
+    return width * SkColorTypeBytesPerPixel(ct);
+}
+
+static inline bool SkColorTypeIsValid(unsigned value) {
+    return value <= kLastEnum_SkColorType;
+}
+
+static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) {
+    if (kUnknown_SkColorType == ct) {
+        return 0;
+    }
+    return y * rowBytes + (x << SkColorTypeShiftPerPixel(ct));
+}
+
 /**
  *  This contains shared checks on SkImageInfo.  Depending on the desired color space behavior,
  *  the caller should choose one of the two versions below.
diff --git a/src/core/SkMipMap.cpp b/src/core/SkMipMap.cpp
index cd81946..f9cb3f1 100644
--- a/src/core/SkMipMap.cpp
+++ b/src/core/SkMipMap.cpp
@@ -9,6 +9,7 @@
 #include "SkBitmap.h"
 #include "SkColorData.h"
 #include "SkHalf.h"
+#include "SkImageInfoPriv.h"
 #include "SkMathPriv.h"
 #include "SkNx.h"
 #include "SkPM4fPriv.h"
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index f352f24..50e466f 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -29,6 +29,7 @@
 #include "SkConvertPixels.h"
 #include "SkDeferredDisplayList.h"
 #include "SkGr.h"
+#include "SkImageInfoPriv.h"
 #include "SkJSONWriter.h"
 #include "SkMakeUnique.h"
 #include "SkTaskGroup.h"
diff --git a/src/image/SkSurface_Raster.cpp b/src/image/SkSurface_Raster.cpp
index c430ad9..47d5542 100644
--- a/src/image/SkSurface_Raster.cpp
+++ b/src/image/SkSurface_Raster.cpp
@@ -80,7 +80,7 @@
         return true;
     }
 
-    int shift = SkColorTypeShiftPerPixel(info.colorType());
+    int shift = info.shiftPerPixel();
 
     uint64_t minRB = (uint64_t)info.width() << shift;
     if (minRB > rowBytes) {
diff --git a/tests/ImageGeneratorTest.cpp b/tests/ImageGeneratorTest.cpp
index 7ee0b90..8a7f0de 100644
--- a/tests/ImageGeneratorTest.cpp
+++ b/tests/ImageGeneratorTest.cpp
@@ -9,6 +9,7 @@
 #include "SkCanvas.h"
 #include "SkGraphics.h"
 #include "SkImageGenerator.h"
+#include "SkImageInfoPriv.h"
 #include "Test.h"
 
 static bool gMyFactoryWasCalled;
diff --git a/tests/SwizzlerTest.cpp b/tests/SwizzlerTest.cpp
index 8950efb..e823e85 100644
--- a/tests/SwizzlerTest.cpp
+++ b/tests/SwizzlerTest.cpp
@@ -5,6 +5,7 @@
  * found in the LICENSE file.
  */
 
+#include "SkImageInfoPriv.h"
 #include "SkSwizzle.h"
 #include "SkSwizzler.h"
 #include "Test.h"
diff --git a/tools/skqp/gm_runner.cpp b/tools/skqp/gm_runner.cpp
index ce0586d..942c577 100644
--- a/tools/skqp/gm_runner.cpp
+++ b/tools/skqp/gm_runner.cpp
@@ -15,6 +15,7 @@
 #include "SkFontMgrPriv.h"
 #include "SkFontStyle.h"
 #include "SkGraphics.h"
+#include "SkImageInfoPriv.h"
 #include "SkSurface.h"
 #include "Test.h"
 #include "gl/GLTestContext.h"