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 "SkMipMap.h" 9 #include "SkBitmap.h" 10 #include "SkColorPriv.h" 11 #include "SkHalf.h" 12 #include "SkMathPriv.h" 13 #include "SkNx.h" 14 #include "SkPM4fPriv.h" 15 #include "SkSRGB.h" 16 #include "SkTypes.h" 17 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; 27 static Sk4h Expand(uint32_t x) { 28 return SkNx_cast<uint16_t>(Sk4b::Load(&x)); 29 } 30 static uint32_t Compact(const Sk4h& x) { 31 uint32_t r; 32 SkNx_cast<uint8_t>(x).store(&r); 33 return r; 34 } 35 }; 36 37 struct ColorTypeFilter_S32 { 38 typedef uint32_t Type; 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); 44 } 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; 50 } 51 }; 52 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 83 struct ColorTypeFilter_F16 { 84 typedef uint64_t Type; // SkHalf x4 85 static Sk4f Expand(uint64_t x) { 86 return SkHalfToFloat_finite_ftz(x); 87 } 88 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 template <typename T> T add_121(const T& a, const T& b, const T& c) { 96 return a + b + b + c; 97 } 98 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 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 // 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. 123 // 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; 139 d[i] = F::Compact(shift_right(c, 1)); 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); 158 d[i] = F::Compact(shift_right(c, 2)); 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; 175 d[i] = F::Compact(shift_right(c, 1)); 176 p0 += 2; 177 } 178 } 179 180 template <typename F> void downsample_2_2(void* dst, const void* src, size_t srcRB, int count) { 181 SkASSERT(count > 0); 182 auto p0 = static_cast<const typename F::Type*>(src); 183 auto p1 = (const typename F::Type*)((const char*)p0 + srcRB); 184 auto d = static_cast<typename F::Type*>(dst); 185 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]); 191 192 auto c = c00 + c10 + c01 + c11; 193 d[i] = F::Compact(shift_right(c, 2)); 194 p0 += 2; 195 p1 += 2; 196 } 197 } 198 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); 215 d[i] = F::Compact(shift_right(c, 3)); 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); 234 d[i] = F::Compact(shift_right(c, 2)); 235 p0 += 2; 236 } 237 } 238 239 template <typename F> void downsample_3_2(void* dst, const void* src, size_t srcRB, int count) { 240 SkASSERT(count > 0); 241 auto p0 = static_cast<const typename F::Type*>(src); 242 auto p1 = (const typename F::Type*)((const char*)p0 + srcRB); 243 auto d = static_cast<typename F::Type*>(dst); 244 245 auto c02 = F::Expand(p0[0]); 246 auto c12 = F::Expand(p1[0]); 247 for (int i = 0; i < count; ++i) { 248 auto c00 = c02; 249 auto c01 = F::Expand(p0[1]); 250 c02 = F::Expand(p0[2]); 251 auto c10 = c12; 252 auto c11 = F::Expand(p1[1]); 253 c12 = F::Expand(p1[2]); 254 255 auto c = add_121(c00, c01, c02) + add_121(c10, c11, c12); 256 d[i] = F::Compact(shift_right(c, 3)); 257 p0 += 2; 258 p1 += 2; 259 } 260 } 261 262 template <typename F> void downsample_3_3(void* dst, const void* src, size_t srcRB, int count) { 263 SkASSERT(count > 0); 264 auto p0 = static_cast<const typename F::Type*>(src); 265 auto p1 = (const typename F::Type*)((const char*)p0 + srcRB); 266 auto p2 = (const typename F::Type*)((const char*)p1 + srcRB); 267 auto d = static_cast<typename F::Type*>(dst); 268 269 // Given pixels: 270 // a0 b0 c0 d0 e0 ... 271 // a1 b1 c1 d1 e1 ... 272 // a2 b2 c2 d2 e2 ... 273 // We want: 274 // (a0 + 2*b0 + c0 + 2*a1 + 4*b1 + 2*c1 + a2 + 2*b2 + c2) / 16 275 // (c0 + 2*d0 + e0 + 2*c1 + 4*d1 + 2*e1 + c2 + 2*d2 + e2) / 16 276 // ... 277 278 auto c0 = F::Expand(p0[0]); 279 auto c1 = F::Expand(p1[0]); 280 auto c2 = F::Expand(p2[0]); 281 auto c = add_121(c0, c1, c2); 282 for (int i = 0; i < count; ++i) { 283 auto a = c; 284 285 auto b0 = F::Expand(p0[1]); 286 auto b1 = F::Expand(p1[1]); 287 auto b2 = F::Expand(p2[1]); 288 auto b = shift_left(add_121(b0, b1, b2), 1); 289 290 c0 = F::Expand(p0[2]); 291 c1 = F::Expand(p1[2]); 292 c2 = F::Expand(p2[2]); 293 c = add_121(c0, c1, c2); 294 295 auto sum = a + b + c; 296 d[i] = F::Compact(shift_right(sum, 4)); 297 p0 += 2; 298 p1 += 2; 299 p2 += 2; 300 } 301 } 302 303 /////////////////////////////////////////////////////////////////////////////////////////////////// 304 305 // Some sRGB specific performance optimizations. 306 307 void downsample_2_2_srgb(void* dst, const void* src, size_t srcRB, int count) { 308 const uint8_t* p0 = ((const uint8_t*) src); 309 const uint8_t* p1 = ((const uint8_t*) src) + srcRB; 310 uint8_t* d = (uint8_t*) dst; 311 312 // Given pixels: 313 // a0 b0 c0 d0 ... 314 // a1 b1 c1 d1 ... 315 // We want: 316 // (a0 + b0 + a1 + b1) / 4 317 // (c0 + d0 + c1 + d1) / 4 318 // ... 319 while (count >= 2) { 320 Sk8h a0c0 = Sk8h(sk_linear12_from_srgb[p0[ 0]], 321 sk_linear12_from_srgb[p0[ 1]], 322 sk_linear12_from_srgb[p0[ 2]], 323 p0[ 3] << 4 , 324 sk_linear12_from_srgb[p0[ 8]], 325 sk_linear12_from_srgb[p0[ 9]], 326 sk_linear12_from_srgb[p0[10]], 327 p0[11] << 4 ); 328 Sk8h b0d0 = Sk8h(sk_linear12_from_srgb[p0[ 4]], 329 sk_linear12_from_srgb[p0[ 5]], 330 sk_linear12_from_srgb[p0[ 6]], 331 p0[ 7] << 4 , 332 sk_linear12_from_srgb[p0[12]], 333 sk_linear12_from_srgb[p0[13]], 334 sk_linear12_from_srgb[p0[14]], 335 p0[15] << 4 ); 336 Sk8h a1c1 = Sk8h(sk_linear12_from_srgb[p1[ 0]], 337 sk_linear12_from_srgb[p1[ 1]], 338 sk_linear12_from_srgb[p1[ 2]], 339 p1[ 3] << 4 , 340 sk_linear12_from_srgb[p1[ 8]], 341 sk_linear12_from_srgb[p1[ 9]], 342 sk_linear12_from_srgb[p1[10]], 343 p1[11] << 4 ); 344 Sk8h b1d1 = Sk8h(sk_linear12_from_srgb[p1[ 4]], 345 sk_linear12_from_srgb[p1[ 5]], 346 sk_linear12_from_srgb[p1[ 6]], 347 p1[ 7] << 4 , 348 sk_linear12_from_srgb[p1[12]], 349 sk_linear12_from_srgb[p1[13]], 350 sk_linear12_from_srgb[p1[14]], 351 p1[15] << 4 ); 352 353 Sk8h avg = (a0c0 + b0d0 + a1c1 + b1d1) >> 2; 354 d[0] = sk_linear12_to_srgb[avg[0]]; 355 d[1] = sk_linear12_to_srgb[avg[1]]; 356 d[2] = sk_linear12_to_srgb[avg[2]]; 357 d[3] = avg[3] >> 4; 358 d[4] = sk_linear12_to_srgb[avg[4]]; 359 d[5] = sk_linear12_to_srgb[avg[5]]; 360 d[6] = sk_linear12_to_srgb[avg[6]]; 361 d[7] = avg[7] >> 4; 362 363 p0 += 16; 364 p1 += 16; 365 d += 8; 366 count -= 2; 367 } 368 369 if (count) { 370 downsample_2_2<ColorTypeFilter_S32>(d, p0, srcRB, count); 371 } 372 } 373 374 /////////////////////////////////////////////////////////////////////////////////////////////////// 375 376 size_t SkMipMap::AllocLevelsSize(int levelCount, size_t pixelSize) { 377 if (levelCount < 0) { 378 return 0; 379 } 380 int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize; 381 if (!sk_64_isS32(size)) { 382 return 0; 383 } 384 return sk_64_asS32(size); 385 } 386 387 SkMipMap* SkMipMap::Build(const SkPixmap& src, SkDestinationSurfaceColorMode colorMode, 388 SkDiscardableFactoryProc fact) { 389 typedef void FilterProc(void*, const void* srcPtr, size_t srcRB, int count); 390 391 FilterProc* proc_1_2 = nullptr; 392 FilterProc* proc_1_3 = nullptr; 393 FilterProc* proc_2_1 = nullptr; 394 FilterProc* proc_2_2 = nullptr; 395 FilterProc* proc_2_3 = nullptr; 396 FilterProc* proc_3_1 = nullptr; 397 FilterProc* proc_3_2 = nullptr; 398 FilterProc* proc_3_3 = nullptr; 399 400 const SkColorType ct = src.colorType(); 401 const SkAlphaType at = src.alphaType(); 402 const bool srgbGamma = (SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware == colorMode) 403 && src.info().gammaCloseToSRGB(); 404 405 switch (ct) { 406 case kRGBA_8888_SkColorType: 407 case kBGRA_8888_SkColorType: 408 if (srgbGamma) { 409 proc_1_2 = downsample_1_2<ColorTypeFilter_S32>; 410 proc_1_3 = downsample_1_3<ColorTypeFilter_S32>; 411 proc_2_1 = downsample_2_1<ColorTypeFilter_S32>; 412 proc_2_2 = downsample_2_2_srgb; 413 proc_2_3 = downsample_2_3<ColorTypeFilter_S32>; 414 proc_3_1 = downsample_3_1<ColorTypeFilter_S32>; 415 proc_3_2 = downsample_3_2<ColorTypeFilter_S32>; 416 proc_3_3 = downsample_3_3<ColorTypeFilter_S32>; 417 } else { 418 proc_1_2 = downsample_1_2<ColorTypeFilter_8888>; 419 proc_1_3 = downsample_1_3<ColorTypeFilter_8888>; 420 proc_2_1 = downsample_2_1<ColorTypeFilter_8888>; 421 proc_2_2 = downsample_2_2<ColorTypeFilter_8888>; 422 proc_2_3 = downsample_2_3<ColorTypeFilter_8888>; 423 proc_3_1 = downsample_3_1<ColorTypeFilter_8888>; 424 proc_3_2 = downsample_3_2<ColorTypeFilter_8888>; 425 proc_3_3 = downsample_3_3<ColorTypeFilter_8888>; 426 } 427 break; 428 case kRGB_565_SkColorType: 429 proc_1_2 = downsample_1_2<ColorTypeFilter_565>; 430 proc_1_3 = downsample_1_3<ColorTypeFilter_565>; 431 proc_2_1 = downsample_2_1<ColorTypeFilter_565>; 432 proc_2_2 = downsample_2_2<ColorTypeFilter_565>; 433 proc_2_3 = downsample_2_3<ColorTypeFilter_565>; 434 proc_3_1 = downsample_3_1<ColorTypeFilter_565>; 435 proc_3_2 = downsample_3_2<ColorTypeFilter_565>; 436 proc_3_3 = downsample_3_3<ColorTypeFilter_565>; 437 break; 438 case kARGB_4444_SkColorType: 439 proc_1_2 = downsample_1_2<ColorTypeFilter_4444>; 440 proc_1_3 = downsample_1_3<ColorTypeFilter_4444>; 441 proc_2_1 = downsample_2_1<ColorTypeFilter_4444>; 442 proc_2_2 = downsample_2_2<ColorTypeFilter_4444>; 443 proc_2_3 = downsample_2_3<ColorTypeFilter_4444>; 444 proc_3_1 = downsample_3_1<ColorTypeFilter_4444>; 445 proc_3_2 = downsample_3_2<ColorTypeFilter_4444>; 446 proc_3_3 = downsample_3_3<ColorTypeFilter_4444>; 447 break; 448 case kAlpha_8_SkColorType: 449 case kGray_8_SkColorType: 450 proc_1_2 = downsample_1_2<ColorTypeFilter_8>; 451 proc_1_3 = downsample_1_3<ColorTypeFilter_8>; 452 proc_2_1 = downsample_2_1<ColorTypeFilter_8>; 453 proc_2_2 = downsample_2_2<ColorTypeFilter_8>; 454 proc_2_3 = downsample_2_3<ColorTypeFilter_8>; 455 proc_3_1 = downsample_3_1<ColorTypeFilter_8>; 456 proc_3_2 = downsample_3_2<ColorTypeFilter_8>; 457 proc_3_3 = downsample_3_3<ColorTypeFilter_8>; 458 break; 459 case kRGBA_F16_SkColorType: 460 proc_1_2 = downsample_1_2<ColorTypeFilter_F16>; 461 proc_1_3 = downsample_1_3<ColorTypeFilter_F16>; 462 proc_2_1 = downsample_2_1<ColorTypeFilter_F16>; 463 proc_2_2 = downsample_2_2<ColorTypeFilter_F16>; 464 proc_2_3 = downsample_2_3<ColorTypeFilter_F16>; 465 proc_3_1 = downsample_3_1<ColorTypeFilter_F16>; 466 proc_3_2 = downsample_3_2<ColorTypeFilter_F16>; 467 proc_3_3 = downsample_3_3<ColorTypeFilter_F16>; 468 break; 469 default: 470 // TODO: We could build miplevels for kIndex8 if the levels were in 8888. 471 // Means using more ram, but the quality would be fine. 472 return nullptr; 473 } 474 475 if (src.width() <= 1 && src.height() <= 1) { 476 return nullptr; 477 } 478 // whip through our loop to compute the exact size needed 479 size_t size = 0; 480 int countLevels = ComputeLevelCount(src.width(), src.height()); 481 for (int currentMipLevel = countLevels; currentMipLevel >= 0; currentMipLevel--) { 482 SkISize mipSize = ComputeLevelSize(src.width(), src.height(), currentMipLevel); 483 size += SkColorTypeMinRowBytes(ct, mipSize.fWidth) * mipSize.fHeight; 484 } 485 486 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size); 487 if (0 == storageSize) { 488 return nullptr; 489 } 490 491 SkMipMap* mipmap; 492 if (fact) { 493 SkDiscardableMemory* dm = fact(storageSize); 494 if (nullptr == dm) { 495 return nullptr; 496 } 497 mipmap = new SkMipMap(storageSize, dm); 498 } else { 499 mipmap = new SkMipMap(sk_malloc_throw(storageSize), storageSize); 500 } 501 502 // init 503 mipmap->fCS = sk_ref_sp(src.info().colorSpace()); 504 mipmap->fCount = countLevels; 505 mipmap->fLevels = (Level*)mipmap->writable_data(); 506 SkASSERT(mipmap->fLevels); 507 508 Level* levels = mipmap->fLevels; 509 uint8_t* baseAddr = (uint8_t*)&levels[countLevels]; 510 uint8_t* addr = baseAddr; 511 int width = src.width(); 512 int height = src.height(); 513 uint32_t rowBytes; 514 SkPixmap srcPM(src); 515 516 for (int i = 0; i < countLevels; ++i) { 517 FilterProc* proc; 518 if (height & 1) { 519 if (height == 1) { // src-height is 1 520 if (width & 1) { // src-width is 3 521 proc = proc_3_1; 522 } else { // src-width is 2 523 proc = proc_2_1; 524 } 525 } else { // src-height is 3 526 if (width & 1) { 527 if (width == 1) { // src-width is 1 528 proc = proc_1_3; 529 } else { // src-width is 3 530 proc = proc_3_3; 531 } 532 } else { // src-width is 2 533 proc = proc_2_3; 534 } 535 } 536 } else { // src-height is 2 537 if (width & 1) { 538 if (width == 1) { // src-width is 1 539 proc = proc_1_2; 540 } else { // src-width is 3 541 proc = proc_3_2; 542 } 543 } else { // src-width is 2 544 proc = proc_2_2; 545 } 546 } 547 width = SkTMax(1, width >> 1); 548 height = SkTMax(1, height >> 1); 549 rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width)); 550 551 // We make the Info w/o any colorspace, since that storage is not under our control, and 552 // will not be deleted in a controlled fashion. When the caller is given the pixmap for 553 // a given level, we augment this pixmap with fCS (which we do manage). 554 new (&levels[i].fPixmap) SkPixmap(SkImageInfo::Make(width, height, ct, at), addr, rowBytes); 555 levels[i].fScale = SkSize::Make(SkIntToScalar(width) / src.width(), 556 SkIntToScalar(height) / src.height()); 557 558 const SkPixmap& dstPM = levels[i].fPixmap; 559 const void* srcBasePtr = srcPM.addr(); 560 void* dstBasePtr = dstPM.writable_addr(); 561 562 const size_t srcRB = srcPM.rowBytes(); 563 for (int y = 0; y < height; y++) { 564 proc(dstBasePtr, srcBasePtr, srcRB, width); 565 srcBasePtr = (char*)srcBasePtr + srcRB * 2; // jump two rows 566 dstBasePtr = (char*)dstBasePtr + dstPM.rowBytes(); 567 } 568 srcPM = dstPM; 569 addr += height * rowBytes; 570 } 571 SkASSERT(addr == baseAddr + size); 572 573 SkASSERT(mipmap->fLevels); 574 return mipmap; 575 } 576 577 int SkMipMap::ComputeLevelCount(int baseWidth, int baseHeight) { 578 if (baseWidth < 1 || baseHeight < 1) { 579 return 0; 580 } 581 582 // OpenGL's spec requires that each mipmap level have height/width equal to 583 // max(1, floor(original_height / 2^i) 584 // (or original_width) where i is the mipmap level. 585 // Continue scaling down until both axes are size 1. 586 587 const int largestAxis = SkTMax(baseWidth, baseHeight); 588 if (largestAxis < 2) { 589 // SkMipMap::Build requires a minimum size of 2. 590 return 0; 591 } 592 const int leadingZeros = SkCLZ(static_cast<uint32_t>(largestAxis)); 593 // If the value 00011010 has 3 leading 0s then it has 5 significant bits 594 // (the bits which are not leading zeros) 595 const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros; 596 // This is making the assumption that the size of a byte is 8 bits 597 // and that sizeof(uint32_t)'s implementation-defined behavior is 4. 598 int mipLevelCount = significantBits; 599 600 // SkMipMap does not include the base mip level. 601 // For example, it contains levels 1-x instead of 0-x. 602 // This is because the image used to create SkMipMap is the base level. 603 // So subtract 1 from the mip level count. 604 if (mipLevelCount > 0) { 605 --mipLevelCount; 606 } 607 608 return mipLevelCount; 609 } 610 611 SkISize SkMipMap::ComputeLevelSize(int baseWidth, int baseHeight, int level) { 612 if (baseWidth < 1 || baseHeight < 1) { 613 return SkISize::Make(0, 0); 614 } 615 616 int maxLevelCount = ComputeLevelCount(baseWidth, baseHeight); 617 if (level >= maxLevelCount || level < 0) { 618 return SkISize::Make(0, 0); 619 } 620 // OpenGL's spec requires that each mipmap level have height/width equal to 621 // max(1, floor(original_height / 2^i) 622 // (or original_width) where i is the mipmap level. 623 624 // SkMipMap does not include the base mip level. 625 // For example, it contains levels 1-x instead of 0-x. 626 // This is because the image used to create SkMipMap is the base level. 627 // So subtract 1 from the mip level to get the index stored by SkMipMap. 628 int width = SkTMax(1, baseWidth >> (level + 1)); 629 int height = SkTMax(1, baseHeight >> (level + 1)); 630 631 return SkISize::Make(width, height); 632 } 633 634 /////////////////////////////////////////////////////////////////////////////// 635 636 bool SkMipMap::extractLevel(const SkSize& scaleSize, Level* levelPtr) const { 637 if (nullptr == fLevels) { 638 return false; 639 } 640 641 SkASSERT(scaleSize.width() >= 0 && scaleSize.height() >= 0); 642 643 #ifndef SK_SUPPORT_LEGACY_ANISOTROPIC_MIPMAP_SCALE 644 // Use the smallest scale to match the GPU impl. 645 const SkScalar scale = SkTMin(scaleSize.width(), scaleSize.height()); 646 #else 647 // Ideally we'd pick the smaller scale, to match Ganesh. But ignoring one of the 648 // scales can produce some atrocious results, so for now we use the geometric mean. 649 // (https://bugs.chromium.org/p/skia/issues/detail?id=4863) 650 const SkScalar scale = SkScalarSqrt(scaleSize.width() * scaleSize.height()); 651 #endif 652 653 if (scale >= SK_Scalar1 || scale <= 0 || !SkScalarIsFinite(scale)) { 654 return false; 655 } 656 657 SkScalar L = -SkScalarLog2(scale); 658 if (!SkScalarIsFinite(L)) { 659 return false; 660 } 661 SkASSERT(L >= 0); 662 int level = SkScalarFloorToInt(L); 663 664 SkASSERT(level >= 0); 665 if (level <= 0) { 666 return false; 667 } 668 669 if (level > fCount) { 670 level = fCount; 671 } 672 if (levelPtr) { 673 *levelPtr = fLevels[level - 1]; 674 // need to augment with our colorspace 675 levelPtr->fPixmap.setColorSpace(fCS); 676 } 677 return true; 678 } 679 680 // Helper which extracts a pixmap from the src bitmap 681 // 682 SkMipMap* SkMipMap::Build(const SkBitmap& src, SkDestinationSurfaceColorMode colorMode, 683 SkDiscardableFactoryProc fact) { 684 SkAutoPixmapUnlock srcUnlocker; 685 if (!src.requestLock(&srcUnlocker)) { 686 return nullptr; 687 } 688 const SkPixmap& srcPixmap = srcUnlocker.pixmap(); 689 // Try to catch where we might have returned nullptr for src crbug.com/492818 690 if (nullptr == srcPixmap.addr()) { 691 sk_throw(); 692 } 693 return Build(srcPixmap, colorMode, fact); 694 } 695 696 int SkMipMap::countLevels() const { 697 return fCount; 698 } 699 700 bool SkMipMap::getLevel(int index, Level* levelPtr) const { 701 if (NULL == fLevels) { 702 return false; 703 } 704 if (index < 0) { 705 return false; 706 } 707 if (index > fCount - 1) { 708 return false; 709 } 710 if (levelPtr) { 711 *levelPtr = fLevels[index]; 712 } 713 return true; 714 } 715