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
DEF_TEST(MallocPixelRef,reporter)22 DEF_TEST(MallocPixelRef, reporter) {
23 REPORTER_ASSERT(reporter, true);
24 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 13);
25 {
26 sk_sp<SkPixelRef> pr(
27 SkMallocPixelRef::MakeAllocate(info, info.minRowBytes() - 1));
28 // rowbytes too small.
29 REPORTER_ASSERT(reporter, nullptr == pr.get());
30 }
31 {
32 size_t rowBytes = info.minRowBytes() - 1;
33 size_t size = info.computeByteSize(rowBytes);
34 sk_sp<SkData> data(SkData::MakeUninitialized(size));
35 sk_sp<SkPixelRef> pr(
36 SkMallocPixelRef::MakeWithData(info, rowBytes, data));
37 // rowbytes too small.
38 REPORTER_ASSERT(reporter, nullptr == pr.get());
39 }
40 {
41 size_t rowBytes = info.minRowBytes() + info.bytesPerPixel();
42 size_t size = info.computeByteSize(rowBytes) - 1;
43 sk_sp<SkData> data(SkData::MakeUninitialized(size));
44 sk_sp<SkPixelRef> pr(
45 SkMallocPixelRef::MakeWithData(info, rowBytes, data));
46 // data too small.
47 REPORTER_ASSERT(reporter, nullptr == pr.get());
48 }
49 size_t rowBytes = info.minRowBytes() + info.bytesPerPixel();
50 size_t size = info.computeByteSize(rowBytes) + 9;
51 {
52 SkAutoMalloc memory(size);
53 auto pr = sk_make_sp<SkPixelRef>(info.width(), info.height(), memory.get(), rowBytes);
54 REPORTER_ASSERT(reporter, pr.get() != nullptr);
55 REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
56 }
57 {
58 sk_sp<SkPixelRef> pr(
59 SkMallocPixelRef::MakeAllocate(info, rowBytes));
60 REPORTER_ASSERT(reporter, pr.get() != nullptr);
61 REPORTER_ASSERT(reporter, pr->pixels());
62 }
63 {
64 void* addr = static_cast<void*>(new uint8_t[size]);
65 sk_sp<SkPixelRef> pr(
66 SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, addr, delete_uint8_proc,
67 nullptr));
68 REPORTER_ASSERT(reporter, pr.get() != nullptr);
69 REPORTER_ASSERT(reporter, addr == pr->pixels());
70 }
71 {
72 int x = 0;
73 SkAutoMalloc memory(size);
74 sk_sp<SkPixelRef> pr(
75 SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, memory.get(),
76 set_to_one_proc, static_cast<void*>(&x)));
77 REPORTER_ASSERT(reporter, pr.get() != nullptr);
78 REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
79 REPORTER_ASSERT(reporter, 0 == x);
80 pr.reset(nullptr);
81 // make sure that set_to_one_proc was called.
82 REPORTER_ASSERT(reporter, 1 == x);
83 }
84 {
85 void* addr = static_cast<void*>(new uint8_t[size]);
86 REPORTER_ASSERT(reporter, addr != nullptr);
87 sk_sp<SkPixelRef> pr(
88 SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, addr, delete_uint8_proc,
89 nullptr));
90 REPORTER_ASSERT(reporter, addr == pr->pixels());
91 }
92 {
93 sk_sp<SkData> data(SkData::MakeUninitialized(size));
94 SkData* dataPtr = data.get();
95 REPORTER_ASSERT(reporter, dataPtr->unique());
96 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithData(info, rowBytes, data);
97 REPORTER_ASSERT(reporter, !(dataPtr->unique()));
98 data.reset(nullptr);
99 REPORTER_ASSERT(reporter, dataPtr->unique());
100 REPORTER_ASSERT(reporter, dataPtr->data() == pr->pixels());
101 }
102 }
103