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/client/gpu_control.h"
19 #include "gpu/command_buffer/common/command_buffer.h"
20 #include "gpu/command_buffer/common/command_buffer_shared.h"
21 #include "gpu/command_buffer/common/gpu_memory_allocation.h"
22 #include "ipc/ipc_listener.h"
23 #include "ui/events/latency_info.h"
25 struct GPUCommandBufferConsoleMessage;
32 class GpuMemoryBuffer;
40 class VideoDecodeAccelerator;
41 class VideoEncodeAccelerator;
47 // Client side proxy that forwards messages synchronously to a
49 class CommandBufferProxyImpl
50 : public gpu::CommandBuffer,
51 public gpu::GpuControl,
53 public base::SupportsWeakPtr<CommandBufferProxyImpl> {
55 class DeletionObserver {
57 // Called during the destruction of the CommandBufferProxyImpl.
58 virtual void OnWillDeleteImpl() = 0;
61 virtual ~DeletionObserver() {}
64 typedef base::Callback<void(
65 const std::string& msg, int id)> GpuConsoleMessageCallback;
67 CommandBufferProxyImpl(GpuChannelHost* channel, int route_id);
68 virtual ~CommandBufferProxyImpl();
70 // Sends an IPC message to create a GpuVideoDecodeAccelerator. Creates and
71 // returns it as an owned pointer to a media::VideoDecodeAccelerator. Returns
72 // NULL on failure to create the GpuVideoDecodeAcceleratorHost.
73 // Note that the GpuVideoDecodeAccelerator may still fail to be created in
74 // the GPU process, even if this returns non-NULL. In this case the VDA client
75 // is notified of an error later, after Initialize().
76 scoped_ptr<media::VideoDecodeAccelerator> CreateVideoDecoder();
78 // Sends an IPC message to create a GpuVideoEncodeAccelerator. Creates and
79 // returns it as an owned pointer to a media::VideoEncodeAccelerator. Returns
80 // NULL on failure to create the GpuVideoEncodeAcceleratorHost.
81 // Note that the GpuVideoEncodeAccelerator may still fail to be created in
82 // the GPU process, even if this returns non-NULL. In this case the VEA client
83 // is notified of an error later, after Initialize();
84 scoped_ptr<media::VideoEncodeAccelerator> CreateVideoEncoder();
86 // IPC::Listener implementation:
87 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
88 virtual void OnChannelError() OVERRIDE;
90 // CommandBuffer implementation:
91 virtual bool Initialize() OVERRIDE;
92 virtual State GetLastState() OVERRIDE;
93 virtual int32 GetLastToken() OVERRIDE;
94 virtual void Flush(int32 put_offset) OVERRIDE;
95 virtual void WaitForTokenInRange(int32 start, int32 end) OVERRIDE;
96 virtual void WaitForGetOffsetInRange(int32 start, int32 end) OVERRIDE;
97 virtual void SetGetBuffer(int32 shm_id) OVERRIDE;
98 virtual scoped_refptr<gpu::Buffer> CreateTransferBuffer(size_t size,
100 virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
102 // gpu::GpuControl implementation:
103 virtual gpu::Capabilities GetCapabilities() OVERRIDE;
104 virtual gfx::GpuMemoryBuffer* CreateGpuMemoryBuffer(size_t width,
106 unsigned internalformat,
109 virtual void DestroyGpuMemoryBuffer(int32 id) OVERRIDE;
110 virtual uint32 InsertSyncPoint() OVERRIDE;
111 virtual void SignalSyncPoint(uint32 sync_point,
112 const base::Closure& callback) OVERRIDE;
113 virtual void SignalQuery(uint32 query,
114 const base::Closure& callback) OVERRIDE;
115 virtual void SetSurfaceVisible(bool visible) OVERRIDE;
116 virtual void SendManagedMemoryStats(const gpu::ManagedMemoryStats& stats)
118 virtual void Echo(const base::Closure& callback) OVERRIDE;
119 virtual uint32 CreateStreamTexture(uint32 texture_id) OVERRIDE;
121 int GetRouteID() const;
122 bool ProduceFrontBuffer(const gpu::Mailbox& mailbox);
123 void SetChannelErrorCallback(const base::Closure& callback);
125 typedef base::Callback<void(const gpu::MemoryAllocation&)>
126 MemoryAllocationChangedCallback;
127 void SetMemoryAllocationChangedCallback(
128 const MemoryAllocationChangedCallback& callback);
129 void AddDeletionObserver(DeletionObserver* observer);
130 void RemoveDeletionObserver(DeletionObserver* observer);
132 bool EnsureBackbuffer();
134 void SetOnConsoleMessageCallback(
135 const GpuConsoleMessageCallback& callback);
137 void SetLatencyInfo(const std::vector<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, scoped_refptr<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);
165 // Try to read an updated copy of the state from shared memory.
166 void TryUpdateState();
168 // The shared memory area used to update state.
169 gpu::CommandBufferSharedState* shared_state() const;
171 // Unowned list of DeletionObservers.
172 ObserverList<DeletionObserver> deletion_observers_;
174 // The last cached state received from the service.
177 // The shared memory area used to update state.
178 scoped_ptr<base::SharedMemory> shared_state_shm_;
180 // |*this| is owned by |*channel_| and so is always outlived by it, so using a
181 // raw pointer is ok.
182 GpuChannelHost* channel_;
184 unsigned int flush_count_;
185 int32 last_put_offset_;
187 // Tasks to be invoked in echo responses.
188 std::queue<base::Closure> echo_tasks_;
190 base::Closure channel_error_callback_;
192 MemoryAllocationChangedCallback memory_allocation_changed_callback_;
194 GpuConsoleMessageCallback console_message_callback_;
196 // Tasks to be invoked in SignalSyncPoint responses.
197 uint32 next_signal_id_;
198 SignalTaskMap signal_tasks_;
200 // Local cache of id to gpu memory buffer mapping.
201 GpuMemoryBufferMap gpu_memory_buffers_;
203 gpu::Capabilities capabilities_;
205 DISALLOW_COPY_AND_ASSIGN(CommandBufferProxyImpl);
208 } // namespace content
210 #endif // CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_