SkMipMap.cpp revision 92561a0b99ad6c08ab7a11dd1872f028199392e9
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 12static 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 40static 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. 46static inline U16CPU pack16(uint32_t c) { 47 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE); 48} 49 50static 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 77static uint32_t expand4444(U16CPU c) { 78 return (c & 0xF0F) | ((c & ~0xF0F) << 12); 79} 80 81static U16CPU collaps4444(uint32_t c) { 82 return (c & 0xF0F) | ((c >> 12) & ~0xF0F); 83} 84 85static 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 112size_t SkMipMap::AllocLevelsSize(int levelCount, size_t pixelSize) { 113 if (levelCount < 0) { 114 return 0; 115 } 116 int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize; 117 if (!sk_64_isS32(size)) { 118 return 0; 119 } 120 return sk_64_asS32(size); 121} 122 123SkMipMap* SkMipMap::Build(const SkBitmap& src, SkDiscardableFactoryProc fact) { 124 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src); 125 126 const SkColorType ct = src.colorType(); 127 const SkAlphaType at = src.alphaType(); 128 switch (ct) { 129 case kRGBA_8888_SkColorType: 130 case kBGRA_8888_SkColorType: 131 proc = downsampleby2_proc32; 132 break; 133 case kRGB_565_SkColorType: 134 proc = downsampleby2_proc16; 135 break; 136 case kARGB_4444_SkColorType: 137 proc = downsampleby2_proc4444; 138 break; 139 default: 140 return NULL; // don't build mipmaps for any other colortypes (yet) 141 } 142 143 SkAutoLockPixels alp(src); 144 if (!src.readyToDraw()) { 145 return NULL; 146 } 147 148 // whip through our loop to compute the exact size needed 149 size_t size = 0; 150 int countLevels = 0; 151 { 152 int width = src.width(); 153 int height = src.height(); 154 for (;;) { 155 width >>= 1; 156 height >>= 1; 157 if (0 == width || 0 == height) { 158 break; 159 } 160 size += SkColorTypeMinRowBytes(ct, width) * height; 161 countLevels += 1; 162 } 163 } 164 if (0 == countLevels) { 165 return NULL; 166 } 167 168 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size); 169 if (0 == storageSize) { 170 return NULL; 171 } 172 173 SkMipMap* mipmap; 174 if (fact) { 175 SkDiscardableMemory* dm = fact(storageSize); 176 if (NULL == dm) { 177 return NULL; 178 } 179 mipmap = SkNEW_ARGS(SkMipMap, (storageSize, dm)); 180 } else { 181 mipmap = SkNEW_ARGS(SkMipMap, (sk_malloc_throw(storageSize), storageSize)); 182 } 183 184 // init 185 mipmap->fCount = countLevels; 186 mipmap->fLevels = (Level*)mipmap->writable_data(); 187 188 Level* levels = mipmap->fLevels; 189 uint8_t* baseAddr = (uint8_t*)&levels[countLevels]; 190 uint8_t* addr = baseAddr; 191 int width = src.width(); 192 int height = src.height(); 193 uint32_t rowBytes; 194 SkBitmap srcBM(src); 195 196 for (int i = 0; i < countLevels; ++i) { 197 width >>= 1; 198 height >>= 1; 199 rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width)); 200 201 levels[i].fPixels = addr; 202 levels[i].fWidth = width; 203 levels[i].fHeight = height; 204 levels[i].fRowBytes = rowBytes; 205 levels[i].fScale = (float)width / src.width(); 206 207 SkBitmap dstBM; 208 dstBM.installPixels(SkImageInfo::Make(width, height, ct, at), addr, rowBytes); 209 210 srcBM.lockPixels(); 211 for (int y = 0; y < height; y++) { 212 for (int x = 0; x < width; x++) { 213 proc(&dstBM, x, y, srcBM); 214 } 215 } 216 srcBM.unlockPixels(); 217 218 srcBM = dstBM; 219 addr += height * rowBytes; 220 } 221 SkASSERT(addr == baseAddr + size); 222 223 return mipmap; 224} 225 226/////////////////////////////////////////////////////////////////////////////// 227 228//static int gCounter; 229 230static SkFixed compute_level(SkScalar scale) { 231 SkFixed s = SkAbs32(SkScalarToFixed(SkScalarInvert(scale))); 232 233 if (s < SK_Fixed1) { 234 return 0; 235 } 236 int clz = SkCLZ(s); 237 SkASSERT(clz >= 1 && clz <= 15); 238 return SkIntToFixed(15 - clz) + ((unsigned)(s << (clz + 1)) >> 16); 239} 240 241bool SkMipMap::extractLevel(SkScalar scale, Level* levelPtr) const { 242 if (NULL == fLevels) { 243 return false; 244 } 245 246 if (scale >= SK_Scalar1) { 247 return false; 248 } 249 250 int level = compute_level(scale) >> 16; 251 SkASSERT(level >= 0); 252 if (level <= 0) { 253 return false; 254 } 255 256 if (level > fCount) { 257 level = fCount; 258 } 259 if (levelPtr) { 260 *levelPtr = fLevels[level - 1]; 261 } 262 return true; 263} 264