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