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 "include/core/SkBitmap.h"
9 #include "include/core/SkTypes.h"
10 #include "include/private/SkColorData.h"
11 #include "include/private/SkHalf.h"
12 #include "include/private/SkImageInfoPriv.h"
13 #include "include/private/SkNx.h"
14 #include "include/private/SkTo.h"
15 #include "include/private/SkVx.h"
16 #include "src/core/SkMathPriv.h"
17 #include "src/core/SkMipmap.h"
18 #include "src/core/SkMipmapBuilder.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,bool computeContents)398 SkMipmap* SkMipmap::Build(const SkPixmap& src, SkDiscardableFactoryProc fact,
399 bool computeContents) {
400 typedef void FilterProc(void*, const void* srcPtr, size_t srcRB, int count);
401
402 FilterProc* proc_1_2 = nullptr;
403 FilterProc* proc_1_3 = nullptr;
404 FilterProc* proc_2_1 = nullptr;
405 FilterProc* proc_2_2 = nullptr;
406 FilterProc* proc_2_3 = nullptr;
407 FilterProc* proc_3_1 = nullptr;
408 FilterProc* proc_3_2 = nullptr;
409 FilterProc* proc_3_3 = nullptr;
410
411 const SkColorType ct = src.colorType();
412 const SkAlphaType at = src.alphaType();
413
414 switch (ct) {
415 case kRGBA_8888_SkColorType:
416 case kBGRA_8888_SkColorType:
417 proc_1_2 = downsample_1_2<ColorTypeFilter_8888>;
418 proc_1_3 = downsample_1_3<ColorTypeFilter_8888>;
419 proc_2_1 = downsample_2_1<ColorTypeFilter_8888>;
420 proc_2_2 = downsample_2_2<ColorTypeFilter_8888>;
421 proc_2_3 = downsample_2_3<ColorTypeFilter_8888>;
422 proc_3_1 = downsample_3_1<ColorTypeFilter_8888>;
423 proc_3_2 = downsample_3_2<ColorTypeFilter_8888>;
424 proc_3_3 = downsample_3_3<ColorTypeFilter_8888>;
425 break;
426 case kRGB_565_SkColorType:
427 proc_1_2 = downsample_1_2<ColorTypeFilter_565>;
428 proc_1_3 = downsample_1_3<ColorTypeFilter_565>;
429 proc_2_1 = downsample_2_1<ColorTypeFilter_565>;
430 proc_2_2 = downsample_2_2<ColorTypeFilter_565>;
431 proc_2_3 = downsample_2_3<ColorTypeFilter_565>;
432 proc_3_1 = downsample_3_1<ColorTypeFilter_565>;
433 proc_3_2 = downsample_3_2<ColorTypeFilter_565>;
434 proc_3_3 = downsample_3_3<ColorTypeFilter_565>;
435 break;
436 case kARGB_4444_SkColorType:
437 proc_1_2 = downsample_1_2<ColorTypeFilter_4444>;
438 proc_1_3 = downsample_1_3<ColorTypeFilter_4444>;
439 proc_2_1 = downsample_2_1<ColorTypeFilter_4444>;
440 proc_2_2 = downsample_2_2<ColorTypeFilter_4444>;
441 proc_2_3 = downsample_2_3<ColorTypeFilter_4444>;
442 proc_3_1 = downsample_3_1<ColorTypeFilter_4444>;
443 proc_3_2 = downsample_3_2<ColorTypeFilter_4444>;
444 proc_3_3 = downsample_3_3<ColorTypeFilter_4444>;
445 break;
446 case kAlpha_8_SkColorType:
447 case kGray_8_SkColorType:
448 proc_1_2 = downsample_1_2<ColorTypeFilter_8>;
449 proc_1_3 = downsample_1_3<ColorTypeFilter_8>;
450 proc_2_1 = downsample_2_1<ColorTypeFilter_8>;
451 proc_2_2 = downsample_2_2<ColorTypeFilter_8>;
452 proc_2_3 = downsample_2_3<ColorTypeFilter_8>;
453 proc_3_1 = downsample_3_1<ColorTypeFilter_8>;
454 proc_3_2 = downsample_3_2<ColorTypeFilter_8>;
455 proc_3_3 = downsample_3_3<ColorTypeFilter_8>;
456 break;
457 case kRGBA_F16Norm_SkColorType:
458 case kRGBA_F16_SkColorType:
459 proc_1_2 = downsample_1_2<ColorTypeFilter_RGBA_F16>;
460 proc_1_3 = downsample_1_3<ColorTypeFilter_RGBA_F16>;
461 proc_2_1 = downsample_2_1<ColorTypeFilter_RGBA_F16>;
462 proc_2_2 = downsample_2_2<ColorTypeFilter_RGBA_F16>;
463 proc_2_3 = downsample_2_3<ColorTypeFilter_RGBA_F16>;
464 proc_3_1 = downsample_3_1<ColorTypeFilter_RGBA_F16>;
465 proc_3_2 = downsample_3_2<ColorTypeFilter_RGBA_F16>;
466 proc_3_3 = downsample_3_3<ColorTypeFilter_RGBA_F16>;
467 break;
468 case kR8G8_unorm_SkColorType:
469 proc_1_2 = downsample_1_2<ColorTypeFilter_88>;
470 proc_1_3 = downsample_1_3<ColorTypeFilter_88>;
471 proc_2_1 = downsample_2_1<ColorTypeFilter_88>;
472 proc_2_2 = downsample_2_2<ColorTypeFilter_88>;
473 proc_2_3 = downsample_2_3<ColorTypeFilter_88>;
474 proc_3_1 = downsample_3_1<ColorTypeFilter_88>;
475 proc_3_2 = downsample_3_2<ColorTypeFilter_88>;
476 proc_3_3 = downsample_3_3<ColorTypeFilter_88>;
477 break;
478 case kR16G16_unorm_SkColorType:
479 proc_1_2 = downsample_1_2<ColorTypeFilter_1616>;
480 proc_1_3 = downsample_1_3<ColorTypeFilter_1616>;
481 proc_2_1 = downsample_2_1<ColorTypeFilter_1616>;
482 proc_2_2 = downsample_2_2<ColorTypeFilter_1616>;
483 proc_2_3 = downsample_2_3<ColorTypeFilter_1616>;
484 proc_3_1 = downsample_3_1<ColorTypeFilter_1616>;
485 proc_3_2 = downsample_3_2<ColorTypeFilter_1616>;
486 proc_3_3 = downsample_3_3<ColorTypeFilter_1616>;
487 break;
488 case kA16_unorm_SkColorType:
489 proc_1_2 = downsample_1_2<ColorTypeFilter_16>;
490 proc_1_3 = downsample_1_3<ColorTypeFilter_16>;
491 proc_2_1 = downsample_2_1<ColorTypeFilter_16>;
492 proc_2_2 = downsample_2_2<ColorTypeFilter_16>;
493 proc_2_3 = downsample_2_3<ColorTypeFilter_16>;
494 proc_3_1 = downsample_3_1<ColorTypeFilter_16>;
495 proc_3_2 = downsample_3_2<ColorTypeFilter_16>;
496 proc_3_3 = downsample_3_3<ColorTypeFilter_16>;
497 break;
498 case kRGBA_1010102_SkColorType:
499 case kBGRA_1010102_SkColorType:
500 proc_1_2 = downsample_1_2<ColorTypeFilter_1010102>;
501 proc_1_3 = downsample_1_3<ColorTypeFilter_1010102>;
502 proc_2_1 = downsample_2_1<ColorTypeFilter_1010102>;
503 proc_2_2 = downsample_2_2<ColorTypeFilter_1010102>;
504 proc_2_3 = downsample_2_3<ColorTypeFilter_1010102>;
505 proc_3_1 = downsample_3_1<ColorTypeFilter_1010102>;
506 proc_3_2 = downsample_3_2<ColorTypeFilter_1010102>;
507 proc_3_3 = downsample_3_3<ColorTypeFilter_1010102>;
508 break;
509 case kA16_float_SkColorType:
510 proc_1_2 = downsample_1_2<ColorTypeFilter_Alpha_F16>;
511 proc_1_3 = downsample_1_3<ColorTypeFilter_Alpha_F16>;
512 proc_2_1 = downsample_2_1<ColorTypeFilter_Alpha_F16>;
513 proc_2_2 = downsample_2_2<ColorTypeFilter_Alpha_F16>;
514 proc_2_3 = downsample_2_3<ColorTypeFilter_Alpha_F16>;
515 proc_3_1 = downsample_3_1<ColorTypeFilter_Alpha_F16>;
516 proc_3_2 = downsample_3_2<ColorTypeFilter_Alpha_F16>;
517 proc_3_3 = downsample_3_3<ColorTypeFilter_Alpha_F16>;
518 break;
519 case kR16G16_float_SkColorType:
520 proc_1_2 = downsample_1_2<ColorTypeFilter_F16F16>;
521 proc_1_3 = downsample_1_3<ColorTypeFilter_F16F16>;
522 proc_2_1 = downsample_2_1<ColorTypeFilter_F16F16>;
523 proc_2_2 = downsample_2_2<ColorTypeFilter_F16F16>;
524 proc_2_3 = downsample_2_3<ColorTypeFilter_F16F16>;
525 proc_3_1 = downsample_3_1<ColorTypeFilter_F16F16>;
526 proc_3_2 = downsample_3_2<ColorTypeFilter_F16F16>;
527 proc_3_3 = downsample_3_3<ColorTypeFilter_F16F16>;
528 break;
529 case kR16G16B16A16_unorm_SkColorType:
530 proc_1_2 = downsample_1_2<ColorTypeFilter_16161616>;
531 proc_1_3 = downsample_1_3<ColorTypeFilter_16161616>;
532 proc_2_1 = downsample_2_1<ColorTypeFilter_16161616>;
533 proc_2_2 = downsample_2_2<ColorTypeFilter_16161616>;
534 proc_2_3 = downsample_2_3<ColorTypeFilter_16161616>;
535 proc_3_1 = downsample_3_1<ColorTypeFilter_16161616>;
536 proc_3_2 = downsample_3_2<ColorTypeFilter_16161616>;
537 proc_3_3 = downsample_3_3<ColorTypeFilter_16161616>;
538 break;
539
540 case kUnknown_SkColorType:
541 case kRGB_888x_SkColorType: // TODO: use 8888?
542 case kRGB_101010x_SkColorType: // TODO: use 1010102?
543 case kBGR_101010x_SkColorType: // TODO: use 1010102?
544 case kRGBA_F32_SkColorType:
545 return nullptr;
546 }
547
548 if (src.width() <= 1 && src.height() <= 1) {
549 return nullptr;
550 }
551 // whip through our loop to compute the exact size needed
552 size_t size = 0;
553 int countLevels = ComputeLevelCount(src.width(), src.height());
554 for (int currentMipLevel = countLevels; currentMipLevel >= 0; currentMipLevel--) {
555 SkISize mipSize = ComputeLevelSize(src.width(), src.height(), currentMipLevel);
556 size += SkColorTypeMinRowBytes(ct, mipSize.fWidth) * mipSize.fHeight;
557 }
558
559 size_t storageSize = SkMipmap::AllocLevelsSize(countLevels, size);
560 if (0 == storageSize) {
561 return nullptr;
562 }
563
564 SkMipmap* mipmap;
565 if (fact) {
566 SkDiscardableMemory* dm = fact(storageSize);
567 if (nullptr == dm) {
568 return nullptr;
569 }
570 mipmap = new SkMipmap(storageSize, dm);
571 } else {
572 mipmap = new SkMipmap(sk_malloc_throw(storageSize), storageSize);
573 }
574
575 // init
576 mipmap->fCS = sk_ref_sp(src.info().colorSpace());
577 mipmap->fCount = countLevels;
578 mipmap->fLevels = (Level*)mipmap->writable_data();
579 SkASSERT(mipmap->fLevels);
580
581 Level* levels = mipmap->fLevels;
582 uint8_t* baseAddr = (uint8_t*)&levels[countLevels];
583 uint8_t* addr = baseAddr;
584 int width = src.width();
585 int height = src.height();
586 uint32_t rowBytes;
587 SkPixmap srcPM(src);
588
589 // Depending on architecture and other factors, the pixel data alignment may need to be as
590 // large as 8 (for F16 pixels). See the comment on SkMipmap::Level.
591 SkASSERT(SkIsAlign8((uintptr_t)addr));
592
593 for (int i = 0; i < countLevels; ++i) {
594 FilterProc* proc;
595 if (height & 1) {
596 if (height == 1) { // src-height is 1
597 if (width & 1) { // src-width is 3
598 proc = proc_3_1;
599 } else { // src-width is 2
600 proc = proc_2_1;
601 }
602 } else { // src-height is 3
603 if (width & 1) {
604 if (width == 1) { // src-width is 1
605 proc = proc_1_3;
606 } else { // src-width is 3
607 proc = proc_3_3;
608 }
609 } else { // src-width is 2
610 proc = proc_2_3;
611 }
612 }
613 } else { // src-height is 2
614 if (width & 1) {
615 if (width == 1) { // src-width is 1
616 proc = proc_1_2;
617 } else { // src-width is 3
618 proc = proc_3_2;
619 }
620 } else { // src-width is 2
621 proc = proc_2_2;
622 }
623 }
624 width = std::max(1, width >> 1);
625 height = std::max(1, height >> 1);
626 rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width));
627
628 // We make the Info w/o any colorspace, since that storage is not under our control, and
629 // will not be deleted in a controlled fashion. When the caller is given the pixmap for
630 // a given level, we augment this pixmap with fCS (which we do manage).
631 new (&levels[i].fPixmap) SkPixmap(SkImageInfo::Make(width, height, ct, at), addr, rowBytes);
632 levels[i].fScale = SkSize::Make(SkIntToScalar(width) / src.width(),
633 SkIntToScalar(height) / src.height());
634
635 const SkPixmap& dstPM = levels[i].fPixmap;
636 if (computeContents) {
637 const void* srcBasePtr = srcPM.addr();
638 void* dstBasePtr = dstPM.writable_addr();
639
640 const size_t srcRB = srcPM.rowBytes();
641 for (int y = 0; y < height; y++) {
642 proc(dstBasePtr, srcBasePtr, srcRB, width);
643 srcBasePtr = (char*)srcBasePtr + srcRB * 2; // jump two rows
644 dstBasePtr = (char*)dstBasePtr + dstPM.rowBytes();
645 }
646 }
647 srcPM = dstPM;
648 addr += height * rowBytes;
649 }
650 SkASSERT(addr == baseAddr + size);
651
652 SkASSERT(mipmap->fLevels);
653 return mipmap;
654 }
655
ComputeLevelCount(int baseWidth,int baseHeight)656 int SkMipmap::ComputeLevelCount(int baseWidth, int baseHeight) {
657 if (baseWidth < 1 || baseHeight < 1) {
658 return 0;
659 }
660
661 // OpenGL's spec requires that each mipmap level have height/width equal to
662 // max(1, floor(original_height / 2^i)
663 // (or original_width) where i is the mipmap level.
664 // Continue scaling down until both axes are size 1.
665
666 const int largestAxis = std::max(baseWidth, baseHeight);
667 if (largestAxis < 2) {
668 // SkMipmap::Build requires a minimum size of 2.
669 return 0;
670 }
671 const int leadingZeros = SkCLZ(static_cast<uint32_t>(largestAxis));
672 // If the value 00011010 has 3 leading 0s then it has 5 significant bits
673 // (the bits which are not leading zeros)
674 const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros;
675 // This is making the assumption that the size of a byte is 8 bits
676 // and that sizeof(uint32_t)'s implementation-defined behavior is 4.
677 int mipLevelCount = significantBits;
678
679 // SkMipmap does not include the base mip level.
680 // For example, it contains levels 1-x instead of 0-x.
681 // This is because the image used to create SkMipmap is the base level.
682 // So subtract 1 from the mip level count.
683 if (mipLevelCount > 0) {
684 --mipLevelCount;
685 }
686
687 return mipLevelCount;
688 }
689
ComputeLevelSize(int baseWidth,int baseHeight,int level)690 SkISize SkMipmap::ComputeLevelSize(int baseWidth, int baseHeight, int level) {
691 if (baseWidth < 1 || baseHeight < 1) {
692 return SkISize::Make(0, 0);
693 }
694
695 int maxLevelCount = ComputeLevelCount(baseWidth, baseHeight);
696 if (level >= maxLevelCount || level < 0) {
697 return SkISize::Make(0, 0);
698 }
699 // OpenGL's spec requires that each mipmap level have height/width equal to
700 // max(1, floor(original_height / 2^i)
701 // (or original_width) where i is the mipmap level.
702
703 // SkMipmap does not include the base mip level.
704 // For example, it contains levels 1-x instead of 0-x.
705 // This is because the image used to create SkMipmap is the base level.
706 // So subtract 1 from the mip level to get the index stored by SkMipmap.
707 int width = std::max(1, baseWidth >> (level + 1));
708 int height = std::max(1, baseHeight >> (level + 1));
709
710 return SkISize::Make(width, height);
711 }
712
713 ///////////////////////////////////////////////////////////////////////////////
714
715 // Returns fractional level value. floor(level) is the index of the larger level.
716 // < 0 means failure.
ComputeLevel(SkSize scaleSize)717 float SkMipmap::ComputeLevel(SkSize scaleSize) {
718 SkASSERT(scaleSize.width() >= 0 && scaleSize.height() >= 0);
719
720 #ifndef SK_SUPPORT_LEGACY_ANISOTROPIC_MIPMAP_SCALE
721 // Use the smallest scale to match the GPU impl.
722 const SkScalar scale = std::min(scaleSize.width(), scaleSize.height());
723 #else
724 // Ideally we'd pick the smaller scale, to match Ganesh. But ignoring one of the
725 // scales can produce some atrocious results, so for now we use the geometric mean.
726 // (https://bugs.chromium.org/p/skia/issues/detail?id=4863)
727 const SkScalar scale = SkScalarSqrt(scaleSize.width() * scaleSize.height());
728 #endif
729
730 if (scale >= SK_Scalar1 || scale <= 0 || !SkScalarIsFinite(scale)) {
731 return -1;
732 }
733
734 SkScalar L = -SkScalarLog2(scale);
735 if (!SkScalarIsFinite(L)) {
736 return -1;
737 }
738 SkASSERT(L >= 0);
739 return L;
740 }
741
extractLevel(SkSize scaleSize,Level * levelPtr) const742 bool SkMipmap::extractLevel(SkSize scaleSize, Level* levelPtr) const {
743 if (nullptr == fLevels) {
744 return false;
745 }
746
747 float L = ComputeLevel(scaleSize);
748 int level = SkScalarFloorToInt(L);
749 if (level <= 0) {
750 return false;
751 }
752
753 if (level > fCount) {
754 level = fCount;
755 }
756 if (levelPtr) {
757 *levelPtr = fLevels[level - 1];
758 // need to augment with our colorspace
759 levelPtr->fPixmap.setColorSpace(fCS);
760 }
761 return true;
762 }
763
validForRootLevel(const SkImageInfo & root) const764 bool SkMipmap::validForRootLevel(const SkImageInfo& root) const {
765 if (nullptr == fLevels) {
766 return false;
767 }
768
769 const SkISize dimension = root.dimensions();
770 if (dimension.width() <= 1 && dimension.height() <= 1) {
771 return false;
772 }
773
774 const SkPixmap& pm = fLevels[0].fPixmap;
775 if (pm. width() != std::max(1, dimension. width() >> 1) ||
776 pm.height() != std::max(1, dimension.height() >> 1)) {
777 return false;
778 }
779
780 for (int i = 0; i < this->countLevels(); ++i) {
781 const SkPixmap& pm = fLevels[0].fPixmap;
782 if (pm.colorType() != root.colorType() ||
783 pm.alphaType() != root.alphaType())
784 return false;
785 }
786 return true;
787 }
788
789 // Helper which extracts a pixmap from the src bitmap
790 //
Build(const SkBitmap & src,SkDiscardableFactoryProc fact)791 SkMipmap* SkMipmap::Build(const SkBitmap& src, SkDiscardableFactoryProc fact) {
792 SkPixmap srcPixmap;
793 if (!src.peekPixels(&srcPixmap)) {
794 return nullptr;
795 }
796 return Build(srcPixmap, fact);
797 }
798
countLevels() const799 int SkMipmap::countLevels() const {
800 return fCount;
801 }
802
getLevel(int index,Level * levelPtr) const803 bool SkMipmap::getLevel(int index, Level* levelPtr) const {
804 if (nullptr == fLevels) {
805 return false;
806 }
807 if (index < 0) {
808 return false;
809 }
810 if (index > fCount - 1) {
811 return false;
812 }
813 if (levelPtr) {
814 *levelPtr = fLevels[index];
815 // need to augment with our colorspace
816 levelPtr->fPixmap.setColorSpace(fCS);
817 }
818 return true;
819 }
820
821 //////////////////////////////////////////////////////////////////////////////////////////////////
822
823 #include "include/core/SkImageGenerator.h"
824 #include "include/core/SkStream.h"
825 #include "include/encode/SkPngEncoder.h"
826 #include "src/core/SkReadBuffer.h"
827 #include "src/core/SkWriteBuffer.h"
828
encode_to_data(const SkPixmap & pm)829 static sk_sp<SkData> encode_to_data(const SkPixmap& pm) {
830 SkDynamicMemoryWStream stream;
831 if (SkPngEncoder::Encode(&stream, pm, SkPngEncoder::Options())) {
832 return stream.detachAsData();
833 }
834 return nullptr;
835 }
836
837 /* Format
838 count_levels:32
839 for each level, starting with the biggest (index 0 in our iterator)
840 encoded_size:32
841 encoded_data (padded)
842 */
serialize() const843 sk_sp<SkData> SkMipmap::serialize() const {
844 const int count = this->countLevels();
845
846 SkBinaryWriteBuffer buffer;
847 buffer.write32(count);
848 for (int i = 0; i < count; ++i) {
849 Level level;
850 if (this->getLevel(i, &level)) {
851 buffer.writeDataAsByteArray(encode_to_data(level.fPixmap).get());
852 } else {
853 return nullptr;
854 }
855 }
856 return buffer.snapshotAsData();
857 }
858
Deserialize(SkMipmapBuilder * builder,const void * data,size_t size)859 bool SkMipmap::Deserialize(SkMipmapBuilder* builder, const void* data, size_t size) {
860 SkReadBuffer buffer(data, size);
861
862 int count = buffer.read32();
863 if (builder->countLevels() != count) {
864 return false;
865 }
866 for (int i = 0; i < count; ++i) {
867 size_t size = buffer.read32();
868 const void* ptr = buffer.skip(size);
869 if (!ptr) {
870 return false;
871 }
872 auto gen = SkImageGenerator::MakeFromEncoded(
873 SkData::MakeWithProc(ptr, size, nullptr, nullptr));
874 if (!gen) {
875 return false;
876 }
877
878 SkPixmap pm = builder->level(i);
879 if (gen->getInfo().dimensions() != pm.dimensions()) {
880 return false;
881 }
882 if (!gen->getPixels(pm)) {
883 return false;
884 }
885 }
886 return buffer.isValid();
887 }
888