• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/SkMipMap.h"
9 
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkTypes.h"
12 #include "include/private/SkColorData.h"
13 #include "include/private/SkHalf.h"
14 #include "include/private/SkImageInfoPriv.h"
15 #include "include/private/SkNx.h"
16 #include "include/private/SkTo.h"
17 #include "src/core/SkMathPriv.h"
18 #include <new>
19 
20 //
21 // ColorTypeFilter is the "Type" we pass to some downsample template functions.
22 // It controls how we expand a pixel into a large type, with space between each component,
23 // so we can then perform our simple filter (either box or triangle) and store the intermediates
24 // in the expanded type.
25 //
26 
27 struct ColorTypeFilter_8888 {
28     typedef uint32_t Type;
ExpandColorTypeFilter_888829     static Sk4h Expand(uint32_t x) {
30         return SkNx_cast<uint16_t>(Sk4b::Load(&x));
31     }
CompactColorTypeFilter_888832     static uint32_t Compact(const Sk4h& x) {
33         uint32_t r;
34         SkNx_cast<uint8_t>(x).store(&r);
35         return r;
36     }
37 };
38 
39 struct ColorTypeFilter_565 {
40     typedef uint16_t Type;
ExpandColorTypeFilter_56541     static uint32_t Expand(uint16_t x) {
42         return (x & ~SK_G16_MASK_IN_PLACE) | ((x & SK_G16_MASK_IN_PLACE) << 16);
43     }
CompactColorTypeFilter_56544     static uint16_t Compact(uint32_t x) {
45         return ((x & ~SK_G16_MASK_IN_PLACE) & 0xFFFF) | ((x >> 16) & SK_G16_MASK_IN_PLACE);
46     }
47 };
48 
49 struct ColorTypeFilter_4444 {
50     typedef uint16_t Type;
ExpandColorTypeFilter_444451     static uint32_t Expand(uint16_t x) {
52         return (x & 0xF0F) | ((x & ~0xF0F) << 12);
53     }
CompactColorTypeFilter_444454     static uint16_t Compact(uint32_t x) {
55         return (x & 0xF0F) | ((x >> 12) & ~0xF0F);
56     }
57 };
58 
59 struct ColorTypeFilter_8 {
60     typedef uint8_t Type;
ExpandColorTypeFilter_861     static unsigned Expand(unsigned x) {
62         return x;
63     }
CompactColorTypeFilter_864     static uint8_t Compact(unsigned x) {
65         return (uint8_t)x;
66     }
67 };
68 
69 struct ColorTypeFilter_F16 {
70     typedef uint64_t Type; // SkHalf x4
ExpandColorTypeFilter_F1671     static Sk4f Expand(uint64_t x) {
72         return SkHalfToFloat_finite_ftz(x);
73     }
CompactColorTypeFilter_F1674     static uint64_t Compact(const Sk4f& x) {
75         uint64_t r;
76         SkFloatToHalf_finite_ftz(x).store(&r);
77         return r;
78     }
79 };
80 
add_121(const T & a,const T & b,const T & c)81 template <typename T> T add_121(const T& a, const T& b, const T& c) {
82     return a + b + b + c;
83 }
84 
shift_right(const T & x,int bits)85 template <typename T> T shift_right(const T& x, int bits) {
86     return x >> bits;
87 }
88 
shift_right(const Sk4f & x,int bits)89 Sk4f shift_right(const Sk4f& x, int bits) {
90     return x * (1.0f / (1 << bits));
91 }
92 
shift_left(const T & x,int bits)93 template <typename T> T shift_left(const T& x, int bits) {
94     return x << bits;
95 }
96 
shift_left(const Sk4f & x,int bits)97 Sk4f shift_left(const Sk4f& x, int bits) {
98     return x * (1 << bits);
99 }
100 
101 //
102 //  To produce each mip level, we need to filter down by 1/2 (e.g. 100x100 -> 50,50)
103 //  If the starting dimension is odd, we floor the size of the lower level (e.g. 101 -> 50)
104 //  In those (odd) cases, we use a triangle filter, with 1-pixel overlap between samplings,
105 //  else for even cases, we just use a 2x box filter.
106 //
107 //  This produces 4 possible isotropic filters: 2x2 2x3 3x2 3x3 where WxH indicates the number of
108 //  src pixels we need to sample in each dimension to produce 1 dst pixel.
109 //
110 //  OpenGL expects a full mipmap stack to contain anisotropic space as well.
111 //  This means a 100x1 image would continue down to a 50x1 image, 25x1 image...
112 //  Because of this, we need 4 more anisotropic filters: 1x2, 1x3, 2x1, 3x1.
113 
downsample_1_2(void * dst,const void * src,size_t srcRB,int count)114 template <typename F> void downsample_1_2(void* dst, const void* src, size_t srcRB, int count) {
115     SkASSERT(count > 0);
116     auto p0 = static_cast<const typename F::Type*>(src);
117     auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
118     auto d = static_cast<typename F::Type*>(dst);
119 
120     for (int i = 0; i < count; ++i) {
121         auto c00 = F::Expand(p0[0]);
122         auto c10 = F::Expand(p1[0]);
123 
124         auto c = c00 + c10;
125         d[i] = F::Compact(shift_right(c, 1));
126         p0 += 2;
127         p1 += 2;
128     }
129 }
130 
downsample_1_3(void * dst,const void * src,size_t srcRB,int count)131 template <typename F> void downsample_1_3(void* dst, const void* src, size_t srcRB, int count) {
132     SkASSERT(count > 0);
133     auto p0 = static_cast<const typename F::Type*>(src);
134     auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
135     auto p2 = (const typename F::Type*)((const char*)p1 + srcRB);
136     auto d = static_cast<typename F::Type*>(dst);
137 
138     for (int i = 0; i < count; ++i) {
139         auto c00 = F::Expand(p0[0]);
140         auto c10 = F::Expand(p1[0]);
141         auto c20 = F::Expand(p2[0]);
142 
143         auto c = add_121(c00, c10, c20);
144         d[i] = F::Compact(shift_right(c, 2));
145         p0 += 2;
146         p1 += 2;
147         p2 += 2;
148     }
149 }
150 
downsample_2_1(void * dst,const void * src,size_t srcRB,int count)151 template <typename F> void downsample_2_1(void* dst, const void* src, size_t srcRB, int count) {
152     SkASSERT(count > 0);
153     auto p0 = static_cast<const typename F::Type*>(src);
154     auto d = static_cast<typename F::Type*>(dst);
155 
156     for (int i = 0; i < count; ++i) {
157         auto c00 = F::Expand(p0[0]);
158         auto c01 = F::Expand(p0[1]);
159 
160         auto c = c00 + c01;
161         d[i] = F::Compact(shift_right(c, 1));
162         p0 += 2;
163     }
164 }
165 
downsample_2_2(void * dst,const void * src,size_t srcRB,int count)166 template <typename F> void downsample_2_2(void* dst, const void* src, size_t srcRB, int count) {
167     SkASSERT(count > 0);
168     auto p0 = static_cast<const typename F::Type*>(src);
169     auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
170     auto d = static_cast<typename F::Type*>(dst);
171 
172     for (int i = 0; i < count; ++i) {
173         auto c00 = F::Expand(p0[0]);
174         auto c01 = F::Expand(p0[1]);
175         auto c10 = F::Expand(p1[0]);
176         auto c11 = F::Expand(p1[1]);
177 
178         auto c = c00 + c10 + c01 + c11;
179         d[i] = F::Compact(shift_right(c, 2));
180         p0 += 2;
181         p1 += 2;
182     }
183 }
184 
downsample_2_3(void * dst,const void * src,size_t srcRB,int count)185 template <typename F> void downsample_2_3(void* dst, const void* src, size_t srcRB, int count) {
186     SkASSERT(count > 0);
187     auto p0 = static_cast<const typename F::Type*>(src);
188     auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
189     auto p2 = (const typename F::Type*)((const char*)p1 + srcRB);
190     auto d = static_cast<typename F::Type*>(dst);
191 
192     for (int i = 0; i < count; ++i) {
193         auto c00 = F::Expand(p0[0]);
194         auto c01 = F::Expand(p0[1]);
195         auto c10 = F::Expand(p1[0]);
196         auto c11 = F::Expand(p1[1]);
197         auto c20 = F::Expand(p2[0]);
198         auto c21 = F::Expand(p2[1]);
199 
200         auto c = add_121(c00, c10, c20) + add_121(c01, c11, c21);
201         d[i] = F::Compact(shift_right(c, 3));
202         p0 += 2;
203         p1 += 2;
204         p2 += 2;
205     }
206 }
207 
downsample_3_1(void * dst,const void * src,size_t srcRB,int count)208 template <typename F> void downsample_3_1(void* dst, const void* src, size_t srcRB, int count) {
209     SkASSERT(count > 0);
210     auto p0 = static_cast<const typename F::Type*>(src);
211     auto d = static_cast<typename F::Type*>(dst);
212 
213     auto c02 = F::Expand(p0[0]);
214     for (int i = 0; i < count; ++i) {
215         auto c00 = c02;
216         auto c01 = F::Expand(p0[1]);
217              c02 = F::Expand(p0[2]);
218 
219         auto c = add_121(c00, c01, c02);
220         d[i] = F::Compact(shift_right(c, 2));
221         p0 += 2;
222     }
223 }
224 
downsample_3_2(void * dst,const void * src,size_t srcRB,int count)225 template <typename F> void downsample_3_2(void* dst, const void* src, size_t srcRB, int count) {
226     SkASSERT(count > 0);
227     auto p0 = static_cast<const typename F::Type*>(src);
228     auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
229     auto d = static_cast<typename F::Type*>(dst);
230 
231     // Given pixels:
232     // a0 b0 c0 d0 e0 ...
233     // a1 b1 c1 d1 e1 ...
234     // We want:
235     // (a0 + 2*b0 + c0 + a1 + 2*b1 + c1) / 8
236     // (c0 + 2*d0 + e0 + c1 + 2*d1 + e1) / 8
237     // ...
238 
239     auto c0 = F::Expand(p0[0]);
240     auto c1 = F::Expand(p1[0]);
241     auto c = c0 + c1;
242     for (int i = 0; i < count; ++i) {
243         auto a = c;
244 
245         auto b0 = F::Expand(p0[1]);
246         auto b1 = F::Expand(p1[1]);
247         auto b = b0 + b0 + b1 + b1;
248 
249         c0 = F::Expand(p0[2]);
250         c1 = F::Expand(p1[2]);
251         c = c0 + c1;
252 
253         auto sum = a + b + c;
254         d[i] = F::Compact(shift_right(sum, 3));
255         p0 += 2;
256         p1 += 2;
257     }
258 }
259 
downsample_3_3(void * dst,const void * src,size_t srcRB,int count)260 template <typename F> void downsample_3_3(void* dst, const void* src, size_t srcRB, int count) {
261     SkASSERT(count > 0);
262     auto p0 = static_cast<const typename F::Type*>(src);
263     auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
264     auto p2 = (const typename F::Type*)((const char*)p1 + srcRB);
265     auto d = static_cast<typename F::Type*>(dst);
266 
267     // Given pixels:
268     // a0 b0 c0 d0 e0 ...
269     // a1 b1 c1 d1 e1 ...
270     // a2 b2 c2 d2 e2 ...
271     // We want:
272     // (a0 + 2*b0 + c0 + 2*a1 + 4*b1 + 2*c1 + a2 + 2*b2 + c2) / 16
273     // (c0 + 2*d0 + e0 + 2*c1 + 4*d1 + 2*e1 + c2 + 2*d2 + e2) / 16
274     // ...
275 
276     auto c0 = F::Expand(p0[0]);
277     auto c1 = F::Expand(p1[0]);
278     auto c2 = F::Expand(p2[0]);
279     auto c = add_121(c0, c1, c2);
280     for (int i = 0; i < count; ++i) {
281         auto a = c;
282 
283         auto b0 = F::Expand(p0[1]);
284         auto b1 = F::Expand(p1[1]);
285         auto b2 = F::Expand(p2[1]);
286         auto b = shift_left(add_121(b0, b1, b2), 1);
287 
288         c0 = F::Expand(p0[2]);
289         c1 = F::Expand(p1[2]);
290         c2 = F::Expand(p2[2]);
291         c = add_121(c0, c1, c2);
292 
293         auto sum = a + b + c;
294         d[i] = F::Compact(shift_right(sum, 4));
295         p0 += 2;
296         p1 += 2;
297         p2 += 2;
298     }
299 }
300 
301 ///////////////////////////////////////////////////////////////////////////////////////////////////
302 
AllocLevelsSize(int levelCount,size_t pixelSize)303 size_t SkMipMap::AllocLevelsSize(int levelCount, size_t pixelSize) {
304     if (levelCount < 0) {
305         return 0;
306     }
307     int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize;
308     if (!SkTFitsIn<int32_t>(size)) {
309         return 0;
310     }
311     return SkTo<int32_t>(size);
312 }
313 
Build(const SkPixmap & src,SkDiscardableFactoryProc fact)314 SkMipMap* SkMipMap::Build(const SkPixmap& src, SkDiscardableFactoryProc fact) {
315     typedef void FilterProc(void*, const void* srcPtr, size_t srcRB, int count);
316 
317     FilterProc* proc_1_2 = nullptr;
318     FilterProc* proc_1_3 = nullptr;
319     FilterProc* proc_2_1 = nullptr;
320     FilterProc* proc_2_2 = nullptr;
321     FilterProc* proc_2_3 = nullptr;
322     FilterProc* proc_3_1 = nullptr;
323     FilterProc* proc_3_2 = nullptr;
324     FilterProc* proc_3_3 = nullptr;
325 
326     const SkColorType ct = src.colorType();
327     const SkAlphaType at = src.alphaType();
328 
329     switch (ct) {
330         case kRGBA_8888_SkColorType:
331         case kBGRA_8888_SkColorType:
332             proc_1_2 = downsample_1_2<ColorTypeFilter_8888>;
333             proc_1_3 = downsample_1_3<ColorTypeFilter_8888>;
334             proc_2_1 = downsample_2_1<ColorTypeFilter_8888>;
335             proc_2_2 = downsample_2_2<ColorTypeFilter_8888>;
336             proc_2_3 = downsample_2_3<ColorTypeFilter_8888>;
337             proc_3_1 = downsample_3_1<ColorTypeFilter_8888>;
338             proc_3_2 = downsample_3_2<ColorTypeFilter_8888>;
339             proc_3_3 = downsample_3_3<ColorTypeFilter_8888>;
340             break;
341         case kRGB_565_SkColorType:
342             proc_1_2 = downsample_1_2<ColorTypeFilter_565>;
343             proc_1_3 = downsample_1_3<ColorTypeFilter_565>;
344             proc_2_1 = downsample_2_1<ColorTypeFilter_565>;
345             proc_2_2 = downsample_2_2<ColorTypeFilter_565>;
346             proc_2_3 = downsample_2_3<ColorTypeFilter_565>;
347             proc_3_1 = downsample_3_1<ColorTypeFilter_565>;
348             proc_3_2 = downsample_3_2<ColorTypeFilter_565>;
349             proc_3_3 = downsample_3_3<ColorTypeFilter_565>;
350             break;
351         case kARGB_4444_SkColorType:
352             proc_1_2 = downsample_1_2<ColorTypeFilter_4444>;
353             proc_1_3 = downsample_1_3<ColorTypeFilter_4444>;
354             proc_2_1 = downsample_2_1<ColorTypeFilter_4444>;
355             proc_2_2 = downsample_2_2<ColorTypeFilter_4444>;
356             proc_2_3 = downsample_2_3<ColorTypeFilter_4444>;
357             proc_3_1 = downsample_3_1<ColorTypeFilter_4444>;
358             proc_3_2 = downsample_3_2<ColorTypeFilter_4444>;
359             proc_3_3 = downsample_3_3<ColorTypeFilter_4444>;
360             break;
361         case kAlpha_8_SkColorType:
362         case kGray_8_SkColorType:
363             proc_1_2 = downsample_1_2<ColorTypeFilter_8>;
364             proc_1_3 = downsample_1_3<ColorTypeFilter_8>;
365             proc_2_1 = downsample_2_1<ColorTypeFilter_8>;
366             proc_2_2 = downsample_2_2<ColorTypeFilter_8>;
367             proc_2_3 = downsample_2_3<ColorTypeFilter_8>;
368             proc_3_1 = downsample_3_1<ColorTypeFilter_8>;
369             proc_3_2 = downsample_3_2<ColorTypeFilter_8>;
370             proc_3_3 = downsample_3_3<ColorTypeFilter_8>;
371             break;
372         case kRGBA_F16Norm_SkColorType:
373         case kRGBA_F16_SkColorType:
374             proc_1_2 = downsample_1_2<ColorTypeFilter_F16>;
375             proc_1_3 = downsample_1_3<ColorTypeFilter_F16>;
376             proc_2_1 = downsample_2_1<ColorTypeFilter_F16>;
377             proc_2_2 = downsample_2_2<ColorTypeFilter_F16>;
378             proc_2_3 = downsample_2_3<ColorTypeFilter_F16>;
379             proc_3_1 = downsample_3_1<ColorTypeFilter_F16>;
380             proc_3_2 = downsample_3_2<ColorTypeFilter_F16>;
381             proc_3_3 = downsample_3_3<ColorTypeFilter_F16>;
382             break;
383         default:
384             return nullptr;
385     }
386 
387     if (src.width() <= 1 && src.height() <= 1) {
388         return nullptr;
389     }
390     // whip through our loop to compute the exact size needed
391     size_t size = 0;
392     int countLevels = ComputeLevelCount(src.width(), src.height());
393     for (int currentMipLevel = countLevels; currentMipLevel >= 0; currentMipLevel--) {
394         SkISize mipSize = ComputeLevelSize(src.width(), src.height(), currentMipLevel);
395         size += SkColorTypeMinRowBytes(ct, mipSize.fWidth) * mipSize.fHeight;
396     }
397 
398     size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size);
399     if (0 == storageSize) {
400         return nullptr;
401     }
402 
403     SkMipMap* mipmap;
404     if (fact) {
405         SkDiscardableMemory* dm = fact(storageSize);
406         if (nullptr == dm) {
407             return nullptr;
408         }
409         mipmap = new SkMipMap(storageSize, dm);
410     } else {
411         mipmap = new SkMipMap(sk_malloc_throw(storageSize), storageSize);
412     }
413 
414     // init
415     mipmap->fCS = sk_ref_sp(src.info().colorSpace());
416     mipmap->fCount = countLevels;
417     mipmap->fLevels = (Level*)mipmap->writable_data();
418     SkASSERT(mipmap->fLevels);
419 
420     Level* levels = mipmap->fLevels;
421     uint8_t*    baseAddr = (uint8_t*)&levels[countLevels];
422     uint8_t*    addr = baseAddr;
423     int         width = src.width();
424     int         height = src.height();
425     uint32_t    rowBytes;
426     SkPixmap    srcPM(src);
427 
428     // Depending on architecture and other factors, the pixel data alignment may need to be as
429     // large as 8 (for F16 pixels). See the comment on SkMipMap::Level.
430     SkASSERT(SkIsAlign8((uintptr_t)addr));
431 
432     for (int i = 0; i < countLevels; ++i) {
433         FilterProc* proc;
434         if (height & 1) {
435             if (height == 1) {        // src-height is 1
436                 if (width & 1) {      // src-width is 3
437                     proc = proc_3_1;
438                 } else {              // src-width is 2
439                     proc = proc_2_1;
440                 }
441             } else {                  // src-height is 3
442                 if (width & 1) {
443                     if (width == 1) { // src-width is 1
444                         proc = proc_1_3;
445                     } else {          // src-width is 3
446                         proc = proc_3_3;
447                     }
448                 } else {              // src-width is 2
449                     proc = proc_2_3;
450                 }
451             }
452         } else {                      // src-height is 2
453             if (width & 1) {
454                 if (width == 1) {     // src-width is 1
455                     proc = proc_1_2;
456                 } else {              // src-width is 3
457                     proc = proc_3_2;
458                 }
459             } else {                  // src-width is 2
460                 proc = proc_2_2;
461             }
462         }
463         width = SkTMax(1, width >> 1);
464         height = SkTMax(1, height >> 1);
465         rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width));
466 
467         // We make the Info w/o any colorspace, since that storage is not under our control, and
468         // will not be deleted in a controlled fashion. When the caller is given the pixmap for
469         // a given level, we augment this pixmap with fCS (which we do manage).
470         new (&levels[i].fPixmap) SkPixmap(SkImageInfo::Make(width, height, ct, at), addr, rowBytes);
471         levels[i].fScale  = SkSize::Make(SkIntToScalar(width)  / src.width(),
472                                          SkIntToScalar(height) / src.height());
473 
474         const SkPixmap& dstPM = levels[i].fPixmap;
475         const void* srcBasePtr = srcPM.addr();
476         void* dstBasePtr = dstPM.writable_addr();
477 
478         const size_t srcRB = srcPM.rowBytes();
479         for (int y = 0; y < height; y++) {
480             proc(dstBasePtr, srcBasePtr, srcRB, width);
481             srcBasePtr = (char*)srcBasePtr + srcRB * 2; // jump two rows
482             dstBasePtr = (char*)dstBasePtr + dstPM.rowBytes();
483         }
484         srcPM = dstPM;
485         addr += height * rowBytes;
486     }
487     SkASSERT(addr == baseAddr + size);
488 
489     SkASSERT(mipmap->fLevels);
490     return mipmap;
491 }
492 
ComputeLevelCount(int baseWidth,int baseHeight)493 int SkMipMap::ComputeLevelCount(int baseWidth, int baseHeight) {
494     if (baseWidth < 1 || baseHeight < 1) {
495         return 0;
496     }
497 
498     // OpenGL's spec requires that each mipmap level have height/width equal to
499     // max(1, floor(original_height / 2^i)
500     // (or original_width) where i is the mipmap level.
501     // Continue scaling down until both axes are size 1.
502 
503     const int largestAxis = SkTMax(baseWidth, baseHeight);
504     if (largestAxis < 2) {
505         // SkMipMap::Build requires a minimum size of 2.
506         return 0;
507     }
508     const int leadingZeros = SkCLZ(static_cast<uint32_t>(largestAxis));
509     // If the value 00011010 has 3 leading 0s then it has 5 significant bits
510     // (the bits which are not leading zeros)
511     const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros;
512     // This is making the assumption that the size of a byte is 8 bits
513     // and that sizeof(uint32_t)'s implementation-defined behavior is 4.
514     int mipLevelCount = significantBits;
515 
516     // SkMipMap does not include the base mip level.
517     // For example, it contains levels 1-x instead of 0-x.
518     // This is because the image used to create SkMipMap is the base level.
519     // So subtract 1 from the mip level count.
520     if (mipLevelCount > 0) {
521         --mipLevelCount;
522     }
523 
524     return mipLevelCount;
525 }
526 
ComputeLevelSize(int baseWidth,int baseHeight,int level)527 SkISize SkMipMap::ComputeLevelSize(int baseWidth, int baseHeight, int level) {
528     if (baseWidth < 1 || baseHeight < 1) {
529         return SkISize::Make(0, 0);
530     }
531 
532     int maxLevelCount = ComputeLevelCount(baseWidth, baseHeight);
533     if (level >= maxLevelCount || level < 0) {
534         return SkISize::Make(0, 0);
535     }
536     // OpenGL's spec requires that each mipmap level have height/width equal to
537     // max(1, floor(original_height / 2^i)
538     // (or original_width) where i is the mipmap level.
539 
540     // SkMipMap does not include the base mip level.
541     // For example, it contains levels 1-x instead of 0-x.
542     // This is because the image used to create SkMipMap is the base level.
543     // So subtract 1 from the mip level to get the index stored by SkMipMap.
544     int width = SkTMax(1, baseWidth >> (level + 1));
545     int height = SkTMax(1, baseHeight >> (level + 1));
546 
547     return SkISize::Make(width, height);
548 }
549 
550 ///////////////////////////////////////////////////////////////////////////////
551 
extractLevel(const SkSize & scaleSize,Level * levelPtr) const552 bool SkMipMap::extractLevel(const SkSize& scaleSize, Level* levelPtr) const {
553     if (nullptr == fLevels) {
554         return false;
555     }
556 
557     SkASSERT(scaleSize.width() >= 0 && scaleSize.height() >= 0);
558 
559 #ifndef SK_SUPPORT_LEGACY_ANISOTROPIC_MIPMAP_SCALE
560     // Use the smallest scale to match the GPU impl.
561     const SkScalar scale = SkTMin(scaleSize.width(), scaleSize.height());
562 #else
563     // Ideally we'd pick the smaller scale, to match Ganesh.  But ignoring one of the
564     // scales can produce some atrocious results, so for now we use the geometric mean.
565     // (https://bugs.chromium.org/p/skia/issues/detail?id=4863)
566     const SkScalar scale = SkScalarSqrt(scaleSize.width() * scaleSize.height());
567 #endif
568 
569     if (scale >= SK_Scalar1 || scale <= 0 || !SkScalarIsFinite(scale)) {
570         return false;
571     }
572 
573     SkScalar L = -SkScalarLog2(scale);
574     if (!SkScalarIsFinite(L)) {
575         return false;
576     }
577     SkASSERT(L >= 0);
578     int level = SkScalarFloorToInt(L);
579 
580     SkASSERT(level >= 0);
581     if (level <= 0) {
582         return false;
583     }
584 
585     if (level > fCount) {
586         level = fCount;
587     }
588     if (levelPtr) {
589         *levelPtr = fLevels[level - 1];
590         // need to augment with our colorspace
591         levelPtr->fPixmap.setColorSpace(fCS);
592     }
593     return true;
594 }
595 
596 // Helper which extracts a pixmap from the src bitmap
597 //
Build(const SkBitmap & src,SkDiscardableFactoryProc fact)598 SkMipMap* SkMipMap::Build(const SkBitmap& src, SkDiscardableFactoryProc fact) {
599     SkPixmap srcPixmap;
600     if (!src.peekPixels(&srcPixmap)) {
601         return nullptr;
602     }
603     return Build(srcPixmap, fact);
604 }
605 
countLevels() const606 int SkMipMap::countLevels() const {
607     return fCount;
608 }
609 
getLevel(int index,Level * levelPtr) const610 bool SkMipMap::getLevel(int index, Level* levelPtr) const {
611     if (nullptr == fLevels) {
612         return false;
613     }
614     if (index < 0) {
615         return false;
616     }
617     if (index > fCount - 1) {
618         return false;
619     }
620     if (levelPtr) {
621         *levelPtr = fLevels[index];
622     }
623     return true;
624 }
625