OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file contains the implementation of the command buffer helper class. | 5 // This file contains the implementation of the command buffer helper class. |
6 | 6 |
7 #include "../client/cmd_buffer_helper.h" | 7 #include "../client/cmd_buffer_helper.h" |
8 #include "../common/command_buffer.h" | 8 #include "../common/command_buffer.h" |
9 #include "../common/trace_event.h" | 9 #include "../common/trace_event.h" |
10 | 10 |
11 namespace gpu { | 11 namespace gpu { |
12 | 12 |
13 namespace { | 13 namespace { |
14 const int kCommandsPerFlushCheck = 100; | 14 const int kCommandsPerFlushCheck = 100; |
15 const double kFlushDelay = 1.0 / (5.0 * 60.0); | 15 const double kFlushDelay = 1.0 / (5.0 * 60.0); |
16 } | 16 } |
17 | 17 |
18 CommandBufferHelper::CommandBufferHelper(CommandBuffer* command_buffer) | 18 CommandBufferHelper::CommandBufferHelper(CommandBuffer* command_buffer) |
19 : command_buffer_(command_buffer), | 19 : command_buffer_(command_buffer), |
20 entries_(NULL), | 20 entries_(NULL), |
21 total_entry_count_(0), | 21 total_entry_count_(0), |
22 usable_entry_count_(0), | 22 usable_entry_count_(0), |
23 token_(0), | 23 token_(0), |
| 24 last_token_read_(-1), |
| 25 get_(0), |
24 put_(0), | 26 put_(0), |
25 last_put_sent_(0), | 27 last_put_sent_(0), |
26 commands_issued_(0), | 28 commands_issued_(0), |
27 last_flush_time_(0) { | 29 last_flush_time_(0) { |
28 } | 30 } |
29 | 31 |
30 bool CommandBufferHelper::Initialize(int32 ring_buffer_size) { | 32 bool CommandBufferHelper::Initialize(int32 ring_buffer_size) { |
31 ring_buffer_ = command_buffer_->GetRingBuffer(); | 33 ring_buffer_ = command_buffer_->GetRingBuffer(); |
32 if (!ring_buffer_.ptr) | 34 if (!ring_buffer_.ptr) |
33 return false; | 35 return false; |
34 | 36 |
35 CommandBuffer::State state = command_buffer_->GetState(); | 37 CommandBuffer::State state = command_buffer_->GetState(); |
36 entries_ = static_cast<CommandBufferEntry*>(ring_buffer_.ptr); | 38 entries_ = static_cast<CommandBufferEntry*>(ring_buffer_.ptr); |
37 int32 num_ring_buffer_entries = ring_buffer_size / sizeof(CommandBufferEntry); | 39 int32 num_ring_buffer_entries = ring_buffer_size / sizeof(CommandBufferEntry); |
38 if (num_ring_buffer_entries > state.num_entries) { | 40 if (num_ring_buffer_entries > state.num_entries) { |
39 return false; | 41 return false; |
40 } | 42 } |
41 | 43 |
42 const int32 kJumpEntries = | 44 const int32 kJumpEntries = |
43 sizeof(cmd::Jump) / sizeof(*entries_); // NOLINT | 45 sizeof(cmd::Jump) / sizeof(*entries_); // NOLINT |
44 | 46 |
45 total_entry_count_ = num_ring_buffer_entries; | 47 total_entry_count_ = num_ring_buffer_entries; |
46 usable_entry_count_ = total_entry_count_ - kJumpEntries; | 48 usable_entry_count_ = total_entry_count_ - kJumpEntries; |
47 put_ = state.put_offset; | 49 put_ = state.put_offset; |
| 50 SynchronizeState(state); |
48 return true; | 51 return true; |
49 } | 52 } |
50 | 53 |
51 CommandBufferHelper::~CommandBufferHelper() { | 54 CommandBufferHelper::~CommandBufferHelper() { |
52 } | 55 } |
53 | 56 |
54 bool CommandBufferHelper::FlushSync() { | 57 bool CommandBufferHelper::FlushSync() { |
55 time(&last_flush_time_); | 58 time(&last_flush_time_); |
56 last_put_sent_ = put_; | 59 last_put_sent_ = put_; |
57 CommandBuffer::State state = command_buffer_->FlushSync(put_, get_offset()); | 60 CommandBuffer::State state = command_buffer_->FlushSync(put_, get_); |
| 61 SynchronizeState(state); |
58 return state.error == error::kNoError; | 62 return state.error == error::kNoError; |
59 } | 63 } |
60 | 64 |
61 void CommandBufferHelper::Flush() { | 65 void CommandBufferHelper::Flush() { |
62 time(&last_flush_time_); | 66 time(&last_flush_time_); |
63 last_put_sent_ = put_; | 67 last_put_sent_ = put_; |
64 command_buffer_->Flush(put_); | 68 command_buffer_->Flush(put_); |
65 } | 69 } |
66 | 70 |
67 // Calls Flush() and then waits until the buffer is empty. Break early if the | 71 // Calls Flush() and then waits until the buffer is empty. Break early if the |
68 // error is set. | 72 // error is set. |
69 bool CommandBufferHelper::Finish() { | 73 bool CommandBufferHelper::Finish() { |
70 TRACE_EVENT0("gpu", "CommandBufferHelper::Finish"); | 74 TRACE_EVENT0("gpu", "CommandBufferHelper::Finish"); |
71 do { | 75 do { |
72 // Do not loop forever if the flush fails, meaning the command buffer reader | 76 // Do not loop forever if the flush fails, meaning the command buffer reader |
73 // has shutdown. | 77 // has shutdown. |
74 if (!FlushSync()) | 78 if (!FlushSync()) |
75 return false; | 79 return false; |
76 } while (put_ != get_offset()); | 80 } while (put_ != get_); |
77 | 81 |
78 return true; | 82 return true; |
79 } | 83 } |
80 | 84 |
81 // Inserts a new token into the command stream. It uses an increasing value | 85 // Inserts a new token into the command stream. It uses an increasing value |
82 // scheme so that we don't lose tokens (a token has passed if the current token | 86 // scheme so that we don't lose tokens (a token has passed if the current token |
83 // value is higher than that token). Calls Finish() if the token value wraps, | 87 // value is higher than that token). Calls Finish() if the token value wraps, |
84 // which will be rare. | 88 // which will be rare. |
85 int32 CommandBufferHelper::InsertToken() { | 89 int32 CommandBufferHelper::InsertToken() { |
86 // Increment token as 31-bit integer. Negative values are used to signal an | 90 // Increment token as 31-bit integer. Negative values are used to signal an |
87 // error. | 91 // error. |
88 token_ = (token_ + 1) & 0x7FFFFFFF; | 92 token_ = (token_ + 1) & 0x7FFFFFFF; |
89 cmd::SetToken& cmd = GetCmdSpace<cmd::SetToken>(); | 93 cmd::SetToken& cmd = GetCmdSpace<cmd::SetToken>(); |
90 cmd.Init(token_); | 94 cmd.Init(token_); |
91 if (token_ == 0) { | 95 if (token_ == 0) { |
92 TRACE_EVENT0("gpu", "CommandBufferHelper::InsertToken(wrapped)"); | 96 TRACE_EVENT0("gpu", "CommandBufferHelper::InsertToken(wrapped)"); |
93 // we wrapped | 97 // we wrapped |
94 Finish(); | 98 Finish(); |
95 GPU_DCHECK_EQ(token_, last_token_read()); | 99 GPU_DCHECK_EQ(token_, last_token_read_); |
96 } | 100 } |
97 return token_; | 101 return token_; |
98 } | 102 } |
99 | 103 |
100 // Waits until the current token value is greater or equal to the value passed | 104 // Waits until the current token value is greater or equal to the value passed |
101 // in argument. | 105 // in argument. |
102 void CommandBufferHelper::WaitForToken(int32 token) { | 106 void CommandBufferHelper::WaitForToken(int32 token) { |
103 TRACE_EVENT_IF_LONGER_THAN0(50, "gpu", "CommandBufferHelper::WaitForToken"); | 107 TRACE_EVENT_IF_LONGER_THAN0(50, "gpu", "CommandBufferHelper::WaitForToken"); |
104 // Return immediately if corresponding InsertToken failed. | 108 // Return immediately if corresponding InsertToken failed. |
105 if (token < 0) | 109 if (token < 0) |
106 return; | 110 return; |
107 if (token > token_) return; // we wrapped | 111 if (token > token_) return; // we wrapped |
108 while (last_token_read() < token) { | 112 while (last_token_read_ < token) { |
109 if (get_offset() == put_) { | 113 if (get_ == put_) { |
110 GPU_LOG(FATAL) << "Empty command buffer while waiting on a token."; | 114 GPU_LOG(FATAL) << "Empty command buffer while waiting on a token."; |
111 return; | 115 return; |
112 } | 116 } |
113 // Do not loop forever if the flush fails, meaning the command buffer reader | 117 // Do not loop forever if the flush fails, meaning the command buffer reader |
114 // has shutdown. | 118 // has shutdown. |
115 if (!FlushSync()) | 119 if (!FlushSync()) |
116 return; | 120 return; |
117 } | 121 } |
118 } | 122 } |
119 | 123 |
| 124 void CommandBufferHelper::YieldScheduler() { |
| 125 cmd::YieldScheduler& cmd = GetCmdSpace<cmd::YieldScheduler>(); |
| 126 cmd.Init(); |
| 127 } |
| 128 |
120 // Waits for available entries, basically waiting until get >= put + count + 1. | 129 // Waits for available entries, basically waiting until get >= put + count + 1. |
121 // It actually waits for contiguous entries, so it may need to wrap the buffer | 130 // It actually waits for contiguous entries, so it may need to wrap the buffer |
122 // around, adding a jump. Thus this function may change the value of put_. The | 131 // around, adding a jump. Thus this function may change the value of put_. The |
123 // function will return early if an error occurs, in which case the available | 132 // function will return early if an error occurs, in which case the available |
124 // space may not be available. | 133 // space may not be available. |
125 void CommandBufferHelper::WaitForAvailableEntries(int32 count) { | 134 void CommandBufferHelper::WaitForAvailableEntries(int32 count) { |
126 GPU_DCHECK(count < usable_entry_count_); | 135 GPU_DCHECK(count < usable_entry_count_); |
127 if (put_ + count > usable_entry_count_) { | 136 if (put_ + count > usable_entry_count_) { |
128 // There's not enough room between the current put and the end of the | 137 // There's not enough room between the current put and the end of the |
129 // buffer, so we need to wrap. We will add a jump back to the start, but we | 138 // buffer, so we need to wrap. We will add a jump back to the start, but we |
130 // need to make sure get wraps first, actually that get is 1 or more (since | 139 // need to make sure get wraps first, actually that get is 1 or more (since |
131 // put will wrap to 0 after we add the jump). | 140 // put will wrap to 0 after we add the jump). |
132 GPU_DCHECK_LE(1, put_); | 141 GPU_DCHECK_LE(1, put_); |
133 if (get_offset() > put_ || get_offset() == 0) { | 142 if (get_ > put_ || get_ == 0) { |
134 TRACE_EVENT0("gpu", "CommandBufferHelper::WaitForAvailableEntries"); | 143 TRACE_EVENT0("gpu", "CommandBufferHelper::WaitForAvailableEntries"); |
135 while (get_offset() > put_ || get_offset() == 0) { | 144 while (get_ > put_ || get_ == 0) { |
136 // Do not loop forever if the flush fails, meaning the command buffer | 145 // Do not loop forever if the flush fails, meaning the command buffer |
137 // reader has shutdown. | 146 // reader has shutdown. |
138 if (!FlushSync()) | 147 if (!FlushSync()) |
139 return; | 148 return; |
140 } | 149 } |
141 } | 150 } |
142 // Insert a jump back to the beginning. | 151 // Insert a jump back to the beginning. |
143 cmd::Jump::Set(&entries_[put_], 0); | 152 cmd::Jump::Set(&entries_[put_], 0); |
144 put_ = 0; | 153 put_ = 0; |
145 } | 154 } |
(...skipping 23 matching lines...) Expand all Loading... |
169 GPU_DCHECK_LE(put_, usable_entry_count_); | 178 GPU_DCHECK_LE(put_, usable_entry_count_); |
170 if (put_ == usable_entry_count_) { | 179 if (put_ == usable_entry_count_) { |
171 cmd::Jump::Set(&entries_[put_], 0); | 180 cmd::Jump::Set(&entries_[put_], 0); |
172 put_ = 0; | 181 put_ = 0; |
173 } | 182 } |
174 return space; | 183 return space; |
175 } | 184 } |
176 | 185 |
177 error::Error CommandBufferHelper::GetError() { | 186 error::Error CommandBufferHelper::GetError() { |
178 CommandBuffer::State state = command_buffer_->GetState(); | 187 CommandBuffer::State state = command_buffer_->GetState(); |
| 188 SynchronizeState(state); |
179 return static_cast<error::Error>(state.error); | 189 return static_cast<error::Error>(state.error); |
180 } | 190 } |
181 | 191 |
| 192 void CommandBufferHelper::SynchronizeState(const CommandBuffer::State& state) { |
| 193 get_ = state.get_offset; |
| 194 last_token_read_ = state.token; |
| 195 } |
| 196 |
182 } // namespace gpu | 197 } // namespace gpu |
OLD | NEW |