[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] | 1df1986 | 2013-05-24 11:26:29 | [diff] [blame] | 7 | #include "gpu/command_buffer/client/cmd_buffer_helper.h" |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 8 | |
| 9 | #include "base/logging.h" |
[email protected] | 1df1986 | 2013-05-24 11:26:29 | [diff] [blame] | 10 | #include "gpu/command_buffer/common/command_buffer.h" |
| 11 | #include "gpu/command_buffer/common/trace_event.h" |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 12 | |
[email protected] | a7a27ace | 2009-12-12 00:11:25 | [diff] [blame] | 13 | namespace gpu { |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 14 | |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 15 | CommandBufferHelper::CommandBufferHelper(CommandBuffer* command_buffer) |
| 16 | : command_buffer_(command_buffer), |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 17 | ring_buffer_id_(-1), |
| 18 | ring_buffer_size_(0), |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 19 | entries_(NULL), |
[email protected] | 9310b26 | 2010-06-03 16:15:47 | [diff] [blame] | 20 | total_entry_count_(0), |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 21 | immediate_entry_count_(0), |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 22 | token_(0), |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 23 | put_(0), |
[email protected] | b36897c1 | 2011-07-12 18:04:10 | [diff] [blame] | 24 | last_put_sent_(0), |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 25 | #if defined(CMD_HELPER_PERIODIC_FLUSH_CHECK) |
[email protected] | b36897c1 | 2011-07-12 18:04:10 | [diff] [blame] | 26 | commands_issued_(0), |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 27 | #endif |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 28 | usable_(true), |
[email protected] | d35e6a7 | 2012-08-25 01:51:13 | [diff] [blame] | 29 | context_lost_(false), |
[email protected] | 362e6d60 | 2012-10-17 16:55:06 | [diff] [blame] | 30 | flush_automatically_(true), |
[email protected] | 3d668fb | 2014-02-22 00:49:38 | [diff] [blame] | 31 | last_flush_time_(0), |
| 32 | flush_generation_(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; |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 37 | CalcImmediateEntries(0); |
[email protected] | 362e6d60 | 2012-10-17 16:55:06 | [diff] [blame] | 38 | } |
| 39 | |
[email protected] | d35e6a7 | 2012-08-25 01:51:13 | [diff] [blame] | 40 | bool CommandBufferHelper::IsContextLost() { |
| 41 | if (!context_lost_) { |
| 42 | context_lost_ = error::IsError(command_buffer()->GetLastError()); |
| 43 | } |
| 44 | return context_lost_; |
| 45 | } |
| 46 | |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 47 | void CommandBufferHelper::CalcImmediateEntries(int waiting_count) { |
| 48 | DCHECK_GE(waiting_count, 0); |
| 49 | |
| 50 | // Check if usable & allocated. |
| 51 | if (!usable() || !HaveRingBuffer()) { |
| 52 | immediate_entry_count_ = 0; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | // Get maximum safe contiguous entries. |
| 57 | const int32 curr_get = get_offset(); |
| 58 | if (curr_get > put_) { |
| 59 | immediate_entry_count_ = curr_get - put_ - 1; |
| 60 | } else { |
| 61 | immediate_entry_count_ = |
| 62 | total_entry_count_ - put_ - (curr_get == 0 ? 1 : 0); |
| 63 | } |
| 64 | |
| 65 | // Limit entry count to force early flushing. |
| 66 | if (flush_automatically_) { |
| 67 | int32 limit = |
| 68 | total_entry_count_ / |
| 69 | ((curr_get == last_put_sent_) ? kAutoFlushSmall : kAutoFlushBig); |
| 70 | |
| 71 | int32 pending = |
| 72 | (put_ + total_entry_count_ - last_put_sent_) % total_entry_count_; |
| 73 | |
| 74 | if (pending > 0 && pending >= limit) { |
| 75 | // Time to force flush. |
| 76 | immediate_entry_count_ = 0; |
| 77 | } else { |
| 78 | // Limit remaining entries, but not lower than waiting_count entries to |
| 79 | // prevent deadlock when command size is greater than the flush limit. |
| 80 | limit -= pending; |
| 81 | limit = limit < waiting_count ? waiting_count : limit; |
| 82 | immediate_entry_count_ = |
| 83 | immediate_entry_count_ > limit ? limit : immediate_entry_count_; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 88 | bool CommandBufferHelper::AllocateRingBuffer() { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 89 | if (!usable()) { |
| 90 | return false; |
| 91 | } |
| 92 | |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 93 | if (HaveRingBuffer()) { |
| 94 | return true; |
| 95 | } |
| 96 | |
[email protected] | 67c8078 | 2012-12-21 01:16:52 | [diff] [blame] | 97 | int32 id = -1; |
[email protected] | 4409660 | 2014-03-26 04:53:58 | [diff] [blame^] | 98 | scoped_refptr<Buffer> buffer = |
| 99 | command_buffer_->CreateTransferBuffer(ring_buffer_size_, &id); |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 100 | if (id < 0) { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 101 | ClearUsable(); |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | |
[email protected] | 67c8078 | 2012-12-21 01:16:52 | [diff] [blame] | 105 | ring_buffer_ = buffer; |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 106 | ring_buffer_id_ = id; |
| 107 | command_buffer_->SetGetBuffer(id); |
| 108 | |
| 109 | // TODO(gman): Do we really need to call GetState here? We know get & put = 0 |
| 110 | // Also do we need to check state.num_entries? |
[email protected] | c77ea36 | 2010-01-29 22:02:36 | [diff] [blame] | 111 | CommandBuffer::State state = command_buffer_->GetState(); |
[email protected] | 4409660 | 2014-03-26 04:53:58 | [diff] [blame^] | 112 | entries_ = static_cast<CommandBufferEntry*>(ring_buffer_->memory()); |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 113 | int32 num_ring_buffer_entries = |
| 114 | ring_buffer_size_ / sizeof(CommandBufferEntry); |
[email protected] | b21265f | 2010-05-12 17:05:13 | [diff] [blame] | 115 | if (num_ring_buffer_entries > state.num_entries) { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 116 | ClearUsable(); |
[email protected] | b21265f | 2010-05-12 17:05:13 | [diff] [blame] | 117 | return false; |
| 118 | } |
[email protected] | 9310b26 | 2010-06-03 16:15:47 | [diff] [blame] | 119 | |
[email protected] | 9310b26 | 2010-06-03 16:15:47 | [diff] [blame] | 120 | total_entry_count_ = num_ring_buffer_entries; |
[email protected] | c77ea36 | 2010-01-29 22:02:36 | [diff] [blame] | 121 | put_ = state.put_offset; |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 122 | CalcImmediateEntries(0); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
[email protected] | a5cf3cb | 2012-08-23 01:08:42 | [diff] [blame] | 126 | void CommandBufferHelper::FreeResources() { |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 127 | if (HaveRingBuffer()) { |
| 128 | command_buffer_->DestroyTransferBuffer(ring_buffer_id_); |
| 129 | ring_buffer_id_ = -1; |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 130 | CalcImmediateEntries(0); |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
[email protected] | a5cf3cb | 2012-08-23 01:08:42 | [diff] [blame] | 134 | void CommandBufferHelper::FreeRingBuffer() { |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 135 | CHECK((put_ == get_offset()) || |
[email protected] | ea51dbd | 2013-01-18 10:03:53 | [diff] [blame] | 136 | error::IsError(command_buffer_->GetLastState().error)); |
[email protected] | a5cf3cb | 2012-08-23 01:08:42 | [diff] [blame] | 137 | FreeResources(); |
| 138 | } |
| 139 | |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 140 | bool CommandBufferHelper::Initialize(int32 ring_buffer_size) { |
| 141 | ring_buffer_size_ = ring_buffer_size; |
| 142 | return AllocateRingBuffer(); |
| 143 | } |
| 144 | |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 145 | CommandBufferHelper::~CommandBufferHelper() { |
[email protected] | a5cf3cb | 2012-08-23 01:08:42 | [diff] [blame] | 146 | FreeResources(); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | 7fe4198b | 2014-03-18 21:52:36 | [diff] [blame] | 149 | bool CommandBufferHelper::WaitForGetOffsetInRange(int32 start, int32 end) { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 150 | if (!usable()) { |
| 151 | return false; |
| 152 | } |
[email protected] | 7fe4198b | 2014-03-18 21:52:36 | [diff] [blame] | 153 | command_buffer_->WaitForGetOffsetInRange(start, end); |
| 154 | return command_buffer_->GetLastError() == gpu::error::kNoError; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 157 | void CommandBufferHelper::Flush() { |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 158 | // Wrap put_ before flush. |
| 159 | if (put_ == total_entry_count_) |
| 160 | put_ = 0; |
| 161 | |
[email protected] | 9f5ab39 | 2013-01-30 12:47:13 | [diff] [blame] | 162 | if (usable() && last_put_sent_ != put_) { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 163 | last_flush_time_ = clock(); |
| 164 | last_put_sent_ = put_; |
| 165 | command_buffer_->Flush(put_); |
[email protected] | cbe0ded | 2014-02-21 20:42:52 | [diff] [blame] | 166 | ++flush_generation_; |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 167 | CalcImmediateEntries(0); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 168 | } |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 169 | } |
| 170 | |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 171 | #if defined(CMD_HELPER_PERIODIC_FLUSH_CHECK) |
| 172 | void CommandBufferHelper::PeriodicFlushCheck() { |
| 173 | clock_t current_time = clock(); |
| 174 | if (current_time - last_flush_time_ > kPeriodicFlushDelay * CLOCKS_PER_SEC) |
| 175 | Flush(); |
| 176 | } |
| 177 | #endif |
| 178 | |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 179 | // Calls Flush() and then waits until the buffer is empty. Break early if the |
| 180 | // error is set. |
| 181 | bool CommandBufferHelper::Finish() { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 182 | TRACE_EVENT0("gpu", "CommandBufferHelper::Finish"); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 183 | if (!usable()) { |
| 184 | return false; |
| 185 | } |
[email protected] | a24e758 | 2012-02-15 23:21:32 | [diff] [blame] | 186 | // If there is no work just exit. |
| 187 | if (put_ == get_offset()) { |
| 188 | return true; |
| 189 | } |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 190 | DCHECK(HaveRingBuffer()); |
[email protected] | 7fe4198b | 2014-03-18 21:52:36 | [diff] [blame] | 191 | Flush(); |
| 192 | if (!WaitForGetOffsetInRange(put_, put_)) |
| 193 | return false; |
| 194 | DCHECK_EQ(get_offset(), put_); |
| 195 | |
| 196 | CalcImmediateEntries(0); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 197 | |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | // Inserts a new token into the command stream. It uses an increasing value |
| 202 | // scheme so that we don't lose tokens (a token has passed if the current token |
| 203 | // value is higher than that token). Calls Finish() if the token value wraps, |
| 204 | // which will be rare. |
| 205 | int32 CommandBufferHelper::InsertToken() { |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 206 | AllocateRingBuffer(); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 207 | if (!usable()) { |
| 208 | return token_; |
| 209 | } |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 210 | DCHECK(HaveRingBuffer()); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 211 | // Increment token as 31-bit integer. Negative values are used to signal an |
| 212 | // error. |
| 213 | token_ = (token_ + 1) & 0x7FFFFFFF; |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 214 | cmd::SetToken* cmd = GetCmdSpace<cmd::SetToken>(); |
| 215 | if (cmd) { |
| 216 | cmd->Init(token_); |
| 217 | if (token_ == 0) { |
| 218 | TRACE_EVENT0("gpu", "CommandBufferHelper::InsertToken(wrapped)"); |
| 219 | // we wrapped |
| 220 | Finish(); |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 221 | DCHECK_EQ(token_, last_token_read()); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 222 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 223 | } |
| 224 | return token_; |
| 225 | } |
| 226 | |
| 227 | // Waits until the current token value is greater or equal to the value passed |
| 228 | // in argument. |
| 229 | void CommandBufferHelper::WaitForToken(int32 token) { |
[email protected] | ea51dbd | 2013-01-18 10:03:53 | [diff] [blame] | 230 | if (!usable() || !HaveRingBuffer()) { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 231 | return; |
| 232 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 233 | // Return immediately if corresponding InsertToken failed. |
| 234 | if (token < 0) |
| 235 | return; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 236 | if (token > token_) return; // we wrapped |
[email protected] | 7fe4198b | 2014-03-18 21:52:36 | [diff] [blame] | 237 | if (last_token_read() > token) |
| 238 | return; |
| 239 | Flush(); |
| 240 | command_buffer_->WaitForTokenInRange(token, token_); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // Waits for available entries, basically waiting until get >= put + count + 1. |
| 244 | // It actually waits for contiguous entries, so it may need to wrap the buffer |
[email protected] | 4725737 | 2013-01-04 18:37:48 | [diff] [blame] | 245 | // around, adding a noops. Thus this function may change the value of put_. The |
[email protected] | 9310b26 | 2010-06-03 16:15:47 | [diff] [blame] | 246 | // function will return early if an error occurs, in which case the available |
| 247 | // space may not be available. |
[email protected] | 3110b12 | 2013-11-19 23:25:54 | [diff] [blame] | 248 | void CommandBufferHelper::WaitForAvailableEntries(int32 count) { |
| 249 | AllocateRingBuffer(); |
| 250 | if (!usable()) { |
| 251 | return; |
| 252 | } |
| 253 | DCHECK(HaveRingBuffer()); |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 254 | DCHECK(count < total_entry_count_); |
[email protected] | 4725737 | 2013-01-04 18:37:48 | [diff] [blame] | 255 | if (put_ + count > total_entry_count_) { |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 256 | // There's not enough room between the current put and the end of the |
[email protected] | 4725737 | 2013-01-04 18:37:48 | [diff] [blame] | 257 | // buffer, so we need to wrap. We will add noops all the way to the end, |
| 258 | // but we need to make sure get wraps first, actually that get is 1 or |
| 259 | // more (since put will wrap to 0 after we add the noops). |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 260 | DCHECK_LE(1, put_); |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 261 | int32 curr_get = get_offset(); |
| 262 | if (curr_get > put_ || curr_get == 0) { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 263 | TRACE_EVENT0("gpu", "CommandBufferHelper::WaitForAvailableEntries"); |
[email protected] | 7fe4198b | 2014-03-18 21:52:36 | [diff] [blame] | 264 | Flush(); |
| 265 | if (!WaitForGetOffsetInRange(1, put_)) |
| 266 | return; |
| 267 | curr_get = get_offset(); |
| 268 | DCHECK_LE(curr_get, put_); |
| 269 | DCHECK_NE(0, curr_get); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 270 | } |
[email protected] | 4725737 | 2013-01-04 18:37:48 | [diff] [blame] | 271 | // Insert Noops to fill out the buffer. |
| 272 | int32 num_entries = total_entry_count_ - put_; |
| 273 | while (num_entries > 0) { |
| 274 | int32 num_to_skip = std::min(CommandHeader::kMaxSize, num_entries); |
| 275 | cmd::Noop::Set(&entries_[put_], num_to_skip); |
| 276 | put_ += num_to_skip; |
| 277 | num_entries -= num_to_skip; |
| 278 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 279 | put_ = 0; |
| 280 | } |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 281 | |
| 282 | // Try to get 'count' entries without flushing. |
| 283 | CalcImmediateEntries(count); |
| 284 | if (immediate_entry_count_ < count) { |
| 285 | // Try again with a shallow Flush(). |
[email protected] | 3110b12 | 2013-11-19 23:25:54 | [diff] [blame] | 286 | Flush(); |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 287 | CalcImmediateEntries(count); |
| 288 | if (immediate_entry_count_ < count) { |
| 289 | // Buffer is full. Need to wait for entries. |
| 290 | TRACE_EVENT0("gpu", "CommandBufferHelper::WaitForAvailableEntries1"); |
[email protected] | 7fe4198b | 2014-03-18 21:52:36 | [diff] [blame] | 291 | if (!WaitForGetOffsetInRange(put_ + count + 1, put_)) |
| 292 | return; |
| 293 | CalcImmediateEntries(count); |
| 294 | DCHECK_GE(immediate_entry_count_, count); |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 295 | } |
[email protected] | 3110b12 | 2013-11-19 23:25:54 | [diff] [blame] | 296 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 297 | } |
| 298 | |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 299 | |
[email protected] | a7a27ace | 2009-12-12 00:11:25 | [diff] [blame] | 300 | } // namespace gpu |