1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/core/SkMipMap.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,256 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 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 "SkMipMap.h" 1.12 +#include "SkBitmap.h" 1.13 +#include "SkColorPriv.h" 1.14 + 1.15 +static void downsampleby2_proc32(SkBitmap* dst, int x, int y, 1.16 + const SkBitmap& src) { 1.17 + x <<= 1; 1.18 + y <<= 1; 1.19 + const SkPMColor* p = src.getAddr32(x, y); 1.20 + const SkPMColor* baseP = p; 1.21 + SkPMColor c, ag, rb; 1.22 + 1.23 + c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF; 1.24 + if (x < src.width() - 1) { 1.25 + p += 1; 1.26 + } 1.27 + c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; 1.28 + 1.29 + p = baseP; 1.30 + if (y < src.height() - 1) { 1.31 + p += src.rowBytes() >> 2; 1.32 + } 1.33 + c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; 1.34 + if (x < src.width() - 1) { 1.35 + p += 1; 1.36 + } 1.37 + c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; 1.38 + 1.39 + *dst->getAddr32(x >> 1, y >> 1) = 1.40 + ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00); 1.41 +} 1.42 + 1.43 +static inline uint32_t expand16(U16CPU c) { 1.44 + return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16); 1.45 +} 1.46 + 1.47 +// returns dirt in the top 16bits, but we don't care, since we only 1.48 +// store the low 16bits. 1.49 +static inline U16CPU pack16(uint32_t c) { 1.50 + return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE); 1.51 +} 1.52 + 1.53 +static void downsampleby2_proc16(SkBitmap* dst, int x, int y, 1.54 + const SkBitmap& src) { 1.55 + x <<= 1; 1.56 + y <<= 1; 1.57 + const uint16_t* p = src.getAddr16(x, y); 1.58 + const uint16_t* baseP = p; 1.59 + SkPMColor c; 1.60 + 1.61 + c = expand16(*p); 1.62 + if (x < src.width() - 1) { 1.63 + p += 1; 1.64 + } 1.65 + c += expand16(*p); 1.66 + 1.67 + p = baseP; 1.68 + if (y < src.height() - 1) { 1.69 + p += src.rowBytes() >> 1; 1.70 + } 1.71 + c += expand16(*p); 1.72 + if (x < src.width() - 1) { 1.73 + p += 1; 1.74 + } 1.75 + c += expand16(*p); 1.76 + 1.77 + *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2); 1.78 +} 1.79 + 1.80 +static uint32_t expand4444(U16CPU c) { 1.81 + return (c & 0xF0F) | ((c & ~0xF0F) << 12); 1.82 +} 1.83 + 1.84 +static U16CPU collaps4444(uint32_t c) { 1.85 + return (c & 0xF0F) | ((c >> 12) & ~0xF0F); 1.86 +} 1.87 + 1.88 +static void downsampleby2_proc4444(SkBitmap* dst, int x, int y, 1.89 + const SkBitmap& src) { 1.90 + x <<= 1; 1.91 + y <<= 1; 1.92 + const uint16_t* p = src.getAddr16(x, y); 1.93 + const uint16_t* baseP = p; 1.94 + uint32_t c; 1.95 + 1.96 + c = expand4444(*p); 1.97 + if (x < src.width() - 1) { 1.98 + p += 1; 1.99 + } 1.100 + c += expand4444(*p); 1.101 + 1.102 + p = baseP; 1.103 + if (y < src.height() - 1) { 1.104 + p += src.rowBytes() >> 1; 1.105 + } 1.106 + c += expand4444(*p); 1.107 + if (x < src.width() - 1) { 1.108 + p += 1; 1.109 + } 1.110 + c += expand4444(*p); 1.111 + 1.112 + *dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2); 1.113 +} 1.114 + 1.115 +SkMipMap::Level* SkMipMap::AllocLevels(int levelCount, size_t pixelSize) { 1.116 + if (levelCount < 0) { 1.117 + return NULL; 1.118 + } 1.119 + int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize; 1.120 + if (!sk_64_isS32(size)) { 1.121 + return NULL; 1.122 + } 1.123 + return (Level*)sk_malloc_throw(sk_64_asS32(size)); 1.124 +} 1.125 + 1.126 +SkMipMap* SkMipMap::Build(const SkBitmap& src) { 1.127 + void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src); 1.128 + 1.129 + const SkBitmap::Config config = src.config(); 1.130 + switch (config) { 1.131 + case SkBitmap::kARGB_8888_Config: 1.132 + proc = downsampleby2_proc32; 1.133 + break; 1.134 + case SkBitmap::kRGB_565_Config: 1.135 + proc = downsampleby2_proc16; 1.136 + break; 1.137 + case SkBitmap::kARGB_4444_Config: 1.138 + proc = downsampleby2_proc4444; 1.139 + break; 1.140 + case SkBitmap::kIndex8_Config: 1.141 + case SkBitmap::kA8_Config: 1.142 + default: 1.143 + return NULL; // don't build mipmaps for these configs 1.144 + } 1.145 + 1.146 + SkAutoLockPixels alp(src); 1.147 + if (!src.readyToDraw()) { 1.148 + return NULL; 1.149 + } 1.150 + 1.151 + // whip through our loop to compute the exact size needed 1.152 + size_t size = 0; 1.153 + int countLevels = 0; 1.154 + { 1.155 + int width = src.width(); 1.156 + int height = src.height(); 1.157 + for (;;) { 1.158 + width >>= 1; 1.159 + height >>= 1; 1.160 + if (0 == width || 0 == height) { 1.161 + break; 1.162 + } 1.163 + size += SkBitmap::ComputeRowBytes(config, width) * height; 1.164 + countLevels += 1; 1.165 + } 1.166 + } 1.167 + if (0 == countLevels) { 1.168 + return NULL; 1.169 + } 1.170 + 1.171 + Level* levels = SkMipMap::AllocLevels(countLevels, size); 1.172 + if (NULL == levels) { 1.173 + return NULL; 1.174 + } 1.175 + 1.176 + uint8_t* baseAddr = (uint8_t*)&levels[countLevels]; 1.177 + uint8_t* addr = baseAddr; 1.178 + int width = src.width(); 1.179 + int height = src.height(); 1.180 + uint32_t rowBytes; 1.181 + SkBitmap srcBM(src); 1.182 + 1.183 + for (int i = 0; i < countLevels; ++i) { 1.184 + width >>= 1; 1.185 + height >>= 1; 1.186 + rowBytes = SkToU32(SkBitmap::ComputeRowBytes(config, width)); 1.187 + 1.188 + levels[i].fPixels = addr; 1.189 + levels[i].fWidth = width; 1.190 + levels[i].fHeight = height; 1.191 + levels[i].fRowBytes = rowBytes; 1.192 + levels[i].fScale = (float)width / src.width(); 1.193 + 1.194 + SkBitmap dstBM; 1.195 + dstBM.setConfig(config, width, height, rowBytes); 1.196 + dstBM.setPixels(addr); 1.197 + 1.198 + srcBM.lockPixels(); 1.199 + for (int y = 0; y < height; y++) { 1.200 + for (int x = 0; x < width; x++) { 1.201 + proc(&dstBM, x, y, srcBM); 1.202 + } 1.203 + } 1.204 + srcBM.unlockPixels(); 1.205 + 1.206 + srcBM = dstBM; 1.207 + addr += height * rowBytes; 1.208 + } 1.209 + SkASSERT(addr == baseAddr + size); 1.210 + 1.211 + return SkNEW_ARGS(SkMipMap, (levels, countLevels, size)); 1.212 +} 1.213 + 1.214 +/////////////////////////////////////////////////////////////////////////////// 1.215 + 1.216 +//static int gCounter; 1.217 + 1.218 +SkMipMap::SkMipMap(Level* levels, int count, size_t size) 1.219 + : fSize(size), fLevels(levels), fCount(count) { 1.220 + SkASSERT(levels); 1.221 + SkASSERT(count > 0); 1.222 +// SkDebugf("mips %d\n", ++gCounter); 1.223 +} 1.224 + 1.225 +SkMipMap::~SkMipMap() { 1.226 + sk_free(fLevels); 1.227 +// SkDebugf("mips %d\n", --gCounter); 1.228 +} 1.229 + 1.230 +static SkFixed compute_level(SkScalar scale) { 1.231 + SkFixed s = SkAbs32(SkScalarToFixed(SkScalarInvert(scale))); 1.232 + 1.233 + if (s < SK_Fixed1) { 1.234 + return 0; 1.235 + } 1.236 + int clz = SkCLZ(s); 1.237 + SkASSERT(clz >= 1 && clz <= 15); 1.238 + return SkIntToFixed(15 - clz) + ((unsigned)(s << (clz + 1)) >> 16); 1.239 +} 1.240 + 1.241 +bool SkMipMap::extractLevel(SkScalar scale, Level* levelPtr) const { 1.242 + if (scale >= SK_Scalar1) { 1.243 + return false; 1.244 + } 1.245 + 1.246 + int level = compute_level(scale) >> 16; 1.247 + SkASSERT(level >= 0); 1.248 + if (level <= 0) { 1.249 + return false; 1.250 + } 1.251 + 1.252 + if (level > fCount) { 1.253 + level = fCount; 1.254 + } 1.255 + if (levelPtr) { 1.256 + *levelPtr = fLevels[level - 1]; 1.257 + } 1.258 + return true; 1.259 +}