MipMapTest.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include "include/core/SkBitmap.h"
  8. #include "include/utils/SkRandom.h"
  9. #include "src/core/SkMipMap.h"
  10. #include "tests/Test.h"
  11. static void make_bitmap(SkBitmap* bm, int width, int height) {
  12. bm->allocN32Pixels(width, height);
  13. bm->eraseColor(SK_ColorWHITE);
  14. }
  15. DEF_TEST(MipMap, reporter) {
  16. SkBitmap bm;
  17. SkRandom rand;
  18. for (int i = 0; i < 500; ++i) {
  19. int width = 1 + rand.nextU() % 1000;
  20. int height = 1 + rand.nextU() % 1000;
  21. make_bitmap(&bm, width, height);
  22. sk_sp<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
  23. REPORTER_ASSERT(reporter, mm->countLevels() == SkMipMap::ComputeLevelCount(width, height));
  24. REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1, SK_Scalar1),
  25. nullptr));
  26. REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1 * 2, SK_Scalar1 * 2),
  27. nullptr));
  28. SkMipMap::Level prevLevel;
  29. sk_bzero(&prevLevel, sizeof(prevLevel));
  30. SkScalar scale = SK_Scalar1;
  31. for (int j = 0; j < 30; ++j) {
  32. scale = scale * 2 / 3;
  33. SkMipMap::Level level;
  34. if (mm->extractLevel(SkSize::Make(scale, scale), &level)) {
  35. REPORTER_ASSERT(reporter, level.fPixmap.addr());
  36. REPORTER_ASSERT(reporter, level.fPixmap.width() > 0);
  37. REPORTER_ASSERT(reporter, level.fPixmap.height() > 0);
  38. REPORTER_ASSERT(reporter, (int)level.fPixmap.rowBytes() >= level.fPixmap.width() * 4);
  39. if (prevLevel.fPixmap.addr()) {
  40. REPORTER_ASSERT(reporter, level.fPixmap.width() <= prevLevel.fPixmap.width());
  41. REPORTER_ASSERT(reporter, level.fPixmap.height() <= prevLevel.fPixmap.height());
  42. }
  43. prevLevel = level;
  44. }
  45. }
  46. }
  47. }
  48. static void test_mipmap_generation(int width, int height, int expectedMipLevelCount,
  49. skiatest::Reporter* reporter) {
  50. SkBitmap bm;
  51. bm.allocN32Pixels(width, height);
  52. bm.eraseColor(SK_ColorWHITE);
  53. sk_sp<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
  54. const int mipLevelCount = mm->countLevels();
  55. REPORTER_ASSERT(reporter, mipLevelCount == expectedMipLevelCount);
  56. REPORTER_ASSERT(reporter, mipLevelCount == SkMipMap::ComputeLevelCount(width, height));
  57. for (int i = 0; i < mipLevelCount; ++i) {
  58. SkMipMap::Level level;
  59. REPORTER_ASSERT(reporter, mm->getLevel(i, &level));
  60. // Make sure the mipmaps contain valid data and that the sizes are correct
  61. REPORTER_ASSERT(reporter, level.fPixmap.addr());
  62. SkISize size = SkMipMap::ComputeLevelSize(width, height, i);
  63. REPORTER_ASSERT(reporter, level.fPixmap.width() == size.width());
  64. REPORTER_ASSERT(reporter, level.fPixmap.height() == size.height());
  65. // + 1 because SkMipMap does not include the base mipmap level.
  66. int twoToTheMipLevel = 1 << (i + 1);
  67. int currentWidth = width / twoToTheMipLevel;
  68. int currentHeight = height / twoToTheMipLevel;
  69. REPORTER_ASSERT(reporter, level.fPixmap.width() == currentWidth);
  70. REPORTER_ASSERT(reporter, level.fPixmap.height() == currentHeight);
  71. }
  72. }
  73. DEF_TEST(MipMap_DirectLevelAccess, reporter) {
  74. // create mipmap with invalid size
  75. {
  76. // SkMipMap current requires the dimensions be greater than 2x2
  77. SkBitmap bm;
  78. bm.allocN32Pixels(1, 1);
  79. bm.eraseColor(SK_ColorWHITE);
  80. sk_sp<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
  81. REPORTER_ASSERT(reporter, mm == nullptr);
  82. }
  83. // check small mipmap's count and levels
  84. // There should be 5 mipmap levels generated:
  85. // 16x16, 8x8, 4x4, 2x2, 1x1
  86. test_mipmap_generation(32, 32, 5, reporter);
  87. // check large mipmap's count and levels
  88. // There should be 9 mipmap levels generated:
  89. // 500x500, 250x250, 125x125, 62x62, 31x31, 15x15, 7x7, 3x3, 1x1
  90. test_mipmap_generation(1000, 1000, 9, reporter);
  91. }
  92. struct LevelCountScenario {
  93. int fWidth;
  94. int fHeight;
  95. int fExpectedLevelCount;
  96. };
  97. DEF_TEST(MipMap_ComputeLevelCount, reporter) {
  98. const LevelCountScenario tests[] = {
  99. // Test mipmaps with negative sizes
  100. {-100, 100, 0},
  101. {100, -100, 0},
  102. {-100, -100, 0},
  103. // Test mipmaps with 0, 1, 2 as dimensions
  104. // (SkMipMap::Build requires a min size of 1)
  105. //
  106. // 0
  107. {0, 100, 0},
  108. {100, 0, 0},
  109. {0, 0, 0},
  110. // 1
  111. {1, 100, 6},
  112. {100, 1, 6},
  113. {1, 1, 0},
  114. // 2
  115. {2, 100, 6},
  116. {100, 2, 6},
  117. {2, 2, 1},
  118. // Test a handful of boundaries such as 63x63 and 64x64
  119. {63, 63, 5},
  120. {64, 64, 6},
  121. {127, 127, 6},
  122. {128, 128, 7},
  123. {255, 255, 7},
  124. {256, 256, 8},
  125. // Test different dimensions, such as 256x64
  126. {64, 129, 7},
  127. {255, 32, 7},
  128. {500, 1000, 9}
  129. };
  130. for (auto& currentTest : tests) {
  131. int levelCount = SkMipMap::ComputeLevelCount(currentTest.fWidth, currentTest.fHeight);
  132. REPORTER_ASSERT(reporter, currentTest.fExpectedLevelCount == levelCount);
  133. }
  134. }
  135. struct LevelSizeScenario {
  136. int fBaseWidth;
  137. int fBaseHeight;
  138. int fLevel;
  139. SkISize fExpectedMipMapLevelSize;
  140. };
  141. DEF_TEST(MipMap_ComputeLevelSize, reporter) {
  142. const LevelSizeScenario tests[] = {
  143. // Test mipmaps with negative sizes
  144. {-100, 100, 0, SkISize::Make(0, 0)},
  145. {100, -100, 0, SkISize::Make(0, 0)},
  146. {-100, -100, 0, SkISize::Make(0, 0)},
  147. // Test mipmaps with 0, 1, 2 as dimensions
  148. // (SkMipMap::Build requires a min size of 1)
  149. //
  150. // 0
  151. {0, 100, 0, SkISize::Make(0, 0)},
  152. {100, 0, 0, SkISize::Make(0, 0)},
  153. {0, 0, 0, SkISize::Make(0, 0)},
  154. // 1
  155. {1, 100, 0, SkISize::Make(1, 50)},
  156. {100, 1, 0, SkISize::Make(50, 1)},
  157. {1, 1, 0, SkISize::Make(0, 0)},
  158. // 2
  159. {2, 100, 0, SkISize::Make(1, 50)},
  160. {100, 2, 1, SkISize::Make(25, 1)},
  161. {2, 2, 0, SkISize::Make(1, 1)},
  162. // Test a handful of cases
  163. {63, 63, 2, SkISize::Make(7, 7)},
  164. {64, 64, 2, SkISize::Make(8, 8)},
  165. {127, 127, 2, SkISize::Make(15, 15)},
  166. {64, 129, 3, SkISize::Make(4, 8)},
  167. {255, 32, 6, SkISize::Make(1, 1)},
  168. {500, 1000, 1, SkISize::Make(125, 250)},
  169. };
  170. for (auto& currentTest : tests) {
  171. SkISize levelSize = SkMipMap::ComputeLevelSize(currentTest.fBaseWidth,
  172. currentTest.fBaseHeight,
  173. currentTest.fLevel);
  174. REPORTER_ASSERT(reporter, currentTest.fExpectedMipMapLevelSize == levelSize);
  175. }
  176. }
  177. DEF_TEST(MipMap_F16, reporter) {
  178. SkBitmap bmp;
  179. bmp.allocPixels(SkImageInfo::Make(10, 10, kRGBA_F16_SkColorType, kPremul_SkAlphaType));
  180. bmp.eraseColor(0);
  181. sk_sp<SkMipMap> mipmap(SkMipMap::Build(bmp, nullptr));
  182. }