Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2013 Google Inc.
      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 "SkMipMap.h"
      9 #include "SkBitmap.h"
     10 #include "SkColorPriv.h"
     11 
     12 static void downsampleby2_proc32(SkBitmap* dst, int x, int y,
     13                                  const SkBitmap& src) {
     14     x <<= 1;
     15     y <<= 1;
     16     const SkPMColor* p = src.getAddr32(x, y);
     17     const SkPMColor* baseP = p;
     18     SkPMColor c, ag, rb;
     19 
     20     c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
     21     if (x < src.width() - 1) {
     22         p += 1;
     23     }
     24     c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
     25 
     26     p = baseP;
     27     if (y < src.height() - 1) {
     28         p += src.rowBytes() >> 2;
     29     }
     30     c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
     31     if (x < src.width() - 1) {
     32         p += 1;
     33     }
     34     c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
     35 
     36     *dst->getAddr32(x >> 1, y >> 1) =
     37     ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
     38 }
     39 
     40 static inline uint32_t expand16(U16CPU c) {
     41     return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
     42 }
     43 
     44 // returns dirt in the top 16bits, but we don't care, since we only
     45 // store the low 16bits.
     46 static inline U16CPU pack16(uint32_t c) {
     47     return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
     48 }
     49 
     50 static void downsampleby2_proc16(SkBitmap* dst, int x, int y,
     51                                  const SkBitmap& src) {
     52     x <<= 1;
     53     y <<= 1;
     54     const uint16_t* p = src.getAddr16(x, y);
     55     const uint16_t* baseP = p;
     56     SkPMColor       c;
     57 
     58     c = expand16(*p);
     59     if (x < src.width() - 1) {
     60         p += 1;
     61     }
     62     c += expand16(*p);
     63 
     64     p = baseP;
     65     if (y < src.height() - 1) {
     66         p += src.rowBytes() >> 1;
     67     }
     68     c += expand16(*p);
     69     if (x < src.width() - 1) {
     70         p += 1;
     71     }
     72     c += expand16(*p);
     73 
     74     *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2);
     75 }
     76 
     77 static uint32_t expand4444(U16CPU c) {
     78     return (c & 0xF0F) | ((c & ~0xF0F) << 12);
     79 }
     80 
     81 static U16CPU collaps4444(uint32_t c) {
     82     return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
     83 }
     84 
     85 static void downsampleby2_proc4444(SkBitmap* dst, int x, int y,
     86                                    const SkBitmap& src) {
     87     x <<= 1;
     88     y <<= 1;
     89     const uint16_t* p = src.getAddr16(x, y);
     90     const uint16_t* baseP = p;
     91     uint32_t        c;
     92 
     93     c = expand4444(*p);
     94     if (x < src.width() - 1) {
     95         p += 1;
     96     }
     97     c += expand4444(*p);
     98 
     99     p = baseP;
    100     if (y < src.height() - 1) {
    101         p += src.rowBytes() >> 1;
    102     }
    103     c += expand4444(*p);
    104     if (x < src.width() - 1) {
    105         p += 1;
    106     }
    107     c += expand4444(*p);
    108 
    109     *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2);
    110 }
    111 
    112 static bool isPos32Bits(const Sk64& value) {
    113     return !value.isNeg() && value.is32();
    114 }
    115 
    116 SkMipMap::Level* SkMipMap::AllocLevels(int levelCount, size_t pixelSize) {
    117     if (levelCount < 0) {
    118         return NULL;
    119     }
    120     Sk64 size;
    121     size.setMul(levelCount + 1, sizeof(Level));
    122     size.add(SkToS32(pixelSize));
    123     if (!isPos32Bits(size)) {
    124         return NULL;
    125     }
    126     return (Level*)sk_malloc_throw(size.get32());
    127 }
    128 
    129 SkMipMap* SkMipMap::Build(const SkBitmap& src) {
    130     void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src);
    131 
    132     const SkBitmap::Config config = src.config();
    133     switch (config) {
    134         case SkBitmap::kARGB_8888_Config:
    135             proc = downsampleby2_proc32;
    136             break;
    137         case SkBitmap::kRGB_565_Config:
    138             proc = downsampleby2_proc16;
    139             break;
    140         case SkBitmap::kARGB_4444_Config:
    141             proc = downsampleby2_proc4444;
    142             break;
    143         case SkBitmap::kIndex8_Config:
    144         case SkBitmap::kA8_Config:
    145         default:
    146             return NULL; // don't build mipmaps for these configs
    147     }
    148 
    149     SkAutoLockPixels alp(src);
    150     if (!src.readyToDraw()) {
    151         return NULL;
    152     }
    153 
    154     // whip through our loop to compute the exact size needed
    155     size_t  size = 0;
    156     int     countLevels = 0;
    157     {
    158         int width = src.width();
    159         int height = src.height();
    160         for (;;) {
    161             width >>= 1;
    162             height >>= 1;
    163             if (0 == width || 0 == height) {
    164                 break;
    165             }
    166             size += SkBitmap::ComputeRowBytes(config, width) * height;
    167             countLevels += 1;
    168         }
    169     }
    170     if (0 == countLevels) {
    171         return NULL;
    172     }
    173 
    174     Level* levels = SkMipMap::AllocLevels(countLevels, size);
    175     if (NULL == levels) {
    176         return NULL;
    177     }
    178 
    179     uint8_t*    baseAddr = (uint8_t*)&levels[countLevels];
    180     uint8_t*    addr = baseAddr;
    181     int         width = src.width();
    182     int         height = src.height();
    183     uint32_t    rowBytes;
    184     SkBitmap    srcBM(src);
    185 
    186     for (int i = 0; i < countLevels; ++i) {
    187         width >>= 1;
    188         height >>= 1;
    189         rowBytes = SkToU32(SkBitmap::ComputeRowBytes(config, width));
    190 
    191         levels[i].fPixels   = addr;
    192         levels[i].fWidth    = width;
    193         levels[i].fHeight   = height;
    194         levels[i].fRowBytes = rowBytes;
    195         levels[i].fScale    = (float)width / src.width();
    196 
    197         SkBitmap dstBM;
    198         dstBM.setConfig(config, width, height, rowBytes);
    199         dstBM.setPixels(addr);
    200 
    201         srcBM.lockPixels();
    202         for (int y = 0; y < height; y++) {
    203             for (int x = 0; x < width; x++) {
    204                 proc(&dstBM, x, y, srcBM);
    205             }
    206         }
    207         srcBM.unlockPixels();
    208 
    209         srcBM = dstBM;
    210         addr += height * rowBytes;
    211     }
    212     SkASSERT(addr == baseAddr + size);
    213 
    214     return SkNEW_ARGS(SkMipMap, (levels, countLevels, size));
    215 }
    216 
    217 ///////////////////////////////////////////////////////////////////////////////
    218 
    219 //static int gCounter;
    220 
    221 SkMipMap::SkMipMap(Level* levels, int count, size_t size)
    222     : fSize(size), fLevels(levels), fCount(count) {
    223     SkASSERT(levels);
    224     SkASSERT(count > 0);
    225 //    SkDebugf("mips %d\n", ++gCounter);
    226 }
    227 
    228 SkMipMap::~SkMipMap() {
    229     sk_free(fLevels);
    230 //    SkDebugf("mips %d\n", --gCounter);
    231 }
    232 
    233 static SkFixed compute_level(SkScalar scale) {
    234     SkFixed s = SkAbs32(SkScalarToFixed(SkScalarInvert(scale)));
    235 
    236     if (s < SK_Fixed1) {
    237         return 0;
    238     }
    239     int clz = SkCLZ(s);
    240     SkASSERT(clz >= 1 && clz <= 15);
    241     return SkIntToFixed(15 - clz) + ((unsigned)(s << (clz + 1)) >> 16);
    242 }
    243 
    244 bool SkMipMap::extractLevel(SkScalar scale, Level* levelPtr) const {
    245     if (scale >= SK_Scalar1) {
    246         return false;
    247     }
    248 
    249     int level = compute_level(scale) >> 16;
    250     SkASSERT(level >= 0);
    251     if (level <= 0) {
    252         return false;
    253     }
    254 
    255     if (level > fCount) {
    256         level = fCount;
    257     }
    258     if (levelPtr) {
    259         *levelPtr = fLevels[level - 1];
    260     }
    261     return true;
    262 }
    263