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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
gpu / ipc / common / gpu_memory_buffer_impl_test_template.h [blame]
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
// This file defines tests that implementations of GpuMemoryBufferFactory should
// pass in order to be conformant.
#ifndef GPU_IPC_COMMON_GPU_MEMORY_BUFFER_IMPL_TEST_TEMPLATE_H_
#define GPU_IPC_COMMON_GPU_MEMORY_BUFFER_IMPL_TEST_TEMPLATE_H_
#include <stddef.h>
#include <string.h>
#include <memory>
#include "base/containers/heap_array.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "components/viz/test/test_gpu_service_holder.h"
#include "gpu/ipc/common/gpu_memory_buffer_support.h"
#include "mojo/public/cpp/base/shared_memory_mojom_traits.h"
#include "mojo/public/cpp/test_support/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/buffer_format_util.h"
#include "ui/gfx/buffer_usage_util.h"
#include "ui/gfx/mojom/buffer_types.mojom.h"
#include "ui/gl/gl_display.h"
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_OZONE)
#include "ui/gl/init/gl_factory.h"
#include "ui/gl/test/gl_surface_test_support.h"
#include "ui/ozone/public/ozone_platform.h"
#endif
namespace gpu {
template <typename GpuMemoryBufferImplType>
class GpuMemoryBufferImplTest : public testing::Test {
public:
GpuMemoryBufferImplTest() = default;
GpuMemoryBufferImpl::DestructionCallback CreateGpuMemoryBuffer(
const gfx::Size& size,
gfx::BufferFormat format,
gfx::BufferUsage usage,
gfx::GpuMemoryBufferHandle* handle,
bool* destroyed) {
return base::BindOnce(&GpuMemoryBufferImplTest::FreeGpuMemoryBuffer,
base::Unretained(this),
GpuMemoryBufferImplType::AllocateForTesting(
size, format, usage, handle),
base::Unretained(destroyed));
}
GpuMemoryBufferSupport* gpu_memory_buffer_support() {
return &gpu_memory_buffer_support_;
}
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_OZONE)
// Overridden from testing::Test:
void SetUp() override {
// https://crrev.com/c/5348599
// GmbImplTestNativePixmap is a no-op, we should run it on a gpu runner.
#if BUILDFLAG(IS_OZONE)
// TODO(329211602): Currently only wayland has a valid
// IsConfigurationSupportedForTest. We should implement that in X11 and
// other platforms either.
if (ui::OzonePlatform::GetPlatformNameForTest() == "wayland") {
run_gpu_test_ = true;
}
#endif
if (run_gpu_test_) {
#if BUILDFLAG(IS_OZONE)
// Make Ozone run in single-process mode.
ui::OzonePlatform::InitParams params;
params.single_process = true;
ui::OzonePlatform::InitializeForUI(params);
ui::OzonePlatform::InitializeForGPU(params);
#endif
}
display_ = gl::GLSurfaceTestSupport::InitializeOneOff();
if (run_gpu_test_) {
// Initialize the gpu service because wayland needs the service to pass
// the display events for initialization of supported formats, etc.
viz::TestGpuServiceHolder::GetInstance();
// Make sure all the tasks posted to the current task runner by the
// initialization functions are run before running the tests, for example,
// WaylandBufferManagerGpu::Initialize.
base::RunLoop().RunUntilIdle();
}
}
void TearDown() override {
if (run_gpu_test_) {
viz::TestGpuServiceHolder::ResetInstance();
}
gl::GLSurfaceTestSupport::ShutdownGL(display_);
}
#endif
protected:
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::MainThreadType::UI};
bool CheckGpuMemoryBufferHandle(const gfx::GpuMemoryBufferHandle& handle) {
#if BUILDFLAG(IS_OZONE)
// Pixmap backend could fail to allocate because of platform difference
// But it is expected behaviour, so we cannot fail.
// https://chromium-review.googlesource.com/c/chromium/src/+/5348599
#else
EXPECT_NE(handle.type, gfx::EMPTY_BUFFER);
#endif
return handle.type != gfx::EMPTY_BUFFER;
}
private:
bool run_gpu_test_ = false;
GpuMemoryBufferSupport gpu_memory_buffer_support_;
raw_ptr<gl::GLDisplay> display_ = nullptr;
void FreeGpuMemoryBuffer(base::OnceClosure free_callback, bool* destroyed) {
std::move(free_callback).Run();
if (destroyed) {
*destroyed = true;
}
}
};
// Subclass test case for tests that require a Create() method,
// not all implementations have that.
template <typename GpuMemoryBufferImplType>
class GpuMemoryBufferImplCreateTest : public testing::Test {
public:
GpuMemoryBufferImplCreateTest() = default;
GpuMemoryBufferSupport* gpu_memory_buffer_support() {
return &gpu_memory_buffer_support_;
}
private:
GpuMemoryBufferSupport gpu_memory_buffer_support_;
};
TYPED_TEST_SUITE_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::SCANOUT_CAMERA_READ_WRITE,
gfx::BufferUsage::CAMERA_AND_CPU_READ_WRITE,
gfx::BufferUsage::SCANOUT_CPU_READ_WRITE,
gfx::BufferUsage::SCANOUT_VDA_WRITE,
gfx::BufferUsage::PROTECTED_SCANOUT,
gfx::BufferUsage::PROTECTED_SCANOUT_VDA_WRITE,
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE,
gfx::BufferUsage::SCANOUT_VEA_CPU_READ,
gfx::BufferUsage::VEA_READ_CAMERA_AND_CPU_READ_WRITE,
};
for (auto usage : usages) {
if (!TestFixture::gpu_memory_buffer_support()
->IsConfigurationSupportedForTest(TypeParam::kBufferType, format,
usage)) {
continue;
}
bool destroyed = false;
gfx::GpuMemoryBufferHandle handle;
GpuMemoryBufferImpl::DestructionCallback destroy_callback =
TestFixture::CreateGpuMemoryBuffer(kBufferSize, format, usage,
&handle, &destroyed);
if (!TestFixture::CheckGpuMemoryBufferHandle(handle)) {
continue;
}
std::unique_ptr<GpuMemoryBufferImpl> buffer(
TestFixture::gpu_memory_buffer_support()
->CreateGpuMemoryBufferImplFromHandle(
std::move(handle), kBufferSize, format, usage,
std::move(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, CreateFromHandleSmallBuffer) {
const gfx::Size kBufferSize(8, 8);
for (auto format : gfx::GetBufferFormatsForTesting()) {
gfx::BufferUsage usages[] = {
gfx::BufferUsage::GPU_READ,
gfx::BufferUsage::SCANOUT,
gfx::BufferUsage::SCANOUT_CAMERA_READ_WRITE,
gfx::BufferUsage::CAMERA_AND_CPU_READ_WRITE,
gfx::BufferUsage::SCANOUT_CPU_READ_WRITE,
gfx::BufferUsage::SCANOUT_VDA_WRITE,
gfx::BufferUsage::PROTECTED_SCANOUT,
gfx::BufferUsage::PROTECTED_SCANOUT_VDA_WRITE,
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE,
gfx::BufferUsage::SCANOUT_VEA_CPU_READ,
gfx::BufferUsage::VEA_READ_CAMERA_AND_CPU_READ_WRITE,
};
for (auto usage : usages) {
if (!TestFixture::gpu_memory_buffer_support()
->IsConfigurationSupportedForTest(TypeParam::kBufferType, format,
usage)) {
continue;
}
bool destroyed = false;
gfx::GpuMemoryBufferHandle handle;
GpuMemoryBufferImpl::DestructionCallback destroy_callback =
TestFixture::CreateGpuMemoryBuffer(kBufferSize, format, usage,
&handle, &destroyed);
if (!TestFixture::CheckGpuMemoryBufferHandle(handle)) {
continue;
}
gfx::Size bogus_size = kBufferSize;
bogus_size.Enlarge(100, 100);
// Handle import should fail when the size is bigger than expected.
std::unique_ptr<GpuMemoryBufferImpl> buffer(
TestFixture::gpu_memory_buffer_support()
->CreateGpuMemoryBufferImplFromHandle(
std::move(handle), bogus_size, format, usage,
std::move(destroy_callback)));
// Only non-mappable GMB implementations can be imported with invalid
// size. In other words all GMP implementations that allow memory mapping
// must validate image size when importing a handle.
if (buffer) {
ASSERT_FALSE(buffer->Map());
}
}
}
}
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 (!TestFixture::gpu_memory_buffer_support()
->IsConfigurationSupportedForTest(
TypeParam::kBufferType, format,
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE)) {
continue;
}
gfx::GpuMemoryBufferHandle handle;
GpuMemoryBufferImpl::DestructionCallback destroy_callback =
TestFixture::CreateGpuMemoryBuffer(
kBufferSize, format, gfx::BufferUsage::GPU_READ_CPU_READ_WRITE,
&handle, nullptr);
if (!TestFixture::CheckGpuMemoryBufferHandle(handle)) {
continue;
}
std::unique_ptr<GpuMemoryBufferImpl> buffer(
TestFixture::gpu_memory_buffer_support()
->CreateGpuMemoryBufferImplFromHandle(
std::move(handle), kBufferSize, format,
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE,
std::move(destroy_callback)));
ASSERT_TRUE(buffer);
const size_t num_planes = gfx::NumberOfPlanesForLinearBufferFormat(format);
// Map buffer into user space.
ASSERT_TRUE(buffer->Map());
// Map the buffer a second time. This should be a noop and simply allow
// multiple clients concurrent read access. Likewise a subsequent Unmap()
// shouldn't invalidate the first's Map().
ASSERT_TRUE(buffer->Map());
buffer->Unmap();
// 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);
auto data = base::HeapArray<char>::Uninit(row_size_in_bytes);
memset(data.data(), 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.data(), row_size_in_bytes);
EXPECT_EQ(0, memcmp(static_cast<char*>(buffer->memory(plane)) +
y * buffer->stride(plane),
data.data(), 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 (!TestFixture::gpu_memory_buffer_support()
->IsConfigurationSupportedForTest(
TypeParam::kBufferType, format,
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE)) {
continue;
}
gfx::GpuMemoryBufferHandle handle;
GpuMemoryBufferImpl::DestructionCallback destroy_callback =
TestFixture::CreateGpuMemoryBuffer(
kBufferSize, format, gfx::BufferUsage::GPU_READ_CPU_READ_WRITE,
&handle, nullptr);
if (!TestFixture::CheckGpuMemoryBufferHandle(handle)) {
continue;
}
std::unique_ptr<GpuMemoryBufferImpl> buffer(
TestFixture::gpu_memory_buffer_support()
->CreateGpuMemoryBufferImplFromHandle(
std::move(handle), kBufferSize, format,
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE,
std::move(destroy_callback)));
ASSERT_TRUE(buffer);
// Map buffer into user space.
ASSERT_TRUE(buffer->Map());
// Copy and compare mapped buffers.
size_t num_planes = gfx::NumberOfPlanesForLinearBufferFormat(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);
auto data = base::HeapArray<char>::Uninit(row_size_in_bytes);
memset(data.data(), 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.data(), row_size_in_bytes);
EXPECT_EQ(0, memcmp(static_cast<char*>(buffer->memory(plane)) +
y * buffer->stride(plane),
data.data(), 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);
auto data = base::HeapArray<char>::Uninit(row_size_in_bytes);
memset(data.data(), 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.data(), row_size_in_bytes));
}
}
buffer->Unmap();
}
}
TYPED_TEST_P(GpuMemoryBufferImplTest, SerializeAndDeserialize) {
const gfx::Size kBufferSize(8, 8);
const gfx::GpuMemoryBufferType kBufferType = TypeParam::kBufferType;
for (auto format : gfx::GetBufferFormatsForTesting()) {
gfx::BufferUsage usages[] = {
gfx::BufferUsage::GPU_READ,
gfx::BufferUsage::SCANOUT,
gfx::BufferUsage::SCANOUT_CAMERA_READ_WRITE,
gfx::BufferUsage::CAMERA_AND_CPU_READ_WRITE,
gfx::BufferUsage::SCANOUT_CPU_READ_WRITE,
gfx::BufferUsage::SCANOUT_VDA_WRITE,
gfx::BufferUsage::PROTECTED_SCANOUT,
gfx::BufferUsage::PROTECTED_SCANOUT_VDA_WRITE,
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE,
gfx::BufferUsage::SCANOUT_VEA_CPU_READ,
gfx::BufferUsage::VEA_READ_CAMERA_AND_CPU_READ_WRITE,
};
for (auto usage : usages) {
if (!TestFixture::gpu_memory_buffer_support()
->IsConfigurationSupportedForTest(TypeParam::kBufferType, format,
usage)) {
continue;
}
bool destroyed = false;
gfx::GpuMemoryBufferHandle handle;
GpuMemoryBufferImpl::DestructionCallback destroy_callback =
TestFixture::CreateGpuMemoryBuffer(kBufferSize, format, usage,
&handle, &destroyed);
if (!TestFixture::CheckGpuMemoryBufferHandle(handle)) {
continue;
}
gfx::GpuMemoryBufferHandle output_handle;
mojo::test::SerializeAndDeserialize<gfx::mojom::GpuMemoryBufferHandle>(
handle, output_handle);
EXPECT_EQ(output_handle.type, kBufferType);
std::unique_ptr<GpuMemoryBufferImpl> buffer(
TestFixture::gpu_memory_buffer_support()
->CreateGpuMemoryBufferImplFromHandle(
std::move(output_handle), kBufferSize, format, usage,
std::move(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);
}
}
}
// The GpuMemoryBufferImplTest test case verifies behavior that is expected
// from a GpuMemoryBuffer implementation in order to be conformant.
REGISTER_TYPED_TEST_SUITE_P(GpuMemoryBufferImplTest,
CreateFromHandle,
CreateFromHandleSmallBuffer,
Map,
PersistentMap,
SerializeAndDeserialize);
TYPED_TEST_SUITE_P(GpuMemoryBufferImplCreateTest);
TYPED_TEST_P(GpuMemoryBufferImplCreateTest, Create) {
const gfx::GpuMemoryBufferId kBufferId(1);
const gfx::Size kBufferSize(8, 8);
gfx::BufferUsage usage = gfx::BufferUsage::GPU_READ;
for (auto format : gfx::GetBufferFormatsForTesting()) {
if (!TestFixture::gpu_memory_buffer_support()
->IsConfigurationSupportedForTest(TypeParam::kBufferType, format,
usage)) {
continue;
}
bool destroyed = false;
std::unique_ptr<TypeParam> buffer(TypeParam::Create(
kBufferId, kBufferSize, format, usage,
base::BindOnce([](bool* destroyed) { *destroyed = true; },
base::Unretained(&destroyed))));
ASSERT_TRUE(buffer);
EXPECT_EQ(buffer->GetFormat(), format);
// Check if destruction callback is executed when deleting the buffer.
buffer.reset();
ASSERT_TRUE(destroyed);
}
}
// The GpuMemoryBufferImplCreateTest test case verifies behavior that is
// expected from a GpuMemoryBuffer Create() implementation in order to be
// conformant.
REGISTER_TYPED_TEST_SUITE_P(GpuMemoryBufferImplCreateTest, Create);
} // namespace gpu
#endif // GPU_IPC_COMMON_GPU_MEMORY_BUFFER_IMPL_TEST_TEMPLATE_H_