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
28 struct ColorTypeFilter_8888 {
29 typedef uint32_t Type;
ExpandColorTypeFilter_888830 static Sk4h Expand(uint32_t x) {
31 return SkNx_cast<uint16_t>(Sk4b::Load(&x));
32 }
CompactColorTypeFilter_888833 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
40 struct ColorTypeFilter_565 {
41 typedef uint16_t Type;
ExpandColorTypeFilter_56542 static uint32_t Expand(uint16_t x) {
43 return (x & ~SK_G16_MASK_IN_PLACE) | ((x & SK_G16_MASK_IN_PLACE) << 16);
44 }
CompactColorTypeFilter_56545 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
50 struct ColorTypeFilter_4444 {
51 typedef uint16_t Type;
ExpandColorTypeFilter_444452 static uint32_t Expand(uint16_t x) {
53 return (x & 0xF0F) | ((x & ~0xF0F) << 12);
54 }
CompactColorTypeFilter_444455 static uint16_t Compact(uint32_t x) {
56 return (x & 0xF0F) | ((x >> 12) & ~0xF0F);
57 }
58 };
59
60 struct ColorTypeFilter_8 {
61 typedef uint8_t Type;
ExpandColorTypeFilter_862 static unsigned Expand(unsigned x) {
63 return x;
64 }
CompactColorTypeFilter_865 static uint8_t Compact(unsigned x) {
66 return (uint8_t)x;
67 }
68 };
69
70 struct ColorTypeFilter_Alpha_F16 {
71 typedef uint16_t Type;
ExpandColorTypeFilter_Alpha_F1672 static Sk4f Expand(uint16_t x) {
73 return SkHalfToFloat_finite_ftz((uint64_t) x); // expand out to four lanes
74
75 }
CompactColorTypeFilter_Alpha_F1676 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
83 struct ColorTypeFilter_RGBA_F16 {
84 typedef uint64_t Type; // SkHalf x4
ExpandColorTypeFilter_RGBA_F1685 static Sk4f Expand(uint64_t x) {
86 return SkHalfToFloat_finite_ftz(x);
87 }
CompactColorTypeFilter_RGBA_F1688 static uint64_t Compact(const Sk4f& x) {
89 uint64_t r;
90 SkFloatToHalf_finite_ftz(x).store(&r);
91 return r;
92 }
93 };
94
95 struct ColorTypeFilter_88 {
96 typedef uint16_t Type;
ExpandColorTypeFilter_8897 static uint32_t Expand(uint16_t x) {
98 return (x & 0xFF) | ((x & ~0xFF) << 8);
99 }
CompactColorTypeFilter_88100 static uint16_t Compact(uint32_t x) {
101 return (x & 0xFF) | ((x >> 8) & ~0xFF);
102 }
103 };
104
105 struct ColorTypeFilter_1616 {
106 typedef uint32_t Type;
ExpandColorTypeFilter_1616107 static uint64_t Expand(uint32_t x) {
108 return (x & 0xFFFF) | ((x & ~0xFFFF) << 16);
109 }
CompactColorTypeFilter_1616110 static uint16_t Compact(uint64_t x) {
111 return (x & 0xFFFF) | ((x >> 16) & ~0xFFFF);
112 }
113 };
114
115 struct ColorTypeFilter_F16F16 {
116 typedef uint32_t Type;
ExpandColorTypeFilter_F16F16117 static Sk4f Expand(uint32_t x) {
118 return SkHalfToFloat_finite_ftz((uint64_t) x); // expand out to four lanes
119 }
CompactColorTypeFilter_F16F16120 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
127 struct ColorTypeFilter_16161616 {
128 typedef uint64_t Type;
ExpandColorTypeFilter_16161616129 static skvx::Vec<4, uint32_t> Expand(uint64_t x) {
130 return skvx::cast<uint32_t>(skvx::Vec<4, uint16_t>::Load(&x));
131 }
CompactColorTypeFilter_16161616132 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
139 struct ColorTypeFilter_16 {
140 typedef uint16_t Type;
ExpandColorTypeFilter_16141 static uint32_t Expand(uint16_t x) {
142 return x;
143 }
CompactColorTypeFilter_16144 static uint16_t Compact(uint32_t x) {
145 return (uint16_t) x;
146 }
147 };
148
149 struct ColorTypeFilter_1010102 {
150 typedef uint32_t Type;
ExpandColorTypeFilter_1010102151 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 }
CompactColorTypeFilter_1010102157 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
add_121(const T & a,const T & b,const T & c)165 template <typename T> T add_121(const T& a, const T& b, const T& c) {
166 return a + b + b + c;
167 }
168
shift_right(const T & x,int bits)169 template <typename T> T shift_right(const T& x, int bits) {
170 return x >> bits;
171 }
172
shift_right(const Sk4f & x,int bits)173 Sk4f shift_right(const Sk4f& x, int bits) {
174 return x * (1.0f / (1 << bits));
175 }
176
shift_left(const T & x,int bits)177 template <typename T> T shift_left(const T& x, int bits) {
178 return x << bits;
179 }
180
shift_left(const Sk4f & x,int bits)181 Sk4f 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
downsample_1_2(void * dst,const void * src,size_t srcRB,int count)198 template <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
downsample_1_3(void * dst,const void * src,size_t srcRB,int count)215 template <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
downsample_2_1(void * dst,const void * src,size_t srcRB,int count)235 template <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
downsample_2_2(void * dst,const void * src,size_t srcRB,int count)250 template <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
downsample_2_3(void * dst,const void * src,size_t srcRB,int count)269 template <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
downsample_3_1(void * dst,const void * src,size_t srcRB,int count)292 template <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
downsample_3_2(void * dst,const void * src,size_t srcRB,int count)309 template <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
downsample_3_3(void * dst,const void * src,size_t srcRB,int count)344 template <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
AllocLevelsSize(int levelCount,size_t pixelSize)387 size_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
Build(const SkPixmap & src,SkDiscardableFactoryProc fact)398 SkMipMap* 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 proc_1_2 = downsample_1_2<ColorTypeFilter_1010102>;
499 proc_1_3 = downsample_1_3<ColorTypeFilter_1010102>;
500 proc_2_1 = downsample_2_1<ColorTypeFilter_1010102>;
501 proc_2_2 = downsample_2_2<ColorTypeFilter_1010102>;
502 proc_2_3 = downsample_2_3<ColorTypeFilter_1010102>;
503 proc_3_1 = downsample_3_1<ColorTypeFilter_1010102>;
504 proc_3_2 = downsample_3_2<ColorTypeFilter_1010102>;
505 proc_3_3 = downsample_3_3<ColorTypeFilter_1010102>;
506 break;
507 case kA16_float_SkColorType:
508 proc_1_2 = downsample_1_2<ColorTypeFilter_Alpha_F16>;
509 proc_1_3 = downsample_1_3<ColorTypeFilter_Alpha_F16>;
510 proc_2_1 = downsample_2_1<ColorTypeFilter_Alpha_F16>;
511 proc_2_2 = downsample_2_2<ColorTypeFilter_Alpha_F16>;
512 proc_2_3 = downsample_2_3<ColorTypeFilter_Alpha_F16>;
513 proc_3_1 = downsample_3_1<ColorTypeFilter_Alpha_F16>;
514 proc_3_2 = downsample_3_2<ColorTypeFilter_Alpha_F16>;
515 proc_3_3 = downsample_3_3<ColorTypeFilter_Alpha_F16>;
516 break;
517 case kR16G16_float_SkColorType:
518 proc_1_2 = downsample_1_2<ColorTypeFilter_F16F16>;
519 proc_1_3 = downsample_1_3<ColorTypeFilter_F16F16>;
520 proc_2_1 = downsample_2_1<ColorTypeFilter_F16F16>;
521 proc_2_2 = downsample_2_2<ColorTypeFilter_F16F16>;
522 proc_2_3 = downsample_2_3<ColorTypeFilter_F16F16>;
523 proc_3_1 = downsample_3_1<ColorTypeFilter_F16F16>;
524 proc_3_2 = downsample_3_2<ColorTypeFilter_F16F16>;
525 proc_3_3 = downsample_3_3<ColorTypeFilter_F16F16>;
526 break;
527 case kR16G16B16A16_unorm_SkColorType:
528 proc_1_2 = downsample_1_2<ColorTypeFilter_16161616>;
529 proc_1_3 = downsample_1_3<ColorTypeFilter_16161616>;
530 proc_2_1 = downsample_2_1<ColorTypeFilter_16161616>;
531 proc_2_2 = downsample_2_2<ColorTypeFilter_16161616>;
532 proc_2_3 = downsample_2_3<ColorTypeFilter_16161616>;
533 proc_3_1 = downsample_3_1<ColorTypeFilter_16161616>;
534 proc_3_2 = downsample_3_2<ColorTypeFilter_16161616>;
535 proc_3_3 = downsample_3_3<ColorTypeFilter_16161616>;
536 break;
537 default:
538 return nullptr;
539 }
540
541 if (src.width() <= 1 && src.height() <= 1) {
542 return nullptr;
543 }
544 // whip through our loop to compute the exact size needed
545 size_t size = 0;
546 int countLevels = ComputeLevelCount(src.width(), src.height());
547 for (int currentMipLevel = countLevels; currentMipLevel >= 0; currentMipLevel--) {
548 SkISize mipSize = ComputeLevelSize(src.width(), src.height(), currentMipLevel);
549 size += SkColorTypeMinRowBytes(ct, mipSize.fWidth) * mipSize.fHeight;
550 }
551
552 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size);
553 if (0 == storageSize) {
554 return nullptr;
555 }
556
557 SkMipMap* mipmap;
558 if (fact) {
559 SkDiscardableMemory* dm = fact(storageSize);
560 if (nullptr == dm) {
561 return nullptr;
562 }
563 mipmap = new SkMipMap(storageSize, dm);
564 } else {
565 mipmap = new SkMipMap(sk_malloc_throw(storageSize), storageSize);
566 }
567
568 // init
569 mipmap->fCS = sk_ref_sp(src.info().colorSpace());
570 mipmap->fCount = countLevels;
571 mipmap->fLevels = (Level*)mipmap->writable_data();
572 SkASSERT(mipmap->fLevels);
573
574 Level* levels = mipmap->fLevels;
575 uint8_t* baseAddr = (uint8_t*)&levels[countLevels];
576 uint8_t* addr = baseAddr;
577 int width = src.width();
578 int height = src.height();
579 uint32_t rowBytes;
580 SkPixmap srcPM(src);
581
582 // Depending on architecture and other factors, the pixel data alignment may need to be as
583 // large as 8 (for F16 pixels). See the comment on SkMipMap::Level.
584 SkASSERT(SkIsAlign8((uintptr_t)addr));
585
586 for (int i = 0; i < countLevels; ++i) {
587 FilterProc* proc;
588 if (height & 1) {
589 if (height == 1) { // src-height is 1
590 if (width & 1) { // src-width is 3
591 proc = proc_3_1;
592 } else { // src-width is 2
593 proc = proc_2_1;
594 }
595 } else { // src-height is 3
596 if (width & 1) {
597 if (width == 1) { // src-width is 1
598 proc = proc_1_3;
599 } else { // src-width is 3
600 proc = proc_3_3;
601 }
602 } else { // src-width is 2
603 proc = proc_2_3;
604 }
605 }
606 } else { // src-height is 2
607 if (width & 1) {
608 if (width == 1) { // src-width is 1
609 proc = proc_1_2;
610 } else { // src-width is 3
611 proc = proc_3_2;
612 }
613 } else { // src-width is 2
614 proc = proc_2_2;
615 }
616 }
617 width = SkTMax(1, width >> 1);
618 height = SkTMax(1, height >> 1);
619 rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width));
620
621 // We make the Info w/o any colorspace, since that storage is not under our control, and
622 // will not be deleted in a controlled fashion. When the caller is given the pixmap for
623 // a given level, we augment this pixmap with fCS (which we do manage).
624 new (&levels[i].fPixmap) SkPixmap(SkImageInfo::Make(width, height, ct, at), addr, rowBytes);
625 levels[i].fScale = SkSize::Make(SkIntToScalar(width) / src.width(),
626 SkIntToScalar(height) / src.height());
627
628 const SkPixmap& dstPM = levels[i].fPixmap;
629 const void* srcBasePtr = srcPM.addr();
630 void* dstBasePtr = dstPM.writable_addr();
631
632 const size_t srcRB = srcPM.rowBytes();
633 for (int y = 0; y < height; y++) {
634 proc(dstBasePtr, srcBasePtr, srcRB, width);
635 srcBasePtr = (char*)srcBasePtr + srcRB * 2; // jump two rows
636 dstBasePtr = (char*)dstBasePtr + dstPM.rowBytes();
637 }
638 srcPM = dstPM;
639 addr += height * rowBytes;
640 }
641 SkASSERT(addr == baseAddr + size);
642
643 SkASSERT(mipmap->fLevels);
644 return mipmap;
645 }
646
ComputeLevelCount(int baseWidth,int baseHeight)647 int SkMipMap::ComputeLevelCount(int baseWidth, int baseHeight) {
648 if (baseWidth < 1 || baseHeight < 1) {
649 return 0;
650 }
651
652 // OpenGL's spec requires that each mipmap level have height/width equal to
653 // max(1, floor(original_height / 2^i)
654 // (or original_width) where i is the mipmap level.
655 // Continue scaling down until both axes are size 1.
656
657 const int largestAxis = SkTMax(baseWidth, baseHeight);
658 if (largestAxis < 2) {
659 // SkMipMap::Build requires a minimum size of 2.
660 return 0;
661 }
662 const int leadingZeros = SkCLZ(static_cast<uint32_t>(largestAxis));
663 // If the value 00011010 has 3 leading 0s then it has 5 significant bits
664 // (the bits which are not leading zeros)
665 const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros;
666 // This is making the assumption that the size of a byte is 8 bits
667 // and that sizeof(uint32_t)'s implementation-defined behavior is 4.
668 int mipLevelCount = significantBits;
669
670 // SkMipMap does not include the base mip level.
671 // For example, it contains levels 1-x instead of 0-x.
672 // This is because the image used to create SkMipMap is the base level.
673 // So subtract 1 from the mip level count.
674 if (mipLevelCount > 0) {
675 --mipLevelCount;
676 }
677
678 return mipLevelCount;
679 }
680
ComputeLevelSize(int baseWidth,int baseHeight,int level)681 SkISize SkMipMap::ComputeLevelSize(int baseWidth, int baseHeight, int level) {
682 if (baseWidth < 1 || baseHeight < 1) {
683 return SkISize::Make(0, 0);
684 }
685
686 int maxLevelCount = ComputeLevelCount(baseWidth, baseHeight);
687 if (level >= maxLevelCount || level < 0) {
688 return SkISize::Make(0, 0);
689 }
690 // OpenGL's spec requires that each mipmap level have height/width equal to
691 // max(1, floor(original_height / 2^i)
692 // (or original_width) where i is the mipmap level.
693
694 // SkMipMap does not include the base mip level.
695 // For example, it contains levels 1-x instead of 0-x.
696 // This is because the image used to create SkMipMap is the base level.
697 // So subtract 1 from the mip level to get the index stored by SkMipMap.
698 int width = SkTMax(1, baseWidth >> (level + 1));
699 int height = SkTMax(1, baseHeight >> (level + 1));
700
701 return SkISize::Make(width, height);
702 }
703
704 ///////////////////////////////////////////////////////////////////////////////
705
extractLevel(const SkSize & scaleSize,Level * levelPtr) const706 bool SkMipMap::extractLevel(const SkSize& scaleSize, Level* levelPtr) const {
707 if (nullptr == fLevels) {
708 return false;
709 }
710
711 SkASSERT(scaleSize.width() >= 0 && scaleSize.height() >= 0);
712
713 #ifndef SK_SUPPORT_LEGACY_ANISOTROPIC_MIPMAP_SCALE
714 // Use the smallest scale to match the GPU impl.
715 const SkScalar scale = SkTMin(scaleSize.width(), scaleSize.height());
716 #else
717 // Ideally we'd pick the smaller scale, to match Ganesh. But ignoring one of the
718 // scales can produce some atrocious results, so for now we use the geometric mean.
719 // (https://bugs.chromium.org/p/skia/issues/detail?id=4863)
720 const SkScalar scale = SkScalarSqrt(scaleSize.width() * scaleSize.height());
721 #endif
722
723 if (scale >= SK_Scalar1 || scale <= 0 || !SkScalarIsFinite(scale)) {
724 return false;
725 }
726
727 SkScalar L = -SkScalarLog2(scale);
728 if (!SkScalarIsFinite(L)) {
729 return false;
730 }
731 SkASSERT(L >= 0);
732 int level = SkScalarFloorToInt(L);
733
734 SkASSERT(level >= 0);
735 if (level <= 0) {
736 return false;
737 }
738
739 if (level > fCount) {
740 level = fCount;
741 }
742 if (levelPtr) {
743 *levelPtr = fLevels[level - 1];
744 // need to augment with our colorspace
745 levelPtr->fPixmap.setColorSpace(fCS);
746 }
747 return true;
748 }
749
750 // Helper which extracts a pixmap from the src bitmap
751 //
Build(const SkBitmap & src,SkDiscardableFactoryProc fact)752 SkMipMap* SkMipMap::Build(const SkBitmap& src, SkDiscardableFactoryProc fact) {
753 SkPixmap srcPixmap;
754 if (!src.peekPixels(&srcPixmap)) {
755 return nullptr;
756 }
757 return Build(srcPixmap, fact);
758 }
759
countLevels() const760 int SkMipMap::countLevels() const {
761 return fCount;
762 }
763
getLevel(int index,Level * levelPtr) const764 bool SkMipMap::getLevel(int index, Level* levelPtr) const {
765 if (nullptr == fLevels) {
766 return false;
767 }
768 if (index < 0) {
769 return false;
770 }
771 if (index > fCount - 1) {
772 return false;
773 }
774 if (levelPtr) {
775 *levelPtr = fLevels[index];
776 }
777 return true;
778 }
779