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/gfx/gpu_memory_buffer.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;
42 class SyncMessageFilter;
46 class VideoDecodeAccelerator;
47 class VideoEncodeAccelerator;
51 class GpuMemoryBufferManager;
55 class CommandBufferProxyImpl;
58 struct GpuListenerInfo {
62 base::WeakPtr<IPC::Listener> listener;
63 scoped_refptr<base::MessageLoopProxy> loop;
66 class CONTENT_EXPORT GpuChannelHostFactory {
68 virtual ~GpuChannelHostFactory() {}
70 virtual bool IsMainThread() = 0;
71 virtual base::MessageLoop* GetMainLoop() = 0;
72 virtual scoped_refptr<base::MessageLoopProxy> GetIOLoopProxy() = 0;
73 virtual scoped_ptr<base::SharedMemory> AllocateSharedMemory(size_t size) = 0;
74 virtual CreateCommandBufferResult CreateViewCommandBuffer(
76 const GPUCreateCommandBufferConfig& init_params,
80 // Encapsulates an IPC channel between the client and one GPU process.
81 // On the GPU process side there's a corresponding GpuChannel.
82 // Every method can be called on any thread with a message loop, except for the
84 class GpuChannelHost : public IPC::Sender,
85 public base::RefCountedThreadSafe<GpuChannelHost> {
87 // Must be called on the main thread (as defined by the factory).
88 static scoped_refptr<GpuChannelHost> Create(
89 GpuChannelHostFactory* factory,
90 const gpu::GPUInfo& gpu_info,
91 const IPC::ChannelHandle& channel_handle,
92 base::WaitableEvent* shutdown_event,
93 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager);
96 DCHECK(channel_filter_.get());
97 return channel_filter_->IsLost();
100 // The GPU stats reported by the GPU process.
101 const gpu::GPUInfo& gpu_info() const { return gpu_info_; }
103 // IPC::Sender implementation:
104 bool Send(IPC::Message* msg) override;
106 // Create and connect to a command buffer in the GPU process.
107 CommandBufferProxyImpl* CreateViewCommandBuffer(
109 CommandBufferProxyImpl* share_group,
110 const std::vector<int32>& attribs,
111 const GURL& active_url,
112 gfx::GpuPreference gpu_preference);
114 // Create and connect to a command buffer in the GPU process.
115 CommandBufferProxyImpl* CreateOffscreenCommandBuffer(
116 const gfx::Size& size,
117 CommandBufferProxyImpl* share_group,
118 const std::vector<int32>& attribs,
119 const GURL& active_url,
120 gfx::GpuPreference gpu_preference);
122 // Creates a video decoder in the GPU process.
123 scoped_ptr<media::VideoDecodeAccelerator> CreateVideoDecoder(
124 int command_buffer_route_id);
126 // Creates a video encoder in the GPU process.
127 scoped_ptr<media::VideoEncodeAccelerator> CreateVideoEncoder(
128 int command_buffer_route_id);
130 // Destroy a command buffer created by this channel.
131 void DestroyCommandBuffer(CommandBufferProxyImpl* command_buffer);
133 // Add a route for the current message loop.
134 void AddRoute(int route_id, base::WeakPtr<IPC::Listener> listener);
135 void RemoveRoute(int route_id);
137 GpuChannelHostFactory* factory() const { return factory_; }
139 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager() const {
140 return gpu_memory_buffer_manager_;
143 // Returns a handle to the shared memory that can be sent via IPC to the
144 // GPU process. The caller is responsible for ensuring it is closed. Returns
145 // an invalid handle on failure.
146 base::SharedMemoryHandle ShareToGpuProcess(
147 base::SharedMemoryHandle source_handle);
149 // Reserve one unused transfer buffer ID.
150 int32 ReserveTransferBufferId();
152 // Returns a GPU memory buffer handle to the buffer that can be sent via
153 // IPC to the GPU process. The caller is responsible for ensuring it is
154 // closed. Returns an invalid handle on failure.
155 gfx::GpuMemoryBufferHandle ShareGpuMemoryBufferToGpuProcess(
156 const gfx::GpuMemoryBufferHandle& source_handle,
157 bool* requires_sync_point);
159 // Reserve one unused image ID.
160 int32 ReserveImageId();
162 // Generate a route ID guaranteed to be unique for this channel.
163 int32 GenerateRouteID();
166 friend class base::RefCountedThreadSafe<GpuChannelHost>;
167 GpuChannelHost(GpuChannelHostFactory* factory,
168 const gpu::GPUInfo& gpu_info,
169 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager);
170 ~GpuChannelHost() override;
171 void Connect(const IPC::ChannelHandle& channel_handle,
172 base::WaitableEvent* shutdown_event);
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::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::MessageFilter implementation
189 // (called on the IO thread):
190 bool OnMessageReceived(const IPC::Message& msg) override;
191 void OnChannelError() override;
193 // The following methods can be called on any thread.
195 // Whether the channel is lost.
199 ~MessageFilter() override;
201 // Threading notes: |listeners_| is only accessed on the IO thread. Every
202 // other field is protected by |lock_|.
203 typedef base::hash_map<int, GpuListenerInfo> ListenerMap;
204 ListenerMap listeners_;
206 // Protects all fields below this one.
207 mutable base::Lock lock_;
209 // Whether the channel has been lost.
213 // Threading notes: all fields are constant during the lifetime of |this|
215 // - |next_transfer_buffer_id_|, atomic type
216 // - |next_image_id_|, atomic type
217 // - |next_route_id_|, atomic type
218 // - |proxies_|, protected by |context_lock_|
219 GpuChannelHostFactory* const factory_;
221 const gpu::GPUInfo gpu_info_;
223 scoped_ptr<IPC::SyncChannel> channel_;
224 scoped_refptr<MessageFilter> channel_filter_;
226 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_;
228 // A filter for sending messages from thread other than the main thread.
229 scoped_refptr<IPC::SyncMessageFilter> sync_filter_;
231 // Transfer buffer IDs are allocated in sequence.
232 base::AtomicSequenceNumber next_transfer_buffer_id_;
234 // Image IDs are allocated in sequence.
235 base::AtomicSequenceNumber next_image_id_;
237 // Route IDs are allocated in sequence.
238 base::AtomicSequenceNumber next_route_id_;
240 // Protects proxies_.
241 mutable base::Lock context_lock_;
242 // Used to look up a proxy from its routing id.
243 typedef base::hash_map<int, CommandBufferProxyImpl*> ProxyMap;
246 DISALLOW_COPY_AND_ASSIGN(GpuChannelHost);
249 } // namespace content
251 #endif // CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_