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_
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/ipc_channel_handle.h"
24 #include "ipc/ipc_sync_channel.h"
25 #include "ipc/message_filter.h"
26 #include "ui/events/latency_info.h"
27 #include "ui/gfx/geometry/size.h"
28 #include "ui/gfx/gpu_memory_buffer.h"
29 #include "ui/gfx/native_widget_types.h"
30 #include "ui/gl/gpu_preference.h"
33 class TransportTextureService
;
34 struct GPUCreateCommandBufferConfig
;
38 class MessageLoopProxy
;
43 class SyncMessageFilter
;
47 class VideoDecodeAccelerator
;
48 class VideoEncodeAccelerator
;
52 class GpuMemoryBufferManager
;
56 class CommandBufferProxyImpl
;
59 struct GpuListenerInfo
{
63 base::WeakPtr
<IPC::Listener
> listener
;
64 scoped_refptr
<base::MessageLoopProxy
> loop
;
67 struct ProxyFlushInfo
{
74 unsigned int flush_count
;
75 std::vector
<ui::LatencyInfo
> latency_info
;
78 class CONTENT_EXPORT GpuChannelHostFactory
{
80 virtual ~GpuChannelHostFactory() {}
82 virtual bool IsMainThread() = 0;
83 virtual base::MessageLoop
* GetMainLoop() = 0;
84 virtual scoped_refptr
<base::MessageLoopProxy
> GetIOLoopProxy() = 0;
85 virtual scoped_ptr
<base::SharedMemory
> AllocateSharedMemory(size_t size
) = 0;
86 virtual CreateCommandBufferResult
CreateViewCommandBuffer(
88 const GPUCreateCommandBufferConfig
& init_params
,
92 // Encapsulates an IPC channel between the client and one GPU process.
93 // On the GPU process side there's a corresponding GpuChannel.
94 // Every method can be called on any thread with a message loop, except for the
96 class GpuChannelHost
: public IPC::Sender
,
97 public base::RefCountedThreadSafe
<GpuChannelHost
> {
99 // Must be called on the main thread (as defined by the factory).
100 static scoped_refptr
<GpuChannelHost
> Create(
101 GpuChannelHostFactory
* factory
,
102 const gpu::GPUInfo
& gpu_info
,
103 const IPC::ChannelHandle
& channel_handle
,
104 base::WaitableEvent
* shutdown_event
,
105 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
);
107 bool IsLost() const {
108 DCHECK(channel_filter_
.get());
109 return channel_filter_
->IsLost();
112 // The GPU stats reported by the GPU process.
113 const gpu::GPUInfo
& gpu_info() const { return gpu_info_
; }
115 // IPC::Sender implementation:
116 bool Send(IPC::Message
* msg
) override
;
118 // Set an ordering barrier. AsyncFlushes any pending barriers on other
119 // routes. Combines multiple OrderingBarriers into a single AsyncFlush.
120 void OrderingBarrier(int route_id
,
122 unsigned int flush_count
,
123 const std::vector
<ui::LatencyInfo
>& latency_info
,
124 bool put_offset_changed
,
127 // Create and connect to a command buffer in the GPU process.
128 CommandBufferProxyImpl
* CreateViewCommandBuffer(
130 CommandBufferProxyImpl
* share_group
,
131 const std::vector
<int32
>& attribs
,
132 const GURL
& active_url
,
133 gfx::GpuPreference gpu_preference
);
135 // Create and connect to a command buffer in the GPU process.
136 CommandBufferProxyImpl
* CreateOffscreenCommandBuffer(
137 const gfx::Size
& size
,
138 CommandBufferProxyImpl
* share_group
,
139 const std::vector
<int32
>& attribs
,
140 const GURL
& active_url
,
141 gfx::GpuPreference gpu_preference
);
143 // Creates a video decoder in the GPU process.
144 scoped_ptr
<media::VideoDecodeAccelerator
> CreateVideoDecoder(
145 int command_buffer_route_id
);
147 // Creates a video encoder in the GPU process.
148 scoped_ptr
<media::VideoEncodeAccelerator
> CreateVideoEncoder(
149 int command_buffer_route_id
);
151 // Destroy a command buffer created by this channel.
152 void DestroyCommandBuffer(CommandBufferProxyImpl
* command_buffer
);
154 // Destroy this channel.
155 void DestroyChannel();
157 // Add a route for the current message loop.
158 void AddRoute(int route_id
, base::WeakPtr
<IPC::Listener
> listener
);
159 void RemoveRoute(int route_id
);
161 GpuChannelHostFactory
* factory() const { return factory_
; }
163 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager() const {
164 return gpu_memory_buffer_manager_
;
167 // Returns a handle to the shared memory that can be sent via IPC to the
168 // GPU process. The caller is responsible for ensuring it is closed. Returns
169 // an invalid handle on failure.
170 base::SharedMemoryHandle
ShareToGpuProcess(
171 base::SharedMemoryHandle source_handle
);
173 // Reserve one unused transfer buffer ID.
174 int32
ReserveTransferBufferId();
176 // Returns a GPU memory buffer handle to the buffer that can be sent via
177 // IPC to the GPU process. The caller is responsible for ensuring it is
178 // closed. Returns an invalid handle on failure.
179 gfx::GpuMemoryBufferHandle
ShareGpuMemoryBufferToGpuProcess(
180 const gfx::GpuMemoryBufferHandle
& source_handle
,
181 bool* requires_sync_point
);
183 // Reserve one unused image ID.
184 int32
ReserveImageId();
186 // Generate a route ID guaranteed to be unique for this channel.
187 int32
GenerateRouteID();
190 friend class base::RefCountedThreadSafe
<GpuChannelHost
>;
191 GpuChannelHost(GpuChannelHostFactory
* factory
,
192 const gpu::GPUInfo
& gpu_info
,
193 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
);
194 ~GpuChannelHost() override
;
195 void Connect(const IPC::ChannelHandle
& channel_handle
,
196 base::WaitableEvent
* shutdown_event
);
197 bool InternalSend(IPC::Message
* msg
);
198 void InternalFlush();
200 // A filter used internally to route incoming messages from the IO thread
201 // to the correct message loop. It also maintains some shared state between
203 class MessageFilter
: public IPC::MessageFilter
{
207 // Called on the IO thread.
208 void AddRoute(int route_id
,
209 base::WeakPtr
<IPC::Listener
> listener
,
210 scoped_refptr
<base::MessageLoopProxy
> loop
);
211 // Called on the IO thread.
212 void RemoveRoute(int route_id
);
214 // IPC::MessageFilter implementation
215 // (called on the IO thread):
216 bool OnMessageReceived(const IPC::Message
& msg
) override
;
217 void OnChannelError() override
;
219 // The following methods can be called on any thread.
221 // Whether the channel is lost.
225 ~MessageFilter() override
;
227 // Threading notes: |listeners_| is only accessed on the IO thread. Every
228 // other field is protected by |lock_|.
229 typedef base::hash_map
<int, GpuListenerInfo
> ListenerMap
;
230 ListenerMap listeners_
;
232 // Protects all fields below this one.
233 mutable base::Lock lock_
;
235 // Whether the channel has been lost.
239 // Threading notes: all fields are constant during the lifetime of |this|
241 // - |next_transfer_buffer_id_|, atomic type
242 // - |next_image_id_|, atomic type
243 // - |next_route_id_|, atomic type
244 // - |proxies_|, protected by |context_lock_|
245 GpuChannelHostFactory
* const factory_
;
247 const gpu::GPUInfo gpu_info_
;
249 scoped_ptr
<IPC::SyncChannel
> channel_
;
250 scoped_refptr
<MessageFilter
> channel_filter_
;
252 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
254 // A filter for sending messages from thread other than the main thread.
255 scoped_refptr
<IPC::SyncMessageFilter
> sync_filter_
;
257 // Transfer buffer IDs are allocated in sequence.
258 base::AtomicSequenceNumber next_transfer_buffer_id_
;
260 // Image IDs are allocated in sequence.
261 base::AtomicSequenceNumber next_image_id_
;
263 // Route IDs are allocated in sequence.
264 base::AtomicSequenceNumber next_route_id_
;
266 // Protects proxies_.
267 mutable base::Lock context_lock_
;
268 // Used to look up a proxy from its routing id.
269 typedef base::hash_map
<int, CommandBufferProxyImpl
*> ProxyMap
;
271 ProxyFlushInfo flush_info_
;
273 DISALLOW_COPY_AND_ASSIGN(GpuChannelHost
);
276 } // namespace content
278 #endif // CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_