OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkMipMap.h" | 8 #include "SkMipMap.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 default: | 363 default: |
364 // TODO: We could build miplevels for kIndex8 if the levels were in
8888. | 364 // TODO: We could build miplevels for kIndex8 if the levels were in
8888. |
365 // Means using more ram, but the quality would be fine. | 365 // Means using more ram, but the quality would be fine. |
366 return nullptr; | 366 return nullptr; |
367 } | 367 } |
368 | 368 |
369 if (src.width() <= 1 && src.height() <= 1) { | 369 if (src.width() <= 1 && src.height() <= 1) { |
370 return nullptr; | 370 return nullptr; |
371 } | 371 } |
372 // whip through our loop to compute the exact size needed | 372 // whip through our loop to compute the exact size needed |
373 size_t size = 0; | 373 size_t size = 0; |
374 int countLevels = ComputeLevelCount(src.width(), src.height()); | 374 int countLevels = ComputeLevelCount(src.width(), src.height()); |
375 for (int currentMipLevel = countLevels; currentMipLevel > 0; currentMipLevel
--) { | 375 for (int currentMipLevel = countLevels; currentMipLevel >= 0; currentMipLeve
l--) { |
376 SkISize mipSize = ComputeLevelSize(src.width(), src.height(), currentMip
Level); | 376 SkISize mipSize = ComputeLevelSize(src.width(), src.height(), currentMip
Level); |
377 size += SkColorTypeMinRowBytes(ct, mipSize.fWidth) * mipSize.fHeight; | 377 size += SkColorTypeMinRowBytes(ct, mipSize.fWidth) * mipSize.fHeight; |
378 } | 378 } |
379 | 379 |
380 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size); | 380 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size); |
381 if (0 == storageSize) { | 381 if (0 == storageSize) { |
382 return nullptr; | 382 return nullptr; |
383 } | 383 } |
384 | 384 |
385 SkMipMap* mipmap; | 385 SkMipMap* mipmap; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 | 495 |
496 return mipLevelCount; | 496 return mipLevelCount; |
497 } | 497 } |
498 | 498 |
499 SkISize SkMipMap::ComputeLevelSize(int baseWidth, int baseHeight, int level) { | 499 SkISize SkMipMap::ComputeLevelSize(int baseWidth, int baseHeight, int level) { |
500 if (baseWidth < 1 || baseHeight < 1) { | 500 if (baseWidth < 1 || baseHeight < 1) { |
501 return SkISize::Make(0, 0); | 501 return SkISize::Make(0, 0); |
502 } | 502 } |
503 | 503 |
504 int maxLevelCount = ComputeLevelCount(baseWidth, baseHeight); | 504 int maxLevelCount = ComputeLevelCount(baseWidth, baseHeight); |
505 if (level > maxLevelCount || level < 0) { | 505 if (level >= maxLevelCount || level < 0) { |
506 return SkISize::Make(0, 0); | 506 return SkISize::Make(0, 0); |
507 } | 507 } |
508 if (level == 0) { | |
509 return SkISize::Make(baseWidth, baseHeight); | |
510 } | |
511 | |
512 // OpenGL's spec requires that each mipmap level have height/width equal to | 508 // OpenGL's spec requires that each mipmap level have height/width equal to |
513 // max(1, floor(original_height / 2^i) | 509 // max(1, floor(original_height / 2^i) |
514 // (or original_width) where i is the mipmap level. | 510 // (or original_width) where i is the mipmap level. |
515 | 511 |
516 int width = SkTMax(1, baseWidth >> level); | 512 // SkMipMap does not include the base mip level. |
517 int height = SkTMax(1, baseHeight >> level); | 513 // For example, it contains levels 1-x instead of 0-x. |
| 514 // This is because the image used to create SkMipMap is the base level. |
| 515 // So subtract 1 from the mip level to get the index stored by SkMipMap. |
| 516 int width = SkTMax(1, baseWidth >> (level + 1)); |
| 517 int height = SkTMax(1, baseHeight >> (level + 1)); |
518 | 518 |
519 return SkISize::Make(width, height); | 519 return SkISize::Make(width, height); |
520 } | 520 } |
521 | 521 |
522 /////////////////////////////////////////////////////////////////////////////// | 522 /////////////////////////////////////////////////////////////////////////////// |
523 | 523 |
524 bool SkMipMap::extractLevel(const SkSize& scaleSize, Level* levelPtr) const { | 524 bool SkMipMap::extractLevel(const SkSize& scaleSize, Level* levelPtr) const { |
525 if (nullptr == fLevels) { | 525 if (nullptr == fLevels) { |
526 return false; | 526 return false; |
527 } | 527 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
592 return false; | 592 return false; |
593 } | 593 } |
594 if (index > fCount - 1) { | 594 if (index > fCount - 1) { |
595 return false; | 595 return false; |
596 } | 596 } |
597 if (levelPtr) { | 597 if (levelPtr) { |
598 *levelPtr = fLevels[index]; | 598 *levelPtr = fLevels[index]; |
599 } | 599 } |
600 return true; | 600 return true; |
601 } | 601 } |
OLD | NEW |