halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +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 | |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame^] | 8 | #include "SkAutoMalloc.h" |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 9 | #include "SkData.h" |
| 10 | #include "SkMallocPixelRef.h" |
| 11 | #include "Test.h" |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 12 | |
| 13 | static void delete_uint8_proc(void* ptr, void*) { |
| 14 | delete[] static_cast<uint8_t*>(ptr); |
| 15 | } |
| 16 | |
| 17 | static void set_to_one_proc(void*, void* context) { |
| 18 | *(static_cast<int*>(context)) = 1; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * This test contains basic sanity checks concerning SkMallocPixelRef. |
| 23 | */ |
| 24 | DEF_TEST(MallocPixelRef, reporter) { |
| 25 | REPORTER_ASSERT(reporter, true); |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 26 | SkImageInfo info = SkImageInfo::MakeN32Premul(10, 13); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 27 | { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 28 | sk_sp<SkMallocPixelRef> pr( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 29 | SkMallocPixelRef::NewAllocate(info, info.minRowBytes() - 1, nullptr)); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 30 | // rowbytes too small. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 31 | REPORTER_ASSERT(reporter, nullptr == pr.get()); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 32 | } |
| 33 | { |
| 34 | size_t rowBytes = info.minRowBytes() - 1; |
| 35 | size_t size = info.getSafeSize(rowBytes); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 36 | sk_sp<SkData> data(SkData::MakeUninitialized(size)); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 37 | sk_sp<SkMallocPixelRef> pr( |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 38 | SkMallocPixelRef::NewWithData(info, rowBytes, nullptr, data.get())); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 39 | // rowbytes too small. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 40 | REPORTER_ASSERT(reporter, nullptr == pr.get()); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 41 | } |
| 42 | { |
| 43 | size_t rowBytes = info.minRowBytes() + 2; |
| 44 | size_t size = info.getSafeSize(rowBytes) - 1; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 45 | sk_sp<SkData> data(SkData::MakeUninitialized(size)); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 46 | sk_sp<SkMallocPixelRef> pr( |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 47 | SkMallocPixelRef::NewWithData(info, rowBytes, nullptr, data.get())); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 48 | // data too small. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 49 | REPORTER_ASSERT(reporter, nullptr == pr.get()); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 50 | } |
| 51 | size_t rowBytes = info.minRowBytes() + 7; |
| 52 | size_t size = info.getSafeSize(rowBytes) + 9; |
| 53 | { |
| 54 | SkAutoMalloc memory(size); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 55 | sk_sp<SkMallocPixelRef> pr( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | SkMallocPixelRef::NewDirect(info, memory.get(), rowBytes, nullptr)); |
| 57 | REPORTER_ASSERT(reporter, pr.get() != nullptr); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 58 | REPORTER_ASSERT(reporter, memory.get() == pr->pixels()); |
| 59 | } |
| 60 | { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 61 | sk_sp<SkMallocPixelRef> pr( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 62 | SkMallocPixelRef::NewAllocate(info, rowBytes, nullptr)); |
| 63 | REPORTER_ASSERT(reporter, pr.get() != nullptr); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 64 | REPORTER_ASSERT(reporter, pr->pixels()); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 65 | } |
| 66 | { |
| 67 | void* addr = static_cast<void*>(new uint8_t[size]); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 68 | sk_sp<SkMallocPixelRef> pr( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | SkMallocPixelRef::NewWithProc(info, rowBytes, nullptr, addr, |
| 70 | delete_uint8_proc, nullptr)); |
| 71 | REPORTER_ASSERT(reporter, pr.get() != nullptr); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 72 | REPORTER_ASSERT(reporter, addr == pr->pixels()); |
| 73 | } |
| 74 | { |
| 75 | int x = 0; |
| 76 | SkAutoMalloc memory(size); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 77 | sk_sp<SkMallocPixelRef> pr( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | SkMallocPixelRef::NewWithProc(info, rowBytes, nullptr, |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 79 | memory.get(), set_to_one_proc, |
| 80 | static_cast<void*>(&x))); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 81 | REPORTER_ASSERT(reporter, pr.get() != nullptr); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 82 | REPORTER_ASSERT(reporter, memory.get() == pr->pixels()); |
| 83 | REPORTER_ASSERT(reporter, 0 == x); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 84 | pr.reset(nullptr); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 85 | // make sure that set_to_one_proc was called. |
| 86 | REPORTER_ASSERT(reporter, 1 == x); |
| 87 | } |
| 88 | { |
| 89 | void* addr = static_cast<void*>(new uint8_t[size]); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 90 | REPORTER_ASSERT(reporter, addr != nullptr); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 91 | sk_sp<SkMallocPixelRef> pr( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 92 | SkMallocPixelRef::NewWithProc(info, rowBytes, nullptr, addr, |
| 93 | delete_uint8_proc, nullptr)); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 94 | REPORTER_ASSERT(reporter, addr == pr->pixels()); |
| 95 | } |
| 96 | { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 97 | sk_sp<SkData> data(SkData::MakeUninitialized(size)); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 98 | SkData* dataPtr = data.get(); |
| 99 | REPORTER_ASSERT(reporter, dataPtr->unique()); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 100 | sk_sp<SkMallocPixelRef> pr( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 101 | SkMallocPixelRef::NewWithData(info, rowBytes, nullptr, data.get())); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 102 | REPORTER_ASSERT(reporter, !(dataPtr->unique())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 103 | data.reset(nullptr); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 104 | REPORTER_ASSERT(reporter, dataPtr->unique()); |
commit-bot@chromium.org | 2c4e75c | 2014-04-21 21:08:14 +0000 | [diff] [blame] | 105 | REPORTER_ASSERT(reporter, dataPtr->data() == pr->pixels()); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 106 | } |
| 107 | } |