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