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