GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl_unittest.cc
blobba55d78349e7b44111439920cad1f98c682d4797
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(1, 1);
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(1, 1);
84 for (auto configuration : supported_configurations_) {
85 if (configuration.usage != gfx::GpuMemoryBuffer::MAP)
86 continue;
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,
98 buffer_size,
99 configuration.format,
100 configuration.usage),
101 buffer_size,
102 configuration.format,
103 base::Bind(&GpuMemoryBufferImplTest::DestroyGpuMemoryBuffer,
104 base::Unretained(this),
105 kBufferId)));
106 ASSERT_TRUE(buffer);
107 EXPECT_FALSE(buffer->IsMapped());
109 void* memory;
110 bool rv = buffer->Map(&memory);
111 ASSERT_TRUE(rv);
112 EXPECT_TRUE(buffer->IsMapped());
113 uint32 stride;
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);
118 buffer->Unmap();
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()));
134 } // namespace
135 } // namespace content