blob: b20e5bbb0c50b4d4caedd73e7454f1d4844c4ab0 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkData.h"
9#include "include/core/SkMallocPixelRef.h"
10#include "src/core/SkAutoMalloc.h"
Hal Canarybb108482019-08-14 12:19:20 -040011#include "src/core/SkPixelRefPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "tests/Test.h"
halcanary@google.com1bed6872014-01-02 17:29:28 +000013
14static void delete_uint8_proc(void* ptr, void*) {
15 delete[] static_cast<uint8_t*>(ptr);
16}
17
18static void set_to_one_proc(void*, void* context) {
19 *(static_cast<int*>(context)) = 1;
20}
21
22/**
23 * This test contains basic sanity checks concerning SkMallocPixelRef.
24 */
25DEF_TEST(MallocPixelRef, reporter) {
26 REPORTER_ASSERT(reporter, true);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +000027 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 13);
halcanary@google.com1bed6872014-01-02 17:29:28 +000028 {
Mike Reed6b3155c2017-04-03 14:41:44 -040029 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040030 SkMallocPixelRef::MakeAllocate(info, info.minRowBytes() - 1));
halcanary@google.com1bed6872014-01-02 17:29:28 +000031 // rowbytes too small.
halcanary96fcdcc2015-08-27 07:41:13 -070032 REPORTER_ASSERT(reporter, nullptr == pr.get());
halcanary@google.com1bed6872014-01-02 17:29:28 +000033 }
34 {
35 size_t rowBytes = info.minRowBytes() - 1;
Mike Reedf0ffb892017-10-03 14:47:21 -040036 size_t size = info.computeByteSize(rowBytes);
bungeman38d909e2016-08-02 14:40:46 -070037 sk_sp<SkData> data(SkData::MakeUninitialized(size));
Mike Reed6b3155c2017-04-03 14:41:44 -040038 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040039 SkMallocPixelRef::MakeWithData(info, rowBytes, data));
halcanary@google.com1bed6872014-01-02 17:29:28 +000040 // rowbytes too small.
halcanary96fcdcc2015-08-27 07:41:13 -070041 REPORTER_ASSERT(reporter, nullptr == pr.get());
halcanary@google.com1bed6872014-01-02 17:29:28 +000042 }
43 {
Leon Scroggins III3eedc972020-01-22 10:44:00 -050044 size_t rowBytes = info.minRowBytes() + info.bytesPerPixel();
Mike Reedf0ffb892017-10-03 14:47:21 -040045 size_t size = info.computeByteSize(rowBytes) - 1;
bungeman38d909e2016-08-02 14:40:46 -070046 sk_sp<SkData> data(SkData::MakeUninitialized(size));
Mike Reed6b3155c2017-04-03 14:41:44 -040047 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040048 SkMallocPixelRef::MakeWithData(info, rowBytes, data));
halcanary@google.com1bed6872014-01-02 17:29:28 +000049 // data too small.
halcanary96fcdcc2015-08-27 07:41:13 -070050 REPORTER_ASSERT(reporter, nullptr == pr.get());
halcanary@google.com1bed6872014-01-02 17:29:28 +000051 }
Leon Scroggins III3eedc972020-01-22 10:44:00 -050052 size_t rowBytes = info.minRowBytes() + info.bytesPerPixel();
Mike Reedf0ffb892017-10-03 14:47:21 -040053 size_t size = info.computeByteSize(rowBytes) + 9;
halcanary@google.com1bed6872014-01-02 17:29:28 +000054 {
55 SkAutoMalloc memory(size);
Hal Canarybb108482019-08-14 12:19:20 -040056 auto pr = sk_make_sp<SkPixelRef>(info.width(), info.height(), 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(
Hal Canarybb108482019-08-14 12:19:20 -040069 SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, addr, delete_uint8_proc,
70 nullptr));
halcanary96fcdcc2015-08-27 07:41:13 -070071 REPORTER_ASSERT(reporter, pr.get() != nullptr);
halcanary@google.com1bed6872014-01-02 17:29:28 +000072 REPORTER_ASSERT(reporter, addr == pr->pixels());
73 }
74 {
75 int x = 0;
76 SkAutoMalloc memory(size);
Mike Reed6b3155c2017-04-03 14:41:44 -040077 sk_sp<SkPixelRef> pr(
Hal Canarybb108482019-08-14 12:19:20 -040078 SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, memory.get(),
79 set_to_one_proc, 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 {
88 void* addr = static_cast<void*>(new uint8_t[size]);
halcanary96fcdcc2015-08-27 07:41:13 -070089 REPORTER_ASSERT(reporter, addr != nullptr);
Mike Reed6b3155c2017-04-03 14:41:44 -040090 sk_sp<SkPixelRef> pr(
Hal Canarybb108482019-08-14 12:19:20 -040091 SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, addr, delete_uint8_proc,
92 nullptr));
halcanary@google.com1bed6872014-01-02 17:29:28 +000093 REPORTER_ASSERT(reporter, addr == pr->pixels());
94 }
95 {
bungeman38d909e2016-08-02 14:40:46 -070096 sk_sp<SkData> data(SkData::MakeUninitialized(size));
halcanary@google.com1bed6872014-01-02 17:29:28 +000097 SkData* dataPtr = data.get();
98 REPORTER_ASSERT(reporter, dataPtr->unique());
Mike Reed086a4272017-07-18 10:53:11 -040099 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithData(info, rowBytes, data);
halcanary@google.com1bed6872014-01-02 17:29:28 +0000100 REPORTER_ASSERT(reporter, !(dataPtr->unique()));
halcanary96fcdcc2015-08-27 07:41:13 -0700101 data.reset(nullptr);
halcanary@google.com1bed6872014-01-02 17:29:28 +0000102 REPORTER_ASSERT(reporter, dataPtr->unique());
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +0000103 REPORTER_ASSERT(reporter, dataPtr->data() == pr->pixels());
halcanary@google.com1bed6872014-01-02 17:29:28 +0000104 }
105}