| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 command buffer helper class. | 5 // This file contains the command buffer helper class. |
| 6 | 6 |
| 7 #ifndef GPU_COMMAND_BUFFER_CLIENT_CMD_BUFFER_HELPER_H_ | 7 #ifndef GPU_COMMAND_BUFFER_CLIENT_CMD_BUFFER_HELPER_H_ |
| 8 #define GPU_COMMAND_BUFFER_CLIENT_CMD_BUFFER_HELPER_H_ | 8 #define GPU_COMMAND_BUFFER_CLIENT_CMD_BUFFER_HELPER_H_ |
| 9 | 9 |
| 10 #include <stddef.h> | 10 #include <stddef.h> |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 // inserted tokens with that value have already passed through the command | 95 // inserted tokens with that value have already passed through the command |
| 96 // stream. | 96 // stream. |
| 97 // Returns: | 97 // Returns: |
| 98 // the value of the new token or -1 if the command buffer reader has | 98 // the value of the new token or -1 if the command buffer reader has |
| 99 // shutdown. | 99 // shutdown. |
| 100 int32_t InsertToken(); | 100 int32_t InsertToken(); |
| 101 | 101 |
| 102 // Returns true if the token has passed. | 102 // Returns true if the token has passed. |
| 103 // Parameters: | 103 // Parameters: |
| 104 // the value of the token to check whether it has passed | 104 // the value of the token to check whether it has passed |
| 105 bool HasTokenPassed(int32_t token) const { | 105 bool HasTokenPassed(int32_t token); |
| 106 if (token > token_) | |
| 107 return true; // we wrapped | |
| 108 return last_token_read() >= token; | |
| 109 } | |
| 110 | 106 |
| 111 // Waits until the token of a particular value has passed through the command | 107 // Waits until the token of a particular value has passed through the command |
| 112 // stream (i.e. commands inserted before that token have been executed). | 108 // stream (i.e. commands inserted before that token have been executed). |
| 113 // NOTE: This will call Flush if it needs to block. | 109 // NOTE: This will call Flush if it needs to block. |
| 114 // Parameters: | 110 // Parameters: |
| 115 // the value of the token to wait for. | 111 // the value of the token to wait for. |
| 116 void WaitForToken(int32_t token); | 112 void WaitForToken(int32_t token); |
| 117 | 113 |
| 118 // Called prior to each command being issued. Waits for a certain amount of | 114 // Called prior to each command being issued. Waits for a certain amount of |
| 119 // space to be available. Returns address of space. | 115 // space to be available. Returns address of space. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 // Typed version of GetSpace for immediate commands. | 168 // Typed version of GetSpace for immediate commands. |
| 173 template <typename T> | 169 template <typename T> |
| 174 T* GetImmediateCmdSpaceTotalSize(size_t total_space) { | 170 T* GetImmediateCmdSpaceTotalSize(size_t total_space) { |
| 175 static_assert(T::kArgFlags == cmd::kAtLeastN, | 171 static_assert(T::kArgFlags == cmd::kAtLeastN, |
| 176 "T::kArgFlags should equal cmd::kAtLeastN"); | 172 "T::kArgFlags should equal cmd::kAtLeastN"); |
| 177 int32_t space_needed = ComputeNumEntries(total_space); | 173 int32_t space_needed = ComputeNumEntries(total_space); |
| 178 T* data = static_cast<T*>(GetSpace(space_needed)); | 174 T* data = static_cast<T*>(GetSpace(space_needed)); |
| 179 return data; | 175 return data; |
| 180 } | 176 } |
| 181 | 177 |
| 182 int32_t last_token_read() const { return command_buffer_->GetLastToken(); } | |
| 183 | |
| 184 int32_t get_offset() const { | |
| 185 return command_buffer_->GetLastState().get_offset; | |
| 186 } | |
| 187 | |
| 188 // Common Commands | 178 // Common Commands |
| 189 void Noop(uint32_t skip_count) { | 179 void Noop(uint32_t skip_count) { |
| 190 cmd::Noop* cmd = GetImmediateCmdSpace<cmd::Noop>( | 180 cmd::Noop* cmd = GetImmediateCmdSpace<cmd::Noop>( |
| 191 (skip_count - 1) * sizeof(CommandBufferEntry)); | 181 (skip_count - 1) * sizeof(CommandBufferEntry)); |
| 192 if (cmd) { | 182 if (cmd) { |
| 193 cmd->Init(skip_count); | 183 cmd->Init(skip_count); |
| 194 } | 184 } |
| 195 } | 185 } |
| 196 | 186 |
| 197 void SetToken(uint32_t token) { | 187 void SetToken(uint32_t token) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 208 } | 198 } |
| 209 } | 199 } |
| 210 | 200 |
| 211 void SetBucketData(uint32_t bucket_id, | 201 void SetBucketData(uint32_t bucket_id, |
| 212 uint32_t offset, | 202 uint32_t offset, |
| 213 uint32_t size, | 203 uint32_t size, |
| 214 uint32_t shared_memory_id, | 204 uint32_t shared_memory_id, |
| 215 uint32_t shared_memory_offset) { | 205 uint32_t shared_memory_offset) { |
| 216 cmd::SetBucketData* cmd = GetCmdSpace<cmd::SetBucketData>(); | 206 cmd::SetBucketData* cmd = GetCmdSpace<cmd::SetBucketData>(); |
| 217 if (cmd) { | 207 if (cmd) { |
| 218 cmd->Init(bucket_id, | 208 cmd->Init(bucket_id, offset, size, shared_memory_id, |
| 219 offset, | |
| 220 size, | |
| 221 shared_memory_id, | |
| 222 shared_memory_offset); | 209 shared_memory_offset); |
| 223 } | 210 } |
| 224 } | 211 } |
| 225 | 212 |
| 226 void SetBucketDataImmediate(uint32_t bucket_id, | 213 void SetBucketDataImmediate(uint32_t bucket_id, |
| 227 uint32_t offset, | 214 uint32_t offset, |
| 228 const void* data, | 215 const void* data, |
| 229 uint32_t size) { | 216 uint32_t size) { |
| 230 cmd::SetBucketDataImmediate* cmd = | 217 cmd::SetBucketDataImmediate* cmd = |
| 231 GetImmediateCmdSpace<cmd::SetBucketDataImmediate>(size); | 218 GetImmediateCmdSpace<cmd::SetBucketDataImmediate>(size); |
| 232 if (cmd) { | 219 if (cmd) { |
| 233 cmd->Init(bucket_id, offset, size); | 220 cmd->Init(bucket_id, offset, size); |
| 234 memcpy(ImmediateDataAddress(cmd), data, size); | 221 memcpy(ImmediateDataAddress(cmd), data, size); |
| 235 } | 222 } |
| 236 } | 223 } |
| 237 | 224 |
| 238 void GetBucketStart(uint32_t bucket_id, | 225 void GetBucketStart(uint32_t bucket_id, |
| 239 uint32_t result_memory_id, | 226 uint32_t result_memory_id, |
| 240 uint32_t result_memory_offset, | 227 uint32_t result_memory_offset, |
| 241 uint32_t data_memory_size, | 228 uint32_t data_memory_size, |
| 242 uint32_t data_memory_id, | 229 uint32_t data_memory_id, |
| 243 uint32_t data_memory_offset) { | 230 uint32_t data_memory_offset) { |
| 244 cmd::GetBucketStart* cmd = GetCmdSpace<cmd::GetBucketStart>(); | 231 cmd::GetBucketStart* cmd = GetCmdSpace<cmd::GetBucketStart>(); |
| 245 if (cmd) { | 232 if (cmd) { |
| 246 cmd->Init(bucket_id, | 233 cmd->Init(bucket_id, result_memory_id, result_memory_offset, |
| 247 result_memory_id, | 234 data_memory_size, data_memory_id, data_memory_offset); |
| 248 result_memory_offset, | |
| 249 data_memory_size, | |
| 250 data_memory_id, | |
| 251 data_memory_offset); | |
| 252 } | 235 } |
| 253 } | 236 } |
| 254 | 237 |
| 255 void GetBucketData(uint32_t bucket_id, | 238 void GetBucketData(uint32_t bucket_id, |
| 256 uint32_t offset, | 239 uint32_t offset, |
| 257 uint32_t size, | 240 uint32_t size, |
| 258 uint32_t shared_memory_id, | 241 uint32_t shared_memory_id, |
| 259 uint32_t shared_memory_offset) { | 242 uint32_t shared_memory_offset) { |
| 260 cmd::GetBucketData* cmd = GetCmdSpace<cmd::GetBucketData>(); | 243 cmd::GetBucketData* cmd = GetCmdSpace<cmd::GetBucketData>(); |
| 261 if (cmd) { | 244 if (cmd) { |
| 262 cmd->Init(bucket_id, | 245 cmd->Init(bucket_id, offset, size, shared_memory_id, |
| 263 offset, | |
| 264 size, | |
| 265 shared_memory_id, | |
| 266 shared_memory_offset); | 246 shared_memory_offset); |
| 267 } | 247 } |
| 268 } | 248 } |
| 269 | 249 |
| 270 CommandBuffer* command_buffer() const { | 250 CommandBuffer* command_buffer() const { return command_buffer_; } |
| 271 return command_buffer_; | |
| 272 } | |
| 273 | 251 |
| 274 scoped_refptr<Buffer> get_ring_buffer() const { return ring_buffer_; } | 252 scoped_refptr<Buffer> get_ring_buffer() const { return ring_buffer_; } |
| 275 | 253 |
| 276 uint32_t flush_generation() const { return flush_generation_; } | 254 uint32_t flush_generation() const { return flush_generation_; } |
| 277 | 255 |
| 278 void FreeRingBuffer(); | 256 void FreeRingBuffer(); |
| 279 | 257 |
| 280 bool HaveRingBuffer() const { | 258 bool HaveRingBuffer() const { return ring_buffer_id_ != -1; } |
| 281 return ring_buffer_id_ != -1; | |
| 282 } | |
| 283 | 259 |
| 284 bool usable () const { | 260 bool usable() const { return usable_; } |
| 285 return usable_; | |
| 286 } | |
| 287 | 261 |
| 288 void ClearUsable() { | 262 void ClearUsable() { |
| 289 usable_ = false; | 263 usable_ = false; |
| 290 context_lost_ = true; | 264 context_lost_ = true; |
| 291 CalcImmediateEntries(0); | 265 CalcImmediateEntries(0); |
| 292 } | 266 } |
| 293 | 267 |
| 294 // Overridden from base::trace_event::MemoryDumpProvider: | 268 // Overridden from base::trace_event::MemoryDumpProvider: |
| 295 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, | 269 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, |
| 296 base::trace_event::ProcessMemoryDump* pmd) override; | 270 base::trace_event::ProcessMemoryDump* pmd) override; |
| 297 | 271 |
| 298 private: | 272 private: |
| 299 // Returns the number of available entries (they may not be contiguous). | |
| 300 int32_t AvailableEntries() { | |
| 301 return (get_offset() - put_ - 1 + total_entry_count_) % total_entry_count_; | |
| 302 } | |
| 303 | |
| 304 void CalcImmediateEntries(int waiting_count); | 273 void CalcImmediateEntries(int waiting_count); |
| 305 bool AllocateRingBuffer(); | 274 bool AllocateRingBuffer(); |
| 306 void FreeResources(); | 275 void FreeResources(); |
| 307 | 276 |
| 308 // Waits for the get offset to be in a specific range, inclusive. Returns | 277 // Waits for the get offset to be in a specific range, inclusive. Returns |
| 309 // false if there was an error. | 278 // false if there was an error. |
| 310 bool WaitForGetOffsetInRange(int32_t start, int32_t end); | 279 bool WaitForGetOffsetInRange(int32_t start, int32_t end); |
| 311 | 280 |
| 312 #if defined(CMD_HELPER_PERIODIC_FLUSH_CHECK) | 281 #if defined(CMD_HELPER_PERIODIC_FLUSH_CHECK) |
| 313 // Calls Flush if automatic flush conditions are met. | 282 // Calls Flush if automatic flush conditions are met. |
| 314 void PeriodicFlushCheck(); | 283 void PeriodicFlushCheck(); |
| 315 #endif | 284 #endif |
| 316 | 285 |
| 317 int32_t GetTotalFreeEntriesNoWaiting() const; | 286 int32_t GetTotalFreeEntriesNoWaiting() const; |
| 318 | 287 |
| 288 // Updates |cached_get_offset_|, |cached_last_token_read_| and |context_lost_| |
| 289 // from given command buffer state. |
| 290 void UpdateCachedState(const CommandBuffer::State& state); |
| 291 |
| 319 CommandBuffer* command_buffer_; | 292 CommandBuffer* command_buffer_; |
| 320 int32_t ring_buffer_id_; | 293 int32_t ring_buffer_id_; |
| 321 int32_t ring_buffer_size_; | 294 int32_t ring_buffer_size_; |
| 322 scoped_refptr<gpu::Buffer> ring_buffer_; | 295 scoped_refptr<gpu::Buffer> ring_buffer_; |
| 323 CommandBufferEntry* entries_; | 296 CommandBufferEntry* entries_; |
| 324 int32_t total_entry_count_; // the total number of entries | 297 int32_t total_entry_count_; // the total number of entries |
| 325 int32_t immediate_entry_count_; | 298 int32_t immediate_entry_count_; |
| 326 int32_t token_; | 299 int32_t token_; |
| 327 int32_t put_; | 300 int32_t put_; |
| 328 int32_t last_put_sent_; | 301 int32_t last_put_sent_; |
| 329 int32_t last_barrier_put_sent_; | 302 int32_t cached_last_token_read_; |
| 303 int32_t cached_get_offset_; |
| 330 | 304 |
| 331 #if defined(CMD_HELPER_PERIODIC_FLUSH_CHECK) | 305 #if defined(CMD_HELPER_PERIODIC_FLUSH_CHECK) |
| 332 int commands_issued_; | 306 int commands_issued_; |
| 333 #endif | 307 #endif |
| 334 | 308 |
| 335 bool usable_; | 309 bool usable_; |
| 336 bool context_lost_; | 310 bool context_lost_; |
| 337 bool flush_automatically_; | 311 bool flush_automatically_; |
| 338 | 312 |
| 339 base::TimeTicks last_flush_time_; | 313 base::TimeTicks last_flush_time_; |
| 340 | 314 |
| 341 // Incremented every time the helper flushes the command buffer. | 315 // Incremented every time the helper flushes the command buffer. |
| 342 // Can be used to track when prior commands have been flushed. | 316 // Can be used to track when prior commands have been flushed. |
| 343 uint32_t flush_generation_; | 317 uint32_t flush_generation_; |
| 344 | 318 |
| 345 friend class CommandBufferHelperTest; | 319 friend class CommandBufferHelperTest; |
| 346 DISALLOW_COPY_AND_ASSIGN(CommandBufferHelper); | 320 DISALLOW_COPY_AND_ASSIGN(CommandBufferHelper); |
| 347 }; | 321 }; |
| 348 | 322 |
| 349 } // namespace gpu | 323 } // namespace gpu |
| 350 | 324 |
| 351 #endif // GPU_COMMAND_BUFFER_CLIENT_CMD_BUFFER_HELPER_H_ | 325 #endif // GPU_COMMAND_BUFFER_CLIENT_CMD_BUFFER_HELPER_H_ |
| OLD | NEW |