MallocPixelRefTest.cpp revision 1bed687f6b8fc67336f0f5d6fb5a5b38dd0fdff9
1/*
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
8#include "SkData.h"
9#include "SkMallocPixelRef.h"
10#include "Test.h"
11#include "TestClassDef.h"
12
13static void delete_uint8_proc(void* ptr, void*) {
14    delete[] static_cast<uint8_t*>(ptr);
15}
16
17static void set_to_one_proc(void*, void* context) {
18    *(static_cast<int*>(context)) = 1;
19}
20
21/**
22 *  This test contains basic sanity checks concerning SkMallocPixelRef.
23 */
24DEF_TEST(MallocPixelRef, reporter) {
25    REPORTER_ASSERT(reporter, true);
26    SkImageInfo info = {10, 13, kPMColor_SkColorType, kPremul_SkAlphaType};
27    {
28        SkAutoTUnref<SkMallocPixelRef> pr(
29            SkMallocPixelRef::NewAllocate(info, info.minRowBytes() - 1, NULL));
30        // rowbytes too small.
31        REPORTER_ASSERT(reporter, NULL == pr.get());
32    }
33    {
34        size_t rowBytes = info.minRowBytes() - 1;
35        size_t size = info.getSafeSize(rowBytes);
36        void* addr = sk_malloc_throw(size);
37        SkAutoDataUnref data(SkData::NewFromMalloc(addr, size));
38        SkAutoTUnref<SkMallocPixelRef> pr(
39            SkMallocPixelRef::NewWithData(info, rowBytes,
40                                          NULL, data.get()));
41        // rowbytes too small.
42        REPORTER_ASSERT(reporter, NULL == pr.get());
43    }
44    {
45        size_t rowBytes = info.minRowBytes() + 2;
46        size_t size = info.getSafeSize(rowBytes) - 1;
47        void* addr = sk_malloc_throw(size);
48        SkAutoDataUnref data(SkData::NewFromMalloc(addr, size));
49        SkAutoTUnref<SkMallocPixelRef> pr(
50            SkMallocPixelRef::NewWithData(info, rowBytes, NULL,
51                                          data.get()));
52        // data too small.
53        REPORTER_ASSERT(reporter, NULL == pr.get());
54    }
55    size_t rowBytes = info.minRowBytes() + 7;
56    size_t size = info.getSafeSize(rowBytes) + 9;
57    {
58        SkAutoMalloc memory(size);
59        SkAutoTUnref<SkMallocPixelRef> pr(
60            SkMallocPixelRef::NewDirect(info, memory.get(), rowBytes, NULL));
61        REPORTER_ASSERT(reporter, pr.get() != NULL);
62        REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
63    }
64    {
65        SkAutoTUnref<SkMallocPixelRef> pr(
66            SkMallocPixelRef::NewAllocate(info, rowBytes, NULL));
67        REPORTER_ASSERT(reporter, pr.get() != NULL);
68        REPORTER_ASSERT(reporter, NULL != pr->pixels());
69    }
70    {
71        void* addr = static_cast<void*>(new uint8_t[size]);
72        SkAutoTUnref<SkMallocPixelRef> pr(
73            SkMallocPixelRef::NewWithProc(info, rowBytes, NULL, addr,
74                                          delete_uint8_proc, NULL));
75        REPORTER_ASSERT(reporter, pr.get() != NULL);
76        REPORTER_ASSERT(reporter, addr == pr->pixels());
77    }
78    {
79        int x = 0;
80        SkAutoMalloc memory(size);
81        REPORTER_ASSERT(reporter, memory.get() != NULL);
82        SkAutoTUnref<SkMallocPixelRef> pr(
83            SkMallocPixelRef::NewWithProc(info, rowBytes, NULL,
84                                          memory.get(), set_to_one_proc,
85                                          static_cast<void*>(&x)));
86        REPORTER_ASSERT(reporter, pr.get() != NULL);
87        REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
88        REPORTER_ASSERT(reporter, 0 == x);
89        pr.reset(NULL);
90        // make sure that set_to_one_proc was called.
91        REPORTER_ASSERT(reporter, 1 == x);
92    }
93    {
94        void* addr = static_cast<void*>(new uint8_t[size]);
95        REPORTER_ASSERT(reporter, addr != NULL);
96        SkAutoTUnref<SkMallocPixelRef> pr(
97            SkMallocPixelRef::NewWithProc(info, rowBytes, NULL, addr,
98                                          delete_uint8_proc, NULL));
99        REPORTER_ASSERT(reporter, addr == pr->pixels());
100    }
101    {
102        void* addr = sk_malloc_throw(size);
103        SkAutoDataUnref data(SkData::NewFromMalloc(addr, size));
104        REPORTER_ASSERT(reporter, data.get() != NULL);
105        SkData* dataPtr = data.get();
106        REPORTER_ASSERT(reporter, dataPtr->unique());
107        SkAutoTUnref<SkMallocPixelRef> pr(
108            SkMallocPixelRef::NewWithData(info, rowBytes, NULL, data.get(), 4));
109        REPORTER_ASSERT(reporter, !(dataPtr->unique()));
110        data.reset(NULL);
111        REPORTER_ASSERT(reporter, dataPtr->unique());
112        REPORTER_ASSERT(reporter,
113            static_cast<const void*>(dataPtr->bytes() + 4) == pr->pixels());
114    }
115}
116
117