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