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 
13 static void delete_uint8_proc(void* ptr, void*) {
14     delete[] static_cast<uint8_t*>(ptr);
15 }
16 
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  */
24 DEF_TEST(MallocPixelRef, reporter) {
25     REPORTER_ASSERT(reporter, true);
26     SkImageInfo info = SkImageInfo::MakeN32Premul(10, 13);
27     {
28         sk_sp<SkPixelRef> pr(
29             SkMallocPixelRef::MakeAllocate(info, info.minRowBytes() - 1));
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.computeByteSize(rowBytes);
36         sk_sp<SkData> data(SkData::MakeUninitialized(size));
37         sk_sp<SkPixelRef> pr(
38             SkMallocPixelRef::MakeWithData(info, rowBytes, data));
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.computeByteSize(rowBytes) - 1;
45         sk_sp<SkData> data(SkData::MakeUninitialized(size));
46         sk_sp<SkPixelRef> pr(
47             SkMallocPixelRef::MakeWithData(info, rowBytes, data));
48         // data too small.
49         REPORTER_ASSERT(reporter, nullptr == pr.get());
50     }
51     size_t rowBytes = info.minRowBytes() + 7;
52     size_t size = info.computeByteSize(rowBytes) + 9;
53     {
54         SkAutoMalloc memory(size);
55         sk_sp<SkPixelRef> pr(
56             SkMallocPixelRef::MakeDirect(info, memory.get(), rowBytes));
57         REPORTER_ASSERT(reporter, pr.get() != nullptr);
58         REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
59     }
60     {
61         sk_sp<SkPixelRef> pr(
62             SkMallocPixelRef::MakeAllocate(info, rowBytes));
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<SkPixelRef> pr(
69             SkMallocPixelRef::MakeWithProc(info, rowBytes, addr, delete_uint8_proc, nullptr));
70         REPORTER_ASSERT(reporter, pr.get() != nullptr);
71         REPORTER_ASSERT(reporter, addr == pr->pixels());
72     }
73     {
74         int x = 0;
75         SkAutoMalloc memory(size);
76         sk_sp<SkPixelRef> pr(
77             SkMallocPixelRef::MakeWithProc(info, rowBytes,
78                                            memory.get(), set_to_one_proc,
79                                            static_cast<void*>(&x)));
80         REPORTER_ASSERT(reporter, pr.get() != nullptr);
81         REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
82         REPORTER_ASSERT(reporter, 0 == x);
83         pr.reset(nullptr);
84         // make sure that set_to_one_proc was called.
85         REPORTER_ASSERT(reporter, 1 == x);
86     }
87     {
88         int x = 0;
89         SkAutoMalloc memory(size);
90         sk_sp<SkPixelRef> pr(
91             SkMallocPixelRef::MakeWithProc(SkImageInfo::MakeN32Premul(-1, -1), rowBytes,
92                                            memory.get(), set_to_one_proc,
93                                            static_cast<void*>(&x)));
94         REPORTER_ASSERT(reporter, pr.get() == nullptr);
95         // make sure that set_to_one_proc was called.
96         REPORTER_ASSERT(reporter, 1 == x);
97     }
98     {
99         void* addr = static_cast<void*>(new uint8_t[size]);
100         REPORTER_ASSERT(reporter, addr != nullptr);
101         sk_sp<SkPixelRef> pr(
102             SkMallocPixelRef::MakeWithProc(info, rowBytes, addr,
103                                            delete_uint8_proc, nullptr));
104         REPORTER_ASSERT(reporter, addr == pr->pixels());
105     }
106     {
107         sk_sp<SkData> data(SkData::MakeUninitialized(size));
108         SkData* dataPtr = data.get();
109         REPORTER_ASSERT(reporter, dataPtr->unique());
110         sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithData(info, rowBytes, data);
111         REPORTER_ASSERT(reporter, !(dataPtr->unique()));
112         data.reset(nullptr);
113         REPORTER_ASSERT(reporter, dataPtr->unique());
114         REPORTER_ASSERT(reporter, dataPtr->data() == pr->pixels());
115     }
116 }
117