2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkMask.h"
|
|
|
|
|
2015-02-04 17:07:17 +00:00
|
|
|
//#define TRACK_SKMASK_LIFETIME
|
|
|
|
|
2009-04-24 12:43:40 +00:00
|
|
|
/** returns the product if it is positive and fits in 31 bits. Otherwise this
|
|
|
|
returns 0.
|
|
|
|
*/
|
|
|
|
static int32_t safeMul32(int32_t a, int32_t b) {
|
2014-01-13 14:53:55 +00:00
|
|
|
int64_t size = sk_64_mul(a, b);
|
|
|
|
if (size > 0 && sk_64_isS32(size)) {
|
|
|
|
return sk_64_asS32(size);
|
2009-04-24 12:43:40 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2009-04-24 12:43:40 +00:00
|
|
|
size_t SkMask::computeImageSize() const {
|
|
|
|
return safeMul32(fBounds.height(), fRowBytes);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2009-04-24 12:43:40 +00:00
|
|
|
size_t SkMask::computeTotalImageSize() const {
|
|
|
|
size_t size = this->computeImageSize();
|
|
|
|
if (fFormat == SkMask::k3D_Format) {
|
2014-01-24 21:46:29 +00:00
|
|
|
size = safeMul32(SkToS32(size), 3);
|
2009-04-24 12:43:40 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2015-02-04 17:07:17 +00:00
|
|
|
#ifdef TRACK_SKMASK_LIFETIME
|
|
|
|
static int gCounter;
|
|
|
|
#endif
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** We explicitly use this allocator for SkBimap pixels, so that we can
|
|
|
|
freely assign memory allocated by one class to the other.
|
|
|
|
*/
|
2011-04-09 19:39:25 +00:00
|
|
|
uint8_t* SkMask::AllocImage(size_t size) {
|
2015-02-04 17:07:17 +00:00
|
|
|
#ifdef TRACK_SKMASK_LIFETIME
|
|
|
|
SkDebugf("SkMask::AllocImage %d\n", gCounter++);
|
|
|
|
#endif
|
2008-12-17 15:59:43 +00:00
|
|
|
return (uint8_t*)sk_malloc_throw(SkAlign4(size));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** We explicitly use this allocator for SkBimap pixels, so that we can
|
|
|
|
freely assign memory allocated by one class to the other.
|
|
|
|
*/
|
2011-04-09 19:39:25 +00:00
|
|
|
void SkMask::FreeImage(void* image) {
|
2015-02-04 17:07:17 +00:00
|
|
|
#ifdef TRACK_SKMASK_LIFETIME
|
|
|
|
if (image) {
|
|
|
|
SkDebugf("SkMask::FreeImage %d\n", --gCounter);
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-17 15:59:43 +00:00
|
|
|
sk_free(image);
|
|
|
|
}
|
|
|
|
|
2011-10-18 17:29:44 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static const int gMaskFormatToShift[] = {
|
|
|
|
~0, // BW -- not supported
|
|
|
|
0, // A8
|
|
|
|
0, // 3D
|
|
|
|
2, // ARGB32
|
|
|
|
1, // LCD16
|
|
|
|
};
|
|
|
|
|
|
|
|
static int maskFormatToShift(SkMask::Format format) {
|
|
|
|
SkASSERT((unsigned)format < SK_ARRAY_COUNT(gMaskFormatToShift));
|
|
|
|
SkASSERT(SkMask::kBW_Format != format);
|
|
|
|
return gMaskFormatToShift[format];
|
|
|
|
}
|
|
|
|
|
|
|
|
void* SkMask::getAddr(int x, int y) const {
|
|
|
|
SkASSERT(kBW_Format != fFormat);
|
|
|
|
SkASSERT(fBounds.contains(x, y));
|
|
|
|
SkASSERT(fImage);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-10-18 17:29:44 +00:00
|
|
|
char* addr = (char*)fImage;
|
|
|
|
addr += (y - fBounds.fTop) * fRowBytes;
|
|
|
|
addr += (x - fBounds.fLeft) << maskFormatToShift(fFormat);
|
|
|
|
return addr;
|
|
|
|
}
|