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"
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"
22 class MockClientCommandBufferImpl
: public MockClientCommandBuffer
{
24 MockClientCommandBufferImpl()
25 : MockClientCommandBuffer(),
26 context_lost_(false) {}
27 ~MockClientCommandBufferImpl() override
{}
29 scoped_refptr
<gpu::Buffer
> CreateTransferBuffer(size_t size
,
35 return MockClientCommandBuffer::CreateTransferBuffer(size
, id
);
38 void set_context_lost(bool context_lost
) {
39 context_lost_
= context_lost
;
46 class BufferTrackerTest
: public testing::Test
{
48 static const int32 kNumCommandEntries
= 400;
49 static const int32 kCommandBufferSizeBytes
=
50 kNumCommandEntries
* sizeof(CommandBufferEntry
);
52 void SetUp() override
{
53 command_buffer_
.reset(new MockClientCommandBufferImpl());
54 helper_
.reset(new GLES2CmdHelper(command_buffer_
.get()));
55 helper_
->Initialize(kCommandBufferSizeBytes
);
57 new MappedMemoryManager(helper_
.get(), MappedMemoryManager::kNoLimit
));
58 buffer_tracker_
.reset(new BufferTracker(mapped_memory_
.get()));
61 void TearDown() override
{
62 buffer_tracker_
.reset();
63 mapped_memory_
.reset();
65 command_buffer_
.reset();
68 scoped_ptr
<MockClientCommandBufferImpl
> command_buffer_
;
69 scoped_ptr
<GLES2CmdHelper
> helper_
;
70 scoped_ptr
<MappedMemoryManager
> mapped_memory_
;
71 scoped_ptr
<BufferTracker
> buffer_tracker_
;
74 TEST_F(BufferTrackerTest
, Basic
) {
75 const GLuint kId1
= 123;
76 const GLuint kId2
= 124;
77 const GLsizeiptr size
= 64;
79 // Check we can create a Buffer.
80 BufferTracker::Buffer
* buffer
= buffer_tracker_
->CreateBuffer(kId1
, size
);
81 ASSERT_TRUE(buffer
!= NULL
);
82 // Check we can get the same Buffer.
83 EXPECT_EQ(buffer
, buffer_tracker_
->GetBuffer(kId1
));
84 // Check mapped memory address.
85 EXPECT_TRUE(buffer
->address() != NULL
);
86 // Check shared memory was allocated.
87 EXPECT_EQ(1lu, mapped_memory_
->num_chunks());
88 // Check we get nothing for a non-existent buffer.
89 EXPECT_TRUE(buffer_tracker_
->GetBuffer(kId2
) == NULL
);
90 // Check we can delete the buffer.
91 buffer_tracker_
->RemoveBuffer(kId1
);
92 // Check shared memory was freed.
93 mapped_memory_
->FreeUnused();
94 EXPECT_EQ(0lu, mapped_memory_
->num_chunks());
95 // Check we get nothing for a non-existent buffer.
96 EXPECT_TRUE(buffer_tracker_
->GetBuffer(kId1
) == NULL
);
99 TEST_F(BufferTrackerTest
, ZeroSize
) {
100 const GLuint kId
= 123;
102 // Check we can create a Buffer with zero size.
103 BufferTracker::Buffer
* buffer
= buffer_tracker_
->CreateBuffer(kId
, 0);
104 ASSERT_TRUE(buffer
!= NULL
);
105 // Check mapped memory address.
106 EXPECT_TRUE(buffer
->address() == NULL
);
107 // Check no shared memory was allocated.
108 EXPECT_EQ(0lu, mapped_memory_
->num_chunks());
109 // Check we can delete the buffer.
110 buffer_tracker_
->RemoveBuffer(kId
);
113 TEST_F(BufferTrackerTest
, LostContext
) {
114 const GLuint kId
= 123;
115 const GLsizeiptr size
= 64;
117 command_buffer_
->set_context_lost(true);
118 // Check we can create a Buffer when after losing context.
119 BufferTracker::Buffer
* buffer
= buffer_tracker_
->CreateBuffer(kId
, size
);
120 ASSERT_TRUE(buffer
!= NULL
);
121 // Check mapped memory address.
122 EXPECT_EQ(64u, buffer
->size());
123 // Check mapped memory address.
124 EXPECT_TRUE(buffer
->address() == NULL
);
125 // Check no shared memory was allocated.
126 EXPECT_EQ(0lu, mapped_memory_
->num_chunks());
127 // Check we can delete the buffer.
128 buffer_tracker_
->RemoveBuffer(kId
);
131 TEST_F(BufferTrackerTest
, Unmanage
) {
132 const GLuint kId
= 123;
133 const GLsizeiptr size
= 64;
135 BufferTracker::Buffer
* buffer
= buffer_tracker_
->CreateBuffer(kId
, size
);
136 ASSERT_TRUE(buffer
!= NULL
);
137 EXPECT_EQ(mapped_memory_
->bytes_in_use(), static_cast<size_t>(size
));
139 void* mem
= buffer
->address();
140 buffer_tracker_
->Unmanage(buffer
);
141 buffer_tracker_
->RemoveBuffer(kId
);
142 EXPECT_EQ(mapped_memory_
->bytes_in_use(), static_cast<size_t>(size
));
144 mapped_memory_
->Free(mem
);
145 EXPECT_EQ(mapped_memory_
->bytes_in_use(), static_cast<size_t>(0));