• 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/SkHalf.h"
12 #include "include/private/SkImageInfoPriv.h"
13 #include "include/private/SkNx.h"
14 #include "include/private/SkTo.h"
15 #include "include/private/SkVx.h"
16 #include "src/core/SkMathPriv.h"
17 #include "src/core/SkMipmap.h"
18 #include "src/core/SkMipmapBuilder.h"
19 #include <new>
20 
21 //
22 // ColorTypeFilter is the "Type" we pass to some downsample template functions.
23 // It controls how we expand a pixel into a large type, with space between each component,
24 // so we can then perform our simple filter (either box or triangle) and store the intermediates
25 // in the expanded type.
26 //
27 
28 struct ColorTypeFilter_8888 {
29     typedef uint32_t Type;
ExpandColorTypeFilter_888830     static Sk4h Expand(uint32_t x) {
31         return SkNx_cast<uint16_t>(Sk4b::Load(&x));
32     }
CompactColorTypeFilter_888833     static uint32_t Compact(const Sk4h& x) {
34         uint32_t r;
35         SkNx_cast<uint8_t>(x).store(&r);
36         return r;
37     }
38 };
39 
40 struct ColorTypeFilter_565 {
41     typedef uint16_t Type;
ExpandColorTypeFilter_56542     static uint32_t Expand(uint16_t x) {
43         return (x & ~SK_G16_MASK_IN_PLACE) | ((x & SK_G16_MASK_IN_PLACE) << 16);
44     }
CompactColorTypeFilter_56545     static uint16_t Compact(uint32_t x) {
46         return ((x & ~SK_G16_MASK_IN_PLACE) & 0xFFFF) | ((x >> 16) & SK_G16_MASK_IN_PLACE);
47     }
48 };
49 
50 struct ColorTypeFilter_4444 {
51     typedef uint16_t Type;
ExpandColorTypeFilter_444452     static uint32_t Expand(uint16_t x) {
53         return (x & 0xF0F) | ((x & ~0xF0F) << 12);
54     }
CompactColorTypeFilter_444455     static uint16_t Compact(uint32_t x) {
56         return (x & 0xF0F) | ((x >> 12) & ~0xF0F);
57     }
58 };
59 
60 struct ColorTypeFilter_8 {
61     typedef uint8_t Type;
ExpandColorTypeFilter_862     static unsigned Expand(unsigned x) {
63         return x;
64     }
CompactColorTypeFilter_865     static uint8_t Compact(unsigned x) {
66         return (uint8_t)x;
67     }
68 };
69 
70 struct ColorTypeFilter_Alpha_F16 {
71     typedef uint16_t Type;
ExpandColorTypeFilter_Alpha_F1672     static Sk4f Expand(uint16_t x) {
73         return SkHalfToFloat_finite_ftz((uint64_t) x); // expand out to four lanes
74 
75     }
CompactColorTypeFilter_Alpha_F1676     static uint16_t Compact(const Sk4f& x) {
77         uint64_t r;
78         SkFloatToHalf_finite_ftz(x).store(&r);
79         return r & 0xFFFF;  // but ignore the extra 3 here
80     }
81 };
82 
83 struct ColorTypeFilter_RGBA_F16 {
84     typedef uint64_t Type; // SkHalf x4
ExpandColorTypeFilter_RGBA_F1685     static Sk4f Expand(uint64_t x) {
86         return SkHalfToFloat_finite_ftz(x);
87     }
CompactColorTypeFilter_RGBA_F1688     static uint64_t Compact(const Sk4f& x) {
89         uint64_t r;
90         SkFloatToHalf_finite_ftz(x).store(&r);
91         return r;
92     }
93 };
94 
95 struct ColorTypeFilter_88 {
96     typedef uint16_t Type;
ExpandColorTypeFilter_8897     static uint32_t Expand(uint16_t x) {
98         return (x & 0xFF) | ((x & ~0xFF) << 8);
99     }
CompactColorTypeFilter_88100     static uint16_t Compact(uint32_t x) {
101         return (x & 0xFF) | ((x >> 8) & ~0xFF);
102     }
103 };
104 
105 struct ColorTypeFilter_1616 {
106     typedef uint32_t Type;
ExpandColorTypeFilter_1616107     static uint64_t Expand(uint32_t x) {
108         return (x & 0xFFFF) | ((x & ~0xFFFF) << 16);
109     }
CompactColorTypeFilter_1616110     static uint16_t Compact(uint64_t x) {
111         return (x & 0xFFFF) | ((x >> 16) & ~0xFFFF);
112     }
113 };
114 
115 struct ColorTypeFilter_F16F16 {
116     typedef uint32_t Type;
ExpandColorTypeFilter_F16F16117     static Sk4f Expand(uint32_t x) {
118         return SkHalfToFloat_finite_ftz((uint64_t) x); // expand out to four lanes
119     }
CompactColorTypeFilter_F16F16120     static uint32_t Compact(const Sk4f& x) {
121         uint64_t r;
122         SkFloatToHalf_finite_ftz(x).store(&r);
123         return (uint32_t) (r & 0xFFFFFFFF);  // but ignore the extra 2 here
124     }
125 };
126 
127 struct ColorTypeFilter_16161616 {
128     typedef uint64_t Type;
ExpandColorTypeFilter_16161616129     static skvx::Vec<4, uint32_t> Expand(uint64_t x) {
130         return skvx::cast<uint32_t>(skvx::Vec<4, uint16_t>::Load(&x));
131     }
CompactColorTypeFilter_16161616132     static uint64_t Compact(const skvx::Vec<4, uint32_t>& x) {
133         uint64_t r;
134         skvx::cast<uint16_t>(x).store(&r);
135         return r;
136     }
137 };
138 
139 struct ColorTypeFilter_16 {
140     typedef uint16_t Type;
ExpandColorTypeFilter_16141     static uint32_t Expand(uint16_t x) {
142         return x;
143     }
CompactColorTypeFilter_16144     static uint16_t Compact(uint32_t x) {
145         return (uint16_t) x;
146     }
147 };
148 
149 struct ColorTypeFilter_1010102 {
150     typedef uint32_t Type;
ExpandColorTypeFilter_1010102151     static uint64_t Expand(uint64_t x) {
152         return (((x      ) & 0x3ff)      ) |
153                (((x >> 10) & 0x3ff) << 20) |
154                (((x >> 20) & 0x3ff) << 40) |
155                (((x >> 30) & 0x3  ) << 60);
156     }
CompactColorTypeFilter_1010102157     static uint32_t Compact(uint64_t x) {
158         return (((x      ) & 0x3ff)      ) |
159                (((x >> 20) & 0x3ff) << 10) |
160                (((x >> 40) & 0x3ff) << 20) |
161                (((x >> 60) & 0x3  ) << 30);
162     }
163 };
164 
add_121(const T & a,const T & b,const T & c)165 template <typename T> T add_121(const T& a, const T& b, const T& c) {
166     return a + b + b + c;
167 }
168 
shift_right(const T & x,int bits)169 template <typename T> T shift_right(const T& x, int bits) {
170     return x >> bits;
171 }
172 
shift_right(const Sk4f & x,int bits)173 Sk4f shift_right(const Sk4f& x, int bits) {
174     return x * (1.0f / (1 << bits));
175 }
176 
shift_left(const T & x,int bits)177 template <typename T> T shift_left(const T& x, int bits) {
178     return x << bits;
179 }
180 
shift_left(const Sk4f & x,int bits)181 Sk4f shift_left(const Sk4f& x, int bits) {
182     return x * (1 << bits);
183 }
184 
185 //
186 //  To produce each mip level, we need to filter down by 1/2 (e.g. 100x100 -> 50,50)
187 //  If the starting dimension is odd, we floor the size of the lower level (e.g. 101 -> 50)
188 //  In those (odd) cases, we use a triangle filter, with 1-pixel overlap between samplings,
189 //  else for even cases, we just use a 2x box filter.
190 //
191 //  This produces 4 possible isotropic filters: 2x2 2x3 3x2 3x3 where WxH indicates the number of
192 //  src pixels we need to sample in each dimension to produce 1 dst pixel.
193 //
194 //  OpenGL expects a full mipmap stack to contain anisotropic space as well.
195 //  This means a 100x1 image would continue down to a 50x1 image, 25x1 image...
196 //  Because of this, we need 4 more anisotropic filters: 1x2, 1x3, 2x1, 3x1.
197 
downsample_1_2(void * dst,const void * src,size_t srcRB,int count)198 template <typename F> void downsample_1_2(void* dst, const void* src, size_t srcRB, int count) {
199     SkASSERT(count > 0);
200     auto p0 = static_cast<const typename F::Type*>(src);
201     auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
202     auto d = static_cast<typename F::Type*>(dst);
203 
204     for (int i = 0; i < count; ++i) {
205         auto c00 = F::Expand(p0[0]);
206         auto c10 = F::Expand(p1[0]);
207 
208         auto c = c00 + c10;
209         d[i] = F::Compact(shift_right(c, 1));
210         p0 += 2;
211         p1 += 2;
212     }
213 }
214 
downsample_1_3(void * dst,const void * src,size_t srcRB,int count)215 template <typename F> void downsample_1_3(void* dst, const void* src, size_t srcRB, int count) {
216     SkASSERT(count > 0);
217     auto p0 = static_cast<const typename F::Type*>(src);
218     auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
219     auto p2 = (const typename F::Type*)((const char*)p1 + srcRB);
220     auto d = static_cast<typename F::Type*>(dst);
221 
222     for (int i = 0; i < count; ++i) {
223         auto c00 = F::Expand(p0[0]);
224         auto c10 = F::Expand(p1[0]);
225         auto c20 = F::Expand(p2[0]);
226 
227         auto c = add_121(c00, c10, c20);
228         d[i] = F::Compact(shift_right(c, 2));
229         p0 += 2;
230         p1 += 2;
231         p2 += 2;
232     }
233 }
234 
downsample_2_1(void * dst,const void * src,size_t srcRB,int count)235 template <typename F> void downsample_2_1(void* dst, const void* src, size_t srcRB, int count) {
236     SkASSERT(count > 0);
237     auto p0 = static_cast<const typename F::Type*>(src);
238     auto d = static_cast<typename F::Type*>(dst);
239 
240     for (int i = 0; i < count; ++i) {
241         auto c00 = F::Expand(p0[0]);
242         auto c01 = F::Expand(p0[1]);
243 
244         auto c = c00 + c01;
245         d[i] = F::Compact(shift_right(c, 1));
246         p0 += 2;
247     }
248 }
249 
downsample_2_2(void * dst,const void * src,size_t srcRB,int count)250 template <typename F> void downsample_2_2(void* dst, const void* src, size_t srcRB, int count) {
251     SkASSERT(count > 0);
252     auto p0 = static_cast<const typename F::Type*>(src);
253     auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
254     auto d = static_cast<typename F::Type*>(dst);
255 
256     for (int i = 0; i < count; ++i) {
257         auto c00 = F::Expand(p0[0]);
258         auto c01 = F::Expand(p0[1]);
259         auto c10 = F::Expand(p1[0]);
260         auto c11 = F::Expand(p1[1]);
261 
262         auto c = c00 + c10 + c01 + c11;
263         d[i] = F::Compact(shift_right(c, 2));
264         p0 += 2;
265         p1 += 2;
266     }
267 }
268 
downsample_2_3(void * dst,const void * src,size_t srcRB,int count)269 template <typename F> void downsample_2_3(void* dst, const void* src, size_t srcRB, int count) {
270     SkASSERT(count > 0);
271     auto p0 = static_cast<const typename F::Type*>(src);
272     auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
273     auto p2 = (const typename F::Type*)((const char*)p1 + srcRB);
274     auto d = static_cast<typename F::Type*>(dst);
275 
276     for (int i = 0; i < count; ++i) {
277         auto c00 = F::Expand(p0[0]);
278         auto c01 = F::Expand(p0[1]);
279         auto c10 = F::Expand(p1[0]);
280         auto c11 = F::Expand(p1[1]);
281         auto c20 = F::Expand(p2[0]);
282         auto c21 = F::Expand(p2[1]);
283 
284         auto c = add_121(c00, c10, c20) + add_121(c01, c11, c21);
285         d[i] = F::Compact(shift_right(c, 3));
286         p0 += 2;
287         p1 += 2;
288         p2 += 2;
289     }
290 }
291 
downsample_3_1(void * dst,const void * src,size_t srcRB,int count)292 template <typename F> void downsample_3_1(void* dst, const void* src, size_t srcRB, int count) {
293     SkASSERT(count > 0);
294     auto p0 = static_cast<const typename F::Type*>(src);
295     auto d = static_cast<typename F::Type*>(dst);
296 
297     auto c02 = F::Expand(p0[0]);
298     for (int i = 0; i < count; ++i) {
299         auto c00 = c02;
300         auto c01 = F::Expand(p0[1]);
301              c02 = F::Expand(p0[2]);
302 
303         auto c = add_121(c00, c01, c02);
304         d[i] = F::Compact(shift_right(c, 2));
305         p0 += 2;
306     }
307 }
308 
downsample_3_2(void * dst,const void * src,size_t srcRB,int count)309 template <typename F> void downsample_3_2(void* dst, const void* src, size_t srcRB, int count) {
310     SkASSERT(count > 0);
311     auto p0 = static_cast<const typename F::Type*>(src);
312     auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
313     auto d = static_cast<typename F::Type*>(dst);
314 
315     // Given pixels:
316     // a0 b0 c0 d0 e0 ...
317     // a1 b1 c1 d1 e1 ...
318     // We want:
319     // (a0 + 2*b0 + c0 + a1 + 2*b1 + c1) / 8
320     // (c0 + 2*d0 + e0 + c1 + 2*d1 + e1) / 8
321     // ...
322 
323     auto c0 = F::Expand(p0[0]);
324     auto c1 = F::Expand(p1[0]);
325     auto c = c0 + c1;
326     for (int i = 0; i < count; ++i) {
327         auto a = c;
328 
329         auto b0 = F::Expand(p0[1]);
330         auto b1 = F::Expand(p1[1]);
331         auto b = b0 + b0 + b1 + b1;
332 
333         c0 = F::Expand(p0[2]);
334         c1 = F::Expand(p1[2]);
335         c = c0 + c1;
336 
337         auto sum = a + b + c;
338         d[i] = F::Compact(shift_right(sum, 3));
339         p0 += 2;
340         p1 += 2;
341     }
342 }
343 
downsample_3_3(void * dst,const void * src,size_t srcRB,int count)344 template <typename F> void downsample_3_3(void* dst, const void* src, size_t srcRB, int count) {
345     SkASSERT(count > 0);
346     auto p0 = static_cast<const typename F::Type*>(src);
347     auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
348     auto p2 = (const typename F::Type*)((const char*)p1 + srcRB);
349     auto d = static_cast<typename F::Type*>(dst);
350 
351     // Given pixels:
352     // a0 b0 c0 d0 e0 ...
353     // a1 b1 c1 d1 e1 ...
354     // a2 b2 c2 d2 e2 ...
355     // We want:
356     // (a0 + 2*b0 + c0 + 2*a1 + 4*b1 + 2*c1 + a2 + 2*b2 + c2) / 16
357     // (c0 + 2*d0 + e0 + 2*c1 + 4*d1 + 2*e1 + c2 + 2*d2 + e2) / 16
358     // ...
359 
360     auto c0 = F::Expand(p0[0]);
361     auto c1 = F::Expand(p1[0]);
362     auto c2 = F::Expand(p2[0]);
363     auto c = add_121(c0, c1, c2);
364     for (int i = 0; i < count; ++i) {
365         auto a = c;
366 
367         auto b0 = F::Expand(p0[1]);
368         auto b1 = F::Expand(p1[1]);
369         auto b2 = F::Expand(p2[1]);
370         auto b = shift_left(add_121(b0, b1, b2), 1);
371 
372         c0 = F::Expand(p0[2]);
373         c1 = F::Expand(p1[2]);
374         c2 = F::Expand(p2[2]);
375         c = add_121(c0, c1, c2);
376 
377         auto sum = a + b + c;
378         d[i] = F::Compact(shift_right(sum, 4));
379         p0 += 2;
380         p1 += 2;
381         p2 += 2;
382     }
383 }
384 
385 ///////////////////////////////////////////////////////////////////////////////////////////////////
386 
AllocLevelsSize(int levelCount,size_t pixelSize)387 size_t SkMipmap::AllocLevelsSize(int levelCount, size_t pixelSize) {
388     if (levelCount < 0) {
389         return 0;
390     }
391     int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize;
392     if (!SkTFitsIn<int32_t>(size)) {
393         return 0;
394     }
395     return SkTo<int32_t>(size);
396 }
397 
Build(const SkPixmap & src,SkDiscardableFactoryProc fact,bool computeContents)398 SkMipmap* SkMipmap::Build(const SkPixmap& src, SkDiscardableFactoryProc fact,
399                           bool computeContents) {
400     typedef void FilterProc(void*, const void* srcPtr, size_t srcRB, int count);
401 
402     FilterProc* proc_1_2 = nullptr;
403     FilterProc* proc_1_3 = nullptr;
404     FilterProc* proc_2_1 = nullptr;
405     FilterProc* proc_2_2 = nullptr;
406     FilterProc* proc_2_3 = nullptr;
407     FilterProc* proc_3_1 = nullptr;
408     FilterProc* proc_3_2 = nullptr;
409     FilterProc* proc_3_3 = nullptr;
410 
411     const SkColorType ct = src.colorType();
412     const SkAlphaType at = src.alphaType();
413 
414     switch (ct) {
415         case kRGBA_8888_SkColorType:
416         case kBGRA_8888_SkColorType:
417             proc_1_2 = downsample_1_2<ColorTypeFilter_8888>;
418             proc_1_3 = downsample_1_3<ColorTypeFilter_8888>;
419             proc_2_1 = downsample_2_1<ColorTypeFilter_8888>;
420             proc_2_2 = downsample_2_2<ColorTypeFilter_8888>;
421             proc_2_3 = downsample_2_3<ColorTypeFilter_8888>;
422             proc_3_1 = downsample_3_1<ColorTypeFilter_8888>;
423             proc_3_2 = downsample_3_2<ColorTypeFilter_8888>;
424             proc_3_3 = downsample_3_3<ColorTypeFilter_8888>;
425             break;
426         case kRGB_565_SkColorType:
427             proc_1_2 = downsample_1_2<ColorTypeFilter_565>;
428             proc_1_3 = downsample_1_3<ColorTypeFilter_565>;
429             proc_2_1 = downsample_2_1<ColorTypeFilter_565>;
430             proc_2_2 = downsample_2_2<ColorTypeFilter_565>;
431             proc_2_3 = downsample_2_3<ColorTypeFilter_565>;
432             proc_3_1 = downsample_3_1<ColorTypeFilter_565>;
433             proc_3_2 = downsample_3_2<ColorTypeFilter_565>;
434             proc_3_3 = downsample_3_3<ColorTypeFilter_565>;
435             break;
436         case kARGB_4444_SkColorType:
437             proc_1_2 = downsample_1_2<ColorTypeFilter_4444>;
438             proc_1_3 = downsample_1_3<ColorTypeFilter_4444>;
439             proc_2_1 = downsample_2_1<ColorTypeFilter_4444>;
440             proc_2_2 = downsample_2_2<ColorTypeFilter_4444>;
441             proc_2_3 = downsample_2_3<ColorTypeFilter_4444>;
442             proc_3_1 = downsample_3_1<ColorTypeFilter_4444>;
443             proc_3_2 = downsample_3_2<ColorTypeFilter_4444>;
444             proc_3_3 = downsample_3_3<ColorTypeFilter_4444>;
445             break;
446         case kAlpha_8_SkColorType:
447         case kGray_8_SkColorType:
448             proc_1_2 = downsample_1_2<ColorTypeFilter_8>;
449             proc_1_3 = downsample_1_3<ColorTypeFilter_8>;
450             proc_2_1 = downsample_2_1<ColorTypeFilter_8>;
451             proc_2_2 = downsample_2_2<ColorTypeFilter_8>;
452             proc_2_3 = downsample_2_3<ColorTypeFilter_8>;
453             proc_3_1 = downsample_3_1<ColorTypeFilter_8>;
454             proc_3_2 = downsample_3_2<ColorTypeFilter_8>;
455             proc_3_3 = downsample_3_3<ColorTypeFilter_8>;
456             break;
457         case kRGBA_F16Norm_SkColorType:
458         case kRGBA_F16_SkColorType:
459             proc_1_2 = downsample_1_2<ColorTypeFilter_RGBA_F16>;
460             proc_1_3 = downsample_1_3<ColorTypeFilter_RGBA_F16>;
461             proc_2_1 = downsample_2_1<ColorTypeFilter_RGBA_F16>;
462             proc_2_2 = downsample_2_2<ColorTypeFilter_RGBA_F16>;
463             proc_2_3 = downsample_2_3<ColorTypeFilter_RGBA_F16>;
464             proc_3_1 = downsample_3_1<ColorTypeFilter_RGBA_F16>;
465             proc_3_2 = downsample_3_2<ColorTypeFilter_RGBA_F16>;
466             proc_3_3 = downsample_3_3<ColorTypeFilter_RGBA_F16>;
467             break;
468         case kR8G8_unorm_SkColorType:
469             proc_1_2 = downsample_1_2<ColorTypeFilter_88>;
470             proc_1_3 = downsample_1_3<ColorTypeFilter_88>;
471             proc_2_1 = downsample_2_1<ColorTypeFilter_88>;
472             proc_2_2 = downsample_2_2<ColorTypeFilter_88>;
473             proc_2_3 = downsample_2_3<ColorTypeFilter_88>;
474             proc_3_1 = downsample_3_1<ColorTypeFilter_88>;
475             proc_3_2 = downsample_3_2<ColorTypeFilter_88>;
476             proc_3_3 = downsample_3_3<ColorTypeFilter_88>;
477             break;
478         case kR16G16_unorm_SkColorType:
479             proc_1_2 = downsample_1_2<ColorTypeFilter_1616>;
480             proc_1_3 = downsample_1_3<ColorTypeFilter_1616>;
481             proc_2_1 = downsample_2_1<ColorTypeFilter_1616>;
482             proc_2_2 = downsample_2_2<ColorTypeFilter_1616>;
483             proc_2_3 = downsample_2_3<ColorTypeFilter_1616>;
484             proc_3_1 = downsample_3_1<ColorTypeFilter_1616>;
485             proc_3_2 = downsample_3_2<ColorTypeFilter_1616>;
486             proc_3_3 = downsample_3_3<ColorTypeFilter_1616>;
487             break;
488         case kA16_unorm_SkColorType:
489             proc_1_2 = downsample_1_2<ColorTypeFilter_16>;
490             proc_1_3 = downsample_1_3<ColorTypeFilter_16>;
491             proc_2_1 = downsample_2_1<ColorTypeFilter_16>;
492             proc_2_2 = downsample_2_2<ColorTypeFilter_16>;
493             proc_2_3 = downsample_2_3<ColorTypeFilter_16>;
494             proc_3_1 = downsample_3_1<ColorTypeFilter_16>;
495             proc_3_2 = downsample_3_2<ColorTypeFilter_16>;
496             proc_3_3 = downsample_3_3<ColorTypeFilter_16>;
497             break;
498         case kRGBA_1010102_SkColorType:
499         case kBGRA_1010102_SkColorType:
500             proc_1_2 = downsample_1_2<ColorTypeFilter_1010102>;
501             proc_1_3 = downsample_1_3<ColorTypeFilter_1010102>;
502             proc_2_1 = downsample_2_1<ColorTypeFilter_1010102>;
503             proc_2_2 = downsample_2_2<ColorTypeFilter_1010102>;
504             proc_2_3 = downsample_2_3<ColorTypeFilter_1010102>;
505             proc_3_1 = downsample_3_1<ColorTypeFilter_1010102>;
506             proc_3_2 = downsample_3_2<ColorTypeFilter_1010102>;
507             proc_3_3 = downsample_3_3<ColorTypeFilter_1010102>;
508             break;
509         case kA16_float_SkColorType:
510             proc_1_2 = downsample_1_2<ColorTypeFilter_Alpha_F16>;
511             proc_1_3 = downsample_1_3<ColorTypeFilter_Alpha_F16>;
512             proc_2_1 = downsample_2_1<ColorTypeFilter_Alpha_F16>;
513             proc_2_2 = downsample_2_2<ColorTypeFilter_Alpha_F16>;
514             proc_2_3 = downsample_2_3<ColorTypeFilter_Alpha_F16>;
515             proc_3_1 = downsample_3_1<ColorTypeFilter_Alpha_F16>;
516             proc_3_2 = downsample_3_2<ColorTypeFilter_Alpha_F16>;
517             proc_3_3 = downsample_3_3<ColorTypeFilter_Alpha_F16>;
518             break;
519         case kR16G16_float_SkColorType:
520             proc_1_2 = downsample_1_2<ColorTypeFilter_F16F16>;
521             proc_1_3 = downsample_1_3<ColorTypeFilter_F16F16>;
522             proc_2_1 = downsample_2_1<ColorTypeFilter_F16F16>;
523             proc_2_2 = downsample_2_2<ColorTypeFilter_F16F16>;
524             proc_2_3 = downsample_2_3<ColorTypeFilter_F16F16>;
525             proc_3_1 = downsample_3_1<ColorTypeFilter_F16F16>;
526             proc_3_2 = downsample_3_2<ColorTypeFilter_F16F16>;
527             proc_3_3 = downsample_3_3<ColorTypeFilter_F16F16>;
528             break;
529         case kR16G16B16A16_unorm_SkColorType:
530             proc_1_2 = downsample_1_2<ColorTypeFilter_16161616>;
531             proc_1_3 = downsample_1_3<ColorTypeFilter_16161616>;
532             proc_2_1 = downsample_2_1<ColorTypeFilter_16161616>;
533             proc_2_2 = downsample_2_2<ColorTypeFilter_16161616>;
534             proc_2_3 = downsample_2_3<ColorTypeFilter_16161616>;
535             proc_3_1 = downsample_3_1<ColorTypeFilter_16161616>;
536             proc_3_2 = downsample_3_2<ColorTypeFilter_16161616>;
537             proc_3_3 = downsample_3_3<ColorTypeFilter_16161616>;
538             break;
539 
540         case kUnknown_SkColorType:
541         case kRGB_888x_SkColorType:     // TODO: use 8888?
542         case kRGB_101010x_SkColorType:  // TODO: use 1010102?
543         case kBGR_101010x_SkColorType:  // TODO: use 1010102?
544         case kRGBA_F32_SkColorType:
545             return nullptr;
546 
547         case kSRGBA_8888_SkColorType:  // TODO: needs careful handling
548             return nullptr;
549     }
550 
551     if (src.width() <= 1 && src.height() <= 1) {
552         return nullptr;
553     }
554     // whip through our loop to compute the exact size needed
555     size_t size = 0;
556     int countLevels = ComputeLevelCount(src.width(), src.height());
557     for (int currentMipLevel = countLevels; currentMipLevel >= 0; currentMipLevel--) {
558         SkISize mipSize = ComputeLevelSize(src.width(), src.height(), currentMipLevel);
559         size += SkColorTypeMinRowBytes(ct, mipSize.fWidth) * mipSize.fHeight;
560     }
561 
562     size_t storageSize = SkMipmap::AllocLevelsSize(countLevels, size);
563     if (0 == storageSize) {
564         return nullptr;
565     }
566 
567     SkMipmap* mipmap;
568     if (fact) {
569         SkDiscardableMemory* dm = fact(storageSize);
570         if (nullptr == dm) {
571             return nullptr;
572         }
573         mipmap = new SkMipmap(storageSize, dm);
574     } else {
575         mipmap = new SkMipmap(sk_malloc_throw(storageSize), storageSize);
576     }
577 
578     // init
579     mipmap->fCS = sk_ref_sp(src.info().colorSpace());
580     mipmap->fCount = countLevels;
581     mipmap->fLevels = (Level*)mipmap->writable_data();
582     SkASSERT(mipmap->fLevels);
583 
584     Level* levels = mipmap->fLevels;
585     uint8_t*    baseAddr = (uint8_t*)&levels[countLevels];
586     uint8_t*    addr = baseAddr;
587     int         width = src.width();
588     int         height = src.height();
589     uint32_t    rowBytes;
590     SkPixmap    srcPM(src);
591 
592     // Depending on architecture and other factors, the pixel data alignment may need to be as
593     // large as 8 (for F16 pixels). See the comment on SkMipmap::Level.
594     SkASSERT(SkIsAlign8((uintptr_t)addr));
595 
596     for (int i = 0; i < countLevels; ++i) {
597         FilterProc* proc;
598         if (height & 1) {
599             if (height == 1) {        // src-height is 1
600                 if (width & 1) {      // src-width is 3
601                     proc = proc_3_1;
602                 } else {              // src-width is 2
603                     proc = proc_2_1;
604                 }
605             } else {                  // src-height is 3
606                 if (width & 1) {
607                     if (width == 1) { // src-width is 1
608                         proc = proc_1_3;
609                     } else {          // src-width is 3
610                         proc = proc_3_3;
611                     }
612                 } else {              // src-width is 2
613                     proc = proc_2_3;
614                 }
615             }
616         } else {                      // src-height is 2
617             if (width & 1) {
618                 if (width == 1) {     // src-width is 1
619                     proc = proc_1_2;
620                 } else {              // src-width is 3
621                     proc = proc_3_2;
622                 }
623             } else {                  // src-width is 2
624                 proc = proc_2_2;
625             }
626         }
627         width = std::max(1, width >> 1);
628         height = std::max(1, height >> 1);
629         rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width));
630 
631         // We make the Info w/o any colorspace, since that storage is not under our control, and
632         // will not be deleted in a controlled fashion. When the caller is given the pixmap for
633         // a given level, we augment this pixmap with fCS (which we do manage).
634         new (&levels[i].fPixmap) SkPixmap(SkImageInfo::Make(width, height, ct, at), addr, rowBytes);
635         levels[i].fScale  = SkSize::Make(SkIntToScalar(width)  / src.width(),
636                                          SkIntToScalar(height) / src.height());
637 
638         const SkPixmap& dstPM = levels[i].fPixmap;
639         if (computeContents) {
640             const void* srcBasePtr = srcPM.addr();
641             void* dstBasePtr = dstPM.writable_addr();
642 
643             const size_t srcRB = srcPM.rowBytes();
644             for (int y = 0; y < height; y++) {
645                 proc(dstBasePtr, srcBasePtr, srcRB, width);
646                 srcBasePtr = (char*)srcBasePtr + srcRB * 2; // jump two rows
647                 dstBasePtr = (char*)dstBasePtr + dstPM.rowBytes();
648             }
649         }
650         srcPM = dstPM;
651         addr += height * rowBytes;
652     }
653     SkASSERT(addr == baseAddr + size);
654 
655     SkASSERT(mipmap->fLevels);
656     return mipmap;
657 }
658 
ComputeLevelCount(int baseWidth,int baseHeight)659 int SkMipmap::ComputeLevelCount(int baseWidth, int baseHeight) {
660     if (baseWidth < 1 || baseHeight < 1) {
661         return 0;
662     }
663 
664     // OpenGL's spec requires that each mipmap level have height/width equal to
665     // max(1, floor(original_height / 2^i)
666     // (or original_width) where i is the mipmap level.
667     // Continue scaling down until both axes are size 1.
668 
669     const int largestAxis = std::max(baseWidth, baseHeight);
670     if (largestAxis < 2) {
671         // SkMipmap::Build requires a minimum size of 2.
672         return 0;
673     }
674     const int leadingZeros = SkCLZ(static_cast<uint32_t>(largestAxis));
675     // If the value 00011010 has 3 leading 0s then it has 5 significant bits
676     // (the bits which are not leading zeros)
677     const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros;
678     // This is making the assumption that the size of a byte is 8 bits
679     // and that sizeof(uint32_t)'s implementation-defined behavior is 4.
680     int mipLevelCount = significantBits;
681 
682     // SkMipmap does not include the base mip level.
683     // For example, it contains levels 1-x instead of 0-x.
684     // This is because the image used to create SkMipmap is the base level.
685     // So subtract 1 from the mip level count.
686     if (mipLevelCount > 0) {
687         --mipLevelCount;
688     }
689 
690     return mipLevelCount;
691 }
692 
ComputeLevelSize(int baseWidth,int baseHeight,int level)693 SkISize SkMipmap::ComputeLevelSize(int baseWidth, int baseHeight, int level) {
694     if (baseWidth < 1 || baseHeight < 1) {
695         return SkISize::Make(0, 0);
696     }
697 
698     int maxLevelCount = ComputeLevelCount(baseWidth, baseHeight);
699     if (level >= maxLevelCount || level < 0) {
700         return SkISize::Make(0, 0);
701     }
702     // OpenGL's spec requires that each mipmap level have height/width equal to
703     // max(1, floor(original_height / 2^i)
704     // (or original_width) where i is the mipmap level.
705 
706     // SkMipmap does not include the base mip level.
707     // For example, it contains levels 1-x instead of 0-x.
708     // This is because the image used to create SkMipmap is the base level.
709     // So subtract 1 from the mip level to get the index stored by SkMipmap.
710     int width = std::max(1, baseWidth >> (level + 1));
711     int height = std::max(1, baseHeight >> (level + 1));
712 
713     return SkISize::Make(width, height);
714 }
715 
716 ///////////////////////////////////////////////////////////////////////////////
717 
718 // Returns fractional level value. floor(level) is the index of the larger level.
719 // < 0 means failure.
ComputeLevel(SkSize scaleSize)720 float SkMipmap::ComputeLevel(SkSize scaleSize) {
721     SkASSERT(scaleSize.width() >= 0 && scaleSize.height() >= 0);
722 
723 #ifndef SK_SUPPORT_LEGACY_ANISOTROPIC_MIPMAP_SCALE
724     // Use the smallest scale to match the GPU impl.
725     const SkScalar scale = std::min(scaleSize.width(), scaleSize.height());
726 #else
727     // Ideally we'd pick the smaller scale, to match Ganesh.  But ignoring one of the
728     // scales can produce some atrocious results, so for now we use the geometric mean.
729     // (https://bugs.chromium.org/p/skia/issues/detail?id=4863)
730     const SkScalar scale = SkScalarSqrt(scaleSize.width() * scaleSize.height());
731 #endif
732 
733     if (scale >= SK_Scalar1 || scale <= 0 || !SkScalarIsFinite(scale)) {
734         return -1;
735     }
736 
737     SkScalar L = -SkScalarLog2(scale);
738     if (!SkScalarIsFinite(L)) {
739         return -1;
740     }
741     SkASSERT(L >= 0);
742     return L;
743 }
744 
extractLevel(SkSize scaleSize,Level * levelPtr) const745 bool SkMipmap::extractLevel(SkSize scaleSize, Level* levelPtr) const {
746     if (nullptr == fLevels) {
747         return false;
748     }
749 
750     float L = ComputeLevel(scaleSize);
751     int level = SkScalarFloorToInt(L);
752     if (level <= 0) {
753         return false;
754     }
755 
756     if (level > fCount) {
757         level = fCount;
758     }
759     if (levelPtr) {
760         *levelPtr = fLevels[level - 1];
761         // need to augment with our colorspace
762         levelPtr->fPixmap.setColorSpace(fCS);
763     }
764     return true;
765 }
766 
validForRootLevel(const SkImageInfo & root) const767 bool SkMipmap::validForRootLevel(const SkImageInfo& root) const {
768     if (nullptr == fLevels) {
769         return false;
770     }
771 
772     const SkISize dimension = root.dimensions();
773     if (dimension.width() <= 1 && dimension.height() <= 1) {
774         return false;
775     }
776 
777     if (fLevels[0].fPixmap. width() != std::max(1, dimension. width() >> 1) ||
778         fLevels[0].fPixmap.height() != std::max(1, dimension.height() >> 1)) {
779         return false;
780     }
781 
782     for (int i = 0; i < this->countLevels(); ++i) {
783         if (fLevels[i].fPixmap.colorType() != root.colorType() ||
784             fLevels[i].fPixmap.alphaType() != root.alphaType()) {
785             return false;
786         }
787     }
788     return true;
789 }
790 
791 // Helper which extracts a pixmap from the src bitmap
792 //
Build(const SkBitmap & src,SkDiscardableFactoryProc fact)793 SkMipmap* SkMipmap::Build(const SkBitmap& src, SkDiscardableFactoryProc fact) {
794     SkPixmap srcPixmap;
795     if (!src.peekPixels(&srcPixmap)) {
796         return nullptr;
797     }
798     return Build(srcPixmap, fact);
799 }
800 
countLevels() const801 int SkMipmap::countLevels() const {
802     return fCount;
803 }
804 
getLevel(int index,Level * levelPtr) const805 bool SkMipmap::getLevel(int index, Level* levelPtr) const {
806     if (nullptr == fLevels) {
807         return false;
808     }
809     if (index < 0) {
810         return false;
811     }
812     if (index > fCount - 1) {
813         return false;
814     }
815     if (levelPtr) {
816         *levelPtr = fLevels[index];
817         // need to augment with our colorspace
818         levelPtr->fPixmap.setColorSpace(fCS);
819     }
820     return true;
821 }
822 
823 //////////////////////////////////////////////////////////////////////////////////////////////////
824 
825 #include "include/core/SkImageGenerator.h"
826 #include "include/core/SkStream.h"
827 #include "include/encode/SkPngEncoder.h"
828 #include "src/core/SkReadBuffer.h"
829 #include "src/core/SkWriteBuffer.h"
830 
encode_to_data(const SkPixmap & pm)831 static sk_sp<SkData> encode_to_data(const SkPixmap& pm) {
832     SkDynamicMemoryWStream stream;
833     if (SkPngEncoder::Encode(&stream, pm, SkPngEncoder::Options())) {
834         return stream.detachAsData();
835     }
836     return nullptr;
837 }
838 
839 /*  Format
840         count_levels:32
841         for each level, starting with the biggest (index 0 in our iterator)
842             encoded_size:32
843             encoded_data (padded)
844  */
serialize() const845 sk_sp<SkData> SkMipmap::serialize() const {
846     const int count = this->countLevels();
847 
848     SkBinaryWriteBuffer buffer;
849     buffer.write32(count);
850     for (int i = 0; i < count; ++i) {
851         Level level;
852         if (this->getLevel(i, &level)) {
853             buffer.writeDataAsByteArray(encode_to_data(level.fPixmap).get());
854         } else {
855             return nullptr;
856         }
857     }
858     return buffer.snapshotAsData();
859 }
860 
Deserialize(SkMipmapBuilder * builder,const void * data,size_t length)861 bool SkMipmap::Deserialize(SkMipmapBuilder* builder, const void* data, size_t length) {
862     SkReadBuffer buffer(data, length);
863 
864     int count = buffer.read32();
865     if (builder->countLevels() != count) {
866         return false;
867     }
868     for (int i = 0; i < count; ++i) {
869         size_t size = buffer.read32();
870         const void* ptr = buffer.skip(size);
871         if (!ptr) {
872             return false;
873         }
874         auto gen = SkImageGenerator::MakeFromEncoded(
875                              SkData::MakeWithProc(ptr, size, nullptr, nullptr));
876         if (!gen) {
877             return false;
878         }
879 
880         SkPixmap pm = builder->level(i);
881         if (gen->getInfo().dimensions() != pm.dimensions()) {
882             return false;
883         }
884         if (!gen->getPixels(pm)) {
885             return false;
886         }
887     }
888     return buffer.isValid();
889 }
890