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