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
MockCommandBufferBase()18 MockCommandBufferBase::MockCommandBufferBase() {
19 }
20
~MockCommandBufferBase()21 MockCommandBufferBase::~MockCommandBufferBase() {
22 }
23
Initialize()24 bool MockCommandBufferBase::Initialize() {
25 return true;
26 }
27
GetLastState()28 CommandBuffer::State MockCommandBufferBase::GetLastState() {
29 return state_;
30 }
31
GetLastToken()32 int32 MockCommandBufferBase::GetLastToken() {
33 return state_.token;
34 }
35
SetGetOffset(int32 get_offset)36 void MockCommandBufferBase::SetGetOffset(int32 get_offset) {
37 state_.get_offset = get_offset;
38 }
39
WaitForTokenInRange(int32 start,int32 end)40 void MockCommandBufferBase::WaitForTokenInRange(int32 start, int32 end) {}
41
WaitForGetOffsetInRange(int32 start,int32 end)42 void MockCommandBufferBase::WaitForGetOffsetInRange(int32 start, int32 end) {
43 state_.get_offset = state_.put_offset;
44 OnFlush();
45 }
46
SetGetBuffer(int transfer_buffer_id)47 void 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.
GetNextFreeTransferBufferId()57 int32 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
CreateTransferBuffer(size_t size,int32 * id)66 scoped_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
DestroyTransferBufferHelper(int32 id)80 void 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
GetTransferBuffer(int32 id)87 scoped_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
FlushHelper(int32 put_offset)93 void MockCommandBufferBase::FlushHelper(int32 put_offset) {
94 state_.put_offset = put_offset;
95 }
96
SetToken(int32 token)97 void MockCommandBufferBase::SetToken(int32 token) {
98 NOTREACHED();
99 state_.token = token;
100 }
101
SetParseError(error::Error error)102 void MockCommandBufferBase::SetParseError(error::Error error) {
103 NOTREACHED();
104 state_.error = error;
105 }
106
SetContextLostReason(error::ContextLostReason reason)107 void 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
115 const int32 MockCommandBufferBase::kTransferBufferBaseId;
116 const int32 MockCommandBufferBase::kMaxTransferBuffers;
117 #endif
118
MockClientCommandBuffer()119 MockClientCommandBuffer::MockClientCommandBuffer() {
120 DelegateToFake();
121 }
122
~MockClientCommandBuffer()123 MockClientCommandBuffer::~MockClientCommandBuffer() {
124 }
125
Flush(int32 put_offset)126 void MockClientCommandBuffer::Flush(int32 put_offset) {
127 FlushHelper(put_offset);
128 }
129
DelegateToFake()130 void MockClientCommandBuffer::DelegateToFake() {
131 ON_CALL(*this, DestroyTransferBuffer(_))
132 .WillByDefault(Invoke(
133 this, &MockCommandBufferBase::DestroyTransferBufferHelper));
134 }
135
MockClientCommandBufferMockFlush()136 MockClientCommandBufferMockFlush::MockClientCommandBufferMockFlush() {
137 DelegateToFake();
138 }
139
~MockClientCommandBufferMockFlush()140 MockClientCommandBufferMockFlush::~MockClientCommandBufferMockFlush() {
141 }
142
DelegateToFake()143 void MockClientCommandBufferMockFlush::DelegateToFake() {
144 MockClientCommandBuffer::DelegateToFake();
145 ON_CALL(*this, Flush(_))
146 .WillByDefault(Invoke(
147 this, &MockCommandBufferBase::FlushHelper));
148 }
149
MockClientGpuControl()150 MockClientGpuControl::MockClientGpuControl() {
151 }
152
~MockClientGpuControl()153 MockClientGpuControl::~MockClientGpuControl() {
154 }
155
156 } // namespace gpu
157
158
159