Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl_unittest.cc
blob3a4789f2e18218f3d042b36e801b0ddd428d2abd
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"
7 #include "base/bind.h"
8 #include "content/common/gpu/gpu_memory_buffer_factory.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace content {
12 namespace {
14 const int kClientId = 1;
16 class GpuMemoryBufferImplTest
17 : public testing::TestWithParam<gfx::GpuMemoryBufferType> {
18 public:
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) {
34 ++buffer_count_;
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);
42 --buffer_count_;
45 std::vector<GpuMemoryBufferFactory::Configuration> supported_configurations_;
46 int buffer_count_;
48 private:
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,
61 buffer_size,
62 configuration.format,
63 configuration.usage),
64 buffer_size,
65 configuration.format,
66 base::Bind(&GpuMemoryBufferImplTest::DestroyGpuMemoryBuffer,
67 base::Unretained(this),
68 kBufferId)));
69 EXPECT_EQ(1, buffer_count_);
70 ASSERT_TRUE(buffer);
71 EXPECT_EQ(buffer->GetFormat(), configuration.format);
73 // Check if destruction callback is executed when deleting the buffer.
74 buffer.reset();
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)
86 continue;
88 scoped_ptr<GpuMemoryBufferImpl> buffer(
89 GpuMemoryBufferImpl::CreateFromHandle(
90 CreateGpuMemoryBuffer(kBufferId, buffer_size, configuration.format,
91 configuration.usage),
92 buffer_size, configuration.format,
93 base::Bind(&GpuMemoryBufferImplTest::DestroyGpuMemoryBuffer,
94 base::Unretained(this), kBufferId)));
95 ASSERT_TRUE(buffer);
96 EXPECT_FALSE(buffer->IsMapped());
98 size_t num_planes =
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());
105 ASSERT_TRUE(rv);
106 EXPECT_TRUE(buffer->IsMapped());
108 // Get strides.
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);
125 buffer->Unmap();
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()));
141 } // namespace
142 } // namespace content