[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] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 21 | token_(0), |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 22 | put_(0), |
[email protected] | b36897c1 | 2011-07-12 18:04:10 | [diff] [blame] | 23 | last_put_sent_(0), |
| 24 | commands_issued_(0), |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 25 | usable_(true), |
[email protected] | d35e6a7 | 2012-08-25 01:51:13 | [diff] [blame] | 26 | context_lost_(false), |
[email protected] | 362e6d60 | 2012-10-17 16:55:06 | [diff] [blame] | 27 | flush_automatically_(true), |
[email protected] | b36897c1 | 2011-07-12 18:04:10 | [diff] [blame] | 28 | last_flush_time_(0) { |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 29 | } |
| 30 | |
[email protected] | 362e6d60 | 2012-10-17 16:55:06 | [diff] [blame] | 31 | void CommandBufferHelper::SetAutomaticFlushes(bool enabled) { |
| 32 | flush_automatically_ = enabled; |
| 33 | } |
| 34 | |
[email protected] | d35e6a7 | 2012-08-25 01:51:13 | [diff] [blame] | 35 | bool CommandBufferHelper::IsContextLost() { |
| 36 | if (!context_lost_) { |
| 37 | context_lost_ = error::IsError(command_buffer()->GetLastError()); |
| 38 | } |
| 39 | return context_lost_; |
| 40 | } |
| 41 | |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 42 | bool CommandBufferHelper::AllocateRingBuffer() { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 43 | if (!usable()) { |
| 44 | return false; |
| 45 | } |
| 46 | |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 47 | if (HaveRingBuffer()) { |
| 48 | return true; |
| 49 | } |
| 50 | |
[email protected] | 67c8078 | 2012-12-21 01:16:52 | [diff] [blame] | 51 | int32 id = -1; |
| 52 | Buffer buffer = command_buffer_->CreateTransferBuffer(ring_buffer_size_, &id); |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 53 | if (id < 0) { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 54 | ClearUsable(); |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
[email protected] | 67c8078 | 2012-12-21 01:16:52 | [diff] [blame] | 58 | ring_buffer_ = buffer; |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 59 | ring_buffer_id_ = id; |
| 60 | command_buffer_->SetGetBuffer(id); |
| 61 | |
| 62 | // TODO(gman): Do we really need to call GetState here? We know get & put = 0 |
| 63 | // Also do we need to check state.num_entries? |
[email protected] | c77ea36 | 2010-01-29 22:02:36 | [diff] [blame] | 64 | CommandBuffer::State state = command_buffer_->GetState(); |
[email protected] | 7477ea6f | 2009-12-22 23:28:15 | [diff] [blame] | 65 | entries_ = static_cast<CommandBufferEntry*>(ring_buffer_.ptr); |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 66 | int32 num_ring_buffer_entries = |
| 67 | ring_buffer_size_ / sizeof(CommandBufferEntry); |
[email protected] | b21265f | 2010-05-12 17:05:13 | [diff] [blame] | 68 | if (num_ring_buffer_entries > state.num_entries) { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 69 | ClearUsable(); |
[email protected] | b21265f | 2010-05-12 17:05:13 | [diff] [blame] | 70 | return false; |
| 71 | } |
[email protected] | 9310b26 | 2010-06-03 16:15:47 | [diff] [blame] | 72 | |
[email protected] | 9310b26 | 2010-06-03 16:15:47 | [diff] [blame] | 73 | total_entry_count_ = num_ring_buffer_entries; |
[email protected] | c77ea36 | 2010-01-29 22:02:36 | [diff] [blame] | 74 | put_ = state.put_offset; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | |
[email protected] | a5cf3cb | 2012-08-23 01:08:42 | [diff] [blame] | 78 | void CommandBufferHelper::FreeResources() { |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 79 | if (HaveRingBuffer()) { |
| 80 | command_buffer_->DestroyTransferBuffer(ring_buffer_id_); |
| 81 | ring_buffer_id_ = -1; |
| 82 | } |
| 83 | } |
| 84 | |
[email protected] | a5cf3cb | 2012-08-23 01:08:42 | [diff] [blame] | 85 | void CommandBufferHelper::FreeRingBuffer() { |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 86 | CHECK((put_ == get_offset()) || |
[email protected] | ea51dbd | 2013-01-18 10:03:53 | [diff] [blame] | 87 | error::IsError(command_buffer_->GetLastState().error)); |
[email protected] | a5cf3cb | 2012-08-23 01:08:42 | [diff] [blame] | 88 | FreeResources(); |
| 89 | } |
| 90 | |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 91 | bool CommandBufferHelper::Initialize(int32 ring_buffer_size) { |
| 92 | ring_buffer_size_ = ring_buffer_size; |
| 93 | return AllocateRingBuffer(); |
| 94 | } |
| 95 | |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 96 | CommandBufferHelper::~CommandBufferHelper() { |
[email protected] | a5cf3cb | 2012-08-23 01:08:42 | [diff] [blame] | 97 | FreeResources(); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 100 | bool CommandBufferHelper::FlushSync() { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 101 | if (!usable()) { |
| 102 | return false; |
| 103 | } |
[email protected] | 14eb04b | 2011-09-08 00:27:07 | [diff] [blame] | 104 | last_flush_time_ = clock(); |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 105 | last_put_sent_ = put_; |
[email protected] | d0f02c4 | 2011-07-21 21:40:48 | [diff] [blame] | 106 | CommandBuffer::State state = command_buffer_->FlushSync(put_, get_offset()); |
[email protected] | f7a64ee | 2010-02-01 22:24:14 | [diff] [blame] | 107 | return state.error == error::kNoError; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 110 | void CommandBufferHelper::Flush() { |
[email protected] | 9f5ab39 | 2013-01-30 12:47:13 | [diff] [blame] | 111 | if (usable() && last_put_sent_ != put_) { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 112 | last_flush_time_ = clock(); |
| 113 | last_put_sent_ = put_; |
| 114 | command_buffer_->Flush(put_); |
| 115 | } |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 118 | // Calls Flush() and then waits until the buffer is empty. Break early if the |
| 119 | // error is set. |
| 120 | bool CommandBufferHelper::Finish() { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 121 | TRACE_EVENT0("gpu", "CommandBufferHelper::Finish"); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 122 | if (!usable()) { |
| 123 | return false; |
| 124 | } |
[email protected] | a24e758 | 2012-02-15 23:21:32 | [diff] [blame] | 125 | // If there is no work just exit. |
| 126 | if (put_ == get_offset()) { |
| 127 | return true; |
| 128 | } |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 129 | DCHECK(HaveRingBuffer()); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 130 | do { |
| 131 | // Do not loop forever if the flush fails, meaning the command buffer reader |
[email protected] | c77ea36 | 2010-01-29 22:02:36 | [diff] [blame] | 132 | // has shutdown. |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 133 | if (!FlushSync()) |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 134 | return false; |
[email protected] | d0f02c4 | 2011-07-21 21:40:48 | [diff] [blame] | 135 | } while (put_ != get_offset()); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | // Inserts a new token into the command stream. It uses an increasing value |
| 141 | // scheme so that we don't lose tokens (a token has passed if the current token |
| 142 | // value is higher than that token). Calls Finish() if the token value wraps, |
| 143 | // which will be rare. |
| 144 | int32 CommandBufferHelper::InsertToken() { |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 145 | AllocateRingBuffer(); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 146 | if (!usable()) { |
| 147 | return token_; |
| 148 | } |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 149 | DCHECK(HaveRingBuffer()); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 150 | // Increment token as 31-bit integer. Negative values are used to signal an |
| 151 | // error. |
| 152 | token_ = (token_ + 1) & 0x7FFFFFFF; |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 153 | cmd::SetToken* cmd = GetCmdSpace<cmd::SetToken>(); |
| 154 | if (cmd) { |
| 155 | cmd->Init(token_); |
| 156 | if (token_ == 0) { |
| 157 | TRACE_EVENT0("gpu", "CommandBufferHelper::InsertToken(wrapped)"); |
| 158 | // we wrapped |
| 159 | Finish(); |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 160 | DCHECK_EQ(token_, last_token_read()); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 161 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 162 | } |
| 163 | return token_; |
| 164 | } |
| 165 | |
| 166 | // Waits until the current token value is greater or equal to the value passed |
| 167 | // in argument. |
| 168 | void CommandBufferHelper::WaitForToken(int32 token) { |
[email protected] | ea51dbd | 2013-01-18 10:03:53 | [diff] [blame] | 169 | if (!usable() || !HaveRingBuffer()) { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 170 | return; |
| 171 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 172 | // Return immediately if corresponding InsertToken failed. |
| 173 | if (token < 0) |
| 174 | return; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 175 | if (token > token_) return; // we wrapped |
[email protected] | d0f02c4 | 2011-07-21 21:40:48 | [diff] [blame] | 176 | while (last_token_read() < token) { |
| 177 | if (get_offset() == put_) { |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 178 | LOG(FATAL) << "Empty command buffer while waiting on a token."; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 179 | return; |
| 180 | } |
| 181 | // Do not loop forever if the flush fails, meaning the command buffer reader |
| 182 | // has shutdown. |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 183 | if (!FlushSync()) |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 184 | return; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | // Waits for available entries, basically waiting until get >= put + count + 1. |
| 189 | // 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] | 190 | // 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] | 191 | // function will return early if an error occurs, in which case the available |
| 192 | // space may not be available. |
[email protected] | 82c6632 | 2013-11-15 21:36:56 | [diff] [blame] | 193 | bool CommandBufferHelper::WaitForAvailableEntries(int32 count) { |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 194 | DCHECK(count < total_entry_count_); |
[email protected] | 4725737 | 2013-01-04 18:37:48 | [diff] [blame] | 195 | if (put_ + count > total_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] | 4725737 | 2013-01-04 18:37:48 | [diff] [blame] | 197 | // buffer, so we need to wrap. We will add noops all the way to the end, |
| 198 | // but we need to make sure get wraps first, actually that get is 1 or |
| 199 | // more (since put will wrap to 0 after we add the noops). |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 200 | 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()) |
[email protected] | 82c6632 | 2013-11-15 21:36:56 | [diff] [blame] | 207 | return false; |
[email protected] | 3a69c6fe | 2011-04-14 22:07:34 | [diff] [blame] | 208 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 209 | } |
[email protected] | 4725737 | 2013-01-04 18:37:48 | [diff] [blame] | 210 | // Insert Noops to fill out the buffer. |
| 211 | int32 num_entries = total_entry_count_ - put_; |
| 212 | while (num_entries > 0) { |
| 213 | int32 num_to_skip = std::min(CommandHeader::kMaxSize, num_entries); |
| 214 | cmd::Noop::Set(&entries_[put_], num_to_skip); |
| 215 | put_ += num_to_skip; |
| 216 | num_entries -= num_to_skip; |
| 217 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 218 | put_ = 0; |
| 219 | } |
[email protected] | 3a69c6fe | 2011-04-14 22:07:34 | [diff] [blame] | 220 | if (AvailableEntries() < count) { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 221 | TRACE_EVENT0("gpu", "CommandBufferHelper::WaitForAvailableEntries1"); |
[email protected] | 3a69c6fe | 2011-04-14 22:07:34 | [diff] [blame] | 222 | while (AvailableEntries() < count) { |
| 223 | // Do not loop forever if the flush fails, meaning the command buffer |
| 224 | // reader has shutdown. |
| 225 | if (!FlushSync()) |
[email protected] | 82c6632 | 2013-11-15 21:36:56 | [diff] [blame] | 226 | return false; |
[email protected] | 3a69c6fe | 2011-04-14 22:07:34 | [diff] [blame] | 227 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 228 | } |
[email protected] | 82c6632 | 2013-11-15 21:36:56 | [diff] [blame] | 229 | FlushIfNeeded(); |
| 230 | return true; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | CommandBufferEntry* CommandBufferHelper::GetSpace(uint32 entries) { |
[email protected] | 82c6632 | 2013-11-15 21:36:56 | [diff] [blame] | 234 | ++commands_issued_; |
| 235 | if (HaveRingBuffer() && usable() && entries <= SpaceAvailableImmediately()) { |
| 236 | FlushIfNeeded(); |
| 237 | return GetSpaceForEntries(entries); |
| 238 | } |
| 239 | |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 240 | AllocateRingBuffer(); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 241 | if (!usable()) { |
| 242 | return NULL; |
| 243 | } |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 244 | DCHECK(HaveRingBuffer()); |
[email protected] | 82c6632 | 2013-11-15 21:36:56 | [diff] [blame] | 245 | if (!WaitForAvailableEntries(entries)) |
| 246 | return NULL; |
| 247 | |
| 248 | return GetSpaceForEntries(entries); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 249 | } |
| 250 | |
[email protected] | a7a27ace | 2009-12-12 00:11:25 | [diff] [blame] | 251 | } // namespace gpu |