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
8 #include "SkBitmap.h"
9 #include "SkMipMap.h"
10 #include "SkRandom.h"
11 #include "Test.h"
12
make_bitmap(SkBitmap * bm,int width,int height)13 static void make_bitmap(SkBitmap* bm, int width, int height) {
14 bm->allocN32Pixels(width, height);
15 bm->eraseColor(SK_ColorWHITE);
16 }
17
DEF_TEST(MipMap,reporter)18 DEF_TEST(MipMap, reporter) {
19 SkBitmap bm;
20 SkRandom rand;
21
22 for (int i = 0; i < 500; ++i) {
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);
28 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm, nullptr));
29
30 REPORTER_ASSERT(reporter, mm->countLevels() == SkMipMap::ComputeLevelCount(width, height));
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));
35
36 SkMipMap::Level prevLevel;
37 sk_bzero(&prevLevel, sizeof(prevLevel));
38
39 SkScalar scale = SK_Scalar1;
40 for (int j = 0; j < 30; ++j) {
41 scale = scale * 2 / 3;
42
43 SkMipMap::Level level;
44 if (mm->extractLevel(SkSize::Make(scale, scale), &level)) {
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);
49
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());
53 }
54 prevLevel = level;
55 }
56 }
57 }
58 }
59
test_mipmap_generation(int width,int height,int expectedMipLevelCount,skiatest::Reporter * reporter)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
DEF_TEST(MipMap_DirectLevelAccess,reporter)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
DEF_TEST(MipMap_ComputeLevelCount,reporter)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, currentTest.fHeight);
152 REPORTER_ASSERT(reporter, currentTest.fExpectedLevelCount == levelCount);
153 }
154 }
155