MJPEG acceleration for video capture, the GPU host part
[chromium-blink-merge.git] / content / common / gpu / client / gpu_channel_host.h
blob81ddbffbcdbb529e38745623d111bde49e0a329e
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_GPU_CHANNEL_HOST_H_
6 #define CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_
8 #include <string>
9 #include <vector>
11 #include "base/atomic_sequence_num.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/process/process.h"
17 #include "base/synchronization/lock.h"
18 #include "content/common/content_export.h"
19 #include "content/common/gpu/gpu_process_launch_causes.h"
20 #include "content/common/gpu/gpu_result_codes.h"
21 #include "content/common/message_router.h"
22 #include "gpu/config/gpu_info.h"
23 #include "ipc/attachment_broker.h"
24 #include "ipc/ipc_channel_handle.h"
25 #include "ipc/ipc_sync_channel.h"
26 #include "ipc/message_filter.h"
27 #include "media/video/jpeg_decode_accelerator.h"
28 #include "ui/events/latency_info.h"
29 #include "ui/gfx/geometry/size.h"
30 #include "ui/gfx/gpu_memory_buffer.h"
31 #include "ui/gfx/native_widget_types.h"
32 #include "ui/gl/gpu_preference.h"
34 class GURL;
35 class TransportTextureService;
36 struct GPUCreateCommandBufferConfig;
38 namespace base {
39 class MessageLoop;
40 class WaitableEvent;
43 namespace IPC {
44 class SyncMessageFilter;
47 namespace media {
48 class JpegDecodeAccelerator;
49 class VideoDecodeAccelerator;
50 class VideoEncodeAccelerator;
53 namespace gpu {
54 class GpuMemoryBufferManager;
57 namespace content {
58 class CommandBufferProxyImpl;
59 class GpuChannelHost;
61 struct GpuListenerInfo {
62 GpuListenerInfo();
63 ~GpuListenerInfo();
65 base::WeakPtr<IPC::Listener> listener;
66 scoped_refptr<base::SingleThreadTaskRunner> task_runner;
69 struct ProxyFlushInfo {
70 ProxyFlushInfo();
71 ~ProxyFlushInfo();
73 bool flush_pending;
74 int route_id;
75 int32 put_offset;
76 unsigned int flush_count;
77 std::vector<ui::LatencyInfo> latency_info;
80 class CONTENT_EXPORT GpuChannelHostFactory
81 : virtual public IPC::SupportsAttachmentBrokering {
82 public:
83 virtual ~GpuChannelHostFactory() {}
85 virtual bool IsMainThread() = 0;
86 virtual scoped_refptr<base::SingleThreadTaskRunner>
87 GetIOThreadTaskRunner() = 0;
88 virtual scoped_ptr<base::SharedMemory> AllocateSharedMemory(size_t size) = 0;
89 virtual CreateCommandBufferResult CreateViewCommandBuffer(
90 int32 surface_id,
91 const GPUCreateCommandBufferConfig& init_params,
92 int32 route_id) = 0;
95 // Encapsulates an IPC channel between the client and one GPU process.
96 // On the GPU process side there's a corresponding GpuChannel.
97 // Every method can be called on any thread with a message loop, except for the
98 // IO thread.
99 class GpuChannelHost : public IPC::Sender,
100 public base::RefCountedThreadSafe<GpuChannelHost> {
101 public:
102 // Must be called on the main thread (as defined by the factory).
103 static scoped_refptr<GpuChannelHost> Create(
104 GpuChannelHostFactory* factory,
105 const gpu::GPUInfo& gpu_info,
106 const IPC::ChannelHandle& channel_handle,
107 base::WaitableEvent* shutdown_event,
108 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager);
110 bool IsLost() const {
111 DCHECK(channel_filter_.get());
112 return channel_filter_->IsLost();
115 // The GPU stats reported by the GPU process.
116 const gpu::GPUInfo& gpu_info() const { return gpu_info_; }
118 // IPC::Sender implementation:
119 bool Send(IPC::Message* msg) override;
121 // Set an ordering barrier. AsyncFlushes any pending barriers on other
122 // routes. Combines multiple OrderingBarriers into a single AsyncFlush.
123 void OrderingBarrier(int route_id,
124 int32 put_offset,
125 unsigned int flush_count,
126 const std::vector<ui::LatencyInfo>& latency_info,
127 bool put_offset_changed,
128 bool do_flush);
130 // Create and connect to a command buffer in the GPU process.
131 CommandBufferProxyImpl* CreateViewCommandBuffer(
132 int32 surface_id,
133 CommandBufferProxyImpl* share_group,
134 const std::vector<int32>& attribs,
135 const GURL& active_url,
136 gfx::GpuPreference gpu_preference);
138 // Create and connect to a command buffer in the GPU process.
139 CommandBufferProxyImpl* CreateOffscreenCommandBuffer(
140 const gfx::Size& size,
141 CommandBufferProxyImpl* share_group,
142 const std::vector<int32>& attribs,
143 const GURL& active_url,
144 gfx::GpuPreference gpu_preference);
146 // Creates a video decoder in the GPU process.
147 scoped_ptr<media::VideoDecodeAccelerator> CreateVideoDecoder(
148 int command_buffer_route_id);
150 // Creates a video encoder in the GPU process.
151 scoped_ptr<media::VideoEncodeAccelerator> CreateVideoEncoder(
152 int command_buffer_route_id);
154 // Creates a JPEG decoder in the GPU process.
155 scoped_ptr<media::JpegDecodeAccelerator> CreateJpegDecoder(
156 media::JpegDecodeAccelerator::Client* client);
158 // Destroy a command buffer created by this channel.
159 void DestroyCommandBuffer(CommandBufferProxyImpl* command_buffer);
161 // Destroy this channel. Must be called on the main thread, before
162 // destruction.
163 void DestroyChannel();
165 // Add a route for the current message loop.
166 void AddRoute(int route_id, base::WeakPtr<IPC::Listener> listener);
167 void RemoveRoute(int route_id);
169 GpuChannelHostFactory* factory() const { return factory_; }
171 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager() const {
172 return gpu_memory_buffer_manager_;
175 // Returns a handle to the shared memory that can be sent via IPC to the
176 // GPU process. The caller is responsible for ensuring it is closed. Returns
177 // an invalid handle on failure.
178 base::SharedMemoryHandle ShareToGpuProcess(
179 base::SharedMemoryHandle source_handle);
181 // Reserve one unused transfer buffer ID.
182 int32 ReserveTransferBufferId();
184 // Returns a GPU memory buffer handle to the buffer that can be sent via
185 // IPC to the GPU process. The caller is responsible for ensuring it is
186 // closed. Returns an invalid handle on failure.
187 gfx::GpuMemoryBufferHandle ShareGpuMemoryBufferToGpuProcess(
188 const gfx::GpuMemoryBufferHandle& source_handle,
189 bool* requires_sync_point);
191 // Reserve one unused image ID.
192 int32 ReserveImageId();
194 // Generate a route ID guaranteed to be unique for this channel.
195 int32 GenerateRouteID();
197 private:
198 friend class base::RefCountedThreadSafe<GpuChannelHost>;
199 GpuChannelHost(GpuChannelHostFactory* factory,
200 const gpu::GPUInfo& gpu_info,
201 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager);
202 ~GpuChannelHost() override;
203 void Connect(const IPC::ChannelHandle& channel_handle,
204 base::WaitableEvent* shutdown_event);
205 bool InternalSend(IPC::Message* msg);
206 void InternalFlush();
208 // A filter used internally to route incoming messages from the IO thread
209 // to the correct message loop. It also maintains some shared state between
210 // all the contexts.
211 class MessageFilter : public IPC::MessageFilter {
212 public:
213 MessageFilter();
215 // Called on the IO thread.
216 void AddRoute(int route_id,
217 base::WeakPtr<IPC::Listener> listener,
218 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
219 // Called on the IO thread.
220 void RemoveRoute(int route_id);
222 // IPC::MessageFilter implementation
223 // (called on the IO thread):
224 bool OnMessageReceived(const IPC::Message& msg) override;
225 void OnChannelError() override;
227 // The following methods can be called on any thread.
229 // Whether the channel is lost.
230 bool IsLost() const;
232 private:
233 ~MessageFilter() override;
235 // Threading notes: |listeners_| is only accessed on the IO thread. Every
236 // other field is protected by |lock_|.
237 typedef base::hash_map<int, GpuListenerInfo> ListenerMap;
238 ListenerMap listeners_;
240 // Protects all fields below this one.
241 mutable base::Lock lock_;
243 // Whether the channel has been lost.
244 bool lost_;
247 // Threading notes: all fields are constant during the lifetime of |this|
248 // except:
249 // - |next_transfer_buffer_id_|, atomic type
250 // - |next_image_id_|, atomic type
251 // - |next_route_id_|, atomic type
252 // - |proxies_|, protected by |context_lock_|
253 GpuChannelHostFactory* const factory_;
255 const gpu::GPUInfo gpu_info_;
257 scoped_refptr<MessageFilter> channel_filter_;
259 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_;
261 // A filter for sending messages from thread other than the main thread.
262 scoped_refptr<IPC::SyncMessageFilter> sync_filter_;
264 // Transfer buffer IDs are allocated in sequence.
265 base::AtomicSequenceNumber next_transfer_buffer_id_;
267 // Image IDs are allocated in sequence.
268 base::AtomicSequenceNumber next_image_id_;
270 // Route IDs are allocated in sequence.
271 base::AtomicSequenceNumber next_route_id_;
273 // Protects channel_ and proxies_.
274 mutable base::Lock context_lock_;
275 scoped_ptr<IPC::SyncChannel> channel_;
276 // Used to look up a proxy from its routing id.
277 typedef base::hash_map<int, CommandBufferProxyImpl*> ProxyMap;
278 ProxyMap proxies_;
279 ProxyFlushInfo flush_info_;
281 DISALLOW_COPY_AND_ASSIGN(GpuChannelHost);
284 } // namespace content
286 #endif // CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_