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