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