• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkBitmap.h"
9 #include "include/utils/SkRandom.h"
10 #include "src/core/SkMipmap.h"
11 #include "tests/Test.h"
12 #include "tools/Resources.h"
13 
make_bitmap(SkBitmap * bm,int width,int height)14 static void make_bitmap(SkBitmap* bm, int width, int height) {
15     bm->allocN32Pixels(width, height);
16     bm->eraseColor(SK_ColorWHITE);
17 }
18 
DEF_TEST(MipMap,reporter)19 DEF_TEST(MipMap, reporter) {
20     SkBitmap bm;
21     SkRandom rand;
22 
23     for (int i = 0; i < 500; ++i) {
24         int width = 1 + rand.nextU() % 1000;
25         int height = 1 + rand.nextU() % 1000;
26         make_bitmap(&bm, width, height);
27         sk_sp<SkMipmap> mm(SkMipmap::Build(bm, nullptr));
28 
29         REPORTER_ASSERT(reporter, mm->countLevels() == SkMipmap::ComputeLevelCount(width, height));
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));
34 
35         SkMipmap::Level prevLevel;
36         sk_bzero(&prevLevel, sizeof(prevLevel));
37 
38         SkScalar scale = SK_Scalar1;
39         for (int j = 0; j < 30; ++j) {
40             scale = scale * 2 / 3;
41 
42             SkMipmap::Level level;
43             if (mm->extractLevel(SkSize::Make(scale, scale), &level)) {
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);
48 
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());
52                 }
53                 prevLevel = level;
54             }
55         }
56     }
57 }
58 
test_mipmap_generation(int width,int height,int expectedMipLevelCount,skiatest::Reporter * reporter)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);
64     sk_sp<SkMipmap> mm(SkMipmap::Build(bm, nullptr));
65 
66     const int mipLevelCount = mm->countLevels();
67     REPORTER_ASSERT(reporter, mipLevelCount == expectedMipLevelCount);
68     REPORTER_ASSERT(reporter, mipLevelCount == SkMipmap::ComputeLevelCount(width, height));
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         SkISize size = SkMipmap::ComputeLevelSize(width, height, i);
75         REPORTER_ASSERT(reporter, level.fPixmap.width() == size.width());
76         REPORTER_ASSERT(reporter, level.fPixmap.height() == size.height());
77 
78         // + 1 because SkMipmap does not include the base mipmap level.
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 
DEF_TEST(MipMap_DirectLevelAccess,reporter)87 DEF_TEST(MipMap_DirectLevelAccess, reporter) {
88     // create mipmap with invalid size
89     {
90         // SkMipmap current requires the dimensions be greater than 2x2
91         SkBitmap bm;
92         bm.allocN32Pixels(1, 1);
93         bm.eraseColor(SK_ColorWHITE);
94         sk_sp<SkMipmap> mm(SkMipmap::Build(bm, nullptr));
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 
DEF_TEST(MipMap_ComputeLevelCount,reporter)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
124         // (SkMipmap::Build requires a min size of 1)
125         //
126         // 0
127         {0, 100, 0},
128         {100, 0, 0},
129         {0, 0, 0},
130         // 1
131         {1, 100, 6},
132         {100, 1, 6},
133         {1, 1, 0},
134         // 2
135         {2, 100, 6},
136         {100, 2, 6},
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
148         {64, 129, 7},
149         {255, 32, 7},
150         {500, 1000, 9}
151     };
152 
153     for (auto& currentTest : tests) {
154         int levelCount = SkMipmap::ComputeLevelCount(currentTest.fWidth, currentTest.fHeight);
155         REPORTER_ASSERT(reporter, currentTest.fExpectedLevelCount == levelCount);
156     }
157 }
158 
159 struct LevelSizeScenario {
160     int fBaseWidth;
161     int fBaseHeight;
162     int fLevel;
163     SkISize fExpectedMipMapLevelSize;
164 };
165 
DEF_TEST(MipMap_ComputeLevelSize,reporter)166 DEF_TEST(MipMap_ComputeLevelSize, reporter) {
167     const LevelSizeScenario tests[] = {
168         // Test mipmaps with negative sizes
169         {-100, 100, 0, SkISize::Make(0, 0)},
170         {100, -100, 0, SkISize::Make(0, 0)},
171         {-100, -100, 0, SkISize::Make(0, 0)},
172 
173         // Test mipmaps with 0, 1, 2 as dimensions
174         // (SkMipmap::Build requires a min size of 1)
175         //
176         // 0
177         {0, 100, 0, SkISize::Make(0, 0)},
178         {100, 0, 0, SkISize::Make(0, 0)},
179         {0, 0, 0, SkISize::Make(0, 0)},
180         // 1
181 
182         {1, 100, 0, SkISize::Make(1, 50)},
183         {100, 1, 0, SkISize::Make(50, 1)},
184         {1, 1, 0, SkISize::Make(0, 0)},
185         // 2
186         {2, 100, 0, SkISize::Make(1, 50)},
187         {100, 2, 1, SkISize::Make(25, 1)},
188         {2, 2, 0, SkISize::Make(1, 1)},
189 
190         // Test a handful of cases
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)},
197     };
198 
199     for (auto& currentTest : tests) {
200         SkISize levelSize = SkMipmap::ComputeLevelSize(currentTest.fBaseWidth,
201                                                        currentTest.fBaseHeight,
202                                                        currentTest.fLevel);
203         REPORTER_ASSERT(reporter, currentTest.fExpectedMipMapLevelSize == levelSize);
204     }
205 }
206 
DEF_TEST(MipMap_F16,reporter)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);
211     sk_sp<SkMipmap> mipmap(SkMipmap::Build(bmp, nullptr));
212 }
213 
214 #include "include/core/SkCanvas.h"
215 #include "include/core/SkSurface.h"
216 #include "src/core/SkMipmapBuilder.h"
217 
fill_in_mips(SkMipmapBuilder * builder,sk_sp<SkImage> img)218 static void fill_in_mips(SkMipmapBuilder* builder, sk_sp<SkImage> img) {
219     int count = builder->countLevels();
220     for (int i = 0; i < count; ++i) {
221         SkPixmap pm = builder->level(i);
222         auto surf = SkSurface::MakeRasterDirect(pm);
223         surf->getCanvas()->drawImageRect(img, SkRect::MakeIWH(pm.width(), pm.height()),
224                                          SkSamplingOptions());
225     }
226 }
227 
DEF_TEST(image_mip_factory,reporter)228 DEF_TEST(image_mip_factory, reporter) {
229     // TODO: what do to about lazy images and mipmaps?
230     auto img = GetResourceAsImage("images/mandrill_128.png")->makeRasterImage();
231 
232     REPORTER_ASSERT(reporter, !img->hasMipmaps());
233     auto img1 = img->withDefaultMipmaps();
234     REPORTER_ASSERT(reporter, img.get() != img1.get());
235     REPORTER_ASSERT(reporter, img1->hasMipmaps());
236 
237     SkMipmapBuilder builder(img->imageInfo());
238     fill_in_mips(&builder, img);
239 
240     auto img2 = builder.attachTo(img);
241     REPORTER_ASSERT(reporter, img.get()  != img2.get());
242     REPORTER_ASSERT(reporter, img1.get() != img2.get());
243     REPORTER_ASSERT(reporter, img2->hasMipmaps());
244 }
245 
246 // Ensure we can't attach mips that don't "match" the image
247 //
DEF_TEST(image_mip_mismatch,reporter)248 DEF_TEST(image_mip_mismatch, reporter) {
249     auto check_fails = [reporter](sk_sp<SkImage> img, const SkImageInfo& info) {
250         SkMipmapBuilder builder(info);
251         fill_in_mips(&builder, img);
252         auto img2 = builder.attachTo(img);
253         // if withMipmaps() succeeds, it returns a new image, otherwise it returns the original
254         REPORTER_ASSERT(reporter, img.get() == img2.get());
255     };
256 
257     auto img = GetResourceAsImage("images/mandrill_128.png")->makeRasterImage();
258 
259     // check size, colortype, and alphatype
260 
261     check_fails(img, img->imageInfo().makeWH(img->width() + 2, img->height() - 3));
262 
263     SkASSERT(img->imageInfo().colorType() != kRGB_565_SkColorType);
264     check_fails(img, img->imageInfo().makeColorType(kRGB_565_SkColorType));
265 
266     SkASSERT(img->imageInfo().alphaType() != kUnpremul_SkAlphaType);
267     check_fails(img, img->imageInfo().makeAlphaType(kUnpremul_SkAlphaType));
268 }
269