blob: ac080994e2237c91e266e86208374df93b6db79a [file] [log] [blame]
halcanary@google.com1bed6872014-01-02 17:29:28 +00001/*
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 Canary95e3c052017-01-11 12:44:43 -05008#include "SkAutoMalloc.h"
halcanary@google.com1bed6872014-01-02 17:29:28 +00009#include "SkData.h"
10#include "SkMallocPixelRef.h"
11#include "Test.h"
halcanary@google.com1bed6872014-01-02 17:29:28 +000012
13static void delete_uint8_proc(void* ptr, void*) {
14 delete[] static_cast<uint8_t*>(ptr);
15}
16
17static 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 */
24DEF_TEST(MallocPixelRef, reporter) {
25 REPORTER_ASSERT(reporter, true);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +000026 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 13);
halcanary@google.com1bed6872014-01-02 17:29:28 +000027 {
Mike Reed6b3155c2017-04-03 14:41:44 -040028 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040029 SkMallocPixelRef::MakeAllocate(info, info.minRowBytes() - 1));
halcanary@google.com1bed6872014-01-02 17:29:28 +000030 // rowbytes too small.
halcanary96fcdcc2015-08-27 07:41:13 -070031 REPORTER_ASSERT(reporter, nullptr == pr.get());
halcanary@google.com1bed6872014-01-02 17:29:28 +000032 }
33 {
34 size_t rowBytes = info.minRowBytes() - 1;
Mike Reedf0ffb892017-10-03 14:47:21 -040035 size_t size = info.computeByteSize(rowBytes);
bungeman38d909e2016-08-02 14:40:46 -070036 sk_sp<SkData> data(SkData::MakeUninitialized(size));
Mike Reed6b3155c2017-04-03 14:41:44 -040037 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040038 SkMallocPixelRef::MakeWithData(info, rowBytes, data));
halcanary@google.com1bed6872014-01-02 17:29:28 +000039 // rowbytes too small.
halcanary96fcdcc2015-08-27 07:41:13 -070040 REPORTER_ASSERT(reporter, nullptr == pr.get());
halcanary@google.com1bed6872014-01-02 17:29:28 +000041 }
42 {
43 size_t rowBytes = info.minRowBytes() + 2;
Mike Reedf0ffb892017-10-03 14:47:21 -040044 size_t size = info.computeByteSize(rowBytes) - 1;
bungeman38d909e2016-08-02 14:40:46 -070045 sk_sp<SkData> data(SkData::MakeUninitialized(size));
Mike Reed6b3155c2017-04-03 14:41:44 -040046 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040047 SkMallocPixelRef::MakeWithData(info, rowBytes, data));
halcanary@google.com1bed6872014-01-02 17:29:28 +000048 // data too small.
halcanary96fcdcc2015-08-27 07:41:13 -070049 REPORTER_ASSERT(reporter, nullptr == pr.get());
halcanary@google.com1bed6872014-01-02 17:29:28 +000050 }
51 size_t rowBytes = info.minRowBytes() + 7;
Mike Reedf0ffb892017-10-03 14:47:21 -040052 size_t size = info.computeByteSize(rowBytes) + 9;
halcanary@google.com1bed6872014-01-02 17:29:28 +000053 {
54 SkAutoMalloc memory(size);
Mike Reed6b3155c2017-04-03 14:41:44 -040055 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040056 SkMallocPixelRef::MakeDirect(info, memory.get(), rowBytes));
halcanary96fcdcc2015-08-27 07:41:13 -070057 REPORTER_ASSERT(reporter, pr.get() != nullptr);
halcanary@google.com1bed6872014-01-02 17:29:28 +000058 REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
59 }
60 {
Mike Reed6b3155c2017-04-03 14:41:44 -040061 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040062 SkMallocPixelRef::MakeAllocate(info, rowBytes));
halcanary96fcdcc2015-08-27 07:41:13 -070063 REPORTER_ASSERT(reporter, pr.get() != nullptr);
bsalomon49f085d2014-09-05 13:34:00 -070064 REPORTER_ASSERT(reporter, pr->pixels());
halcanary@google.com1bed6872014-01-02 17:29:28 +000065 }
66 {
67 void* addr = static_cast<void*>(new uint8_t[size]);
Mike Reed6b3155c2017-04-03 14:41:44 -040068 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040069 SkMallocPixelRef::MakeWithProc(info, rowBytes, addr, delete_uint8_proc, nullptr));
halcanary96fcdcc2015-08-27 07:41:13 -070070 REPORTER_ASSERT(reporter, pr.get() != nullptr);
halcanary@google.com1bed6872014-01-02 17:29:28 +000071 REPORTER_ASSERT(reporter, addr == pr->pixels());
72 }
73 {
74 int x = 0;
75 SkAutoMalloc memory(size);
Mike Reed6b3155c2017-04-03 14:41:44 -040076 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040077 SkMallocPixelRef::MakeWithProc(info, rowBytes,
Mike Reed6b3155c2017-04-03 14:41:44 -040078 memory.get(), set_to_one_proc,
79 static_cast<void*>(&x)));
halcanary96fcdcc2015-08-27 07:41:13 -070080 REPORTER_ASSERT(reporter, pr.get() != nullptr);
halcanary@google.com1bed6872014-01-02 17:29:28 +000081 REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
82 REPORTER_ASSERT(reporter, 0 == x);
halcanary96fcdcc2015-08-27 07:41:13 -070083 pr.reset(nullptr);
halcanary@google.com1bed6872014-01-02 17:29:28 +000084 // make sure that set_to_one_proc was called.
85 REPORTER_ASSERT(reporter, 1 == x);
86 }
87 {
Ben Wagner373b5382017-03-02 17:33:21 -050088 int x = 0;
89 SkAutoMalloc memory(size);
Mike Reed6b3155c2017-04-03 14:41:44 -040090 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040091 SkMallocPixelRef::MakeWithProc(SkImageInfo::MakeN32Premul(-1, -1), rowBytes,
Mike Reed6b3155c2017-04-03 14:41:44 -040092 memory.get(), set_to_one_proc,
93 static_cast<void*>(&x)));
Ben Wagner373b5382017-03-02 17:33:21 -050094 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.com1bed6872014-01-02 17:29:28 +000099 void* addr = static_cast<void*>(new uint8_t[size]);
halcanary96fcdcc2015-08-27 07:41:13 -0700100 REPORTER_ASSERT(reporter, addr != nullptr);
Mike Reed6b3155c2017-04-03 14:41:44 -0400101 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -0400102 SkMallocPixelRef::MakeWithProc(info, rowBytes, addr,
Mike Reed6b3155c2017-04-03 14:41:44 -0400103 delete_uint8_proc, nullptr));
halcanary@google.com1bed6872014-01-02 17:29:28 +0000104 REPORTER_ASSERT(reporter, addr == pr->pixels());
105 }
106 {
bungeman38d909e2016-08-02 14:40:46 -0700107 sk_sp<SkData> data(SkData::MakeUninitialized(size));
halcanary@google.com1bed6872014-01-02 17:29:28 +0000108 SkData* dataPtr = data.get();
109 REPORTER_ASSERT(reporter, dataPtr->unique());
Mike Reed086a4272017-07-18 10:53:11 -0400110 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithData(info, rowBytes, data);
halcanary@google.com1bed6872014-01-02 17:29:28 +0000111 REPORTER_ASSERT(reporter, !(dataPtr->unique()));
halcanary96fcdcc2015-08-27 07:41:13 -0700112 data.reset(nullptr);
halcanary@google.com1bed6872014-01-02 17:29:28 +0000113 REPORTER_ASSERT(reporter, dataPtr->unique());
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +0000114 REPORTER_ASSERT(reporter, dataPtr->data() == pr->pixels());
halcanary@google.com1bed6872014-01-02 17:29:28 +0000115 }
116}