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 3 // found in the LICENSE file. 4 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_COMMAND_BUFFER_SERVICE_H_ 6 #define GPU_COMMAND_BUFFER_SERVICE_COMMAND_BUFFER_SERVICE_H_ 7 8 #include "base/callback.h" 9 #include "base/memory/shared_memory.h" 10 #include "gpu/command_buffer/common/command_buffer.h" 11 #include "gpu/command_buffer/common/command_buffer_shared.h" 12 13 namespace gpu { 14 15 class TransferBufferManagerInterface; 16 17 class GPU_EXPORT CommandBufferServiceBase : public CommandBuffer { 18 public: 19 // Sets the current get offset. This can be called from any thread. 20 virtual void SetGetOffset(int32 get_offset) = 0; 21 22 // Get the transfer buffer associated with an ID. Returns a null buffer for 23 // ID 0. 24 virtual scoped_refptr<gpu::Buffer> GetTransferBuffer(int32 id) = 0; 25 26 // Allows the reader to update the current token value. 27 virtual void SetToken(int32 token) = 0; 28 29 // Allows the reader to set the current parse error. 30 virtual void SetParseError(error::Error) = 0; 31 32 // Allows the reader to set the current context lost reason. 33 // NOTE: if calling this in conjunction with SetParseError, 34 // call this first. 35 virtual void SetContextLostReason(error::ContextLostReason) = 0; 36 }; 37 38 // An object that implements a shared memory command buffer and a synchronous 39 // API to manage the put and get pointers. 40 class GPU_EXPORT CommandBufferService : public CommandBufferServiceBase { 41 public: 42 typedef base::Callback<bool(int32)> GetBufferChangedCallback; 43 explicit CommandBufferService( 44 TransferBufferManagerInterface* transfer_buffer_manager); 45 virtual ~CommandBufferService(); 46 47 // CommandBuffer implementation: 48 virtual bool Initialize() OVERRIDE; 49 virtual State GetLastState() OVERRIDE; 50 virtual int32 GetLastToken() OVERRIDE; 51 virtual void Flush(int32 put_offset) OVERRIDE; 52 virtual void WaitForTokenInRange(int32 start, int32 end) OVERRIDE; 53 virtual void WaitForGetOffsetInRange(int32 start, int32 end) OVERRIDE; 54 virtual void SetGetBuffer(int32 transfer_buffer_id) OVERRIDE; 55 virtual scoped_refptr<Buffer> CreateTransferBuffer(size_t size, 56 int32* id) OVERRIDE; 57 virtual void DestroyTransferBuffer(int32 id) OVERRIDE; 58 59 // CommandBufferServiceBase implementation: 60 virtual void SetGetOffset(int32 get_offset) OVERRIDE; 61 virtual scoped_refptr<Buffer> GetTransferBuffer(int32 id) OVERRIDE; 62 virtual void SetToken(int32 token) OVERRIDE; 63 virtual void SetParseError(error::Error error) OVERRIDE; 64 virtual void SetContextLostReason(error::ContextLostReason) OVERRIDE; 65 66 // Sets a callback that is called whenever the put offset is changed. When 67 // called with sync==true, the callback must not return until some progress 68 // has been made (unless the command buffer is empty), i.e. the get offset 69 // must have changed. It need not process the entire command buffer though. 70 // This allows concurrency between the writer and the reader while giving the 71 // writer a means of waiting for the reader to make some progress before 72 // attempting to write more to the command buffer. Takes ownership of 73 // callback. 74 virtual void SetPutOffsetChangeCallback(const base::Closure& callback); 75 // Sets a callback that is called whenever the get buffer is changed. 76 virtual void SetGetBufferChangeCallback( 77 const GetBufferChangedCallback& callback); 78 virtual void SetParseErrorCallback(const base::Closure& callback); 79 80 // Setup the shared memory that shared state should be copied into. 81 void SetSharedStateBuffer(scoped_ptr<BufferBacking> shared_state_buffer); 82 83 // Copy the current state into the shared state transfer buffer. 84 void UpdateState(); 85 86 // Registers an existing shared memory object and get an ID that can be used 87 // to identify it in the command buffer. 88 bool RegisterTransferBuffer(int32 id, scoped_ptr<BufferBacking> buffer); 89 90 private: 91 int32 ring_buffer_id_; 92 scoped_refptr<Buffer> ring_buffer_; 93 scoped_ptr<BufferBacking> shared_state_buffer_; 94 CommandBufferSharedState* shared_state_; 95 int32 num_entries_; 96 int32 get_offset_; 97 int32 put_offset_; 98 base::Closure put_offset_change_callback_; 99 GetBufferChangedCallback get_buffer_change_callback_; 100 base::Closure parse_error_callback_; 101 TransferBufferManagerInterface* transfer_buffer_manager_; 102 int32 token_; 103 uint32 generation_; 104 error::Error error_; 105 error::ContextLostReason context_lost_reason_; 106 107 DISALLOW_COPY_AND_ASSIGN(CommandBufferService); 108 }; 109 110 } // namespace gpu 111 112 #endif // GPU_COMMAND_BUFFER_SERVICE_COMMAND_BUFFER_SERVICE_H_ 113