OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/gpu/client/gpu_memory_buffer_impl.h" | 5 #include "content/common/gpu/client/gpu_memory_buffer_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "content/common/gpu/gpu_memory_buffer_factory.h" | 8 #include "content/common/gpu/gpu_memory_buffer_factory.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 std::vector<GpuMemoryBufferFactory::Configuration> supported_configurations_; | 45 std::vector<GpuMemoryBufferFactory::Configuration> supported_configurations_; |
46 int buffer_count_; | 46 int buffer_count_; |
47 | 47 |
48 private: | 48 private: |
49 scoped_ptr<GpuMemoryBufferFactory> factory_; | 49 scoped_ptr<GpuMemoryBufferFactory> factory_; |
50 }; | 50 }; |
51 | 51 |
52 TEST_P(GpuMemoryBufferImplTest, CreateFromHandle) { | 52 TEST_P(GpuMemoryBufferImplTest, CreateFromHandle) { |
53 const int kBufferId = 1; | 53 const int kBufferId = 1; |
54 | 54 |
55 gfx::Size buffer_size(1, 1); | 55 gfx::Size buffer_size(8, 8); |
56 | 56 |
57 for (auto configuration : supported_configurations_) { | 57 for (auto configuration : supported_configurations_) { |
58 scoped_ptr<GpuMemoryBufferImpl> buffer( | 58 scoped_ptr<GpuMemoryBufferImpl> buffer( |
59 GpuMemoryBufferImpl::CreateFromHandle( | 59 GpuMemoryBufferImpl::CreateFromHandle( |
60 CreateGpuMemoryBuffer(kBufferId, | 60 CreateGpuMemoryBuffer(kBufferId, |
61 buffer_size, | 61 buffer_size, |
62 configuration.format, | 62 configuration.format, |
63 configuration.usage), | 63 configuration.usage), |
64 buffer_size, | 64 buffer_size, |
65 configuration.format, | 65 configuration.format, |
66 base::Bind(&GpuMemoryBufferImplTest::DestroyGpuMemoryBuffer, | 66 base::Bind(&GpuMemoryBufferImplTest::DestroyGpuMemoryBuffer, |
67 base::Unretained(this), | 67 base::Unretained(this), |
68 kBufferId))); | 68 kBufferId))); |
69 EXPECT_EQ(1, buffer_count_); | 69 EXPECT_EQ(1, buffer_count_); |
70 ASSERT_TRUE(buffer); | 70 ASSERT_TRUE(buffer); |
71 EXPECT_EQ(buffer->GetFormat(), configuration.format); | 71 EXPECT_EQ(buffer->GetFormat(), configuration.format); |
72 | 72 |
73 // Check if destruction callback is executed when deleting the buffer. | 73 // Check if destruction callback is executed when deleting the buffer. |
74 buffer.reset(); | 74 buffer.reset(); |
75 EXPECT_EQ(0, buffer_count_); | 75 EXPECT_EQ(0, buffer_count_); |
76 } | 76 } |
77 } | 77 } |
78 | 78 |
79 TEST_P(GpuMemoryBufferImplTest, Map) { | 79 TEST_P(GpuMemoryBufferImplTest, Map) { |
80 const int kBufferId = 1; | 80 const int kBufferId = 1; |
81 | 81 |
82 gfx::Size buffer_size(1, 1); | 82 gfx::Size buffer_size(2, 2); |
83 | 83 |
84 for (auto configuration : supported_configurations_) { | 84 for (auto configuration : supported_configurations_) { |
85 if (configuration.usage != gfx::GpuMemoryBuffer::MAP) | 85 if (configuration.usage != gfx::GpuMemoryBuffer::MAP) |
86 continue; | 86 continue; |
87 | 87 |
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); | |
94 | |
95 scoped_ptr<GpuMemoryBufferImpl> buffer( | 88 scoped_ptr<GpuMemoryBufferImpl> buffer( |
96 GpuMemoryBufferImpl::CreateFromHandle( | 89 GpuMemoryBufferImpl::CreateFromHandle( |
97 CreateGpuMemoryBuffer(kBufferId, | 90 CreateGpuMemoryBuffer(kBufferId, buffer_size, configuration.format, |
98 buffer_size, | |
99 configuration.format, | |
100 configuration.usage), | 91 configuration.usage), |
101 buffer_size, | 92 buffer_size, configuration.format, |
102 configuration.format, | |
103 base::Bind(&GpuMemoryBufferImplTest::DestroyGpuMemoryBuffer, | 93 base::Bind(&GpuMemoryBufferImplTest::DestroyGpuMemoryBuffer, |
104 base::Unretained(this), | 94 base::Unretained(this), kBufferId))); |
105 kBufferId))); | |
106 ASSERT_TRUE(buffer); | 95 ASSERT_TRUE(buffer); |
107 EXPECT_FALSE(buffer->IsMapped()); | 96 EXPECT_FALSE(buffer->IsMapped()); |
108 | 97 |
109 void* memory; | 98 size_t num_planes = |
110 bool rv = buffer->Map(&memory); | 99 GpuMemoryBufferImpl::NumberOfPlanesForGpuMemoryBufferFormat( |
| 100 configuration.format); |
| 101 |
| 102 // Map buffer into user space. |
| 103 scoped_ptr<void*[]> mapped_buffers(new void*[num_planes]); |
| 104 bool rv = buffer->Map(mapped_buffers.get()); |
111 ASSERT_TRUE(rv); | 105 ASSERT_TRUE(rv); |
112 EXPECT_TRUE(buffer->IsMapped()); | 106 EXPECT_TRUE(buffer->IsMapped()); |
113 uint32 stride; | 107 |
114 buffer->GetStride(&stride); | 108 // Get strides. |
115 EXPECT_GE(stride, width_in_bytes); | 109 scoped_ptr<uint32[]> strides(new uint32[num_planes]); |
116 memcpy(memory, data.get(), width_in_bytes); | 110 buffer->GetStride(strides.get()); |
117 EXPECT_EQ(memcmp(memory, data.get(), width_in_bytes), 0); | 111 |
| 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); |
| 118 EXPECT_GE(strides[i], width_in_bytes); |
| 119 |
| 120 scoped_ptr<char[]> data(new char[width_in_bytes]); |
| 121 memset(data.get(), 0x2a + i, width_in_bytes); |
| 122 memcpy(mapped_buffers[i], data.get(), width_in_bytes); |
| 123 EXPECT_EQ(memcmp(mapped_buffers[i], data.get(), width_in_bytes), 0); |
| 124 } |
| 125 |
118 buffer->Unmap(); | 126 buffer->Unmap(); |
119 EXPECT_FALSE(buffer->IsMapped()); | 127 EXPECT_FALSE(buffer->IsMapped()); |
120 } | 128 } |
121 } | 129 } |
122 | 130 |
123 std::vector<gfx::GpuMemoryBufferType> GetSupportedGpuMemoryBufferTypes() { | 131 std::vector<gfx::GpuMemoryBufferType> GetSupportedGpuMemoryBufferTypes() { |
124 std::vector<gfx::GpuMemoryBufferType> supported_types; | 132 std::vector<gfx::GpuMemoryBufferType> supported_types; |
125 GpuMemoryBufferFactory::GetSupportedTypes(&supported_types); | 133 GpuMemoryBufferFactory::GetSupportedTypes(&supported_types); |
126 return supported_types; | 134 return supported_types; |
127 } | 135 } |
128 | 136 |
129 INSTANTIATE_TEST_CASE_P( | 137 INSTANTIATE_TEST_CASE_P( |
130 GpuMemoryBufferImplTests, | 138 GpuMemoryBufferImplTests, |
131 GpuMemoryBufferImplTest, | 139 GpuMemoryBufferImplTest, |
132 ::testing::ValuesIn(GetSupportedGpuMemoryBufferTypes())); | 140 ::testing::ValuesIn(GetSupportedGpuMemoryBufferTypes())); |
133 | 141 |
134 } // namespace | 142 } // namespace |
135 } // namespace content | 143 } // namespace content |
OLD | NEW |