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/SkColor.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkMallocPixelRef.h"
15 #include "include/core/SkPixelRef.h"
16 #include "include/core/SkPixmap.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkTypes.h"
20 #include "include/private/base/SkDebug.h"
21 #include "include/private/base/SkFloatingPoint.h"
22 #include "include/private/base/SkMalloc.h"
23 #include "include/private/base/SkTo.h"
24 #include "src/base/SkRandom.h"
25 #include "tests/Test.h"
26 #include "tools/ToolUtils.h"
27
28 #include <cstddef>
29 #include <initializer_list>
30
test_peekpixels(skiatest::Reporter * reporter)31 static void test_peekpixels(skiatest::Reporter* reporter) {
32 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
33
34 SkPixmap pmap;
35 SkBitmap bm;
36
37 // empty should return false
38 REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr));
39 REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap));
40
41 // no pixels should return false
42 bm.setInfo(SkImageInfo::MakeN32Premul(10, 10));
43 REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr));
44 REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap));
45
46 // real pixels should return true
47 bm.allocPixels(info);
48 REPORTER_ASSERT(reporter, bm.peekPixels(nullptr));
49 REPORTER_ASSERT(reporter, bm.peekPixels(&pmap));
50 REPORTER_ASSERT(reporter, pmap.info() == bm.info());
51 REPORTER_ASSERT(reporter, pmap.addr() == bm.getPixels());
52 REPORTER_ASSERT(reporter, pmap.rowBytes() == bm.rowBytes());
53 }
54
55 // https://code.google.com/p/chromium/issues/detail?id=446164
test_bigalloc(skiatest::Reporter * reporter)56 static void test_bigalloc(skiatest::Reporter* reporter) {
57 const int width = 0x40000001;
58 const int height = 0x00000096;
59 const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
60
61 SkBitmap bm;
62 REPORTER_ASSERT(reporter, !bm.tryAllocPixels(info));
63
64 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, info.minRowBytes());
65 REPORTER_ASSERT(reporter, !pr);
66 }
67
test_allocpixels(skiatest::Reporter * reporter)68 static void test_allocpixels(skiatest::Reporter* reporter) {
69 const int width = 10;
70 const int height = 10;
71 const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
72 const size_t explicitRowBytes = info.minRowBytes() + 24;
73
74 SkBitmap bm;
75 bm.setInfo(info);
76 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
77 bm.allocPixels();
78 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
79 bm.reset();
80 bm.allocPixels(info);
81 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
82
83 bm.setInfo(info, explicitRowBytes);
84 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
85 bm.allocPixels();
86 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
87 bm.reset();
88 bm.allocPixels(info, explicitRowBytes);
89 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
90
91 bm.reset();
92 bm.setInfo(info, 0);
93 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
94 bm.reset();
95 bm.allocPixels(info, 0);
96 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
97
98 bm.reset();
99 bool success = bm.setInfo(info, info.minRowBytes() - 1); // invalid for 32bit
100 REPORTER_ASSERT(reporter, !success);
101 REPORTER_ASSERT(reporter, bm.isNull());
102
103 for (SkColorType ct : {
104 kAlpha_8_SkColorType,
105 kRGB_565_SkColorType,
106 kARGB_4444_SkColorType,
107 kRGBA_8888_SkColorType,
108 kBGRA_8888_SkColorType,
109 kRGB_888x_SkColorType,
110 kRGBA_1010102_SkColorType,
111 kRGB_101010x_SkColorType,
112 kGray_8_SkColorType,
113 kRGBA_F16Norm_SkColorType,
114 kRGBA_F16_SkColorType,
115 kRGBA_F32_SkColorType,
116 kR8G8_unorm_SkColorType,
117 kA16_unorm_SkColorType,
118 kR16G16_unorm_SkColorType,
119 kA16_float_SkColorType,
120 kR16G16_float_SkColorType,
121 kR16G16B16A16_unorm_SkColorType,
122 }) {
123 SkImageInfo imageInfo = info.makeColorType(ct);
124 for (int rowBytesPadding = 1; rowBytesPadding <= 17; rowBytesPadding++) {
125 bm.reset();
126 success = bm.setInfo(imageInfo, imageInfo.minRowBytes() + rowBytesPadding);
127 if (rowBytesPadding % imageInfo.bytesPerPixel() == 0) {
128 REPORTER_ASSERT(reporter, success);
129 success = bm.tryAllocPixels();
130 REPORTER_ASSERT(reporter, success);
131 } else {
132 // Not pixel aligned.
133 REPORTER_ASSERT(reporter, !success);
134 REPORTER_ASSERT(reporter, bm.isNull());
135 }
136 }
137 }
138 }
139
test_bigwidth(skiatest::Reporter * reporter)140 static void test_bigwidth(skiatest::Reporter* reporter) {
141 SkBitmap bm;
142 int width = 1 << 29; // *4 will be the high-bit of 32bit int
143
144 SkImageInfo info = SkImageInfo::MakeA8(width, 1);
145 REPORTER_ASSERT(reporter, bm.setInfo(info));
146 REPORTER_ASSERT(reporter, bm.setInfo(info.makeColorType(kRGB_565_SkColorType)));
147
148 // for a 4-byte config, this width will compute a rowbytes of 0x80000000,
149 // which does not fit in a int32_t. setConfig should detect this, and fail.
150
151 // TODO: perhaps skia can relax this, and only require that rowBytes fit
152 // in a uint32_t (or larger), but for now this is the constraint.
153
154 REPORTER_ASSERT(reporter, !bm.setInfo(info.makeColorType(kN32_SkColorType)));
155 }
156
DEF_TEST(Bitmap,reporter)157 DEF_TEST(Bitmap, reporter) {
158 // Zero-sized bitmaps are allowed
159 for (int width = 0; width < 2; ++width) {
160 for (int height = 0; height < 2; ++height) {
161 SkBitmap bm;
162 bool setConf = bm.setInfo(SkImageInfo::MakeN32Premul(width, height));
163 REPORTER_ASSERT(reporter, setConf);
164 if (setConf) {
165 bm.allocPixels();
166 }
167 REPORTER_ASSERT(reporter, SkToBool(width & height) != bm.empty());
168 }
169 }
170
171 test_bigwidth(reporter);
172 test_allocpixels(reporter);
173 test_bigalloc(reporter);
174 test_peekpixels(reporter);
175 }
176
DEF_TEST(Bitmap_setColorSpace,r)177 DEF_TEST(Bitmap_setColorSpace, r) {
178 // Make a 1x1 bitmap holding 50% gray in default colorspace.
179 SkBitmap source;
180 source.allocN32Pixels(1,1);
181 source.eraseColor(SkColorSetARGB(0xFF, 0x80, 0x80, 0x80));
182
183 // Readback should use the normal sRGB colorspace.
184 const SkImageInfo kReadbackInfo = SkImageInfo::Make(/*width=*/1,
185 /*height=*/1,
186 kN32_SkColorType,
187 kOpaque_SkAlphaType,
188 SkColorSpace::MakeSRGB());
189 // Do readback and verify that the color is gray.
190 uint8_t pixelData[4];
191 REPORTER_ASSERT(r, source.readPixels(kReadbackInfo,
192 pixelData,
193 /*dstRowBytes=*/4,
194 /*srcX=*/0,
195 /*srcY=*/0));
196 REPORTER_ASSERT(r, pixelData[0] == 0x80);
197 REPORTER_ASSERT(r, pixelData[1] == 0x80);
198 REPORTER_ASSERT(r, pixelData[2] == 0x80);
199
200 // Also check the color with getColor4f, which does not honor colorspaces.
201 uint32_t colorRGBA = source.getColor4f(0, 0).toBytes_RGBA();
202 REPORTER_ASSERT(r, colorRGBA == 0xFF808080, "RGBA=%08X", colorRGBA);
203
204 // Convert the SkBitmap's colorspace to linear.
205 source.setColorSpace(SkColorSpace::MakeSRGBLinear());
206
207 // Readback again and verify that the color is interpreted differently.
208 REPORTER_ASSERT(r, source.readPixels(kReadbackInfo,
209 pixelData,
210 /*dstRowBytes=*/4,
211 /*srcX=*/0,
212 /*srcY=*/0));
213 REPORTER_ASSERT(r, pixelData[0] == 0xBC, "R:%02X", pixelData[0]);
214 REPORTER_ASSERT(r, pixelData[1] == 0xBC, "G:%02X", pixelData[1]);
215 REPORTER_ASSERT(r, pixelData[2] == 0xBC, "B:%02X", pixelData[2]);
216
217 // Since getColor4f does not honor colorspaces, this should still contain 50% gray.
218 colorRGBA = source.getColor4f(0, 0).toBytes_RGBA();
219 REPORTER_ASSERT(r, colorRGBA == 0xFF808080, "RGBA=%08X", colorRGBA);
220 }
221
222 /**
223 * This test checks that getColor works for both swizzles.
224 */
DEF_TEST(Bitmap_getColor_Swizzle,r)225 DEF_TEST(Bitmap_getColor_Swizzle, r) {
226 SkBitmap source;
227 source.allocN32Pixels(1,1);
228 source.eraseColor(SK_ColorRED);
229 SkColorType colorTypes[] = {
230 kRGBA_8888_SkColorType,
231 kBGRA_8888_SkColorType,
232 };
233 for (SkColorType ct : colorTypes) {
234 SkBitmap copy;
235 if (!ToolUtils::copy_to(©, ct, source)) {
236 ERRORF(r, "SkBitmap::copy failed %d", (int)ct);
237 continue;
238 }
239 REPORTER_ASSERT(r, source.getColor(0, 0) == copy.getColor(0, 0));
240 }
241 }
242
test_erasecolor_premul(skiatest::Reporter * reporter,SkColorType ct,SkColor input,SkColor expected)243 static void test_erasecolor_premul(skiatest::Reporter* reporter, SkColorType ct, SkColor input,
244 SkColor expected) {
245 SkBitmap bm;
246 bm.allocPixels(SkImageInfo::Make(1, 1, ct, kPremul_SkAlphaType));
247 bm.eraseColor(input);
248 INFOF(reporter, "expected: %x actual: %x\n", expected, bm.getColor(0, 0));
249 REPORTER_ASSERT(reporter, bm.getColor(0, 0) == expected);
250 }
251
252 /**
253 * This test checks that eraseColor premultiplies the color correctly.
254 */
DEF_TEST(Bitmap_eraseColor_Premul,r)255 DEF_TEST(Bitmap_eraseColor_Premul, r) {
256 SkColor color = 0x80FF0080;
257 test_erasecolor_premul(r, kAlpha_8_SkColorType, color, 0x80000000);
258 test_erasecolor_premul(r, kRGB_565_SkColorType, color, 0xFF840042);
259 test_erasecolor_premul(r, kARGB_4444_SkColorType, color, 0x88FF0080);
260 test_erasecolor_premul(r, kRGBA_8888_SkColorType, color, color);
261 test_erasecolor_premul(r, kBGRA_8888_SkColorType, color, color);
262 }
263
264 // Test that SkBitmap::ComputeOpaque() is correct for various colortypes.
DEF_TEST(Bitmap_compute_is_opaque,r)265 DEF_TEST(Bitmap_compute_is_opaque, r) {
266
267 for (int i = 1; i <= kLastEnum_SkColorType; ++i) {
268 SkColorType ct = (SkColorType) i;
269 SkBitmap bm;
270 SkAlphaType at = SkColorTypeIsAlwaysOpaque(ct) ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
271 bm.allocPixels(SkImageInfo::Make(13, 17, ct, at));
272 bm.eraseColor(SkColorSetARGB(255, 10, 20, 30));
273 REPORTER_ASSERT(r, SkBitmap::ComputeIsOpaque(bm));
274
275 bm.eraseColor(SkColorSetARGB(128, 255, 255, 255));
276 bool isOpaque = SkBitmap::ComputeIsOpaque(bm);
277 bool shouldBeOpaque = (at == kOpaque_SkAlphaType);
278 REPORTER_ASSERT(r, isOpaque == shouldBeOpaque);
279 }
280 }
281
282 // Test that erase+getColor round trips with RGBA_F16 pixels.
DEF_TEST(Bitmap_erase_f16_erase_getColor,r)283 DEF_TEST(Bitmap_erase_f16_erase_getColor, r) {
284 SkRandom random;
285 SkPixmap pm;
286 SkBitmap bm;
287 bm.allocPixels(SkImageInfo::Make(1, 1, kRGBA_F16_SkColorType, kPremul_SkAlphaType));
288 REPORTER_ASSERT(r, bm.peekPixels(&pm));
289 for (unsigned i = 0; i < 0x100; ++i) {
290 // Test all possible values of blue component.
291 SkColor color1 = (SkColor)((random.nextU() & 0xFFFFFF00) | i);
292 // Test all possible values of alpha component.
293 SkColor color2 = (SkColor)((random.nextU() & 0x00FFFFFF) | (i << 24));
294 for (SkColor color : {color1, color2}) {
295 pm.erase(color);
296 if (SkColorGetA(color) != 0) {
297 REPORTER_ASSERT(r, color == pm.getColor(0, 0));
298 } else {
299 REPORTER_ASSERT(r, 0 == SkColorGetA(pm.getColor(0, 0)));
300 }
301 }
302 }
303 }
304
305 // Verify that SkBitmap::erase erases in SRGB, regardless of the SkColorSpace of the
306 // SkBitmap.
DEF_TEST(Bitmap_erase_srgb,r)307 DEF_TEST(Bitmap_erase_srgb, r) {
308 SkBitmap bm;
309 // Use a color spin from SRGB.
310 bm.allocPixels(SkImageInfo::Make(1, 1, kN32_SkColorType, kPremul_SkAlphaType,
311 SkColorSpace::MakeSRGB()->makeColorSpin()));
312 // RED will be converted into the spun color space.
313 bm.eraseColor(SK_ColorRED);
314 // getColor doesn't take the color space into account, so the returned color
315 // is different due to the color spin.
316 REPORTER_ASSERT(r, bm.getColor(0, 0) == SK_ColorBLUE);
317 }
318
319 // Make sure that the bitmap remains valid when pixelref is removed.
DEF_TEST(Bitmap_clear_pixelref_keep_info,r)320 DEF_TEST(Bitmap_clear_pixelref_keep_info, r) {
321 SkBitmap bm;
322 bm.allocPixels(SkImageInfo::MakeN32Premul(100,100));
323 bm.setPixelRef(nullptr, 0, 0);
324 SkDEBUGCODE(bm.validate();)
325 }
326
327 // At the time of writing, SkBitmap::erase() works when the color is zero for all formats,
328 // but some formats failed when the color is non-zero!
DEF_TEST(Bitmap_erase,r)329 DEF_TEST(Bitmap_erase, r) {
330 SkColorType colorTypes[] = {
331 kRGB_565_SkColorType,
332 kARGB_4444_SkColorType,
333 kRGB_888x_SkColorType,
334 kRGBA_8888_SkColorType,
335 kBGRA_8888_SkColorType,
336 kRGB_101010x_SkColorType,
337 kRGBA_1010102_SkColorType,
338 };
339
340 for (SkColorType ct : colorTypes) {
341 SkImageInfo info = SkImageInfo::Make(1,1, (SkColorType)ct, kPremul_SkAlphaType);
342
343 SkBitmap bm;
344 bm.allocPixels(info);
345
346 bm.eraseColor(0x00000000);
347 if (SkColorTypeIsAlwaysOpaque(ct)) {
348 REPORTER_ASSERT(r, bm.getColor(0,0) == 0xff000000);
349 } else {
350 REPORTER_ASSERT(r, bm.getColor(0,0) == 0x00000000);
351 }
352
353 bm.eraseColor(0xaabbccdd);
354 REPORTER_ASSERT(r, bm.getColor(0,0) != 0xff000000);
355 REPORTER_ASSERT(r, bm.getColor(0,0) != 0x00000000);
356 }
357 }
358
check_alphas(skiatest::Reporter * reporter,const SkBitmap & bm,bool (* pred)(float expected,float actual),SkColorType ct)359 static void check_alphas(skiatest::Reporter* reporter, const SkBitmap& bm,
360 bool (*pred)(float expected, float actual), SkColorType ct) {
361 SkASSERT(bm.width() == 16);
362 SkASSERT(bm.height() == 16);
363
364 int alpha = 0;
365 for (int y = 0; y < 16; ++y) {
366 for (int x = 0; x < 16; ++x) {
367 float expected = alpha / 255.0f;
368 float actual = bm.getAlphaf(x, y);
369 if (!pred(expected, actual)) {
370 ERRORF(reporter, "%s: got %g, want %g\n",
371 ToolUtils::colortype_name(ct), actual, expected);
372 }
373 alpha += 1;
374 }
375 }
376 }
377
unit_compare(float expected,float actual,float tol=1.0f/(1<<12))378 static bool unit_compare(float expected, float actual, float tol = 1.0f/(1<<12)) {
379 SkASSERT(expected >= 0 && expected <= 1);
380 SkASSERT( actual >= 0 && actual <= 1);
381 if (expected == 0 || expected == 1) {
382 return actual == expected;
383 } else {
384 return SkScalarNearlyEqual(expected, actual, tol);
385 }
386 }
387
unit_discretize(float value,float scale)388 static float unit_discretize(float value, float scale) {
389 SkASSERT(value >= 0 && value <= 1);
390 if (value == 1) {
391 return 1;
392 } else {
393 return std::floor(value * scale + 0.5f) / scale;
394 }
395 }
396
DEF_TEST(getalphaf,reporter)397 DEF_TEST(getalphaf, reporter) {
398 SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16);
399 SkBitmap bm;
400 bm.allocPixels(info);
401
402 int alpha = 0;
403 for (int y = 0; y < 16; ++y) {
404 for (int x = 0; x < 16; ++x) {
405 *bm.getAddr32(x, y) = alpha++ << 24;
406 }
407 }
408
409 auto nearly = [](float expected, float actual) -> bool {
410 return unit_compare(expected, actual);
411 };
412 auto nearly4bit = [](float expected, float actual) -> bool {
413 expected = unit_discretize(expected, 15);
414 return unit_compare(expected, actual);
415 };
416 auto nearly2bit = [](float expected, float actual) -> bool {
417 expected = unit_discretize(expected, 3);
418 return unit_compare(expected, actual);
419 };
420 auto opaque = [](float expected, float actual) -> bool {
421 return actual == 1.0f;
422 };
423
424 auto nearly_half = [](float expected, float actual) -> bool {
425 return unit_compare(expected, actual, 1.0f/(1<<10));
426 };
427
428 const struct {
429 SkColorType fColorType;
430 bool (*fPred)(float, float);
431 } recs[] = {
432 { kRGB_565_SkColorType, opaque },
433 { kGray_8_SkColorType, opaque },
434 { kR8G8_unorm_SkColorType, opaque },
435 { kR16G16_unorm_SkColorType, opaque },
436 { kR16G16_float_SkColorType, opaque },
437 { kRGB_888x_SkColorType, opaque },
438 { kRGB_101010x_SkColorType, opaque },
439 { kRGB_F16F16F16x_SkColorType, opaque },
440
441 { kAlpha_8_SkColorType, nearly },
442 { kA16_unorm_SkColorType, nearly },
443 { kA16_float_SkColorType, nearly_half },
444 { kRGBA_8888_SkColorType, nearly },
445 { kBGRA_8888_SkColorType, nearly },
446 { kR16G16B16A16_unorm_SkColorType, nearly },
447 { kRGBA_F16_SkColorType, nearly_half },
448 { kRGBA_F32_SkColorType, nearly },
449
450 { kRGBA_1010102_SkColorType, nearly2bit },
451
452 { kARGB_4444_SkColorType, nearly4bit },
453 };
454
455 for (const auto& rec : recs) {
456 SkBitmap tmp;
457 tmp.allocPixels(bm.info().makeColorType(rec.fColorType));
458 if (bm.readPixels(tmp.pixmap())) {
459 check_alphas(reporter, tmp, rec.fPred, rec.fColorType);
460 } else {
461 SkDebugf("can't readpixels\n");
462 }
463 }
464 }
465
466 /* computeByteSize() is documented to return 0 if height is zero, but does not
467 * special-case width==0, so computeByteSize() can return non-zero for that
468 * (since it is defined to return (height-1)*rb + ...
469 *
470 * Test that allocPixels() respects this, and allocates a buffer as large as
471 * computeByteSize()... even though the bitmap is logicallly empty.
472 */
DEF_TEST(bitmap_zerowidth_crbug_1103827,reporter)473 DEF_TEST(bitmap_zerowidth_crbug_1103827, reporter) {
474 const size_t big_rb = 1 << 16;
475
476 struct {
477 int width, height;
478 size_t rowbytes, expected_size;
479 } rec[] = {
480 { 2, 0, big_rb, 0 }, // zero-height means zero-size
481 { 0, 2, big_rb, big_rb }, // zero-width is computed normally
482 };
483
484 for (const auto& r : rec) {
485 auto info = SkImageInfo::Make(r.width, r.height,
486 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
487 size_t size = info.computeByteSize(r.rowbytes);
488 REPORTER_ASSERT(reporter, size == r.expected_size);
489
490 SkBitmap bm;
491 bm.setInfo(info, r.rowbytes);
492 REPORTER_ASSERT(reporter, size == bm.computeByteSize());
493
494 // Be sure we can actually write to that much memory. If the bitmap underallocated
495 // the buffer, this should trash memory and crash (we hope).
496 bm.allocPixels();
497 sk_bzero(bm.getPixels(), size);
498 }
499 }
500