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"
25 #include "ui/gfx/swap_result.h"
27 struct GPUCommandBufferConsoleMessage
;
38 class VideoDecodeAccelerator
;
39 class VideoEncodeAccelerator
;
45 // Client side proxy that forwards messages synchronously to a
47 class CommandBufferProxyImpl
48 : public gpu::CommandBuffer
,
49 public gpu::GpuControl
,
51 public base::SupportsWeakPtr
<CommandBufferProxyImpl
> {
53 class DeletionObserver
{
55 // Called during the destruction of the CommandBufferProxyImpl.
56 virtual void OnWillDeleteImpl() = 0;
59 virtual ~DeletionObserver() {}
62 typedef base::Callback
<void(
63 const std::string
& msg
, int id
)> GpuConsoleMessageCallback
;
65 CommandBufferProxyImpl(GpuChannelHost
* channel
, int route_id
);
66 ~CommandBufferProxyImpl() override
;
68 // Sends an IPC message to create a GpuVideoDecodeAccelerator. Creates and
69 // returns it as an owned pointer to a media::VideoDecodeAccelerator. Returns
70 // NULL on failure to create the GpuVideoDecodeAcceleratorHost.
71 // Note that the GpuVideoDecodeAccelerator may still fail to be created in
72 // the GPU process, even if this returns non-NULL. In this case the VDA client
73 // is notified of an error later, after Initialize().
74 scoped_ptr
<media::VideoDecodeAccelerator
> CreateVideoDecoder();
76 // Sends an IPC message to create a GpuVideoEncodeAccelerator. Creates and
77 // returns it as an owned pointer to a media::VideoEncodeAccelerator. Returns
78 // NULL on failure to create the GpuVideoEncodeAcceleratorHost.
79 // Note that the GpuVideoEncodeAccelerator may still fail to be created in
80 // the GPU process, even if this returns non-NULL. In this case the VEA client
81 // is notified of an error later, after Initialize();
82 scoped_ptr
<media::VideoEncodeAccelerator
> CreateVideoEncoder();
84 // IPC::Listener implementation:
85 bool OnMessageReceived(const IPC::Message
& message
) override
;
86 void OnChannelError() override
;
88 // CommandBuffer implementation:
89 bool Initialize() override
;
90 State
GetLastState() override
;
91 int32
GetLastToken() override
;
92 void Flush(int32 put_offset
) override
;
93 void OrderingBarrier(int32 put_offset
) override
;
94 void WaitForTokenInRange(int32 start
, int32 end
) override
;
95 void WaitForGetOffsetInRange(int32 start
, int32 end
) override
;
96 void SetGetBuffer(int32 shm_id
) override
;
97 scoped_refptr
<gpu::Buffer
> CreateTransferBuffer(size_t size
,
99 void DestroyTransferBuffer(int32 id
) override
;
101 // gpu::GpuControl implementation:
102 gpu::Capabilities
GetCapabilities() override
;
103 int32
CreateImage(ClientBuffer buffer
,
106 unsigned internalformat
) override
;
107 void DestroyImage(int32 id
) override
;
108 int32
CreateGpuMemoryBufferImage(size_t width
,
110 unsigned internalformat
,
111 unsigned usage
) override
;
112 uint32
InsertSyncPoint() override
;
113 uint32_t InsertFutureSyncPoint() override
;
114 void RetireSyncPoint(uint32_t sync_point
) override
;
115 void SignalSyncPoint(uint32 sync_point
,
116 const base::Closure
& callback
) override
;
117 void SignalQuery(uint32 query
, const base::Closure
& callback
) override
;
118 void SetSurfaceVisible(bool visible
) override
;
119 uint32
CreateStreamTexture(uint32 texture_id
) override
;
120 void SetLock(base::Lock
* lock
) override
;
121 bool IsGpuChannelLost() override
;
123 bool ProduceFrontBuffer(const gpu::Mailbox
& mailbox
);
124 void SetContextLostCallback(const base::Closure
& callback
);
126 typedef base::Callback
<void(const gpu::MemoryAllocation
&)>
127 MemoryAllocationChangedCallback
;
128 void SetMemoryAllocationChangedCallback(
129 const MemoryAllocationChangedCallback
& callback
);
130 void AddDeletionObserver(DeletionObserver
* observer
);
131 void RemoveDeletionObserver(DeletionObserver
* observer
);
133 bool EnsureBackbuffer();
135 void SetOnConsoleMessageCallback(
136 const GpuConsoleMessageCallback
& callback
);
138 void SetLatencyInfo(const std::vector
<ui::LatencyInfo
>& latency_info
);
139 using SwapBuffersCompletionCallback
=
140 base::Callback
<void(const std::vector
<ui::LatencyInfo
>& latency_info
,
141 gfx::SwapResult result
)>;
142 void SetSwapBuffersCompletionCallback(
143 const SwapBuffersCompletionCallback
& callback
);
145 using UpdateVSyncParametersCallback
=
146 base::Callback
<void(base::TimeTicks timebase
, base::TimeDelta interval
)>;
147 void SetUpdateVSyncParametersCallback(
148 const UpdateVSyncParametersCallback
& callback
);
150 // TODO(apatrick): this is a temporary optimization while skia is calling
151 // ContentGLContext::MakeCurrent prior to every GL call. It saves returning 6
152 // ints redundantly when only the error is needed for the
153 // CommandBufferProxyImpl implementation.
154 gpu::error::Error
GetLastError() override
;
156 int route_id() const { return route_id_
; }
158 GpuChannelHost
* channel() const { return channel_
; }
160 base::SharedMemoryHandle
GetSharedStateHandle() const {
161 return shared_state_shm_
->handle();
165 typedef std::map
<int32
, scoped_refptr
<gpu::Buffer
> > TransferBufferMap
;
166 typedef base::hash_map
<uint32
, base::Closure
> SignalTaskMap
;
170 lock_
->AssertAcquired();
173 // Send an IPC message over the GPU channel. This is private to fully
174 // encapsulate the channel; all callers of this function must explicitly
175 // verify that the context has not been lost.
176 bool Send(IPC::Message
* msg
);
179 void OnUpdateState(const gpu::CommandBuffer::State
& state
);
180 void OnDestroyed(gpu::error::ContextLostReason reason
,
181 gpu::error::Error error
);
182 void OnConsoleMessage(const GPUCommandBufferConsoleMessage
& message
);
183 void OnSetMemoryAllocation(const gpu::MemoryAllocation
& allocation
);
184 void OnSignalSyncPointAck(uint32 id
);
185 void OnSwapBuffersCompleted(const std::vector
<ui::LatencyInfo
>& latency_info
,
186 gfx::SwapResult result
);
187 void OnUpdateVSyncParameters(base::TimeTicks timebase
,
188 base::TimeDelta interval
);
190 // Try to read an updated copy of the state from shared memory.
191 void TryUpdateState();
193 // The shared memory area used to update state.
194 gpu::CommandBufferSharedState
* shared_state() const;
198 // Unowned list of DeletionObservers.
199 base::ObserverList
<DeletionObserver
> deletion_observers_
;
201 // The last cached state received from the service.
204 // The shared memory area used to update state.
205 scoped_ptr
<base::SharedMemory
> shared_state_shm_
;
207 // |*this| is owned by |*channel_| and so is always outlived by it, so using a
208 // raw pointer is ok.
209 GpuChannelHost
* channel_
;
211 unsigned int flush_count_
;
212 int32 last_put_offset_
;
213 int32 last_barrier_put_offset_
;
215 base::Closure context_lost_callback_
;
217 MemoryAllocationChangedCallback memory_allocation_changed_callback_
;
219 GpuConsoleMessageCallback console_message_callback_
;
221 // Tasks to be invoked in SignalSyncPoint responses.
222 uint32 next_signal_id_
;
223 SignalTaskMap signal_tasks_
;
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_