reed@google.com | dc6c8ba | 2013-07-18 21:14:04 +0000 | [diff] [blame^] | 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 | |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 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.getConfig(); |
| 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 | |
| 196 | SkBitmap dstBM; |
| 197 | dstBM.setConfig(config, width, height, rowBytes); |
| 198 | dstBM.setPixels(addr); |
| 199 | |
| 200 | srcBM.lockPixels(); |
| 201 | for (int y = 0; y < height; y++) { |
| 202 | for (int x = 0; x < width; x++) { |
| 203 | proc(&dstBM, x, y, srcBM); |
| 204 | } |
| 205 | } |
| 206 | srcBM.unlockPixels(); |
| 207 | |
| 208 | srcBM = dstBM; |
| 209 | addr += height * rowBytes; |
| 210 | } |
| 211 | SkASSERT(addr == baseAddr + size); |
| 212 | |
| 213 | return SkNEW_ARGS(SkMipMap, (levels, countLevels)); |
| 214 | } |
| 215 | |
| 216 | static SkFixed compute_level(SkScalar scale) { |
| 217 | SkFixed s = SkAbs32(SkScalarToFixed(SkScalarInvert(scale))); |
| 218 | |
| 219 | if (s < SK_Fixed1) { |
| 220 | return 0; |
| 221 | } |
| 222 | int clz = SkCLZ(s); |
| 223 | SkASSERT(clz >= 1 && clz <= 15); |
| 224 | return SkIntToFixed(15 - clz) + ((unsigned)(s << (clz + 1)) >> 16); |
| 225 | } |
| 226 | |
| 227 | bool SkMipMap::extractLevel(SkScalar scale, Level* levelPtr) const { |
| 228 | if (scale >= SK_Scalar1) { |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | int level = compute_level(scale) >> 16; |
| 233 | SkASSERT(level >= 0); |
| 234 | if (level <= 0) { |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | if (level > fCount) { |
| 239 | level = fCount; |
| 240 | } |
| 241 | if (levelPtr) { |
| 242 | *levelPtr = fLevels[level - 1]; |
| 243 | } |
| 244 | return true; |
| 245 | } |
| 246 | |