rewrite SkMasks::CreateMasks to work in bytesPerPixel

This ought to make it a bit more clear that undefined
shifts (like 1<<31, 1<<32, 1<<33) are not possible,
only 1<<8, 1<<16, 1<<24.

Change-Id: Ia358f9204e5956ba6c23603c5961af86a991b659
Reviewed-on: https://skia-review.googlesource.com/c/192030
Reviewed-by: Leon Scroggins <scroggo@google.com>
Reviewed-by: Greg Kaiser <gkaiser@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
This commit is contained in:
Mike Klein 2019-02-13 12:25:55 -05:00 committed by Skia Commit-Bot
parent e317664bbd
commit 65e841715e
4 changed files with 25 additions and 23 deletions

View File

@ -520,7 +520,8 @@ SkCodec::Result SkBmpCodec::ReadHeader(SkStream* stream, bool inIco,
if (codecOut) {
// Check that input bit masks are valid and create the masks object
std::unique_ptr<SkMasks> masks(SkMasks::CreateMasks(inputMasks, bitsPerPixel));
SkASSERT(bitsPerPixel % 8 == 0);
std::unique_ptr<SkMasks> masks(SkMasks::CreateMasks(inputMasks, bitsPerPixel/8));
if (nullptr == masks) {
SkCodecPrintf("Error: invalid input masks.\n");
return kInvalidInput;

View File

@ -86,7 +86,7 @@ uint8_t SkMasks::getAlpha(uint32_t pixel) const {
* Process an input mask to obtain the necessary information
*
*/
const SkMasks::MaskInfo process_mask(uint32_t mask, uint32_t bpp) {
static const SkMasks::MaskInfo process_mask(uint32_t mask) {
// Determine properties of the mask
uint32_t tempMask = mask;
uint32_t shift = 0;
@ -116,9 +116,7 @@ const SkMasks::MaskInfo process_mask(uint32_t mask, uint32_t bpp) {
}
}
// Save the calculated values
const SkMasks::MaskInfo info = { mask, shift, size };
return info;
return { mask, shift, size };
}
/*
@ -126,29 +124,32 @@ const SkMasks::MaskInfo process_mask(uint32_t mask, uint32_t bpp) {
* Create the masks object
*
*/
SkMasks* SkMasks::CreateMasks(InputMasks masks, uint32_t bitsPerPixel) {
// Trim the input masks according to bitsPerPixel
if (bitsPerPixel < 32) {
masks.red &= (1 << bitsPerPixel) - 1;
SkMasks* SkMasks::CreateMasks(InputMasks masks, int bytesPerPixel) {
SkASSERT(0 < bytesPerPixel && bytesPerPixel <= 4);
// Trim the input masks to match bytesPerPixel.
if (bytesPerPixel < 4) {
int bitsPerPixel = 8*bytesPerPixel;
masks.red &= (1 << bitsPerPixel) - 1;
masks.green &= (1 << bitsPerPixel) - 1;
masks.blue &= (1 << bitsPerPixel) - 1;
masks.blue &= (1 << bitsPerPixel) - 1;
masks.alpha &= (1 << bitsPerPixel) - 1;
}
// Check that masks do not overlap
if (((masks.red & masks.green) | (masks.red & masks.blue) |
(masks.red & masks.alpha) | (masks.green & masks.blue) |
(masks.green & masks.alpha) | (masks.blue & masks.alpha)) != 0) {
// Check that masks do not overlap.
if (((masks.red & masks.green) |
(masks.red & masks.blue ) |
(masks.red & masks.alpha) |
(masks.green & masks.blue ) |
(masks.green & masks.alpha) |
(masks.blue & masks.alpha) ) != 0) {
return nullptr;
}
// Collect information about the masks
const MaskInfo red = process_mask(masks.red, bitsPerPixel);
const MaskInfo green = process_mask(masks.green, bitsPerPixel);
const MaskInfo blue = process_mask(masks.blue, bitsPerPixel);
const MaskInfo alpha = process_mask(masks.alpha, bitsPerPixel);
return new SkMasks(red, green, blue, alpha);
return new SkMasks(process_mask(masks.red ),
process_mask(masks.green),
process_mask(masks.blue ),
process_mask(masks.alpha));
}

View File

@ -45,7 +45,7 @@ public:
* Create the masks object
*
*/
static SkMasks* CreateMasks(InputMasks masks, uint32_t bpp);
static SkMasks* CreateMasks(InputMasks masks, int bytesPerPixel);
/*
*

View File

@ -17,7 +17,7 @@
GrStrikeCache::GrStrikeCache(const GrCaps* caps, size_t maxTextureBytes)
: fPreserveStrike(nullptr)
, f565Masks(SkMasks::CreateMasks({0xF800, 0x07E0, 0x001F, 0},
GrMaskFormatBytesPerPixel(kA565_GrMaskFormat) * 8)) { }
GrMaskFormatBytesPerPixel(kA565_GrMaskFormat))) { }
GrStrikeCache::~GrStrikeCache() {
StrikeHash::Iter iter(&fCache);