OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkMipMap.h" | 9 #include "SkMipMap.h" |
10 #include "SkRandom.h" | 10 #include "SkRandom.h" |
11 #include "Test.h" | 11 #include "Test.h" |
12 | 12 |
13 static void make_bitmap(SkBitmap* bm, SkRandom& rand) { | 13 static void make_bitmap(SkBitmap* bm, int width, int height) { |
14 // for now, Build needs a min size of 2, otherwise it will return nullptr. | 14 bm->allocN32Pixels(width, height); |
15 // should fix that to support 1 X N, where N > 1 to return non-null. | |
16 int w = 2 + rand.nextU() % 1000; | |
17 int h = 2 + rand.nextU() % 1000; | |
18 bm->allocN32Pixels(w, h); | |
19 bm->eraseColor(SK_ColorWHITE); | 15 bm->eraseColor(SK_ColorWHITE); |
20 } | 16 } |
21 | 17 |
22 DEF_TEST(MipMap, reporter) { | 18 DEF_TEST(MipMap, reporter) { |
23 SkBitmap bm; | 19 SkBitmap bm; |
24 SkRandom rand; | 20 SkRandom rand; |
25 | 21 |
26 for (int i = 0; i < 500; ++i) { | 22 for (int i = 0; i < 500; ++i) { |
27 make_bitmap(&bm, rand); | 23 // for now, Build needs a min size of 2, otherwise it will return nullpt
r. |
| 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); |
28 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr)); | 28 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr)); |
29 | 29 |
| 30 REPORTER_ASSERT(reporter, mm->countLevels() == SkMipMap::ComputeLevelCou
nt(width, height)); |
30 REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1, SK_
Scalar1), | 31 REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1, SK_
Scalar1), |
31 nullptr)); | 32 nullptr)); |
32 REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1 * 2,
SK_Scalar1 * 2), | 33 REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1 * 2,
SK_Scalar1 * 2), |
33 nullptr)); | 34 nullptr)); |
34 | 35 |
35 SkMipMap::Level prevLevel; | 36 SkMipMap::Level prevLevel; |
36 sk_bzero(&prevLevel, sizeof(prevLevel)); | 37 sk_bzero(&prevLevel, sizeof(prevLevel)); |
37 | 38 |
38 SkScalar scale = SK_Scalar1; | 39 SkScalar scale = SK_Scalar1; |
39 for (int j = 0; j < 30; ++j) { | 40 for (int j = 0; j < 30; ++j) { |
40 scale = scale * 2 / 3; | 41 scale = scale * 2 / 3; |
41 | 42 |
42 SkMipMap::Level level; | 43 SkMipMap::Level level; |
43 if (mm->extractLevel(SkSize::Make(scale, scale), &level)) { | 44 if (mm->extractLevel(SkSize::Make(scale, scale), &level)) { |
44 REPORTER_ASSERT(reporter, level.fPixmap.addr()); | 45 REPORTER_ASSERT(reporter, level.fPixmap.addr()); |
45 REPORTER_ASSERT(reporter, level.fPixmap.width() > 0); | 46 REPORTER_ASSERT(reporter, level.fPixmap.width() > 0); |
46 REPORTER_ASSERT(reporter, level.fPixmap.height() > 0); | 47 REPORTER_ASSERT(reporter, level.fPixmap.height() > 0); |
47 REPORTER_ASSERT(reporter, (int)level.fPixmap.rowBytes() >= level
.fPixmap.width() * 4); | 48 REPORTER_ASSERT(reporter, (int)level.fPixmap.rowBytes() >= level
.fPixmap.width() * 4); |
48 | 49 |
49 if (prevLevel.fPixmap.addr()) { | 50 if (prevLevel.fPixmap.addr()) { |
50 REPORTER_ASSERT(reporter, level.fPixmap.width() <= prevLevel
.fPixmap.width()); | 51 REPORTER_ASSERT(reporter, level.fPixmap.width() <= prevLevel
.fPixmap.width()); |
51 REPORTER_ASSERT(reporter, level.fPixmap.height() <= prevLeve
l.fPixmap.height()); | 52 REPORTER_ASSERT(reporter, level.fPixmap.height() <= prevLeve
l.fPixmap.height()); |
52 } | 53 } |
53 prevLevel = level; | 54 prevLevel = level; |
54 } | 55 } |
55 } | 56 } |
56 } | 57 } |
57 } | 58 } |
| 59 |
| 60 static void test_mipmap_generation(int width, int height, int expectedMipLevelCo
unt, |
| 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 corre
ct |
| 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 |
| 128 {1, 100, 0}, |
| 129 {100, 1, 0}, |
| 130 {1, 1, 0}, |
| 131 // 2 |
| 132 {2, 100, 1}, |
| 133 {100, 2, 1}, |
| 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 |
| 145 {64, 129, 6}, |
| 146 {255, 32, 5}, |
| 147 {500, 1000, 8} |
| 148 }; |
| 149 |
| 150 for (auto& currentTest : tests) { |
| 151 int levelCount = SkMipMap::ComputeLevelCount(currentTest.fWidth, current
Test.fHeight); |
| 152 REPORTER_ASSERT(reporter, currentTest.fExpectedLevelCount == levelCount)
; |
| 153 } |
| 154 } |
OLD | NEW |