diff options
author | Mike Reed <reed@google.com> | 2018-02-09 13:26:46 -0500 |
---|---|---|
committer | Mike Reed <reed@google.com> | 2018-02-09 20:38:32 +0000 |
commit | 7fcfb621998648ba018e3b89e2cab3135bd46a1f (patch) | |
tree | ce43ae14602539ef7b541e91cc1372c1ee4b4e0e | |
parent | 47cf048abecb5064b9e851ea01f75b23797c1611 (diff) |
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>
-rw-r--r-- | dm/DMSrcSink.cpp | 7 | ||||
-rw-r--r-- | gm/encode-alpha-jpeg.cpp | 1 | ||||
-rw-r--r-- | gm/readpixels.cpp | 1 | ||||
-rw-r--r-- | include/core/SkImageInfo.h | 87 | ||||
-rw-r--r-- | include/core/SkPixmap.h | 10 | ||||
-rw-r--r-- | src/codec/SkCodec.cpp | 2 | ||||
-rw-r--r-- | src/codec/SkGifCodec.cpp | 2 | ||||
-rw-r--r-- | src/codec/SkSwizzler.cpp | 2 | ||||
-rw-r--r-- | src/codec/SkWebpCodec.cpp | 2 | ||||
-rw-r--r-- | src/core/SkImageInfo.cpp | 29 | ||||
-rw-r--r-- | src/core/SkImageInfoPriv.h | 36 | ||||
-rw-r--r-- | src/core/SkMipMap.cpp | 1 | ||||
-rw-r--r-- | src/gpu/GrContext.cpp | 1 | ||||
-rw-r--r-- | src/image/SkSurface_Raster.cpp | 2 | ||||
-rw-r--r-- | tests/ImageGeneratorTest.cpp | 1 | ||||
-rw-r--r-- | tests/SwizzlerTest.cpp | 1 | ||||
-rw-r--r-- | tools/skqp/gm_runner.cpp | 1 |
17 files changed, 108 insertions, 78 deletions
diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp index f2efbad7ac..0dd35ff169 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 @@ Error CodecSrc::draw(SkCanvas* canvas) const { } 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 @@ Error AndroidCodecSrc::draw(SkCanvas* canvas) const { } 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 @@ Error ImageGenSrc::draw(SkCanvas* canvas) const { 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 dc07bf34d7..6d26d7a85c 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 254b8fbeb4..c112314df1 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 6876c8bb04..c21c3df8c9 100644 --- a/include/core/SkImageInfo.h +++ b/include/core/SkImageInfo.h @@ -52,10 +52,6 @@ static inline bool SkAlphaTypeIsOpaque(SkAlphaType at) { 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 @@ enum SkColorType { #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 @@ public: 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 @@ public: 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 f7fe710c6e..c8b83004f0 100644 --- a/include/core/SkPixmap.h +++ b/include/core/SkPixmap.h @@ -279,7 +279,7 @@ public: @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 @@ public: @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 @@ public: @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 @@ public: @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 @@ public: @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 b2902ca75f..53fa6f4643 100644 --- a/src/codec/SkCodec.cpp +++ b/src/codec/SkCodec.cpp @@ -214,7 +214,7 @@ static void zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes, 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 dff8136d75..1118ed0e7d 100644 --- a/src/codec/SkGifCodec.cpp +++ b/src/codec/SkGifCodec.cpp @@ -541,7 +541,7 @@ void SkGifCodec::haveDecodedRow(int frameIndex, const unsigned char* rowBegin, // 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 f3d97af3d8..4b350c4ec4 100644 --- a/src/codec/SkSwizzler.cpp +++ b/src/codec/SkSwizzler.cpp @@ -786,7 +786,7 @@ SkSwizzler* SkSwizzler::CreateSwizzler(const SkEncodedInfo& encodedInfo, 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 6618072c8a..95ae9b9593 100644 --- a/src/codec/SkWebpCodec.cpp +++ b/src/codec/SkWebpCodec.cpp @@ -617,7 +617,7 @@ SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, 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 a3d352f05f..b684cdfcf4 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 @@ static SkColorType stored_to_live(unsigned stored) { /////////////////////////////////////////////////////////////////////////////////////////////////// +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 716c35d43d..bd0b990d0e 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 cd81946a52..f9cb3f1cf5 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 f352f24b36..50e466f8f1 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 c430ad90b0..47d5542ac6 100644 --- a/src/image/SkSurface_Raster.cpp +++ b/src/image/SkSurface_Raster.cpp @@ -80,7 +80,7 @@ bool SkSurfaceValidateRasterInfo(const SkImageInfo& info, size_t rowBytes) { 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 7ee0b90c4c..8a7f0decec 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 8950efb153..e823e8559f 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 ce0586d490..942c577cf3 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" |