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