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); |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 26 | sk_sp<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); |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 63 | sk_sp<SkMipMap> mm(SkMipMap::Build(bm, nullptr)); |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 64 | |
| 65 | const int mipLevelCount = mm->countLevels(); |
| 66 | REPORTER_ASSERT(reporter, mipLevelCount == expectedMipLevelCount); |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 67 | REPORTER_ASSERT(reporter, mipLevelCount == SkMipMap::ComputeLevelCount(width, height)); |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 68 | for (int i = 0; i < mipLevelCount; ++i) { |
| 69 | SkMipMap::Level level; |
| 70 | REPORTER_ASSERT(reporter, mm->getLevel(i, &level)); |
| 71 | // Make sure the mipmaps contain valid data and that the sizes are correct |
| 72 | REPORTER_ASSERT(reporter, level.fPixmap.addr()); |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 73 | SkISize size = SkMipMap::ComputeLevelSize(width, height, i); |
| 74 | REPORTER_ASSERT(reporter, level.fPixmap.width() == size.width()); |
| 75 | REPORTER_ASSERT(reporter, level.fPixmap.height() == size.height()); |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 76 | |
| 77 | // + 1 because SkMipMap does not include the base mipmap level. |
| 78 | int twoToTheMipLevel = 1 << (i + 1); |
| 79 | int currentWidth = width / twoToTheMipLevel; |
| 80 | int currentHeight = height / twoToTheMipLevel; |
| 81 | REPORTER_ASSERT(reporter, level.fPixmap.width() == currentWidth); |
| 82 | REPORTER_ASSERT(reporter, level.fPixmap.height() == currentHeight); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | DEF_TEST(MipMap_DirectLevelAccess, reporter) { |
| 87 | // create mipmap with invalid size |
| 88 | { |
| 89 | // SkMipMap current requires the dimensions be greater than 2x2 |
| 90 | SkBitmap bm; |
| 91 | bm.allocN32Pixels(1, 1); |
| 92 | bm.eraseColor(SK_ColorWHITE); |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 93 | sk_sp<SkMipMap> mm(SkMipMap::Build(bm, nullptr)); |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 94 | |
| 95 | REPORTER_ASSERT(reporter, mm == nullptr); |
| 96 | } |
| 97 | |
| 98 | // check small mipmap's count and levels |
| 99 | // There should be 5 mipmap levels generated: |
| 100 | // 16x16, 8x8, 4x4, 2x2, 1x1 |
| 101 | test_mipmap_generation(32, 32, 5, reporter); |
| 102 | |
| 103 | // check large mipmap's count and levels |
| 104 | // There should be 9 mipmap levels generated: |
| 105 | // 500x500, 250x250, 125x125, 62x62, 31x31, 15x15, 7x7, 3x3, 1x1 |
| 106 | test_mipmap_generation(1000, 1000, 9, reporter); |
| 107 | } |
| 108 | |
| 109 | struct LevelCountScenario { |
| 110 | int fWidth; |
| 111 | int fHeight; |
| 112 | int fExpectedLevelCount; |
| 113 | }; |
| 114 | |
| 115 | DEF_TEST(MipMap_ComputeLevelCount, reporter) { |
| 116 | const LevelCountScenario tests[] = { |
| 117 | // Test mipmaps with negative sizes |
| 118 | {-100, 100, 0}, |
| 119 | {100, -100, 0}, |
| 120 | {-100, -100, 0}, |
| 121 | |
| 122 | // Test mipmaps with 0, 1, 2 as dimensions |
brianosman | 9cf31f4 | 2016-03-25 12:55:52 -0700 | [diff] [blame] | 123 | // (SkMipMap::Build requires a min size of 1) |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 124 | // |
| 125 | // 0 |
| 126 | {0, 100, 0}, |
| 127 | {100, 0, 0}, |
| 128 | {0, 0, 0}, |
| 129 | // 1 |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 130 | {1, 100, 6}, |
| 131 | {100, 1, 6}, |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 132 | {1, 1, 0}, |
| 133 | // 2 |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 134 | {2, 100, 6}, |
| 135 | {100, 2, 6}, |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 136 | {2, 2, 1}, |
| 137 | |
| 138 | // Test a handful of boundaries such as 63x63 and 64x64 |
| 139 | {63, 63, 5}, |
| 140 | {64, 64, 6}, |
| 141 | {127, 127, 6}, |
| 142 | {128, 128, 7}, |
| 143 | {255, 255, 7}, |
| 144 | {256, 256, 8}, |
| 145 | |
| 146 | // Test different dimensions, such as 256x64 |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 147 | {64, 129, 7}, |
| 148 | {255, 32, 7}, |
| 149 | {500, 1000, 9} |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | for (auto& currentTest : tests) { |
| 153 | int levelCount = SkMipMap::ComputeLevelCount(currentTest.fWidth, currentTest.fHeight); |
| 154 | REPORTER_ASSERT(reporter, currentTest.fExpectedLevelCount == levelCount); |
| 155 | } |
| 156 | } |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 157 | |
| 158 | struct LevelSizeScenario { |
| 159 | int fBaseWidth; |
| 160 | int fBaseHeight; |
| 161 | int fLevel; |
cblume | 44e09ec | 2016-06-03 11:59:50 -0700 | [diff] [blame] | 162 | SkISize fExpectedMipMapLevelSize; |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | DEF_TEST(MipMap_ComputeLevelSize, reporter) { |
| 166 | const LevelSizeScenario tests[] = { |
| 167 | // Test mipmaps with negative sizes |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 168 | {-100, 100, 0, SkISize::Make(0, 0)}, |
| 169 | {100, -100, 0, SkISize::Make(0, 0)}, |
| 170 | {-100, -100, 0, SkISize::Make(0, 0)}, |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 171 | |
| 172 | // Test mipmaps with 0, 1, 2 as dimensions |
| 173 | // (SkMipMap::Build requires a min size of 1) |
| 174 | // |
| 175 | // 0 |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 176 | {0, 100, 0, SkISize::Make(0, 0)}, |
| 177 | {100, 0, 0, SkISize::Make(0, 0)}, |
| 178 | {0, 0, 0, SkISize::Make(0, 0)}, |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 179 | // 1 |
| 180 | |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 181 | {1, 100, 0, SkISize::Make(1, 50)}, |
| 182 | {100, 1, 0, SkISize::Make(50, 1)}, |
| 183 | {1, 1, 0, SkISize::Make(0, 0)}, |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 184 | // 2 |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 185 | {2, 100, 0, SkISize::Make(1, 50)}, |
| 186 | {100, 2, 1, SkISize::Make(25, 1)}, |
| 187 | {2, 2, 0, SkISize::Make(1, 1)}, |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 188 | |
| 189 | // Test a handful of cases |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 190 | {63, 63, 2, SkISize::Make(7, 7)}, |
| 191 | {64, 64, 2, SkISize::Make(8, 8)}, |
| 192 | {127, 127, 2, SkISize::Make(15, 15)}, |
| 193 | {64, 129, 3, SkISize::Make(4, 8)}, |
| 194 | {255, 32, 6, SkISize::Make(1, 1)}, |
| 195 | {500, 1000, 1, SkISize::Make(125, 250)}, |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | for (auto& currentTest : tests) { |
cblume | 44e09ec | 2016-06-03 11:59:50 -0700 | [diff] [blame] | 199 | SkISize levelSize = SkMipMap::ComputeLevelSize(currentTest.fBaseWidth, |
| 200 | currentTest.fBaseHeight, |
| 201 | currentTest.fLevel); |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 202 | REPORTER_ASSERT(reporter, currentTest.fExpectedMipMapLevelSize == levelSize); |
| 203 | } |
| 204 | } |