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 | { |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame^] | 28 | sk_sp<SkPixelRef> pr( |
| 29 | SkMallocPixelRef::MakeAllocate(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)); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame^] | 37 | sk_sp<SkPixelRef> pr( |
| 38 | SkMallocPixelRef::MakeWithData(info, rowBytes, nullptr, data)); |
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)); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame^] | 46 | sk_sp<SkPixelRef> pr( |
| 47 | SkMallocPixelRef::MakeWithData(info, rowBytes, nullptr, data)); |
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); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame^] | 55 | sk_sp<SkPixelRef> pr( |
| 56 | SkMallocPixelRef::MakeDirect(info, memory.get(), rowBytes, nullptr)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 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 | { |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame^] | 61 | sk_sp<SkPixelRef> pr( |
| 62 | SkMallocPixelRef::MakeAllocate(info, rowBytes, nullptr)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 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]); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame^] | 68 | sk_sp<SkPixelRef> pr( |
| 69 | SkMallocPixelRef::MakeWithProc(info, rowBytes, nullptr, addr, |
| 70 | delete_uint8_proc, nullptr)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 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); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame^] | 77 | sk_sp<SkPixelRef> pr( |
| 78 | SkMallocPixelRef::MakeWithProc(info, rowBytes, nullptr, |
| 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 | { |
Ben Wagner | 373b538 | 2017-03-02 17:33:21 -0500 | [diff] [blame] | 89 | int x = 0; |
| 90 | SkAutoMalloc memory(size); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame^] | 91 | sk_sp<SkPixelRef> pr( |
| 92 | SkMallocPixelRef::MakeWithProc(SkImageInfo::MakeN32Premul(-1, -1), rowBytes, nullptr, |
| 93 | memory.get(), set_to_one_proc, |
| 94 | static_cast<void*>(&x))); |
Ben Wagner | 373b538 | 2017-03-02 17:33:21 -0500 | [diff] [blame] | 95 | REPORTER_ASSERT(reporter, pr.get() == nullptr); |
| 96 | // make sure that set_to_one_proc was called. |
| 97 | REPORTER_ASSERT(reporter, 1 == x); |
| 98 | } |
| 99 | { |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 100 | void* addr = static_cast<void*>(new uint8_t[size]); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 101 | REPORTER_ASSERT(reporter, addr != nullptr); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame^] | 102 | sk_sp<SkPixelRef> pr( |
| 103 | SkMallocPixelRef::MakeWithProc(info, rowBytes, nullptr, addr, |
| 104 | delete_uint8_proc, nullptr)); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 105 | REPORTER_ASSERT(reporter, addr == pr->pixels()); |
| 106 | } |
| 107 | { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 108 | sk_sp<SkData> data(SkData::MakeUninitialized(size)); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 109 | SkData* dataPtr = data.get(); |
| 110 | REPORTER_ASSERT(reporter, dataPtr->unique()); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame^] | 111 | sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithData(info, rowBytes, nullptr, data); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 112 | REPORTER_ASSERT(reporter, !(dataPtr->unique())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 113 | data.reset(nullptr); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 114 | REPORTER_ASSERT(reporter, dataPtr->unique()); |
commit-bot@chromium.org | 2c4e75c | 2014-04-21 21:08:14 +0000 | [diff] [blame] | 115 | REPORTER_ASSERT(reporter, dataPtr->data() == pr->pixels()); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 116 | } |
| 117 | } |