blob: e267d6f72c425c40402bf7a798b0190e5fb5669f [file] [log] [blame]
halcanary@google.com1bed6872014-01-02 17:29:28 +00001/*
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"
halcanary@google.com1bed6872014-01-02 17:29:28 +000011
12static void delete_uint8_proc(void* ptr, void*) {
13 delete[] static_cast<uint8_t*>(ptr);
14}
15
16static 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 */
23DEF_TEST(MallocPixelRef, reporter) {
24 REPORTER_ASSERT(reporter, true);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +000025 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 13);
halcanary@google.com1bed6872014-01-02 17:29:28 +000026 {
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);
reed9594da12014-09-12 12:12:27 -070035 SkAutoDataUnref data(SkData::NewUninitialized(size));
halcanary@google.com1bed6872014-01-02 17:29:28 +000036 SkAutoTUnref<SkMallocPixelRef> pr(
reed9594da12014-09-12 12:12:27 -070037 SkMallocPixelRef::NewWithData(info, rowBytes, NULL, data));
halcanary@google.com1bed6872014-01-02 17:29:28 +000038 // rowbytes too small.
39 REPORTER_ASSERT(reporter, NULL == pr.get());
40 }
41 {
42 size_t rowBytes = info.minRowBytes() + 2;
43 size_t size = info.getSafeSize(rowBytes) - 1;
reed9594da12014-09-12 12:12:27 -070044 SkAutoDataUnref data(SkData::NewUninitialized(size));
halcanary@google.com1bed6872014-01-02 17:29:28 +000045 SkAutoTUnref<SkMallocPixelRef> pr(
reed9594da12014-09-12 12:12:27 -070046 SkMallocPixelRef::NewWithData(info, rowBytes, NULL, data));
halcanary@google.com1bed6872014-01-02 17:29:28 +000047 // data too small.
48 REPORTER_ASSERT(reporter, NULL == pr.get());
49 }
50 size_t rowBytes = info.minRowBytes() + 7;
51 size_t size = info.getSafeSize(rowBytes) + 9;
52 {
53 SkAutoMalloc memory(size);
54 SkAutoTUnref<SkMallocPixelRef> pr(
55 SkMallocPixelRef::NewDirect(info, memory.get(), rowBytes, NULL));
56 REPORTER_ASSERT(reporter, pr.get() != NULL);
57 REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
58 }
59 {
60 SkAutoTUnref<SkMallocPixelRef> pr(
61 SkMallocPixelRef::NewAllocate(info, rowBytes, NULL));
62 REPORTER_ASSERT(reporter, pr.get() != NULL);
bsalomon49f085d2014-09-05 13:34:00 -070063 REPORTER_ASSERT(reporter, pr->pixels());
halcanary@google.com1bed6872014-01-02 17:29:28 +000064 }
65 {
66 void* addr = static_cast<void*>(new uint8_t[size]);
67 SkAutoTUnref<SkMallocPixelRef> pr(
68 SkMallocPixelRef::NewWithProc(info, rowBytes, NULL, addr,
69 delete_uint8_proc, NULL));
70 REPORTER_ASSERT(reporter, pr.get() != NULL);
71 REPORTER_ASSERT(reporter, addr == pr->pixels());
72 }
73 {
74 int x = 0;
75 SkAutoMalloc memory(size);
halcanary@google.com1bed6872014-01-02 17:29:28 +000076 SkAutoTUnref<SkMallocPixelRef> pr(
77 SkMallocPixelRef::NewWithProc(info, rowBytes, NULL,
78 memory.get(), set_to_one_proc,
79 static_cast<void*>(&x)));
80 REPORTER_ASSERT(reporter, pr.get() != NULL);
81 REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
82 REPORTER_ASSERT(reporter, 0 == x);
83 pr.reset(NULL);
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 != NULL);
90 SkAutoTUnref<SkMallocPixelRef> pr(
91 SkMallocPixelRef::NewWithProc(info, rowBytes, NULL, addr,
92 delete_uint8_proc, NULL));
93 REPORTER_ASSERT(reporter, addr == pr->pixels());
94 }
95 {
reed9594da12014-09-12 12:12:27 -070096 SkAutoDataUnref data(SkData::NewUninitialized(size));
halcanary@google.com1bed6872014-01-02 17:29:28 +000097 SkData* dataPtr = data.get();
98 REPORTER_ASSERT(reporter, dataPtr->unique());
99 SkAutoTUnref<SkMallocPixelRef> pr(
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +0000100 SkMallocPixelRef::NewWithData(info, rowBytes, NULL, data.get()));
halcanary@google.com1bed6872014-01-02 17:29:28 +0000101 REPORTER_ASSERT(reporter, !(dataPtr->unique()));
102 data.reset(NULL);
103 REPORTER_ASSERT(reporter, dataPtr->unique());
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +0000104 REPORTER_ASSERT(reporter, dataPtr->data() == pr->pixels());
halcanary@google.com1bed6872014-01-02 17:29:28 +0000105 }
106}