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/core/SkColor.h"
10 #include "include/core/SkImageInfo.h"
11 #include "include/core/SkMallocPixelRef.h"
12 #include "include/core/SkPixelRef.h"
13 #include "include/core/SkPixmap.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkTypes.h"
17 #include "include/private/SkFloatingPoint.h"
18 #include "include/utils/SkRandom.h"
19 #include "tests/Test.h"
20 #include "tools/ToolUtils.h"
21
22 #include <initializer_list>
23
test_peekpixels(skiatest::Reporter * reporter)24 static void test_peekpixels(skiatest::Reporter* reporter) {
25 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
26
27 SkPixmap pmap;
28 SkBitmap bm;
29
30 // empty should return false
31 REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr));
32 REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap));
33
34 // no pixels should return false
35 bm.setInfo(SkImageInfo::MakeN32Premul(10, 10));
36 REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr));
37 REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap));
38
39 // real pixels should return true
40 bm.allocPixels(info);
41 REPORTER_ASSERT(reporter, bm.peekPixels(nullptr));
42 REPORTER_ASSERT(reporter, bm.peekPixels(&pmap));
43 REPORTER_ASSERT(reporter, pmap.info() == bm.info());
44 REPORTER_ASSERT(reporter, pmap.addr() == bm.getPixels());
45 REPORTER_ASSERT(reporter, pmap.rowBytes() == bm.rowBytes());
46 }
47
48 // https://code.google.com/p/chromium/issues/detail?id=446164
test_bigalloc(skiatest::Reporter * reporter)49 static void test_bigalloc(skiatest::Reporter* reporter) {
50 const int width = 0x40000001;
51 const int height = 0x00000096;
52 const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
53
54 SkBitmap bm;
55 REPORTER_ASSERT(reporter, !bm.tryAllocPixels(info));
56
57 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, info.minRowBytes());
58 REPORTER_ASSERT(reporter, !pr);
59 }
60
test_allocpixels(skiatest::Reporter * reporter)61 static void test_allocpixels(skiatest::Reporter* reporter) {
62 const int width = 10;
63 const int height = 10;
64 const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
65 const size_t explicitRowBytes = info.minRowBytes() + 24;
66
67 SkBitmap bm;
68 bm.setInfo(info);
69 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
70 bm.allocPixels();
71 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
72 bm.reset();
73 bm.allocPixels(info);
74 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
75
76 bm.setInfo(info, explicitRowBytes);
77 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
78 bm.allocPixels();
79 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
80 bm.reset();
81 bm.allocPixels(info, explicitRowBytes);
82 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
83
84 bm.reset();
85 bm.setInfo(info, 0);
86 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
87 bm.reset();
88 bm.allocPixels(info, 0);
89 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
90
91 bm.reset();
92 bool success = bm.setInfo(info, info.minRowBytes() - 1); // invalid for 32bit
93 REPORTER_ASSERT(reporter, !success);
94 REPORTER_ASSERT(reporter, bm.isNull());
95
96 for (SkColorType ct : {
97 kAlpha_8_SkColorType,
98 kRGB_565_SkColorType,
99 kARGB_4444_SkColorType,
100 kRGBA_8888_SkColorType,
101 kBGRA_8888_SkColorType,
102 kRGB_888x_SkColorType,
103 kRGBA_1010102_SkColorType,
104 kRGB_101010x_SkColorType,
105 kGray_8_SkColorType,
106 kRGBA_F16Norm_SkColorType,
107 kRGBA_F16_SkColorType,
108 kRGBA_F32_SkColorType,
109 kR8G8_unorm_SkColorType,
110 kA16_unorm_SkColorType,
111 kR16G16_unorm_SkColorType,
112 kA16_float_SkColorType,
113 kR16G16_float_SkColorType,
114 kR16G16B16A16_unorm_SkColorType,
115 }) {
116 SkImageInfo imageInfo = info.makeColorType(ct);
117 for (int rowBytesPadding = 1; rowBytesPadding <= 17; rowBytesPadding++) {
118 bm.reset();
119 success = bm.setInfo(imageInfo, imageInfo.minRowBytes() + rowBytesPadding);
120 if (rowBytesPadding % imageInfo.bytesPerPixel() == 0) {
121 REPORTER_ASSERT(reporter, success);
122 success = bm.tryAllocPixels();
123 REPORTER_ASSERT(reporter, success);
124 } else {
125 // Not pixel aligned.
126 REPORTER_ASSERT(reporter, !success);
127 REPORTER_ASSERT(reporter, bm.isNull());
128 }
129 }
130 }
131 }
132
test_bigwidth(skiatest::Reporter * reporter)133 static void test_bigwidth(skiatest::Reporter* reporter) {
134 SkBitmap bm;
135 int width = 1 << 29; // *4 will be the high-bit of 32bit int
136
137 SkImageInfo info = SkImageInfo::MakeA8(width, 1);
138 REPORTER_ASSERT(reporter, bm.setInfo(info));
139 REPORTER_ASSERT(reporter, bm.setInfo(info.makeColorType(kRGB_565_SkColorType)));
140
141 // for a 4-byte config, this width will compute a rowbytes of 0x80000000,
142 // which does not fit in a int32_t. setConfig should detect this, and fail.
143
144 // TODO: perhaps skia can relax this, and only require that rowBytes fit
145 // in a uint32_t (or larger), but for now this is the constraint.
146
147 REPORTER_ASSERT(reporter, !bm.setInfo(info.makeColorType(kN32_SkColorType)));
148 }
149
150 /**
151 * This test contains basic sanity checks concerning bitmaps.
152 */
DEF_TEST(Bitmap,reporter)153 DEF_TEST(Bitmap, reporter) {
154 // Zero-sized bitmaps are allowed
155 for (int width = 0; width < 2; ++width) {
156 for (int height = 0; height < 2; ++height) {
157 SkBitmap bm;
158 bool setConf = bm.setInfo(SkImageInfo::MakeN32Premul(width, height));
159 REPORTER_ASSERT(reporter, setConf);
160 if (setConf) {
161 bm.allocPixels();
162 }
163 REPORTER_ASSERT(reporter, SkToBool(width & height) != bm.empty());
164 }
165 }
166
167 test_bigwidth(reporter);
168 test_allocpixels(reporter);
169 test_bigalloc(reporter);
170 test_peekpixels(reporter);
171 }
172
173 /**
174 * This test checks that getColor works for both swizzles.
175 */
DEF_TEST(Bitmap_getColor_Swizzle,r)176 DEF_TEST(Bitmap_getColor_Swizzle, r) {
177 SkBitmap source;
178 source.allocN32Pixels(1,1);
179 source.eraseColor(SK_ColorRED);
180 SkColorType colorTypes[] = {
181 kRGBA_8888_SkColorType,
182 kBGRA_8888_SkColorType,
183 };
184 for (SkColorType ct : colorTypes) {
185 SkBitmap copy;
186 if (!ToolUtils::copy_to(©, ct, source)) {
187 ERRORF(r, "SkBitmap::copy failed %d", (int)ct);
188 continue;
189 }
190 REPORTER_ASSERT(r, source.getColor(0, 0) == copy.getColor(0, 0));
191 }
192 }
193
test_erasecolor_premul(skiatest::Reporter * reporter,SkColorType ct,SkColor input,SkColor expected)194 static void test_erasecolor_premul(skiatest::Reporter* reporter, SkColorType ct, SkColor input,
195 SkColor expected) {
196 SkBitmap bm;
197 bm.allocPixels(SkImageInfo::Make(1, 1, ct, kPremul_SkAlphaType));
198 bm.eraseColor(input);
199 INFOF(reporter, "expected: %x actual: %x\n", expected, bm.getColor(0, 0));
200 REPORTER_ASSERT(reporter, bm.getColor(0, 0) == expected);
201 }
202
203 /**
204 * This test checks that eraseColor premultiplies the color correctly.
205 */
DEF_TEST(Bitmap_eraseColor_Premul,r)206 DEF_TEST(Bitmap_eraseColor_Premul, r) {
207 SkColor color = 0x80FF0080;
208 test_erasecolor_premul(r, kAlpha_8_SkColorType, color, 0x80000000);
209 test_erasecolor_premul(r, kRGB_565_SkColorType, color, 0xFF840042);
210 test_erasecolor_premul(r, kARGB_4444_SkColorType, color, 0x88FF0080);
211 test_erasecolor_premul(r, kRGBA_8888_SkColorType, color, color);
212 test_erasecolor_premul(r, kBGRA_8888_SkColorType, color, color);
213 }
214
215 // Test that SkBitmap::ComputeOpaque() is correct for various colortypes.
DEF_TEST(Bitmap_compute_is_opaque,r)216 DEF_TEST(Bitmap_compute_is_opaque, r) {
217
218 for (int i = 1; i <= kLastEnum_SkColorType; ++i) {
219 SkColorType ct = (SkColorType) i;
220 SkBitmap bm;
221 SkAlphaType at = SkColorTypeIsAlwaysOpaque(ct) ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
222 bm.allocPixels(SkImageInfo::Make(13, 17, ct, at));
223 bm.eraseColor(SkColorSetARGB(255, 10, 20, 30));
224 REPORTER_ASSERT(r, SkBitmap::ComputeIsOpaque(bm));
225
226 bm.eraseColor(SkColorSetARGB(128, 255, 255, 255));
227 bool isOpaque = SkBitmap::ComputeIsOpaque(bm);
228 bool shouldBeOpaque = (at == kOpaque_SkAlphaType);
229 REPORTER_ASSERT(r, isOpaque == shouldBeOpaque);
230 }
231 }
232
233 // Test that erase+getColor round trips with RGBA_F16 pixels.
DEF_TEST(Bitmap_erase_f16_erase_getColor,r)234 DEF_TEST(Bitmap_erase_f16_erase_getColor, r) {
235 SkRandom random;
236 SkPixmap pm;
237 SkBitmap bm;
238 bm.allocPixels(SkImageInfo::Make(1, 1, kRGBA_F16_SkColorType, kPremul_SkAlphaType));
239 REPORTER_ASSERT(r, bm.peekPixels(&pm));
240 for (unsigned i = 0; i < 0x100; ++i) {
241 // Test all possible values of blue component.
242 SkColor color1 = (SkColor)((random.nextU() & 0xFFFFFF00) | i);
243 // Test all possible values of alpha component.
244 SkColor color2 = (SkColor)((random.nextU() & 0x00FFFFFF) | (i << 24));
245 for (SkColor color : {color1, color2}) {
246 pm.erase(color);
247 if (SkColorGetA(color) != 0) {
248 REPORTER_ASSERT(r, color == pm.getColor(0, 0));
249 } else {
250 REPORTER_ASSERT(r, 0 == SkColorGetA(pm.getColor(0, 0)));
251 }
252 }
253 }
254 }
255
256 // Make sure that the bitmap remains valid when pixelref is removed.
DEF_TEST(Bitmap_clear_pixelref_keep_info,r)257 DEF_TEST(Bitmap_clear_pixelref_keep_info, r) {
258 SkBitmap bm;
259 bm.allocPixels(SkImageInfo::MakeN32Premul(100,100));
260 bm.setPixelRef(nullptr, 0, 0);
261 SkDEBUGCODE(bm.validate();)
262 }
263
264 // At the time of writing, SkBitmap::erase() works when the color is zero for all formats,
265 // but some formats failed when the color is non-zero!
DEF_TEST(Bitmap_erase,r)266 DEF_TEST(Bitmap_erase, r) {
267 SkColorType colorTypes[] = {
268 kRGB_565_SkColorType,
269 kARGB_4444_SkColorType,
270 kRGB_888x_SkColorType,
271 kRGBA_8888_SkColorType,
272 kBGRA_8888_SkColorType,
273 kRGB_101010x_SkColorType,
274 kRGBA_1010102_SkColorType,
275 };
276
277 for (SkColorType ct : colorTypes) {
278 SkImageInfo info = SkImageInfo::Make(1,1, (SkColorType)ct, kPremul_SkAlphaType);
279
280 SkBitmap bm;
281 bm.allocPixels(info);
282
283 bm.eraseColor(0x00000000);
284 if (SkColorTypeIsAlwaysOpaque(ct)) {
285 REPORTER_ASSERT(r, bm.getColor(0,0) == 0xff000000);
286 } else {
287 REPORTER_ASSERT(r, bm.getColor(0,0) == 0x00000000);
288 }
289
290 bm.eraseColor(0xaabbccdd);
291 REPORTER_ASSERT(r, bm.getColor(0,0) != 0xff000000);
292 REPORTER_ASSERT(r, bm.getColor(0,0) != 0x00000000);
293 }
294 }
295
check_alphas(skiatest::Reporter * reporter,const SkBitmap & bm,bool (* pred)(float expected,float actual),SkColorType ct)296 static void check_alphas(skiatest::Reporter* reporter, const SkBitmap& bm,
297 bool (*pred)(float expected, float actual), SkColorType ct) {
298 SkASSERT(bm.width() == 16);
299 SkASSERT(bm.height() == 16);
300
301 int alpha = 0;
302 for (int y = 0; y < 16; ++y) {
303 for (int x = 0; x < 16; ++x) {
304 float expected = alpha / 255.0f;
305 float actual = bm.getAlphaf(x, y);
306 if (!pred(expected, actual)) {
307 ERRORF(reporter, "%s: got %g, want %g\n",
308 ToolUtils::colortype_name(ct), actual, expected);
309 }
310 alpha += 1;
311 }
312 }
313 }
314
unit_compare(float expected,float actual,float tol=1.0f/(1<<12))315 static bool unit_compare(float expected, float actual, float tol = 1.0f/(1<<12)) {
316 SkASSERT(expected >= 0 && expected <= 1);
317 SkASSERT( actual >= 0 && actual <= 1);
318 if (expected == 0 || expected == 1) {
319 return actual == expected;
320 } else {
321 return SkScalarNearlyEqual(expected, actual, tol);
322 }
323 }
324
unit_discretize(float value,float scale)325 static float unit_discretize(float value, float scale) {
326 SkASSERT(value >= 0 && value <= 1);
327 if (value == 1) {
328 return 1;
329 } else {
330 return sk_float_floor(value * scale + 0.5f) / scale;
331 }
332 }
333
DEF_TEST(getalphaf,reporter)334 DEF_TEST(getalphaf, reporter) {
335 SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16);
336 SkBitmap bm;
337 bm.allocPixels(info);
338
339 int alpha = 0;
340 for (int y = 0; y < 16; ++y) {
341 for (int x = 0; x < 16; ++x) {
342 *bm.getAddr32(x, y) = alpha++ << 24;
343 }
344 }
345
346 auto nearly = [](float expected, float actual) -> bool {
347 return unit_compare(expected, actual);
348 };
349 auto nearly4bit = [](float expected, float actual) -> bool {
350 expected = unit_discretize(expected, 15);
351 return unit_compare(expected, actual);
352 };
353 auto nearly2bit = [](float expected, float actual) -> bool {
354 expected = unit_discretize(expected, 3);
355 return unit_compare(expected, actual);
356 };
357 auto opaque = [](float expected, float actual) -> bool {
358 return actual == 1.0f;
359 };
360
361 auto nearly_half = [](float expected, float actual) -> bool {
362 return unit_compare(expected, actual, 1.0f/(1<<10));
363 };
364
365 const struct {
366 SkColorType fColorType;
367 bool (*fPred)(float, float);
368 } recs[] = {
369 { kRGB_565_SkColorType, opaque },
370 { kGray_8_SkColorType, opaque },
371 { kR8G8_unorm_SkColorType, opaque },
372 { kR16G16_unorm_SkColorType, opaque },
373 { kR16G16_float_SkColorType, opaque },
374 { kRGB_888x_SkColorType, opaque },
375 { kRGB_101010x_SkColorType, opaque },
376
377 { kAlpha_8_SkColorType, nearly },
378 { kA16_unorm_SkColorType, nearly },
379 { kA16_float_SkColorType, nearly_half },
380 { kRGBA_8888_SkColorType, nearly },
381 { kBGRA_8888_SkColorType, nearly },
382 { kR16G16B16A16_unorm_SkColorType, nearly },
383 { kRGBA_F16_SkColorType, nearly_half },
384 { kRGBA_F32_SkColorType, nearly },
385
386 { kRGBA_1010102_SkColorType, nearly2bit },
387
388 { kARGB_4444_SkColorType, nearly4bit },
389 };
390
391 for (const auto& rec : recs) {
392 SkBitmap tmp;
393 tmp.allocPixels(bm.info().makeColorType(rec.fColorType));
394 if (bm.readPixels(tmp.pixmap())) {
395 check_alphas(reporter, tmp, rec.fPred, rec.fColorType);
396 } else {
397 SkDebugf("can't readpixels\n");
398 }
399 }
400 }
401