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 <new> |
19 | |
20 | // |
21 | // ColorTypeFilter is the "Type" we pass to some downsample template functions. |
22 | // It controls how we expand a pixel into a large type, with space between each component, |
23 | // so we can then perform our simple filter (either box or triangle) and store the intermediates |
24 | // in the expanded type. |
25 | // |
26 | |
27 | struct ColorTypeFilter_8888 { |
28 | typedef uint32_t Type; |
29 | static Sk4h Expand(uint32_t x) { |
30 | return SkNx_cast<uint16_t>(Sk4b::Load(&x)); |
31 | } |
32 | static uint32_t Compact(const Sk4h& x) { |
33 | uint32_t r; |
34 | SkNx_cast<uint8_t>(x).store(&r); |
35 | return r; |
36 | } |
37 | }; |
38 | |
39 | struct ColorTypeFilter_565 { |
40 | typedef uint16_t Type; |
41 | static uint32_t Expand(uint16_t x) { |
42 | return (x & ~SK_G16_MASK_IN_PLACE) | ((x & SK_G16_MASK_IN_PLACE) << 16); |
43 | } |
44 | static uint16_t Compact(uint32_t x) { |
45 | return ((x & ~SK_G16_MASK_IN_PLACE) & 0xFFFF) | ((x >> 16) & SK_G16_MASK_IN_PLACE); |
46 | } |
47 | }; |
48 | |
49 | struct ColorTypeFilter_4444 { |
50 | typedef uint16_t Type; |
51 | static uint32_t Expand(uint16_t x) { |
52 | return (x & 0xF0F) | ((x & ~0xF0F) << 12); |
53 | } |
54 | static uint16_t Compact(uint32_t x) { |
55 | return (x & 0xF0F) | ((x >> 12) & ~0xF0F); |
56 | } |
57 | }; |
58 | |
59 | struct ColorTypeFilter_8 { |
60 | typedef uint8_t Type; |
61 | static unsigned Expand(unsigned x) { |
62 | return x; |
63 | } |
64 | static uint8_t Compact(unsigned x) { |
65 | return (uint8_t)x; |
66 | } |
67 | }; |
68 | |
69 | struct ColorTypeFilter_Alpha_F16 { |
70 | typedef uint16_t Type; |
71 | static Sk4f Expand(uint16_t x) { |
72 | return SkHalfToFloat_finite_ftz((uint64_t) x); // expand out to four lanes |
73 | |
74 | } |
75 | static uint16_t Compact(const Sk4f& x) { |
76 | uint64_t r; |
77 | SkFloatToHalf_finite_ftz(x).store(&r); |
78 | return r & 0xFFFF; // but ignore the extra 3 here |
79 | } |
80 | }; |
81 | |
82 | struct ColorTypeFilter_RGBA_F16 { |
83 | typedef uint64_t Type; // SkHalf x4 |
84 | static Sk4f Expand(uint64_t x) { |
85 | return SkHalfToFloat_finite_ftz(x); |
86 | } |
87 | static uint64_t Compact(const Sk4f& x) { |
88 | uint64_t r; |
89 | SkFloatToHalf_finite_ftz(x).store(&r); |
90 | return r; |
91 | } |
92 | }; |
93 | |
94 | struct ColorTypeFilter_88 { |
95 | typedef uint16_t Type; |
96 | static uint32_t Expand(uint16_t x) { |
97 | return (x & 0xFF) | ((x & ~0xFF) << 8); |
98 | } |
99 | static uint16_t Compact(uint32_t x) { |
100 | return (x & 0xFF) | ((x >> 8) & ~0xFF); |
101 | } |
102 | }; |
103 | |
104 | struct ColorTypeFilter_1616 { |
105 | typedef uint32_t Type; |
106 | static uint64_t Expand(uint32_t x) { |
107 | return (x & 0xFFFF) | ((x & ~0xFFFF) << 16); |
108 | } |
109 | static uint16_t Compact(uint64_t x) { |
110 | return (x & 0xFFFF) | ((x >> 16) & ~0xFFFF); |
111 | } |
112 | }; |
113 | |
114 | struct ColorTypeFilter_F16F16 { |
115 | typedef uint32_t Type; |
116 | static Sk4f Expand(uint32_t x) { |
117 | return SkHalfToFloat_finite_ftz((uint64_t) x); // expand out to four lanes |
118 | } |
119 | static uint32_t Compact(const Sk4f& x) { |
120 | uint64_t r; |
121 | SkFloatToHalf_finite_ftz(x).store(&r); |
122 | return (uint32_t) (r & 0xFFFFFFFF); // but ignore the extra 2 here |
123 | } |
124 | }; |
125 | |
126 | struct ColorTypeFilter_16161616 { |
127 | typedef uint64_t Type; |
128 | static skvx::Vec<4, uint32_t> Expand(uint64_t x) { |
129 | return skvx::cast<uint32_t>(skvx::Vec<4, uint16_t>::Load(&x)); |
130 | } |
131 | static uint64_t Compact(const skvx::Vec<4, uint32_t>& x) { |
132 | uint64_t r; |
133 | skvx::cast<uint16_t>(x).store(&r); |
134 | return r; |
135 | } |
136 | }; |
137 | |
138 | struct ColorTypeFilter_16 { |
139 | typedef uint16_t Type; |
140 | static uint32_t Expand(uint16_t x) { |
141 | return x; |
142 | } |
143 | static uint16_t Compact(uint32_t x) { |
144 | return (uint16_t) x; |
145 | } |
146 | }; |
147 | |
148 | struct ColorTypeFilter_1010102 { |
149 | typedef uint32_t Type; |
150 | static uint64_t Expand(uint64_t x) { |
151 | return (((x ) & 0x3ff) ) | |
152 | (((x >> 10) & 0x3ff) << 20) | |
153 | (((x >> 20) & 0x3ff) << 40) | |
154 | (((x >> 30) & 0x3 ) << 60); |
155 | } |
156 | static uint32_t Compact(uint64_t x) { |
157 | return (((x ) & 0x3ff) ) | |
158 | (((x >> 20) & 0x3ff) << 10) | |
159 | (((x >> 40) & 0x3ff) << 20) | |
160 | (((x >> 60) & 0x3 ) << 30); |
161 | } |
162 | }; |
163 | |
164 | template <typename T> T add_121(const T& a, const T& b, const T& c) { |
165 | return a + b + b + c; |
166 | } |
167 | |
168 | template <typename T> T shift_right(const T& x, int bits) { |
169 | return x >> bits; |
170 | } |
171 | |
172 | Sk4f shift_right(const Sk4f& x, int bits) { |
173 | return x * (1.0f / (1 << bits)); |
174 | } |
175 | |
176 | template <typename T> T shift_left(const T& x, int bits) { |
177 | return x << bits; |
178 | } |
179 | |
180 | Sk4f shift_left(const Sk4f& x, int bits) { |
181 | return x * (1 << bits); |
182 | } |
183 | |
184 | // |
185 | // To produce each mip level, we need to filter down by 1/2 (e.g. 100x100 -> 50,50) |
186 | // If the starting dimension is odd, we floor the size of the lower level (e.g. 101 -> 50) |
187 | // In those (odd) cases, we use a triangle filter, with 1-pixel overlap between samplings, |
188 | // else for even cases, we just use a 2x box filter. |
189 | // |
190 | // This produces 4 possible isotropic filters: 2x2 2x3 3x2 3x3 where WxH indicates the number of |
191 | // src pixels we need to sample in each dimension to produce 1 dst pixel. |
192 | // |
193 | // OpenGL expects a full mipmap stack to contain anisotropic space as well. |
194 | // This means a 100x1 image would continue down to a 50x1 image, 25x1 image... |
195 | // Because of this, we need 4 more anisotropic filters: 1x2, 1x3, 2x1, 3x1. |
196 | |
197 | template <typename F> void downsample_1_2(void* dst, const void* src, size_t srcRB, int count) { |
198 | SkASSERT(count > 0); |
199 | auto p0 = static_cast<const typename F::Type*>(src); |
200 | auto p1 = (const typename F::Type*)((const char*)p0 + srcRB); |
201 | auto d = static_cast<typename F::Type*>(dst); |
202 | |
203 | for (int i = 0; i < count; ++i) { |
204 | auto c00 = F::Expand(p0[0]); |
205 | auto c10 = F::Expand(p1[0]); |
206 | |
207 | auto c = c00 + c10; |
208 | d[i] = F::Compact(shift_right(c, 1)); |
209 | p0 += 2; |
210 | p1 += 2; |
211 | } |
212 | } |
213 | |
214 | template <typename F> void downsample_1_3(void* dst, const void* src, size_t srcRB, int count) { |
215 | SkASSERT(count > 0); |
216 | auto p0 = static_cast<const typename F::Type*>(src); |
217 | auto p1 = (const typename F::Type*)((const char*)p0 + srcRB); |
218 | auto p2 = (const typename F::Type*)((const char*)p1 + srcRB); |
219 | auto d = static_cast<typename F::Type*>(dst); |
220 | |
221 | for (int i = 0; i < count; ++i) { |
222 | auto c00 = F::Expand(p0[0]); |
223 | auto c10 = F::Expand(p1[0]); |
224 | auto c20 = F::Expand(p2[0]); |
225 | |
226 | auto c = add_121(c00, c10, c20); |
227 | d[i] = F::Compact(shift_right(c, 2)); |
228 | p0 += 2; |
229 | p1 += 2; |
230 | p2 += 2; |
231 | } |
232 | } |
233 | |
234 | template <typename F> void downsample_2_1(void* dst, const void* src, size_t srcRB, int count) { |
235 | SkASSERT(count > 0); |
236 | auto p0 = static_cast<const typename F::Type*>(src); |
237 | auto d = static_cast<typename F::Type*>(dst); |
238 | |
239 | for (int i = 0; i < count; ++i) { |
240 | auto c00 = F::Expand(p0[0]); |
241 | auto c01 = F::Expand(p0[1]); |
242 | |
243 | auto c = c00 + c01; |
244 | d[i] = F::Compact(shift_right(c, 1)); |
245 | p0 += 2; |
246 | } |
247 | } |
248 | |
249 | template <typename F> void downsample_2_2(void* dst, const void* src, size_t srcRB, int count) { |
250 | SkASSERT(count > 0); |
251 | auto p0 = static_cast<const typename F::Type*>(src); |
252 | auto p1 = (const typename F::Type*)((const char*)p0 + srcRB); |
253 | auto d = static_cast<typename F::Type*>(dst); |
254 | |
255 | for (int i = 0; i < count; ++i) { |
256 | auto c00 = F::Expand(p0[0]); |
257 | auto c01 = F::Expand(p0[1]); |
258 | auto c10 = F::Expand(p1[0]); |
259 | auto c11 = F::Expand(p1[1]); |
260 | |
261 | auto c = c00 + c10 + c01 + c11; |
262 | d[i] = F::Compact(shift_right(c, 2)); |
263 | p0 += 2; |
264 | p1 += 2; |
265 | } |
266 | } |
267 | |
268 | template <typename F> void downsample_2_3(void* dst, const void* src, size_t srcRB, int count) { |
269 | SkASSERT(count > 0); |
270 | auto p0 = static_cast<const typename F::Type*>(src); |
271 | auto p1 = (const typename F::Type*)((const char*)p0 + srcRB); |
272 | auto p2 = (const typename F::Type*)((const char*)p1 + srcRB); |
273 | auto d = static_cast<typename F::Type*>(dst); |
274 | |
275 | for (int i = 0; i < count; ++i) { |
276 | auto c00 = F::Expand(p0[0]); |
277 | auto c01 = F::Expand(p0[1]); |
278 | auto c10 = F::Expand(p1[0]); |
279 | auto c11 = F::Expand(p1[1]); |
280 | auto c20 = F::Expand(p2[0]); |
281 | auto c21 = F::Expand(p2[1]); |
282 | |
283 | auto c = add_121(c00, c10, c20) + add_121(c01, c11, c21); |
284 | d[i] = F::Compact(shift_right(c, 3)); |
285 | p0 += 2; |
286 | p1 += 2; |
287 | p2 += 2; |
288 | } |
289 | } |
290 | |
291 | template <typename F> void downsample_3_1(void* dst, const void* src, size_t srcRB, int count) { |
292 | SkASSERT(count > 0); |
293 | auto p0 = static_cast<const typename F::Type*>(src); |
294 | auto d = static_cast<typename F::Type*>(dst); |
295 | |
296 | auto c02 = F::Expand(p0[0]); |
297 | for (int i = 0; i < count; ++i) { |
298 | auto c00 = c02; |
299 | auto c01 = F::Expand(p0[1]); |
300 | c02 = F::Expand(p0[2]); |
301 | |
302 | auto c = add_121(c00, c01, c02); |
303 | d[i] = F::Compact(shift_right(c, 2)); |
304 | p0 += 2; |
305 | } |
306 | } |
307 | |
308 | template <typename F> void downsample_3_2(void* dst, const void* src, size_t srcRB, int count) { |
309 | SkASSERT(count > 0); |
310 | auto p0 = static_cast<const typename F::Type*>(src); |
311 | auto p1 = (const typename F::Type*)((const char*)p0 + srcRB); |
312 | auto d = static_cast<typename F::Type*>(dst); |
313 | |
314 | // Given pixels: |
315 | // a0 b0 c0 d0 e0 ... |
316 | // a1 b1 c1 d1 e1 ... |
317 | // We want: |
318 | // (a0 + 2*b0 + c0 + a1 + 2*b1 + c1) / 8 |
319 | // (c0 + 2*d0 + e0 + c1 + 2*d1 + e1) / 8 |
320 | // ... |
321 | |
322 | auto c0 = F::Expand(p0[0]); |
323 | auto c1 = F::Expand(p1[0]); |
324 | auto c = c0 + c1; |
325 | for (int i = 0; i < count; ++i) { |
326 | auto a = c; |
327 | |
328 | auto b0 = F::Expand(p0[1]); |
329 | auto b1 = F::Expand(p1[1]); |
330 | auto b = b0 + b0 + b1 + b1; |
331 | |
332 | c0 = F::Expand(p0[2]); |
333 | c1 = F::Expand(p1[2]); |
334 | c = c0 + c1; |
335 | |
336 | auto sum = a + b + c; |
337 | d[i] = F::Compact(shift_right(sum, 3)); |
338 | p0 += 2; |
339 | p1 += 2; |
340 | } |
341 | } |
342 | |
343 | template <typename F> void downsample_3_3(void* dst, const void* src, size_t srcRB, int count) { |
344 | SkASSERT(count > 0); |
345 | auto p0 = static_cast<const typename F::Type*>(src); |
346 | auto p1 = (const typename F::Type*)((const char*)p0 + srcRB); |
347 | auto p2 = (const typename F::Type*)((const char*)p1 + srcRB); |
348 | auto d = static_cast<typename F::Type*>(dst); |
349 | |
350 | // Given pixels: |
351 | // a0 b0 c0 d0 e0 ... |
352 | // a1 b1 c1 d1 e1 ... |
353 | // a2 b2 c2 d2 e2 ... |
354 | // We want: |
355 | // (a0 + 2*b0 + c0 + 2*a1 + 4*b1 + 2*c1 + a2 + 2*b2 + c2) / 16 |
356 | // (c0 + 2*d0 + e0 + 2*c1 + 4*d1 + 2*e1 + c2 + 2*d2 + e2) / 16 |
357 | // ... |
358 | |
359 | auto c0 = F::Expand(p0[0]); |
360 | auto c1 = F::Expand(p1[0]); |
361 | auto c2 = F::Expand(p2[0]); |
362 | auto c = add_121(c0, c1, c2); |
363 | for (int i = 0; i < count; ++i) { |
364 | auto a = c; |
365 | |
366 | auto b0 = F::Expand(p0[1]); |
367 | auto b1 = F::Expand(p1[1]); |
368 | auto b2 = F::Expand(p2[1]); |
369 | auto b = shift_left(add_121(b0, b1, b2), 1); |
370 | |
371 | c0 = F::Expand(p0[2]); |
372 | c1 = F::Expand(p1[2]); |
373 | c2 = F::Expand(p2[2]); |
374 | c = add_121(c0, c1, c2); |
375 | |
376 | auto sum = a + b + c; |
377 | d[i] = F::Compact(shift_right(sum, 4)); |
378 | p0 += 2; |
379 | p1 += 2; |
380 | p2 += 2; |
381 | } |
382 | } |
383 | |
384 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
385 | |
386 | size_t SkMipmap::AllocLevelsSize(int levelCount, size_t pixelSize) { |
387 | if (levelCount < 0) { |
388 | return 0; |
389 | } |
390 | int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize; |
391 | if (!SkTFitsIn<int32_t>(size)) { |
392 | return 0; |
393 | } |
394 | return SkTo<int32_t>(size); |
395 | } |
396 | |
397 | SkMipmap* SkMipmap::Build(const SkPixmap& src, SkDiscardableFactoryProc fact, |
398 | bool computeContents) { |
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 | if (computeContents) { |
636 | const void* srcBasePtr = srcPM.addr(); |
637 | void* dstBasePtr = dstPM.writable_addr(); |
638 | |
639 | const size_t srcRB = srcPM.rowBytes(); |
640 | for (int y = 0; y < height; y++) { |
641 | proc(dstBasePtr, srcBasePtr, srcRB, width); |
642 | srcBasePtr = (char*)srcBasePtr + srcRB * 2; // jump two rows |
643 | dstBasePtr = (char*)dstBasePtr + dstPM.rowBytes(); |
644 | } |
645 | } |
646 | srcPM = dstPM; |
647 | addr += height * rowBytes; |
648 | } |
649 | SkASSERT(addr == baseAddr + size); |
650 | |
651 | SkASSERT(mipmap->fLevels); |
652 | return mipmap; |
653 | } |
654 | |
655 | int SkMipmap::ComputeLevelCount(int baseWidth, int baseHeight) { |
656 | if (baseWidth < 1 || baseHeight < 1) { |
657 | return 0; |
658 | } |
659 | |
660 | // OpenGL's spec requires that each mipmap level have height/width equal to |
661 | // max(1, floor(original_height / 2^i) |
662 | // (or original_width) where i is the mipmap level. |
663 | // Continue scaling down until both axes are size 1. |
664 | |
665 | const int largestAxis = std::max(baseWidth, baseHeight); |
666 | if (largestAxis < 2) { |
667 | // SkMipmap::Build requires a minimum size of 2. |
668 | return 0; |
669 | } |
670 | const int leadingZeros = SkCLZ(static_cast<uint32_t>(largestAxis)); |
671 | // If the value 00011010 has 3 leading 0s then it has 5 significant bits |
672 | // (the bits which are not leading zeros) |
673 | const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros; |
674 | // This is making the assumption that the size of a byte is 8 bits |
675 | // and that sizeof(uint32_t)'s implementation-defined behavior is 4. |
676 | int mipLevelCount = significantBits; |
677 | |
678 | // SkMipmap does not include the base mip level. |
679 | // For example, it contains levels 1-x instead of 0-x. |
680 | // This is because the image used to create SkMipmap is the base level. |
681 | // So subtract 1 from the mip level count. |
682 | if (mipLevelCount > 0) { |
683 | --mipLevelCount; |
684 | } |
685 | |
686 | return mipLevelCount; |
687 | } |
688 | |
689 | SkISize SkMipmap::ComputeLevelSize(int baseWidth, int baseHeight, int level) { |
690 | if (baseWidth < 1 || baseHeight < 1) { |
691 | return SkISize::Make(0, 0); |
692 | } |
693 | |
694 | int maxLevelCount = ComputeLevelCount(baseWidth, baseHeight); |
695 | if (level >= maxLevelCount || level < 0) { |
696 | return SkISize::Make(0, 0); |
697 | } |
698 | // OpenGL's spec requires that each mipmap level have height/width equal to |
699 | // max(1, floor(original_height / 2^i) |
700 | // (or original_width) where i is the mipmap level. |
701 | |
702 | // SkMipmap does not include the base mip level. |
703 | // For example, it contains levels 1-x instead of 0-x. |
704 | // This is because the image used to create SkMipmap is the base level. |
705 | // So subtract 1 from the mip level to get the index stored by SkMipmap. |
706 | int width = std::max(1, baseWidth >> (level + 1)); |
707 | int height = std::max(1, baseHeight >> (level + 1)); |
708 | |
709 | return SkISize::Make(width, height); |
710 | } |
711 | |
712 | /////////////////////////////////////////////////////////////////////////////// |
713 | |
714 | // Returns fractional level value. floor(level) is the index of the larger level. |
715 | // < 0 means failure. |
716 | float SkMipmap::ComputeLevel(SkSize scaleSize) { |
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 -1; |
731 | } |
732 | |
733 | SkScalar L = -SkScalarLog2(scale); |
734 | if (!SkScalarIsFinite(L)) { |
735 | return -1; |
736 | } |
737 | SkASSERT(L >= 0); |
738 | return L; |
739 | } |
740 | |
741 | bool SkMipmap::(SkSize scaleSize, Level* levelPtr) const { |
742 | if (nullptr == fLevels) { |
743 | return false; |
744 | } |
745 | |
746 | float L = ComputeLevel(scaleSize); |
747 | int level = SkScalarFloorToInt(L); |
748 | if (level <= 0) { |
749 | return false; |
750 | } |
751 | |
752 | if (level > fCount) { |
753 | level = fCount; |
754 | } |
755 | if (levelPtr) { |
756 | *levelPtr = fLevels[level - 1]; |
757 | // need to augment with our colorspace |
758 | levelPtr->fPixmap.setColorSpace(fCS); |
759 | } |
760 | return true; |
761 | } |
762 | |
763 | bool SkMipmap::validForRootLevel(const SkImageInfo& root) const { |
764 | if (nullptr == fLevels) { |
765 | return false; |
766 | } |
767 | |
768 | const SkISize dimension = root.dimensions(); |
769 | if (dimension.width() <= 1 && dimension.height() <= 1) { |
770 | return false; |
771 | } |
772 | |
773 | const SkPixmap& pm = fLevels[0].fPixmap; |
774 | if (pm. width() != std::max(1, dimension. width() >> 1) || |
775 | pm.height() != std::max(1, dimension.height() >> 1)) { |
776 | return false; |
777 | } |
778 | |
779 | for (int i = 0; i < this->countLevels(); ++i) { |
780 | const SkPixmap& pm = fLevels[0].fPixmap; |
781 | if (pm.colorType() != root.colorType() || |
782 | pm.alphaType() != root.alphaType()) |
783 | return false; |
784 | } |
785 | return true; |
786 | } |
787 | |
788 | // Helper which extracts a pixmap from the src bitmap |
789 | // |
790 | SkMipmap* SkMipmap::Build(const SkBitmap& src, SkDiscardableFactoryProc fact) { |
791 | SkPixmap srcPixmap; |
792 | if (!src.peekPixels(&srcPixmap)) { |
793 | return nullptr; |
794 | } |
795 | return Build(srcPixmap, fact); |
796 | } |
797 | |
798 | int SkMipmap::countLevels() const { |
799 | return fCount; |
800 | } |
801 | |
802 | bool SkMipmap::getLevel(int index, Level* levelPtr) const { |
803 | if (nullptr == fLevels) { |
804 | return false; |
805 | } |
806 | if (index < 0) { |
807 | return false; |
808 | } |
809 | if (index > fCount - 1) { |
810 | return false; |
811 | } |
812 | if (levelPtr) { |
813 | *levelPtr = fLevels[index]; |
814 | // need to augment with our colorspace |
815 | levelPtr->fPixmap.setColorSpace(fCS); |
816 | } |
817 | return true; |
818 | } |
819 | |
820 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
821 | |
822 | #include "include/core/SkImageGenerator.h" |
823 | #include "include/core/SkStream.h" |
824 | #include "include/encode/SkPngEncoder.h" |
825 | #include "src/core/SkReadBuffer.h" |
826 | #include "src/core/SkWriteBuffer.h" |
827 | |
828 | static sk_sp<SkData> encode_to_data(const SkPixmap& pm) { |
829 | SkDynamicMemoryWStream stream; |
830 | if (SkPngEncoder::Encode(&stream, pm, SkPngEncoder::Options())) { |
831 | return stream.detachAsData(); |
832 | } |
833 | return nullptr; |
834 | } |
835 | |
836 | /* Format |
837 | count_levels:32 |
838 | for each level, starting with the biggest (index 0 in our iterator) |
839 | encoded_size:32 |
840 | encoded_data (padded) |
841 | */ |
842 | sk_sp<SkData> SkMipmap::serialize() const { |
843 | const int count = this->countLevels(); |
844 | |
845 | SkBinaryWriteBuffer buffer; |
846 | buffer.write32(count); |
847 | for (int i = 0; i < count; ++i) { |
848 | Level level; |
849 | if (this->getLevel(i, &level)) { |
850 | buffer.writeDataAsByteArray(encode_to_data(level.fPixmap).get()); |
851 | } else { |
852 | return nullptr; |
853 | } |
854 | } |
855 | return buffer.snapshotAsData(); |
856 | } |
857 | |
858 | bool SkMipmap::Deserialize(SkMipmapBuilder* builder, const void* data, size_t size) { |
859 | SkReadBuffer buffer(data, size); |
860 | |
861 | int count = buffer.read32(); |
862 | if (builder->countLevels() != count) { |
863 | return false; |
864 | } |
865 | for (int i = 0; i < count; ++i) { |
866 | size_t size = buffer.read32(); |
867 | const void* ptr = buffer.skip(size); |
868 | if (!ptr) { |
869 | return false; |
870 | } |
871 | auto gen = SkImageGenerator::MakeFromEncoded( |
872 | SkData::MakeWithProc(ptr, size, nullptr, nullptr)); |
873 | if (!gen) { |
874 | return false; |
875 | } |
876 | |
877 | SkPixmap pm = builder->level(i); |
878 | if (gen->getInfo().dimensions() != pm.dimensions()) { |
879 | return false; |
880 | } |
881 | if (!gen->getPixels(pm)) { |
882 | return false; |
883 | } |
884 | } |
885 | return buffer.isValid(); |
886 | } |
887 | |