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 "include/core/SkData.h"
9 #include "include/core/SkMallocPixelRef.h"
10 #include "src/core/SkAutoMalloc.h"
11 #include "src/core/SkPixelRefPriv.h"
12 #include "tests/Test.h"
13
delete_uint8_proc(void * ptr,void *)14 static void delete_uint8_proc(void* ptr, void*) {
15 delete[] static_cast<uint8_t*>(ptr);
16 }
17
set_to_one_proc(void *,void * context)18 static void set_to_one_proc(void*, void* context) {
19 *(static_cast<int*>(context)) = 1;
20 }
21
22 /**
23 * This test contains basic sanity checks concerning SkMallocPixelRef.
24 */
DEF_TEST(MallocPixelRef,reporter)25 DEF_TEST(MallocPixelRef, reporter) {
26 REPORTER_ASSERT(reporter, true);
27 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 13);
28 {
29 sk_sp<SkPixelRef> pr(
30 SkMallocPixelRef::MakeAllocate(info, info.minRowBytes() - 1));
31 // rowbytes too small.
32 REPORTER_ASSERT(reporter, nullptr == pr.get());
33 }
34 {
35 size_t rowBytes = info.minRowBytes() - 1;
36 size_t size = info.computeByteSize(rowBytes);
37 sk_sp<SkData> data(SkData::MakeUninitialized(size));
38 sk_sp<SkPixelRef> pr(
39 SkMallocPixelRef::MakeWithData(info, rowBytes, data));
40 // rowbytes too small.
41 REPORTER_ASSERT(reporter, nullptr == pr.get());
42 }
43 {
44 size_t rowBytes = info.minRowBytes() + 2;
45 size_t size = info.computeByteSize(rowBytes) - 1;
46 sk_sp<SkData> data(SkData::MakeUninitialized(size));
47 sk_sp<SkPixelRef> pr(
48 SkMallocPixelRef::MakeWithData(info, rowBytes, data));
49 // data too small.
50 REPORTER_ASSERT(reporter, nullptr == pr.get());
51 }
52 size_t rowBytes = info.minRowBytes() + 7;
53 size_t size = info.computeByteSize(rowBytes) + 9;
54 {
55 SkAutoMalloc memory(size);
56 auto pr = sk_make_sp<SkPixelRef>(info.width(), info.height(), 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 SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, addr, delete_uint8_proc,
70 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<SkPixelRef> pr(
78 SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, memory.get(),
79 set_to_one_proc, 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 void* addr = static_cast<void*>(new uint8_t[size]);
89 REPORTER_ASSERT(reporter, addr != nullptr);
90 sk_sp<SkPixelRef> pr(
91 SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, addr, delete_uint8_proc,
92 nullptr));
93 REPORTER_ASSERT(reporter, addr == pr->pixels());
94 }
95 {
96 sk_sp<SkData> data(SkData::MakeUninitialized(size));
97 SkData* dataPtr = data.get();
98 REPORTER_ASSERT(reporter, dataPtr->unique());
99 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithData(info, rowBytes, data);
100 REPORTER_ASSERT(reporter, !(dataPtr->unique()));
101 data.reset(nullptr);
102 REPORTER_ASSERT(reporter, dataPtr->unique());
103 REPORTER_ASSERT(reporter, dataPtr->data() == pr->pixels());
104 }
105 }
106