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