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) { |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 23 | // for now, Build needs a min size of 2, otherwise it will return nullptr. |
| 24 | // should fix that to support 1 X N, where N > 1 to return non-null. |
| 25 | int width = 2 + rand.nextU() % 1000; |
| 26 | int height = 2 + rand.nextU() % 1000; |
| 27 | make_bitmap(&bm, width, height); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 28 | SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr)); |
skia.committer@gmail.com | a799198 | 2013-07-19 07:00:57 +0000 | [diff] [blame] | 29 | |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 30 | REPORTER_ASSERT(reporter, mm->countLevels() == SkMipMap::ComputeLevelCount(width, height)); |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 31 | REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1, SK_Scalar1), |
| 32 | nullptr)); |
| 33 | REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1 * 2, SK_Scalar1 * 2), |
| 34 | nullptr)); |
skia.committer@gmail.com | a799198 | 2013-07-19 07:00:57 +0000 | [diff] [blame] | 35 | |
reed@google.com | 2c38fed | 2013-07-24 14:55:00 +0000 | [diff] [blame] | 36 | SkMipMap::Level prevLevel; |
| 37 | sk_bzero(&prevLevel, sizeof(prevLevel)); |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 38 | |
| 39 | SkScalar scale = SK_Scalar1; |
| 40 | for (int j = 0; j < 30; ++j) { |
| 41 | scale = scale * 2 / 3; |
| 42 | |
| 43 | SkMipMap::Level level; |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 44 | if (mm->extractLevel(SkSize::Make(scale, scale), &level)) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 45 | REPORTER_ASSERT(reporter, level.fPixmap.addr()); |
| 46 | REPORTER_ASSERT(reporter, level.fPixmap.width() > 0); |
| 47 | REPORTER_ASSERT(reporter, level.fPixmap.height() > 0); |
| 48 | 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] | 49 | |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 50 | if (prevLevel.fPixmap.addr()) { |
| 51 | REPORTER_ASSERT(reporter, level.fPixmap.width() <= prevLevel.fPixmap.width()); |
| 52 | REPORTER_ASSERT(reporter, level.fPixmap.height() <= prevLevel.fPixmap.height()); |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 53 | } |
| 54 | prevLevel = level; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 59 | |
| 60 | static void test_mipmap_generation(int width, int height, int expectedMipLevelCount, |
| 61 | skiatest::Reporter* reporter) { |
| 62 | SkBitmap bm; |
| 63 | bm.allocN32Pixels(width, height); |
| 64 | bm.eraseColor(SK_ColorWHITE); |
| 65 | SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr)); |
| 66 | |
| 67 | const int mipLevelCount = mm->countLevels(); |
| 68 | REPORTER_ASSERT(reporter, mipLevelCount == expectedMipLevelCount); |
| 69 | for (int i = 0; i < mipLevelCount; ++i) { |
| 70 | SkMipMap::Level level; |
| 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()); |
| 74 | |
| 75 | // + 1 because SkMipMap does not include the base mipmap level. |
| 76 | int twoToTheMipLevel = 1 << (i + 1); |
| 77 | int currentWidth = width / twoToTheMipLevel; |
| 78 | int currentHeight = height / twoToTheMipLevel; |
| 79 | REPORTER_ASSERT(reporter, level.fPixmap.width() == currentWidth); |
| 80 | REPORTER_ASSERT(reporter, level.fPixmap.height() == currentHeight); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | DEF_TEST(MipMap_DirectLevelAccess, reporter) { |
| 85 | // create mipmap with invalid size |
| 86 | { |
| 87 | // SkMipMap current requires the dimensions be greater than 2x2 |
| 88 | SkBitmap bm; |
| 89 | bm.allocN32Pixels(1, 1); |
| 90 | bm.eraseColor(SK_ColorWHITE); |
| 91 | SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr)); |
| 92 | |
| 93 | REPORTER_ASSERT(reporter, mm == nullptr); |
| 94 | } |
| 95 | |
| 96 | // check small mipmap's count and levels |
| 97 | // There should be 5 mipmap levels generated: |
| 98 | // 16x16, 8x8, 4x4, 2x2, 1x1 |
| 99 | test_mipmap_generation(32, 32, 5, reporter); |
| 100 | |
| 101 | // check large mipmap's count and levels |
| 102 | // There should be 9 mipmap levels generated: |
| 103 | // 500x500, 250x250, 125x125, 62x62, 31x31, 15x15, 7x7, 3x3, 1x1 |
| 104 | test_mipmap_generation(1000, 1000, 9, reporter); |
| 105 | } |
| 106 | |
| 107 | struct LevelCountScenario { |
| 108 | int fWidth; |
| 109 | int fHeight; |
| 110 | int fExpectedLevelCount; |
| 111 | }; |
| 112 | |
| 113 | DEF_TEST(MipMap_ComputeLevelCount, reporter) { |
| 114 | const LevelCountScenario tests[] = { |
| 115 | // Test mipmaps with negative sizes |
| 116 | {-100, 100, 0}, |
| 117 | {100, -100, 0}, |
| 118 | {-100, -100, 0}, |
| 119 | |
| 120 | // Test mipmaps with 0, 1, 2 as dimensions |
| 121 | // (SkMipMap::Build requires a min size of 2) |
| 122 | // |
| 123 | // 0 |
| 124 | {0, 100, 0}, |
| 125 | {100, 0, 0}, |
| 126 | {0, 0, 0}, |
| 127 | // 1 |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 128 | {1, 100, 6}, |
| 129 | {100, 1, 6}, |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 130 | {1, 1, 0}, |
| 131 | // 2 |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 132 | {2, 100, 6}, |
| 133 | {100, 2, 6}, |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 134 | {2, 2, 1}, |
| 135 | |
| 136 | // Test a handful of boundaries such as 63x63 and 64x64 |
| 137 | {63, 63, 5}, |
| 138 | {64, 64, 6}, |
| 139 | {127, 127, 6}, |
| 140 | {128, 128, 7}, |
| 141 | {255, 255, 7}, |
| 142 | {256, 256, 8}, |
| 143 | |
| 144 | // Test different dimensions, such as 256x64 |
cblume | 5b9ad76 | 2016-03-01 13:54:30 -0800 | [diff] [blame] | 145 | {64, 129, 7}, |
| 146 | {255, 32, 7}, |
| 147 | {500, 1000, 9} |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | for (auto& currentTest : tests) { |
| 151 | int levelCount = SkMipMap::ComputeLevelCount(currentTest.fWidth, currentTest.fHeight); |
| 152 | REPORTER_ASSERT(reporter, currentTest.fExpectedLevelCount == levelCount); |
| 153 | } |
| 154 | } |