Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
MallocPixelRefTest.cpp
Go to the documentation of this file.
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
15#include "tests/Test.h"
16
17#include <cstddef>
18#include <cstdint>
19
20static void delete_uint8_proc(void* ptr, void*) {
21 delete[] static_cast<uint8_t*>(ptr);
22}
23
24static void set_to_one_proc(void*, void* context) {
25 *(static_cast<int*>(context)) = 1;
26}
27
28DEF_TEST(MallocPixelRef, reporter) {
31 {
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);
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;
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 {
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]);
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);
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());
86 pr.reset(nullptr);
87 // make sure that set_to_one_proc was called.
89 }
90 {
91 void* addr = static_cast<void*>(new uint8_t[size]);
92 REPORTER_ASSERT(reporter, addr != nullptr);
94 SkMakePixelRefWithProc(info.width(), info.height(), rowBytes, addr, delete_uint8_proc,
95 nullptr));
96 REPORTER_ASSERT(reporter, addr == pr->pixels());
97 }
98 {
100 SkData* dataPtr = data.get();
101 REPORTER_ASSERT(reporter, dataPtr->unique());
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}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
reporter
static void delete_uint8_proc(void *ptr, void *)
static void set_to_one_proc(void *, void *context)
sk_sp< SkPixelRef > SkMakePixelRefWithProc(int width, int height, size_t rowBytes, void *addr, void(*releaseProc)(void *addr, void *ctx), void *ctx)
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
void * get()
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition SkData.cpp:116
const void * data() const
Definition SkData.h:37
bool unique() const
Definition SkRefCnt.h:175
T * get() const
Definition SkRefCnt.h:303
void reset(T *ptr=nullptr)
Definition SkRefCnt.h:310
double x
SK_API sk_sp< SkPixelRef > MakeWithData(const SkImageInfo &, size_t rowBytes, sk_sp< SkData > data)
SK_API sk_sp< SkPixelRef > MakeAllocate(const SkImageInfo &, size_t rowBytes)
static SkImageInfo MakeN32Premul(int width, int height)