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