Home | History | Annotate | Download | only in client
      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(helper_.get()));
     55     buffer_tracker_.reset(new BufferTracker(mapped_memory_.get()));
     56   }
     57 
     58   virtual void TearDown() {
     59     buffer_tracker_.reset();
     60     mapped_memory_.reset();
     61     helper_.reset();
     62     command_buffer_.reset();
     63   }
     64 
     65   scoped_ptr<MockClientCommandBufferImpl> command_buffer_;
     66   scoped_ptr<GLES2CmdHelper> helper_;
     67   scoped_ptr<MappedMemoryManager> mapped_memory_;
     68   scoped_ptr<BufferTracker> buffer_tracker_;
     69 };
     70 
     71 TEST_F(BufferTrackerTest, Basic) {
     72   const GLuint kId1 = 123;
     73   const GLuint kId2 = 124;
     74   const GLsizeiptr size = 64;
     75 
     76   // Check we can create a Buffer.
     77   BufferTracker::Buffer* buffer = buffer_tracker_->CreateBuffer(kId1, size);
     78   ASSERT_TRUE(buffer != NULL);
     79   // Check we can get the same Buffer.
     80   EXPECT_EQ(buffer, buffer_tracker_->GetBuffer(kId1));
     81   // Check mapped memory address.
     82   EXPECT_TRUE(buffer->address() != NULL);
     83   // Check shared memory was allocated.
     84   EXPECT_EQ(1lu, mapped_memory_->num_chunks());
     85   // Check we get nothing for a non-existent buffer.
     86   EXPECT_TRUE(buffer_tracker_->GetBuffer(kId2) == NULL);
     87   // Check we can delete the buffer.
     88   buffer_tracker_->RemoveBuffer(kId1);
     89   // Check shared memory was freed.
     90   mapped_memory_->FreeUnused();
     91   EXPECT_EQ(0lu, mapped_memory_->num_chunks());
     92   // Check we get nothing for a non-existent buffer.
     93   EXPECT_TRUE(buffer_tracker_->GetBuffer(kId1) == NULL);
     94 }
     95 
     96 TEST_F(BufferTrackerTest, ZeroSize) {
     97   const GLuint kId = 123;
     98 
     99   // Check we can create a Buffer with zero size.
    100   BufferTracker::Buffer* buffer = buffer_tracker_->CreateBuffer(kId, 0);
    101   ASSERT_TRUE(buffer != NULL);
    102   // Check mapped memory address.
    103   EXPECT_TRUE(buffer->address() == NULL);
    104   // Check no shared memory was allocated.
    105   EXPECT_EQ(0lu, mapped_memory_->num_chunks());
    106   // Check we can delete the buffer.
    107   buffer_tracker_->RemoveBuffer(kId);
    108 }
    109 
    110 TEST_F(BufferTrackerTest, LostContext) {
    111   const GLuint kId = 123;
    112   const GLsizeiptr size = 64;
    113 
    114   command_buffer_->set_context_lost(true);
    115   // Check we can create a Buffer when after losing context.
    116   BufferTracker::Buffer* buffer = buffer_tracker_->CreateBuffer(kId, size);
    117   ASSERT_TRUE(buffer != NULL);
    118   // Check mapped memory address.
    119   EXPECT_EQ(64u, buffer->size());
    120   // Check mapped memory address.
    121   EXPECT_TRUE(buffer->address() == NULL);
    122   // Check no shared memory was allocated.
    123   EXPECT_EQ(0lu, mapped_memory_->num_chunks());
    124   // Check we can delete the buffer.
    125   buffer_tracker_->RemoveBuffer(kId);
    126 }
    127 
    128 }  // namespace gles2
    129 }  // namespace gpu
    130