reed@google.com | dc6c8ba | 2013-07-18 21:14:04 +0000 | [diff] [blame] | 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 | |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 8 | #include "SkMipMap.h" |
| 9 | #include "SkBitmap.h" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 10 | #include "SkColorData.h" |
brianosman | 1817d28 | 2016-04-06 07:32:08 -0700 | [diff] [blame] | 11 | #include "SkHalf.h" |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 12 | #include "SkMathPriv.h" |
mtklein | 550e9b0 | 2016-01-20 11:55:51 -0800 | [diff] [blame] | 13 | #include "SkNx.h" |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 14 | #include "SkPM4fPriv.h" |
Matt Sarett | 08541e8 | 2017-03-08 16:30:18 -0500 | [diff] [blame] | 15 | #include "SkSRGB.h" |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 16 | #include "SkTypes.h" |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 17 | |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 18 | // |
| 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 | |
| 25 | struct ColorTypeFilter_8888 { |
| 26 | typedef uint32_t Type; |
mtklein | 550e9b0 | 2016-01-20 11:55:51 -0800 | [diff] [blame] | 27 | static Sk4h Expand(uint32_t x) { |
mtklein | 507ef6d | 2016-01-31 08:02:47 -0800 | [diff] [blame] | 28 | return SkNx_cast<uint16_t>(Sk4b::Load(&x)); |
mtklein | 550e9b0 | 2016-01-20 11:55:51 -0800 | [diff] [blame] | 29 | } |
| 30 | static uint32_t Compact(const Sk4h& x) { |
| 31 | uint32_t r; |
mtklein | 507ef6d | 2016-01-31 08:02:47 -0800 | [diff] [blame] | 32 | SkNx_cast<uint8_t>(x).store(&r); |
mtklein | 550e9b0 | 2016-01-20 11:55:51 -0800 | [diff] [blame] | 33 | return r; |
| 34 | } |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 35 | }; |
| 36 | |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 37 | struct ColorTypeFilter_S32 { |
| 38 | typedef uint32_t Type; |
Matt Sarett | 08541e8 | 2017-03-08 16:30:18 -0500 | [diff] [blame] | 39 | 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); |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 44 | } |
Matt Sarett | 08541e8 | 2017-03-08 16:30:18 -0500 | [diff] [blame] | 45 | 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; |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 50 | } |
| 51 | }; |
| 52 | |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 53 | struct 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 | |
| 63 | struct 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 | |
| 73 | struct 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 | |
brianosman | 1817d28 | 2016-04-06 07:32:08 -0700 | [diff] [blame] | 83 | struct ColorTypeFilter_F16 { |
| 84 | typedef uint64_t Type; // SkHalf x4 |
| 85 | static Sk4f Expand(uint64_t x) { |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 86 | return SkHalfToFloat_finite_ftz(x); |
brianosman | 1817d28 | 2016-04-06 07:32:08 -0700 | [diff] [blame] | 87 | } |
| 88 | static uint64_t Compact(const Sk4f& x) { |
msarett | 6bdbf44 | 2016-07-19 09:07:55 -0700 | [diff] [blame] | 89 | uint64_t r; |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 90 | SkFloatToHalf_finite_ftz(x).store(&r); |
msarett | 6bdbf44 | 2016-07-19 09:07:55 -0700 | [diff] [blame] | 91 | return r; |
brianosman | 1817d28 | 2016-04-06 07:32:08 -0700 | [diff] [blame] | 92 | } |
| 93 | }; |
| 94 | |
mtklein | 550e9b0 | 2016-01-20 11:55:51 -0800 | [diff] [blame] | 95 | template <typename T> T add_121(const T& a, const T& b, const T& c) { |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 96 | return a + b + b + c; |
| 97 | } |
| 98 | |
brianosman | 1817d28 | 2016-04-06 07:32:08 -0700 | [diff] [blame] | 99 | template <typename T> T shift_right(const T& x, int bits) { |
| 100 | return x >> bits; |
| 101 | } |
| 102 | |
| 103 | Sk4f shift_right(const Sk4f& x, int bits) { |
| 104 | return x * (1.0f / (1 << bits)); |
| 105 | } |
| 106 | |
| 107 | template <typename T> T shift_left(const T& x, int bits) { |
| 108 | return x << bits; |
| 109 | } |
| 110 | |
| 111 | Sk4f shift_left(const Sk4f& x, int bits) { |
| 112 | return x * (1 << bits); |
| 113 | } |
| 114 | |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 115 | // |
| 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 | // |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 121 | // 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. |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 123 | // |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 124 | // 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 | |
| 128 | template <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; |
brianosman | 1817d28 | 2016-04-06 07:32:08 -0700 | [diff] [blame] | 139 | d[i] = F::Compact(shift_right(c, 1)); |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 140 | p0 += 2; |
| 141 | p1 += 2; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | template <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); |
brianosman | 1817d28 | 2016-04-06 07:32:08 -0700 | [diff] [blame] | 158 | d[i] = F::Compact(shift_right(c, 2)); |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 159 | p0 += 2; |
| 160 | p1 += 2; |
| 161 | p2 += 2; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | template <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; |
brianosman | 1817d28 | 2016-04-06 07:32:08 -0700 | [diff] [blame] | 175 | d[i] = F::Compact(shift_right(c, 1)); |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 176 | p0 += 2; |
| 177 | } |
| 178 | } |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 179 | |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 180 | template <typename F> void downsample_2_2(void* dst, const void* src, size_t srcRB, int count) { |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 181 | SkASSERT(count > 0); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 182 | auto p0 = static_cast<const typename F::Type*>(src); |
| 183 | auto p1 = (const typename F::Type*)((const char*)p0 + srcRB); |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 184 | auto d = static_cast<typename F::Type*>(dst); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 185 | |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 186 | 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]); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 191 | |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 192 | auto c = c00 + c10 + c01 + c11; |
brianosman | 1817d28 | 2016-04-06 07:32:08 -0700 | [diff] [blame] | 193 | d[i] = F::Compact(shift_right(c, 2)); |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 194 | p0 += 2; |
| 195 | p1 += 2; |
| 196 | } |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 197 | } |
| 198 | |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 199 | template <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); |
brianosman | 1817d28 | 2016-04-06 07:32:08 -0700 | [diff] [blame] | 215 | d[i] = F::Compact(shift_right(c, 3)); |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 216 | p0 += 2; |
| 217 | p1 += 2; |
| 218 | p2 += 2; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | template <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); |
brianosman | 1817d28 | 2016-04-06 07:32:08 -0700 | [diff] [blame] | 234 | d[i] = F::Compact(shift_right(c, 2)); |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 235 | p0 += 2; |
| 236 | } |
| 237 | } |
| 238 | |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 239 | template <typename F> void downsample_3_2(void* dst, const void* src, size_t srcRB, int count) { |
| 240 | SkASSERT(count > 0); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 241 | auto p0 = static_cast<const typename F::Type*>(src); |
| 242 | auto p1 = (const typename F::Type*)((const char*)p0 + srcRB); |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 243 | auto d = static_cast<typename F::Type*>(dst); |
mtklein | 550e9b0 | 2016-01-20 11:55:51 -0800 | [diff] [blame] | 244 | |
Matt Sarett | 8a8e5fe | 2017-04-11 11:53:24 -0400 | [diff] [blame] | 245 | // 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 Sarett | 88ec28e | 2017-03-22 17:27:43 -0400 | [diff] [blame] | 252 | |
Matt Sarett | 8a8e5fe | 2017-04-11 11:53:24 -0400 | [diff] [blame] | 253 | 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)); |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 269 | p0 += 2; |
| 270 | p1 += 2; |
| 271 | } |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 272 | } |
| 273 | |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 274 | template <typename F> void downsample_3_3(void* dst, const void* src, size_t srcRB, int count) { |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 275 | SkASSERT(count > 0); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 276 | 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); |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 279 | auto d = static_cast<typename F::Type*>(dst); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 280 | |
Matt Sarett | b667fe2 | 2017-03-22 09:20:02 -0400 | [diff] [blame] | 281 | // 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 | // ... |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 289 | |
Matt Sarett | b667fe2 | 2017-03-22 09:20:02 -0400 | [diff] [blame] | 290 | 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)); |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 309 | p0 += 2; |
| 310 | p1 += 2; |
| 311 | p2 += 2; |
| 312 | } |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 316 | |
Matt Sarett | 08541e8 | 2017-03-08 16:30:18 -0500 | [diff] [blame] | 317 | // Some sRGB specific performance optimizations. |
| 318 | |
| 319 | void 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 Sarett | 8a8e5fe | 2017-04-11 11:53:24 -0400 | [diff] [blame] | 339 | p0[11] << 4 ); |
Matt Sarett | 08541e8 | 2017-03-08 16:30:18 -0500 | [diff] [blame] | 340 | 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 Sarett | 8a8e5fe | 2017-04-11 11:53:24 -0400 | [diff] [blame] | 386 | void 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 Sarett | 08541e8 | 2017-03-08 16:30:18 -0500 | [diff] [blame] | 472 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 473 | |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 474 | size_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 Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 485 | SkMipMap* SkMipMap::Build(const SkPixmap& src, SkDestinationSurfaceColorMode colorMode, |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 486 | SkDiscardableFactoryProc fact) { |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 487 | typedef void FilterProc(void*, const void* srcPtr, size_t srcRB, int count); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 488 | |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 489 | FilterProc* proc_1_2 = nullptr; |
| 490 | FilterProc* proc_1_3 = nullptr; |
| 491 | FilterProc* proc_2_1 = nullptr; |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 492 | FilterProc* proc_2_2 = nullptr; |
| 493 | FilterProc* proc_2_3 = nullptr; |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 494 | FilterProc* proc_3_1 = nullptr; |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 495 | 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 Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 500 | const bool srgbGamma = (SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware == colorMode) |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 501 | && src.info().gammaCloseToSRGB(); |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 502 | |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 503 | switch (ct) { |
| 504 | case kRGBA_8888_SkColorType: |
| 505 | case kBGRA_8888_SkColorType: |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 506 | 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 Sarett | 08541e8 | 2017-03-08 16:30:18 -0500 | [diff] [blame] | 510 | proc_2_2 = downsample_2_2_srgb; |
Matt Sarett | 8a8e5fe | 2017-04-11 11:53:24 -0400 | [diff] [blame] | 511 | proc_2_3 = downsample_2_3_srgb; |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 512 | 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 | } |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 525 | break; |
| 526 | case kRGB_565_SkColorType: |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 527 | 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>; |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 530 | proc_2_2 = downsample_2_2<ColorTypeFilter_565>; |
| 531 | proc_2_3 = downsample_2_3<ColorTypeFilter_565>; |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 532 | proc_3_1 = downsample_3_1<ColorTypeFilter_565>; |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 533 | proc_3_2 = downsample_3_2<ColorTypeFilter_565>; |
| 534 | proc_3_3 = downsample_3_3<ColorTypeFilter_565>; |
| 535 | break; |
| 536 | case kARGB_4444_SkColorType: |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 537 | 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>; |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 540 | proc_2_2 = downsample_2_2<ColorTypeFilter_4444>; |
| 541 | proc_2_3 = downsample_2_3<ColorTypeFilter_4444>; |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 542 | proc_3_1 = downsample_3_1<ColorTypeFilter_4444>; |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 543 | 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: |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 548 | 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>; |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 551 | proc_2_2 = downsample_2_2<ColorTypeFilter_8>; |
| 552 | proc_2_3 = downsample_2_3<ColorTypeFilter_8>; |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 553 | proc_3_1 = downsample_3_1<ColorTypeFilter_8>; |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 554 | proc_3_2 = downsample_3_2<ColorTypeFilter_8>; |
| 555 | proc_3_3 = downsample_3_3<ColorTypeFilter_8>; |
| 556 | break; |
brianosman | 1817d28 | 2016-04-06 07:32:08 -0700 | [diff] [blame] | 557 | 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; |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 567 | 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 | |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 573 | if (src.width() <= 1 && src.height() <= 1) { |
| 574 | return nullptr; |
| 575 | } |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 576 | // whip through our loop to compute the exact size needed |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 577 | size_t size = 0; |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 578 | int countLevels = ComputeLevelCount(src.width(), src.height()); |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 579 | for (int currentMipLevel = countLevels; currentMipLevel >= 0; currentMipLevel--) { |
cblume | 44e09ec | 2016-06-03 11:59:50 -0700 | [diff] [blame] | 580 | SkISize mipSize = ComputeLevelSize(src.width(), src.height(), currentMipLevel); |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 581 | size += SkColorTypeMinRowBytes(ct, mipSize.fWidth) * mipSize.fHeight; |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 582 | } |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 583 | |
| 584 | size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size); |
| 585 | if (0 == storageSize) { |
| 586 | return nullptr; |
| 587 | } |
| 588 | |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 589 | 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 |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 601 | mipmap->fCS = sk_ref_sp(src.info().colorSpace()); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 602 | mipmap->fCount = countLevels; |
| 603 | mipmap->fLevels = (Level*)mipmap->writable_data(); |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 604 | SkASSERT(mipmap->fLevels); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 605 | |
| 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; |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 612 | SkPixmap srcPM(src); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 613 | |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 614 | for (int i = 0; i < countLevels; ++i) { |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 615 | FilterProc* proc; |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 616 | 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 | } |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 633 | } |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 634 | } 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 |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 642 | proc = proc_2_2; |
| 643 | } |
| 644 | } |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 645 | width = SkTMax(1, width >> 1); |
| 646 | height = SkTMax(1, height >> 1); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 647 | rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width)); |
| 648 | |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 649 | // 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). |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 652 | new (&levels[i].fPixmap) SkPixmap(SkImageInfo::Make(width, height, ct, at), addr, rowBytes); |
fmalita | 921d7ac | 2016-01-22 11:45:39 -0800 | [diff] [blame] | 653 | levels[i].fScale = SkSize::Make(SkIntToScalar(width) / src.width(), |
| 654 | SkIntToScalar(height) / src.height()); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 655 | |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 656 | const SkPixmap& dstPM = levels[i].fPixmap; |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 657 | const void* srcBasePtr = srcPM.addr(); |
| 658 | void* dstBasePtr = dstPM.writable_addr(); |
| 659 | |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 660 | const size_t srcRB = srcPM.rowBytes(); |
| 661 | for (int y = 0; y < height; y++) { |
reed | 326253e | 2016-01-16 09:23:48 -0800 | [diff] [blame] | 662 | proc(dstBasePtr, srcBasePtr, srcRB, width); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 663 | srcBasePtr = (char*)srcBasePtr + srcRB * 2; // jump two rows |
| 664 | dstBasePtr = (char*)dstBasePtr + dstPM.rowBytes(); |
| 665 | } |
| 666 | srcPM = dstPM; |
| 667 | addr += height * rowBytes; |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 668 | } |
| 669 | SkASSERT(addr == baseAddr + size); |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 670 | |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 671 | SkASSERT(mipmap->fLevels); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 672 | return mipmap; |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 673 | } |
| 674 | |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 675 | int SkMipMap::ComputeLevelCount(int baseWidth, int baseHeight) { |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 676 | if (baseWidth < 1 || baseHeight < 1) { |
| 677 | return 0; |
| 678 | } |
| 679 | |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 680 | // 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. |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 684 | |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 685 | const int largestAxis = SkTMax(baseWidth, baseHeight); |
| 686 | if (largestAxis < 2) { |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 687 | // SkMipMap::Build requires a minimum size of 2. |
| 688 | return 0; |
| 689 | } |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 690 | const int leadingZeros = SkCLZ(static_cast<uint32_t>(largestAxis)); |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 691 | // 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 | |
cblume | 44e09ec | 2016-06-03 11:59:50 -0700 | [diff] [blame] | 709 | SkISize SkMipMap::ComputeLevelSize(int baseWidth, int baseHeight, int level) { |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 710 | if (baseWidth < 1 || baseHeight < 1) { |
cblume | 44e09ec | 2016-06-03 11:59:50 -0700 | [diff] [blame] | 711 | return SkISize::Make(0, 0); |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | int maxLevelCount = ComputeLevelCount(baseWidth, baseHeight); |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 715 | if (level >= maxLevelCount || level < 0) { |
cblume | 44e09ec | 2016-06-03 11:59:50 -0700 | [diff] [blame] | 716 | return SkISize::Make(0, 0); |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 717 | } |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 718 | // 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 | |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 722 | // 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)); |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 728 | |
cblume | 44e09ec | 2016-06-03 11:59:50 -0700 | [diff] [blame] | 729 | return SkISize::Make(width, height); |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 730 | } |
| 731 | |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 732 | /////////////////////////////////////////////////////////////////////////////// |
| 733 | |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 734 | bool SkMipMap::extractLevel(const SkSize& scaleSize, Level* levelPtr) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 735 | if (nullptr == fLevels) { |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 736 | return false; |
| 737 | } |
| 738 | |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 739 | SkASSERT(scaleSize.width() >= 0 && scaleSize.height() >= 0); |
fmalita | d10f5b3 | 2016-02-17 06:17:12 -0800 | [diff] [blame] | 740 | |
| 741 | #ifndef SK_SUPPORT_LEGACY_ANISOTROPIC_MIPMAP_SCALE |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 742 | // Use the smallest scale to match the GPU impl. |
| 743 | const SkScalar scale = SkTMin(scaleSize.width(), scaleSize.height()); |
fmalita | d10f5b3 | 2016-02-17 06:17:12 -0800 | [diff] [blame] | 744 | #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 |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 750 | |
reed | 7729e56 | 2015-01-16 08:35:09 -0800 | [diff] [blame] | 751 | if (scale >= SK_Scalar1 || scale <= 0 || !SkScalarIsFinite(scale)) { |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 752 | return false; |
| 753 | } |
| 754 | |
reed | 7729e56 | 2015-01-16 08:35:09 -0800 | [diff] [blame] | 755 | SkScalar L = -SkScalarLog2(scale); |
| 756 | if (!SkScalarIsFinite(L)) { |
| 757 | return false; |
| 758 | } |
| 759 | SkASSERT(L >= 0); |
reed | c1e1faa | 2015-05-06 13:02:37 -0700 | [diff] [blame] | 760 | int level = SkScalarFloorToInt(L); |
reed | 7729e56 | 2015-01-16 08:35:09 -0800 | [diff] [blame] | 761 | |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 762 | 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]; |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 772 | // need to augment with our colorspace |
| 773 | levelPtr->fPixmap.setColorSpace(fCS); |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 774 | } |
| 775 | return true; |
| 776 | } |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 777 | |
cblume | 6121405 | 2016-01-26 09:10:48 -0800 | [diff] [blame] | 778 | // Helper which extracts a pixmap from the src bitmap |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 779 | // |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 780 | SkMipMap* SkMipMap::Build(const SkBitmap& src, SkDestinationSurfaceColorMode colorMode, |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 781 | SkDiscardableFactoryProc fact) { |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 782 | SkPixmap srcPixmap; |
| 783 | if (!src.peekPixels(&srcPixmap)) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 784 | return nullptr; |
| 785 | } |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 786 | return Build(srcPixmap, colorMode, fact); |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 787 | } |
| 788 | |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 789 | int SkMipMap::countLevels() const { |
| 790 | return fCount; |
| 791 | } |
| 792 | |
| 793 | bool SkMipMap::getLevel(int index, Level* levelPtr) const { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 794 | if (nullptr == fLevels) { |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 795 | 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 | } |