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.
5 #ifndef CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_
6 #define CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "gpu/command_buffer/common/command_buffer.h"
19 #include "gpu/command_buffer/common/command_buffer_shared.h"
20 #include "gpu/command_buffer/common/gpu_control.h"
21 #include "gpu/command_buffer/common/gpu_memory_allocation.h"
22 #include "ipc/ipc_listener.h"
23 #include "media/video/video_decode_accelerator.h"
24 #include "ui/events/latency_info.h"
26 struct GPUCommandBufferConsoleMessage;
33 class GpuMemoryBuffer;
43 // Client side proxy that forwards messages synchronously to a
45 class CommandBufferProxyImpl
46 : public gpu::CommandBuffer,
47 public gpu::GpuControl,
49 public base::SupportsWeakPtr<CommandBufferProxyImpl> {
51 class DeletionObserver {
53 // Called during the destruction of the CommandBufferProxyImpl.
54 virtual void OnWillDeleteImpl() = 0;
57 virtual ~DeletionObserver() {}
60 typedef base::Callback<void(
61 const std::string& msg, int id)> GpuConsoleMessageCallback;
63 CommandBufferProxyImpl(GpuChannelHost* channel, int route_id);
64 virtual ~CommandBufferProxyImpl();
66 // Sends an IPC message to create a GpuVideoDecodeAccelerator. Creates and
67 // returns it as an owned pointer to a media::VideoDecodeAccelerator. Returns
68 // NULL on failure to create the GpuVideoDecodeAcceleratorHost.
69 // Note that the GpuVideoDecodeAccelerator may still fail to be created in
70 // the GPU process, even if this returns non-NULL. In this case the client is
71 // notified of an error later.
72 scoped_ptr<media::VideoDecodeAccelerator> CreateVideoDecoder(
73 media::VideoCodecProfile profile,
74 media::VideoDecodeAccelerator::Client* client);
76 // IPC::Listener implementation:
77 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
78 virtual void OnChannelError() OVERRIDE;
80 // CommandBuffer implementation:
81 virtual bool Initialize() OVERRIDE;
82 virtual State GetState() OVERRIDE;
83 virtual State GetLastState() OVERRIDE;
84 virtual int32 GetLastToken() OVERRIDE;
85 virtual void Flush(int32 put_offset) OVERRIDE;
86 virtual State FlushSync(int32 put_offset, int32 last_known_get) OVERRIDE;
87 virtual void SetGetBuffer(int32 shm_id) OVERRIDE;
88 virtual void SetGetOffset(int32 get_offset) OVERRIDE;
89 virtual gpu::Buffer CreateTransferBuffer(size_t size,
91 virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
92 virtual gpu::Buffer GetTransferBuffer(int32 id) OVERRIDE;
93 virtual void SetToken(int32 token) OVERRIDE;
94 virtual void SetParseError(gpu::error::Error error) OVERRIDE;
95 virtual void SetContextLostReason(
96 gpu::error::ContextLostReason reason) OVERRIDE;
98 // gpu::GpuControl implementation:
99 virtual bool SupportsGpuMemoryBuffer() OVERRIDE;
100 virtual gfx::GpuMemoryBuffer* CreateGpuMemoryBuffer(
103 unsigned internalformat,
105 virtual void DestroyGpuMemoryBuffer(int32 id) OVERRIDE;
106 virtual bool GenerateMailboxNames(unsigned num,
107 std::vector<gpu::Mailbox>* names) OVERRIDE;
108 virtual uint32 InsertSyncPoint() OVERRIDE;
109 virtual void SignalSyncPoint(uint32 sync_point,
110 const base::Closure& callback) OVERRIDE;
111 virtual void SignalQuery(uint32 query,
112 const base::Closure& callback) OVERRIDE;
113 virtual void SendManagedMemoryStats(const gpu::ManagedMemoryStats& stats)
116 int GetRouteID() const;
117 bool Echo(const base::Closure& callback);
118 bool ProduceFrontBuffer(const gpu::Mailbox& mailbox);
119 void SetChannelErrorCallback(const base::Closure& callback);
121 typedef base::Callback<void(const gpu::MemoryAllocation&)>
122 MemoryAllocationChangedCallback;
123 void SetMemoryAllocationChangedCallback(
124 const MemoryAllocationChangedCallback& callback);
125 void AddDeletionObserver(DeletionObserver* observer);
126 void RemoveDeletionObserver(DeletionObserver* observer);
128 bool DiscardBackbuffer();
129 bool EnsureBackbuffer();
131 // Sends an IPC message with the new state of surface visibility.
132 bool SetSurfaceVisible(bool visible);
134 void SetOnConsoleMessageCallback(
135 const GpuConsoleMessageCallback& callback);
137 void SetLatencyInfo(const ui::LatencyInfo& latency_info);
139 // TODO(apatrick): this is a temporary optimization while skia is calling
140 // ContentGLContext::MakeCurrent prior to every GL call. It saves returning 6
141 // ints redundantly when only the error is needed for the
142 // CommandBufferProxyImpl implementation.
143 virtual gpu::error::Error GetLastError() OVERRIDE;
145 GpuChannelHost* channel() const { return channel_; }
148 typedef std::map<int32, gpu::Buffer> TransferBufferMap;
149 typedef base::hash_map<uint32, base::Closure> SignalTaskMap;
150 typedef std::map<int32, gfx::GpuMemoryBuffer*> GpuMemoryBufferMap;
152 // Send an IPC message over the GPU channel. This is private to fully
153 // encapsulate the channel; all callers of this function must explicitly
154 // verify that the context has not been lost.
155 bool Send(IPC::Message* msg);
158 void OnUpdateState(const gpu::CommandBuffer::State& state);
159 void OnDestroyed(gpu::error::ContextLostReason reason);
161 void OnConsoleMessage(const GPUCommandBufferConsoleMessage& message);
162 void OnSetMemoryAllocation(const gpu::MemoryAllocation& allocation);
163 void OnSignalSyncPointAck(uint32 id);
164 void OnGenerateMailboxNamesReply(const std::vector<std::string>& names);
166 // Try to read an updated copy of the state from shared memory.
167 void TryUpdateState();
169 // The shared memory area used to update state.
170 gpu::CommandBufferSharedState* shared_state() const {
171 return reinterpret_cast<gpu::CommandBufferSharedState*>(
172 shared_state_shm_->memory());
175 // Local cache of id to transfer buffer mapping.
176 TransferBufferMap transfer_buffers_;
178 // Unowned list of DeletionObservers.
179 ObserverList<DeletionObserver> deletion_observers_;
181 // The last cached state received from the service.
184 // The shared memory area used to update state.
185 scoped_ptr<base::SharedMemory> shared_state_shm_;
187 // |*this| is owned by |*channel_| and so is always outlived by it, so using a
188 // raw pointer is ok.
189 GpuChannelHost* channel_;
191 unsigned int flush_count_;
192 int32 last_put_offset_;
194 // Tasks to be invoked in echo responses.
195 std::queue<base::Closure> echo_tasks_;
197 base::Closure channel_error_callback_;
199 MemoryAllocationChangedCallback memory_allocation_changed_callback_;
201 GpuConsoleMessageCallback console_message_callback_;
203 // Tasks to be invoked in SignalSyncPoint responses.
204 uint32 next_signal_id_;
205 SignalTaskMap signal_tasks_;
207 // Local cache of id to gpu memory buffer mapping.
208 GpuMemoryBufferMap gpu_memory_buffers_;
210 DISALLOW_COPY_AND_ASSIGN(CommandBufferProxyImpl);
213 } // namespace content
215 #endif // CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_