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