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 gpu::Capabilities GetCapabilities() OVERRIDE;
100 virtual gfx::GpuMemoryBuffer* CreateGpuMemoryBuffer(
103 unsigned internalformat,
105 virtual void DestroyGpuMemoryBuffer(int32 id) OVERRIDE;
106 virtual uint32 InsertSyncPoint() OVERRIDE;
107 virtual void SignalSyncPoint(uint32 sync_point,
108 const base::Closure& callback) OVERRIDE;
109 virtual void SignalQuery(uint32 query,
110 const base::Closure& callback) OVERRIDE;
111 virtual void SetSurfaceVisible(bool visible) OVERRIDE;
112 virtual void SendManagedMemoryStats(const gpu::ManagedMemoryStats& stats)
114 virtual void Echo(const base::Closure& callback) OVERRIDE;
115 virtual uint32 CreateStreamTexture(uint32 texture_id) OVERRIDE;
117 int GetRouteID() const;
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 EnsureBackbuffer();
130 void SetOnConsoleMessageCallback(
131 const GpuConsoleMessageCallback& callback);
133 void SetLatencyInfo(const std::vector<ui::LatencyInfo>& latency_info);
135 // TODO(apatrick): this is a temporary optimization while skia is calling
136 // ContentGLContext::MakeCurrent prior to every GL call. It saves returning 6
137 // ints redundantly when only the error is needed for the
138 // CommandBufferProxyImpl implementation.
139 virtual gpu::error::Error GetLastError() OVERRIDE;
141 GpuChannelHost* channel() const { return channel_; }
144 typedef std::map<int32, gpu::Buffer> TransferBufferMap;
145 typedef base::hash_map<uint32, base::Closure> SignalTaskMap;
146 typedef std::map<int32, gfx::GpuMemoryBuffer*> GpuMemoryBufferMap;
148 // Send an IPC message over the GPU channel. This is private to fully
149 // encapsulate the channel; all callers of this function must explicitly
150 // verify that the context has not been lost.
151 bool Send(IPC::Message* msg);
154 void OnUpdateState(const gpu::CommandBuffer::State& state);
155 void OnDestroyed(gpu::error::ContextLostReason reason);
157 void OnConsoleMessage(const GPUCommandBufferConsoleMessage& message);
158 void OnSetMemoryAllocation(const gpu::MemoryAllocation& allocation);
159 void OnSignalSyncPointAck(uint32 id);
161 // Try to read an updated copy of the state from shared memory.
162 void TryUpdateState();
164 // The shared memory area used to update state.
165 gpu::CommandBufferSharedState* shared_state() const {
166 return reinterpret_cast<gpu::CommandBufferSharedState*>(
167 shared_state_shm_->memory());
170 // Local cache of id to transfer buffer mapping.
171 TransferBufferMap transfer_buffers_;
173 // Unowned list of DeletionObservers.
174 ObserverList<DeletionObserver> deletion_observers_;
176 // The last cached state received from the service.
179 // The shared memory area used to update state.
180 scoped_ptr<base::SharedMemory> shared_state_shm_;
182 // |*this| is owned by |*channel_| and so is always outlived by it, so using a
183 // raw pointer is ok.
184 GpuChannelHost* channel_;
186 unsigned int flush_count_;
187 int32 last_put_offset_;
189 // Tasks to be invoked in echo responses.
190 std::queue<base::Closure> echo_tasks_;
192 base::Closure channel_error_callback_;
194 MemoryAllocationChangedCallback memory_allocation_changed_callback_;
196 GpuConsoleMessageCallback console_message_callback_;
198 // Tasks to be invoked in SignalSyncPoint responses.
199 uint32 next_signal_id_;
200 SignalTaskMap signal_tasks_;
202 // Local cache of id to gpu memory buffer mapping.
203 GpuMemoryBufferMap gpu_memory_buffers_;
205 gpu::Capabilities capabilities_;
207 DISALLOW_COPY_AND_ASSIGN(CommandBufferProxyImpl);
210 } // namespace content
212 #endif // CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_