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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright 2007 The Android Open Source Project
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkMask.h"
michael@0 9
michael@0 10 /** returns the product if it is positive and fits in 31 bits. Otherwise this
michael@0 11 returns 0.
michael@0 12 */
michael@0 13 static int32_t safeMul32(int32_t a, int32_t b) {
michael@0 14 int64_t size = sk_64_mul(a, b);
michael@0 15 if (size > 0 && sk_64_isS32(size)) {
michael@0 16 return sk_64_asS32(size);
michael@0 17 }
michael@0 18 return 0;
michael@0 19 }
michael@0 20
michael@0 21 size_t SkMask::computeImageSize() const {
michael@0 22 return safeMul32(fBounds.height(), fRowBytes);
michael@0 23 }
michael@0 24
michael@0 25 size_t SkMask::computeTotalImageSize() const {
michael@0 26 size_t size = this->computeImageSize();
michael@0 27 if (fFormat == SkMask::k3D_Format) {
michael@0 28 size = safeMul32(SkToS32(size), 3);
michael@0 29 }
michael@0 30 return size;
michael@0 31 }
michael@0 32
michael@0 33 /** We explicitly use this allocator for SkBimap pixels, so that we can
michael@0 34 freely assign memory allocated by one class to the other.
michael@0 35 */
michael@0 36 uint8_t* SkMask::AllocImage(size_t size) {
michael@0 37 return (uint8_t*)sk_malloc_throw(SkAlign4(size));
michael@0 38 }
michael@0 39
michael@0 40 /** We explicitly use this allocator for SkBimap pixels, so that we can
michael@0 41 freely assign memory allocated by one class to the other.
michael@0 42 */
michael@0 43 void SkMask::FreeImage(void* image) {
michael@0 44 sk_free(image);
michael@0 45 }
michael@0 46
michael@0 47 ///////////////////////////////////////////////////////////////////////////////
michael@0 48
michael@0 49 static const int gMaskFormatToShift[] = {
michael@0 50 ~0, // BW -- not supported
michael@0 51 0, // A8
michael@0 52 0, // 3D
michael@0 53 2, // ARGB32
michael@0 54 1, // LCD16
michael@0 55 2 // LCD32
michael@0 56 };
michael@0 57
michael@0 58 static int maskFormatToShift(SkMask::Format format) {
michael@0 59 SkASSERT((unsigned)format < SK_ARRAY_COUNT(gMaskFormatToShift));
michael@0 60 SkASSERT(SkMask::kBW_Format != format);
michael@0 61 return gMaskFormatToShift[format];
michael@0 62 }
michael@0 63
michael@0 64 void* SkMask::getAddr(int x, int y) const {
michael@0 65 SkASSERT(kBW_Format != fFormat);
michael@0 66 SkASSERT(fBounds.contains(x, y));
michael@0 67 SkASSERT(fImage);
michael@0 68
michael@0 69 char* addr = (char*)fImage;
michael@0 70 addr += (y - fBounds.fTop) * fRowBytes;
michael@0 71 addr += (x - fBounds.fLeft) << maskFormatToShift(fFormat);
michael@0 72 return addr;
michael@0 73 }

mercurial