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/message_router.h"
21 #include "gpu/config/gpu_info.h"
22 #include "ipc/ipc_channel_handle.h"
23 #include "ipc/ipc_channel_proxy.h"
24 #include "ipc/ipc_sync_channel.h"
25 #include "media/video/video_decode_accelerator.h"
26 #include "media/video/video_encode_accelerator.h"
27 #include "ui/gfx/native_widget_types.h"
28 #include "ui/gfx/size.h"
29 #include "ui/gl/gpu_preference.h"
32 class TransportTextureService
;
33 struct GPUCreateCommandBufferConfig
;
37 class MessageLoopProxy
;
45 class SyncMessageFilter
;
49 class CommandBufferProxyImpl
;
51 struct GpuRenderingStats
;
53 struct GpuListenerInfo
{
57 base::WeakPtr
<IPC::Listener
> listener
;
58 scoped_refptr
<base::MessageLoopProxy
> loop
;
61 class CONTENT_EXPORT GpuChannelHostFactory
{
63 typedef base::Callback
<void(const gfx::Size
)> CreateImageCallback
;
65 virtual ~GpuChannelHostFactory() {}
67 virtual bool IsMainThread() = 0;
68 virtual base::MessageLoop
* GetMainLoop() = 0;
69 virtual scoped_refptr
<base::MessageLoopProxy
> GetIOLoopProxy() = 0;
70 virtual base::WaitableEvent
* GetShutDownEvent() = 0;
71 virtual scoped_ptr
<base::SharedMemory
> AllocateSharedMemory(size_t size
) = 0;
72 virtual int32
CreateViewCommandBuffer(
73 int32 surface_id
, const GPUCreateCommandBufferConfig
& init_params
) = 0;
74 virtual GpuChannelHost
* EstablishGpuChannelSync(CauseForGpuLaunch
) = 0;
75 virtual void CreateImage(
76 gfx::PluginWindowHandle window
,
78 const CreateImageCallback
& callback
) = 0;
79 virtual void DeleteImage(int32 image_id
, int32 sync_point
) = 0;
82 // Encapsulates an IPC channel between the client and one GPU process.
83 // On the GPU process side there's a corresponding GpuChannel.
84 // Every method can be called on any thread with a message loop, except for the
86 class GpuChannelHost
: public IPC::Sender
,
87 public base::RefCountedThreadSafe
<GpuChannelHost
> {
89 // Must be called on the main thread (as defined by the factory).
90 static scoped_refptr
<GpuChannelHost
> Create(
91 GpuChannelHostFactory
* factory
,
94 const gpu::GPUInfo
& gpu_info
,
95 const IPC::ChannelHandle
& channel_handle
);
98 DCHECK(channel_filter_
.get());
99 return channel_filter_
->IsLost();
102 // The GPU stats reported by the GPU process.
103 const gpu::GPUInfo
& gpu_info() const { return gpu_info_
; }
105 // IPC::Sender implementation:
106 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
108 // Create and connect to a command buffer in the GPU process.
109 CommandBufferProxyImpl
* CreateViewCommandBuffer(
111 CommandBufferProxyImpl
* share_group
,
112 const std::vector
<int32
>& attribs
,
113 const GURL
& active_url
,
114 gfx::GpuPreference gpu_preference
);
116 // Create and connect to a command buffer in the GPU process.
117 CommandBufferProxyImpl
* CreateOffscreenCommandBuffer(
118 const gfx::Size
& size
,
119 CommandBufferProxyImpl
* share_group
,
120 const std::vector
<int32
>& attribs
,
121 const GURL
& active_url
,
122 gfx::GpuPreference gpu_preference
);
124 // Creates a video decoder in the GPU process.
125 scoped_ptr
<media::VideoDecodeAccelerator
> CreateVideoDecoder(
126 int command_buffer_route_id
,
127 media::VideoCodecProfile profile
,
128 media::VideoDecodeAccelerator::Client
* client
);
130 // Creates a video encoder in the GPU process.
131 scoped_ptr
<media::VideoEncodeAccelerator
> CreateVideoEncoder(
132 media::VideoEncodeAccelerator::Client
* client
);
134 // Destroy a command buffer created by this channel.
135 void DestroyCommandBuffer(CommandBufferProxyImpl
* command_buffer
);
137 // Collect rendering stats from GPU process.
138 bool CollectRenderingStatsForSurface(
139 int surface_id
, GpuRenderingStats
* stats
);
141 // Add a route for the current message loop.
142 void AddRoute(int route_id
, base::WeakPtr
<IPC::Listener
> listener
);
143 void RemoveRoute(int route_id
);
145 GpuChannelHostFactory
* factory() const { return factory_
; }
146 int gpu_host_id() const { return gpu_host_id_
; }
148 int client_id() const { return client_id_
; }
150 // Returns a handle to the shared memory that can be sent via IPC to the
151 // GPU process. The caller is responsible for ensuring it is closed. Returns
152 // an invalid handle on failure.
153 base::SharedMemoryHandle
ShareToGpuProcess(
154 base::SharedMemoryHandle source_handle
);
156 // Generates |num| unique mailbox names that can be used with
157 // GL_texture_mailbox_CHROMIUM. Unlike genMailboxCHROMIUM, this IPC is
158 // handled only on the GPU process' IO thread, and so is not effectively
160 bool GenerateMailboxNames(unsigned num
, std::vector
<gpu::Mailbox
>* names
);
162 // Reserve one unused transfer buffer ID.
163 int32
ReserveTransferBufferId();
166 friend class base::RefCountedThreadSafe
<GpuChannelHost
>;
167 GpuChannelHost(GpuChannelHostFactory
* factory
,
170 const gpu::GPUInfo
& gpu_info
);
171 virtual ~GpuChannelHost();
172 void Connect(const IPC::ChannelHandle
& channel_handle
);
174 // A filter used internally to route incoming messages from the IO thread
175 // to the correct message loop. It also maintains some shared state between
177 class MessageFilter
: public IPC::ChannelProxy::MessageFilter
{
181 // Called on the IO thread.
182 void AddRoute(int route_id
,
183 base::WeakPtr
<IPC::Listener
> listener
,
184 scoped_refptr
<base::MessageLoopProxy
> loop
);
185 // Called on the IO thread.
186 void RemoveRoute(int route_id
);
188 // IPC::ChannelProxy::MessageFilter implementation
189 // (called on the IO thread):
190 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
191 virtual void OnChannelError() OVERRIDE
;
193 // The following methods can be called on any thread.
195 // Whether the channel is lost.
198 // Gets mailboxes from the pool, and return the number of mailboxes to ask
199 // the GPU process to maintain a good pool size. The caller is responsible
200 // for sending the GpuChannelMsg_GenerateMailboxNamesAsync message.
201 size_t GetMailboxNames(size_t num
, std::vector
<gpu::Mailbox
>* names
);
204 virtual ~MessageFilter();
205 bool OnControlMessageReceived(const IPC::Message
& msg
);
208 void OnGenerateMailboxNamesReply(const std::vector
<gpu::Mailbox
>& names
);
210 // Threading notes: |listeners_| is only accessed on the IO thread. Every
211 // other field is protected by |lock_|.
212 typedef base::hash_map
<int, GpuListenerInfo
> ListenerMap
;
213 ListenerMap listeners_
;
215 // Protexts all fields below this one.
216 mutable base::Lock lock_
;
218 // Whether the channel has been lost.
221 // A pool of valid mailbox names.
222 std::vector
<gpu::Mailbox
> mailbox_name_pool_
;
224 // Number of pending mailbox requested from the GPU process.
225 size_t requested_mailboxes_
;
228 // Threading notes: all fields are constant during the lifetime of |this|
230 // - |next_transfer_buffer_id_|, atomic type
231 // - |proxies_|, protected by |context_lock_|
232 GpuChannelHostFactory
* const factory_
;
233 const int client_id_
;
234 const int gpu_host_id_
;
236 const gpu::GPUInfo gpu_info_
;
238 scoped_ptr
<IPC::SyncChannel
> channel_
;
239 scoped_refptr
<MessageFilter
> channel_filter_
;
241 // A filter for sending messages from thread other than the main thread.
242 scoped_refptr
<IPC::SyncMessageFilter
> sync_filter_
;
244 // Transfer buffer IDs are allocated in sequence.
245 base::AtomicSequenceNumber next_transfer_buffer_id_
;
247 // Protects proxies_.
248 mutable base::Lock context_lock_
;
249 // Used to look up a proxy from its routing id.
250 typedef base::hash_map
<int, CommandBufferProxyImpl
*> ProxyMap
;
253 DISALLOW_COPY_AND_ASSIGN(GpuChannelHost
);
256 } // namespace content
258 #endif // CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_