Home | History | Annotate | Download | only in client
      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 CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_
      6 #define CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_
      7 
      8 #include <map>
      9 #include <queue>
     10 #include <string>
     11 
     12 #include "gpu/ipc/command_buffer_proxy.h"
     13 
     14 #include "base/callback.h"
     15 #include "base/compiler_specific.h"
     16 #include "base/containers/hash_tables.h"
     17 #include "base/memory/ref_counted.h"
     18 #include "base/memory/weak_ptr.h"
     19 #include "base/observer_list.h"
     20 #include "content/common/gpu/gpu_memory_allocation.h"
     21 #include "content/common/gpu/gpu_memory_allocation.h"
     22 #include "gpu/command_buffer/common/command_buffer.h"
     23 #include "gpu/command_buffer/common/command_buffer_shared.h"
     24 #include "ipc/ipc_listener.h"
     25 #include "media/video/video_decode_accelerator.h"
     26 #include "ui/base/latency_info.h"
     27 
     28 struct GPUCommandBufferConsoleMessage;
     29 
     30 namespace base {
     31 class SharedMemory;
     32 }
     33 
     34 namespace gpu {
     35 struct Mailbox;
     36 }
     37 
     38 namespace content {
     39 class GpuChannelHost;
     40 
     41 // Client side proxy that forwards messages synchronously to a
     42 // CommandBufferStub.
     43 class CommandBufferProxyImpl
     44     : public CommandBufferProxy,
     45       public IPC::Listener,
     46       public base::SupportsWeakPtr<CommandBufferProxyImpl> {
     47  public:
     48   class DeletionObserver {
     49    public:
     50     // Called during the destruction of the CommandBufferProxyImpl.
     51     virtual void OnWillDeleteImpl() = 0;
     52 
     53    protected:
     54     virtual ~DeletionObserver() {}
     55   };
     56 
     57   typedef base::Callback<void(
     58       const std::string& msg, int id)> GpuConsoleMessageCallback;
     59 
     60   CommandBufferProxyImpl(GpuChannelHost* channel, int route_id);
     61   virtual ~CommandBufferProxyImpl();
     62 
     63   // Sends an IPC message to create a GpuVideoDecodeAccelerator. Creates and
     64   // returns it as an owned pointer to a media::VideoDecodeAccelerator.  Returns
     65   // NULL on failure to create the GpuVideoDecodeAcceleratorHost.
     66   // Note that the GpuVideoDecodeAccelerator may still fail to be created in
     67   // the GPU process, even if this returns non-NULL. In this case the client is
     68   // notified of an error later.
     69   scoped_ptr<media::VideoDecodeAccelerator> CreateVideoDecoder(
     70       media::VideoCodecProfile profile,
     71       media::VideoDecodeAccelerator::Client* client);
     72 
     73   // IPC::Listener implementation:
     74   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     75   virtual void OnChannelError() OVERRIDE;
     76 
     77   // CommandBufferProxy implementation:
     78   virtual int GetRouteID() const OVERRIDE;
     79   virtual bool Echo(const base::Closure& callback) OVERRIDE;
     80   virtual bool ProduceFrontBuffer(const gpu::Mailbox& mailbox) OVERRIDE;
     81   virtual void SetChannelErrorCallback(const base::Closure& callback) OVERRIDE;
     82 
     83   // CommandBuffer implementation:
     84   virtual bool Initialize() OVERRIDE;
     85   virtual State GetState() OVERRIDE;
     86   virtual State GetLastState() OVERRIDE;
     87   virtual int32 GetLastToken() OVERRIDE;
     88   virtual void Flush(int32 put_offset) OVERRIDE;
     89   virtual State FlushSync(int32 put_offset, int32 last_known_get) OVERRIDE;
     90   virtual void SetGetBuffer(int32 shm_id) OVERRIDE;
     91   virtual void SetGetOffset(int32 get_offset) OVERRIDE;
     92   virtual gpu::Buffer CreateTransferBuffer(size_t size,
     93                                            int32* id) OVERRIDE;
     94   virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
     95   virtual gpu::Buffer GetTransferBuffer(int32 id) OVERRIDE;
     96   virtual void SetToken(int32 token) OVERRIDE;
     97   virtual void SetParseError(gpu::error::Error error) OVERRIDE;
     98   virtual void SetContextLostReason(
     99       gpu::error::ContextLostReason reason) OVERRIDE;
    100   virtual uint32 InsertSyncPoint() OVERRIDE;
    101 
    102   void SetMemoryAllocationChangedCallback(
    103       const base::Callback<void(const GpuMemoryAllocationForRenderer&)>&
    104           callback);
    105 
    106   void AddDeletionObserver(DeletionObserver* observer);
    107   void RemoveDeletionObserver(DeletionObserver* observer);
    108 
    109   bool DiscardBackbuffer();
    110   bool EnsureBackbuffer();
    111 
    112   // Makes this command buffer invoke a task when a sync point is reached, or
    113   // the command buffer that inserted that sync point is destroyed.
    114   bool SignalSyncPoint(uint32 sync_point,
    115                        const base::Closure& callback);
    116 
    117   // Makes this command buffer invoke a task when a query is completed, or
    118   // the command buffer that inserted that sync point is destroyed or the
    119   // query was deleted. Should be invoked after endQuery.
    120   bool SignalQuery(unsigned query, const base::Closure& callback);
    121 
    122   // Generates n unique mailbox names that can be used with
    123   // GL_texture_mailbox_CHROMIUM. Unlike genMailboxCHROMIUM, this IPC is
    124   // handled only on the GPU process' IO thread, and so is not effectively
    125   // a finish.
    126   bool GenerateMailboxNames(unsigned num, std::vector<gpu::Mailbox>* names);
    127 
    128   // Sends an IPC message with the new state of surface visibility.
    129   bool SetSurfaceVisible(bool visible);
    130 
    131   void SetOnConsoleMessageCallback(
    132       const GpuConsoleMessageCallback& callback);
    133 
    134   void SetLatencyInfo(const ui::LatencyInfo& latency_info);
    135 
    136   // TODO(apatrick): this is a temporary optimization while skia is calling
    137   // ContentGLContext::MakeCurrent prior to every GL call. It saves returning 6
    138   // ints redundantly when only the error is needed for the
    139   // CommandBufferProxyImpl implementation.
    140   virtual gpu::error::Error GetLastError() OVERRIDE;
    141 
    142   void SendManagedMemoryStats(const GpuManagedMemoryStats& stats);
    143 
    144   GpuChannelHost* channel() const { return channel_; }
    145 
    146  private:
    147   typedef std::map<int32, gpu::Buffer> TransferBufferMap;
    148   typedef base::hash_map<uint32, base::Closure> SignalTaskMap;
    149 
    150   // Send an IPC message over the GPU channel. This is private to fully
    151   // encapsulate the channel; all callers of this function must explicitly
    152   // verify that the context has not been lost.
    153   bool Send(IPC::Message* msg);
    154 
    155   // Message handlers:
    156   void OnUpdateState(const gpu::CommandBuffer::State& state);
    157   void OnDestroyed(gpu::error::ContextLostReason reason);
    158   void OnEchoAck();
    159   void OnConsoleMessage(const GPUCommandBufferConsoleMessage& message);
    160   void OnSetMemoryAllocation(const GpuMemoryAllocationForRenderer& allocation);
    161   void OnSignalSyncPointAck(uint32 id);
    162   void OnGenerateMailboxNamesReply(const std::vector<std::string>& names);
    163 
    164   // Try to read an updated copy of the state from shared memory.
    165   void TryUpdateState();
    166 
    167   // The shared memory area used to update state.
    168   gpu::CommandBufferSharedState* shared_state() const {
    169     return reinterpret_cast<gpu::CommandBufferSharedState*>(
    170         shared_state_shm_->memory());
    171   }
    172 
    173   // Local cache of id to transfer buffer mapping.
    174   TransferBufferMap transfer_buffers_;
    175 
    176   // Unowned list of DeletionObservers.
    177   ObserverList<DeletionObserver> deletion_observers_;
    178 
    179   // The last cached state received from the service.
    180   State last_state_;
    181 
    182   // The shared memory area used to update state.
    183   scoped_ptr<base::SharedMemory> shared_state_shm_;
    184 
    185   // |*this| is owned by |*channel_| and so is always outlived by it, so using a
    186   // raw pointer is ok.
    187   GpuChannelHost* channel_;
    188   int route_id_;
    189   unsigned int flush_count_;
    190   int32 last_put_offset_;
    191 
    192   // Tasks to be invoked in echo responses.
    193   std::queue<base::Closure> echo_tasks_;
    194 
    195   base::Closure channel_error_callback_;
    196 
    197   base::Callback<void(const GpuMemoryAllocationForRenderer&)>
    198       memory_allocation_changed_callback_;
    199 
    200   GpuConsoleMessageCallback console_message_callback_;
    201 
    202   // Tasks to be invoked in SignalSyncPoint responses.
    203   uint32 next_signal_id_;
    204   SignalTaskMap signal_tasks_;
    205 
    206   DISALLOW_COPY_AND_ASSIGN(CommandBufferProxyImpl);
    207 };
    208 
    209 }  // namespace content
    210 
    211 #endif  // CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_
    212