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