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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkData.h" |
| 9 | #include "include/core/SkMallocPixelRef.h" |
| 10 | #include "src/core/SkAutoMalloc.h" |
Hal Canary | bb10848 | 2019-08-14 12:19:20 -0400 | [diff] [blame] | 11 | #include "src/core/SkPixelRefPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "tests/Test.h" |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 13 | |
| 14 | static void delete_uint8_proc(void* ptr, void*) { |
| 15 | delete[] static_cast<uint8_t*>(ptr); |
| 16 | } |
| 17 | |
| 18 | static void set_to_one_proc(void*, void* context) { |
| 19 | *(static_cast<int*>(context)) = 1; |
| 20 | } |
| 21 | |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 22 | DEF_TEST(MallocPixelRef, reporter) { |
| 23 | REPORTER_ASSERT(reporter, true); |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 24 | SkImageInfo info = SkImageInfo::MakeN32Premul(10, 13); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 25 | { |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 26 | sk_sp<SkPixelRef> pr( |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 27 | SkMallocPixelRef::MakeAllocate(info, info.minRowBytes() - 1)); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 28 | // rowbytes too small. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 29 | REPORTER_ASSERT(reporter, nullptr == pr.get()); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 30 | } |
| 31 | { |
| 32 | size_t rowBytes = info.minRowBytes() - 1; |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 33 | size_t size = info.computeByteSize(rowBytes); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 34 | sk_sp<SkData> data(SkData::MakeUninitialized(size)); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 35 | sk_sp<SkPixelRef> pr( |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 36 | SkMallocPixelRef::MakeWithData(info, rowBytes, data)); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 37 | // rowbytes too small. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 38 | REPORTER_ASSERT(reporter, nullptr == pr.get()); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 39 | } |
| 40 | { |
Leon Scroggins III | 3eedc97 | 2020-01-22 10:44:00 -0500 | [diff] [blame] | 41 | size_t rowBytes = info.minRowBytes() + info.bytesPerPixel(); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 42 | size_t size = info.computeByteSize(rowBytes) - 1; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 43 | sk_sp<SkData> data(SkData::MakeUninitialized(size)); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 44 | sk_sp<SkPixelRef> pr( |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 45 | SkMallocPixelRef::MakeWithData(info, rowBytes, data)); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 46 | // data too small. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 47 | REPORTER_ASSERT(reporter, nullptr == pr.get()); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 48 | } |
Leon Scroggins III | 3eedc97 | 2020-01-22 10:44:00 -0500 | [diff] [blame] | 49 | size_t rowBytes = info.minRowBytes() + info.bytesPerPixel(); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 50 | size_t size = info.computeByteSize(rowBytes) + 9; |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 51 | { |
| 52 | SkAutoMalloc memory(size); |
Hal Canary | bb10848 | 2019-08-14 12:19:20 -0400 | [diff] [blame] | 53 | auto pr = sk_make_sp<SkPixelRef>(info.width(), info.height(), memory.get(), rowBytes); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 54 | REPORTER_ASSERT(reporter, pr.get() != nullptr); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 55 | REPORTER_ASSERT(reporter, memory.get() == pr->pixels()); |
| 56 | } |
| 57 | { |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 58 | sk_sp<SkPixelRef> pr( |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 59 | SkMallocPixelRef::MakeAllocate(info, rowBytes)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 60 | REPORTER_ASSERT(reporter, pr.get() != nullptr); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 61 | REPORTER_ASSERT(reporter, pr->pixels()); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 62 | } |
| 63 | { |
| 64 | void* addr = static_cast<void*>(new uint8_t[size]); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 65 | sk_sp<SkPixelRef> pr( |
Hal Canary | bb10848 | 2019-08-14 12:19:20 -0400 | [diff] [blame] | 66 | SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, addr, delete_uint8_proc, |
| 67 | nullptr)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 68 | REPORTER_ASSERT(reporter, pr.get() != nullptr); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 69 | REPORTER_ASSERT(reporter, addr == pr->pixels()); |
| 70 | } |
| 71 | { |
| 72 | int x = 0; |
| 73 | SkAutoMalloc memory(size); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 74 | sk_sp<SkPixelRef> pr( |
Hal Canary | bb10848 | 2019-08-14 12:19:20 -0400 | [diff] [blame] | 75 | SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, memory.get(), |
| 76 | set_to_one_proc, static_cast<void*>(&x))); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | REPORTER_ASSERT(reporter, pr.get() != nullptr); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 78 | REPORTER_ASSERT(reporter, memory.get() == pr->pixels()); |
| 79 | REPORTER_ASSERT(reporter, 0 == x); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | pr.reset(nullptr); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 81 | // make sure that set_to_one_proc was called. |
| 82 | REPORTER_ASSERT(reporter, 1 == x); |
| 83 | } |
| 84 | { |
| 85 | void* addr = static_cast<void*>(new uint8_t[size]); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 86 | REPORTER_ASSERT(reporter, addr != nullptr); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 87 | sk_sp<SkPixelRef> pr( |
Hal Canary | bb10848 | 2019-08-14 12:19:20 -0400 | [diff] [blame] | 88 | SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, addr, delete_uint8_proc, |
| 89 | nullptr)); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 90 | REPORTER_ASSERT(reporter, addr == pr->pixels()); |
| 91 | } |
| 92 | { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 93 | sk_sp<SkData> data(SkData::MakeUninitialized(size)); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 94 | SkData* dataPtr = data.get(); |
| 95 | REPORTER_ASSERT(reporter, dataPtr->unique()); |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 96 | sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithData(info, rowBytes, data); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 97 | REPORTER_ASSERT(reporter, !(dataPtr->unique())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 98 | data.reset(nullptr); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 99 | REPORTER_ASSERT(reporter, dataPtr->unique()); |
commit-bot@chromium.org | 2c4e75c | 2014-04-21 21:08:14 +0000 | [diff] [blame] | 100 | REPORTER_ASSERT(reporter, dataPtr->data() == pr->pixels()); |
halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame] | 101 | } |
| 102 | } |