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/SkFloatingPoint.h"
21 #include "include/private/base/SkMalloc.h"
22 #include "include/private/base/SkDebug.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
177 /**
178 * This test checks that getColor works for both swizzles.
179 */
DEF_TEST(Bitmap_getColor_Swizzle,r)180 DEF_TEST(Bitmap_getColor_Swizzle, r) {
181 SkBitmap source;
182 source.allocN32Pixels(1,1);
183 source.eraseColor(SK_ColorRED);
184 SkColorType colorTypes[] = {
185 kRGBA_8888_SkColorType,
186 kBGRA_8888_SkColorType,
187 };
188 for (SkColorType ct : colorTypes) {
189 SkBitmap copy;
190 if (!ToolUtils::copy_to(©, ct, source)) {
191 ERRORF(r, "SkBitmap::copy failed %d", (int)ct);
192 continue;
193 }
194 REPORTER_ASSERT(r, source.getColor(0, 0) == copy.getColor(0, 0));
195 }
196 }
197
test_erasecolor_premul(skiatest::Reporter * reporter,SkColorType ct,SkColor input,SkColor expected)198 static void test_erasecolor_premul(skiatest::Reporter* reporter, SkColorType ct, SkColor input,
199 SkColor expected) {
200 SkBitmap bm;
201 bm.allocPixels(SkImageInfo::Make(1, 1, ct, kPremul_SkAlphaType));
202 bm.eraseColor(input);
203 INFOF(reporter, "expected: %x actual: %x\n", expected, bm.getColor(0, 0));
204 REPORTER_ASSERT(reporter, bm.getColor(0, 0) == expected);
205 }
206
207 /**
208 * This test checks that eraseColor premultiplies the color correctly.
209 */
DEF_TEST(Bitmap_eraseColor_Premul,r)210 DEF_TEST(Bitmap_eraseColor_Premul, r) {
211 SkColor color = 0x80FF0080;
212 test_erasecolor_premul(r, kAlpha_8_SkColorType, color, 0x80000000);
213 test_erasecolor_premul(r, kRGB_565_SkColorType, color, 0xFF840042);
214 test_erasecolor_premul(r, kARGB_4444_SkColorType, color, 0x88FF0080);
215 test_erasecolor_premul(r, kRGBA_8888_SkColorType, color, color);
216 test_erasecolor_premul(r, kBGRA_8888_SkColorType, color, color);
217 }
218
219 // Test that SkBitmap::ComputeOpaque() is correct for various colortypes.
DEF_TEST(Bitmap_compute_is_opaque,r)220 DEF_TEST(Bitmap_compute_is_opaque, r) {
221
222 for (int i = 1; i <= kLastEnum_SkColorType; ++i) {
223 SkColorType ct = (SkColorType) i;
224 SkBitmap bm;
225 SkAlphaType at = SkColorTypeIsAlwaysOpaque(ct) ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
226 bm.allocPixels(SkImageInfo::Make(13, 17, ct, at));
227 bm.eraseColor(SkColorSetARGB(255, 10, 20, 30));
228 REPORTER_ASSERT(r, SkBitmap::ComputeIsOpaque(bm));
229
230 bm.eraseColor(SkColorSetARGB(128, 255, 255, 255));
231 bool isOpaque = SkBitmap::ComputeIsOpaque(bm);
232 bool shouldBeOpaque = (at == kOpaque_SkAlphaType);
233 REPORTER_ASSERT(r, isOpaque == shouldBeOpaque);
234 }
235 }
236
237 // Test that erase+getColor round trips with RGBA_F16 pixels.
DEF_TEST(Bitmap_erase_f16_erase_getColor,r)238 DEF_TEST(Bitmap_erase_f16_erase_getColor, r) {
239 SkRandom random;
240 SkPixmap pm;
241 SkBitmap bm;
242 bm.allocPixels(SkImageInfo::Make(1, 1, kRGBA_F16_SkColorType, kPremul_SkAlphaType));
243 REPORTER_ASSERT(r, bm.peekPixels(&pm));
244 for (unsigned i = 0; i < 0x100; ++i) {
245 // Test all possible values of blue component.
246 SkColor color1 = (SkColor)((random.nextU() & 0xFFFFFF00) | i);
247 // Test all possible values of alpha component.
248 SkColor color2 = (SkColor)((random.nextU() & 0x00FFFFFF) | (i << 24));
249 for (SkColor color : {color1, color2}) {
250 pm.erase(color);
251 if (SkColorGetA(color) != 0) {
252 REPORTER_ASSERT(r, color == pm.getColor(0, 0));
253 } else {
254 REPORTER_ASSERT(r, 0 == SkColorGetA(pm.getColor(0, 0)));
255 }
256 }
257 }
258 }
259
260 // Verify that SkBitmap::erase erases in SRGB, regardless of the SkColorSpace of the
261 // SkBitmap.
DEF_TEST(Bitmap_erase_srgb,r)262 DEF_TEST(Bitmap_erase_srgb, r) {
263 SkBitmap bm;
264 // Use a color spin from SRGB.
265 bm.allocPixels(SkImageInfo::Make(1, 1, kN32_SkColorType, kPremul_SkAlphaType,
266 SkColorSpace::MakeSRGB()->makeColorSpin()));
267 // RED will be converted into the spun color space.
268 bm.eraseColor(SK_ColorRED);
269 // getColor doesn't take the color space into account, so the returned color
270 // is different due to the color spin.
271 REPORTER_ASSERT(r, bm.getColor(0, 0) == SK_ColorBLUE);
272 }
273
274 // Make sure that the bitmap remains valid when pixelref is removed.
DEF_TEST(Bitmap_clear_pixelref_keep_info,r)275 DEF_TEST(Bitmap_clear_pixelref_keep_info, r) {
276 SkBitmap bm;
277 bm.allocPixels(SkImageInfo::MakeN32Premul(100,100));
278 bm.setPixelRef(nullptr, 0, 0);
279 SkDEBUGCODE(bm.validate();)
280 }
281
282 // At the time of writing, SkBitmap::erase() works when the color is zero for all formats,
283 // but some formats failed when the color is non-zero!
DEF_TEST(Bitmap_erase,r)284 DEF_TEST(Bitmap_erase, r) {
285 SkColorType colorTypes[] = {
286 kRGB_565_SkColorType,
287 kARGB_4444_SkColorType,
288 kRGB_888x_SkColorType,
289 kRGBA_8888_SkColorType,
290 kBGRA_8888_SkColorType,
291 kRGB_101010x_SkColorType,
292 kRGBA_1010102_SkColorType,
293 };
294
295 for (SkColorType ct : colorTypes) {
296 SkImageInfo info = SkImageInfo::Make(1,1, (SkColorType)ct, kPremul_SkAlphaType);
297
298 SkBitmap bm;
299 bm.allocPixels(info);
300
301 bm.eraseColor(0x00000000);
302 if (SkColorTypeIsAlwaysOpaque(ct)) {
303 REPORTER_ASSERT(r, bm.getColor(0,0) == 0xff000000);
304 } else {
305 REPORTER_ASSERT(r, bm.getColor(0,0) == 0x00000000);
306 }
307
308 bm.eraseColor(0xaabbccdd);
309 REPORTER_ASSERT(r, bm.getColor(0,0) != 0xff000000);
310 REPORTER_ASSERT(r, bm.getColor(0,0) != 0x00000000);
311 }
312 }
313
check_alphas(skiatest::Reporter * reporter,const SkBitmap & bm,bool (* pred)(float expected,float actual),SkColorType ct)314 static void check_alphas(skiatest::Reporter* reporter, const SkBitmap& bm,
315 bool (*pred)(float expected, float actual), SkColorType ct) {
316 SkASSERT(bm.width() == 16);
317 SkASSERT(bm.height() == 16);
318
319 int alpha = 0;
320 for (int y = 0; y < 16; ++y) {
321 for (int x = 0; x < 16; ++x) {
322 float expected = alpha / 255.0f;
323 float actual = bm.getAlphaf(x, y);
324 if (!pred(expected, actual)) {
325 ERRORF(reporter, "%s: got %g, want %g\n",
326 ToolUtils::colortype_name(ct), actual, expected);
327 }
328 alpha += 1;
329 }
330 }
331 }
332
unit_compare(float expected,float actual,float tol=1.0f/(1<<12))333 static bool unit_compare(float expected, float actual, float tol = 1.0f/(1<<12)) {
334 SkASSERT(expected >= 0 && expected <= 1);
335 SkASSERT( actual >= 0 && actual <= 1);
336 if (expected == 0 || expected == 1) {
337 return actual == expected;
338 } else {
339 return SkScalarNearlyEqual(expected, actual, tol);
340 }
341 }
342
unit_discretize(float value,float scale)343 static float unit_discretize(float value, float scale) {
344 SkASSERT(value >= 0 && value <= 1);
345 if (value == 1) {
346 return 1;
347 } else {
348 return sk_float_floor(value * scale + 0.5f) / scale;
349 }
350 }
351
DEF_TEST(getalphaf,reporter)352 DEF_TEST(getalphaf, reporter) {
353 SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16);
354 SkBitmap bm;
355 bm.allocPixels(info);
356
357 int alpha = 0;
358 for (int y = 0; y < 16; ++y) {
359 for (int x = 0; x < 16; ++x) {
360 *bm.getAddr32(x, y) = alpha++ << 24;
361 }
362 }
363
364 auto nearly = [](float expected, float actual) -> bool {
365 return unit_compare(expected, actual);
366 };
367 auto nearly4bit = [](float expected, float actual) -> bool {
368 expected = unit_discretize(expected, 15);
369 return unit_compare(expected, actual);
370 };
371 auto nearly2bit = [](float expected, float actual) -> bool {
372 expected = unit_discretize(expected, 3);
373 return unit_compare(expected, actual);
374 };
375 auto opaque = [](float expected, float actual) -> bool {
376 return actual == 1.0f;
377 };
378
379 auto nearly_half = [](float expected, float actual) -> bool {
380 return unit_compare(expected, actual, 1.0f/(1<<10));
381 };
382
383 const struct {
384 SkColorType fColorType;
385 bool (*fPred)(float, float);
386 } recs[] = {
387 { kRGB_565_SkColorType, opaque },
388 { kGray_8_SkColorType, opaque },
389 { kR8G8_unorm_SkColorType, opaque },
390 { kR16G16_unorm_SkColorType, opaque },
391 { kR16G16_float_SkColorType, opaque },
392 { kRGB_888x_SkColorType, opaque },
393 { kRGB_101010x_SkColorType, opaque },
394
395 { kAlpha_8_SkColorType, nearly },
396 { kA16_unorm_SkColorType, nearly },
397 { kA16_float_SkColorType, nearly_half },
398 { kRGBA_8888_SkColorType, nearly },
399 { kBGRA_8888_SkColorType, nearly },
400 { kR16G16B16A16_unorm_SkColorType, nearly },
401 { kRGBA_F16_SkColorType, nearly_half },
402 { kRGBA_F32_SkColorType, nearly },
403
404 { kRGBA_1010102_SkColorType, nearly2bit },
405
406 { kARGB_4444_SkColorType, nearly4bit },
407 };
408
409 for (const auto& rec : recs) {
410 SkBitmap tmp;
411 tmp.allocPixels(bm.info().makeColorType(rec.fColorType));
412 if (bm.readPixels(tmp.pixmap())) {
413 check_alphas(reporter, tmp, rec.fPred, rec.fColorType);
414 } else {
415 SkDebugf("can't readpixels\n");
416 }
417 }
418 }
419
420 /* computeByteSize() is documented to return 0 if height is zero, but does not
421 * special-case width==0, so computeByteSize() can return non-zero for that
422 * (since it is defined to return (height-1)*rb + ...
423 *
424 * Test that allocPixels() respects this, and allocates a buffer as large as
425 * computeByteSize()... even though the bitmap is logicallly empty.
426 */
DEF_TEST(bitmap_zerowidth_crbug_1103827,reporter)427 DEF_TEST(bitmap_zerowidth_crbug_1103827, reporter) {
428 const size_t big_rb = 1 << 16;
429
430 struct {
431 int width, height;
432 size_t rowbytes, expected_size;
433 } rec[] = {
434 { 2, 0, big_rb, 0 }, // zero-height means zero-size
435 { 0, 2, big_rb, big_rb }, // zero-width is computed normally
436 };
437
438 for (const auto& r : rec) {
439 auto info = SkImageInfo::Make(r.width, r.height,
440 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
441 size_t size = info.computeByteSize(r.rowbytes);
442 REPORTER_ASSERT(reporter, size == r.expected_size);
443
444 SkBitmap bm;
445 bm.setInfo(info, r.rowbytes);
446 REPORTER_ASSERT(reporter, size == bm.computeByteSize());
447
448 // Be sure we can actually write to that much memory. If the bitmap underallocated
449 // the buffer, this should trash memory and crash (we hope).
450 bm.allocPixels();
451 sk_bzero(bm.getPixels(), size);
452 }
453 }
454