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" |
| 9 | |
| 10 | #include "Test.h" |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 11 | |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 12 | static void test_allocpixels(skiatest::Reporter* reporter) { |
| 13 | const int width = 10; |
| 14 | const int height = 10; |
| 15 | const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 16 | const size_t explicitRowBytes = info.minRowBytes() + 24; |
| 17 | |
| 18 | SkBitmap bm; |
| 19 | bm.setInfo(info); |
| 20 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 21 | bm.allocPixels(); |
| 22 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 23 | bm.reset(); |
| 24 | bm.allocPixels(info); |
| 25 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 26 | |
| 27 | bm.setInfo(info, explicitRowBytes); |
| 28 | REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes()); |
| 29 | bm.allocPixels(); |
| 30 | REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes()); |
| 31 | bm.reset(); |
| 32 | bm.allocPixels(info, explicitRowBytes); |
| 33 | REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes()); |
| 34 | |
| 35 | bm.reset(); |
| 36 | bm.setInfo(info, 0); |
| 37 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 38 | bm.reset(); |
| 39 | bm.allocPixels(info, 0); |
| 40 | REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes()); |
| 41 | |
| 42 | bm.reset(); |
| 43 | bool success = bm.setInfo(info, info.minRowBytes() - 1); // invalid for 32bit |
| 44 | REPORTER_ASSERT(reporter, !success); |
| 45 | REPORTER_ASSERT(reporter, bm.isNull()); |
| 46 | } |
| 47 | |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 48 | static void test_bigwidth(skiatest::Reporter* reporter) { |
| 49 | SkBitmap bm; |
| 50 | int width = 1 << 29; // *4 will be the high-bit of 32bit int |
| 51 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 52 | SkImageInfo info = SkImageInfo::MakeA8(width, 1); |
| 53 | REPORTER_ASSERT(reporter, bm.setInfo(info)); |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 54 | info.fColorType = kRGB_565_SkColorType; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 55 | REPORTER_ASSERT(reporter, bm.setInfo(info)); |
skia.committer@gmail.com | f5e1f63 | 2013-12-31 07:01:36 +0000 | [diff] [blame] | 56 | |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 57 | // for a 4-byte config, this width will compute a rowbytes of 0x80000000, |
| 58 | // which does not fit in a int32_t. setConfig should detect this, and fail. |
| 59 | |
| 60 | // TODO: perhaps skia can relax this, and only require that rowBytes fit |
| 61 | // in a uint32_t (or larger), but for now this is the constraint. |
| 62 | |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 63 | info.fColorType = kN32_SkColorType; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 64 | REPORTER_ASSERT(reporter, !bm.setInfo(info)); |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 65 | } |
| 66 | |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 67 | /** |
| 68 | * This test contains basic sanity checks concerning bitmaps. |
| 69 | */ |
| 70 | DEF_TEST(Bitmap, reporter) { |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 71 | // Zero-sized bitmaps are allowed |
| 72 | for (int width = 0; width < 2; ++width) { |
| 73 | for (int height = 0; height < 2; ++height) { |
| 74 | SkBitmap bm; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 75 | bool setConf = bm.setInfo(SkImageInfo::MakeN32Premul(width, height)); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 76 | REPORTER_ASSERT(reporter, setConf); |
| 77 | if (setConf) { |
| 78 | REPORTER_ASSERT(reporter, bm.allocPixels(NULL)); |
| 79 | } |
halcanary@google.com | 2af6c12 | 2013-12-13 19:25:21 +0000 | [diff] [blame] | 80 | REPORTER_ASSERT(reporter, SkToBool(width & height) != bm.empty()); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
skia.committer@gmail.com | f5e1f63 | 2013-12-31 07:01:36 +0000 | [diff] [blame] | 83 | |
reed@google.com | 4856964 | 2013-12-30 19:21:22 +0000 | [diff] [blame] | 84 | test_bigwidth(reporter); |
reed | f0aed97 | 2014-07-01 12:48:11 -0700 | [diff] [blame] | 85 | test_allocpixels(reporter); |
halcanary@google.com | 4428734 | 2013-12-13 18:29:51 +0000 | [diff] [blame] | 86 | } |