michael@0: /* michael@0: * Copyright 2007 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkMask.h" michael@0: michael@0: /** returns the product if it is positive and fits in 31 bits. Otherwise this michael@0: returns 0. michael@0: */ michael@0: static int32_t safeMul32(int32_t a, int32_t b) { michael@0: int64_t size = sk_64_mul(a, b); michael@0: if (size > 0 && sk_64_isS32(size)) { michael@0: return sk_64_asS32(size); michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: size_t SkMask::computeImageSize() const { michael@0: return safeMul32(fBounds.height(), fRowBytes); michael@0: } michael@0: michael@0: size_t SkMask::computeTotalImageSize() const { michael@0: size_t size = this->computeImageSize(); michael@0: if (fFormat == SkMask::k3D_Format) { michael@0: size = safeMul32(SkToS32(size), 3); michael@0: } michael@0: return size; michael@0: } michael@0: michael@0: /** We explicitly use this allocator for SkBimap pixels, so that we can michael@0: freely assign memory allocated by one class to the other. michael@0: */ michael@0: uint8_t* SkMask::AllocImage(size_t size) { michael@0: return (uint8_t*)sk_malloc_throw(SkAlign4(size)); michael@0: } michael@0: michael@0: /** We explicitly use this allocator for SkBimap pixels, so that we can michael@0: freely assign memory allocated by one class to the other. michael@0: */ michael@0: void SkMask::FreeImage(void* image) { michael@0: sk_free(image); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static const int gMaskFormatToShift[] = { michael@0: ~0, // BW -- not supported michael@0: 0, // A8 michael@0: 0, // 3D michael@0: 2, // ARGB32 michael@0: 1, // LCD16 michael@0: 2 // LCD32 michael@0: }; michael@0: michael@0: static int maskFormatToShift(SkMask::Format format) { michael@0: SkASSERT((unsigned)format < SK_ARRAY_COUNT(gMaskFormatToShift)); michael@0: SkASSERT(SkMask::kBW_Format != format); michael@0: return gMaskFormatToShift[format]; michael@0: } michael@0: michael@0: void* SkMask::getAddr(int x, int y) const { michael@0: SkASSERT(kBW_Format != fFormat); michael@0: SkASSERT(fBounds.contains(x, y)); michael@0: SkASSERT(fImage); michael@0: michael@0: char* addr = (char*)fImage; michael@0: addr += (y - fBounds.fTop) * fRowBytes; michael@0: addr += (x - fBounds.fLeft) << maskFormatToShift(fFormat); michael@0: return addr; michael@0: }