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(1, 1);
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(1, 1);
84 for (auto configuration
: supported_configurations_
) {
85 if (configuration
.usage
!= gfx::GpuMemoryBuffer::MAP
)
88 size_t width_in_bytes
= 0;
89 EXPECT_TRUE(GpuMemoryBufferImpl::StrideInBytes(
90 buffer_size
.width(), configuration
.format
, &width_in_bytes
));
91 EXPECT_GT(width_in_bytes
, 0u);
92 scoped_ptr
<char[]> data(new char[width_in_bytes
]);
93 memset(data
.get(), 0x2a, width_in_bytes
);
95 scoped_ptr
<GpuMemoryBufferImpl
> buffer(
96 GpuMemoryBufferImpl::CreateFromHandle(
97 CreateGpuMemoryBuffer(kBufferId
,
100 configuration
.usage
),
102 configuration
.format
,
103 base::Bind(&GpuMemoryBufferImplTest::DestroyGpuMemoryBuffer
,
104 base::Unretained(this),
107 EXPECT_FALSE(buffer
->IsMapped());
110 bool rv
= buffer
->Map(&memory
);
112 EXPECT_TRUE(buffer
->IsMapped());
114 buffer
->GetStride(&stride
);
115 EXPECT_GE(stride
, width_in_bytes
);
116 memcpy(memory
, data
.get(), width_in_bytes
);
117 EXPECT_EQ(memcmp(memory
, data
.get(), width_in_bytes
), 0);
119 EXPECT_FALSE(buffer
->IsMapped());
123 std::vector
<gfx::GpuMemoryBufferType
> GetSupportedGpuMemoryBufferTypes() {
124 std::vector
<gfx::GpuMemoryBufferType
> supported_types
;
125 GpuMemoryBufferFactory::GetSupportedTypes(&supported_types
);
126 return supported_types
;
129 INSTANTIATE_TEST_CASE_P(
130 GpuMemoryBufferImplTests
,
131 GpuMemoryBufferImplTest
,
132 ::testing::ValuesIn(GetSupportedGpuMemoryBufferTypes()));
135 } // namespace content