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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 15 | #include "include/core/SkScalar.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkTypes.h" |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 17 | #include "include/private/SkFloatingPoint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/utils/SkRandom.h" |
| 19 | #include "tests/Test.h" |
| 20 | #include "tools/ToolUtils.h" |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 21 | |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 22 | #include <initializer_list> |
| 23 | |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 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 |
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 | |
| 34 | // no pixels should return false |
| 35 | bm.setInfo(SkImageInfo::MakeN32Premul(10, 10)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 36 | REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr)); |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 37 | REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap)); |
| 38 | |
| 39 | // real pixels should return true |
| 40 | bm.allocPixels(info); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 41 | REPORTER_ASSERT(reporter, bm.peekPixels(nullptr)); |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 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()); |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 46 | } |
| 47 | |
reed | 2ff257b | 2015-01-23 07:51:14 -0800 | [diff] [blame] | 48 | // https://code.google.com/p/chromium/issues/detail?id=446164 |
| 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 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 57 | sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, info.minRowBytes()); |
reed | 2ff257b | 2015-01-23 07:51:14 -0800 | [diff] [blame] | 58 | REPORTER_ASSERT(reporter, !pr); |
| 59 | } |
| 60 | |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 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()); |
Leon Scroggins III | 3eedc97 | 2020-01-22 10:44:00 -0500 | [diff] [blame] | 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 | } |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 131 | } |
| 132 | |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 137 | SkImageInfo info = SkImageInfo::MakeA8(width, 1); |
| 138 | REPORTER_ASSERT(reporter, bm.setInfo(info)); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 139 | REPORTER_ASSERT(reporter, bm.setInfo(info.makeColorType(kRGB_565_SkColorType))); |
skia.committer@gmail.com | f5e1f63 | 2013-12-31 07:01:36 +0000 | [diff] [blame] | 140 | |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 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 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 147 | REPORTER_ASSERT(reporter, !bm.setInfo(info.makeColorType(kN32_SkColorType))); |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 148 | } |
| 149 | |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 150 | DEF_TEST(Bitmap, reporter) { |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 151 | // Zero-sized bitmaps are allowed |
| 152 | for (int width = 0; width < 2; ++width) { |
| 153 | for (int height = 0; height < 2; ++height) { |
| 154 | SkBitmap bm; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 155 | bool setConf = bm.setInfo(SkImageInfo::MakeN32Premul(width, height)); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 156 | REPORTER_ASSERT(reporter, setConf); |
| 157 | if (setConf) { |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 158 | bm.allocPixels(); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 159 | } |
halcanary@google.com | 2af6c12 | 2013-12-13 19:25:21 +0000 | [diff] [blame] | 160 | REPORTER_ASSERT(reporter, SkToBool(width & height) != bm.empty()); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
skia.committer@gmail.com | f5e1f63 | 2013-12-31 07:01:36 +0000 | [diff] [blame] | 163 | |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 164 | test_bigwidth(reporter); |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 165 | test_allocpixels(reporter); |
reed | 2ff257b | 2015-01-23 07:51:14 -0800 | [diff] [blame] | 166 | test_bigalloc(reporter); |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 167 | test_peekpixels(reporter); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 168 | } |
halcanary | b260d13 | 2015-12-09 10:21:59 -0800 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * This test checks that getColor works for both swizzles. |
| 172 | */ |
| 173 | DEF_TEST(Bitmap_getColor_Swizzle, r) { |
| 174 | SkBitmap source; |
| 175 | source.allocN32Pixels(1,1); |
| 176 | source.eraseColor(SK_ColorRED); |
| 177 | SkColorType colorTypes[] = { |
| 178 | kRGBA_8888_SkColorType, |
| 179 | kBGRA_8888_SkColorType, |
| 180 | }; |
| 181 | for (SkColorType ct : colorTypes) { |
| 182 | SkBitmap copy; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 183 | if (!ToolUtils::copy_to(©, ct, source)) { |
halcanary | b260d13 | 2015-12-09 10:21:59 -0800 | [diff] [blame] | 184 | ERRORF(r, "SkBitmap::copy failed %d", (int)ct); |
| 185 | continue; |
| 186 | } |
halcanary | b260d13 | 2015-12-09 10:21:59 -0800 | [diff] [blame] | 187 | REPORTER_ASSERT(r, source.getColor(0, 0) == copy.getColor(0, 0)); |
| 188 | } |
| 189 | } |
benjaminwagner | a1bb8e0 | 2015-12-11 14:08:58 -0800 | [diff] [blame] | 190 | |
| 191 | static void test_erasecolor_premul(skiatest::Reporter* reporter, SkColorType ct, SkColor input, |
| 192 | SkColor expected) { |
| 193 | SkBitmap bm; |
| 194 | bm.allocPixels(SkImageInfo::Make(1, 1, ct, kPremul_SkAlphaType)); |
| 195 | bm.eraseColor(input); |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 196 | INFOF(reporter, "expected: %x actual: %x\n", expected, bm.getColor(0, 0)); |
benjaminwagner | a1bb8e0 | 2015-12-11 14:08:58 -0800 | [diff] [blame] | 197 | REPORTER_ASSERT(reporter, bm.getColor(0, 0) == expected); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * This test checks that eraseColor premultiplies the color correctly. |
| 202 | */ |
| 203 | DEF_TEST(Bitmap_eraseColor_Premul, r) { |
| 204 | SkColor color = 0x80FF0080; |
| 205 | test_erasecolor_premul(r, kAlpha_8_SkColorType, color, 0x80000000); |
| 206 | test_erasecolor_premul(r, kRGB_565_SkColorType, color, 0xFF840042); |
| 207 | test_erasecolor_premul(r, kARGB_4444_SkColorType, color, 0x88FF0080); |
| 208 | test_erasecolor_premul(r, kRGBA_8888_SkColorType, color, color); |
| 209 | test_erasecolor_premul(r, kBGRA_8888_SkColorType, color, color); |
| 210 | } |
Hal Canary | 4cba3fe | 2016-12-07 14:59:27 -0500 | [diff] [blame] | 211 | |
| 212 | // Test that SkBitmap::ComputeOpaque() is correct for various colortypes. |
| 213 | DEF_TEST(Bitmap_compute_is_opaque, r) { |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 214 | |
| 215 | for (int i = 1; i <= kLastEnum_SkColorType; ++i) { |
| 216 | SkColorType ct = (SkColorType) i; |
Hal Canary | 4cba3fe | 2016-12-07 14:59:27 -0500 | [diff] [blame] | 217 | SkBitmap bm; |
Brian Osman | 2091fbb | 2018-10-11 11:05:37 -0400 | [diff] [blame] | 218 | SkAlphaType at = SkColorTypeIsAlwaysOpaque(ct) ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 219 | bm.allocPixels(SkImageInfo::Make(13, 17, ct, at)); |
Hal Canary | 4cba3fe | 2016-12-07 14:59:27 -0500 | [diff] [blame] | 220 | bm.eraseColor(SkColorSetARGB(255, 10, 20, 30)); |
| 221 | REPORTER_ASSERT(r, SkBitmap::ComputeIsOpaque(bm)); |
| 222 | |
| 223 | bm.eraseColor(SkColorSetARGB(128, 255, 255, 255)); |
| 224 | bool isOpaque = SkBitmap::ComputeIsOpaque(bm); |
Brian Osman | 2091fbb | 2018-10-11 11:05:37 -0400 | [diff] [blame] | 225 | bool shouldBeOpaque = (at == kOpaque_SkAlphaType); |
Hal Canary | 4cba3fe | 2016-12-07 14:59:27 -0500 | [diff] [blame] | 226 | REPORTER_ASSERT(r, isOpaque == shouldBeOpaque); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Test that erase+getColor round trips with RGBA_F16 pixels. |
| 231 | DEF_TEST(Bitmap_erase_f16_erase_getColor, r) { |
| 232 | SkRandom random; |
| 233 | SkPixmap pm; |
| 234 | SkBitmap bm; |
| 235 | bm.allocPixels(SkImageInfo::Make(1, 1, kRGBA_F16_SkColorType, kPremul_SkAlphaType)); |
| 236 | REPORTER_ASSERT(r, bm.peekPixels(&pm)); |
| 237 | for (unsigned i = 0; i < 0x100; ++i) { |
| 238 | // Test all possible values of blue component. |
| 239 | SkColor color1 = (SkColor)((random.nextU() & 0xFFFFFF00) | i); |
| 240 | // Test all possible values of alpha component. |
| 241 | SkColor color2 = (SkColor)((random.nextU() & 0x00FFFFFF) | (i << 24)); |
| 242 | for (SkColor color : {color1, color2}) { |
| 243 | pm.erase(color); |
| 244 | if (SkColorGetA(color) != 0) { |
| 245 | REPORTER_ASSERT(r, color == pm.getColor(0, 0)); |
| 246 | } else { |
| 247 | REPORTER_ASSERT(r, 0 == SkColorGetA(pm.getColor(0, 0))); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
Leon Scroggins | 565f17f | 2019-01-08 14:06:17 +0000 | [diff] [blame] | 253 | // Verify that SkBitmap::erase erases in SRGB, regardless of the SkColorSpace of the |
| 254 | // SkBitmap. |
| 255 | DEF_TEST(Bitmap_erase_srgb, r) { |
| 256 | SkBitmap bm; |
| 257 | // Use a color spin from SRGB. |
| 258 | bm.allocPixels(SkImageInfo::Make(1, 1, kN32_SkColorType, kPremul_SkAlphaType, |
| 259 | SkColorSpace::MakeSRGB()->makeColorSpin())); |
| 260 | // RED will be converted into the spun color space. |
| 261 | bm.eraseColor(SK_ColorRED); |
| 262 | // getColor doesn't take the color space into account, so the returned color |
| 263 | // is different due to the color spin. |
| 264 | REPORTER_ASSERT(r, bm.getColor(0, 0) == SK_ColorBLUE); |
| 265 | } |
| 266 | |
Hal Canary | cff9ab7 | 2018-01-12 11:09:54 -0500 | [diff] [blame] | 267 | // Make sure that the bitmap remains valid when pixelref is removed. |
| 268 | DEF_TEST(Bitmap_clear_pixelref_keep_info, r) { |
| 269 | SkBitmap bm; |
| 270 | bm.allocPixels(SkImageInfo::MakeN32Premul(100,100)); |
| 271 | bm.setPixelRef(nullptr, 0, 0); |
| 272 | SkDEBUGCODE(bm.validate();) |
| 273 | } |
| 274 | |
Mike Klein | 18e9ba1 | 2018-03-06 08:43:22 -0500 | [diff] [blame] | 275 | // At the time of writing, SkBitmap::erase() works when the color is zero for all formats, |
| 276 | // but some formats failed when the color is non-zero! |
| 277 | DEF_TEST(Bitmap_erase, r) { |
| 278 | SkColorType colorTypes[] = { |
| 279 | kRGB_565_SkColorType, |
| 280 | kARGB_4444_SkColorType, |
| 281 | kRGB_888x_SkColorType, |
| 282 | kRGBA_8888_SkColorType, |
| 283 | kBGRA_8888_SkColorType, |
| 284 | kRGB_101010x_SkColorType, |
| 285 | kRGBA_1010102_SkColorType, |
| 286 | }; |
| 287 | |
| 288 | for (SkColorType ct : colorTypes) { |
| 289 | SkImageInfo info = SkImageInfo::Make(1,1, (SkColorType)ct, kPremul_SkAlphaType); |
| 290 | |
| 291 | SkBitmap bm; |
| 292 | bm.allocPixels(info); |
| 293 | |
| 294 | bm.eraseColor(0x00000000); |
| 295 | if (SkColorTypeIsAlwaysOpaque(ct)) { |
| 296 | REPORTER_ASSERT(r, bm.getColor(0,0) == 0xff000000); |
| 297 | } else { |
| 298 | REPORTER_ASSERT(r, bm.getColor(0,0) == 0x00000000); |
| 299 | } |
| 300 | |
| 301 | bm.eraseColor(0xaabbccdd); |
| 302 | REPORTER_ASSERT(r, bm.getColor(0,0) != 0xff000000); |
| 303 | REPORTER_ASSERT(r, bm.getColor(0,0) != 0x00000000); |
| 304 | } |
| 305 | } |
Mike Reed | c25f440 | 2018-09-20 15:00:56 -0400 | [diff] [blame] | 306 | |
| 307 | static void check_alphas(skiatest::Reporter* reporter, const SkBitmap& bm, |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 308 | bool (*pred)(float expected, float actual), SkColorType ct) { |
Mike Reed | c25f440 | 2018-09-20 15:00:56 -0400 | [diff] [blame] | 309 | SkASSERT(bm.width() == 16); |
| 310 | SkASSERT(bm.height() == 16); |
| 311 | |
| 312 | int alpha = 0; |
| 313 | for (int y = 0; y < 16; ++y) { |
| 314 | for (int x = 0; x < 16; ++x) { |
| 315 | float expected = alpha / 255.0f; |
| 316 | float actual = bm.getAlphaf(x, y); |
| 317 | if (!pred(expected, actual)) { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 318 | ERRORF(reporter, "%s: got %g, want %g\n", |
| 319 | ToolUtils::colortype_name(ct), actual, expected); |
Mike Reed | c25f440 | 2018-09-20 15:00:56 -0400 | [diff] [blame] | 320 | } |
| 321 | alpha += 1; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | static bool unit_compare(float expected, float actual, float tol = 1.0f/(1<<12)) { |
| 327 | SkASSERT(expected >= 0 && expected <= 1); |
| 328 | SkASSERT( actual >= 0 && actual <= 1); |
| 329 | if (expected == 0 || expected == 1) { |
| 330 | return actual == expected; |
| 331 | } else { |
| 332 | return SkScalarNearlyEqual(expected, actual, tol); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | static float unit_discretize(float value, float scale) { |
| 337 | SkASSERT(value >= 0 && value <= 1); |
| 338 | if (value == 1) { |
| 339 | return 1; |
| 340 | } else { |
| 341 | return sk_float_floor(value * scale + 0.5f) / scale; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | DEF_TEST(getalphaf, reporter) { |
| 346 | SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16); |
| 347 | SkBitmap bm; |
| 348 | bm.allocPixels(info); |
| 349 | |
| 350 | int alpha = 0; |
| 351 | for (int y = 0; y < 16; ++y) { |
| 352 | for (int x = 0; x < 16; ++x) { |
| 353 | *bm.getAddr32(x, y) = alpha++ << 24; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | auto nearly = [](float expected, float actual) -> bool { |
| 358 | return unit_compare(expected, actual); |
| 359 | }; |
| 360 | auto nearly4bit = [](float expected, float actual) -> bool { |
| 361 | expected = unit_discretize(expected, 15); |
| 362 | return unit_compare(expected, actual); |
| 363 | }; |
| 364 | auto nearly2bit = [](float expected, float actual) -> bool { |
| 365 | expected = unit_discretize(expected, 3); |
| 366 | return unit_compare(expected, actual); |
| 367 | }; |
| 368 | auto opaque = [](float expected, float actual) -> bool { |
| 369 | return actual == 1.0f; |
| 370 | }; |
| 371 | |
| 372 | auto nearly_half = [](float expected, float actual) -> bool { |
| 373 | return unit_compare(expected, actual, 1.0f/(1<<10)); |
| 374 | }; |
| 375 | |
| 376 | const struct { |
| 377 | SkColorType fColorType; |
| 378 | bool (*fPred)(float, float); |
| 379 | } recs[] = { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 380 | { kRGB_565_SkColorType, opaque }, |
| 381 | { kGray_8_SkColorType, opaque }, |
| 382 | { kR8G8_unorm_SkColorType, opaque }, |
| 383 | { kR16G16_unorm_SkColorType, opaque }, |
| 384 | { kR16G16_float_SkColorType, opaque }, |
| 385 | { kRGB_888x_SkColorType, opaque }, |
| 386 | { kRGB_101010x_SkColorType, opaque }, |
Mike Reed | c25f440 | 2018-09-20 15:00:56 -0400 | [diff] [blame] | 387 | |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 388 | { kAlpha_8_SkColorType, nearly }, |
| 389 | { kA16_unorm_SkColorType, nearly }, |
| 390 | { kA16_float_SkColorType, nearly_half }, |
| 391 | { kRGBA_8888_SkColorType, nearly }, |
| 392 | { kBGRA_8888_SkColorType, nearly }, |
| 393 | { kR16G16B16A16_unorm_SkColorType, nearly }, |
| 394 | { kRGBA_F16_SkColorType, nearly_half }, |
| 395 | { kRGBA_F32_SkColorType, nearly }, |
Mike Reed | c25f440 | 2018-09-20 15:00:56 -0400 | [diff] [blame] | 396 | |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 397 | { kRGBA_1010102_SkColorType, nearly2bit }, |
Mike Reed | c25f440 | 2018-09-20 15:00:56 -0400 | [diff] [blame] | 398 | |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 399 | { kARGB_4444_SkColorType, nearly4bit }, |
Mike Reed | c25f440 | 2018-09-20 15:00:56 -0400 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | for (const auto& rec : recs) { |
| 403 | SkBitmap tmp; |
| 404 | tmp.allocPixels(bm.info().makeColorType(rec.fColorType)); |
| 405 | if (bm.readPixels(tmp.pixmap())) { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 406 | check_alphas(reporter, tmp, rec.fPred, rec.fColorType); |
Mike Reed | c25f440 | 2018-09-20 15:00:56 -0400 | [diff] [blame] | 407 | } else { |
| 408 | SkDebugf("can't readpixels\n"); |
| 409 | } |
| 410 | } |
| 411 | } |
Mike Reed | c1eb58d | 2020-07-20 18:12:00 -0400 | [diff] [blame] | 412 | |
| 413 | /* computeByteSize() is documented to return 0 if height is zero, but does not |
| 414 | * special-case width==0, so computeByteSize() can return non-zero for that |
| 415 | * (since it is defined to return (height-1)*rb + ... |
| 416 | * |
| 417 | * Test that allocPixels() respects this, and allocates a buffer as large as |
| 418 | * computeByteSize()... even though the bitmap is logicallly empty. |
| 419 | */ |
| 420 | DEF_TEST(bitmap_zerowidth_crbug_1103827, reporter) { |
| 421 | const size_t big_rb = 1 << 16; |
| 422 | |
| 423 | struct { |
| 424 | int width, height; |
| 425 | size_t rowbytes, expected_size; |
| 426 | } rec[] = { |
| 427 | { 2, 0, big_rb, 0 }, // zero-height means zero-size |
| 428 | { 0, 2, big_rb, big_rb }, // zero-width is computed normally |
| 429 | }; |
| 430 | |
| 431 | for (const auto& r : rec) { |
| 432 | auto info = SkImageInfo::Make(r.width, r.height, |
| 433 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 434 | size_t size = info.computeByteSize(r.rowbytes); |
| 435 | REPORTER_ASSERT(reporter, size == r.expected_size); |
| 436 | |
| 437 | SkBitmap bm; |
| 438 | bm.setInfo(info, r.rowbytes); |
| 439 | REPORTER_ASSERT(reporter, size == bm.computeByteSize()); |
| 440 | |
| 441 | // Be sure we can actually write to that much memory. If the bitmap underallocated |
| 442 | // the buffer, this should trash memory and crash (we hope). |
| 443 | bm.allocPixels(); |
| 444 | sk_bzero(bm.getPixels(), size); |
| 445 | } |
| 446 | } |