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 GLES2Implementation.
      6 
      7 #include "gpu/command_buffer/client/client_test_helper.h"
      8 
      9 #include "gpu/command_buffer/common/command_buffer.h"
     10 #include "gpu/command_buffer/client/cmd_buffer_helper.h"
     11 #include "testing/gmock/include/gmock/gmock.h"
     12 
     13 using ::testing::_;
     14 using ::testing::Invoke;
     15 
     16 namespace gpu {
     17 
     18 MockCommandBufferBase::~MockCommandBufferBase() {
     19 }
     20 
     21 bool MockCommandBufferBase::Initialize() {
     22   return true;
     23 }
     24 
     25 CommandBuffer::State MockCommandBufferBase::GetState() {
     26   return state_;
     27 }
     28 
     29 CommandBuffer::State MockCommandBufferBase::GetLastState() {
     30   return state_;
     31 }
     32 
     33 int32 MockCommandBufferBase::GetLastToken() {
     34   return state_.token;
     35 }
     36 
     37 void MockCommandBufferBase::SetGetOffset(int32 get_offset) {
     38   state_.get_offset = get_offset;
     39 }
     40 
     41 CommandBuffer::State MockCommandBufferBase::FlushSync(
     42     int32 put_offset, int32 last_known_get) {
     43   state_.put_offset = put_offset;
     44   state_.get_offset = put_offset;
     45   OnFlush();
     46   return state_;
     47 }
     48 
     49 void MockCommandBufferBase::SetGetBuffer(int transfer_buffer_id) {
     50   ring_buffer_buffer_ = GetTransferBuffer(transfer_buffer_id);
     51   ring_buffer_ = static_cast<CommandBufferEntry*>(ring_buffer_buffer_.ptr);
     52   state_.num_entries = ring_buffer_buffer_.size / sizeof(ring_buffer_[0]);
     53   state_.token = 10000;  // All token checks in the tests should pass.
     54 }
     55 
     56 // Get's the Id of the next transfer buffer that will be returned
     57 // by CreateTransferBuffer. This is useful for testing expected ids.
     58 int32 MockCommandBufferBase::GetNextFreeTransferBufferId() {
     59   for (size_t ii = 0; ii < arraysize(transfer_buffers_); ++ii) {
     60     if (!transfer_buffers_[ii].get()) {
     61       return kTransferBufferBaseId + ii;
     62     }
     63   }
     64   return -1;
     65 }
     66 
     67 Buffer MockCommandBufferBase::CreateTransferBuffer(size_t size, int32* id) {
     68   *id = GetNextFreeTransferBufferId();
     69   if (*id >= 0) {
     70     int32 ndx = *id - kTransferBufferBaseId;
     71     transfer_buffers_[ndx].reset(new int8[size]);
     72     transfer_buffer_buffers_[ndx].ptr = transfer_buffers_[ndx].get();
     73     transfer_buffer_buffers_[ndx].size = size;
     74   }
     75   return GetTransferBuffer(*id);
     76 }
     77 
     78 void MockCommandBufferBase::DestroyTransferBufferHelper(int32 id) {
     79   DCHECK_GE(id, kTransferBufferBaseId);
     80   DCHECK_LT(id, kTransferBufferBaseId + kMaxTransferBuffers);
     81   id -= kTransferBufferBaseId;
     82   transfer_buffers_[id].reset();
     83   transfer_buffer_buffers_[id] = Buffer();
     84 }
     85 
     86 Buffer MockCommandBufferBase::GetTransferBuffer(int32 id) {
     87   DCHECK_GE(id, kTransferBufferBaseId);
     88   DCHECK_LT(id, kTransferBufferBaseId + kMaxTransferBuffers);
     89   return transfer_buffer_buffers_[id - kTransferBufferBaseId];
     90 }
     91 
     92 void MockCommandBufferBase::FlushHelper(int32 put_offset) {
     93   state_.put_offset = put_offset;
     94 }
     95 
     96 void MockCommandBufferBase::SetToken(int32 token) {
     97   NOTREACHED();
     98   state_.token = token;
     99 }
    100 
    101 void MockCommandBufferBase::SetParseError(error::Error error) {
    102   NOTREACHED();
    103   state_.error = error;
    104 }
    105 
    106 void MockCommandBufferBase::SetContextLostReason(
    107     error::ContextLostReason reason) {
    108   NOTREACHED();
    109   state_.context_lost_reason = reason;
    110 }
    111 
    112 // GCC requires these declarations, but MSVC requires they not be present
    113 #ifndef _MSC_VER
    114 const int32 MockCommandBufferBase::kTransferBufferBaseId;
    115 const int32 MockCommandBufferBase::kMaxTransferBuffers;
    116 #endif
    117 
    118 MockClientCommandBuffer::MockClientCommandBuffer() {
    119   DelegateToFake();
    120 }
    121 
    122 MockClientCommandBuffer::~MockClientCommandBuffer() {
    123 }
    124 
    125 void MockClientCommandBuffer::Flush(int32 put_offset) {
    126   FlushHelper(put_offset);
    127 }
    128 
    129 void MockClientCommandBuffer::DelegateToFake() {
    130   ON_CALL(*this, DestroyTransferBuffer(_))
    131       .WillByDefault(Invoke(
    132           this, &MockCommandBufferBase::DestroyTransferBufferHelper));
    133 }
    134 
    135 MockClientCommandBufferMockFlush::MockClientCommandBufferMockFlush() {
    136   DelegateToFake();
    137 }
    138 
    139 MockClientCommandBufferMockFlush::~MockClientCommandBufferMockFlush() {
    140 }
    141 
    142 void MockClientCommandBufferMockFlush::DelegateToFake() {
    143   MockClientCommandBuffer::DelegateToFake();
    144   ON_CALL(*this, Flush(_))
    145       .WillByDefault(Invoke(
    146           this, &MockCommandBufferBase::FlushHelper));
    147 }
    148 
    149 MockClientGpuControl::MockClientGpuControl() {
    150 }
    151 
    152 MockClientGpuControl::~MockClientGpuControl() {
    153 }
    154 
    155 }  // namespace gpu
    156 
    157 
    158