1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
gpu / command_buffer / client / mock_transfer_buffer.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "gpu/command_buffer/client/mock_transfer_buffer.h"
#include "base/bits.h"
#include "gpu/command_buffer/common/command_buffer.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gpu {
MockTransferBuffer::MockTransferBuffer(CommandBuffer* command_buffer,
unsigned int size,
unsigned int result_size,
unsigned int alignment,
bool initialize_fail)
: command_buffer_(command_buffer),
size_(size),
result_size_(result_size),
alignment_(alignment),
actual_buffer_index_(0),
expected_buffer_index_(0),
last_alloc_(nullptr),
expected_offset_(result_size),
actual_offset_(result_size),
initialize_fail_(initialize_fail) {
// We have to allocate the buffers here because
// we need to know their address before
// {Raster,GLES2}Implementation::Initialize is called.
for (int ii = 0; ii < kNumBuffers; ++ii) {
buffers_[ii] = command_buffer_->CreateTransferBuffer(
size_ + ii * alignment_, &buffer_ids_[ii]);
EXPECT_NE(-1, buffer_ids_[ii]);
}
}
MockTransferBuffer::~MockTransferBuffer() = default;
base::UnguessableToken MockTransferBuffer::shared_memory_guid() const {
return base::UnguessableToken();
}
bool MockTransferBuffer::Initialize(unsigned int starting_buffer_size,
unsigned int result_size,
unsigned int /* min_buffer_size */,
unsigned int /* max_buffer_size */,
unsigned int alignment) {
// Just check they match.
return size_ == starting_buffer_size && result_size_ == result_size &&
alignment_ == alignment && !initialize_fail_;
}
int MockTransferBuffer::GetShmId() {
return buffer_ids_[actual_buffer_index_];
}
void* MockTransferBuffer::AcquireResultBuffer() {
EXPECT_FALSE(outstanding_result_pointer_);
outstanding_result_pointer_ = true;
return actual_buffer() + actual_buffer_index_ * alignment_;
}
void MockTransferBuffer::ReleaseResultBuffer() {
EXPECT_TRUE(outstanding_result_pointer_);
outstanding_result_pointer_ = false;
}
int MockTransferBuffer::GetResultOffset() {
return actual_buffer_index_ * alignment_;
}
void MockTransferBuffer::Free() {
NOTREACHED();
}
bool MockTransferBuffer::HaveBuffer() const {
return true;
}
void* MockTransferBuffer::AllocUpTo(unsigned int size,
unsigned int* size_allocated) {
EXPECT_TRUE(size_allocated != nullptr);
EXPECT_TRUE(last_alloc_ == nullptr);
// Toggle which buffer we get each time to simulate the buffer being
// reallocated.
actual_buffer_index_ = (actual_buffer_index_ + 1) % kNumBuffers;
size = std::min(size, MaxTransferBufferSize());
if (actual_offset_ + size > size_) {
actual_offset_ = result_size_;
}
uint32_t offset = actual_offset_;
actual_offset_ += RoundToAlignment(size);
*size_allocated = size;
// Make sure each buffer has a different offset.
last_alloc_ = actual_buffer() + offset + actual_buffer_index_ * alignment_;
return last_alloc_;
}
void* MockTransferBuffer::Alloc(unsigned int size) {
EXPECT_LE(size, MaxTransferBufferSize());
unsigned int temp = 0;
void* p = AllocUpTo(size, &temp);
EXPECT_EQ(temp, size);
return p;
}
RingBuffer::Offset MockTransferBuffer::GetOffset(void* pointer) const {
// Make sure each buffer has a different offset.
return static_cast<uint8_t*>(pointer) - actual_buffer();
}
void MockTransferBuffer::DiscardBlock(void* p) {
EXPECT_EQ(last_alloc_, p);
last_alloc_ = nullptr;
}
void MockTransferBuffer::FreePendingToken(void* p, unsigned int /* token */) {
EXPECT_EQ(last_alloc_, p);
last_alloc_ = nullptr;
}
unsigned int MockTransferBuffer::GetSize() const {
return 0;
}
unsigned int MockTransferBuffer::GetFreeSize() const {
return 0;
}
unsigned int MockTransferBuffer::GetFragmentedFreeSize() const {
return 0;
}
unsigned int MockTransferBuffer::GetMaxSize() const {
return 0;
}
void MockTransferBuffer::ShrinkLastBlock(unsigned int new_size) {}
uint32_t MockTransferBuffer::MaxTransferBufferSize() {
return size_ - result_size_;
}
unsigned int MockTransferBuffer::RoundToAlignment(unsigned int size) {
return base::bits::AlignUp(size, alignment_);
}
bool MockTransferBuffer::InSync() {
return expected_buffer_index_ == actual_buffer_index_ &&
expected_offset_ == actual_offset_;
}
MockTransferBuffer::ExpectedMemoryInfo MockTransferBuffer::GetExpectedMemory(
uint32_t size) {
ExpectedMemoryInfo mem;
mem.offset = AllocateExpectedTransferBuffer(size);
mem.id = GetExpectedTransferBufferId();
mem.ptr = static_cast<uint8_t*>(
GetExpectedTransferAddressFromOffset(mem.offset, size));
return mem;
}
MockTransferBuffer::ExpectedMemoryInfo
MockTransferBuffer::GetExpectedResultMemory(uint32_t size) {
ExpectedMemoryInfo mem;
mem.offset = GetExpectedResultBufferOffset();
mem.id = GetExpectedResultBufferId();
mem.ptr = static_cast<uint8_t*>(
GetExpectedTransferAddressFromOffset(mem.offset, size));
return mem;
}
uint32_t MockTransferBuffer::AllocateExpectedTransferBuffer(uint32_t size) {
EXPECT_LE(size, MaxTransferBufferSize());
// Toggle which buffer we get each time to simulate the buffer being
// reallocated.
expected_buffer_index_ = (expected_buffer_index_ + 1) % kNumBuffers;
if (expected_offset_ + size > size_) {
expected_offset_ = result_size_;
}
uint32_t offset = expected_offset_;
expected_offset_ += RoundToAlignment(size);
// Make sure each buffer has a different offset.
return offset + expected_buffer_index_ * alignment_;
}
void* MockTransferBuffer::GetExpectedTransferAddressFromOffset(uint32_t offset,
uint32_t size) {
EXPECT_GE(offset, expected_buffer_index_ * alignment_);
EXPECT_LE(offset + size, size_ + expected_buffer_index_ * alignment_);
return expected_buffer() + offset;
}
int MockTransferBuffer::GetExpectedResultBufferId() {
return buffer_ids_[expected_buffer_index_];
}
uint32_t MockTransferBuffer::GetExpectedResultBufferOffset() {
return expected_buffer_index_ * alignment_;
}
int MockTransferBuffer::GetExpectedTransferBufferId() {
return buffer_ids_[expected_buffer_index_];
}
} // namespace gpu