move zero-init to sk_malloc for masks

Bug: skia:
Change-Id: I75d557068bdcd9e9f7e380e4fa447f9d83dd1554
Reviewed-on: https://skia-review.googlesource.com/98200
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2018-01-22 13:34:53 -05:00 committed by Skia Commit-Bot
parent ec97ac9be2
commit baafcdcd54
5 changed files with 18 additions and 30 deletions

View File

@ -114,7 +114,11 @@ struct SkMask {
*/
void* getAddr(int x, int y) const;
static uint8_t* AllocImage(size_t bytes);
enum AllocType {
kUninit_Alloc,
kZeroInit_Alloc,
};
static uint8_t* AllocImage(size_t bytes, AllocType = kUninit_Alloc);
static void FreeImage(void* image);
enum CreateMode {

View File

@ -1730,8 +1730,7 @@ bool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
// we're too big to allocate the mask, abort
return false;
}
mask->fImage = SkMask::AllocImage(size);
memset(mask->fImage, 0, mask->computeImageSize());
mask->fImage = SkMask::AllocImage(size, SkMask::kZeroInit_Alloc);
}
if (SkMask::kJustComputeBounds_CreateMode != mode) {

View File

@ -6,10 +6,8 @@
*/
#include "SkMask.h"
#include "SkMalloc.h"
//#define TRACK_SKMASK_LIFETIME
#include "SkSafeMath.h"
/** returns the product if it is positive and fits in 31 bits. Otherwise this
returns 0.
@ -34,36 +32,22 @@ size_t SkMask::computeTotalImageSize() const {
return size;
}
#ifdef TRACK_SKMASK_LIFETIME
static int gCounter;
#endif
/** We explicitly use this allocator for SkBimap pixels, so that we can
freely assign memory allocated by one class to the other.
*/
uint8_t* SkMask::AllocImage(size_t size) {
#ifdef TRACK_SKMASK_LIFETIME
SkDebugf("SkMask::AllocImage %d\n", gCounter++);
#endif
size_t aligned_size = std::numeric_limits<size_t>::max();
// Expand size to next multiple of four.
size_t adjustment = 3;
if (size + adjustment > size) {
aligned_size = (size + adjustment) & ~adjustment;
uint8_t* SkMask::AllocImage(size_t size, AllocType at) {
size_t aligned_size = SkSafeMath::Align4(size);
unsigned flags = SK_MALLOC_THROW;
if (at == kZeroInit_Alloc) {
flags |= SK_MALLOC_ZERO_INITIALIZE;
}
return static_cast<uint8_t*>(sk_malloc_throw(aligned_size));
return static_cast<uint8_t*>(sk_malloc_flags(aligned_size, flags));
}
/** We explicitly use this allocator for SkBimap pixels, so that we can
freely assign memory allocated by one class to the other.
*/
void SkMask::FreeImage(void* image) {
#ifdef TRACK_SKMASK_LIFETIME
if (image) {
SkDebugf("SkMask::FreeImage %d\n", --gCounter);
}
#endif
sk_free(image);
}

View File

@ -51,6 +51,10 @@ public:
// These saturate to their results
static size_t Add(size_t x, size_t y);
static size_t Mul(size_t x, size_t y);
static size_t Align4(size_t x) {
SkSafeMath safe;
return safe.alignUp(x, 4);
}
private:
uint32_t mul32(uint32_t x, uint32_t y) {

View File

@ -347,13 +347,10 @@ static bool prepare_to_draw_into_mask(const SkRect& bounds, SkMask* mask) {
mask->fRowBytes = SkAlign4(mask->fBounds.width());
mask->fFormat = SkMask::kA8_Format;
const size_t size = mask->computeImageSize();
mask->fImage = SkMask::AllocImage(size);
mask->fImage = SkMask::AllocImage(size, SkMask::kZeroInit_Alloc);
if (nullptr == mask->fImage) {
return false;
}
// FIXME: use sk_calloc in AllocImage?
sk_bzero(mask->fImage, size);
return true;
}