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
10 #include "SkBitmap.h"
11 #include "SkColorData.h"
12 #include "SkHalf.h"
13 #include "SkImageInfoPriv.h"
14 #include "SkMathPriv.h"
15 #include "SkNx.h"
16 #include "SkTo.h"
17 #include "SkTypes.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) | ((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_F16_SkColorType:
373 proc_1_2 = downsample_1_2<ColorTypeFilter_F16>;
374 proc_1_3 = downsample_1_3<ColorTypeFilter_F16>;
375 proc_2_1 = downsample_2_1<ColorTypeFilter_F16>;
376 proc_2_2 = downsample_2_2<ColorTypeFilter_F16>;
377 proc_2_3 = downsample_2_3<ColorTypeFilter_F16>;
378 proc_3_1 = downsample_3_1<ColorTypeFilter_F16>;
379 proc_3_2 = downsample_3_2<ColorTypeFilter_F16>;
380 proc_3_3 = downsample_3_3<ColorTypeFilter_F16>;
381 break;
382 default:
383 return nullptr;
384 }
385
386 if (src.width() <= 1 && src.height() <= 1) {
387 return nullptr;
388 }
389 // whip through our loop to compute the exact size needed
390 size_t size = 0;
391 int countLevels = ComputeLevelCount(src.width(), src.height());
392 for (int currentMipLevel = countLevels; currentMipLevel >= 0; currentMipLevel--) {
393 SkISize mipSize = ComputeLevelSize(src.width(), src.height(), currentMipLevel);
394 size += SkColorTypeMinRowBytes(ct, mipSize.fWidth) * mipSize.fHeight;
395 }
396
397 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size);
398 if (0 == storageSize) {
399 return nullptr;
400 }
401
402 SkMipMap* mipmap;
403 if (fact) {
404 SkDiscardableMemory* dm = fact(storageSize);
405 if (nullptr == dm) {
406 return nullptr;
407 }
408 mipmap = new SkMipMap(storageSize, dm);
409 } else {
410 mipmap = new SkMipMap(sk_malloc_throw(storageSize), storageSize);
411 }
412
413 // init
414 mipmap->fCS = sk_ref_sp(src.info().colorSpace());
415 mipmap->fCount = countLevels;
416 mipmap->fLevels = (Level*)mipmap->writable_data();
417 SkASSERT(mipmap->fLevels);
418
419 Level* levels = mipmap->fLevels;
420 uint8_t* baseAddr = (uint8_t*)&levels[countLevels];
421 uint8_t* addr = baseAddr;
422 int width = src.width();
423 int height = src.height();
424 uint32_t rowBytes;
425 SkPixmap srcPM(src);
426
427 // Depending on architecture and other factors, the pixel data alignment may need to be as
428 // large as 8 (for F16 pixels). See the comment on SkMipMap::Level.
429 SkASSERT(SkIsAlign8((uintptr_t)addr));
430
431 for (int i = 0; i < countLevels; ++i) {
432 FilterProc* proc;
433 if (height & 1) {
434 if (height == 1) { // src-height is 1
435 if (width & 1) { // src-width is 3
436 proc = proc_3_1;
437 } else { // src-width is 2
438 proc = proc_2_1;
439 }
440 } else { // src-height is 3
441 if (width & 1) {
442 if (width == 1) { // src-width is 1
443 proc = proc_1_3;
444 } else { // src-width is 3
445 proc = proc_3_3;
446 }
447 } else { // src-width is 2
448 proc = proc_2_3;
449 }
450 }
451 } else { // src-height is 2
452 if (width & 1) {
453 if (width == 1) { // src-width is 1
454 proc = proc_1_2;
455 } else { // src-width is 3
456 proc = proc_3_2;
457 }
458 } else { // src-width is 2
459 proc = proc_2_2;
460 }
461 }
462 width = SkTMax(1, width >> 1);
463 height = SkTMax(1, height >> 1);
464 rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width));
465
466 // We make the Info w/o any colorspace, since that storage is not under our control, and
467 // will not be deleted in a controlled fashion. When the caller is given the pixmap for
468 // a given level, we augment this pixmap with fCS (which we do manage).
469 new (&levels[i].fPixmap) SkPixmap(SkImageInfo::Make(width, height, ct, at), addr, rowBytes);
470 levels[i].fScale = SkSize::Make(SkIntToScalar(width) / src.width(),
471 SkIntToScalar(height) / src.height());
472
473 const SkPixmap& dstPM = levels[i].fPixmap;
474 const void* srcBasePtr = srcPM.addr();
475 void* dstBasePtr = dstPM.writable_addr();
476
477 const size_t srcRB = srcPM.rowBytes();
478 for (int y = 0; y < height; y++) {
479 proc(dstBasePtr, srcBasePtr, srcRB, width);
480 srcBasePtr = (char*)srcBasePtr + srcRB * 2; // jump two rows
481 dstBasePtr = (char*)dstBasePtr + dstPM.rowBytes();
482 }
483 srcPM = dstPM;
484 addr += height * rowBytes;
485 }
486 SkASSERT(addr == baseAddr + size);
487
488 SkASSERT(mipmap->fLevels);
489 return mipmap;
490 }
491
ComputeLevelCount(int baseWidth,int baseHeight)492 int SkMipMap::ComputeLevelCount(int baseWidth, int baseHeight) {
493 if (baseWidth < 1 || baseHeight < 1) {
494 return 0;
495 }
496
497 // OpenGL's spec requires that each mipmap level have height/width equal to
498 // max(1, floor(original_height / 2^i)
499 // (or original_width) where i is the mipmap level.
500 // Continue scaling down until both axes are size 1.
501
502 const int largestAxis = SkTMax(baseWidth, baseHeight);
503 if (largestAxis < 2) {
504 // SkMipMap::Build requires a minimum size of 2.
505 return 0;
506 }
507 const int leadingZeros = SkCLZ(static_cast<uint32_t>(largestAxis));
508 // If the value 00011010 has 3 leading 0s then it has 5 significant bits
509 // (the bits which are not leading zeros)
510 const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros;
511 // This is making the assumption that the size of a byte is 8 bits
512 // and that sizeof(uint32_t)'s implementation-defined behavior is 4.
513 int mipLevelCount = significantBits;
514
515 // SkMipMap does not include the base mip level.
516 // For example, it contains levels 1-x instead of 0-x.
517 // This is because the image used to create SkMipMap is the base level.
518 // So subtract 1 from the mip level count.
519 if (mipLevelCount > 0) {
520 --mipLevelCount;
521 }
522
523 return mipLevelCount;
524 }
525
ComputeLevelSize(int baseWidth,int baseHeight,int level)526 SkISize SkMipMap::ComputeLevelSize(int baseWidth, int baseHeight, int level) {
527 if (baseWidth < 1 || baseHeight < 1) {
528 return SkISize::Make(0, 0);
529 }
530
531 int maxLevelCount = ComputeLevelCount(baseWidth, baseHeight);
532 if (level >= maxLevelCount || level < 0) {
533 return SkISize::Make(0, 0);
534 }
535 // OpenGL's spec requires that each mipmap level have height/width equal to
536 // max(1, floor(original_height / 2^i)
537 // (or original_width) where i is the mipmap level.
538
539 // SkMipMap does not include the base mip level.
540 // For example, it contains levels 1-x instead of 0-x.
541 // This is because the image used to create SkMipMap is the base level.
542 // So subtract 1 from the mip level to get the index stored by SkMipMap.
543 int width = SkTMax(1, baseWidth >> (level + 1));
544 int height = SkTMax(1, baseHeight >> (level + 1));
545
546 return SkISize::Make(width, height);
547 }
548
549 ///////////////////////////////////////////////////////////////////////////////
550
extractLevel(const SkSize & scaleSize,Level * levelPtr) const551 bool SkMipMap::extractLevel(const SkSize& scaleSize, Level* levelPtr) const {
552 if (nullptr == fLevels) {
553 return false;
554 }
555
556 SkASSERT(scaleSize.width() >= 0 && scaleSize.height() >= 0);
557
558 #ifndef SK_SUPPORT_LEGACY_ANISOTROPIC_MIPMAP_SCALE
559 // Use the smallest scale to match the GPU impl.
560 const SkScalar scale = SkTMin(scaleSize.width(), scaleSize.height());
561 #else
562 // Ideally we'd pick the smaller scale, to match Ganesh. But ignoring one of the
563 // scales can produce some atrocious results, so for now we use the geometric mean.
564 // (https://bugs.chromium.org/p/skia/issues/detail?id=4863)
565 const SkScalar scale = SkScalarSqrt(scaleSize.width() * scaleSize.height());
566 #endif
567
568 if (scale >= SK_Scalar1 || scale <= 0 || !SkScalarIsFinite(scale)) {
569 return false;
570 }
571
572 SkScalar L = -SkScalarLog2(scale);
573 if (!SkScalarIsFinite(L)) {
574 return false;
575 }
576 SkASSERT(L >= 0);
577 int level = SkScalarFloorToInt(L);
578
579 SkASSERT(level >= 0);
580 if (level <= 0) {
581 return false;
582 }
583
584 if (level > fCount) {
585 level = fCount;
586 }
587 if (levelPtr) {
588 *levelPtr = fLevels[level - 1];
589 // need to augment with our colorspace
590 levelPtr->fPixmap.setColorSpace(fCS);
591 }
592 return true;
593 }
594
595 // Helper which extracts a pixmap from the src bitmap
596 //
Build(const SkBitmap & src,SkDiscardableFactoryProc fact)597 SkMipMap* SkMipMap::Build(const SkBitmap& src, SkDiscardableFactoryProc fact) {
598 SkPixmap srcPixmap;
599 if (!src.peekPixels(&srcPixmap)) {
600 return nullptr;
601 }
602 return Build(srcPixmap, fact);
603 }
604
countLevels() const605 int SkMipMap::countLevels() const {
606 return fCount;
607 }
608
getLevel(int index,Level * levelPtr) const609 bool SkMipMap::getLevel(int index, Level* levelPtr) const {
610 if (nullptr == fLevels) {
611 return false;
612 }
613 if (index < 0) {
614 return false;
615 }
616 if (index > fCount - 1) {
617 return false;
618 }
619 if (levelPtr) {
620 *levelPtr = fLevels[index];
621 }
622 return true;
623 }
624