blob: 1288a279f1da8d89681184b1fc814866023fc66d [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
halcanary@google.com1bed6872014-01-02 17:29:28 +000022DEF_TEST(MallocPixelRef, reporter) {
23 REPORTER_ASSERT(reporter, true);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +000024 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 13);
halcanary@google.com1bed6872014-01-02 17:29:28 +000025 {
Mike Reed6b3155c2017-04-03 14:41:44 -040026 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040027 SkMallocPixelRef::MakeAllocate(info, info.minRowBytes() - 1));
halcanary@google.com1bed6872014-01-02 17:29:28 +000028 // rowbytes too small.
halcanary96fcdcc2015-08-27 07:41:13 -070029 REPORTER_ASSERT(reporter, nullptr == pr.get());
halcanary@google.com1bed6872014-01-02 17:29:28 +000030 }
31 {
32 size_t rowBytes = info.minRowBytes() - 1;
Mike Reedf0ffb892017-10-03 14:47:21 -040033 size_t size = info.computeByteSize(rowBytes);
bungeman38d909e2016-08-02 14:40:46 -070034 sk_sp<SkData> data(SkData::MakeUninitialized(size));
Mike Reed6b3155c2017-04-03 14:41:44 -040035 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040036 SkMallocPixelRef::MakeWithData(info, rowBytes, data));
halcanary@google.com1bed6872014-01-02 17:29:28 +000037 // rowbytes too small.
halcanary96fcdcc2015-08-27 07:41:13 -070038 REPORTER_ASSERT(reporter, nullptr == pr.get());
halcanary@google.com1bed6872014-01-02 17:29:28 +000039 }
40 {
Leon Scroggins III3eedc972020-01-22 10:44:00 -050041 size_t rowBytes = info.minRowBytes() + info.bytesPerPixel();
Mike Reedf0ffb892017-10-03 14:47:21 -040042 size_t size = info.computeByteSize(rowBytes) - 1;
bungeman38d909e2016-08-02 14:40:46 -070043 sk_sp<SkData> data(SkData::MakeUninitialized(size));
Mike Reed6b3155c2017-04-03 14:41:44 -040044 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040045 SkMallocPixelRef::MakeWithData(info, rowBytes, data));
halcanary@google.com1bed6872014-01-02 17:29:28 +000046 // data too small.
halcanary96fcdcc2015-08-27 07:41:13 -070047 REPORTER_ASSERT(reporter, nullptr == pr.get());
halcanary@google.com1bed6872014-01-02 17:29:28 +000048 }
Leon Scroggins III3eedc972020-01-22 10:44:00 -050049 size_t rowBytes = info.minRowBytes() + info.bytesPerPixel();
Mike Reedf0ffb892017-10-03 14:47:21 -040050 size_t size = info.computeByteSize(rowBytes) + 9;
halcanary@google.com1bed6872014-01-02 17:29:28 +000051 {
52 SkAutoMalloc memory(size);
Hal Canarybb108482019-08-14 12:19:20 -040053 auto pr = sk_make_sp<SkPixelRef>(info.width(), info.height(), memory.get(), rowBytes);
halcanary96fcdcc2015-08-27 07:41:13 -070054 REPORTER_ASSERT(reporter, pr.get() != nullptr);
halcanary@google.com1bed6872014-01-02 17:29:28 +000055 REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
56 }
57 {
Mike Reed6b3155c2017-04-03 14:41:44 -040058 sk_sp<SkPixelRef> pr(
Mike Reed086a4272017-07-18 10:53:11 -040059 SkMallocPixelRef::MakeAllocate(info, rowBytes));
halcanary96fcdcc2015-08-27 07:41:13 -070060 REPORTER_ASSERT(reporter, pr.get() != nullptr);
bsalomon49f085d2014-09-05 13:34:00 -070061 REPORTER_ASSERT(reporter, pr->pixels());
halcanary@google.com1bed6872014-01-02 17:29:28 +000062 }
63 {
64 void* addr = static_cast<void*>(new uint8_t[size]);
Mike Reed6b3155c2017-04-03 14:41:44 -040065 sk_sp<SkPixelRef> pr(
Hal Canarybb108482019-08-14 12:19:20 -040066 SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, addr, delete_uint8_proc,
67 nullptr));
halcanary96fcdcc2015-08-27 07:41:13 -070068 REPORTER_ASSERT(reporter, pr.get() != nullptr);
halcanary@google.com1bed6872014-01-02 17:29:28 +000069 REPORTER_ASSERT(reporter, addr == pr->pixels());
70 }
71 {
72 int x = 0;
73 SkAutoMalloc memory(size);
Mike Reed6b3155c2017-04-03 14:41:44 -040074 sk_sp<SkPixelRef> pr(
Hal Canarybb108482019-08-14 12:19:20 -040075 SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, memory.get(),
76 set_to_one_proc, static_cast<void*>(&x)));
halcanary96fcdcc2015-08-27 07:41:13 -070077 REPORTER_ASSERT(reporter, pr.get() != nullptr);
halcanary@google.com1bed6872014-01-02 17:29:28 +000078 REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
79 REPORTER_ASSERT(reporter, 0 == x);
halcanary96fcdcc2015-08-27 07:41:13 -070080 pr.reset(nullptr);
halcanary@google.com1bed6872014-01-02 17:29:28 +000081 // 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]);
halcanary96fcdcc2015-08-27 07:41:13 -070086 REPORTER_ASSERT(reporter, addr != nullptr);
Mike Reed6b3155c2017-04-03 14:41:44 -040087 sk_sp<SkPixelRef> pr(
Hal Canarybb108482019-08-14 12:19:20 -040088 SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, addr, delete_uint8_proc,
89 nullptr));
halcanary@google.com1bed6872014-01-02 17:29:28 +000090 REPORTER_ASSERT(reporter, addr == pr->pixels());
91 }
92 {
bungeman38d909e2016-08-02 14:40:46 -070093 sk_sp<SkData> data(SkData::MakeUninitialized(size));
halcanary@google.com1bed6872014-01-02 17:29:28 +000094 SkData* dataPtr = data.get();
95 REPORTER_ASSERT(reporter, dataPtr->unique());
Mike Reed086a4272017-07-18 10:53:11 -040096 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithData(info, rowBytes, data);
halcanary@google.com1bed6872014-01-02 17:29:28 +000097 REPORTER_ASSERT(reporter, !(dataPtr->unique()));
halcanary96fcdcc2015-08-27 07:41:13 -070098 data.reset(nullptr);
halcanary@google.com1bed6872014-01-02 17:29:28 +000099 REPORTER_ASSERT(reporter, dataPtr->unique());
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +0000100 REPORTER_ASSERT(reporter, dataPtr->data() == pr->pixels());
halcanary@google.com1bed6872014-01-02 17:29:28 +0000101 }
102}