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