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