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