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#include "SkMath.h"
12#include "SkNx.h"
13#include "SkTypes.h"
14
15//
16// ColorTypeFilter is the "Type" we pass to some downsample template functions.
17// It controls how we expand a pixel into a large type, with space between each component,
18// so we can then perform our simple filter (either box or triangle) and store the intermediates
19// in the expanded type.
20//
21
22struct ColorTypeFilter_8888 {
23    typedef uint32_t Type;
24#if defined(SKNX_IS_FAST)
25    static Sk4h Expand(uint32_t x) {
26        return SkNx_cast<uint16_t>(Sk4b::Load(&x));
27    }
28    static uint32_t Compact(const Sk4h& x) {
29        uint32_t r;
30        SkNx_cast<uint8_t>(x).store(&r);
31        return r;
32    }
33#else
34    static uint64_t Expand(uint32_t x) {
35        return (x & 0xFF00FF) | ((uint64_t)(x & 0xFF00FF00) << 24);
36    }
37    static uint32_t Compact(uint64_t x) {
38        return (uint32_t)((x & 0xFF00FF) | ((x >> 24) & 0xFF00FF00));
39    }
40#endif
41};
42
43struct ColorTypeFilter_565 {
44    typedef uint16_t Type;
45    static uint32_t Expand(uint16_t x) {
46        return (x & ~SK_G16_MASK_IN_PLACE) | ((x & SK_G16_MASK_IN_PLACE) << 16);
47    }
48    static uint16_t Compact(uint32_t x) {
49        return (x & ~SK_G16_MASK_IN_PLACE) | ((x >> 16) & SK_G16_MASK_IN_PLACE);
50    }
51};
52
53struct ColorTypeFilter_4444 {
54    typedef uint16_t Type;
55    static uint32_t Expand(uint16_t x) {
56        return (x & 0xF0F) | ((x & ~0xF0F) << 12);
57    }
58    static uint16_t Compact(uint32_t x) {
59        return (x & 0xF0F) | ((x >> 12) & ~0xF0F);
60    }
61};
62
63struct ColorTypeFilter_8 {
64    typedef uint8_t Type;
65    static unsigned Expand(unsigned x) {
66        return x;
67    }
68    static uint8_t Compact(unsigned x) {
69        return (uint8_t)x;
70    }
71};
72
73template <typename T> T add_121(const T& a, const T& b, const T& c) {
74    return a + b + b + c;
75}
76
77//
78//  To produce each mip level, we need to filter down by 1/2 (e.g. 100x100 -> 50,50)
79//  If the starting dimension is odd, we floor the size of the lower level (e.g. 101 -> 50)
80//  In those (odd) cases, we use a triangle filter, with 1-pixel overlap between samplings,
81//  else for even cases, we just use a 2x box filter.
82//
83//  This produces 4 possible filters: 2x2 2x3 3x2 3x3 where WxH indicates the number of src pixels
84//  we need to sample in each dimension to produce 1 dst pixel.
85//
86
87template <typename F> void downsample_2_2(void* dst, const void* src, size_t srcRB, int count) {
88    auto p0 = static_cast<const typename F::Type*>(src);
89    auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
90    auto d = static_cast<typename F::Type*>(dst);
91
92    for (int i = 0; i < count; ++i) {
93        auto c00 = F::Expand(p0[0]);
94        auto c01 = F::Expand(p0[1]);
95        auto c10 = F::Expand(p1[0]);
96        auto c11 = F::Expand(p1[1]);
97
98        auto c = c00 + c10 + c01 + c11;
99        d[i] = F::Compact(c >> 2);
100        p0 += 2;
101        p1 += 2;
102    }
103}
104
105template <typename F> void downsample_3_2(void* dst, const void* src, size_t srcRB, int count) {
106    SkASSERT(count > 0);
107    auto p0 = static_cast<const typename F::Type*>(src);
108    auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
109    auto d = static_cast<typename F::Type*>(dst);
110
111    auto c02 = F::Expand(p0[0]);
112    auto c12 = F::Expand(p1[0]);
113    for (int i = 0; i < count; ++i) {
114        auto c00 = c02;
115        auto c01 = F::Expand(p0[1]);
116             c02 = F::Expand(p0[2]);
117        auto c10 = c12;
118        auto c11 = F::Expand(p1[1]);
119             c12 = F::Expand(p1[2]);
120
121        auto c = add_121(c00, c01, c02) + add_121(c10, c11, c12);
122        d[i] = F::Compact(c >> 3);
123        p0 += 2;
124        p1 += 2;
125    }
126}
127
128template <typename F> void downsample_2_3(void* dst, const void* src, size_t srcRB, int count) {
129    auto p0 = static_cast<const typename F::Type*>(src);
130    auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
131    auto p2 = (const typename F::Type*)((const char*)p1 + srcRB);
132    auto d = static_cast<typename F::Type*>(dst);
133
134    for (int i = 0; i < count; ++i) {
135        auto c00 = F::Expand(p0[0]);
136        auto c01 = F::Expand(p0[1]);
137        auto c10 = F::Expand(p1[0]);
138        auto c11 = F::Expand(p1[1]);
139        auto c20 = F::Expand(p2[0]);
140        auto c21 = F::Expand(p2[1]);
141
142        auto c = add_121(c00, c10, c20) + add_121(c01, c11, c21);
143        d[i] = F::Compact(c >> 3);
144        p0 += 2;
145        p1 += 2;
146        p2 += 2;
147    }
148}
149
150template <typename F> void downsample_3_3(void* dst, const void* src, size_t srcRB, int count) {
151    auto p0 = static_cast<const typename F::Type*>(src);
152    auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
153    auto p2 = (const typename F::Type*)((const char*)p1 + srcRB);
154    auto d = static_cast<typename F::Type*>(dst);
155
156    auto c02 = F::Expand(p0[0]);
157    auto c12 = F::Expand(p1[0]);
158    auto c22 = F::Expand(p2[0]);
159    for (int i = 0; i < count; ++i) {
160        auto c00 = c02;
161        auto c01 = F::Expand(p0[1]);
162             c02 = F::Expand(p0[2]);
163        auto c10 = c12;
164        auto c11 = F::Expand(p1[1]);
165             c12 = F::Expand(p1[2]);
166        auto c20 = c22;
167        auto c21 = F::Expand(p2[1]);
168             c22 = F::Expand(p2[2]);
169
170        auto c = add_121(c00, c01, c02) + (add_121(c10, c11, c12) << 1) + add_121(c20, c21, c22);
171        d[i] = F::Compact(c >> 4);
172        p0 += 2;
173        p1 += 2;
174        p2 += 2;
175    }
176}
177
178///////////////////////////////////////////////////////////////////////////////////////////////////
179
180size_t SkMipMap::AllocLevelsSize(int levelCount, size_t pixelSize) {
181    if (levelCount < 0) {
182        return 0;
183    }
184    int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize;
185    if (!sk_64_isS32(size)) {
186        return 0;
187    }
188    return sk_64_asS32(size);
189}
190
191SkMipMap* SkMipMap::Build(const SkPixmap& src, SkDiscardableFactoryProc fact) {
192    typedef void FilterProc(void*, const void* srcPtr, size_t srcRB, int count);
193
194    FilterProc* proc_2_2 = nullptr;
195    FilterProc* proc_2_3 = nullptr;
196    FilterProc* proc_3_2 = nullptr;
197    FilterProc* proc_3_3 = nullptr;
198
199    const SkColorType ct = src.colorType();
200    const SkAlphaType at = src.alphaType();
201    switch (ct) {
202        case kRGBA_8888_SkColorType:
203        case kBGRA_8888_SkColorType:
204            proc_2_2 = downsample_2_2<ColorTypeFilter_8888>;
205            proc_2_3 = downsample_2_3<ColorTypeFilter_8888>;
206            proc_3_2 = downsample_3_2<ColorTypeFilter_8888>;
207            proc_3_3 = downsample_3_3<ColorTypeFilter_8888>;
208            break;
209        case kRGB_565_SkColorType:
210            proc_2_2 = downsample_2_2<ColorTypeFilter_565>;
211            proc_2_3 = downsample_2_3<ColorTypeFilter_565>;
212            proc_3_2 = downsample_3_2<ColorTypeFilter_565>;
213            proc_3_3 = downsample_3_3<ColorTypeFilter_565>;
214            break;
215        case kARGB_4444_SkColorType:
216            proc_2_2 = downsample_2_2<ColorTypeFilter_4444>;
217            proc_2_3 = downsample_2_3<ColorTypeFilter_4444>;
218            proc_3_2 = downsample_3_2<ColorTypeFilter_4444>;
219            proc_3_3 = downsample_3_3<ColorTypeFilter_4444>;
220            break;
221        case kAlpha_8_SkColorType:
222        case kGray_8_SkColorType:
223            proc_2_2 = downsample_2_2<ColorTypeFilter_8>;
224            proc_2_3 = downsample_2_3<ColorTypeFilter_8>;
225            proc_3_2 = downsample_3_2<ColorTypeFilter_8>;
226            proc_3_3 = downsample_3_3<ColorTypeFilter_8>;
227            break;
228        default:
229            // TODO: We could build miplevels for kIndex8 if the levels were in 8888.
230            //       Means using more ram, but the quality would be fine.
231            return nullptr;
232    }
233
234    // whip through our loop to compute the exact size needed
235    size_t  size = 0;
236    int     countLevels = 0;
237    {
238        int width = src.width();
239        int height = src.height();
240        for (;;) {
241            width >>= 1;
242            height >>= 1;
243            if (0 == width || 0 == height) {
244                break;
245            }
246            size += SkColorTypeMinRowBytes(ct, width) * height;
247            countLevels += 1;
248        }
249    }
250    if (0 == countLevels) {
251        return nullptr;
252    }
253
254    SkASSERT(countLevels == SkMipMap::ComputeLevelCount(src.width(), src.height()));
255
256    size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size);
257    if (0 == storageSize) {
258        return nullptr;
259    }
260
261    SkMipMap* mipmap;
262    if (fact) {
263        SkDiscardableMemory* dm = fact(storageSize);
264        if (nullptr == dm) {
265            return nullptr;
266        }
267        mipmap = new SkMipMap(storageSize, dm);
268    } else {
269        mipmap = new SkMipMap(sk_malloc_throw(storageSize), storageSize);
270    }
271
272    // init
273    mipmap->fCount = countLevels;
274    mipmap->fLevels = (Level*)mipmap->writable_data();
275
276    Level* levels = mipmap->fLevels;
277    uint8_t*    baseAddr = (uint8_t*)&levels[countLevels];
278    uint8_t*    addr = baseAddr;
279    int         width = src.width();
280    int         height = src.height();
281    uint32_t    rowBytes;
282    SkPixmap    srcPM(src);
283
284    for (int i = 0; i < countLevels; ++i) {
285        FilterProc* proc;
286        if (height & 1) {        // src-height is 3
287            if (width & 1) {    // src-width is 3
288                proc = proc_3_3;
289            } else {            // src-width is 2
290                proc = proc_2_3;
291            }
292        } else {                // src-height is 2
293            if (width & 1) {    // src-width is 3
294                proc = proc_3_2;
295            } else {            // src-width is 2
296                proc = proc_2_2;
297            }
298        }
299        width >>= 1;
300        height >>= 1;
301        rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width));
302
303        levels[i].fPixmap = SkPixmap(SkImageInfo::Make(width, height, ct, at), addr, rowBytes);
304        levels[i].fScale  = SkSize::Make(SkIntToScalar(width)  / src.width(),
305                                         SkIntToScalar(height) / src.height());
306
307        const SkPixmap& dstPM = levels[i].fPixmap;
308        const void* srcBasePtr = srcPM.addr();
309        void* dstBasePtr = dstPM.writable_addr();
310
311        const size_t srcRB = srcPM.rowBytes();
312        for (int y = 0; y < height; y++) {
313            proc(dstBasePtr, srcBasePtr, srcRB, width);
314            srcBasePtr = (char*)srcBasePtr + srcRB * 2; // jump two rows
315            dstBasePtr = (char*)dstBasePtr + dstPM.rowBytes();
316        }
317        srcPM = dstPM;
318        addr += height * rowBytes;
319    }
320    SkASSERT(addr == baseAddr + size);
321
322    return mipmap;
323}
324
325int SkMipMap::ComputeLevelCount(int baseWidth, int baseHeight) {
326    // OpenGL's spec requires that each mipmap level have height/width equal to
327    // max(1, floor(original_height / 2^i)
328    // (or original_width) where i is the mipmap level.
329    // Continue scaling down until both axes are size 1.
330    //
331    // This means it maintains isotropic space (both axes scaling down
332    // at the same rate) until one axis hits size 1.
333    // At that point, OpenGL continues to scale down into anisotropic space
334    // (where the scales are not the same between axes).
335    //
336    // Skia currently does not go into anisotropic space.
337    // Once an axis hits size 1 we stop.
338    // All this means is rather than use the largest axis we will use the
339    // smallest axis.
340
341    const int smallestAxis = SkTMin(baseWidth, baseHeight);
342    if (smallestAxis < 2) {
343        // SkMipMap::Build requires a minimum size of 2.
344        return 0;
345    }
346    const int leadingZeros = SkCLZ(static_cast<uint32_t>(smallestAxis));
347    // If the value 00011010 has 3 leading 0s then it has 5 significant bits
348    // (the bits which are not leading zeros)
349    const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros;
350    // This is making the assumption that the size of a byte is 8 bits
351    // and that sizeof(uint32_t)'s implementation-defined behavior is 4.
352    int mipLevelCount = significantBits;
353
354    // SkMipMap does not include the base mip level.
355    // For example, it contains levels 1-x instead of 0-x.
356    // This is because the image used to create SkMipMap is the base level.
357    // So subtract 1 from the mip level count.
358    if (mipLevelCount > 0) {
359        --mipLevelCount;
360    }
361
362    return mipLevelCount;
363}
364
365///////////////////////////////////////////////////////////////////////////////
366
367bool SkMipMap::extractLevel(const SkSize& scaleSize, Level* levelPtr) const {
368    if (nullptr == fLevels) {
369        return false;
370    }
371
372    SkASSERT(scaleSize.width() >= 0 && scaleSize.height() >= 0);
373
374#ifndef SK_SUPPORT_LEGACY_ANISOTROPIC_MIPMAP_SCALE
375    // Use the smallest scale to match the GPU impl.
376    const SkScalar scale = SkTMin(scaleSize.width(), scaleSize.height());
377#else
378    // Ideally we'd pick the smaller scale, to match Ganesh.  But ignoring one of the
379    // scales can produce some atrocious results, so for now we use the geometric mean.
380    // (https://bugs.chromium.org/p/skia/issues/detail?id=4863)
381    const SkScalar scale = SkScalarSqrt(scaleSize.width() * scaleSize.height());
382#endif
383
384    if (scale >= SK_Scalar1 || scale <= 0 || !SkScalarIsFinite(scale)) {
385        return false;
386    }
387
388    SkScalar L = -SkScalarLog2(scale);
389    if (!SkScalarIsFinite(L)) {
390        return false;
391    }
392    SkASSERT(L >= 0);
393//    int rndLevel = SkScalarRoundToInt(L);
394    int level = SkScalarFloorToInt(L);
395//    SkDebugf("mipmap scale=%g L=%g level=%d rndLevel=%d\n", scale, L, level, rndLevel);
396
397    SkASSERT(level >= 0);
398    if (level <= 0) {
399        return false;
400    }
401
402    if (level > fCount) {
403        level = fCount;
404    }
405    if (levelPtr) {
406        *levelPtr = fLevels[level - 1];
407    }
408    return true;
409}
410
411// Helper which extracts a pixmap from the src bitmap
412//
413SkMipMap* SkMipMap::Build(const SkBitmap& src, SkDiscardableFactoryProc fact) {
414    SkAutoPixmapUnlock srcUnlocker;
415    if (!src.requestLock(&srcUnlocker)) {
416        return nullptr;
417    }
418    const SkPixmap& srcPixmap = srcUnlocker.pixmap();
419    // Try to catch where we might have returned nullptr for src crbug.com/492818
420    if (nullptr == srcPixmap.addr()) {
421        sk_throw();
422    }
423    return Build(srcPixmap, fact);
424}
425
426int SkMipMap::countLevels() const {
427    return fCount;
428}
429
430bool SkMipMap::getLevel(int index, Level* levelPtr) const {
431    if (NULL == fLevels) {
432        return false;
433    }
434    if (index < 0) {
435        return false;
436    }
437    if (index > fCount - 1) {
438        return false;
439    }
440    if (levelPtr) {
441        *levelPtr = fLevels[index];
442    }
443    return true;
444}
445