Home | History | Annotate | Download | only in service
      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 // An object that implements a shared memory command buffer and a synchronous
     18 // API to manage the put and get pointers.
     19 class GPU_EXPORT CommandBufferService : public CommandBuffer {
     20  public:
     21   typedef base::Callback<bool(int32)> GetBufferChangedCallback;
     22   explicit CommandBufferService(
     23       TransferBufferManagerInterface* transfer_buffer_manager);
     24   virtual ~CommandBufferService();
     25 
     26   // CommandBuffer implementation:
     27   virtual bool Initialize() OVERRIDE;
     28   virtual State GetState() OVERRIDE;
     29   virtual State GetLastState() OVERRIDE;
     30   virtual int32 GetLastToken() OVERRIDE;
     31   virtual void Flush(int32 put_offset) OVERRIDE;
     32   virtual State FlushSync(int32 put_offset, int32 last_known_get) OVERRIDE;
     33   virtual void SetGetBuffer(int32 transfer_buffer_id) OVERRIDE;
     34   virtual void SetGetOffset(int32 get_offset) OVERRIDE;
     35   virtual Buffer CreateTransferBuffer(size_t size, int32* id) OVERRIDE;
     36   virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
     37   virtual Buffer GetTransferBuffer(int32 id) OVERRIDE;
     38   virtual void SetToken(int32 token) OVERRIDE;
     39   virtual void SetParseError(error::Error error) OVERRIDE;
     40   virtual void SetContextLostReason(error::ContextLostReason) OVERRIDE;
     41 
     42   // Sets a callback that is called whenever the put offset is changed. When
     43   // called with sync==true, the callback must not return until some progress
     44   // has been made (unless the command buffer is empty), i.e. the get offset
     45   // must have changed. It need not process the entire command buffer though.
     46   // This allows concurrency between the writer and the reader while giving the
     47   // writer a means of waiting for the reader to make some progress before
     48   // attempting to write more to the command buffer. Takes ownership of
     49   // callback.
     50   virtual void SetPutOffsetChangeCallback(const base::Closure& callback);
     51   // Sets a callback that is called whenever the get buffer is changed.
     52   virtual void SetGetBufferChangeCallback(
     53       const GetBufferChangedCallback& callback);
     54   virtual void SetParseErrorCallback(const base::Closure& callback);
     55 
     56   // Setup the shared memory that shared state should be copied into.
     57   bool SetSharedStateBuffer(scoped_ptr<base::SharedMemory> shared_state_shm);
     58 
     59   // Copy the current state into the shared state transfer buffer.
     60   void UpdateState();
     61 
     62   // Register an existing shared memory object and get an ID that can be used
     63   // to identify it in the command buffer. Callee dups the handle until
     64   // DestroyTransferBuffer is called.
     65   bool RegisterTransferBuffer(int32 id,
     66                               base::SharedMemory* shared_memory,
     67                               size_t size);
     68 
     69  private:
     70   int32 ring_buffer_id_;
     71   Buffer ring_buffer_;
     72   scoped_ptr<base::SharedMemory> shared_state_shm_;
     73   CommandBufferSharedState* shared_state_;
     74   int32 num_entries_;
     75   int32 get_offset_;
     76   int32 put_offset_;
     77   base::Closure put_offset_change_callback_;
     78   GetBufferChangedCallback get_buffer_change_callback_;
     79   base::Closure parse_error_callback_;
     80   TransferBufferManagerInterface* transfer_buffer_manager_;
     81   int32 token_;
     82   uint32 generation_;
     83   error::Error error_;
     84   error::ContextLostReason context_lost_reason_;
     85 
     86   DISALLOW_COPY_AND_ASSIGN(CommandBufferService);
     87 };
     88 
     89 }  // namespace gpu
     90 
     91 #endif  // GPU_COMMAND_BUFFER_SERVICE_COMMAND_BUFFER_SERVICE_H_
     92