gfx/skia/trunk/src/core/SkMask.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/core/SkMask.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,73 @@
     1.4 +/*
     1.5 + * Copyright 2007 The Android Open Source Project
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#include "SkMask.h"
    1.12 +
    1.13 +/** returns the product if it is positive and fits in 31 bits. Otherwise this
    1.14 +    returns 0.
    1.15 + */
    1.16 +static int32_t safeMul32(int32_t a, int32_t b) {
    1.17 +    int64_t size = sk_64_mul(a, b);
    1.18 +    if (size > 0 && sk_64_isS32(size)) {
    1.19 +        return sk_64_asS32(size);
    1.20 +    }
    1.21 +    return 0;
    1.22 +}
    1.23 +
    1.24 +size_t SkMask::computeImageSize() const {
    1.25 +    return safeMul32(fBounds.height(), fRowBytes);
    1.26 +}
    1.27 +
    1.28 +size_t SkMask::computeTotalImageSize() const {
    1.29 +    size_t size = this->computeImageSize();
    1.30 +    if (fFormat == SkMask::k3D_Format) {
    1.31 +        size = safeMul32(SkToS32(size), 3);
    1.32 +    }
    1.33 +    return size;
    1.34 +}
    1.35 +
    1.36 +/** We explicitly use this allocator for SkBimap pixels, so that we can
    1.37 +    freely assign memory allocated by one class to the other.
    1.38 +*/
    1.39 +uint8_t* SkMask::AllocImage(size_t size) {
    1.40 +    return (uint8_t*)sk_malloc_throw(SkAlign4(size));
    1.41 +}
    1.42 +
    1.43 +/** We explicitly use this allocator for SkBimap pixels, so that we can
    1.44 +    freely assign memory allocated by one class to the other.
    1.45 +*/
    1.46 +void SkMask::FreeImage(void* image) {
    1.47 +    sk_free(image);
    1.48 +}
    1.49 +
    1.50 +///////////////////////////////////////////////////////////////////////////////
    1.51 +
    1.52 +static const int gMaskFormatToShift[] = {
    1.53 +    ~0, // BW -- not supported
    1.54 +    0,  // A8
    1.55 +    0,  // 3D
    1.56 +    2,  // ARGB32
    1.57 +    1,  // LCD16
    1.58 +    2   // LCD32
    1.59 +};
    1.60 +
    1.61 +static int maskFormatToShift(SkMask::Format format) {
    1.62 +    SkASSERT((unsigned)format < SK_ARRAY_COUNT(gMaskFormatToShift));
    1.63 +    SkASSERT(SkMask::kBW_Format != format);
    1.64 +    return gMaskFormatToShift[format];
    1.65 +}
    1.66 +
    1.67 +void* SkMask::getAddr(int x, int y) const {
    1.68 +    SkASSERT(kBW_Format != fFormat);
    1.69 +    SkASSERT(fBounds.contains(x, y));
    1.70 +    SkASSERT(fImage);
    1.71 +
    1.72 +    char* addr = (char*)fImage;
    1.73 +    addr += (y - fBounds.fTop) * fRowBytes;
    1.74 +    addr += (x - fBounds.fLeft) << maskFormatToShift(fFormat);
    1.75 +    return addr;
    1.76 +}

mercurial