1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file defines tests that implementations of GpuMemoryBufferFactory should
// pass in order to be conformant.
#ifndef CONTENT_TEST_GPU_MEMORY_BUFFER_IMPL_TEST_TEMPLATE_H_
#define CONTENT_TEST_GPU_MEMORY_BUFFER_IMPL_TEST_TEMPLATE_H_
#include <stddef.h>
#include <string.h>
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/buffer_format_util.h"
namespace content {
template <typename GpuMemoryBufferImplType>
class GpuMemoryBufferImplTest : public testing::Test {
public:
GpuMemoryBufferImpl::DestructionCallback AllocateGpuMemoryBuffer(
const gfx::Size& size,
gfx::BufferFormat format,
gfx::BufferUsage usage,
gfx::GpuMemoryBufferHandle* handle,
bool* destroyed) {
return base::Bind(&GpuMemoryBufferImplTest::FreeGpuMemoryBuffer,
base::Unretained(this),
GpuMemoryBufferImplType::AllocateForTesting(
size, format, usage, handle),
base::Unretained(destroyed));
}
private:
void FreeGpuMemoryBuffer(const base::Closure& free_callback,
bool* destroyed,
const gpu::SyncToken& sync_token) {
free_callback.Run();
if (destroyed)
*destroyed = true;
}
};
TYPED_TEST_CASE_P(GpuMemoryBufferImplTest);
TYPED_TEST_P(GpuMemoryBufferImplTest, CreateFromHandle) {
const gfx::Size kBufferSize(8, 8);
for (auto format : gfx::GetBufferFormatsForTesting()) {
gfx::BufferUsage usages[] = {
gfx::BufferUsage::GPU_READ, gfx::BufferUsage::SCANOUT,
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE,
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE_PERSISTENT};
for (auto usage : usages) {
if (!TypeParam::IsConfigurationSupported(format, usage))
continue;
bool destroyed = false;
gfx::GpuMemoryBufferHandle handle;
GpuMemoryBufferImpl::DestructionCallback destroy_callback =
TestFixture::AllocateGpuMemoryBuffer(kBufferSize, format, usage,
&handle, &destroyed);
scoped_ptr<TypeParam> buffer(TypeParam::CreateFromHandle(
handle, kBufferSize, format, usage, destroy_callback));
ASSERT_TRUE(buffer);
EXPECT_EQ(buffer->GetFormat(), format);
// Check if destruction callback is executed when deleting the buffer.
buffer.reset();
ASSERT_TRUE(destroyed);
}
}
}
TYPED_TEST_P(GpuMemoryBufferImplTest, Map) {
// Use a multiple of 4 for both dimensions to support compressed formats.
const gfx::Size kBufferSize(4, 4);
for (auto format : gfx::GetBufferFormatsForTesting()) {
if (!TypeParam::IsConfigurationSupported(
format, gfx::BufferUsage::GPU_READ_CPU_READ_WRITE)) {
continue;
}
gfx::GpuMemoryBufferHandle handle;
GpuMemoryBufferImpl::DestructionCallback destroy_callback =
TestFixture::AllocateGpuMemoryBuffer(
kBufferSize, format, gfx::BufferUsage::GPU_READ_CPU_READ_WRITE,
&handle, nullptr);
scoped_ptr<TypeParam> buffer(TypeParam::CreateFromHandle(
handle, kBufferSize, format, gfx::BufferUsage::GPU_READ_CPU_READ_WRITE,
destroy_callback));
ASSERT_TRUE(buffer);
const size_t num_planes = gfx::NumberOfPlanesForBufferFormat(format);
// Map buffer into user space.
ASSERT_TRUE(buffer->Map());
// Copy and compare mapped buffers.
for (size_t plane = 0; plane < num_planes; ++plane) {
const size_t row_size_in_bytes =
gfx::RowSizeForBufferFormat(kBufferSize.width(), format, plane);
EXPECT_GT(row_size_in_bytes, 0u);
scoped_ptr<char[]> data(new char[row_size_in_bytes]);
memset(data.get(), 0x2a + plane, row_size_in_bytes);
size_t height = kBufferSize.height() /
gfx::SubsamplingFactorForBufferFormat(format, plane);
for (size_t y = 0; y < height; ++y) {
memcpy(static_cast<char*>(buffer->memory(plane)) +
y * buffer->stride(plane),
data.get(), row_size_in_bytes);
EXPECT_EQ(0, memcmp(static_cast<char*>(buffer->memory(plane)) +
y * buffer->stride(plane),
data.get(), row_size_in_bytes));
}
}
buffer->Unmap();
}
}
TYPED_TEST_P(GpuMemoryBufferImplTest, PersistentMap) {
// Use a multiple of 4 for both dimensions to support compressed formats.
const gfx::Size kBufferSize(4, 4);
for (auto format : gfx::GetBufferFormatsForTesting()) {
if (!TypeParam::IsConfigurationSupported(
format, gfx::BufferUsage::GPU_READ_CPU_READ_WRITE_PERSISTENT)) {
continue;
}
gfx::GpuMemoryBufferHandle handle;
GpuMemoryBufferImpl::DestructionCallback destroy_callback =
TestFixture::AllocateGpuMemoryBuffer(
kBufferSize, format,
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE_PERSISTENT, &handle,
nullptr);
scoped_ptr<TypeParam> buffer(TypeParam::CreateFromHandle(
handle, kBufferSize, format,
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE_PERSISTENT,
destroy_callback));
ASSERT_TRUE(buffer);
// Map buffer into user space.
ASSERT_TRUE(buffer->Map());
// Copy and compare mapped buffers.
size_t num_planes = gfx::NumberOfPlanesForBufferFormat(format);
for (size_t plane = 0; plane < num_planes; ++plane) {
const size_t row_size_in_bytes =
gfx::RowSizeForBufferFormat(kBufferSize.width(), format, plane);
EXPECT_GT(row_size_in_bytes, 0u);
scoped_ptr<char[]> data(new char[row_size_in_bytes]);
memset(data.get(), 0x2a + plane, row_size_in_bytes);
size_t height = kBufferSize.height() /
gfx::SubsamplingFactorForBufferFormat(format, plane);
for (size_t y = 0; y < height; ++y) {
memcpy(static_cast<char*>(buffer->memory(plane)) +
y * buffer->stride(plane),
data.get(), row_size_in_bytes);
EXPECT_EQ(0, memcmp(static_cast<char*>(buffer->memory(plane)) +
y * buffer->stride(plane),
data.get(), row_size_in_bytes));
}
}
buffer->Unmap();
// Remap the buffer, and compare again. It should contain the same data.
ASSERT_TRUE(buffer->Map());
for (size_t plane = 0; plane < num_planes; ++plane) {
const size_t row_size_in_bytes =
gfx::RowSizeForBufferFormat(kBufferSize.width(), format, plane);
scoped_ptr<char[]> data(new char[row_size_in_bytes]);
memset(data.get(), 0x2a + plane, row_size_in_bytes);
size_t height = kBufferSize.height() /
gfx::SubsamplingFactorForBufferFormat(format, plane);
for (size_t y = 0; y < height; ++y) {
EXPECT_EQ(0, memcmp(static_cast<char*>(buffer->memory(plane)) +
y * buffer->stride(plane),
data.get(), row_size_in_bytes));
}
}
buffer->Unmap();
}
}
// The GpuMemoryBufferImplTest test case verifies behavior that is expected
// from a GpuMemoryBuffer implementation in order to be conformant.
REGISTER_TYPED_TEST_CASE_P(GpuMemoryBufferImplTest,
CreateFromHandle,
Map,
PersistentMap);
} // namespace content
#endif // CONTENT_TEST_GPU_MEMORY_BUFFER_IMPL_TEST_TEMPLATE_H_
|