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