diff --git a/gm/imagealphathreshold.cpp b/gm/imagealphathreshold.cpp index 3783bfcb3a..4bdf6eb5a2 100644 --- a/gm/imagealphathreshold.cpp +++ b/gm/imagealphathreshold.cpp @@ -89,7 +89,7 @@ private: // Create a 'width' x 'height' SkSurface that matches the colorType of 'canvas' as // best we can static sk_sp make_color_matching_surface(SkCanvas* canvas, int width, int height, - SkAlphaType alphaType) { + SkAlphaType at) { SkColorType ct = canvas->imageInfo().colorType(); sk_sp cs(canvas->imageInfo().refColorSpace()); @@ -98,9 +98,11 @@ static sk_sp make_color_matching_surface(SkCanvas* canvas, int width, // For backends that aren't yet color-space aware we just fallback to N32. ct = kN32_SkColorType; cs = nullptr; + } else if (SkColorTypeIsAlwaysOpaque(ct)) { + at = kOpaque_SkAlphaType; } - SkImageInfo info = SkImageInfo::Make(width, height, ct, alphaType, std::move(cs)); + SkImageInfo info = SkImageInfo::Make(width, height, ct, at, std::move(cs)); return sk_tool_utils::makeSurface(canvas, info); } diff --git a/include/core/SkImageInfo.h b/include/core/SkImageInfo.h index c21c3df8c9..cf9d244169 100644 --- a/include/core/SkImageInfo.h +++ b/include/core/SkImageInfo.h @@ -93,6 +93,11 @@ enum SkColorType { */ SK_API int SkColorTypeBytesPerPixel(SkColorType ct); +/** + * Returns true iff the colortype is always considered opaque (i.e. does not store alpha). + */ +SK_API bool SkColorTypeIsAlwaysOpaque(SkColorType ct); + /** * 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 @@ -110,8 +115,8 @@ SK_API int SkColorTypeBytesPerPixel(SkColorType ct); * 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); +SK_API bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType, + SkAlphaType* canonical = nullptr); /////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/SkImageInfo.cpp b/src/core/SkImageInfo.cpp index b684cdfcf4..0b06334484 100644 --- a/src/core/SkImageInfo.cpp +++ b/src/core/SkImageInfo.cpp @@ -79,6 +79,19 @@ static SkColorType stored_to_live(unsigned stored) { return kUnknown_SkColorType; } +bool SkColorTypeIsAlwaysOpaque(SkColorType ct) { + switch (ct) { + case kRGB_565_SkColorType: + case kRGB_888x_SkColorType: + case kRGB_101010x_SkColorType: + case kGray_8_SkColorType: + return true; + default: + break; + } + return false; +} + /////////////////////////////////////////////////////////////////////////////////////////////////// int SkImageInfo::bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }