- add sources.
[platform/framework/web/crosswalk.git] / src / content / common / gpu / client / gpu_channel_host.h
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.
4
5 #ifndef CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_
6 #define CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_
7
8 #include <string>
9 #include <vector>
10
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"
31
32 class GURL;
33 class TransportTextureService;
34 struct GPUCreateCommandBufferConfig;
35
36 namespace base {
37 class MessageLoop;
38 class MessageLoopProxy;
39 }
40
41 namespace gpu {
42 struct Mailbox;
43 }
44
45 namespace IPC {
46 class SyncMessageFilter;
47 }
48
49 namespace content {
50 class CommandBufferProxyImpl;
51 class GpuChannelHost;
52 struct GpuRenderingStats;
53
54 struct GpuListenerInfo {
55   GpuListenerInfo();
56   ~GpuListenerInfo();
57
58   base::WeakPtr<IPC::Listener> listener;
59   scoped_refptr<base::MessageLoopProxy> loop;
60 };
61
62 class CONTENT_EXPORT GpuChannelHostFactory {
63  public:
64   typedef base::Callback<void(const gfx::Size)> CreateImageCallback;
65
66   virtual ~GpuChannelHostFactory() {}
67
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,
77       int32 image_id,
78       const CreateImageCallback& callback) = 0;
79   virtual void DeleteImage(int32 image_id, int32 sync_point) = 0;
80   virtual scoped_ptr<gfx::GpuMemoryBuffer> AllocateGpuMemoryBuffer(
81       size_t width,
82       size_t height,
83       unsigned internalformat) = 0;
84 };
85
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
89 // IO thread.
90 class GpuChannelHost : public IPC::Sender,
91                        public base::RefCountedThreadSafe<GpuChannelHost> {
92  public:
93   // Must be called on the main thread (as defined by the factory).
94   static scoped_refptr<GpuChannelHost> Create(
95       GpuChannelHostFactory* factory,
96       int gpu_host_id,
97       int client_id,
98       const gpu::GPUInfo& gpu_info,
99       const IPC::ChannelHandle& channel_handle);
100
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);
104
105   bool IsLost() const {
106     DCHECK(channel_filter_.get());
107     return channel_filter_->IsLost();
108   }
109
110   // The GPU stats reported by the GPU process.
111   const gpu::GPUInfo& gpu_info() const { return gpu_info_; }
112
113   // IPC::Sender implementation:
114   virtual bool Send(IPC::Message* msg) OVERRIDE;
115
116   // Create and connect to a command buffer in the GPU process.
117   CommandBufferProxyImpl* CreateViewCommandBuffer(
118       int32 surface_id,
119       CommandBufferProxyImpl* share_group,
120       const std::vector<int32>& attribs,
121       const GURL& active_url,
122       gfx::GpuPreference gpu_preference);
123
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);
131
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);
137
138   // Creates a video encoder in the GPU process.
139   scoped_ptr<media::VideoEncodeAccelerator> CreateVideoEncoder(
140       media::VideoEncodeAccelerator::Client* client);
141
142   // Destroy a command buffer created by this channel.
143   void DestroyCommandBuffer(CommandBufferProxyImpl* command_buffer);
144
145   // Collect rendering stats from GPU process.
146   bool CollectRenderingStatsForSurface(
147       int surface_id, GpuRenderingStats* stats);
148
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);
152
153   GpuChannelHostFactory* factory() const { return factory_; }
154   int gpu_host_id() const { return gpu_host_id_; }
155
156   int client_id() const { return client_id_; }
157
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);
163
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
167   // a finish.
168   bool GenerateMailboxNames(unsigned num, std::vector<gpu::Mailbox>* names);
169
170   // Reserve one unused transfer buffer ID.
171   int32 ReserveTransferBufferId();
172
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);
178
179   // Reserve one unused gpu memory buffer ID.
180   int32 ReserveGpuMemoryBufferId();
181
182  private:
183   friend class base::RefCountedThreadSafe<GpuChannelHost>;
184   GpuChannelHost(GpuChannelHostFactory* factory,
185                  int gpu_host_id,
186                  int client_id,
187                  const gpu::GPUInfo& gpu_info);
188   virtual ~GpuChannelHost();
189   void Connect(const IPC::ChannelHandle& channel_handle);
190
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
193   // all the contexts.
194   class MessageFilter : public IPC::ChannelProxy::MessageFilter {
195    public:
196     MessageFilter();
197
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);
204
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;
209
210     // The following methods can be called on any thread.
211
212     // Whether the channel is lost.
213     bool IsLost() const;
214
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);
219
220    private:
221     virtual ~MessageFilter();
222     bool OnControlMessageReceived(const IPC::Message& msg);
223
224     // Message handlers.
225     void OnGenerateMailboxNamesReply(const std::vector<gpu::Mailbox>& names);
226
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_;
231
232     // Protexts all fields below this one.
233     mutable base::Lock lock_;
234
235     // Whether the channel has been lost.
236     bool lost_;
237
238     // A pool of valid mailbox names.
239     std::vector<gpu::Mailbox> mailbox_name_pool_;
240
241     // Number of pending mailbox requested from the GPU process.
242     size_t requested_mailboxes_;
243   };
244
245   // Threading notes: all fields are constant during the lifetime of |this|
246   // except:
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_;
253
254   const gpu::GPUInfo gpu_info_;
255
256   scoped_ptr<IPC::SyncChannel> channel_;
257   scoped_refptr<MessageFilter> channel_filter_;
258
259   // A filter for sending messages from thread other than the main thread.
260   scoped_refptr<IPC::SyncMessageFilter> sync_filter_;
261
262   // Transfer buffer IDs are allocated in sequence.
263   base::AtomicSequenceNumber next_transfer_buffer_id_;
264
265   // Gpu memory buffer IDs are allocated in sequence.
266   base::AtomicSequenceNumber next_gpu_memory_buffer_id_;
267
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;
272   ProxyMap proxies_;
273
274   DISALLOW_COPY_AND_ASSIGN(GpuChannelHost);
275 };
276
277 }  // namespace content
278
279 #endif  // CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_