2014-01-02 17:29:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkData.h"
|
|
|
|
#include "include/core/SkMallocPixelRef.h"
|
|
|
|
#include "src/core/SkAutoMalloc.h"
|
|
|
|
#include "tests/Test.h"
|
2014-01-02 17:29:28 +00:00
|
|
|
|
|
|
|
static void delete_uint8_proc(void* ptr, void*) {
|
|
|
|
delete[] static_cast<uint8_t*>(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_to_one_proc(void*, void* context) {
|
|
|
|
*(static_cast<int*>(context)) = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This test contains basic sanity checks concerning SkMallocPixelRef.
|
|
|
|
*/
|
|
|
|
DEF_TEST(MallocPixelRef, reporter) {
|
|
|
|
REPORTER_ASSERT(reporter, true);
|
2014-01-15 02:38:22 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(10, 13);
|
2014-01-02 17:29:28 +00:00
|
|
|
{
|
2017-04-03 18:41:44 +00:00
|
|
|
sk_sp<SkPixelRef> pr(
|
2017-07-18 14:53:11 +00:00
|
|
|
SkMallocPixelRef::MakeAllocate(info, info.minRowBytes() - 1));
|
2014-01-02 17:29:28 +00:00
|
|
|
// rowbytes too small.
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == pr.get());
|
2014-01-02 17:29:28 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t rowBytes = info.minRowBytes() - 1;
|
2017-10-03 18:47:21 +00:00
|
|
|
size_t size = info.computeByteSize(rowBytes);
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> data(SkData::MakeUninitialized(size));
|
2017-04-03 18:41:44 +00:00
|
|
|
sk_sp<SkPixelRef> pr(
|
2017-07-18 14:53:11 +00:00
|
|
|
SkMallocPixelRef::MakeWithData(info, rowBytes, data));
|
2014-01-02 17:29:28 +00:00
|
|
|
// rowbytes too small.
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == pr.get());
|
2014-01-02 17:29:28 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t rowBytes = info.minRowBytes() + 2;
|
2017-10-03 18:47:21 +00:00
|
|
|
size_t size = info.computeByteSize(rowBytes) - 1;
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> data(SkData::MakeUninitialized(size));
|
2017-04-03 18:41:44 +00:00
|
|
|
sk_sp<SkPixelRef> pr(
|
2017-07-18 14:53:11 +00:00
|
|
|
SkMallocPixelRef::MakeWithData(info, rowBytes, data));
|
2014-01-02 17:29:28 +00:00
|
|
|
// data too small.
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == pr.get());
|
2014-01-02 17:29:28 +00:00
|
|
|
}
|
|
|
|
size_t rowBytes = info.minRowBytes() + 7;
|
2017-10-03 18:47:21 +00:00
|
|
|
size_t size = info.computeByteSize(rowBytes) + 9;
|
2014-01-02 17:29:28 +00:00
|
|
|
{
|
|
|
|
SkAutoMalloc memory(size);
|
2017-04-03 18:41:44 +00:00
|
|
|
sk_sp<SkPixelRef> pr(
|
2017-07-18 14:53:11 +00:00
|
|
|
SkMallocPixelRef::MakeDirect(info, memory.get(), rowBytes));
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, pr.get() != nullptr);
|
2014-01-02 17:29:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
|
|
|
|
}
|
|
|
|
{
|
2017-04-03 18:41:44 +00:00
|
|
|
sk_sp<SkPixelRef> pr(
|
2017-07-18 14:53:11 +00:00
|
|
|
SkMallocPixelRef::MakeAllocate(info, rowBytes));
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, pr.get() != nullptr);
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, pr->pixels());
|
2014-01-02 17:29:28 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
void* addr = static_cast<void*>(new uint8_t[size]);
|
2017-04-03 18:41:44 +00:00
|
|
|
sk_sp<SkPixelRef> pr(
|
2017-07-18 14:53:11 +00:00
|
|
|
SkMallocPixelRef::MakeWithProc(info, rowBytes, addr, delete_uint8_proc, nullptr));
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, pr.get() != nullptr);
|
2014-01-02 17:29:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, addr == pr->pixels());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
int x = 0;
|
|
|
|
SkAutoMalloc memory(size);
|
2017-04-03 18:41:44 +00:00
|
|
|
sk_sp<SkPixelRef> pr(
|
2017-07-18 14:53:11 +00:00
|
|
|
SkMallocPixelRef::MakeWithProc(info, rowBytes,
|
2017-04-03 18:41:44 +00:00
|
|
|
memory.get(), set_to_one_proc,
|
|
|
|
static_cast<void*>(&x)));
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, pr.get() != nullptr);
|
2014-01-02 17:29:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == x);
|
2015-08-27 14:41:13 +00:00
|
|
|
pr.reset(nullptr);
|
2014-01-02 17:29:28 +00:00
|
|
|
// make sure that set_to_one_proc was called.
|
|
|
|
REPORTER_ASSERT(reporter, 1 == x);
|
|
|
|
}
|
2017-03-02 22:33:21 +00:00
|
|
|
{
|
|
|
|
int x = 0;
|
|
|
|
SkAutoMalloc memory(size);
|
2017-04-03 18:41:44 +00:00
|
|
|
sk_sp<SkPixelRef> pr(
|
2017-07-18 14:53:11 +00:00
|
|
|
SkMallocPixelRef::MakeWithProc(SkImageInfo::MakeN32Premul(-1, -1), rowBytes,
|
2017-04-03 18:41:44 +00:00
|
|
|
memory.get(), set_to_one_proc,
|
|
|
|
static_cast<void*>(&x)));
|
2017-03-02 22:33:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, pr.get() == nullptr);
|
|
|
|
// make sure that set_to_one_proc was called.
|
|
|
|
REPORTER_ASSERT(reporter, 1 == x);
|
|
|
|
}
|
2014-01-02 17:29:28 +00:00
|
|
|
{
|
|
|
|
void* addr = static_cast<void*>(new uint8_t[size]);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, addr != nullptr);
|
2017-04-03 18:41:44 +00:00
|
|
|
sk_sp<SkPixelRef> pr(
|
2017-07-18 14:53:11 +00:00
|
|
|
SkMallocPixelRef::MakeWithProc(info, rowBytes, addr,
|
2017-04-03 18:41:44 +00:00
|
|
|
delete_uint8_proc, nullptr));
|
2014-01-02 17:29:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, addr == pr->pixels());
|
|
|
|
}
|
|
|
|
{
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> data(SkData::MakeUninitialized(size));
|
2014-01-02 17:29:28 +00:00
|
|
|
SkData* dataPtr = data.get();
|
|
|
|
REPORTER_ASSERT(reporter, dataPtr->unique());
|
2017-07-18 14:53:11 +00:00
|
|
|
sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithData(info, rowBytes, data);
|
2014-01-02 17:29:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, !(dataPtr->unique()));
|
2015-08-27 14:41:13 +00:00
|
|
|
data.reset(nullptr);
|
2014-01-02 17:29:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, dataPtr->unique());
|
2014-04-21 21:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, dataPtr->data() == pr->pixels());
|
2014-01-02 17:29:28 +00:00
|
|
|
}
|
|
|
|
}
|