[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1bee398 | 2009-12-17 23:15:28 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 4 | |
| 5 | // This file contains the implementation of the command buffer helper class. |
| 6 | |
[email protected] | 9f42732 | 2010-03-08 22:58:58 | [diff] [blame] | 7 | #include "../client/cmd_buffer_helper.h" |
| 8 | #include "../common/command_buffer.h" |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 9 | #include "../common/trace_event.h" |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 10 | |
[email protected] | a7a27ace | 2009-12-12 00:11:25 | [diff] [blame] | 11 | namespace gpu { |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 12 | |
[email protected] | b36897c1 | 2011-07-12 18:04:10 | [diff] [blame] | 13 | namespace { |
| 14 | const int kCommandsPerFlushCheck = 100; |
| 15 | const double kFlushDelay = 1.0 / (5.0 * 60.0); |
| 16 | } |
| 17 | |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 18 | CommandBufferHelper::CommandBufferHelper(CommandBuffer* command_buffer) |
| 19 | : command_buffer_(command_buffer), |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 20 | ring_buffer_id_(-1), |
| 21 | ring_buffer_size_(0), |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 22 | entries_(NULL), |
[email protected] | 9310b26 | 2010-06-03 16:15:47 | [diff] [blame] | 23 | total_entry_count_(0), |
| 24 | usable_entry_count_(0), |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 25 | token_(0), |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 26 | put_(0), |
[email protected] | b36897c1 | 2011-07-12 18:04:10 | [diff] [blame] | 27 | last_put_sent_(0), |
| 28 | commands_issued_(0), |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 29 | usable_(true), |
[email protected] | b36897c1 | 2011-07-12 18:04:10 | [diff] [blame] | 30 | last_flush_time_(0) { |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 31 | } |
| 32 | |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 33 | bool CommandBufferHelper::AllocateRingBuffer() { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 34 | if (!usable()) { |
| 35 | return false; |
| 36 | } |
| 37 | |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 38 | if (HaveRingBuffer()) { |
| 39 | return true; |
| 40 | } |
| 41 | |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 42 | int32 id = command_buffer_->CreateTransferBuffer(ring_buffer_size_, -1); |
| 43 | if (id < 0) { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 44 | ClearUsable(); |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 45 | return false; |
| 46 | } |
| 47 | |
| 48 | ring_buffer_ = command_buffer_->GetTransferBuffer(id); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 49 | if (!ring_buffer_.ptr) { |
| 50 | ClearUsable(); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 51 | return false; |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 52 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 53 | |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 54 | ring_buffer_id_ = id; |
| 55 | command_buffer_->SetGetBuffer(id); |
| 56 | |
| 57 | // TODO(gman): Do we really need to call GetState here? We know get & put = 0 |
| 58 | // Also do we need to check state.num_entries? |
[email protected] | c77ea36 | 2010-01-29 22:02:36 | [diff] [blame] | 59 | CommandBuffer::State state = command_buffer_->GetState(); |
[email protected] | 7477ea6f | 2009-12-22 23:28:15 | [diff] [blame] | 60 | entries_ = static_cast<CommandBufferEntry*>(ring_buffer_.ptr); |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 61 | int32 num_ring_buffer_entries = |
| 62 | ring_buffer_size_ / sizeof(CommandBufferEntry); |
[email protected] | b21265f | 2010-05-12 17:05:13 | [diff] [blame] | 63 | if (num_ring_buffer_entries > state.num_entries) { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 64 | ClearUsable(); |
[email protected] | b21265f | 2010-05-12 17:05:13 | [diff] [blame] | 65 | return false; |
| 66 | } |
[email protected] | 9310b26 | 2010-06-03 16:15:47 | [diff] [blame] | 67 | |
| 68 | const int32 kJumpEntries = |
| 69 | sizeof(cmd::Jump) / sizeof(*entries_); // NOLINT |
| 70 | |
| 71 | total_entry_count_ = num_ring_buffer_entries; |
| 72 | usable_entry_count_ = total_entry_count_ - kJumpEntries; |
[email protected] | c77ea36 | 2010-01-29 22:02:36 | [diff] [blame] | 73 | put_ = state.put_offset; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 77 | void CommandBufferHelper::FreeRingBuffer() { |
| 78 | GPU_CHECK_EQ(put_, get_offset()); |
| 79 | if (HaveRingBuffer()) { |
| 80 | command_buffer_->DestroyTransferBuffer(ring_buffer_id_); |
| 81 | ring_buffer_id_ = -1; |
| 82 | } |
| 83 | } |
| 84 | |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 85 | bool CommandBufferHelper::Initialize(int32 ring_buffer_size) { |
| 86 | ring_buffer_size_ = ring_buffer_size; |
| 87 | return AllocateRingBuffer(); |
| 88 | } |
| 89 | |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 90 | CommandBufferHelper::~CommandBufferHelper() { |
| 91 | } |
| 92 | |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 93 | bool CommandBufferHelper::FlushSync() { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 94 | if (!usable()) { |
| 95 | return false; |
| 96 | } |
[email protected] | 14eb04b | 2011-09-08 00:27:07 | [diff] [blame] | 97 | last_flush_time_ = clock(); |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 98 | last_put_sent_ = put_; |
[email protected] | d0f02c4 | 2011-07-21 21:40:48 | [diff] [blame] | 99 | CommandBuffer::State state = command_buffer_->FlushSync(put_, get_offset()); |
[email protected] | f7a64ee | 2010-02-01 22:24:14 | [diff] [blame] | 100 | return state.error == error::kNoError; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 103 | void CommandBufferHelper::Flush() { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 104 | if (usable()) { |
| 105 | last_flush_time_ = clock(); |
| 106 | last_put_sent_ = put_; |
| 107 | command_buffer_->Flush(put_); |
| 108 | } |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 111 | // Calls Flush() and then waits until the buffer is empty. Break early if the |
| 112 | // error is set. |
| 113 | bool CommandBufferHelper::Finish() { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 114 | TRACE_EVENT0("gpu", "CommandBufferHelper::Finish"); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 115 | if (!usable()) { |
| 116 | return false; |
| 117 | } |
[email protected] | a24e758 | 2012-02-15 23:21:32 | [diff] [blame^] | 118 | // If there is no work just exit. |
| 119 | if (put_ == get_offset()) { |
| 120 | return true; |
| 121 | } |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 122 | GPU_DCHECK(HaveRingBuffer()); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 123 | do { |
| 124 | // Do not loop forever if the flush fails, meaning the command buffer reader |
[email protected] | c77ea36 | 2010-01-29 22:02:36 | [diff] [blame] | 125 | // has shutdown. |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 126 | if (!FlushSync()) |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 127 | return false; |
[email protected] | d0f02c4 | 2011-07-21 21:40:48 | [diff] [blame] | 128 | } while (put_ != get_offset()); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 129 | |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | // Inserts a new token into the command stream. It uses an increasing value |
| 134 | // scheme so that we don't lose tokens (a token has passed if the current token |
| 135 | // value is higher than that token). Calls Finish() if the token value wraps, |
| 136 | // which will be rare. |
| 137 | int32 CommandBufferHelper::InsertToken() { |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 138 | AllocateRingBuffer(); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 139 | if (!usable()) { |
| 140 | return token_; |
| 141 | } |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 142 | GPU_DCHECK(HaveRingBuffer()); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 143 | // Increment token as 31-bit integer. Negative values are used to signal an |
| 144 | // error. |
| 145 | token_ = (token_ + 1) & 0x7FFFFFFF; |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 146 | cmd::SetToken* cmd = GetCmdSpace<cmd::SetToken>(); |
| 147 | if (cmd) { |
| 148 | cmd->Init(token_); |
| 149 | if (token_ == 0) { |
| 150 | TRACE_EVENT0("gpu", "CommandBufferHelper::InsertToken(wrapped)"); |
| 151 | // we wrapped |
| 152 | Finish(); |
| 153 | GPU_DCHECK_EQ(token_, last_token_read()); |
| 154 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 155 | } |
| 156 | return token_; |
| 157 | } |
| 158 | |
| 159 | // Waits until the current token value is greater or equal to the value passed |
| 160 | // in argument. |
| 161 | void CommandBufferHelper::WaitForToken(int32 token) { |
[email protected] | 73e5f815 | 2011-05-13 23:30:35 | [diff] [blame] | 162 | TRACE_EVENT_IF_LONGER_THAN0(50, "gpu", "CommandBufferHelper::WaitForToken"); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 163 | if (!usable()) { |
| 164 | return; |
| 165 | } |
| 166 | GPU_DCHECK(HaveRingBuffer()); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 167 | // Return immediately if corresponding InsertToken failed. |
| 168 | if (token < 0) |
| 169 | return; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 170 | if (token > token_) return; // we wrapped |
[email protected] | d0f02c4 | 2011-07-21 21:40:48 | [diff] [blame] | 171 | while (last_token_read() < token) { |
| 172 | if (get_offset() == put_) { |
[email protected] | 20407e9 | 2010-09-08 18:31:08 | [diff] [blame] | 173 | GPU_LOG(FATAL) << "Empty command buffer while waiting on a token."; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | // Do not loop forever if the flush fails, meaning the command buffer reader |
| 177 | // has shutdown. |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 178 | if (!FlushSync()) |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 179 | return; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | // Waits for available entries, basically waiting until get >= put + count + 1. |
| 184 | // It actually waits for contiguous entries, so it may need to wrap the buffer |
[email protected] | 9310b26 | 2010-06-03 16:15:47 | [diff] [blame] | 185 | // around, adding a jump. Thus this function may change the value of put_. The |
| 186 | // function will return early if an error occurs, in which case the available |
| 187 | // space may not be available. |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 188 | void CommandBufferHelper::WaitForAvailableEntries(int32 count) { |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 189 | AllocateRingBuffer(); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 190 | if (!usable()) { |
| 191 | return; |
| 192 | } |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 193 | GPU_DCHECK(HaveRingBuffer()); |
[email protected] | 827467f | 2011-05-11 20:37:38 | [diff] [blame] | 194 | GPU_DCHECK(count < usable_entry_count_); |
[email protected] | 9310b26 | 2010-06-03 16:15:47 | [diff] [blame] | 195 | if (put_ + count > usable_entry_count_) { |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 196 | // There's not enough room between the current put and the end of the |
[email protected] | 9310b26 | 2010-06-03 16:15:47 | [diff] [blame] | 197 | // buffer, so we need to wrap. We will add a jump back to the start, but we |
| 198 | // need to make sure get wraps first, actually that get is 1 or more (since |
| 199 | // put will wrap to 0 after we add the jump). |
[email protected] | 20407e9 | 2010-09-08 18:31:08 | [diff] [blame] | 200 | GPU_DCHECK_LE(1, put_); |
[email protected] | d0f02c4 | 2011-07-21 21:40:48 | [diff] [blame] | 201 | if (get_offset() > put_ || get_offset() == 0) { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 202 | TRACE_EVENT0("gpu", "CommandBufferHelper::WaitForAvailableEntries"); |
[email protected] | d0f02c4 | 2011-07-21 21:40:48 | [diff] [blame] | 203 | while (get_offset() > put_ || get_offset() == 0) { |
[email protected] | 3a69c6fe | 2011-04-14 22:07:34 | [diff] [blame] | 204 | // Do not loop forever if the flush fails, meaning the command buffer |
| 205 | // reader has shutdown. |
| 206 | if (!FlushSync()) |
| 207 | return; |
| 208 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 209 | } |
[email protected] | 9310b26 | 2010-06-03 16:15:47 | [diff] [blame] | 210 | // Insert a jump back to the beginning. |
| 211 | cmd::Jump::Set(&entries_[put_], 0); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 212 | put_ = 0; |
| 213 | } |
[email protected] | 3a69c6fe | 2011-04-14 22:07:34 | [diff] [blame] | 214 | if (AvailableEntries() < count) { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 215 | TRACE_EVENT0("gpu", "CommandBufferHelper::WaitForAvailableEntries1"); |
[email protected] | 3a69c6fe | 2011-04-14 22:07:34 | [diff] [blame] | 216 | while (AvailableEntries() < count) { |
| 217 | // Do not loop forever if the flush fails, meaning the command buffer |
| 218 | // reader has shutdown. |
| 219 | if (!FlushSync()) |
| 220 | return; |
| 221 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 222 | } |
[email protected] | 14eb04b | 2011-09-08 00:27:07 | [diff] [blame] | 223 | // Force a flush if the buffer is getting half full, or even earlier if the |
| 224 | // reader is known to be idle. |
| 225 | int32 pending = |
| 226 | (put_ + usable_entry_count_ - last_put_sent_) % usable_entry_count_; |
| 227 | int32 limit = usable_entry_count_ / |
| 228 | ((get_offset() == last_put_sent_) ? 16 : 2); |
| 229 | if (pending > limit) { |
| 230 | Flush(); |
| 231 | } else if (commands_issued_ % kCommandsPerFlushCheck == 0) { |
[email protected] | d15c0165 | 2011-12-15 20:02:45 | [diff] [blame] | 232 | #if !defined(OS_ANDROID) |
[email protected] | 14eb04b | 2011-09-08 00:27:07 | [diff] [blame] | 233 | // Allow this command buffer to be pre-empted by another if a "reasonable" |
[email protected] | d15c0165 | 2011-12-15 20:02:45 | [diff] [blame] | 234 | // amount of work has been done. On highend machines, this reduces the |
| 235 | // latency of GPU commands. However, on Android, this can cause the |
| 236 | // kernel to thrash between generating GPU commands and executing them. |
[email protected] | 14eb04b | 2011-09-08 00:27:07 | [diff] [blame] | 237 | clock_t current_time = clock(); |
| 238 | if (current_time - last_flush_time_ > kFlushDelay * CLOCKS_PER_SEC) |
[email protected] | b36897c1 | 2011-07-12 18:04:10 | [diff] [blame] | 239 | Flush(); |
[email protected] | d15c0165 | 2011-12-15 20:02:45 | [diff] [blame] | 240 | #endif |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 241 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | CommandBufferEntry* CommandBufferHelper::GetSpace(uint32 entries) { |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 245 | AllocateRingBuffer(); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 246 | if (!usable()) { |
| 247 | return NULL; |
| 248 | } |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 249 | GPU_DCHECK(HaveRingBuffer()); |
[email protected] | b36897c1 | 2011-07-12 18:04:10 | [diff] [blame] | 250 | ++commands_issued_; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 251 | WaitForAvailableEntries(entries); |
| 252 | CommandBufferEntry* space = &entries_[put_]; |
| 253 | put_ += entries; |
[email protected] | 20407e9 | 2010-09-08 18:31:08 | [diff] [blame] | 254 | GPU_DCHECK_LE(put_, usable_entry_count_); |
[email protected] | 9310b26 | 2010-06-03 16:15:47 | [diff] [blame] | 255 | if (put_ == usable_entry_count_) { |
| 256 | cmd::Jump::Set(&entries_[put_], 0); |
[email protected] | 7e5bee5 | 2010-03-08 18:21:00 | [diff] [blame] | 257 | put_ = 0; |
| 258 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 259 | return space; |
| 260 | } |
| 261 | |
[email protected] | f7a64ee | 2010-02-01 22:24:14 | [diff] [blame] | 262 | error::Error CommandBufferHelper::GetError() { |
[email protected] | c77ea36 | 2010-01-29 22:02:36 | [diff] [blame] | 263 | CommandBuffer::State state = command_buffer_->GetState(); |
[email protected] | f7a64ee | 2010-02-01 22:24:14 | [diff] [blame] | 264 | return static_cast<error::Error>(state.error); |
[email protected] | c77ea36 | 2010-01-29 22:02:36 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | a7a27ace | 2009-12-12 00:11:25 | [diff] [blame] | 267 | } // namespace gpu |