[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 | |
avi | f15d60a | 2015-12-21 17:06:33 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
martina.kollarova | c640df1 | 2015-07-10 08:30:38 | [diff] [blame] | 11 | #include <algorithm> |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 12 | #include "base/logging.h" |
reveman | 0cf65ee8 | 2015-08-25 22:15:24 | [diff] [blame] | 13 | #include "base/strings/stringprintf.h" |
gab | b2370531 | 2016-05-11 18:44:56 | [diff] [blame] | 14 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | da618e2 | 2014-07-11 09:27:16 | [diff] [blame] | 15 | #include "base/time/time.h" |
reveman | 0cf65ee8 | 2015-08-25 22:15:24 | [diff] [blame] | 16 | #include "base/trace_event/memory_allocator_dump.h" |
| 17 | #include "base/trace_event/memory_dump_manager.h" |
| 18 | #include "base/trace_event/process_memory_dump.h" |
martina.kollarova | c0d13d1 | 2016-01-20 08:53:44 | [diff] [blame] | 19 | #include "base/trace_event/trace_event.h" |
martina.kollarova | c640df1 | 2015-07-10 08:30:38 | [diff] [blame] | 20 | #include "gpu/command_buffer/common/buffer.h" |
[email protected] | 1df1986 | 2013-05-24 11:26:29 | [diff] [blame] | 21 | #include "gpu/command_buffer/common/command_buffer.h" |
martina.kollarova | c640df1 | 2015-07-10 08:30:38 | [diff] [blame] | 22 | #include "gpu/command_buffer/common/constants.h" |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 23 | |
[email protected] | a7a27ace | 2009-12-12 00:11:25 | [diff] [blame] | 24 | namespace gpu { |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 25 | |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 26 | CommandBufferHelper::CommandBufferHelper(CommandBuffer* command_buffer) |
Sunny Sachanandani | 5acdc81 | 2018-01-25 01:56:07 | [diff] [blame] | 27 | : command_buffer_(command_buffer) {} |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 28 | |
[email protected] | 362e6d60 | 2012-10-17 16:55:06 | [diff] [blame] | 29 | void CommandBufferHelper::SetAutomaticFlushes(bool enabled) { |
| 30 | flush_automatically_ = enabled; |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 31 | CalcImmediateEntries(0); |
[email protected] | 362e6d60 | 2012-10-17 16:55:06 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | d35e6a7 | 2012-08-25 01:51:13 | [diff] [blame] | 34 | bool CommandBufferHelper::IsContextLost() { |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 35 | if (!context_lost_) |
| 36 | context_lost_ = error::IsError(command_buffer()->GetLastState().error); |
[email protected] | d35e6a7 | 2012-08-25 01:51:13 | [diff] [blame] | 37 | return context_lost_; |
| 38 | } |
| 39 | |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 40 | void CommandBufferHelper::CalcImmediateEntries(int waiting_count) { |
| 41 | DCHECK_GE(waiting_count, 0); |
| 42 | |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 43 | // If not allocated, no entries are available. If not usable, it will not be |
| 44 | // allocated. |
| 45 | if (!HaveRingBuffer()) { |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 46 | immediate_entry_count_ = 0; |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | // Get maximum safe contiguous entries. |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 51 | const int32_t curr_get = cached_get_offset_; |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 52 | if (curr_get > put_) { |
| 53 | immediate_entry_count_ = curr_get - put_ - 1; |
| 54 | } else { |
| 55 | immediate_entry_count_ = |
| 56 | total_entry_count_ - put_ - (curr_get == 0 ? 1 : 0); |
| 57 | } |
| 58 | |
| 59 | // Limit entry count to force early flushing. |
| 60 | if (flush_automatically_) { |
avi | f15d60a | 2015-12-21 17:06:33 | [diff] [blame] | 61 | int32_t limit = |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 62 | total_entry_count_ / |
Sunny Sachanandani | 5acdc81 | 2018-01-25 01:56:07 | [diff] [blame] | 63 | ((curr_get == last_flush_put_) ? kAutoFlushSmall : kAutoFlushBig); |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 64 | |
James Darpinian | 62ca13c | 2018-05-31 04:12:06 | [diff] [blame] | 65 | int32_t pending = (put_ + total_entry_count_ - last_ordering_barrier_put_) % |
| 66 | total_entry_count_; |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 67 | |
| 68 | if (pending > 0 && pending >= limit) { |
| 69 | // Time to force flush. |
| 70 | immediate_entry_count_ = 0; |
| 71 | } else { |
| 72 | // Limit remaining entries, but not lower than waiting_count entries to |
| 73 | // prevent deadlock when command size is greater than the flush limit. |
| 74 | limit -= pending; |
| 75 | limit = limit < waiting_count ? waiting_count : limit; |
| 76 | immediate_entry_count_ = |
| 77 | immediate_entry_count_ > limit ? limit : immediate_entry_count_; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 82 | bool CommandBufferHelper::AllocateRingBuffer() { |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 83 | if (!usable()) { |
| 84 | return false; |
| 85 | } |
| 86 | |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 87 | if (HaveRingBuffer()) { |
| 88 | return true; |
| 89 | } |
| 90 | |
avi | f15d60a | 2015-12-21 17:06:33 | [diff] [blame] | 91 | int32_t id = -1; |
[email protected] | 4409660 | 2014-03-26 04:53:58 | [diff] [blame] | 92 | scoped_refptr<Buffer> buffer = |
| 93 | command_buffer_->CreateTransferBuffer(ring_buffer_size_, &id); |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 94 | if (id < 0) { |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 95 | usable_ = false; |
| 96 | context_lost_ = true; |
| 97 | CalcImmediateEntries(0); |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 101 | SetGetBuffer(id, std::move(buffer)); |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | void CommandBufferHelper::SetGetBuffer(int32_t id, |
| 106 | scoped_refptr<Buffer> buffer) { |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 107 | command_buffer_->SetGetBuffer(id); |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 108 | ring_buffer_ = std::move(buffer); |
| 109 | ring_buffer_id_ = id; |
Antoine Labour | d346994 | 2017-05-16 21:23:42 | [diff] [blame] | 110 | ++set_get_buffer_count_; |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 111 | entries_ = ring_buffer_ |
| 112 | ? static_cast<CommandBufferEntry*>(ring_buffer_->memory()) |
| 113 | : 0; |
| 114 | total_entry_count_ = |
| 115 | ring_buffer_ ? ring_buffer_size_ / sizeof(CommandBufferEntry) : 0; |
[email protected] | bae2377 | 2014-04-16 09:50:55 | [diff] [blame] | 116 | // Call to SetGetBuffer(id) above resets get and put offsets to 0. |
| 117 | // No need to query it through IPC. |
| 118 | put_ = 0; |
Sunny Sachanandani | 5acdc81 | 2018-01-25 01:56:07 | [diff] [blame] | 119 | last_flush_put_ = 0; |
| 120 | last_ordering_barrier_put_ = 0; |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 121 | cached_get_offset_ = 0; |
Antoine Labour | d346994 | 2017-05-16 21:23:42 | [diff] [blame] | 122 | service_on_old_buffer_ = true; |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 123 | CalcImmediateEntries(0); |
[email protected] | 617296e | 2011-12-15 05:37:57 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | a5cf3cb | 2012-08-23 01:08:42 | [diff] [blame] | 126 | void CommandBufferHelper::FreeRingBuffer() { |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 127 | if (HaveRingBuffer()) { |
James Darpinian | d1b4ae2 | 2018-06-19 19:49:08 | [diff] [blame] | 128 | OrderingBarrier(); |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 129 | command_buffer_->DestroyTransferBuffer(ring_buffer_id_); |
James Darpinian | d1b4ae2 | 2018-06-19 19:49:08 | [diff] [blame] | 130 | // SetGetBuffer is an IPC, so previous work needs to be flushed first. |
| 131 | Flush(); |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 132 | SetGetBuffer(-1, nullptr); |
| 133 | } |
[email protected] | a5cf3cb | 2012-08-23 01:08:42 | [diff] [blame] | 134 | } |
| 135 | |
Antoine Labour | 5cb6cecb | 2019-01-05 00:47:25 | [diff] [blame] | 136 | gpu::ContextResult CommandBufferHelper::Initialize(uint32_t ring_buffer_size) { |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 137 | ring_buffer_size_ = ring_buffer_size; |
danakj | 45cfd23 | 2017-10-18 19:31:31 | [diff] [blame] | 138 | if (!AllocateRingBuffer()) { |
| 139 | // This would fail if CreateTransferBuffer fails, which will not fail for |
| 140 | // transient reasons such as context loss. See http://crrev.com/c/720269 |
danakj | 514516a | 2017-10-19 20:20:31 | [diff] [blame] | 141 | LOG(ERROR) << "ContextResult::kFatalFailure: " |
| 142 | << "CommandBufferHelper::AllocateRingBuffer() failed"; |
danakj | 45cfd23 | 2017-10-18 19:31:31 | [diff] [blame] | 143 | return gpu::ContextResult::kFatalFailure; |
| 144 | } |
| 145 | return gpu::ContextResult::kSuccess; |
[email protected] | 503b3a2 | 2011-12-12 23:29:40 | [diff] [blame] | 146 | } |
| 147 | |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 148 | CommandBufferHelper::~CommandBufferHelper() { |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 149 | FreeRingBuffer(); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 150 | } |
| 151 | |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 152 | void CommandBufferHelper::UpdateCachedState(const CommandBuffer::State& state) { |
Antoine Labour | d346994 | 2017-05-16 21:23:42 | [diff] [blame] | 153 | // If the service hasn't seen the current get buffer yet (i.e. hasn't |
| 154 | // processed the latest SetGetBuffer), it's as if it hadn't processed anything |
| 155 | // in it, i.e. get == 0. |
| 156 | service_on_old_buffer_ = |
| 157 | (state.set_get_buffer_count != set_get_buffer_count_); |
| 158 | cached_get_offset_ = service_on_old_buffer_ ? 0 : state.get_offset; |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 159 | cached_last_token_read_ = state.token; |
| 160 | context_lost_ = error::IsError(state.error); |
| 161 | } |
| 162 | |
avi | f15d60a | 2015-12-21 17:06:33 | [diff] [blame] | 163 | bool CommandBufferHelper::WaitForGetOffsetInRange(int32_t start, int32_t end) { |
vmiura | 926192c | 2015-12-11 20:10:03 | [diff] [blame] | 164 | DCHECK(start >= 0 && start <= total_entry_count_); |
| 165 | DCHECK(end >= 0 && end <= total_entry_count_); |
Antoine Labour | d346994 | 2017-05-16 21:23:42 | [diff] [blame] | 166 | CommandBuffer::State last_state = command_buffer_->WaitForGetOffsetInRange( |
| 167 | set_get_buffer_count_, start, end); |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 168 | UpdateCachedState(last_state); |
| 169 | return !context_lost_; |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 170 | } |
| 171 | |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 172 | void CommandBufferHelper::Flush() { |
Khushal | 058b1a9 | 2018-02-14 21:25:50 | [diff] [blame] | 173 | TRACE_EVENT0("gpu", "CommandBufferHelper::Flush"); |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 174 | // Wrap put_ before flush. |
| 175 | if (put_ == total_entry_count_) |
| 176 | put_ = 0; |
| 177 | |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 178 | if (HaveRingBuffer()) { |
[email protected] | da618e2 | 2014-07-11 09:27:16 | [diff] [blame] | 179 | last_flush_time_ = base::TimeTicks::Now(); |
Sunny Sachanandani | 5acdc81 | 2018-01-25 01:56:07 | [diff] [blame] | 180 | last_flush_put_ = put_; |
| 181 | last_ordering_barrier_put_ = put_; |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 182 | command_buffer_->Flush(put_); |
[email protected] | cbe0ded | 2014-02-21 20:42:52 | [diff] [blame] | 183 | ++flush_generation_; |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 184 | CalcImmediateEntries(0); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 185 | } |
[email protected] | 7d5b8d1 | 2011-01-14 23:43:15 | [diff] [blame] | 186 | } |
| 187 | |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 188 | void CommandBufferHelper::FlushLazy() { |
Sunny Sachanandani | 5acdc81 | 2018-01-25 01:56:07 | [diff] [blame] | 189 | if (put_ == last_flush_put_ && put_ == last_ordering_barrier_put_) |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 190 | return; |
| 191 | Flush(); |
| 192 | } |
| 193 | |
vmiura | b700b43 | 2015-02-06 16:42:51 | [diff] [blame] | 194 | void CommandBufferHelper::OrderingBarrier() { |
| 195 | // Wrap put_ before setting the barrier. |
| 196 | if (put_ == total_entry_count_) |
| 197 | put_ = 0; |
| 198 | |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 199 | if (HaveRingBuffer()) { |
Sunny Sachanandani | 5acdc81 | 2018-01-25 01:56:07 | [diff] [blame] | 200 | last_ordering_barrier_put_ = put_; |
vmiura | b700b43 | 2015-02-06 16:42:51 | [diff] [blame] | 201 | command_buffer_->OrderingBarrier(put_); |
| 202 | ++flush_generation_; |
| 203 | CalcImmediateEntries(0); |
| 204 | } |
| 205 | } |
| 206 | |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 207 | #if defined(CMD_HELPER_PERIODIC_FLUSH_CHECK) |
| 208 | void CommandBufferHelper::PeriodicFlushCheck() { |
[email protected] | da618e2 | 2014-07-11 09:27:16 | [diff] [blame] | 209 | base::TimeTicks current_time = base::TimeTicks::Now(); |
| 210 | if (current_time - last_flush_time_ > |
| 211 | base::TimeDelta::FromMicroseconds(kPeriodicFlushDelayInMicroseconds)) { |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 212 | Flush(); |
[email protected] | da618e2 | 2014-07-11 09:27:16 | [diff] [blame] | 213 | } |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 214 | } |
| 215 | #endif |
| 216 | |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 217 | // Calls Flush() and then waits until the buffer is empty. Break early if the |
| 218 | // error is set. |
| 219 | bool CommandBufferHelper::Finish() { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 220 | TRACE_EVENT0("gpu", "CommandBufferHelper::Finish"); |
[email protected] | a24e758 | 2012-02-15 23:21:32 | [diff] [blame] | 221 | // If there is no work just exit. |
Antoine Labour | d346994 | 2017-05-16 21:23:42 | [diff] [blame] | 222 | if (put_ == cached_get_offset_ && !service_on_old_buffer_) { |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 223 | return !context_lost_; |
[email protected] | a24e758 | 2012-02-15 23:21:32 | [diff] [blame] | 224 | } |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 225 | FlushLazy(); |
[email protected] | 7fe4198b | 2014-03-18 21:52:36 | [diff] [blame] | 226 | if (!WaitForGetOffsetInRange(put_, put_)) |
| 227 | return false; |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 228 | DCHECK_EQ(cached_get_offset_, put_); |
[email protected] | 7fe4198b | 2014-03-18 21:52:36 | [diff] [blame] | 229 | |
| 230 | CalcImmediateEntries(0); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 231 | |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | // Inserts a new token into the command stream. It uses an increasing value |
| 236 | // scheme so that we don't lose tokens (a token has passed if the current token |
| 237 | // value is higher than that token). Calls Finish() if the token value wraps, |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 238 | // which will be rare. If we can't allocate a command buffer, token doesn't |
| 239 | // increase, ensuring WaitForToken eventually returns. |
avi | f15d60a | 2015-12-21 17:06:33 | [diff] [blame] | 240 | int32_t CommandBufferHelper::InsertToken() { |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 241 | // Increment token as 31-bit integer. Negative values are used to signal an |
| 242 | // error. |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 243 | cmd::SetToken* cmd = GetCmdSpace<cmd::SetToken>(); |
| 244 | if (cmd) { |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 245 | token_ = (token_ + 1) & 0x7FFFFFFF; |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 246 | cmd->Init(token_); |
| 247 | if (token_ == 0) { |
| 248 | TRACE_EVENT0("gpu", "CommandBufferHelper::InsertToken(wrapped)"); |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 249 | bool finished = Finish(); // we wrapped |
| 250 | DCHECK(!finished || (cached_last_token_read_ == 0)); |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 251 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 252 | } |
| 253 | return token_; |
| 254 | } |
| 255 | |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 256 | bool CommandBufferHelper::HasTokenPassed(int32_t token) { |
| 257 | // If token_ wrapped around we Finish'd. |
| 258 | if (token > token_) |
| 259 | return true; |
| 260 | // Don't update state if we don't have to. |
| 261 | if (token <= cached_last_token_read_) |
| 262 | return true; |
Adrienne Walker | 7a78f8c | 2019-08-09 18:35:32 | [diff] [blame^] | 263 | RefreshCachedToken(); |
| 264 | return token <= cached_last_token_read_; |
| 265 | } |
| 266 | |
| 267 | void CommandBufferHelper::RefreshCachedToken() { |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 268 | CommandBuffer::State last_state = command_buffer_->GetLastState(); |
| 269 | UpdateCachedState(last_state); |
Adrienne Walker | 7a78f8c | 2019-08-09 18:35:32 | [diff] [blame^] | 270 | } |
| 271 | |
| 272 | bool CommandBufferHelper::HasCachedTokenPassed(int32_t token) { |
| 273 | if (token > token_) |
| 274 | return true; |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 275 | return token <= cached_last_token_read_; |
| 276 | } |
| 277 | |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 278 | // Waits until the current token value is greater or equal to the value passed |
| 279 | // in argument. |
avi | f15d60a | 2015-12-21 17:06:33 | [diff] [blame] | 280 | void CommandBufferHelper::WaitForToken(int32_t token) { |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 281 | DCHECK_GE(token, 0); |
| 282 | if (HasTokenPassed(token)) |
[email protected] | c448840 | 2012-01-11 01:05:49 | [diff] [blame] | 283 | return; |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 284 | FlushLazy(); |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 285 | CommandBuffer::State last_state = |
| 286 | command_buffer_->WaitForTokenInRange(token, token_); |
| 287 | UpdateCachedState(last_state); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // Waits for available entries, basically waiting until get >= put + count + 1. |
| 291 | // 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] | 292 | // 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] | 293 | // function will return early if an error occurs, in which case the available |
| 294 | // space may not be available. |
avi | f15d60a | 2015-12-21 17:06:33 | [diff] [blame] | 295 | void CommandBufferHelper::WaitForAvailableEntries(int32_t count) { |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 296 | if (!AllocateRingBuffer()) |
[email protected] | 3110b12 | 2013-11-19 23:25:54 | [diff] [blame] | 297 | return; |
[email protected] | 3110b12 | 2013-11-19 23:25:54 | [diff] [blame] | 298 | DCHECK(HaveRingBuffer()); |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 299 | DCHECK(count < total_entry_count_); |
[email protected] | 4725737 | 2013-01-04 18:37:48 | [diff] [blame] | 300 | if (put_ + count > total_entry_count_) { |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 301 | // 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] | 302 | // buffer, so we need to wrap. We will add noops all the way to the end, |
| 303 | // but we need to make sure get wraps first, actually that get is 1 or |
| 304 | // more (since put will wrap to 0 after we add the noops). |
[email protected] | cf1aa98 | 2013-11-05 21:49:37 | [diff] [blame] | 305 | DCHECK_LE(1, put_); |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 306 | int32_t curr_get = cached_get_offset_; |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 307 | if (curr_get > put_ || curr_get == 0) { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 308 | TRACE_EVENT0("gpu", "CommandBufferHelper::WaitForAvailableEntries"); |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 309 | FlushLazy(); |
[email protected] | 7fe4198b | 2014-03-18 21:52:36 | [diff] [blame] | 310 | if (!WaitForGetOffsetInRange(1, put_)) |
| 311 | return; |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 312 | curr_get = cached_get_offset_; |
[email protected] | 7fe4198b | 2014-03-18 21:52:36 | [diff] [blame] | 313 | DCHECK_LE(curr_get, put_); |
| 314 | DCHECK_NE(0, curr_get); |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 315 | } |
[email protected] | 4725737 | 2013-01-04 18:37:48 | [diff] [blame] | 316 | // Insert Noops to fill out the buffer. |
avi | f15d60a | 2015-12-21 17:06:33 | [diff] [blame] | 317 | int32_t num_entries = total_entry_count_ - put_; |
[email protected] | 4725737 | 2013-01-04 18:37:48 | [diff] [blame] | 318 | while (num_entries > 0) { |
avi | f15d60a | 2015-12-21 17:06:33 | [diff] [blame] | 319 | int32_t num_to_skip = std::min(CommandHeader::kMaxSize, num_entries); |
[email protected] | 4725737 | 2013-01-04 18:37:48 | [diff] [blame] | 320 | cmd::Noop::Set(&entries_[put_], num_to_skip); |
| 321 | put_ += num_to_skip; |
| 322 | num_entries -= num_to_skip; |
| 323 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 324 | put_ = 0; |
| 325 | } |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 326 | |
| 327 | // Try to get 'count' entries without flushing. |
| 328 | CalcImmediateEntries(count); |
| 329 | if (immediate_entry_count_ < count) { |
James Darpinian | 62ca13c | 2018-05-31 04:12:06 | [diff] [blame] | 330 | // Update cached_get_offset_ and try again. |
| 331 | UpdateCachedState(command_buffer_->GetLastState()); |
| 332 | CalcImmediateEntries(count); |
| 333 | } |
| 334 | |
| 335 | if (immediate_entry_count_ < count) { |
| 336 | // Try again with a shallow Flush(). Flush can change immediate_entry_count_ |
| 337 | // because of the auto flush logic. |
Antoine Labour | 1a9ef39 | 2017-08-24 19:40:33 | [diff] [blame] | 338 | FlushLazy(); |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 339 | CalcImmediateEntries(count); |
| 340 | if (immediate_entry_count_ < count) { |
| 341 | // Buffer is full. Need to wait for entries. |
| 342 | TRACE_EVENT0("gpu", "CommandBufferHelper::WaitForAvailableEntries1"); |
vmiura | 926192c | 2015-12-11 20:10:03 | [diff] [blame] | 343 | if (!WaitForGetOffsetInRange((put_ + count + 1) % total_entry_count_, |
| 344 | put_)) |
[email protected] | 7fe4198b | 2014-03-18 21:52:36 | [diff] [blame] | 345 | return; |
| 346 | CalcImmediateEntries(count); |
| 347 | DCHECK_GE(immediate_entry_count_, count); |
[email protected] | 15691b4 | 2014-02-12 00:56:00 | [diff] [blame] | 348 | } |
[email protected] | 3110b12 | 2013-11-19 23:25:54 | [diff] [blame] | 349 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 350 | } |
| 351 | |
avi | f15d60a | 2015-12-21 17:06:33 | [diff] [blame] | 352 | int32_t CommandBufferHelper::GetTotalFreeEntriesNoWaiting() const { |
sunnyps | 12856605 | 2016-12-09 21:06:43 | [diff] [blame] | 353 | int32_t current_get_offset = cached_get_offset_; |
reveman | 0cf65ee8 | 2015-08-25 22:15:24 | [diff] [blame] | 354 | if (current_get_offset > put_) { |
| 355 | return current_get_offset - put_ - 1; |
| 356 | } else { |
| 357 | return current_get_offset + total_entry_count_ - put_ - |
| 358 | (current_get_offset == 0 ? 1 : 0); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | bool CommandBufferHelper::OnMemoryDump( |
| 363 | const base::trace_event::MemoryDumpArgs& args, |
| 364 | base::trace_event::ProcessMemoryDump* pmd) { |
ericrk | eff77698 | 2016-11-03 21:37:31 | [diff] [blame] | 365 | using base::trace_event::MemoryAllocatorDump; |
| 366 | using base::trace_event::MemoryDumpLevelOfDetail; |
| 367 | |
reveman | 0cf65ee8 | 2015-08-25 22:15:24 | [diff] [blame] | 368 | if (!HaveRingBuffer()) |
| 369 | return true; |
| 370 | |
avi | f15d60a | 2015-12-21 17:06:33 | [diff] [blame] | 371 | const uint64_t tracing_process_id = |
reveman | 0cf65ee8 | 2015-08-25 22:15:24 | [diff] [blame] | 372 | base::trace_event::MemoryDumpManager::GetInstance() |
| 373 | ->GetTracingProcessId(); |
| 374 | |
ericrk | eff77698 | 2016-11-03 21:37:31 | [diff] [blame] | 375 | MemoryAllocatorDump* dump = pmd->CreateAllocatorDump(base::StringPrintf( |
| 376 | "gpu/command_buffer_memory/buffer_%d", ring_buffer_id_)); |
| 377 | dump->AddScalar(MemoryAllocatorDump::kNameSize, |
| 378 | MemoryAllocatorDump::kUnitsBytes, ring_buffer_size_); |
| 379 | |
| 380 | if (args.level_of_detail != MemoryDumpLevelOfDetail::BACKGROUND) { |
| 381 | dump->AddScalar( |
| 382 | "free_size", MemoryAllocatorDump::kUnitsBytes, |
| 383 | GetTotalFreeEntriesNoWaiting() * sizeof(CommandBufferEntry)); |
Hajime Hoshi | 35e4fd7 | 2017-06-12 04:21:23 | [diff] [blame] | 384 | base::UnguessableToken shared_memory_guid = |
Alexandr Ilin | 15bb703 | 2018-07-13 10:09:06 | [diff] [blame] | 385 | ring_buffer_->backing()->GetGUID(); |
ericrk | eff77698 | 2016-11-03 21:37:31 | [diff] [blame] | 386 | const int kImportance = 2; |
Hajime Hoshi | 35e4fd7 | 2017-06-12 04:21:23 | [diff] [blame] | 387 | if (!shared_memory_guid.is_empty()) { |
Hajime Hoshi | 841a2089 | 2017-08-16 10:18:47 | [diff] [blame] | 388 | pmd->CreateSharedMemoryOwnershipEdge(dump->guid(), shared_memory_guid, |
| 389 | kImportance); |
Hajime Hoshi | 35e4fd7 | 2017-06-12 04:21:23 | [diff] [blame] | 390 | } else { |
Hajime Hoshi | 841a2089 | 2017-08-16 10:18:47 | [diff] [blame] | 391 | auto guid = GetBufferGUIDForTracing(tracing_process_id, ring_buffer_id_); |
Hajime Hoshi | 35e4fd7 | 2017-06-12 04:21:23 | [diff] [blame] | 392 | pmd->CreateSharedGlobalAllocatorDump(guid); |
| 393 | pmd->AddOwnershipEdge(dump->guid(), guid, kImportance); |
| 394 | } |
ericrk | eff77698 | 2016-11-03 21:37:31 | [diff] [blame] | 395 | } |
reveman | 0cf65ee8 | 2015-08-25 22:15:24 | [diff] [blame] | 396 | return true; |
| 397 | } |
[email protected] | 96449d2c | 2009-11-25 00:01:32 | [diff] [blame] | 398 | |
[email protected] | a7a27ace | 2009-12-12 00:11:25 | [diff] [blame] | 399 | } // namespace gpu |