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. 4 5 // Tests for the BufferTracker. 6 7 #include "gpu/command_buffer/client/buffer_tracker.h" 8 9 #include <GLES2/gl2ext.h> 10 #include "base/memory/scoped_ptr.h" 11 #include "gpu/command_buffer/client/client_test_helper.h" 12 #include "gpu/command_buffer/client/gles2_cmd_helper.h" 13 #include "gpu/command_buffer/client/mapped_memory.h" 14 #include "gpu/command_buffer/common/command_buffer.h" 15 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 18 namespace gpu { 19 namespace gles2 { 20 21 class MockClientCommandBufferImpl : public MockClientCommandBuffer { 22 public: 23 MockClientCommandBufferImpl() 24 : MockClientCommandBuffer(), 25 context_lost_(false) {} 26 virtual ~MockClientCommandBufferImpl() {} 27 28 virtual Buffer CreateTransferBuffer(size_t size, int32* id) OVERRIDE { 29 if (context_lost_) { 30 *id = -1; 31 return gpu::Buffer(); 32 } 33 return MockClientCommandBuffer::CreateTransferBuffer(size, id); 34 } 35 36 void set_context_lost(bool context_lost) { 37 context_lost_ = context_lost; 38 } 39 40 private: 41 bool context_lost_; 42 }; 43 44 class BufferTrackerTest : public testing::Test { 45 protected: 46 static const int32 kNumCommandEntries = 400; 47 static const int32 kCommandBufferSizeBytes = 48 kNumCommandEntries * sizeof(CommandBufferEntry); 49 50 virtual void SetUp() { 51 command_buffer_.reset(new MockClientCommandBufferImpl()); 52 helper_.reset(new GLES2CmdHelper(command_buffer_.get())); 53 helper_->Initialize(kCommandBufferSizeBytes); 54 mapped_memory_.reset(new MappedMemoryManager( 55 helper_.get(), MappedMemoryManager::kNoLimit)); 56 buffer_tracker_.reset(new BufferTracker(mapped_memory_.get())); 57 } 58 59 virtual void TearDown() { 60 buffer_tracker_.reset(); 61 mapped_memory_.reset(); 62 helper_.reset(); 63 command_buffer_.reset(); 64 } 65 66 scoped_ptr<MockClientCommandBufferImpl> command_buffer_; 67 scoped_ptr<GLES2CmdHelper> helper_; 68 scoped_ptr<MappedMemoryManager> mapped_memory_; 69 scoped_ptr<BufferTracker> buffer_tracker_; 70 }; 71 72 TEST_F(BufferTrackerTest, Basic) { 73 const GLuint kId1 = 123; 74 const GLuint kId2 = 124; 75 const GLsizeiptr size = 64; 76 77 // Check we can create a Buffer. 78 BufferTracker::Buffer* buffer = buffer_tracker_->CreateBuffer(kId1, size); 79 ASSERT_TRUE(buffer != NULL); 80 // Check we can get the same Buffer. 81 EXPECT_EQ(buffer, buffer_tracker_->GetBuffer(kId1)); 82 // Check mapped memory address. 83 EXPECT_TRUE(buffer->address() != NULL); 84 // Check shared memory was allocated. 85 EXPECT_EQ(1lu, mapped_memory_->num_chunks()); 86 // Check we get nothing for a non-existent buffer. 87 EXPECT_TRUE(buffer_tracker_->GetBuffer(kId2) == NULL); 88 // Check we can delete the buffer. 89 buffer_tracker_->RemoveBuffer(kId1); 90 // Check shared memory was freed. 91 mapped_memory_->FreeUnused(); 92 EXPECT_EQ(0lu, mapped_memory_->num_chunks()); 93 // Check we get nothing for a non-existent buffer. 94 EXPECT_TRUE(buffer_tracker_->GetBuffer(kId1) == NULL); 95 } 96 97 TEST_F(BufferTrackerTest, ZeroSize) { 98 const GLuint kId = 123; 99 100 // Check we can create a Buffer with zero size. 101 BufferTracker::Buffer* buffer = buffer_tracker_->CreateBuffer(kId, 0); 102 ASSERT_TRUE(buffer != NULL); 103 // Check mapped memory address. 104 EXPECT_TRUE(buffer->address() == NULL); 105 // Check no shared memory was allocated. 106 EXPECT_EQ(0lu, mapped_memory_->num_chunks()); 107 // Check we can delete the buffer. 108 buffer_tracker_->RemoveBuffer(kId); 109 } 110 111 TEST_F(BufferTrackerTest, LostContext) { 112 const GLuint kId = 123; 113 const GLsizeiptr size = 64; 114 115 command_buffer_->set_context_lost(true); 116 // Check we can create a Buffer when after losing context. 117 BufferTracker::Buffer* buffer = buffer_tracker_->CreateBuffer(kId, size); 118 ASSERT_TRUE(buffer != NULL); 119 // Check mapped memory address. 120 EXPECT_EQ(64u, buffer->size()); 121 // Check mapped memory address. 122 EXPECT_TRUE(buffer->address() == NULL); 123 // Check no shared memory was allocated. 124 EXPECT_EQ(0lu, mapped_memory_->num_chunks()); 125 // Check we can delete the buffer. 126 buffer_tracker_->RemoveBuffer(kId); 127 } 128 129 } // namespace gles2 130 } // namespace gpu 131