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