halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 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 "SkBitmap.h" |
reed | 2ff257b | 2015-01-23 07:51:14 -0800 | [diff] [blame] | 9 | #include "SkMallocPixelRef.h" |
Hal Canary | 02e65df | 2016-12-07 16:21:53 -0500 | [diff] [blame] | 10 | #include "SkRandom.h" |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 11 | #include "Test.h" |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 12 | #include "sk_tool_utils.h" |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 13 | |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 14 | static void test_peekpixels(skiatest::Reporter* reporter) { |
| 15 | const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
| 16 | |
| 17 | SkPixmap pmap; |
| 18 | SkBitmap bm; |
| 19 | |
| 20 | // empty should return false |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 21 | REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr)); |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 22 | REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap)); |
| 23 | |
| 24 | // no pixels should return false |
| 25 | bm.setInfo(SkImageInfo::MakeN32Premul(10, 10)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 26 | REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr)); |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 27 | REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap)); |
| 28 | |
| 29 | // real pixels should return true |
| 30 | bm.allocPixels(info); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 31 | REPORTER_ASSERT(reporter, bm.peekPixels(nullptr)); |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 32 | REPORTER_ASSERT(reporter, bm.peekPixels(&pmap)); |
| 33 | REPORTER_ASSERT(reporter, pmap.info() == bm.info()); |
| 34 | REPORTER_ASSERT(reporter, pmap.addr() == bm.getPixels()); |
| 35 | REPORTER_ASSERT(reporter, pmap.rowBytes() == bm.rowBytes()); |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 36 | } |
| 37 | |
reed | 2ff257b | 2015-01-23 07:51:14 -0800 | [diff] [blame] | 38 | // https://code.google.com/p/chromium/issues/detail?id=446164 |
| 39 | static void test_bigalloc(skiatest::Reporter* reporter) { |
| 40 | const int width = 0x40000001; |
| 41 | const int height = 0x00000096; |
| 42 | const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 43 | |
| 44 | SkBitmap bm; |
| 45 | REPORTER_ASSERT(reporter, !bm.tryAllocPixels(info)); |
| 46 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 47 | sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, info.minRowBytes()); |
reed | 2ff257b | 2015-01-23 07:51:14 -0800 | [diff] [blame] | 48 | REPORTER_ASSERT(reporter, !pr); |
| 49 | } |
| 50 | |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 51 | static void test_allocpixels(skiatest::Reporter* reporter) { |
| 52 | const int width = 10; |
| 53 | const int height = 10; |
| 54 | const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 55 | const size_t explicitRowBytes = info.minRowBytes() + 24; |
| 56 | |
| 57 | SkBitmap bm; |
| 58 | bm.setInfo(info); |
| 59 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 60 | bm.allocPixels(); |
| 61 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 62 | bm.reset(); |
| 63 | bm.allocPixels(info); |
| 64 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 65 | |
| 66 | bm.setInfo(info, explicitRowBytes); |
| 67 | REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes()); |
| 68 | bm.allocPixels(); |
| 69 | REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes()); |
| 70 | bm.reset(); |
| 71 | bm.allocPixels(info, explicitRowBytes); |
| 72 | REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes()); |
| 73 | |
| 74 | bm.reset(); |
| 75 | bm.setInfo(info, 0); |
| 76 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 77 | bm.reset(); |
| 78 | bm.allocPixels(info, 0); |
| 79 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 80 | |
| 81 | bm.reset(); |
| 82 | bool success = bm.setInfo(info, info.minRowBytes() - 1); // invalid for 32bit |
| 83 | REPORTER_ASSERT(reporter, !success); |
| 84 | REPORTER_ASSERT(reporter, bm.isNull()); |
| 85 | } |
| 86 | |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 87 | static void test_bigwidth(skiatest::Reporter* reporter) { |
| 88 | SkBitmap bm; |
| 89 | int width = 1 << 29; // *4 will be the high-bit of 32bit int |
| 90 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 91 | SkImageInfo info = SkImageInfo::MakeA8(width, 1); |
| 92 | REPORTER_ASSERT(reporter, bm.setInfo(info)); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 93 | REPORTER_ASSERT(reporter, bm.setInfo(info.makeColorType(kRGB_565_SkColorType))); |
skia.committer@gmail.com | f5e1f63 | 2013-12-31 07:01:36 +0000 | [diff] [blame] | 94 | |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 95 | // for a 4-byte config, this width will compute a rowbytes of 0x80000000, |
| 96 | // which does not fit in a int32_t. setConfig should detect this, and fail. |
| 97 | |
| 98 | // TODO: perhaps skia can relax this, and only require that rowBytes fit |
| 99 | // in a uint32_t (or larger), but for now this is the constraint. |
| 100 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 101 | REPORTER_ASSERT(reporter, !bm.setInfo(info.makeColorType(kN32_SkColorType))); |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 102 | } |
| 103 | |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 104 | /** |
| 105 | * This test contains basic sanity checks concerning bitmaps. |
| 106 | */ |
| 107 | DEF_TEST(Bitmap, reporter) { |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 108 | // Zero-sized bitmaps are allowed |
| 109 | for (int width = 0; width < 2; ++width) { |
| 110 | for (int height = 0; height < 2; ++height) { |
| 111 | SkBitmap bm; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 112 | bool setConf = bm.setInfo(SkImageInfo::MakeN32Premul(width, height)); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 113 | REPORTER_ASSERT(reporter, setConf); |
| 114 | if (setConf) { |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 115 | bm.allocPixels(); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 116 | } |
halcanary@google.com | 2af6c12 | 2013-12-13 19:25:21 +0000 | [diff] [blame] | 117 | REPORTER_ASSERT(reporter, SkToBool(width & height) != bm.empty()); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
skia.committer@gmail.com | f5e1f63 | 2013-12-31 07:01:36 +0000 | [diff] [blame] | 120 | |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 121 | test_bigwidth(reporter); |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 122 | test_allocpixels(reporter); |
reed | 2ff257b | 2015-01-23 07:51:14 -0800 | [diff] [blame] | 123 | test_bigalloc(reporter); |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 124 | test_peekpixels(reporter); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 125 | } |
halcanary | b260d13 | 2015-12-09 10:21:59 -0800 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * This test checks that getColor works for both swizzles. |
| 129 | */ |
| 130 | DEF_TEST(Bitmap_getColor_Swizzle, r) { |
| 131 | SkBitmap source; |
| 132 | source.allocN32Pixels(1,1); |
| 133 | source.eraseColor(SK_ColorRED); |
| 134 | SkColorType colorTypes[] = { |
| 135 | kRGBA_8888_SkColorType, |
| 136 | kBGRA_8888_SkColorType, |
| 137 | }; |
| 138 | for (SkColorType ct : colorTypes) { |
| 139 | SkBitmap copy; |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 140 | if (!sk_tool_utils::copy_to(©, ct, source)) { |
halcanary | b260d13 | 2015-12-09 10:21:59 -0800 | [diff] [blame] | 141 | ERRORF(r, "SkBitmap::copy failed %d", (int)ct); |
| 142 | continue; |
| 143 | } |
halcanary | b260d13 | 2015-12-09 10:21:59 -0800 | [diff] [blame] | 144 | REPORTER_ASSERT(r, source.getColor(0, 0) == copy.getColor(0, 0)); |
| 145 | } |
| 146 | } |
benjaminwagner | a1bb8e0 | 2015-12-11 14:08:58 -0800 | [diff] [blame] | 147 | |
| 148 | static void test_erasecolor_premul(skiatest::Reporter* reporter, SkColorType ct, SkColor input, |
| 149 | SkColor expected) { |
| 150 | SkBitmap bm; |
| 151 | bm.allocPixels(SkImageInfo::Make(1, 1, ct, kPremul_SkAlphaType)); |
| 152 | bm.eraseColor(input); |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 153 | INFOF(reporter, "expected: %x actual: %x\n", expected, bm.getColor(0, 0)); |
benjaminwagner | a1bb8e0 | 2015-12-11 14:08:58 -0800 | [diff] [blame] | 154 | REPORTER_ASSERT(reporter, bm.getColor(0, 0) == expected); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * This test checks that eraseColor premultiplies the color correctly. |
| 159 | */ |
| 160 | DEF_TEST(Bitmap_eraseColor_Premul, r) { |
| 161 | SkColor color = 0x80FF0080; |
| 162 | test_erasecolor_premul(r, kAlpha_8_SkColorType, color, 0x80000000); |
| 163 | test_erasecolor_premul(r, kRGB_565_SkColorType, color, 0xFF840042); |
| 164 | test_erasecolor_premul(r, kARGB_4444_SkColorType, color, 0x88FF0080); |
| 165 | test_erasecolor_premul(r, kRGBA_8888_SkColorType, color, color); |
| 166 | test_erasecolor_premul(r, kBGRA_8888_SkColorType, color, color); |
| 167 | } |
Hal Canary | 4cba3fe | 2016-12-07 14:59:27 -0500 | [diff] [blame] | 168 | |
| 169 | // Test that SkBitmap::ComputeOpaque() is correct for various colortypes. |
| 170 | DEF_TEST(Bitmap_compute_is_opaque, r) { |
| 171 | struct { |
| 172 | SkColorType fCT; |
| 173 | SkAlphaType fAT; |
| 174 | } types[] = { |
| 175 | { kGray_8_SkColorType, kOpaque_SkAlphaType }, |
| 176 | { kAlpha_8_SkColorType, kPremul_SkAlphaType }, |
| 177 | { kARGB_4444_SkColorType, kPremul_SkAlphaType }, |
| 178 | { kRGB_565_SkColorType, kOpaque_SkAlphaType }, |
| 179 | { kBGRA_8888_SkColorType, kPremul_SkAlphaType }, |
| 180 | { kRGBA_8888_SkColorType, kPremul_SkAlphaType }, |
| 181 | { kRGBA_F16_SkColorType, kPremul_SkAlphaType }, |
| 182 | }; |
| 183 | for (auto type : types) { |
| 184 | SkBitmap bm; |
| 185 | REPORTER_ASSERT(r, !SkBitmap::ComputeIsOpaque(bm)); |
| 186 | |
| 187 | bm.allocPixels(SkImageInfo::Make(13, 17, type.fCT, type.fAT)); |
| 188 | bm.eraseColor(SkColorSetARGB(255, 10, 20, 30)); |
| 189 | REPORTER_ASSERT(r, SkBitmap::ComputeIsOpaque(bm)); |
| 190 | |
| 191 | bm.eraseColor(SkColorSetARGB(128, 255, 255, 255)); |
| 192 | bool isOpaque = SkBitmap::ComputeIsOpaque(bm); |
| 193 | bool shouldBeOpaque = (type.fAT == kOpaque_SkAlphaType); |
| 194 | REPORTER_ASSERT(r, isOpaque == shouldBeOpaque); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Test that erase+getColor round trips with RGBA_F16 pixels. |
| 199 | DEF_TEST(Bitmap_erase_f16_erase_getColor, r) { |
| 200 | SkRandom random; |
| 201 | SkPixmap pm; |
| 202 | SkBitmap bm; |
| 203 | bm.allocPixels(SkImageInfo::Make(1, 1, kRGBA_F16_SkColorType, kPremul_SkAlphaType)); |
| 204 | REPORTER_ASSERT(r, bm.peekPixels(&pm)); |
| 205 | for (unsigned i = 0; i < 0x100; ++i) { |
| 206 | // Test all possible values of blue component. |
| 207 | SkColor color1 = (SkColor)((random.nextU() & 0xFFFFFF00) | i); |
| 208 | // Test all possible values of alpha component. |
| 209 | SkColor color2 = (SkColor)((random.nextU() & 0x00FFFFFF) | (i << 24)); |
| 210 | for (SkColor color : {color1, color2}) { |
| 211 | pm.erase(color); |
| 212 | if (SkColorGetA(color) != 0) { |
| 213 | REPORTER_ASSERT(r, color == pm.getColor(0, 0)); |
| 214 | } else { |
| 215 | REPORTER_ASSERT(r, 0 == SkColorGetA(pm.getColor(0, 0))); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |