Upstream version 9.38.198.0
[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/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"
30
31 class GURL;
32 class TransportTextureService;
33 struct GPUCreateCommandBufferConfig;
34
35 namespace base {
36 class MessageLoop;
37 class MessageLoopProxy;
38 class WaitableEvent;
39 }
40
41 namespace IPC {
42 class SyncMessageFilter;
43 }
44
45 namespace media {
46 class VideoDecodeAccelerator;
47 class VideoEncodeAccelerator;
48 }
49
50 namespace content {
51 class CommandBufferProxyImpl;
52 class GpuChannelHost;
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   virtual ~GpuChannelHostFactory() {}
65
66   virtual bool IsMainThread() = 0;
67   virtual base::MessageLoop* GetMainLoop() = 0;
68   virtual scoped_refptr<base::MessageLoopProxy> GetIOLoopProxy() = 0;
69   virtual scoped_ptr<base::SharedMemory> AllocateSharedMemory(size_t size) = 0;
70   virtual CreateCommandBufferResult CreateViewCommandBuffer(
71       int32 surface_id,
72       const GPUCreateCommandBufferConfig& init_params,
73       int32 route_id) = 0;
74   virtual scoped_ptr<gfx::GpuMemoryBuffer> AllocateGpuMemoryBuffer(
75       size_t width,
76       size_t height,
77       unsigned internalformat,
78       unsigned usage) = 0;
79   virtual void DeleteGpuMemoryBuffer(
80       scoped_ptr<gfx::GpuMemoryBuffer> buffer) = 0;
81 };
82
83 // Encapsulates an IPC channel between the client and one GPU process.
84 // On the GPU process side there's a corresponding GpuChannel.
85 // Every method can be called on any thread with a message loop, except for the
86 // IO thread.
87 class GpuChannelHost : public IPC::Sender,
88                        public base::RefCountedThreadSafe<GpuChannelHost> {
89  public:
90   // Must be called on the main thread (as defined by the factory).
91   static scoped_refptr<GpuChannelHost> Create(
92       GpuChannelHostFactory* factory,
93       const gpu::GPUInfo& gpu_info,
94       const IPC::ChannelHandle& channel_handle,
95       base::WaitableEvent* shutdown_event);
96
97   // Returns true if |handle| is a valid GpuMemoryBuffer handle that
98   // can be shared to the GPU process.
99   static bool IsValidGpuMemoryBuffer(gfx::GpuMemoryBufferHandle handle);
100
101   bool IsLost() const {
102     DCHECK(channel_filter_.get());
103     return channel_filter_->IsLost();
104   }
105
106   // The GPU stats reported by the GPU process.
107   const gpu::GPUInfo& gpu_info() const { return gpu_info_; }
108
109   // IPC::Sender implementation:
110   virtual bool Send(IPC::Message* msg) OVERRIDE;
111
112   // Create and connect to a command buffer in the GPU process.
113   CommandBufferProxyImpl* CreateViewCommandBuffer(
114       int32 surface_id,
115       CommandBufferProxyImpl* share_group,
116       const std::vector<int32>& attribs,
117       const GURL& active_url,
118       gfx::GpuPreference gpu_preference);
119
120   // Create and connect to a command buffer in the GPU process.
121   CommandBufferProxyImpl* CreateOffscreenCommandBuffer(
122       const gfx::Size& size,
123       CommandBufferProxyImpl* share_group,
124       const std::vector<int32>& attribs,
125       const GURL& active_url,
126       gfx::GpuPreference gpu_preference);
127
128   // Creates a video decoder in the GPU process.
129   scoped_ptr<media::VideoDecodeAccelerator> CreateVideoDecoder(
130       int command_buffer_route_id);
131
132   // Creates a video encoder in the GPU process.
133   scoped_ptr<media::VideoEncodeAccelerator> CreateVideoEncoder(
134       int command_buffer_route_id);
135
136   // Destroy a command buffer created by this channel.
137   void DestroyCommandBuffer(CommandBufferProxyImpl* command_buffer);
138
139   // Add a route for the current message loop.
140   void AddRoute(int route_id, base::WeakPtr<IPC::Listener> listener);
141   void RemoveRoute(int route_id);
142
143   GpuChannelHostFactory* factory() const { return factory_; }
144
145   // Returns a handle to the shared memory that can be sent via IPC to the
146   // GPU process. The caller is responsible for ensuring it is closed. Returns
147   // an invalid handle on failure.
148   base::SharedMemoryHandle ShareToGpuProcess(
149       base::SharedMemoryHandle source_handle);
150
151   // Reserve one unused transfer buffer ID.
152   int32 ReserveTransferBufferId();
153
154   // Returns a GPU memory buffer handle to the buffer that can be sent via
155   // IPC to the GPU process. The caller is responsible for ensuring it is
156   // closed. Returns an invalid handle on failure.
157   gfx::GpuMemoryBufferHandle ShareGpuMemoryBufferToGpuProcess(
158       gfx::GpuMemoryBufferHandle source_handle);
159
160   // Reserve one unused gpu memory buffer ID.
161   int32 ReserveGpuMemoryBufferId();
162
163   // Generate a route ID guaranteed to be unique for this channel.
164   int32 GenerateRouteID();
165
166  private:
167   friend class base::RefCountedThreadSafe<GpuChannelHost>;
168   GpuChannelHost(GpuChannelHostFactory* factory,
169                  const gpu::GPUInfo& gpu_info);
170   virtual ~GpuChannelHost();
171   void Connect(const IPC::ChannelHandle& channel_handle,
172                base::WaitableEvent* shutdown_event);
173
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
176   // all the contexts.
177   class MessageFilter : public IPC::MessageFilter {
178    public:
179     MessageFilter();
180
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);
187
188     // IPC::MessageFilter implementation
189     // (called on the IO thread):
190     virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
191     virtual void OnChannelError() OVERRIDE;
192
193     // The following methods can be called on any thread.
194
195     // Whether the channel is lost.
196     bool IsLost() const;
197
198    private:
199     virtual ~MessageFilter();
200
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_;
205
206     // Protects all fields below this one.
207     mutable base::Lock lock_;
208
209     // Whether the channel has been lost.
210     bool lost_;
211   };
212
213   // Threading notes: all fields are constant during the lifetime of |this|
214   // except:
215   // - |next_transfer_buffer_id_|, atomic type
216   // - |next_gpu_memory_buffer_id_|, atomic type
217   // - |next_route_id_|, atomic type
218   // - |proxies_|, protected by |context_lock_|
219   GpuChannelHostFactory* const factory_;
220
221   const gpu::GPUInfo gpu_info_;
222
223   scoped_ptr<IPC::SyncChannel> channel_;
224   scoped_refptr<MessageFilter> channel_filter_;
225
226   // A filter for sending messages from thread other than the main thread.
227   scoped_refptr<IPC::SyncMessageFilter> sync_filter_;
228
229   // Transfer buffer IDs are allocated in sequence.
230   base::AtomicSequenceNumber next_transfer_buffer_id_;
231
232   // Gpu memory buffer IDs are allocated in sequence.
233   base::AtomicSequenceNumber next_gpu_memory_buffer_id_;
234
235   // Route IDs are allocated in sequence.
236   base::AtomicSequenceNumber next_route_id_;
237
238   // Protects proxies_.
239   mutable base::Lock context_lock_;
240   // Used to look up a proxy from its routing id.
241   typedef base::hash_map<int, CommandBufferProxyImpl*> ProxyMap;
242   ProxyMap proxies_;
243
244   DISALLOW_COPY_AND_ASSIGN(GpuChannelHost);
245 };
246
247 }  // namespace content
248
249 #endif  // CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_