1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/common/gpu/client/gpu_memory_buffer_impl.h"
8 #include "content/common/gpu/gpu_memory_buffer_factory.h"
9 #include "testing/gtest/include/gtest/gtest.h"
14 const int kClientId
= 1;
16 class GpuMemoryBufferImplTest
17 : public testing::TestWithParam
<gfx::GpuMemoryBufferType
> {
19 GpuMemoryBufferImplTest() : buffer_count_(0), factory_(nullptr) {}
21 // Overridden from testing::Test:
22 void SetUp() override
{
23 factory_
= GpuMemoryBufferFactory::Create(GetParam());
24 factory_
->GetSupportedGpuMemoryBufferConfigurations(
25 &supported_configurations_
);
27 void TearDown() override
{ factory_
.reset(); }
29 gfx::GpuMemoryBufferHandle
CreateGpuMemoryBuffer(
30 gfx::GpuMemoryBufferId id
,
31 const gfx::Size
& size
,
32 gfx::GpuMemoryBuffer::Format format
,
33 gfx::GpuMemoryBuffer::Usage usage
) {
35 return factory_
->CreateGpuMemoryBuffer(id
, size
, format
, usage
, kClientId
,
36 gfx::kNullPluginWindow
);
39 void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id
, uint32 sync_point
) {
40 factory_
->DestroyGpuMemoryBuffer(id
, kClientId
);
41 DCHECK_GT(buffer_count_
, 0);
45 std::vector
<GpuMemoryBufferFactory::Configuration
> supported_configurations_
;
49 scoped_ptr
<GpuMemoryBufferFactory
> factory_
;
52 TEST_P(GpuMemoryBufferImplTest
, CreateFromHandle
) {
53 const int kBufferId
= 1;
55 gfx::Size
buffer_size(8, 8);
57 for (auto configuration
: supported_configurations_
) {
58 scoped_ptr
<GpuMemoryBufferImpl
> buffer(
59 GpuMemoryBufferImpl::CreateFromHandle(
60 CreateGpuMemoryBuffer(kBufferId
,
66 base::Bind(&GpuMemoryBufferImplTest::DestroyGpuMemoryBuffer
,
67 base::Unretained(this),
69 EXPECT_EQ(1, buffer_count_
);
71 EXPECT_EQ(buffer
->GetFormat(), configuration
.format
);
73 // Check if destruction callback is executed when deleting the buffer.
75 EXPECT_EQ(0, buffer_count_
);
79 TEST_P(GpuMemoryBufferImplTest
, Map
) {
80 const int kBufferId
= 1;
82 gfx::Size
buffer_size(2, 2);
84 for (auto configuration
: supported_configurations_
) {
85 if (configuration
.usage
!= gfx::GpuMemoryBuffer::MAP
)
88 scoped_ptr
<GpuMemoryBufferImpl
> buffer(
89 GpuMemoryBufferImpl::CreateFromHandle(
90 CreateGpuMemoryBuffer(kBufferId
, buffer_size
, configuration
.format
,
92 buffer_size
, configuration
.format
,
93 base::Bind(&GpuMemoryBufferImplTest::DestroyGpuMemoryBuffer
,
94 base::Unretained(this), kBufferId
)));
96 EXPECT_FALSE(buffer
->IsMapped());
99 GpuMemoryBufferImpl::NumberOfPlanesForGpuMemoryBufferFormat(
100 configuration
.format
);
102 // Map buffer into user space.
103 scoped_ptr
<void*[]> mapped_buffers(new void*[num_planes
]);
104 bool rv
= buffer
->Map(mapped_buffers
.get());
106 EXPECT_TRUE(buffer
->IsMapped());
109 scoped_ptr
<int[]> strides(new int[num_planes
]);
110 buffer
->GetStride(strides
.get());
112 // Copy and compare mapped buffers.
113 for (size_t i
= 0; i
< num_planes
; ++i
) {
114 size_t width_in_bytes
= 0u;
115 EXPECT_TRUE(GpuMemoryBufferImpl::StrideInBytes(
116 buffer_size
.width(), configuration
.format
, i
, &width_in_bytes
));
117 EXPECT_GT(width_in_bytes
, 0u);
119 scoped_ptr
<char[]> data(new char[width_in_bytes
]);
120 memset(data
.get(), 0x2a + i
, width_in_bytes
);
121 memcpy(mapped_buffers
[i
], data
.get(), width_in_bytes
);
122 EXPECT_EQ(memcmp(mapped_buffers
[i
], data
.get(), width_in_bytes
), 0);
126 EXPECT_FALSE(buffer
->IsMapped());
130 std::vector
<gfx::GpuMemoryBufferType
> GetSupportedGpuMemoryBufferTypes() {
131 std::vector
<gfx::GpuMemoryBufferType
> supported_types
;
132 GpuMemoryBufferFactory::GetSupportedTypes(&supported_types
);
133 return supported_types
;
136 INSTANTIATE_TEST_CASE_P(
137 GpuMemoryBufferImplTests
,
138 GpuMemoryBufferImplTest
,
139 ::testing::ValuesIn(GetSupportedGpuMemoryBufferTypes()));
142 } // namespace content