blob: cd81946a5213111de59de356d7a4b4a6d08ebd7b [file] [log] [blame]
reed@google.comdc6c8ba2013-07-18 21:14:04 +00001/*
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
reed@google.comeed6f1b2013-07-18 19:53:31 +00008#include "SkMipMap.h"
9#include "SkBitmap.h"
Cary Clarka4083c92017-09-15 11:59:23 -040010#include "SkColorData.h"
brianosman1817d282016-04-06 07:32:08 -070011#include "SkHalf.h"
halcanary4dbbd042016-06-07 17:21:10 -070012#include "SkMathPriv.h"
mtklein550e9b02016-01-20 11:55:51 -080013#include "SkNx.h"
reed6644d932016-06-10 11:41:47 -070014#include "SkPM4fPriv.h"
Matt Sarett08541e82017-03-08 16:30:18 -050015#include "SkSRGB.h"
cblumee2412d52016-02-17 14:53:23 -080016#include "SkTypes.h"
reed@google.comeed6f1b2013-07-18 19:53:31 +000017
reed32e0b4a2016-01-15 13:17:08 -080018//
19// ColorTypeFilter is the "Type" we pass to some downsample template functions.
20// It controls how we expand a pixel into a large type, with space between each component,
21// so we can then perform our simple filter (either box or triangle) and store the intermediates
22// in the expanded type.
23//
24
25struct ColorTypeFilter_8888 {
26 typedef uint32_t Type;
mtklein550e9b02016-01-20 11:55:51 -080027 static Sk4h Expand(uint32_t x) {
mtklein507ef6d2016-01-31 08:02:47 -080028 return SkNx_cast<uint16_t>(Sk4b::Load(&x));
mtklein550e9b02016-01-20 11:55:51 -080029 }
30 static uint32_t Compact(const Sk4h& x) {
31 uint32_t r;
mtklein507ef6d2016-01-31 08:02:47 -080032 SkNx_cast<uint8_t>(x).store(&r);
mtklein550e9b02016-01-20 11:55:51 -080033 return r;
34 }
reed32e0b4a2016-01-15 13:17:08 -080035};
36
reed6644d932016-06-10 11:41:47 -070037struct ColorTypeFilter_S32 {
38 typedef uint32_t Type;
Matt Sarett08541e82017-03-08 16:30:18 -050039 static Sk4h Expand(uint32_t x) {
40 return Sk4h(sk_linear12_from_srgb[(x ) & 0xFF],
41 sk_linear12_from_srgb[(x >> 8) & 0xFF],
42 sk_linear12_from_srgb[(x >> 16) & 0xFF],
43 (x >> 24) << 4);
reed6644d932016-06-10 11:41:47 -070044 }
Matt Sarett08541e82017-03-08 16:30:18 -050045 static uint32_t Compact(const Sk4h& x) {
46 return sk_linear12_to_srgb[x[0]] |
47 sk_linear12_to_srgb[x[1]] << 8 |
48 sk_linear12_to_srgb[x[2]] << 16 |
49 (x[3] >> 4) << 24;
reed6644d932016-06-10 11:41:47 -070050 }
51};
52
reed32e0b4a2016-01-15 13:17:08 -080053struct ColorTypeFilter_565 {
54 typedef uint16_t Type;
55 static uint32_t Expand(uint16_t x) {
56 return (x & ~SK_G16_MASK_IN_PLACE) | ((x & SK_G16_MASK_IN_PLACE) << 16);
57 }
58 static uint16_t Compact(uint32_t x) {
59 return (x & ~SK_G16_MASK_IN_PLACE) | ((x >> 16) & SK_G16_MASK_IN_PLACE);
60 }
61};
62
63struct ColorTypeFilter_4444 {
64 typedef uint16_t Type;
65 static uint32_t Expand(uint16_t x) {
66 return (x & 0xF0F) | ((x & ~0xF0F) << 12);
67 }
68 static uint16_t Compact(uint32_t x) {
69 return (x & 0xF0F) | ((x >> 12) & ~0xF0F);
70 }
71};
72
73struct ColorTypeFilter_8 {
74 typedef uint8_t Type;
75 static unsigned Expand(unsigned x) {
76 return x;
77 }
78 static uint8_t Compact(unsigned x) {
79 return (uint8_t)x;
80 }
81};
82
brianosman1817d282016-04-06 07:32:08 -070083struct ColorTypeFilter_F16 {
84 typedef uint64_t Type; // SkHalf x4
85 static Sk4f Expand(uint64_t x) {
mtklein8ae991e2016-08-22 13:20:18 -070086 return SkHalfToFloat_finite_ftz(x);
brianosman1817d282016-04-06 07:32:08 -070087 }
88 static uint64_t Compact(const Sk4f& x) {
msarett6bdbf442016-07-19 09:07:55 -070089 uint64_t r;
mtklein8ae991e2016-08-22 13:20:18 -070090 SkFloatToHalf_finite_ftz(x).store(&r);
msarett6bdbf442016-07-19 09:07:55 -070091 return r;
brianosman1817d282016-04-06 07:32:08 -070092 }
93};
94
mtklein550e9b02016-01-20 11:55:51 -080095template <typename T> T add_121(const T& a, const T& b, const T& c) {
reed32e0b4a2016-01-15 13:17:08 -080096 return a + b + b + c;
97}
98
brianosman1817d282016-04-06 07:32:08 -070099template <typename T> T shift_right(const T& x, int bits) {
100 return x >> bits;
101}
102
103Sk4f shift_right(const Sk4f& x, int bits) {
104 return x * (1.0f / (1 << bits));
105}
106
107template <typename T> T shift_left(const T& x, int bits) {
108 return x << bits;
109}
110
111Sk4f shift_left(const Sk4f& x, int bits) {
112 return x * (1 << bits);
113}
114
reed32e0b4a2016-01-15 13:17:08 -0800115//
116// To produce each mip level, we need to filter down by 1/2 (e.g. 100x100 -> 50,50)
117// If the starting dimension is odd, we floor the size of the lower level (e.g. 101 -> 50)
118// In those (odd) cases, we use a triangle filter, with 1-pixel overlap between samplings,
119// else for even cases, we just use a 2x box filter.
120//
cblume5b9ad762016-03-01 13:54:30 -0800121// This produces 4 possible isotropic filters: 2x2 2x3 3x2 3x3 where WxH indicates the number of
122// src pixels we need to sample in each dimension to produce 1 dst pixel.
reed32e0b4a2016-01-15 13:17:08 -0800123//
cblume5b9ad762016-03-01 13:54:30 -0800124// OpenGL expects a full mipmap stack to contain anisotropic space as well.
125// This means a 100x1 image would continue down to a 50x1 image, 25x1 image...
126// Because of this, we need 4 more anisotropic filters: 1x2, 1x3, 2x1, 3x1.
127
128template <typename F> void downsample_1_2(void* dst, const void* src, size_t srcRB, int count) {
129 SkASSERT(count > 0);
130 auto p0 = static_cast<const typename F::Type*>(src);
131 auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
132 auto d = static_cast<typename F::Type*>(dst);
133
134 for (int i = 0; i < count; ++i) {
135 auto c00 = F::Expand(p0[0]);
136 auto c10 = F::Expand(p1[0]);
137
138 auto c = c00 + c10;
brianosman1817d282016-04-06 07:32:08 -0700139 d[i] = F::Compact(shift_right(c, 1));
cblume5b9ad762016-03-01 13:54:30 -0800140 p0 += 2;
141 p1 += 2;
142 }
143}
144
145template <typename F> void downsample_1_3(void* dst, const void* src, size_t srcRB, int count) {
146 SkASSERT(count > 0);
147 auto p0 = static_cast<const typename F::Type*>(src);
148 auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
149 auto p2 = (const typename F::Type*)((const char*)p1 + srcRB);
150 auto d = static_cast<typename F::Type*>(dst);
151
152 for (int i = 0; i < count; ++i) {
153 auto c00 = F::Expand(p0[0]);
154 auto c10 = F::Expand(p1[0]);
155 auto c20 = F::Expand(p2[0]);
156
157 auto c = add_121(c00, c10, c20);
brianosman1817d282016-04-06 07:32:08 -0700158 d[i] = F::Compact(shift_right(c, 2));
cblume5b9ad762016-03-01 13:54:30 -0800159 p0 += 2;
160 p1 += 2;
161 p2 += 2;
162 }
163}
164
165template <typename F> void downsample_2_1(void* dst, const void* src, size_t srcRB, int count) {
166 SkASSERT(count > 0);
167 auto p0 = static_cast<const typename F::Type*>(src);
168 auto d = static_cast<typename F::Type*>(dst);
169
170 for (int i = 0; i < count; ++i) {
171 auto c00 = F::Expand(p0[0]);
172 auto c01 = F::Expand(p0[1]);
173
174 auto c = c00 + c01;
brianosman1817d282016-04-06 07:32:08 -0700175 d[i] = F::Compact(shift_right(c, 1));
cblume5b9ad762016-03-01 13:54:30 -0800176 p0 += 2;
177 }
178}
reed32e0b4a2016-01-15 13:17:08 -0800179
reed326253e2016-01-16 09:23:48 -0800180template <typename F> void downsample_2_2(void* dst, const void* src, size_t srcRB, int count) {
cblume5b9ad762016-03-01 13:54:30 -0800181 SkASSERT(count > 0);
reed32e0b4a2016-01-15 13:17:08 -0800182 auto p0 = static_cast<const typename F::Type*>(src);
183 auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
reed326253e2016-01-16 09:23:48 -0800184 auto d = static_cast<typename F::Type*>(dst);
reed32e0b4a2016-01-15 13:17:08 -0800185
reed326253e2016-01-16 09:23:48 -0800186 for (int i = 0; i < count; ++i) {
187 auto c00 = F::Expand(p0[0]);
188 auto c01 = F::Expand(p0[1]);
189 auto c10 = F::Expand(p1[0]);
190 auto c11 = F::Expand(p1[1]);
reed32e0b4a2016-01-15 13:17:08 -0800191
reed326253e2016-01-16 09:23:48 -0800192 auto c = c00 + c10 + c01 + c11;
brianosman1817d282016-04-06 07:32:08 -0700193 d[i] = F::Compact(shift_right(c, 2));
reed326253e2016-01-16 09:23:48 -0800194 p0 += 2;
195 p1 += 2;
196 }
reed32e0b4a2016-01-15 13:17:08 -0800197}
198
cblume5b9ad762016-03-01 13:54:30 -0800199template <typename F> void downsample_2_3(void* dst, const void* src, size_t srcRB, int count) {
200 SkASSERT(count > 0);
201 auto p0 = static_cast<const typename F::Type*>(src);
202 auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
203 auto p2 = (const typename F::Type*)((const char*)p1 + srcRB);
204 auto d = static_cast<typename F::Type*>(dst);
205
206 for (int i = 0; i < count; ++i) {
207 auto c00 = F::Expand(p0[0]);
208 auto c01 = F::Expand(p0[1]);
209 auto c10 = F::Expand(p1[0]);
210 auto c11 = F::Expand(p1[1]);
211 auto c20 = F::Expand(p2[0]);
212 auto c21 = F::Expand(p2[1]);
213
214 auto c = add_121(c00, c10, c20) + add_121(c01, c11, c21);
brianosman1817d282016-04-06 07:32:08 -0700215 d[i] = F::Compact(shift_right(c, 3));
cblume5b9ad762016-03-01 13:54:30 -0800216 p0 += 2;
217 p1 += 2;
218 p2 += 2;
219 }
220}
221
222template <typename F> void downsample_3_1(void* dst, const void* src, size_t srcRB, int count) {
223 SkASSERT(count > 0);
224 auto p0 = static_cast<const typename F::Type*>(src);
225 auto d = static_cast<typename F::Type*>(dst);
226
227 auto c02 = F::Expand(p0[0]);
228 for (int i = 0; i < count; ++i) {
229 auto c00 = c02;
230 auto c01 = F::Expand(p0[1]);
231 c02 = F::Expand(p0[2]);
232
233 auto c = add_121(c00, c01, c02);
brianosman1817d282016-04-06 07:32:08 -0700234 d[i] = F::Compact(shift_right(c, 2));
cblume5b9ad762016-03-01 13:54:30 -0800235 p0 += 2;
236 }
237}
238
reed326253e2016-01-16 09:23:48 -0800239template <typename F> void downsample_3_2(void* dst, const void* src, size_t srcRB, int count) {
240 SkASSERT(count > 0);
reed32e0b4a2016-01-15 13:17:08 -0800241 auto p0 = static_cast<const typename F::Type*>(src);
242 auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
reed326253e2016-01-16 09:23:48 -0800243 auto d = static_cast<typename F::Type*>(dst);
mtklein550e9b02016-01-20 11:55:51 -0800244
Matt Sarett8a8e5fe2017-04-11 11:53:24 -0400245 // Given pixels:
246 // a0 b0 c0 d0 e0 ...
247 // a1 b1 c1 d1 e1 ...
248 // We want:
249 // (a0 + 2*b0 + c0 + a1 + 2*b1 + c1) / 8
250 // (c0 + 2*d0 + e0 + c1 + 2*d1 + e1) / 8
251 // ...
Matt Sarett88ec28e2017-03-22 17:27:43 -0400252
Matt Sarett8a8e5fe2017-04-11 11:53:24 -0400253 auto c0 = F::Expand(p0[0]);
254 auto c1 = F::Expand(p1[0]);
255 auto c = c0 + c1;
256 for (int i = 0; i < count; ++i) {
257 auto a = c;
258
259 auto b0 = F::Expand(p0[1]);
260 auto b1 = F::Expand(p1[1]);
261 auto b = b0 + b0 + b1 + b1;
262
263 c0 = F::Expand(p0[2]);
264 c1 = F::Expand(p1[2]);
265 c = c0 + c1;
266
267 auto sum = a + b + c;
268 d[i] = F::Compact(shift_right(sum, 3));
reed326253e2016-01-16 09:23:48 -0800269 p0 += 2;
270 p1 += 2;
271 }
reed32e0b4a2016-01-15 13:17:08 -0800272}
273
reed326253e2016-01-16 09:23:48 -0800274template <typename F> void downsample_3_3(void* dst, const void* src, size_t srcRB, int count) {
cblume5b9ad762016-03-01 13:54:30 -0800275 SkASSERT(count > 0);
reed32e0b4a2016-01-15 13:17:08 -0800276 auto p0 = static_cast<const typename F::Type*>(src);
277 auto p1 = (const typename F::Type*)((const char*)p0 + srcRB);
278 auto p2 = (const typename F::Type*)((const char*)p1 + srcRB);
reed326253e2016-01-16 09:23:48 -0800279 auto d = static_cast<typename F::Type*>(dst);
reed32e0b4a2016-01-15 13:17:08 -0800280
Matt Sarettb667fe22017-03-22 09:20:02 -0400281 // Given pixels:
282 // a0 b0 c0 d0 e0 ...
283 // a1 b1 c1 d1 e1 ...
284 // a2 b2 c2 d2 e2 ...
285 // We want:
286 // (a0 + 2*b0 + c0 + 2*a1 + 4*b1 + 2*c1 + a2 + 2*b2 + c2) / 16
287 // (c0 + 2*d0 + e0 + 2*c1 + 4*d1 + 2*e1 + c2 + 2*d2 + e2) / 16
288 // ...
reed32e0b4a2016-01-15 13:17:08 -0800289
Matt Sarettb667fe22017-03-22 09:20:02 -0400290 auto c0 = F::Expand(p0[0]);
291 auto c1 = F::Expand(p1[0]);
292 auto c2 = F::Expand(p2[0]);
293 auto c = add_121(c0, c1, c2);
294 for (int i = 0; i < count; ++i) {
295 auto a = c;
296
297 auto b0 = F::Expand(p0[1]);
298 auto b1 = F::Expand(p1[1]);
299 auto b2 = F::Expand(p2[1]);
300 auto b = shift_left(add_121(b0, b1, b2), 1);
301
302 c0 = F::Expand(p0[2]);
303 c1 = F::Expand(p1[2]);
304 c2 = F::Expand(p2[2]);
305 c = add_121(c0, c1, c2);
306
307 auto sum = a + b + c;
308 d[i] = F::Compact(shift_right(sum, 4));
reed326253e2016-01-16 09:23:48 -0800309 p0 += 2;
310 p1 += 2;
311 p2 += 2;
312 }
reed32e0b4a2016-01-15 13:17:08 -0800313}
314
315///////////////////////////////////////////////////////////////////////////////////////////////////
316
Matt Sarett08541e82017-03-08 16:30:18 -0500317// Some sRGB specific performance optimizations.
318
319void downsample_2_2_srgb(void* dst, const void* src, size_t srcRB, int count) {
320 const uint8_t* p0 = ((const uint8_t*) src);
321 const uint8_t* p1 = ((const uint8_t*) src) + srcRB;
322 uint8_t* d = (uint8_t*) dst;
323
324 // Given pixels:
325 // a0 b0 c0 d0 ...
326 // a1 b1 c1 d1 ...
327 // We want:
328 // (a0 + b0 + a1 + b1) / 4
329 // (c0 + d0 + c1 + d1) / 4
330 // ...
331 while (count >= 2) {
332 Sk8h a0c0 = Sk8h(sk_linear12_from_srgb[p0[ 0]],
333 sk_linear12_from_srgb[p0[ 1]],
334 sk_linear12_from_srgb[p0[ 2]],
335 p0[ 3] << 4 ,
336 sk_linear12_from_srgb[p0[ 8]],
337 sk_linear12_from_srgb[p0[ 9]],
338 sk_linear12_from_srgb[p0[10]],
Matt Sarett8a8e5fe2017-04-11 11:53:24 -0400339 p0[11] << 4 );
Matt Sarett08541e82017-03-08 16:30:18 -0500340 Sk8h b0d0 = Sk8h(sk_linear12_from_srgb[p0[ 4]],
341 sk_linear12_from_srgb[p0[ 5]],
342 sk_linear12_from_srgb[p0[ 6]],
343 p0[ 7] << 4 ,
344 sk_linear12_from_srgb[p0[12]],
345 sk_linear12_from_srgb[p0[13]],
346 sk_linear12_from_srgb[p0[14]],
347 p0[15] << 4 );
348 Sk8h a1c1 = Sk8h(sk_linear12_from_srgb[p1[ 0]],
349 sk_linear12_from_srgb[p1[ 1]],
350 sk_linear12_from_srgb[p1[ 2]],
351 p1[ 3] << 4 ,
352 sk_linear12_from_srgb[p1[ 8]],
353 sk_linear12_from_srgb[p1[ 9]],
354 sk_linear12_from_srgb[p1[10]],
355 p1[11] << 4 );
356 Sk8h b1d1 = Sk8h(sk_linear12_from_srgb[p1[ 4]],
357 sk_linear12_from_srgb[p1[ 5]],
358 sk_linear12_from_srgb[p1[ 6]],
359 p1[ 7] << 4 ,
360 sk_linear12_from_srgb[p1[12]],
361 sk_linear12_from_srgb[p1[13]],
362 sk_linear12_from_srgb[p1[14]],
363 p1[15] << 4 );
364
365 Sk8h avg = (a0c0 + b0d0 + a1c1 + b1d1) >> 2;
366 d[0] = sk_linear12_to_srgb[avg[0]];
367 d[1] = sk_linear12_to_srgb[avg[1]];
368 d[2] = sk_linear12_to_srgb[avg[2]];
369 d[3] = avg[3] >> 4;
370 d[4] = sk_linear12_to_srgb[avg[4]];
371 d[5] = sk_linear12_to_srgb[avg[5]];
372 d[6] = sk_linear12_to_srgb[avg[6]];
373 d[7] = avg[7] >> 4;
374
375 p0 += 16;
376 p1 += 16;
377 d += 8;
378 count -= 2;
379 }
380
381 if (count) {
382 downsample_2_2<ColorTypeFilter_S32>(d, p0, srcRB, count);
383 }
384}
385
Matt Sarett8a8e5fe2017-04-11 11:53:24 -0400386void downsample_2_3_srgb(void* dst, const void* src, size_t srcRB, int count) {
387 const uint8_t* p0 = ((const uint8_t*) src);
388 const uint8_t* p1 = p0 + srcRB;
389 const uint8_t* p2 = p1 + srcRB;
390 uint8_t* d = (uint8_t*) dst;
391
392 // Given pixels:
393 // a0 b0 c0 d0 ...
394 // a1 b1 c1 d1 ...
395 // a2 b2 c2 d2 ...
396 // We want:
397 // (a0 + b0 + 2*a1 + 2*b1 + a2 + b2) / 8
398 // (c0 + d0 + 2*c1 + 2*d1 + c2 + d2) / 8
399 // ...
400 while (count >= 2) {
401 Sk8h a0c0 = Sk8h(sk_linear12_from_srgb[p0[ 0]],
402 sk_linear12_from_srgb[p0[ 1]],
403 sk_linear12_from_srgb[p0[ 2]],
404 p0[ 3] << 4 ,
405 sk_linear12_from_srgb[p0[ 8]],
406 sk_linear12_from_srgb[p0[ 9]],
407 sk_linear12_from_srgb[p0[10]],
408 p0[11] << 4 );
409 Sk8h b0d0 = Sk8h(sk_linear12_from_srgb[p0[ 4]],
410 sk_linear12_from_srgb[p0[ 5]],
411 sk_linear12_from_srgb[p0[ 6]],
412 p0[ 7] << 4 ,
413 sk_linear12_from_srgb[p0[12]],
414 sk_linear12_from_srgb[p0[13]],
415 sk_linear12_from_srgb[p0[14]],
416 p0[15] << 4 );
417 Sk8h a1c1 = Sk8h(sk_linear12_from_srgb[p1[ 0]],
418 sk_linear12_from_srgb[p1[ 1]],
419 sk_linear12_from_srgb[p1[ 2]],
420 p1[ 3] << 4 ,
421 sk_linear12_from_srgb[p1[ 8]],
422 sk_linear12_from_srgb[p1[ 9]],
423 sk_linear12_from_srgb[p1[10]],
424 p1[11] << 4 );
425 Sk8h b1d1 = Sk8h(sk_linear12_from_srgb[p1[ 4]],
426 sk_linear12_from_srgb[p1[ 5]],
427 sk_linear12_from_srgb[p1[ 6]],
428 p1[ 7] << 4 ,
429 sk_linear12_from_srgb[p1[12]],
430 sk_linear12_from_srgb[p1[13]],
431 sk_linear12_from_srgb[p1[14]],
432 p1[15] << 4 );
433 Sk8h a2c2 = Sk8h(sk_linear12_from_srgb[p2[ 0]],
434 sk_linear12_from_srgb[p2[ 1]],
435 sk_linear12_from_srgb[p2[ 2]],
436 p2[ 3] << 4 ,
437 sk_linear12_from_srgb[p2[ 8]],
438 sk_linear12_from_srgb[p2[ 9]],
439 sk_linear12_from_srgb[p2[10]],
440 p2[11] << 4 );
441 Sk8h b2d2 = Sk8h(sk_linear12_from_srgb[p2[ 4]],
442 sk_linear12_from_srgb[p2[ 5]],
443 sk_linear12_from_srgb[p2[ 6]],
444 p2[ 7] << 4 ,
445 sk_linear12_from_srgb[p2[12]],
446 sk_linear12_from_srgb[p2[13]],
447 sk_linear12_from_srgb[p2[14]],
448 p2[15] << 4 );
449
450 Sk8h avg = (a0c0 + b0d0 + a1c1 + a1c1 + b1d1 + b1d1 + a2c2 + b2d2) >> 3;
451 d[0] = sk_linear12_to_srgb[avg[0]];
452 d[1] = sk_linear12_to_srgb[avg[1]];
453 d[2] = sk_linear12_to_srgb[avg[2]];
454 d[3] = avg[3] >> 4;
455 d[4] = sk_linear12_to_srgb[avg[4]];
456 d[5] = sk_linear12_to_srgb[avg[5]];
457 d[6] = sk_linear12_to_srgb[avg[6]];
458 d[7] = avg[7] >> 4;
459
460 p0 += 16;
461 p1 += 16;
462 p2 += 16;
463 d += 8;
464 count -= 2;
465 }
466
467 if (count) {
468 downsample_2_3<ColorTypeFilter_S32>(d, p0, srcRB, count);
469 }
470}
471
Matt Sarett08541e82017-03-08 16:30:18 -0500472///////////////////////////////////////////////////////////////////////////////////////////////////
473
reed32e0b4a2016-01-15 13:17:08 -0800474size_t SkMipMap::AllocLevelsSize(int levelCount, size_t pixelSize) {
475 if (levelCount < 0) {
476 return 0;
477 }
478 int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize;
479 if (!sk_64_isS32(size)) {
480 return 0;
481 }
482 return sk_64_asS32(size);
483}
484
Brian Osman7b8400d2016-11-08 17:08:54 -0500485SkMipMap* SkMipMap::Build(const SkPixmap& src, SkDestinationSurfaceColorMode colorMode,
reed6644d932016-06-10 11:41:47 -0700486 SkDiscardableFactoryProc fact) {
reed326253e2016-01-16 09:23:48 -0800487 typedef void FilterProc(void*, const void* srcPtr, size_t srcRB, int count);
reed32e0b4a2016-01-15 13:17:08 -0800488
cblume5b9ad762016-03-01 13:54:30 -0800489 FilterProc* proc_1_2 = nullptr;
490 FilterProc* proc_1_3 = nullptr;
491 FilterProc* proc_2_1 = nullptr;
reed32e0b4a2016-01-15 13:17:08 -0800492 FilterProc* proc_2_2 = nullptr;
493 FilterProc* proc_2_3 = nullptr;
cblume5b9ad762016-03-01 13:54:30 -0800494 FilterProc* proc_3_1 = nullptr;
reed32e0b4a2016-01-15 13:17:08 -0800495 FilterProc* proc_3_2 = nullptr;
496 FilterProc* proc_3_3 = nullptr;
497
498 const SkColorType ct = src.colorType();
499 const SkAlphaType at = src.alphaType();
Brian Osman7b8400d2016-11-08 17:08:54 -0500500 const bool srgbGamma = (SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware == colorMode)
reeddabe5d32016-06-21 10:28:14 -0700501 && src.info().gammaCloseToSRGB();
reed6644d932016-06-10 11:41:47 -0700502
reed32e0b4a2016-01-15 13:17:08 -0800503 switch (ct) {
504 case kRGBA_8888_SkColorType:
505 case kBGRA_8888_SkColorType:
reed6644d932016-06-10 11:41:47 -0700506 if (srgbGamma) {
507 proc_1_2 = downsample_1_2<ColorTypeFilter_S32>;
508 proc_1_3 = downsample_1_3<ColorTypeFilter_S32>;
509 proc_2_1 = downsample_2_1<ColorTypeFilter_S32>;
Matt Sarett08541e82017-03-08 16:30:18 -0500510 proc_2_2 = downsample_2_2_srgb;
Matt Sarett8a8e5fe2017-04-11 11:53:24 -0400511 proc_2_3 = downsample_2_3_srgb;
reed6644d932016-06-10 11:41:47 -0700512 proc_3_1 = downsample_3_1<ColorTypeFilter_S32>;
513 proc_3_2 = downsample_3_2<ColorTypeFilter_S32>;
514 proc_3_3 = downsample_3_3<ColorTypeFilter_S32>;
515 } else {
516 proc_1_2 = downsample_1_2<ColorTypeFilter_8888>;
517 proc_1_3 = downsample_1_3<ColorTypeFilter_8888>;
518 proc_2_1 = downsample_2_1<ColorTypeFilter_8888>;
519 proc_2_2 = downsample_2_2<ColorTypeFilter_8888>;
520 proc_2_3 = downsample_2_3<ColorTypeFilter_8888>;
521 proc_3_1 = downsample_3_1<ColorTypeFilter_8888>;
522 proc_3_2 = downsample_3_2<ColorTypeFilter_8888>;
523 proc_3_3 = downsample_3_3<ColorTypeFilter_8888>;
524 }
reed32e0b4a2016-01-15 13:17:08 -0800525 break;
526 case kRGB_565_SkColorType:
cblume5b9ad762016-03-01 13:54:30 -0800527 proc_1_2 = downsample_1_2<ColorTypeFilter_565>;
528 proc_1_3 = downsample_1_3<ColorTypeFilter_565>;
529 proc_2_1 = downsample_2_1<ColorTypeFilter_565>;
reed32e0b4a2016-01-15 13:17:08 -0800530 proc_2_2 = downsample_2_2<ColorTypeFilter_565>;
531 proc_2_3 = downsample_2_3<ColorTypeFilter_565>;
cblume5b9ad762016-03-01 13:54:30 -0800532 proc_3_1 = downsample_3_1<ColorTypeFilter_565>;
reed32e0b4a2016-01-15 13:17:08 -0800533 proc_3_2 = downsample_3_2<ColorTypeFilter_565>;
534 proc_3_3 = downsample_3_3<ColorTypeFilter_565>;
535 break;
536 case kARGB_4444_SkColorType:
cblume5b9ad762016-03-01 13:54:30 -0800537 proc_1_2 = downsample_1_2<ColorTypeFilter_4444>;
538 proc_1_3 = downsample_1_3<ColorTypeFilter_4444>;
539 proc_2_1 = downsample_2_1<ColorTypeFilter_4444>;
reed32e0b4a2016-01-15 13:17:08 -0800540 proc_2_2 = downsample_2_2<ColorTypeFilter_4444>;
541 proc_2_3 = downsample_2_3<ColorTypeFilter_4444>;
cblume5b9ad762016-03-01 13:54:30 -0800542 proc_3_1 = downsample_3_1<ColorTypeFilter_4444>;
reed32e0b4a2016-01-15 13:17:08 -0800543 proc_3_2 = downsample_3_2<ColorTypeFilter_4444>;
544 proc_3_3 = downsample_3_3<ColorTypeFilter_4444>;
545 break;
546 case kAlpha_8_SkColorType:
547 case kGray_8_SkColorType:
cblume5b9ad762016-03-01 13:54:30 -0800548 proc_1_2 = downsample_1_2<ColorTypeFilter_8>;
549 proc_1_3 = downsample_1_3<ColorTypeFilter_8>;
550 proc_2_1 = downsample_2_1<ColorTypeFilter_8>;
reed32e0b4a2016-01-15 13:17:08 -0800551 proc_2_2 = downsample_2_2<ColorTypeFilter_8>;
552 proc_2_3 = downsample_2_3<ColorTypeFilter_8>;
cblume5b9ad762016-03-01 13:54:30 -0800553 proc_3_1 = downsample_3_1<ColorTypeFilter_8>;
reed32e0b4a2016-01-15 13:17:08 -0800554 proc_3_2 = downsample_3_2<ColorTypeFilter_8>;
555 proc_3_3 = downsample_3_3<ColorTypeFilter_8>;
556 break;
brianosman1817d282016-04-06 07:32:08 -0700557 case kRGBA_F16_SkColorType:
558 proc_1_2 = downsample_1_2<ColorTypeFilter_F16>;
559 proc_1_3 = downsample_1_3<ColorTypeFilter_F16>;
560 proc_2_1 = downsample_2_1<ColorTypeFilter_F16>;
561 proc_2_2 = downsample_2_2<ColorTypeFilter_F16>;
562 proc_2_3 = downsample_2_3<ColorTypeFilter_F16>;
563 proc_3_1 = downsample_3_1<ColorTypeFilter_F16>;
564 proc_3_2 = downsample_3_2<ColorTypeFilter_F16>;
565 proc_3_3 = downsample_3_3<ColorTypeFilter_F16>;
566 break;
reed32e0b4a2016-01-15 13:17:08 -0800567 default:
568 // TODO: We could build miplevels for kIndex8 if the levels were in 8888.
569 // Means using more ram, but the quality would be fine.
570 return nullptr;
571 }
572
cblume5b9ad762016-03-01 13:54:30 -0800573 if (src.width() <= 1 && src.height() <= 1) {
574 return nullptr;
575 }
reed32e0b4a2016-01-15 13:17:08 -0800576 // whip through our loop to compute the exact size needed
cblume609623b2016-06-09 09:44:33 -0700577 size_t size = 0;
cblumef95ff4a2016-06-02 09:01:48 -0700578 int countLevels = ComputeLevelCount(src.width(), src.height());
cblume609623b2016-06-09 09:44:33 -0700579 for (int currentMipLevel = countLevels; currentMipLevel >= 0; currentMipLevel--) {
cblume44e09ec2016-06-03 11:59:50 -0700580 SkISize mipSize = ComputeLevelSize(src.width(), src.height(), currentMipLevel);
cblumef95ff4a2016-06-02 09:01:48 -0700581 size += SkColorTypeMinRowBytes(ct, mipSize.fWidth) * mipSize.fHeight;
reed32e0b4a2016-01-15 13:17:08 -0800582 }
reed32e0b4a2016-01-15 13:17:08 -0800583
584 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size);
585 if (0 == storageSize) {
586 return nullptr;
587 }
588
reed32e0b4a2016-01-15 13:17:08 -0800589 SkMipMap* mipmap;
590 if (fact) {
591 SkDiscardableMemory* dm = fact(storageSize);
592 if (nullptr == dm) {
593 return nullptr;
594 }
595 mipmap = new SkMipMap(storageSize, dm);
596 } else {
597 mipmap = new SkMipMap(sk_malloc_throw(storageSize), storageSize);
598 }
599
600 // init
reed6644d932016-06-10 11:41:47 -0700601 mipmap->fCS = sk_ref_sp(src.info().colorSpace());
reed32e0b4a2016-01-15 13:17:08 -0800602 mipmap->fCount = countLevels;
603 mipmap->fLevels = (Level*)mipmap->writable_data();
reed6644d932016-06-10 11:41:47 -0700604 SkASSERT(mipmap->fLevels);
reed32e0b4a2016-01-15 13:17:08 -0800605
606 Level* levels = mipmap->fLevels;
607 uint8_t* baseAddr = (uint8_t*)&levels[countLevels];
608 uint8_t* addr = baseAddr;
609 int width = src.width();
610 int height = src.height();
611 uint32_t rowBytes;
reed67b09bf2016-01-16 18:50:35 -0800612 SkPixmap srcPM(src);
reed32e0b4a2016-01-15 13:17:08 -0800613
reed32e0b4a2016-01-15 13:17:08 -0800614 for (int i = 0; i < countLevels; ++i) {
reed326253e2016-01-16 09:23:48 -0800615 FilterProc* proc;
cblume5b9ad762016-03-01 13:54:30 -0800616 if (height & 1) {
617 if (height == 1) { // src-height is 1
618 if (width & 1) { // src-width is 3
619 proc = proc_3_1;
620 } else { // src-width is 2
621 proc = proc_2_1;
622 }
623 } else { // src-height is 3
624 if (width & 1) {
625 if (width == 1) { // src-width is 1
626 proc = proc_1_3;
627 } else { // src-width is 3
628 proc = proc_3_3;
629 }
630 } else { // src-width is 2
631 proc = proc_2_3;
632 }
reed326253e2016-01-16 09:23:48 -0800633 }
cblume5b9ad762016-03-01 13:54:30 -0800634 } else { // src-height is 2
635 if (width & 1) {
636 if (width == 1) { // src-width is 1
637 proc = proc_1_2;
638 } else { // src-width is 3
639 proc = proc_3_2;
640 }
641 } else { // src-width is 2
reed326253e2016-01-16 09:23:48 -0800642 proc = proc_2_2;
643 }
644 }
cblume5b9ad762016-03-01 13:54:30 -0800645 width = SkTMax(1, width >> 1);
646 height = SkTMax(1, height >> 1);
reed32e0b4a2016-01-15 13:17:08 -0800647 rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width));
648
reed6644d932016-06-10 11:41:47 -0700649 // We make the Info w/o any colorspace, since that storage is not under our control, and
650 // will not be deleted in a controlled fashion. When the caller is given the pixmap for
651 // a given level, we augment this pixmap with fCS (which we do manage).
msarett23c51102016-05-27 07:39:02 -0700652 new (&levels[i].fPixmap) SkPixmap(SkImageInfo::Make(width, height, ct, at), addr, rowBytes);
fmalita921d7ac2016-01-22 11:45:39 -0800653 levels[i].fScale = SkSize::Make(SkIntToScalar(width) / src.width(),
654 SkIntToScalar(height) / src.height());
reed32e0b4a2016-01-15 13:17:08 -0800655
reed67b09bf2016-01-16 18:50:35 -0800656 const SkPixmap& dstPM = levels[i].fPixmap;
reed32e0b4a2016-01-15 13:17:08 -0800657 const void* srcBasePtr = srcPM.addr();
658 void* dstBasePtr = dstPM.writable_addr();
659
reed32e0b4a2016-01-15 13:17:08 -0800660 const size_t srcRB = srcPM.rowBytes();
661 for (int y = 0; y < height; y++) {
reed326253e2016-01-16 09:23:48 -0800662 proc(dstBasePtr, srcBasePtr, srcRB, width);
reed32e0b4a2016-01-15 13:17:08 -0800663 srcBasePtr = (char*)srcBasePtr + srcRB * 2; // jump two rows
664 dstBasePtr = (char*)dstBasePtr + dstPM.rowBytes();
665 }
666 srcPM = dstPM;
667 addr += height * rowBytes;
reed32e0b4a2016-01-15 13:17:08 -0800668 }
669 SkASSERT(addr == baseAddr + size);
reed@google.comeed6f1b2013-07-18 19:53:31 +0000670
reed6644d932016-06-10 11:41:47 -0700671 SkASSERT(mipmap->fLevels);
reed9d93c2e2014-10-08 05:17:12 -0700672 return mipmap;
reed@google.comd94697c2013-07-24 14:31:33 +0000673}
674
cblumee2412d52016-02-17 14:53:23 -0800675int SkMipMap::ComputeLevelCount(int baseWidth, int baseHeight) {
cblume5b9ad762016-03-01 13:54:30 -0800676 if (baseWidth < 1 || baseHeight < 1) {
677 return 0;
678 }
679
cblumee2412d52016-02-17 14:53:23 -0800680 // OpenGL's spec requires that each mipmap level have height/width equal to
681 // max(1, floor(original_height / 2^i)
682 // (or original_width) where i is the mipmap level.
683 // Continue scaling down until both axes are size 1.
cblumee2412d52016-02-17 14:53:23 -0800684
cblume5b9ad762016-03-01 13:54:30 -0800685 const int largestAxis = SkTMax(baseWidth, baseHeight);
686 if (largestAxis < 2) {
cblumee2412d52016-02-17 14:53:23 -0800687 // SkMipMap::Build requires a minimum size of 2.
688 return 0;
689 }
cblume5b9ad762016-03-01 13:54:30 -0800690 const int leadingZeros = SkCLZ(static_cast<uint32_t>(largestAxis));
cblumee2412d52016-02-17 14:53:23 -0800691 // If the value 00011010 has 3 leading 0s then it has 5 significant bits
692 // (the bits which are not leading zeros)
693 const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros;
694 // This is making the assumption that the size of a byte is 8 bits
695 // and that sizeof(uint32_t)'s implementation-defined behavior is 4.
696 int mipLevelCount = significantBits;
697
698 // SkMipMap does not include the base mip level.
699 // For example, it contains levels 1-x instead of 0-x.
700 // This is because the image used to create SkMipMap is the base level.
701 // So subtract 1 from the mip level count.
702 if (mipLevelCount > 0) {
703 --mipLevelCount;
704 }
705
706 return mipLevelCount;
707}
708
cblume44e09ec2016-06-03 11:59:50 -0700709SkISize SkMipMap::ComputeLevelSize(int baseWidth, int baseHeight, int level) {
cblumef95ff4a2016-06-02 09:01:48 -0700710 if (baseWidth < 1 || baseHeight < 1) {
cblume44e09ec2016-06-03 11:59:50 -0700711 return SkISize::Make(0, 0);
cblumef95ff4a2016-06-02 09:01:48 -0700712 }
713
714 int maxLevelCount = ComputeLevelCount(baseWidth, baseHeight);
cblume609623b2016-06-09 09:44:33 -0700715 if (level >= maxLevelCount || level < 0) {
cblume44e09ec2016-06-03 11:59:50 -0700716 return SkISize::Make(0, 0);
cblumef95ff4a2016-06-02 09:01:48 -0700717 }
cblumef95ff4a2016-06-02 09:01:48 -0700718 // OpenGL's spec requires that each mipmap level have height/width equal to
719 // max(1, floor(original_height / 2^i)
720 // (or original_width) where i is the mipmap level.
721
cblume609623b2016-06-09 09:44:33 -0700722 // SkMipMap does not include the base mip level.
723 // For example, it contains levels 1-x instead of 0-x.
724 // This is because the image used to create SkMipMap is the base level.
725 // So subtract 1 from the mip level to get the index stored by SkMipMap.
726 int width = SkTMax(1, baseWidth >> (level + 1));
727 int height = SkTMax(1, baseHeight >> (level + 1));
cblumef95ff4a2016-06-02 09:01:48 -0700728
cblume44e09ec2016-06-03 11:59:50 -0700729 return SkISize::Make(width, height);
cblumef95ff4a2016-06-02 09:01:48 -0700730}
731
reed@google.comd94697c2013-07-24 14:31:33 +0000732///////////////////////////////////////////////////////////////////////////////
733
fmalita33ed3ad2016-02-09 08:20:18 -0800734bool SkMipMap::extractLevel(const SkSize& scaleSize, Level* levelPtr) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700735 if (nullptr == fLevels) {
reed9d93c2e2014-10-08 05:17:12 -0700736 return false;
737 }
738
fmalita33ed3ad2016-02-09 08:20:18 -0800739 SkASSERT(scaleSize.width() >= 0 && scaleSize.height() >= 0);
fmalitad10f5b32016-02-17 06:17:12 -0800740
741#ifndef SK_SUPPORT_LEGACY_ANISOTROPIC_MIPMAP_SCALE
fmalita33ed3ad2016-02-09 08:20:18 -0800742 // Use the smallest scale to match the GPU impl.
743 const SkScalar scale = SkTMin(scaleSize.width(), scaleSize.height());
fmalitad10f5b32016-02-17 06:17:12 -0800744#else
745 // Ideally we'd pick the smaller scale, to match Ganesh. But ignoring one of the
746 // scales can produce some atrocious results, so for now we use the geometric mean.
747 // (https://bugs.chromium.org/p/skia/issues/detail?id=4863)
748 const SkScalar scale = SkScalarSqrt(scaleSize.width() * scaleSize.height());
749#endif
fmalita33ed3ad2016-02-09 08:20:18 -0800750
reed7729e562015-01-16 08:35:09 -0800751 if (scale >= SK_Scalar1 || scale <= 0 || !SkScalarIsFinite(scale)) {
reed@google.comeed6f1b2013-07-18 19:53:31 +0000752 return false;
753 }
754
reed7729e562015-01-16 08:35:09 -0800755 SkScalar L = -SkScalarLog2(scale);
756 if (!SkScalarIsFinite(L)) {
757 return false;
758 }
759 SkASSERT(L >= 0);
reedc1e1faa2015-05-06 13:02:37 -0700760 int level = SkScalarFloorToInt(L);
reed7729e562015-01-16 08:35:09 -0800761
reed@google.comeed6f1b2013-07-18 19:53:31 +0000762 SkASSERT(level >= 0);
763 if (level <= 0) {
764 return false;
765 }
766
767 if (level > fCount) {
768 level = fCount;
769 }
770 if (levelPtr) {
771 *levelPtr = fLevels[level - 1];
reed6644d932016-06-10 11:41:47 -0700772 // need to augment with our colorspace
773 levelPtr->fPixmap.setColorSpace(fCS);
reed@google.comeed6f1b2013-07-18 19:53:31 +0000774 }
775 return true;
776}
reed67b09bf2016-01-16 18:50:35 -0800777
cblume61214052016-01-26 09:10:48 -0800778// Helper which extracts a pixmap from the src bitmap
reed67b09bf2016-01-16 18:50:35 -0800779//
Brian Osman7b8400d2016-11-08 17:08:54 -0500780SkMipMap* SkMipMap::Build(const SkBitmap& src, SkDestinationSurfaceColorMode colorMode,
reed6644d932016-06-10 11:41:47 -0700781 SkDiscardableFactoryProc fact) {
Mike Reed4edb5d22017-04-17 11:02:51 -0400782 SkPixmap srcPixmap;
783 if (!src.peekPixels(&srcPixmap)) {
reed67b09bf2016-01-16 18:50:35 -0800784 return nullptr;
785 }
Brian Osman7b8400d2016-11-08 17:08:54 -0500786 return Build(srcPixmap, colorMode, fact);
reed67b09bf2016-01-16 18:50:35 -0800787}
788
cblumee2412d52016-02-17 14:53:23 -0800789int SkMipMap::countLevels() const {
790 return fCount;
791}
792
793bool SkMipMap::getLevel(int index, Level* levelPtr) const {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400794 if (nullptr == fLevels) {
cblumee2412d52016-02-17 14:53:23 -0800795 return false;
796 }
797 if (index < 0) {
798 return false;
799 }
800 if (index > fCount - 1) {
801 return false;
802 }
803 if (levelPtr) {
804 *levelPtr = fLevels[index];
805 }
806 return true;
807}