Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / gpu / command_buffer / client / buffer_tracker_unittest.cc
blobe8d4d8183081db9aed8b131db134a826a1c02c3b
1 // Copyright (c) 2012 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 // Tests for the BufferTracker.
7 #include "gpu/command_buffer/client/buffer_tracker.h"
9 #include <GLES2/gl2.h>
10 #include <GLES2/gl2ext.h>
11 #include "base/memory/scoped_ptr.h"
12 #include "gpu/command_buffer/client/client_test_helper.h"
13 #include "gpu/command_buffer/client/gles2_cmd_helper.h"
14 #include "gpu/command_buffer/client/mapped_memory.h"
15 #include "gpu/command_buffer/common/command_buffer.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace gpu {
20 namespace gles2 {
22 class MockClientCommandBufferImpl : public MockClientCommandBuffer {
23 public:
24 MockClientCommandBufferImpl()
25 : MockClientCommandBuffer(),
26 context_lost_(false) {}
27 ~MockClientCommandBufferImpl() override {}
29 scoped_refptr<gpu::Buffer> CreateTransferBuffer(size_t size,
30 int32* id) override {
31 if (context_lost_) {
32 *id = -1;
33 return NULL;
35 return MockClientCommandBuffer::CreateTransferBuffer(size, id);
38 void set_context_lost(bool context_lost) {
39 context_lost_ = context_lost;
42 private:
43 bool context_lost_;
46 namespace {
47 void EmptyPoll() {
51 class BufferTrackerTest : public testing::Test {
52 protected:
53 static const int32 kNumCommandEntries = 400;
54 static const int32 kCommandBufferSizeBytes =
55 kNumCommandEntries * sizeof(CommandBufferEntry);
57 void SetUp() override {
58 command_buffer_.reset(new MockClientCommandBufferImpl());
59 helper_.reset(new GLES2CmdHelper(command_buffer_.get()));
60 helper_->Initialize(kCommandBufferSizeBytes);
61 mapped_memory_.reset(new MappedMemoryManager(
62 helper_.get(), base::Bind(&EmptyPoll), MappedMemoryManager::kNoLimit));
63 buffer_tracker_.reset(new BufferTracker(mapped_memory_.get()));
66 void TearDown() override {
67 buffer_tracker_.reset();
68 mapped_memory_.reset();
69 helper_.reset();
70 command_buffer_.reset();
73 scoped_ptr<MockClientCommandBufferImpl> command_buffer_;
74 scoped_ptr<GLES2CmdHelper> helper_;
75 scoped_ptr<MappedMemoryManager> mapped_memory_;
76 scoped_ptr<BufferTracker> buffer_tracker_;
79 TEST_F(BufferTrackerTest, Basic) {
80 const GLuint kId1 = 123;
81 const GLuint kId2 = 124;
82 const GLsizeiptr size = 64;
84 // Check we can create a Buffer.
85 BufferTracker::Buffer* buffer = buffer_tracker_->CreateBuffer(kId1, size);
86 ASSERT_TRUE(buffer != NULL);
87 // Check we can get the same Buffer.
88 EXPECT_EQ(buffer, buffer_tracker_->GetBuffer(kId1));
89 // Check mapped memory address.
90 EXPECT_TRUE(buffer->address() != NULL);
91 // Check shared memory was allocated.
92 EXPECT_EQ(1lu, mapped_memory_->num_chunks());
93 // Check we get nothing for a non-existent buffer.
94 EXPECT_TRUE(buffer_tracker_->GetBuffer(kId2) == NULL);
95 // Check we can delete the buffer.
96 buffer_tracker_->RemoveBuffer(kId1);
97 // Check shared memory was freed.
98 mapped_memory_->FreeUnused();
99 EXPECT_EQ(0lu, mapped_memory_->num_chunks());
100 // Check we get nothing for a non-existent buffer.
101 EXPECT_TRUE(buffer_tracker_->GetBuffer(kId1) == NULL);
104 TEST_F(BufferTrackerTest, ZeroSize) {
105 const GLuint kId = 123;
107 // Check we can create a Buffer with zero size.
108 BufferTracker::Buffer* buffer = buffer_tracker_->CreateBuffer(kId, 0);
109 ASSERT_TRUE(buffer != NULL);
110 // Check mapped memory address.
111 EXPECT_TRUE(buffer->address() == NULL);
112 // Check no shared memory was allocated.
113 EXPECT_EQ(0lu, mapped_memory_->num_chunks());
114 // Check we can delete the buffer.
115 buffer_tracker_->RemoveBuffer(kId);
118 TEST_F(BufferTrackerTest, LostContext) {
119 const GLuint kId = 123;
120 const GLsizeiptr size = 64;
122 command_buffer_->set_context_lost(true);
123 // Check we can create a Buffer when after losing context.
124 BufferTracker::Buffer* buffer = buffer_tracker_->CreateBuffer(kId, size);
125 ASSERT_TRUE(buffer != NULL);
126 // Check mapped memory address.
127 EXPECT_EQ(64u, buffer->size());
128 // Check mapped memory address.
129 EXPECT_TRUE(buffer->address() == NULL);
130 // Check no shared memory was allocated.
131 EXPECT_EQ(0lu, mapped_memory_->num_chunks());
132 // Check we can delete the buffer.
133 buffer_tracker_->RemoveBuffer(kId);
136 TEST_F(BufferTrackerTest, Unmanage) {
137 const GLuint kId = 123;
138 const GLsizeiptr size = 64;
140 BufferTracker::Buffer* buffer = buffer_tracker_->CreateBuffer(kId, size);
141 ASSERT_TRUE(buffer != NULL);
142 EXPECT_EQ(mapped_memory_->bytes_in_use(), static_cast<size_t>(size));
144 void* mem = buffer->address();
145 buffer_tracker_->Unmanage(buffer);
146 buffer_tracker_->RemoveBuffer(kId);
147 EXPECT_EQ(mapped_memory_->bytes_in_use(), static_cast<size_t>(size));
149 mapped_memory_->Free(mem);
150 EXPECT_EQ(mapped_memory_->bytes_in_use(), static_cast<size_t>(0));
153 } // namespace gles2
154 } // namespace gpu