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" |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 10 | #include "Test.h" |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 11 | |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 12 | static void test_peekpixels(skiatest::Reporter* reporter) { |
| 13 | const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
| 14 | |
| 15 | SkPixmap pmap; |
| 16 | SkBitmap bm; |
| 17 | |
| 18 | // empty should return false |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 19 | REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr)); |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 20 | REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap)); |
| 21 | |
| 22 | // no pixels should return false |
| 23 | bm.setInfo(SkImageInfo::MakeN32Premul(10, 10)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 24 | REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr)); |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 25 | REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap)); |
| 26 | |
| 27 | // real pixels should return true |
| 28 | bm.allocPixels(info); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 29 | REPORTER_ASSERT(reporter, bm.peekPixels(nullptr)); |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 30 | REPORTER_ASSERT(reporter, bm.peekPixels(&pmap)); |
| 31 | REPORTER_ASSERT(reporter, pmap.info() == bm.info()); |
| 32 | REPORTER_ASSERT(reporter, pmap.addr() == bm.getPixels()); |
| 33 | REPORTER_ASSERT(reporter, pmap.rowBytes() == bm.rowBytes()); |
| 34 | REPORTER_ASSERT(reporter, pmap.ctable() == bm.getColorTable()); |
| 35 | } |
| 36 | |
reed | 2ff257b | 2015-01-23 07:51:14 -0800 | [diff] [blame] | 37 | // https://code.google.com/p/chromium/issues/detail?id=446164 |
| 38 | static void test_bigalloc(skiatest::Reporter* reporter) { |
| 39 | const int width = 0x40000001; |
| 40 | const int height = 0x00000096; |
| 41 | const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 42 | |
| 43 | SkBitmap bm; |
| 44 | REPORTER_ASSERT(reporter, !bm.tryAllocPixels(info)); |
| 45 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 46 | SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, info.minRowBytes(), nullptr); |
reed | 2ff257b | 2015-01-23 07:51:14 -0800 | [diff] [blame] | 47 | REPORTER_ASSERT(reporter, !pr); |
| 48 | } |
| 49 | |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 50 | static void test_allocpixels(skiatest::Reporter* reporter) { |
| 51 | const int width = 10; |
| 52 | const int height = 10; |
| 53 | const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 54 | const size_t explicitRowBytes = info.minRowBytes() + 24; |
| 55 | |
| 56 | SkBitmap bm; |
| 57 | bm.setInfo(info); |
| 58 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 59 | bm.allocPixels(); |
| 60 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 61 | bm.reset(); |
| 62 | bm.allocPixels(info); |
| 63 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 64 | |
| 65 | bm.setInfo(info, explicitRowBytes); |
| 66 | REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes()); |
| 67 | bm.allocPixels(); |
| 68 | REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes()); |
| 69 | bm.reset(); |
| 70 | bm.allocPixels(info, explicitRowBytes); |
| 71 | REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes()); |
| 72 | |
| 73 | bm.reset(); |
| 74 | bm.setInfo(info, 0); |
| 75 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 76 | bm.reset(); |
| 77 | bm.allocPixels(info, 0); |
| 78 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 79 | |
| 80 | bm.reset(); |
| 81 | bool success = bm.setInfo(info, info.minRowBytes() - 1); // invalid for 32bit |
| 82 | REPORTER_ASSERT(reporter, !success); |
| 83 | REPORTER_ASSERT(reporter, bm.isNull()); |
| 84 | } |
| 85 | |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 86 | static void test_bigwidth(skiatest::Reporter* reporter) { |
| 87 | SkBitmap bm; |
| 88 | int width = 1 << 29; // *4 will be the high-bit of 32bit int |
| 89 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 90 | SkImageInfo info = SkImageInfo::MakeA8(width, 1); |
| 91 | REPORTER_ASSERT(reporter, bm.setInfo(info)); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 92 | REPORTER_ASSERT(reporter, bm.setInfo(info.makeColorType(kRGB_565_SkColorType))); |
skia.committer@gmail.com | f5e1f63 | 2013-12-31 07:01:36 +0000 | [diff] [blame] | 93 | |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 94 | // for a 4-byte config, this width will compute a rowbytes of 0x80000000, |
| 95 | // which does not fit in a int32_t. setConfig should detect this, and fail. |
| 96 | |
| 97 | // TODO: perhaps skia can relax this, and only require that rowBytes fit |
| 98 | // in a uint32_t (or larger), but for now this is the constraint. |
| 99 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 100 | REPORTER_ASSERT(reporter, !bm.setInfo(info.makeColorType(kN32_SkColorType))); |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 101 | } |
| 102 | |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 103 | /** |
| 104 | * This test contains basic sanity checks concerning bitmaps. |
| 105 | */ |
| 106 | DEF_TEST(Bitmap, reporter) { |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 107 | // Zero-sized bitmaps are allowed |
| 108 | for (int width = 0; width < 2; ++width) { |
| 109 | for (int height = 0; height < 2; ++height) { |
| 110 | SkBitmap bm; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 111 | bool setConf = bm.setInfo(SkImageInfo::MakeN32Premul(width, height)); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 112 | REPORTER_ASSERT(reporter, setConf); |
| 113 | if (setConf) { |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 114 | bm.allocPixels(); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 115 | } |
halcanary@google.com | 2af6c12 | 2013-12-13 19:25:21 +0000 | [diff] [blame] | 116 | REPORTER_ASSERT(reporter, SkToBool(width & height) != bm.empty()); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
skia.committer@gmail.com | f5e1f63 | 2013-12-31 07:01:36 +0000 | [diff] [blame] | 119 | |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 120 | test_bigwidth(reporter); |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 121 | test_allocpixels(reporter); |
reed | 2ff257b | 2015-01-23 07:51:14 -0800 | [diff] [blame] | 122 | test_bigalloc(reporter); |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 123 | test_peekpixels(reporter); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 124 | } |
halcanary | b260d13 | 2015-12-09 10:21:59 -0800 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * This test checks that getColor works for both swizzles. |
| 128 | */ |
| 129 | DEF_TEST(Bitmap_getColor_Swizzle, r) { |
| 130 | SkBitmap source; |
| 131 | source.allocN32Pixels(1,1); |
| 132 | source.eraseColor(SK_ColorRED); |
| 133 | SkColorType colorTypes[] = { |
| 134 | kRGBA_8888_SkColorType, |
| 135 | kBGRA_8888_SkColorType, |
| 136 | }; |
| 137 | for (SkColorType ct : colorTypes) { |
| 138 | SkBitmap copy; |
| 139 | if (!source.copyTo(©, ct)) { |
| 140 | ERRORF(r, "SkBitmap::copy failed %d", (int)ct); |
| 141 | continue; |
| 142 | } |
| 143 | SkAutoLockPixels autoLockPixels1(copy); |
| 144 | SkAutoLockPixels autoLockPixels2(source); |
| 145 | REPORTER_ASSERT(r, source.getColor(0, 0) == copy.getColor(0, 0)); |
| 146 | } |
| 147 | } |
benjaminwagner | a1bb8e0 | 2015-12-11 14:08:58 -0800 | [diff] [blame] | 148 | |
| 149 | static void test_erasecolor_premul(skiatest::Reporter* reporter, SkColorType ct, SkColor input, |
| 150 | SkColor expected) { |
| 151 | SkBitmap bm; |
| 152 | bm.allocPixels(SkImageInfo::Make(1, 1, ct, kPremul_SkAlphaType)); |
| 153 | bm.eraseColor(input); |
halcanary | 02faf31 | 2015-12-15 09:20:19 -0800 | [diff] [blame] | 154 | if (reporter->verbose()) { |
| 155 | SkDebugf("expected: %x actual: %x\n", expected, bm.getColor(0, 0)); |
| 156 | } |
benjaminwagner | a1bb8e0 | 2015-12-11 14:08:58 -0800 | [diff] [blame] | 157 | REPORTER_ASSERT(reporter, bm.getColor(0, 0) == expected); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * This test checks that eraseColor premultiplies the color correctly. |
| 162 | */ |
| 163 | DEF_TEST(Bitmap_eraseColor_Premul, r) { |
| 164 | SkColor color = 0x80FF0080; |
| 165 | test_erasecolor_premul(r, kAlpha_8_SkColorType, color, 0x80000000); |
| 166 | test_erasecolor_premul(r, kRGB_565_SkColorType, color, 0xFF840042); |
| 167 | test_erasecolor_premul(r, kARGB_4444_SkColorType, color, 0x88FF0080); |
| 168 | test_erasecolor_premul(r, kRGBA_8888_SkColorType, color, color); |
| 169 | test_erasecolor_premul(r, kBGRA_8888_SkColorType, color, color); |
| 170 | } |