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
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file contains the implementation of the command buffer helper class.
#include "gpu/command_buffer/client/cmd_buffer_helper.h"
#include "gpu/command_buffer/common/command_buffer.h"
namespace gpu {
CommandBufferHelper::CommandBufferHelper(CommandBuffer* command_buffer)
: command_buffer_(command_buffer),
entries_(NULL),
entry_count_(0),
token_(0),
last_token_read_(-1),
get_(0),
put_(0) {
}
bool CommandBufferHelper::Initialize() {
ring_buffer_ = command_buffer_->GetRingBuffer();
if (!ring_buffer_.ptr)
return false;
CommandBuffer::State state = command_buffer_->GetState();
entries_ = static_cast<CommandBufferEntry*>(ring_buffer_.ptr);
entry_count_ = state.size;
put_ = state.put_offset;
SynchronizeState(state);
return true;
}
CommandBufferHelper::~CommandBufferHelper() {
}
bool CommandBufferHelper::Flush() {
CommandBuffer::State state = command_buffer_->Flush(put_);
SynchronizeState(state);
return state.error == error::kNoError;
}
// Calls Flush() and then waits until the buffer is empty. Break early if the
// error is set.
bool CommandBufferHelper::Finish() {
do {
// Do not loop forever if the flush fails, meaning the command buffer reader
// has shutdown.
if (!Flush())
return false;
} while (put_ != get_);
return true;
}
// Inserts a new token into the command stream. It uses an increasing value
// scheme so that we don't lose tokens (a token has passed if the current token
// value is higher than that token). Calls Finish() if the token value wraps,
// which will be rare.
int32 CommandBufferHelper::InsertToken() {
// Increment token as 31-bit integer. Negative values are used to signal an
// error.
token_ = (token_ + 1) & 0x7FFFFFFF;
cmd::SetToken& cmd = GetCmdSpace<cmd::SetToken>();
cmd.Init(token_);
if (token_ == 0) {
// we wrapped
Finish();
DCHECK_EQ(token_, last_token_read_);
}
return token_;
}
// Waits until the current token value is greater or equal to the value passed
// in argument.
void CommandBufferHelper::WaitForToken(int32 token) {
// Return immediately if corresponding InsertToken failed.
if (token < 0)
return;
if (last_token_read_ >= token) return; // fast path.
if (token > token_) return; // we wrapped
Flush();
while (last_token_read_ < token) {
if (get_ == put_) {
LOG(FATAL) << "Empty command buffer while waiting on a token.";
return;
}
// Do not loop forever if the flush fails, meaning the command buffer reader
// has shutdown.
if (!Flush())
return;
}
}
// Waits for available entries, basically waiting until get >= put + count + 1.
// It actually waits for contiguous entries, so it may need to wrap the buffer
// around, adding noops. Thus this function may change the value of put_.
// The function will return early if an error occurs, in which case the
// available space may not be available.
void CommandBufferHelper::WaitForAvailableEntries(int32 count) {
CHECK(count < entry_count_);
if (put_ + count > entry_count_) {
// There's not enough room between the current put and the end of the
// buffer, so we need to wrap. We will add noops all the way to the end,
// but we need to make sure get wraps first, actually that get is 1 or
// more (since put will wrap to 0 after we add the noops).
DCHECK_LE(1, put_);
Flush();
while (get_ > put_ || get_ == 0) {
// Do not loop forever if the flush fails, meaning the command buffer
// reader has shutdown.
if (!Flush())
return;
}
// Insert Noops to fill out buffer.
int32 num_entries = entry_count_ - put_;
while (num_entries > 0) {
int32 num_to_skip = std::min(CommandHeader::kMaxSize, num_entries);
cmd::Noop::Set(&entries_[put_], num_to_skip);
put_ += num_to_skip;
num_entries -= num_to_skip;
}
put_ = 0;
}
// If we have enough room, return immediatly.
if (count <= AvailableEntries()) return;
// Otherwise flush, and wait until we do have enough room.
Flush();
while (AvailableEntries() < count) {
// Do not loop forever if the flush fails, meaning the command buffer reader
// has shutdown.
if (!Flush())
return;
}
}
CommandBufferEntry* CommandBufferHelper::GetSpace(uint32 entries) {
WaitForAvailableEntries(entries);
CommandBufferEntry* space = &entries_[put_];
put_ += entries;
return space;
}
error::Error CommandBufferHelper::GetError() {
CommandBuffer::State state = command_buffer_->GetState();
SynchronizeState(state);
return static_cast<error::Error>(state.error);
}
void CommandBufferHelper::SynchronizeState(CommandBuffer::State state) {
get_ = state.get_offset;
last_token_read_ = state.token;
}
} // namespace gpu
|