From 1bed687f6b8fc67336f0f5d6fb5a5b38dd0fdff9 Mon Sep 17 00:00:00 2001 From: "halcanary@google.com" Date: Thu, 2 Jan 2014 17:29:28 +0000 Subject: Add a release procedure to SkMallocPixelRef; remove SkDataPixelRef This works in a way that is similar to SkData. SkMallocPixelRef::NewWithProc Motivation: Chrome has a ETC1PixelRef which calls delete[] on the pixles on destruction. There is no reason for them to almost duplicate our class, when we can provide them a more flexible class. Example use: static void delete_uint8_proc(void* ptr, void*) { delete[] static_cast(ptr); } SkPixelRef* new_delete_pixref(const SkImageInfo& info, SkColorTable* ctable) { size_t rb = info.minRowBytes(); return SkMallocPixelRef::NewWithProc( info, rb, ctable, new uint8_t[info.getSafeSize(rb)], delete_uint8_proc, NULL); } SkMallocPixelRef::NewWithData Motivation: This allows up to eliminate SkDataPixelRef. We modified SkImage_Raster to use MallocPixelRef rather than SkDataPixlRef. Also: Unit tests in tests/MallocPixelRefTest. BUG= R=reed@google.com Review URL: https://codereview.chromium.org/106883006 git-svn-id: http://skia.googlecode.com/svn/trunk@12861 2bbb7eff-a529-9590-31e7-b0007b416f81 --- tests/MallocPixelRefTest.cpp | 116 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 tests/MallocPixelRefTest.cpp (limited to 'tests/MallocPixelRefTest.cpp') diff --git a/tests/MallocPixelRefTest.cpp b/tests/MallocPixelRefTest.cpp new file mode 100644 index 0000000000..8fcdc86f93 --- /dev/null +++ b/tests/MallocPixelRefTest.cpp @@ -0,0 +1,116 @@ +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkData.h" +#include "SkMallocPixelRef.h" +#include "Test.h" +#include "TestClassDef.h" + +static void delete_uint8_proc(void* ptr, void*) { + delete[] static_cast(ptr); +} + +static void set_to_one_proc(void*, void* context) { + *(static_cast(context)) = 1; +} + +/** + * This test contains basic sanity checks concerning SkMallocPixelRef. + */ +DEF_TEST(MallocPixelRef, reporter) { + REPORTER_ASSERT(reporter, true); + SkImageInfo info = {10, 13, kPMColor_SkColorType, kPremul_SkAlphaType}; + { + SkAutoTUnref pr( + SkMallocPixelRef::NewAllocate(info, info.minRowBytes() - 1, NULL)); + // rowbytes too small. + REPORTER_ASSERT(reporter, NULL == pr.get()); + } + { + size_t rowBytes = info.minRowBytes() - 1; + size_t size = info.getSafeSize(rowBytes); + void* addr = sk_malloc_throw(size); + SkAutoDataUnref data(SkData::NewFromMalloc(addr, size)); + SkAutoTUnref pr( + SkMallocPixelRef::NewWithData(info, rowBytes, + NULL, data.get())); + // rowbytes too small. + REPORTER_ASSERT(reporter, NULL == pr.get()); + } + { + size_t rowBytes = info.minRowBytes() + 2; + size_t size = info.getSafeSize(rowBytes) - 1; + void* addr = sk_malloc_throw(size); + SkAutoDataUnref data(SkData::NewFromMalloc(addr, size)); + SkAutoTUnref pr( + SkMallocPixelRef::NewWithData(info, rowBytes, NULL, + data.get())); + // data too small. + REPORTER_ASSERT(reporter, NULL == pr.get()); + } + size_t rowBytes = info.minRowBytes() + 7; + size_t size = info.getSafeSize(rowBytes) + 9; + { + SkAutoMalloc memory(size); + SkAutoTUnref pr( + SkMallocPixelRef::NewDirect(info, memory.get(), rowBytes, NULL)); + REPORTER_ASSERT(reporter, pr.get() != NULL); + REPORTER_ASSERT(reporter, memory.get() == pr->pixels()); + } + { + SkAutoTUnref pr( + SkMallocPixelRef::NewAllocate(info, rowBytes, NULL)); + REPORTER_ASSERT(reporter, pr.get() != NULL); + REPORTER_ASSERT(reporter, NULL != pr->pixels()); + } + { + void* addr = static_cast(new uint8_t[size]); + SkAutoTUnref pr( + SkMallocPixelRef::NewWithProc(info, rowBytes, NULL, addr, + delete_uint8_proc, NULL)); + REPORTER_ASSERT(reporter, pr.get() != NULL); + REPORTER_ASSERT(reporter, addr == pr->pixels()); + } + { + int x = 0; + SkAutoMalloc memory(size); + REPORTER_ASSERT(reporter, memory.get() != NULL); + SkAutoTUnref pr( + SkMallocPixelRef::NewWithProc(info, rowBytes, NULL, + memory.get(), set_to_one_proc, + static_cast(&x))); + REPORTER_ASSERT(reporter, pr.get() != NULL); + REPORTER_ASSERT(reporter, memory.get() == pr->pixels()); + REPORTER_ASSERT(reporter, 0 == x); + pr.reset(NULL); + // make sure that set_to_one_proc was called. + REPORTER_ASSERT(reporter, 1 == x); + } + { + void* addr = static_cast(new uint8_t[size]); + REPORTER_ASSERT(reporter, addr != NULL); + SkAutoTUnref pr( + SkMallocPixelRef::NewWithProc(info, rowBytes, NULL, addr, + delete_uint8_proc, NULL)); + REPORTER_ASSERT(reporter, addr == pr->pixels()); + } + { + void* addr = sk_malloc_throw(size); + SkAutoDataUnref data(SkData::NewFromMalloc(addr, size)); + REPORTER_ASSERT(reporter, data.get() != NULL); + SkData* dataPtr = data.get(); + REPORTER_ASSERT(reporter, dataPtr->unique()); + SkAutoTUnref pr( + SkMallocPixelRef::NewWithData(info, rowBytes, NULL, data.get(), 4)); + REPORTER_ASSERT(reporter, !(dataPtr->unique())); + data.reset(NULL); + REPORTER_ASSERT(reporter, dataPtr->unique()); + REPORTER_ASSERT(reporter, + static_cast(dataPtr->bytes() + 4) == pr->pixels()); + } +} + -- cgit v1.2.3