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/gpu_memory_buffer.h"
28 #include "ui/gfx/native_widget_types.h"
29 #include "ui/gfx/size.h"
30 #include "ui/gl/gpu_preference.h"
33 class TransportTextureService;
34 struct GPUCreateCommandBufferConfig;
38 class MessageLoopProxy;
46 class SyncMessageFilter;
50 class CommandBufferProxyImpl;
52 struct GpuRenderingStats;
54 struct GpuListenerInfo {
58 base::WeakPtr<IPC::Listener> listener;
59 scoped_refptr<base::MessageLoopProxy> loop;
62 class CONTENT_EXPORT GpuChannelHostFactory {
64 typedef base::Callback<void(const gfx::Size)> CreateImageCallback;
66 virtual ~GpuChannelHostFactory() {}
68 virtual bool IsMainThread() = 0;
69 virtual base::MessageLoop* GetMainLoop() = 0;
70 virtual scoped_refptr<base::MessageLoopProxy> GetIOLoopProxy() = 0;
71 virtual base::WaitableEvent* GetShutDownEvent() = 0;
72 virtual scoped_ptr<base::SharedMemory> AllocateSharedMemory(size_t size) = 0;
73 virtual int32 CreateViewCommandBuffer(
74 int32 surface_id, const GPUCreateCommandBufferConfig& init_params) = 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;
80 virtual scoped_ptr<gfx::GpuMemoryBuffer> AllocateGpuMemoryBuffer(
83 unsigned internalformat) = 0;
86 // Encapsulates an IPC channel between the client and one GPU process.
87 // On the GPU process side there's a corresponding GpuChannel.
88 // Every method can be called on any thread with a message loop, except for the
90 class GpuChannelHost : public IPC::Sender,
91 public base::RefCountedThreadSafe<GpuChannelHost> {
93 // Must be called on the main thread (as defined by the factory).
94 static scoped_refptr<GpuChannelHost> Create(
95 GpuChannelHostFactory* factory,
98 const gpu::GPUInfo& gpu_info,
99 const IPC::ChannelHandle& channel_handle);
101 // Returns true if |handle| is a valid GpuMemoryBuffer handle that
102 // can be shared to the GPU process.
103 static bool IsValidGpuMemoryBuffer(gfx::GpuMemoryBufferHandle handle);
105 bool IsLost() const {
106 DCHECK(channel_filter_.get());
107 return channel_filter_->IsLost();
110 // The GPU stats reported by the GPU process.
111 const gpu::GPUInfo& gpu_info() const { return gpu_info_; }
113 // IPC::Sender implementation:
114 virtual bool Send(IPC::Message* msg) OVERRIDE;
116 // Create and connect to a command buffer in the GPU process.
117 CommandBufferProxyImpl* CreateViewCommandBuffer(
119 CommandBufferProxyImpl* share_group,
120 const std::vector<int32>& attribs,
121 const GURL& active_url,
122 gfx::GpuPreference gpu_preference);
124 // Create and connect to a command buffer in the GPU process.
125 CommandBufferProxyImpl* CreateOffscreenCommandBuffer(
126 const gfx::Size& size,
127 CommandBufferProxyImpl* share_group,
128 const std::vector<int32>& attribs,
129 const GURL& active_url,
130 gfx::GpuPreference gpu_preference);
132 // Creates a video decoder in the GPU process.
133 scoped_ptr<media::VideoDecodeAccelerator> CreateVideoDecoder(
134 int command_buffer_route_id,
135 media::VideoCodecProfile profile,
136 media::VideoDecodeAccelerator::Client* client);
138 // Creates a video encoder in the GPU process.
139 scoped_ptr<media::VideoEncodeAccelerator> CreateVideoEncoder(
140 media::VideoEncodeAccelerator::Client* client);
142 // Destroy a command buffer created by this channel.
143 void DestroyCommandBuffer(CommandBufferProxyImpl* command_buffer);
145 // Collect rendering stats from GPU process.
146 bool CollectRenderingStatsForSurface(
147 int surface_id, GpuRenderingStats* stats);
149 // Add a route for the current message loop.
150 void AddRoute(int route_id, base::WeakPtr<IPC::Listener> listener);
151 void RemoveRoute(int route_id);
153 GpuChannelHostFactory* factory() const { return factory_; }
154 int gpu_host_id() const { return gpu_host_id_; }
156 int client_id() const { return client_id_; }
158 // Returns a handle to the shared memory that can be sent via IPC to the
159 // GPU process. The caller is responsible for ensuring it is closed. Returns
160 // an invalid handle on failure.
161 base::SharedMemoryHandle ShareToGpuProcess(
162 base::SharedMemoryHandle source_handle);
164 // Generates |num| unique mailbox names that can be used with
165 // GL_texture_mailbox_CHROMIUM. Unlike genMailboxCHROMIUM, this IPC is
166 // handled only on the GPU process' IO thread, and so is not effectively
168 bool GenerateMailboxNames(unsigned num, std::vector<gpu::Mailbox>* names);
170 // Reserve one unused transfer buffer ID.
171 int32 ReserveTransferBufferId();
173 // Returns a GPU memory buffer handle to the buffer that can be sent via
174 // IPC to the GPU process. The caller is responsible for ensuring it is
175 // closed. Returns an invalid handle on failure.
176 gfx::GpuMemoryBufferHandle ShareGpuMemoryBufferToGpuProcess(
177 gfx::GpuMemoryBufferHandle source_handle);
179 // Reserve one unused gpu memory buffer ID.
180 int32 ReserveGpuMemoryBufferId();
183 friend class base::RefCountedThreadSafe<GpuChannelHost>;
184 GpuChannelHost(GpuChannelHostFactory* factory,
187 const gpu::GPUInfo& gpu_info);
188 virtual ~GpuChannelHost();
189 void Connect(const IPC::ChannelHandle& channel_handle);
191 // A filter used internally to route incoming messages from the IO thread
192 // to the correct message loop. It also maintains some shared state between
194 class MessageFilter : public IPC::ChannelProxy::MessageFilter {
198 // Called on the IO thread.
199 void AddRoute(int route_id,
200 base::WeakPtr<IPC::Listener> listener,
201 scoped_refptr<base::MessageLoopProxy> loop);
202 // Called on the IO thread.
203 void RemoveRoute(int route_id);
205 // IPC::ChannelProxy::MessageFilter implementation
206 // (called on the IO thread):
207 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
208 virtual void OnChannelError() OVERRIDE;
210 // The following methods can be called on any thread.
212 // Whether the channel is lost.
215 // Gets mailboxes from the pool, and return the number of mailboxes to ask
216 // the GPU process to maintain a good pool size. The caller is responsible
217 // for sending the GpuChannelMsg_GenerateMailboxNamesAsync message.
218 size_t GetMailboxNames(size_t num, std::vector<gpu::Mailbox>* names);
221 virtual ~MessageFilter();
222 bool OnControlMessageReceived(const IPC::Message& msg);
225 void OnGenerateMailboxNamesReply(const std::vector<gpu::Mailbox>& names);
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 // Protexts all fields below this one.
233 mutable base::Lock lock_;
235 // Whether the channel has been lost.
238 // A pool of valid mailbox names.
239 std::vector<gpu::Mailbox> mailbox_name_pool_;
241 // Number of pending mailbox requested from the GPU process.
242 size_t requested_mailboxes_;
245 // Threading notes: all fields are constant during the lifetime of |this|
247 // - |next_transfer_buffer_id_|, atomic type
248 // - |next_gpu_memory_buffer_id_|, atomic type
249 // - |proxies_|, protected by |context_lock_|
250 GpuChannelHostFactory* const factory_;
251 const int client_id_;
252 const int gpu_host_id_;
254 const gpu::GPUInfo gpu_info_;
256 scoped_ptr<IPC::SyncChannel> channel_;
257 scoped_refptr<MessageFilter> channel_filter_;
259 // A filter for sending messages from thread other than the main thread.
260 scoped_refptr<IPC::SyncMessageFilter> sync_filter_;
262 // Transfer buffer IDs are allocated in sequence.
263 base::AtomicSequenceNumber next_transfer_buffer_id_;
265 // Gpu memory buffer IDs are allocated in sequence.
266 base::AtomicSequenceNumber next_gpu_memory_buffer_id_;
268 // Protects proxies_.
269 mutable base::Lock context_lock_;
270 // Used to look up a proxy from its routing id.
271 typedef base::hash_map<int, CommandBufferProxyImpl*> ProxyMap;
274 DISALLOW_COPY_AND_ASSIGN(GpuChannelHost);
277 } // namespace content
279 #endif // CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_