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