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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/utils/SkRandom.h" |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 10 | #include "src/core/SkMipmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "tests/Test.h" |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 12 | #include "tools/Resources.h" |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 13 | |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 14 | static void make_bitmap(SkBitmap* bm, int width, int height) { |
| 15 | bm->allocN32Pixels(width, height); |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 16 | bm->eraseColor(SK_ColorWHITE); |
| 17 | } |
| 18 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 19 | DEF_TEST(MipMap, reporter) { |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 20 | SkBitmap bm; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 21 | SkRandom rand; |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 22 | |
| 23 | for (int i = 0; i < 500; ++i) { |
brianosman | 9cf31f4 | 2016-03-25 12:55:52 -0700 | [diff] [blame] | 24 | int width = 1 + rand.nextU() % 1000; |
| 25 | int height = 1 + rand.nextU() % 1000; |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 26 | make_bitmap(&bm, width, height); |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 27 | sk_sp<SkMipmap> mm(SkMipmap::Build(bm, nullptr)); |
skia.committer@gmail.com | a799198 | 2013-07-19 07:00:57 +0000 | [diff] [blame] | 28 | |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 29 | REPORTER_ASSERT(reporter, mm->countLevels() == SkMipmap::ComputeLevelCount(width, height)); |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 30 | REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1, SK_Scalar1), |
| 31 | nullptr)); |
| 32 | REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1 * 2, SK_Scalar1 * 2), |
| 33 | nullptr)); |
skia.committer@gmail.com | a799198 | 2013-07-19 07:00:57 +0000 | [diff] [blame] | 34 | |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 35 | SkMipmap::Level prevLevel; |
reed@google.com | 2c38fed | 2013-07-24 14:55:00 +0000 | [diff] [blame] | 36 | sk_bzero(&prevLevel, sizeof(prevLevel)); |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 37 | |
| 38 | SkScalar scale = SK_Scalar1; |
| 39 | for (int j = 0; j < 30; ++j) { |
| 40 | scale = scale * 2 / 3; |
| 41 | |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 42 | SkMipmap::Level level; |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 43 | if (mm->extractLevel(SkSize::Make(scale, scale), &level)) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 44 | REPORTER_ASSERT(reporter, level.fPixmap.addr()); |
| 45 | REPORTER_ASSERT(reporter, level.fPixmap.width() > 0); |
| 46 | REPORTER_ASSERT(reporter, level.fPixmap.height() > 0); |
| 47 | 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] | 48 | |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 49 | if (prevLevel.fPixmap.addr()) { |
| 50 | REPORTER_ASSERT(reporter, level.fPixmap.width() <= prevLevel.fPixmap.width()); |
| 51 | REPORTER_ASSERT(reporter, level.fPixmap.height() <= prevLevel.fPixmap.height()); |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 52 | } |
| 53 | prevLevel = level; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 58 | |
| 59 | static void test_mipmap_generation(int width, int height, int expectedMipLevelCount, |
| 60 | skiatest::Reporter* reporter) { |
| 61 | SkBitmap bm; |
| 62 | bm.allocN32Pixels(width, height); |
| 63 | bm.eraseColor(SK_ColorWHITE); |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 64 | sk_sp<SkMipmap> mm(SkMipmap::Build(bm, nullptr)); |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 65 | |
| 66 | const int mipLevelCount = mm->countLevels(); |
| 67 | REPORTER_ASSERT(reporter, mipLevelCount == expectedMipLevelCount); |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 68 | REPORTER_ASSERT(reporter, mipLevelCount == SkMipmap::ComputeLevelCount(width, height)); |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 69 | for (int i = 0; i < mipLevelCount; ++i) { |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 70 | SkMipmap::Level level; |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 71 | REPORTER_ASSERT(reporter, mm->getLevel(i, &level)); |
| 72 | // Make sure the mipmaps contain valid data and that the sizes are correct |
| 73 | REPORTER_ASSERT(reporter, level.fPixmap.addr()); |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 74 | SkISize size = SkMipmap::ComputeLevelSize(width, height, i); |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 75 | REPORTER_ASSERT(reporter, level.fPixmap.width() == size.width()); |
| 76 | REPORTER_ASSERT(reporter, level.fPixmap.height() == size.height()); |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 77 | |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 78 | // + 1 because SkMipmap does not include the base mipmap level. |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 79 | int twoToTheMipLevel = 1 << (i + 1); |
| 80 | int currentWidth = width / twoToTheMipLevel; |
| 81 | int currentHeight = height / twoToTheMipLevel; |
| 82 | REPORTER_ASSERT(reporter, level.fPixmap.width() == currentWidth); |
| 83 | REPORTER_ASSERT(reporter, level.fPixmap.height() == currentHeight); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | DEF_TEST(MipMap_DirectLevelAccess, reporter) { |
| 88 | // create mipmap with invalid size |
| 89 | { |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 90 | // SkMipmap current requires the dimensions be greater than 2x2 |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 91 | SkBitmap bm; |
| 92 | bm.allocN32Pixels(1, 1); |
| 93 | bm.eraseColor(SK_ColorWHITE); |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 94 | sk_sp<SkMipmap> mm(SkMipmap::Build(bm, nullptr)); |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 95 | |
| 96 | REPORTER_ASSERT(reporter, mm == nullptr); |
| 97 | } |
| 98 | |
| 99 | // check small mipmap's count and levels |
| 100 | // There should be 5 mipmap levels generated: |
| 101 | // 16x16, 8x8, 4x4, 2x2, 1x1 |
| 102 | test_mipmap_generation(32, 32, 5, reporter); |
| 103 | |
| 104 | // check large mipmap's count and levels |
| 105 | // There should be 9 mipmap levels generated: |
| 106 | // 500x500, 250x250, 125x125, 62x62, 31x31, 15x15, 7x7, 3x3, 1x1 |
| 107 | test_mipmap_generation(1000, 1000, 9, reporter); |
| 108 | } |
| 109 | |
| 110 | struct LevelCountScenario { |
| 111 | int fWidth; |
| 112 | int fHeight; |
| 113 | int fExpectedLevelCount; |
| 114 | }; |
| 115 | |
| 116 | DEF_TEST(MipMap_ComputeLevelCount, reporter) { |
| 117 | const LevelCountScenario tests[] = { |
| 118 | // Test mipmaps with negative sizes |
| 119 | {-100, 100, 0}, |
| 120 | {100, -100, 0}, |
| 121 | {-100, -100, 0}, |
| 122 | |
| 123 | // Test mipmaps with 0, 1, 2 as dimensions |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 124 | // (SkMipmap::Build requires a min size of 1) |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 125 | // |
| 126 | // 0 |
| 127 | {0, 100, 0}, |
| 128 | {100, 0, 0}, |
| 129 | {0, 0, 0}, |
| 130 | // 1 |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 131 | {1, 100, 6}, |
| 132 | {100, 1, 6}, |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 133 | {1, 1, 0}, |
| 134 | // 2 |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 135 | {2, 100, 6}, |
| 136 | {100, 2, 6}, |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 137 | {2, 2, 1}, |
| 138 | |
| 139 | // Test a handful of boundaries such as 63x63 and 64x64 |
| 140 | {63, 63, 5}, |
| 141 | {64, 64, 6}, |
| 142 | {127, 127, 6}, |
| 143 | {128, 128, 7}, |
| 144 | {255, 255, 7}, |
| 145 | {256, 256, 8}, |
| 146 | |
| 147 | // Test different dimensions, such as 256x64 |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 148 | {64, 129, 7}, |
| 149 | {255, 32, 7}, |
| 150 | {500, 1000, 9} |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | for (auto& currentTest : tests) { |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 154 | int levelCount = SkMipmap::ComputeLevelCount(currentTest.fWidth, currentTest.fHeight); |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 155 | REPORTER_ASSERT(reporter, currentTest.fExpectedLevelCount == levelCount); |
| 156 | } |
| 157 | } |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 158 | |
| 159 | struct LevelSizeScenario { |
| 160 | int fBaseWidth; |
| 161 | int fBaseHeight; |
| 162 | int fLevel; |
cblume | 44e09ec | 2016-06-03 11:59:50 -0700 | [diff] [blame] | 163 | SkISize fExpectedMipMapLevelSize; |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | DEF_TEST(MipMap_ComputeLevelSize, reporter) { |
| 167 | const LevelSizeScenario tests[] = { |
| 168 | // Test mipmaps with negative sizes |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 169 | {-100, 100, 0, SkISize::Make(0, 0)}, |
| 170 | {100, -100, 0, SkISize::Make(0, 0)}, |
| 171 | {-100, -100, 0, SkISize::Make(0, 0)}, |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 172 | |
| 173 | // Test mipmaps with 0, 1, 2 as dimensions |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 174 | // (SkMipmap::Build requires a min size of 1) |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 175 | // |
| 176 | // 0 |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 177 | {0, 100, 0, SkISize::Make(0, 0)}, |
| 178 | {100, 0, 0, SkISize::Make(0, 0)}, |
| 179 | {0, 0, 0, SkISize::Make(0, 0)}, |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 180 | // 1 |
| 181 | |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 182 | {1, 100, 0, SkISize::Make(1, 50)}, |
| 183 | {100, 1, 0, SkISize::Make(50, 1)}, |
| 184 | {1, 1, 0, SkISize::Make(0, 0)}, |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 185 | // 2 |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 186 | {2, 100, 0, SkISize::Make(1, 50)}, |
| 187 | {100, 2, 1, SkISize::Make(25, 1)}, |
| 188 | {2, 2, 0, SkISize::Make(1, 1)}, |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 189 | |
| 190 | // Test a handful of cases |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 191 | {63, 63, 2, SkISize::Make(7, 7)}, |
| 192 | {64, 64, 2, SkISize::Make(8, 8)}, |
| 193 | {127, 127, 2, SkISize::Make(15, 15)}, |
| 194 | {64, 129, 3, SkISize::Make(4, 8)}, |
| 195 | {255, 32, 6, SkISize::Make(1, 1)}, |
| 196 | {500, 1000, 1, SkISize::Make(125, 250)}, |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | for (auto& currentTest : tests) { |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 200 | SkISize levelSize = SkMipmap::ComputeLevelSize(currentTest.fBaseWidth, |
cblume | 44e09ec | 2016-06-03 11:59:50 -0700 | [diff] [blame] | 201 | currentTest.fBaseHeight, |
| 202 | currentTest.fLevel); |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 203 | REPORTER_ASSERT(reporter, currentTest.fExpectedMipMapLevelSize == levelSize); |
| 204 | } |
| 205 | } |
Brian Osman | b237f8a | 2018-12-04 09:06:00 -0500 | [diff] [blame] | 206 | |
| 207 | DEF_TEST(MipMap_F16, reporter) { |
| 208 | SkBitmap bmp; |
| 209 | bmp.allocPixels(SkImageInfo::Make(10, 10, kRGBA_F16_SkColorType, kPremul_SkAlphaType)); |
| 210 | bmp.eraseColor(0); |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 211 | sk_sp<SkMipmap> mipmap(SkMipmap::Build(bmp, nullptr)); |
Brian Osman | b237f8a | 2018-12-04 09:06:00 -0500 | [diff] [blame] | 212 | } |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 213 | |
| 214 | #include "include/core/SkCanvas.h" |
| 215 | #include "include/core/SkSurface.h" |
| 216 | |
Mike Reed | 1529295 | 2020-07-25 12:47:14 -0400 | [diff] [blame] | 217 | static void fill_in_mips(SkMipmapBuilder* builder, sk_sp<SkImage> img) { |
| 218 | int count = builder->countLevels(); |
| 219 | for (int i = 0; i < count; ++i) { |
| 220 | SkPixmap pm = builder->level(i); |
| 221 | auto surf = SkSurface::MakeRasterDirect(pm); |
| 222 | surf->getCanvas()->drawImageRect(img, SkRect::MakeIWH(pm.width(), pm.height()), nullptr); |
| 223 | } |
| 224 | } |
| 225 | |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 226 | DEF_TEST(image_mip_factory, reporter) { |
| 227 | // TODO: what do to about lazy images and mipmaps? |
| 228 | auto img = GetResourceAsImage("images/mandrill_128.png")->makeRasterImage(); |
| 229 | |
| 230 | REPORTER_ASSERT(reporter, !img->hasMipmaps()); |
| 231 | auto img1 = img->withMipmaps(nullptr); |
| 232 | REPORTER_ASSERT(reporter, img.get() != img1.get()); |
| 233 | REPORTER_ASSERT(reporter, img1->hasMipmaps()); |
| 234 | |
| 235 | SkMipmapBuilder builder(img->imageInfo()); |
Mike Reed | 1529295 | 2020-07-25 12:47:14 -0400 | [diff] [blame] | 236 | fill_in_mips(&builder, img); |
| 237 | |
Mike Reed | 2fe1569 | 2020-07-14 11:29:38 -0400 | [diff] [blame] | 238 | auto img2 = img->withMipmaps(builder.detach()); |
| 239 | REPORTER_ASSERT(reporter, !builder.detach()); |
| 240 | REPORTER_ASSERT(reporter, img.get() != img2.get()); |
| 241 | REPORTER_ASSERT(reporter, img1.get() != img2.get()); |
| 242 | REPORTER_ASSERT(reporter, img2->hasMipmaps()); |
| 243 | } |
Mike Reed | 1529295 | 2020-07-25 12:47:14 -0400 | [diff] [blame] | 244 | |
| 245 | // Ensure we can't attach mips that don't "match" the image |
| 246 | // |
| 247 | DEF_TEST(image_mip_mismatch, reporter) { |
| 248 | auto check_fails = [reporter](sk_sp<SkImage> img, const SkImageInfo& info) { |
| 249 | SkMipmapBuilder builder(info); |
| 250 | fill_in_mips(&builder, img); |
| 251 | auto img2 = img->withMipmaps(builder.detach()); |
| 252 | // if withMipmaps() succeeds, it returns a new image, otherwise it returns the original |
| 253 | REPORTER_ASSERT(reporter, img.get() == img2.get()); |
| 254 | }; |
| 255 | |
| 256 | auto img = GetResourceAsImage("images/mandrill_128.png")->makeRasterImage(); |
| 257 | |
| 258 | // check size, colortype, and alphatype |
| 259 | |
| 260 | check_fails(img, img->imageInfo().makeWH(img->width() + 2, img->height() - 3)); |
| 261 | |
| 262 | SkASSERT(img->imageInfo().colorType() != kRGB_565_SkColorType); |
| 263 | check_fails(img, img->imageInfo().makeColorType(kRGB_565_SkColorType)); |
| 264 | |
| 265 | SkASSERT(img->imageInfo().alphaType() != kUnpremul_SkAlphaType); |
| 266 | check_fails(img, img->imageInfo().makeAlphaType(kUnpremul_SkAlphaType)); |
| 267 | } |