This source file includes following definitions.
- context_lost_
- CreateTransferBuffer
- set_context_lost
- EmptyPoll
- SetUp
- TearDown
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "gpu/command_buffer/client/buffer_tracker.h"
#include <GLES2/gl2ext.h>
#include "base/memory/scoped_ptr.h"
#include "gpu/command_buffer/client/client_test_helper.h"
#include "gpu/command_buffer/client/gles2_cmd_helper.h"
#include "gpu/command_buffer/client/mapped_memory.h"
#include "gpu/command_buffer/common/command_buffer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gpu {
namespace gles2 {
class MockClientCommandBufferImpl : public MockClientCommandBuffer {
public:
MockClientCommandBufferImpl()
: MockClientCommandBuffer(),
context_lost_(false) {}
virtual ~MockClientCommandBufferImpl() {}
virtual scoped_refptr<gpu::Buffer> CreateTransferBuffer(size_t size,
int32* id) OVERRIDE {
if (context_lost_) {
*id = -1;
return NULL;
}
return MockClientCommandBuffer::CreateTransferBuffer(size, id);
}
void set_context_lost(bool context_lost) {
context_lost_ = context_lost;
}
private:
bool context_lost_;
};
namespace {
void EmptyPoll() {
}
}
class BufferTrackerTest : public testing::Test {
protected:
static const int32 kNumCommandEntries = 400;
static const int32 kCommandBufferSizeBytes =
kNumCommandEntries * sizeof(CommandBufferEntry);
virtual void SetUp() {
command_buffer_.reset(new MockClientCommandBufferImpl());
helper_.reset(new GLES2CmdHelper(command_buffer_.get()));
helper_->Initialize(kCommandBufferSizeBytes);
mapped_memory_.reset(new MappedMemoryManager(
helper_.get(), base::Bind(&EmptyPoll), MappedMemoryManager::kNoLimit));
buffer_tracker_.reset(new BufferTracker(mapped_memory_.get()));
}
virtual void TearDown() {
buffer_tracker_.reset();
mapped_memory_.reset();
helper_.reset();
command_buffer_.reset();
}
scoped_ptr<MockClientCommandBufferImpl> command_buffer_;
scoped_ptr<GLES2CmdHelper> helper_;
scoped_ptr<MappedMemoryManager> mapped_memory_;
scoped_ptr<BufferTracker> buffer_tracker_;
};
TEST_F(BufferTrackerTest, Basic) {
const GLuint kId1 = 123;
const GLuint kId2 = 124;
const GLsizeiptr size = 64;
BufferTracker::Buffer* buffer = buffer_tracker_->CreateBuffer(kId1, size);
ASSERT_TRUE(buffer != NULL);
EXPECT_EQ(buffer, buffer_tracker_->GetBuffer(kId1));
EXPECT_TRUE(buffer->address() != NULL);
EXPECT_EQ(1lu, mapped_memory_->num_chunks());
EXPECT_TRUE(buffer_tracker_->GetBuffer(kId2) == NULL);
buffer_tracker_->RemoveBuffer(kId1);
mapped_memory_->FreeUnused();
EXPECT_EQ(0lu, mapped_memory_->num_chunks());
EXPECT_TRUE(buffer_tracker_->GetBuffer(kId1) == NULL);
}
TEST_F(BufferTrackerTest, ZeroSize) {
const GLuint kId = 123;
BufferTracker::Buffer* buffer = buffer_tracker_->CreateBuffer(kId, 0);
ASSERT_TRUE(buffer != NULL);
EXPECT_TRUE(buffer->address() == NULL);
EXPECT_EQ(0lu, mapped_memory_->num_chunks());
buffer_tracker_->RemoveBuffer(kId);
}
TEST_F(BufferTrackerTest, LostContext) {
const GLuint kId = 123;
const GLsizeiptr size = 64;
command_buffer_->set_context_lost(true);
BufferTracker::Buffer* buffer = buffer_tracker_->CreateBuffer(kId, size);
ASSERT_TRUE(buffer != NULL);
EXPECT_EQ(64u, buffer->size());
EXPECT_TRUE(buffer->address() == NULL);
EXPECT_EQ(0lu, mapped_memory_->num_chunks());
buffer_tracker_->RemoveBuffer(kId);
}
TEST_F(BufferTrackerTest, Unmanage) {
const GLuint kId = 123;
const GLsizeiptr size = 64;
BufferTracker::Buffer* buffer = buffer_tracker_->CreateBuffer(kId, size);
ASSERT_TRUE(buffer != NULL);
EXPECT_EQ(mapped_memory_->bytes_in_use(), static_cast<size_t>(size));
void* mem = buffer->address();
buffer_tracker_->Unmanage(buffer);
buffer_tracker_->RemoveBuffer(kId);
EXPECT_EQ(mapped_memory_->bytes_in_use(), static_cast<size_t>(size));
mapped_memory_->Free(mem);
EXPECT_EQ(mapped_memory_->bytes_in_use(), static_cast<size_t>(0));
}
}
}