Add ICU message format support
[chromium-blink-merge.git] / content / common / gpu / client / gpu_channel_host.h
blobcce68330cba84a63cc51cff2533ed458c13cc9f9
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/scoped_ptr_hash_map.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 scoped_ptr<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 scoped_ptr<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 JPEG decoder in the GPU process.
147 scoped_ptr<media::JpegDecodeAccelerator> CreateJpegDecoder(
148 media::JpegDecodeAccelerator::Client* client);
150 // Destroy a command buffer created by this channel.
151 void DestroyCommandBuffer(CommandBufferProxyImpl* command_buffer);
153 // Destroy this channel. Must be called on the main thread, before
154 // destruction.
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();
189 private:
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
202 // all the contexts.
203 class MessageFilter : public IPC::MessageFilter {
204 public:
205 MessageFilter();
207 // Called on the IO thread.
208 void AddRoute(int route_id,
209 base::WeakPtr<IPC::Listener> listener,
210 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
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.
222 bool IsLost() const;
224 private:
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.
236 bool lost_;
239 // Threading notes: all fields are constant during the lifetime of |this|
240 // except:
241 // - |next_transfer_buffer_id_|, atomic type
242 // - |next_image_id_|, atomic type
243 // - |next_route_id_|, atomic type
244 // - |channel_| and |flush_info_|, protected by |context_lock_|
245 GpuChannelHostFactory* const factory_;
247 const gpu::GPUInfo gpu_info_;
249 scoped_refptr<MessageFilter> channel_filter_;
251 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_;
253 // A filter for sending messages from thread other than the main thread.
254 scoped_refptr<IPC::SyncMessageFilter> sync_filter_;
256 // Transfer buffer IDs are allocated in sequence.
257 base::AtomicSequenceNumber next_transfer_buffer_id_;
259 // Image IDs are allocated in sequence.
260 base::AtomicSequenceNumber next_image_id_;
262 // Route IDs are allocated in sequence.
263 base::AtomicSequenceNumber next_route_id_;
265 // Protects channel_ and flush_info_.
266 mutable base::Lock context_lock_;
267 scoped_ptr<IPC::SyncChannel> channel_;
268 ProxyFlushInfo flush_info_;
270 DISALLOW_COPY_AND_ASSIGN(GpuChannelHost);
273 } // namespace content
275 #endif // CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_