1fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot/* 2fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot * Copyright 2013 Google Inc. 3fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot * 4fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot * Use of this source code is governed by a BSD-style license that can be 5fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot * found in the LICENSE file. 6fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot */ 7fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot 8fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot#include "SkAutoMalloc.h" 9fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot#include "SkData.h" 10fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot#include "SkMallocPixelRef.h" 11fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot#include "Test.h" 12fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot 13fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robotstatic void delete_uint8_proc(void* ptr, void*) { 14fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot delete[] static_cast<uint8_t*>(ptr); 15fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot} 16fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot 17fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robotstatic void set_to_one_proc(void*, void* context) { 18fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot *(static_cast<int*>(context)) = 1; 19fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot} 20fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot 21fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot/** 22fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot * This test contains basic sanity checks concerning SkMallocPixelRef. 23fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot */ 24fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team RobotDEF_TEST(MallocPixelRef, reporter) { 25fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, true); 26fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot SkImageInfo info = SkImageInfo::MakeN32Premul(10, 13); 27fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot { 28fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot sk_sp<SkPixelRef> pr( 29fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot SkMallocPixelRef::MakeAllocate(info, info.minRowBytes() - 1)); 30fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot // rowbytes too small. 31fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, nullptr == pr.get()); 32fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot } 33fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot { 34fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot size_t rowBytes = info.minRowBytes() - 1; 35fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot size_t size = info.computeByteSize(rowBytes); 36fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot sk_sp<SkData> data(SkData::MakeUninitialized(size)); 37fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot sk_sp<SkPixelRef> pr( 38fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot SkMallocPixelRef::MakeWithData(info, rowBytes, data)); 39fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot // rowbytes too small. 40fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, nullptr == pr.get()); 41fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot } 42fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot { 43fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot size_t rowBytes = info.minRowBytes() + 2; 44fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot size_t size = info.computeByteSize(rowBytes) - 1; 45fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot sk_sp<SkData> data(SkData::MakeUninitialized(size)); 46fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot sk_sp<SkPixelRef> pr( 47fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot SkMallocPixelRef::MakeWithData(info, rowBytes, data)); 48fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot // data too small. 49fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, nullptr == pr.get()); 50fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot } 51fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot size_t rowBytes = info.minRowBytes() + 7; 52fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot size_t size = info.computeByteSize(rowBytes) + 9; 53fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot { 54fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot SkAutoMalloc memory(size); 55fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot sk_sp<SkPixelRef> pr( 56fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot SkMallocPixelRef::MakeDirect(info, memory.get(), rowBytes)); 57fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, pr.get() != nullptr); 58fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, memory.get() == pr->pixels()); 59fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot } 60fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot { 61fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot sk_sp<SkPixelRef> pr( 62fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot SkMallocPixelRef::MakeAllocate(info, rowBytes)); 63fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, pr.get() != nullptr); 64fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, pr->pixels()); 65fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot } 66fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot { 67fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot void* addr = static_cast<void*>(new uint8_t[size]); 68fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot sk_sp<SkPixelRef> pr( 69fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot SkMallocPixelRef::MakeWithProc(info, rowBytes, addr, delete_uint8_proc, nullptr)); 70fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, pr.get() != nullptr); 71fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, addr == pr->pixels()); 72fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot } 73fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot { 74fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot int x = 0; 75fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot SkAutoMalloc memory(size); 76fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot sk_sp<SkPixelRef> pr( 77fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot SkMallocPixelRef::MakeWithProc(info, rowBytes, 78fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot memory.get(), set_to_one_proc, 79fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot static_cast<void*>(&x))); 80fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, pr.get() != nullptr); 81fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, memory.get() == pr->pixels()); 82fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, 0 == x); 83fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot pr.reset(nullptr); 84fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot // make sure that set_to_one_proc was called. 85fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, 1 == x); 86fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot } 87fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot { 88fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot int x = 0; 89fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot SkAutoMalloc memory(size); 90fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot sk_sp<SkPixelRef> pr( 91fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot SkMallocPixelRef::MakeWithProc(SkImageInfo::MakeN32Premul(-1, -1), rowBytes, 92fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot memory.get(), set_to_one_proc, 93fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot static_cast<void*>(&x))); 94fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, pr.get() == nullptr); 95fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot // make sure that set_to_one_proc was called. 96fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, 1 == x); 97fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot } 98fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot { 99fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot void* addr = static_cast<void*>(new uint8_t[size]); 100fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, addr != nullptr); 101fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot sk_sp<SkPixelRef> pr( 102fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot SkMallocPixelRef::MakeWithProc(info, rowBytes, addr, 103fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot delete_uint8_proc, nullptr)); 104fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, addr == pr->pixels()); 105fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot } 106fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot { 107fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot sk_sp<SkData> data(SkData::MakeUninitialized(size)); 108fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot SkData* dataPtr = data.get(); 109fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, dataPtr->unique()); 110fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithData(info, rowBytes, data); 111fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, !(dataPtr->unique())); 112fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot data.reset(nullptr); 113fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, dataPtr->unique()); 114fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot REPORTER_ASSERT(reporter, dataPtr->data() == pr->pixels()); 115fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot } 116fe17456d5e528078ce69b5f15cf7adf1fab963fandroid-build-team Robot} 117