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/containers/scoped_ptr_hash_map.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/observer_list.h"
19 #include "gpu/command_buffer/client/gpu_control.h"
20 #include "gpu/command_buffer/common/command_buffer.h"
21 #include "gpu/command_buffer/common/command_buffer_shared.h"
22 #include "gpu/command_buffer/common/gpu_memory_allocation.h"
23 #include "ipc/ipc_listener.h"
24 #include "ui/events/latency_info.h"
26 struct GPUCommandBufferConsoleMessage
;
37 class VideoDecodeAccelerator
;
38 class VideoEncodeAccelerator
;
44 // Client side proxy that forwards messages synchronously to a
46 class CommandBufferProxyImpl
47 : public gpu::CommandBuffer
,
48 public gpu::GpuControl
,
50 public base::SupportsWeakPtr
<CommandBufferProxyImpl
> {
52 class DeletionObserver
{
54 // Called during the destruction of the CommandBufferProxyImpl.
55 virtual void OnWillDeleteImpl() = 0;
58 virtual ~DeletionObserver() {}
61 typedef base::Callback
<void(
62 const std::string
& msg
, int id
)> GpuConsoleMessageCallback
;
64 CommandBufferProxyImpl(GpuChannelHost
* channel
, int route_id
);
65 ~CommandBufferProxyImpl() override
;
67 // Sends an IPC message to create a GpuVideoDecodeAccelerator. Creates and
68 // returns it as an owned pointer to a media::VideoDecodeAccelerator. Returns
69 // NULL on failure to create the GpuVideoDecodeAcceleratorHost.
70 // Note that the GpuVideoDecodeAccelerator may still fail to be created in
71 // the GPU process, even if this returns non-NULL. In this case the VDA client
72 // is notified of an error later, after Initialize().
73 scoped_ptr
<media::VideoDecodeAccelerator
> CreateVideoDecoder();
75 // Sends an IPC message to create a GpuVideoEncodeAccelerator. Creates and
76 // returns it as an owned pointer to a media::VideoEncodeAccelerator. Returns
77 // NULL on failure to create the GpuVideoEncodeAcceleratorHost.
78 // Note that the GpuVideoEncodeAccelerator may still fail to be created in
79 // the GPU process, even if this returns non-NULL. In this case the VEA client
80 // is notified of an error later, after Initialize();
81 scoped_ptr
<media::VideoEncodeAccelerator
> CreateVideoEncoder();
83 // IPC::Listener implementation:
84 bool OnMessageReceived(const IPC::Message
& message
) override
;
85 void OnChannelError() override
;
87 // CommandBuffer implementation:
88 bool Initialize() override
;
89 State
GetLastState() override
;
90 int32
GetLastToken() override
;
91 void Flush(int32 put_offset
) override
;
92 void OrderingBarrier(int32 put_offset
) override
;
93 void WaitForTokenInRange(int32 start
, int32 end
) override
;
94 void WaitForGetOffsetInRange(int32 start
, int32 end
) override
;
95 void SetGetBuffer(int32 shm_id
) override
;
96 scoped_refptr
<gpu::Buffer
> CreateTransferBuffer(size_t size
,
98 void DestroyTransferBuffer(int32 id
) override
;
100 // gpu::GpuControl implementation:
101 gpu::Capabilities
GetCapabilities() override
;
102 int32
CreateImage(ClientBuffer buffer
,
105 unsigned internalformat
) override
;
106 void DestroyImage(int32 id
) override
;
107 int32
CreateGpuMemoryBufferImage(size_t width
,
109 unsigned internalformat
,
110 unsigned usage
) override
;
111 uint32
InsertSyncPoint() override
;
112 uint32_t InsertFutureSyncPoint() override
;
113 void RetireSyncPoint(uint32_t sync_point
) override
;
114 void SignalSyncPoint(uint32 sync_point
,
115 const base::Closure
& callback
) override
;
116 void SignalQuery(uint32 query
, const base::Closure
& callback
) override
;
117 void SetSurfaceVisible(bool visible
) override
;
118 uint32
CreateStreamTexture(uint32 texture_id
) override
;
119 void SetLock(base::Lock
* lock
) override
;
121 int GetRouteID() const;
122 bool ProduceFrontBuffer(const gpu::Mailbox
& mailbox
);
123 void SetContextLostCallback(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
);
138 using SwapBuffersCompletionCallback
=
139 base::Callback
<void(const std::vector
<ui::LatencyInfo
>& latency_info
)>;
140 void SetSwapBuffersCompletionCallback(
141 const SwapBuffersCompletionCallback
& callback
);
143 using UpdateVSyncParametersCallback
=
144 base::Callback
<void(base::TimeTicks timebase
, base::TimeDelta interval
)>;
145 void SetUpdateVSyncParametersCallback(
146 const UpdateVSyncParametersCallback
& callback
);
148 // TODO(apatrick): this is a temporary optimization while skia is calling
149 // ContentGLContext::MakeCurrent prior to every GL call. It saves returning 6
150 // ints redundantly when only the error is needed for the
151 // CommandBufferProxyImpl implementation.
152 gpu::error::Error
GetLastError() override
;
154 GpuChannelHost
* channel() const { return channel_
; }
156 base::SharedMemoryHandle
GetSharedStateHandle() const {
157 return shared_state_shm_
->handle();
161 typedef std::map
<int32
, scoped_refptr
<gpu::Buffer
> > TransferBufferMap
;
162 typedef base::hash_map
<uint32
, base::Closure
> SignalTaskMap
;
163 typedef base::ScopedPtrHashMap
<int32
, scoped_ptr
<gfx::GpuMemoryBuffer
>>
168 lock_
->AssertAcquired();
171 // Send an IPC message over the GPU channel. This is private to fully
172 // encapsulate the channel; all callers of this function must explicitly
173 // verify that the context has not been lost.
174 bool Send(IPC::Message
* msg
);
177 void OnUpdateState(const gpu::CommandBuffer::State
& state
);
178 void OnDestroyed(gpu::error::ContextLostReason reason
,
179 gpu::error::Error error
);
180 void OnConsoleMessage(const GPUCommandBufferConsoleMessage
& message
);
181 void OnSetMemoryAllocation(const gpu::MemoryAllocation
& allocation
);
182 void OnSignalSyncPointAck(uint32 id
);
183 void OnSwapBuffersCompleted(const std::vector
<ui::LatencyInfo
>& latency_info
);
184 void OnUpdateVSyncParameters(base::TimeTicks timebase
,
185 base::TimeDelta interval
);
187 // Try to read an updated copy of the state from shared memory.
188 void TryUpdateState();
190 // The shared memory area used to update state.
191 gpu::CommandBufferSharedState
* shared_state() const;
195 // Unowned list of DeletionObservers.
196 ObserverList
<DeletionObserver
> deletion_observers_
;
198 // The last cached state received from the service.
201 // The shared memory area used to update state.
202 scoped_ptr
<base::SharedMemory
> shared_state_shm_
;
204 // |*this| is owned by |*channel_| and so is always outlived by it, so using a
205 // raw pointer is ok.
206 GpuChannelHost
* channel_
;
208 unsigned int flush_count_
;
209 int32 last_put_offset_
;
210 int32 last_barrier_put_offset_
;
212 base::Closure context_lost_callback_
;
214 MemoryAllocationChangedCallback memory_allocation_changed_callback_
;
216 GpuConsoleMessageCallback console_message_callback_
;
218 // Tasks to be invoked in SignalSyncPoint responses.
219 uint32 next_signal_id_
;
220 SignalTaskMap signal_tasks_
;
222 // Local cache of id to gpu memory buffer mapping.
223 GpuMemoryBufferMap gpu_memory_buffers_
;
225 gpu::Capabilities capabilities_
;
227 std::vector
<ui::LatencyInfo
> latency_info_
;
229 SwapBuffersCompletionCallback swap_buffers_completion_callback_
;
230 UpdateVSyncParametersCallback update_vsync_parameters_completion_callback_
;
232 DISALLOW_COPY_AND_ASSIGN(CommandBufferProxyImpl
);
235 } // namespace content
237 #endif // CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_