blob: e21d7275b201e27527ef2183a6b8df99b787c785 [file] [log] [blame]
halcanary@google.com44287342013-12-13 18:29:51 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
Ben Wagner9707a7e2019-05-06 17:17:19 -040015#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkTypes.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040017#include "include/private/SkFloatingPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/utils/SkRandom.h"
19#include "tests/Test.h"
20#include "tools/ToolUtils.h"
halcanary@google.com44287342013-12-13 18:29:51 +000021
Ben Wagner9707a7e2019-05-06 17:17:19 -040022#include <initializer_list>
23
reedcb674142015-06-05 06:58:22 -070024static 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
halcanary96fcdcc2015-08-27 07:41:13 -070031 REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr));
reedcb674142015-06-05 06:58:22 -070032 REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap));
33
34 // no pixels should return false
35 bm.setInfo(SkImageInfo::MakeN32Premul(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -070036 REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr));
reedcb674142015-06-05 06:58:22 -070037 REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap));
38
39 // real pixels should return true
40 bm.allocPixels(info);
halcanary96fcdcc2015-08-27 07:41:13 -070041 REPORTER_ASSERT(reporter, bm.peekPixels(nullptr));
reedcb674142015-06-05 06:58:22 -070042 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());
reedcb674142015-06-05 06:58:22 -070046}
47
reed2ff257b2015-01-23 07:51:14 -080048// https://code.google.com/p/chromium/issues/detail?id=446164
49static 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
Mike Reed086a4272017-07-18 10:53:11 -040057 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, info.minRowBytes());
reed2ff257b2015-01-23 07:51:14 -080058 REPORTER_ASSERT(reporter, !pr);
59}
60
reedf0aed972014-07-01 12:48:11 -070061static 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
reed@google.com48569642013-12-30 19:21:22 +000097static void test_bigwidth(skiatest::Reporter* reporter) {
98 SkBitmap bm;
99 int width = 1 << 29; // *4 will be the high-bit of 32bit int
100
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000101 SkImageInfo info = SkImageInfo::MakeA8(width, 1);
102 REPORTER_ASSERT(reporter, bm.setInfo(info));
reede5ea5002014-09-03 11:54:58 -0700103 REPORTER_ASSERT(reporter, bm.setInfo(info.makeColorType(kRGB_565_SkColorType)));
skia.committer@gmail.comf5e1f632013-12-31 07:01:36 +0000104
reed@google.com48569642013-12-30 19:21:22 +0000105 // for a 4-byte config, this width will compute a rowbytes of 0x80000000,
106 // which does not fit in a int32_t. setConfig should detect this, and fail.
107
108 // TODO: perhaps skia can relax this, and only require that rowBytes fit
109 // in a uint32_t (or larger), but for now this is the constraint.
110
reede5ea5002014-09-03 11:54:58 -0700111 REPORTER_ASSERT(reporter, !bm.setInfo(info.makeColorType(kN32_SkColorType)));
reed@google.com48569642013-12-30 19:21:22 +0000112}
113
halcanary@google.com44287342013-12-13 18:29:51 +0000114/**
115 * This test contains basic sanity checks concerning bitmaps.
116 */
117DEF_TEST(Bitmap, reporter) {
halcanary@google.com44287342013-12-13 18:29:51 +0000118 // Zero-sized bitmaps are allowed
119 for (int width = 0; width < 2; ++width) {
120 for (int height = 0; height < 2; ++height) {
121 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000122 bool setConf = bm.setInfo(SkImageInfo::MakeN32Premul(width, height));
halcanary@google.com44287342013-12-13 18:29:51 +0000123 REPORTER_ASSERT(reporter, setConf);
124 if (setConf) {
reed84825042014-09-02 12:50:45 -0700125 bm.allocPixels();
halcanary@google.com44287342013-12-13 18:29:51 +0000126 }
halcanary@google.com2af6c122013-12-13 19:25:21 +0000127 REPORTER_ASSERT(reporter, SkToBool(width & height) != bm.empty());
halcanary@google.com44287342013-12-13 18:29:51 +0000128 }
129 }
skia.committer@gmail.comf5e1f632013-12-31 07:01:36 +0000130
reed@google.com48569642013-12-30 19:21:22 +0000131 test_bigwidth(reporter);
reedf0aed972014-07-01 12:48:11 -0700132 test_allocpixels(reporter);
reed2ff257b2015-01-23 07:51:14 -0800133 test_bigalloc(reporter);
reedcb674142015-06-05 06:58:22 -0700134 test_peekpixels(reporter);
halcanary@google.com44287342013-12-13 18:29:51 +0000135}
halcanaryb260d132015-12-09 10:21:59 -0800136
137/**
138 * This test checks that getColor works for both swizzles.
139 */
140DEF_TEST(Bitmap_getColor_Swizzle, r) {
141 SkBitmap source;
142 source.allocN32Pixels(1,1);
143 source.eraseColor(SK_ColorRED);
144 SkColorType colorTypes[] = {
145 kRGBA_8888_SkColorType,
146 kBGRA_8888_SkColorType,
147 };
148 for (SkColorType ct : colorTypes) {
149 SkBitmap copy;
Mike Kleinea3f0142019-03-20 11:12:10 -0500150 if (!ToolUtils::copy_to(&copy, ct, source)) {
halcanaryb260d132015-12-09 10:21:59 -0800151 ERRORF(r, "SkBitmap::copy failed %d", (int)ct);
152 continue;
153 }
halcanaryb260d132015-12-09 10:21:59 -0800154 REPORTER_ASSERT(r, source.getColor(0, 0) == copy.getColor(0, 0));
155 }
156}
benjaminwagnera1bb8e02015-12-11 14:08:58 -0800157
158static void test_erasecolor_premul(skiatest::Reporter* reporter, SkColorType ct, SkColor input,
159 SkColor expected) {
160 SkBitmap bm;
161 bm.allocPixels(SkImageInfo::Make(1, 1, ct, kPremul_SkAlphaType));
162 bm.eraseColor(input);
halcanary7d571242016-02-24 17:59:16 -0800163 INFOF(reporter, "expected: %x actual: %x\n", expected, bm.getColor(0, 0));
benjaminwagnera1bb8e02015-12-11 14:08:58 -0800164 REPORTER_ASSERT(reporter, bm.getColor(0, 0) == expected);
165}
166
167/**
168 * This test checks that eraseColor premultiplies the color correctly.
169 */
170DEF_TEST(Bitmap_eraseColor_Premul, r) {
171 SkColor color = 0x80FF0080;
172 test_erasecolor_premul(r, kAlpha_8_SkColorType, color, 0x80000000);
173 test_erasecolor_premul(r, kRGB_565_SkColorType, color, 0xFF840042);
174 test_erasecolor_premul(r, kARGB_4444_SkColorType, color, 0x88FF0080);
175 test_erasecolor_premul(r, kRGBA_8888_SkColorType, color, color);
176 test_erasecolor_premul(r, kBGRA_8888_SkColorType, color, color);
177}
Hal Canary4cba3fe2016-12-07 14:59:27 -0500178
179// Test that SkBitmap::ComputeOpaque() is correct for various colortypes.
180DEF_TEST(Bitmap_compute_is_opaque, r) {
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400181
182 for (int i = 1; i <= kLastEnum_SkColorType; ++i) {
183 SkColorType ct = (SkColorType) i;
Hal Canary4cba3fe2016-12-07 14:59:27 -0500184 SkBitmap bm;
Brian Osman2091fbb2018-10-11 11:05:37 -0400185 SkAlphaType at = SkColorTypeIsAlwaysOpaque(ct) ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
186 bm.allocPixels(SkImageInfo::Make(13, 17, ct, at));
Hal Canary4cba3fe2016-12-07 14:59:27 -0500187 bm.eraseColor(SkColorSetARGB(255, 10, 20, 30));
188 REPORTER_ASSERT(r, SkBitmap::ComputeIsOpaque(bm));
189
190 bm.eraseColor(SkColorSetARGB(128, 255, 255, 255));
191 bool isOpaque = SkBitmap::ComputeIsOpaque(bm);
Brian Osman2091fbb2018-10-11 11:05:37 -0400192 bool shouldBeOpaque = (at == kOpaque_SkAlphaType);
Hal Canary4cba3fe2016-12-07 14:59:27 -0500193 REPORTER_ASSERT(r, isOpaque == shouldBeOpaque);
194 }
195}
196
197// Test that erase+getColor round trips with RGBA_F16 pixels.
198DEF_TEST(Bitmap_erase_f16_erase_getColor, r) {
199 SkRandom random;
200 SkPixmap pm;
201 SkBitmap bm;
202 bm.allocPixels(SkImageInfo::Make(1, 1, kRGBA_F16_SkColorType, kPremul_SkAlphaType));
203 REPORTER_ASSERT(r, bm.peekPixels(&pm));
204 for (unsigned i = 0; i < 0x100; ++i) {
205 // Test all possible values of blue component.
206 SkColor color1 = (SkColor)((random.nextU() & 0xFFFFFF00) | i);
207 // Test all possible values of alpha component.
208 SkColor color2 = (SkColor)((random.nextU() & 0x00FFFFFF) | (i << 24));
209 for (SkColor color : {color1, color2}) {
210 pm.erase(color);
211 if (SkColorGetA(color) != 0) {
212 REPORTER_ASSERT(r, color == pm.getColor(0, 0));
213 } else {
214 REPORTER_ASSERT(r, 0 == SkColorGetA(pm.getColor(0, 0)));
215 }
216 }
217 }
218}
219
Hal Canarycff9ab72018-01-12 11:09:54 -0500220// Make sure that the bitmap remains valid when pixelref is removed.
221DEF_TEST(Bitmap_clear_pixelref_keep_info, r) {
222 SkBitmap bm;
223 bm.allocPixels(SkImageInfo::MakeN32Premul(100,100));
224 bm.setPixelRef(nullptr, 0, 0);
225 SkDEBUGCODE(bm.validate();)
226}
227
Mike Klein18e9ba12018-03-06 08:43:22 -0500228// At the time of writing, SkBitmap::erase() works when the color is zero for all formats,
229// but some formats failed when the color is non-zero!
230DEF_TEST(Bitmap_erase, r) {
231 SkColorType colorTypes[] = {
232 kRGB_565_SkColorType,
233 kARGB_4444_SkColorType,
234 kRGB_888x_SkColorType,
235 kRGBA_8888_SkColorType,
236 kBGRA_8888_SkColorType,
237 kRGB_101010x_SkColorType,
238 kRGBA_1010102_SkColorType,
239 };
240
241 for (SkColorType ct : colorTypes) {
242 SkImageInfo info = SkImageInfo::Make(1,1, (SkColorType)ct, kPremul_SkAlphaType);
243
244 SkBitmap bm;
245 bm.allocPixels(info);
246
247 bm.eraseColor(0x00000000);
248 if (SkColorTypeIsAlwaysOpaque(ct)) {
249 REPORTER_ASSERT(r, bm.getColor(0,0) == 0xff000000);
250 } else {
251 REPORTER_ASSERT(r, bm.getColor(0,0) == 0x00000000);
252 }
253
254 bm.eraseColor(0xaabbccdd);
255 REPORTER_ASSERT(r, bm.getColor(0,0) != 0xff000000);
256 REPORTER_ASSERT(r, bm.getColor(0,0) != 0x00000000);
257 }
258}
Mike Reedc25f4402018-09-20 15:00:56 -0400259
260static void check_alphas(skiatest::Reporter* reporter, const SkBitmap& bm,
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400261 bool (*pred)(float expected, float actual), SkColorType ct) {
Mike Reedc25f4402018-09-20 15:00:56 -0400262 SkASSERT(bm.width() == 16);
263 SkASSERT(bm.height() == 16);
264
265 int alpha = 0;
266 for (int y = 0; y < 16; ++y) {
267 for (int x = 0; x < 16; ++x) {
268 float expected = alpha / 255.0f;
269 float actual = bm.getAlphaf(x, y);
270 if (!pred(expected, actual)) {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400271 ERRORF(reporter, "%s: got %g, want %g\n",
272 ToolUtils::colortype_name(ct), actual, expected);
Mike Reedc25f4402018-09-20 15:00:56 -0400273 }
274 alpha += 1;
275 }
276 }
277}
278
279static bool unit_compare(float expected, float actual, float tol = 1.0f/(1<<12)) {
280 SkASSERT(expected >= 0 && expected <= 1);
281 SkASSERT( actual >= 0 && actual <= 1);
282 if (expected == 0 || expected == 1) {
283 return actual == expected;
284 } else {
285 return SkScalarNearlyEqual(expected, actual, tol);
286 }
287}
288
289static float unit_discretize(float value, float scale) {
290 SkASSERT(value >= 0 && value <= 1);
291 if (value == 1) {
292 return 1;
293 } else {
294 return sk_float_floor(value * scale + 0.5f) / scale;
295 }
296}
297
298DEF_TEST(getalphaf, reporter) {
299 SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16);
300 SkBitmap bm;
301 bm.allocPixels(info);
302
303 int alpha = 0;
304 for (int y = 0; y < 16; ++y) {
305 for (int x = 0; x < 16; ++x) {
306 *bm.getAddr32(x, y) = alpha++ << 24;
307 }
308 }
309
310 auto nearly = [](float expected, float actual) -> bool {
311 return unit_compare(expected, actual);
312 };
313 auto nearly4bit = [](float expected, float actual) -> bool {
314 expected = unit_discretize(expected, 15);
315 return unit_compare(expected, actual);
316 };
317 auto nearly2bit = [](float expected, float actual) -> bool {
318 expected = unit_discretize(expected, 3);
319 return unit_compare(expected, actual);
320 };
321 auto opaque = [](float expected, float actual) -> bool {
322 return actual == 1.0f;
323 };
324
325 auto nearly_half = [](float expected, float actual) -> bool {
326 return unit_compare(expected, actual, 1.0f/(1<<10));
327 };
328
329 const struct {
330 SkColorType fColorType;
331 bool (*fPred)(float, float);
332 } recs[] = {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400333 { kRGB_565_SkColorType, opaque },
334 { kGray_8_SkColorType, opaque },
335 { kR8G8_unorm_SkColorType, opaque },
336 { kR16G16_unorm_SkColorType, opaque },
337 { kR16G16_float_SkColorType, opaque },
338 { kRGB_888x_SkColorType, opaque },
339 { kRGB_101010x_SkColorType, opaque },
Mike Reedc25f4402018-09-20 15:00:56 -0400340
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400341 { kAlpha_8_SkColorType, nearly },
342 { kA16_unorm_SkColorType, nearly },
343 { kA16_float_SkColorType, nearly_half },
344 { kRGBA_8888_SkColorType, nearly },
345 { kBGRA_8888_SkColorType, nearly },
346 { kR16G16B16A16_unorm_SkColorType, nearly },
347 { kRGBA_F16_SkColorType, nearly_half },
348 { kRGBA_F32_SkColorType, nearly },
Mike Reedc25f4402018-09-20 15:00:56 -0400349
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400350 { kRGBA_1010102_SkColorType, nearly2bit },
Mike Reedc25f4402018-09-20 15:00:56 -0400351
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400352 { kARGB_4444_SkColorType, nearly4bit },
Mike Reedc25f4402018-09-20 15:00:56 -0400353 };
354
355 for (const auto& rec : recs) {
356 SkBitmap tmp;
357 tmp.allocPixels(bm.info().makeColorType(rec.fColorType));
358 if (bm.readPixels(tmp.pixmap())) {
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400359 check_alphas(reporter, tmp, rec.fPred, rec.fColorType);
Mike Reedc25f4402018-09-20 15:00:56 -0400360 } else {
361 SkDebugf("can't readpixels\n");
362 }
363 }
364}