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 downsample32_nocheck(void* dst, int, int, const void* srcPtr, const SkBitmap& srcBM) {
13    const uint32_t* p = static_cast<const uint32_t*>(srcPtr);
14    const uint32_t* baseP = p;
15    uint32_t c, ag, rb;
16
17    c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
18    p += 1;
19
20    c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
21
22    p = baseP;
23    p += srcBM.rowBytes() >> 2;
24
25    c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
26    p += 1;
27
28    c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
29
30    *(uint32_t*)dst = ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
31}
32
33static void downsample32_check(void* dst, int x, int y, const void* srcPtr, const SkBitmap& srcBM) {
34    const uint32_t* p = static_cast<const uint32_t*>(srcPtr);
35    const uint32_t* baseP = p;
36
37    x <<= 1;
38    y <<= 1;
39    SkASSERT(srcBM.getAddr32(x, y) == p);
40
41    SkPMColor c, ag, rb;
42
43    c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
44    if (x < srcBM.width() - 1) {
45        p += 1;
46    }
47    c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
48
49    p = baseP;
50    if (y < srcBM.height() - 1) {
51        p += srcBM.rowBytes() >> 2;
52    }
53    c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
54    if (x < srcBM.width() - 1) {
55        p += 1;
56    }
57    c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
58
59    *((uint32_t*)dst) = ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
60}
61
62static inline uint32_t expand16(U16CPU c) {
63    return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
64}
65
66// returns dirt in the top 16bits, but we don't care, since we only
67// store the low 16bits.
68static inline U16CPU pack16(uint32_t c) {
69    return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
70}
71
72static void downsample16(void* dst, int x, int y, const void* srcPtr, const SkBitmap& srcBM) {
73    const uint16_t* p = static_cast<const uint16_t*>(srcPtr);
74    const uint16_t* baseP = p;
75
76    x <<= 1;
77    y <<= 1;
78    SkASSERT(srcBM.getAddr16(x, y) == p);
79
80    SkPMColor c;
81
82    c = expand16(*p);
83    if (x < srcBM.width() - 1) {
84        p += 1;
85    }
86    c += expand16(*p);
87
88    p = baseP;
89    if (y < srcBM.height() - 1) {
90        p += srcBM.rowBytes() >> 1;
91    }
92    c += expand16(*p);
93    if (x < srcBM.width() - 1) {
94        p += 1;
95    }
96    c += expand16(*p);
97
98    *((uint16_t*)dst) = (uint16_t)pack16(c >> 2);
99}
100
101static uint32_t expand4444(U16CPU c) {
102    return (c & 0xF0F) | ((c & ~0xF0F) << 12);
103}
104
105static U16CPU collaps4444(uint32_t c) {
106    return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
107}
108
109static void downsample4444(void* dst, int x, int y, const void* srcPtr, const SkBitmap& srcBM) {
110    const uint16_t* p = static_cast<const uint16_t*>(srcPtr);
111    const uint16_t* baseP = p;
112
113    x <<= 1;
114    y <<= 1;
115    SkASSERT(srcBM.getAddr16(x, y) == p);
116
117    uint32_t c;
118
119    c = expand4444(*p);
120    if (x < srcBM.width() - 1) {
121        p += 1;
122    }
123    c += expand4444(*p);
124
125    p = baseP;
126    if (y < srcBM.height() - 1) {
127        p += srcBM.rowBytes() >> 1;
128    }
129    c += expand4444(*p);
130    if (x < srcBM.width() - 1) {
131        p += 1;
132    }
133    c += expand4444(*p);
134
135   *((uint16_t*)dst) = (uint16_t)collaps4444(c >> 2);
136}
137
138static void downsample8_nocheck(void* dst, int, int, const void* srcPtr, const SkBitmap& srcBM) {
139    const size_t rb = srcBM.rowBytes();
140    const uint8_t* p = static_cast<const uint8_t*>(srcPtr);
141    *(uint8_t*)dst = (p[0] + p[1] + p[rb] + p[rb + 1]) >> 2;
142}
143
144static void downsample8_check(void* dst, int x, int y, const void* srcPtr, const SkBitmap& srcBM) {
145    const uint8_t* p = static_cast<const uint8_t*>(srcPtr);
146    const uint8_t* baseP = p;
147
148    x <<= 1;
149    y <<= 1;
150    SkASSERT(srcBM.getAddr8(x, y) == p);
151
152    unsigned c = *p;
153    if (x < srcBM.width() - 1) {
154        p += 1;
155    }
156    c += *p;
157
158    p = baseP;
159    if (y < srcBM.height() - 1) {
160        p += srcBM.rowBytes();
161    }
162    c += *p;
163    if (x < srcBM.width() - 1) {
164        p += 1;
165    }
166    c += *p;
167
168    *(uint8_t*)dst = c >> 2;
169}
170
171size_t SkMipMap::AllocLevelsSize(int levelCount, size_t pixelSize) {
172    if (levelCount < 0) {
173        return 0;
174    }
175    int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize;
176    if (!sk_64_isS32(size)) {
177        return 0;
178    }
179    return sk_64_asS32(size);
180}
181
182typedef void SkDownSampleProc(void*, int x, int y, const void* srcPtr, const SkBitmap& srcBM);
183
184SkMipMap* SkMipMap::Build(const SkBitmap& src, SkDiscardableFactoryProc fact) {
185    SkDownSampleProc* proc_nocheck, *proc_check;
186
187    const SkColorType ct = src.colorType();
188    const SkAlphaType at = src.alphaType();
189    switch (ct) {
190        case kRGBA_8888_SkColorType:
191        case kBGRA_8888_SkColorType:
192            proc_check = downsample32_check;
193            proc_nocheck = downsample32_nocheck;
194            break;
195        case kRGB_565_SkColorType:
196            proc_check = downsample16;
197            proc_nocheck = proc_check;
198            break;
199        case kARGB_4444_SkColorType:
200            proc_check = downsample4444;
201            proc_nocheck = proc_check;
202            break;
203        case kAlpha_8_SkColorType:
204        case kGray_8_SkColorType:
205            proc_check = downsample8_check;
206            proc_nocheck = downsample8_nocheck;
207            break;
208        default:
209            return NULL; // don't build mipmaps for any other colortypes (yet)
210    }
211
212    SkAutoLockPixels alp(src);
213    if (!src.readyToDraw()) {
214        return NULL;
215    }
216
217    // whip through our loop to compute the exact size needed
218    size_t  size = 0;
219    int     countLevels = 0;
220    {
221        int width = src.width();
222        int height = src.height();
223        for (;;) {
224            width >>= 1;
225            height >>= 1;
226            if (0 == width || 0 == height) {
227                break;
228            }
229            size += SkColorTypeMinRowBytes(ct, width) * height;
230            countLevels += 1;
231        }
232    }
233    if (0 == countLevels) {
234        return NULL;
235    }
236
237    size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size);
238    if (0 == storageSize) {
239        return NULL;
240    }
241
242    SkMipMap* mipmap;
243    if (fact) {
244        SkDiscardableMemory* dm = fact(storageSize);
245        if (NULL == dm) {
246            return NULL;
247        }
248        mipmap = SkNEW_ARGS(SkMipMap, (storageSize, dm));
249    } else {
250        mipmap = SkNEW_ARGS(SkMipMap, (sk_malloc_throw(storageSize), storageSize));
251    }
252
253    // init
254    mipmap->fCount = countLevels;
255    mipmap->fLevels = (Level*)mipmap->writable_data();
256
257    Level* levels = mipmap->fLevels;
258    uint8_t*    baseAddr = (uint8_t*)&levels[countLevels];
259    uint8_t*    addr = baseAddr;
260    int         width = src.width();
261    int         height = src.height();
262    uint32_t    rowBytes;
263    SkBitmap    srcBM(src);
264
265    for (int i = 0; i < countLevels; ++i) {
266        width >>= 1;
267        height >>= 1;
268        rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width));
269
270        levels[i].fPixels   = addr;
271        levels[i].fWidth    = width;
272        levels[i].fHeight   = height;
273        levels[i].fRowBytes = rowBytes;
274        levels[i].fScale    = (float)width / src.width();
275
276        SkBitmap dstBM;
277        dstBM.installPixels(SkImageInfo::Make(width, height, ct, at), addr, rowBytes);
278
279        srcBM.lockPixels();
280        const int widthEven = width & ~1;
281        const int heightEven = height & ~1;
282        const size_t pixelSize = srcBM.info().bytesPerPixel();
283
284        const void* srcBasePtr = srcBM.getPixels();
285        void* dstBasePtr = dstBM.getPixels();
286        for (int y = 0; y < heightEven; y++) {
287            const void* srcPtr = srcBasePtr;
288            void* dstPtr = dstBasePtr;
289            for (int x = 0; x < widthEven; x++) {
290                proc_nocheck(dstPtr, x, y, srcPtr, srcBM);
291                srcPtr = (char*)srcPtr + pixelSize * 2;
292                dstPtr = (char*)dstPtr + pixelSize;
293            }
294            if (width & 1) {
295                proc_check(dstPtr, widthEven, y, srcPtr, srcBM);
296            }
297
298            srcBasePtr = (char*)srcBasePtr + srcBM.rowBytes() * 2;
299            dstBasePtr = (char*)dstBasePtr + dstBM.rowBytes();
300        }
301        if (height & 1) {
302            const void* srcPtr = srcBasePtr;
303            void* dstPtr = dstBasePtr;
304            for (int x = 0; x < width; x++) {
305                proc_check(dstPtr, x, heightEven, srcPtr, srcBM);
306                srcPtr = (char*)srcPtr + pixelSize * 2;
307                dstPtr = (char*)dstPtr + pixelSize;
308            }
309        }
310
311        srcBM.unlockPixels();
312
313        srcBM = dstBM;
314        addr += height * rowBytes;
315    }
316    SkASSERT(addr == baseAddr + size);
317
318    return mipmap;
319}
320
321///////////////////////////////////////////////////////////////////////////////
322
323bool SkMipMap::extractLevel(SkScalar scale, Level* levelPtr) const {
324    if (NULL == fLevels) {
325        return false;
326    }
327
328    if (scale >= SK_Scalar1 || scale <= 0 || !SkScalarIsFinite(scale)) {
329        return false;
330    }
331
332    SkScalar L = -SkScalarLog2(scale);
333    if (!SkScalarIsFinite(L)) {
334        return false;
335    }
336    SkASSERT(L >= 0);
337//    int rndLevel = SkScalarRoundToInt(L);
338    int level = SkScalarFloorToInt(L);
339//    SkDebugf("mipmap scale=%g L=%g level=%d rndLevel=%d\n", scale, L, level, rndLevel);
340
341    SkASSERT(level >= 0);
342    if (level <= 0) {
343        return false;
344    }
345
346    if (level > fCount) {
347        level = fCount;
348    }
349    if (levelPtr) {
350        *levelPtr = fLevels[level - 1];
351    }
352    return true;
353}
354