079b549a597bf93024f3e52ca2ee6b923cf0251e
[platform/framework/web/crosswalk.git] / src / content / browser / gpu / gpu_process_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_BROWSER_GPU_GPU_PROCESS_HOST_H_
6 #define CONTENT_BROWSER_GPU_GPU_PROCESS_HOST_H_
7
8 #include <map>
9 #include <queue>
10 #include <set>
11 #include <string>
12
13 #include "base/callback.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/non_thread_safe.h"
17 #include "base/time/time.h"
18 #include "content/browser/gpu/gpu_surface_tracker.h"
19 #include "content/common/content_export.h"
20 #include "content/common/gpu/gpu_memory_uma_stats.h"
21 #include "content/common/gpu/gpu_process_launch_causes.h"
22 #include "content/public/browser/browser_child_process_host_delegate.h"
23 #include "content/public/browser/gpu_data_manager.h"
24 #include "gpu/command_buffer/common/constants.h"
25 #include "gpu/config/gpu_info.h"
26 #include "ipc/ipc_sender.h"
27 #include "ipc/message_filter.h"
28 #include "ui/gfx/native_widget_types.h"
29 #include "ui/gfx/size.h"
30 #include "url/gurl.h"
31
32 struct GPUCreateCommandBufferConfig;
33 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
34 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
35 struct GpuHostMsg_AcceleratedSurfaceRelease_Params;
36
37 namespace gfx {
38 struct GpuMemoryBufferHandle;
39 }
40
41 namespace IPC {
42 struct ChannelHandle;
43 }
44
45 namespace content {
46 class BrowserChildProcessHostImpl;
47 class GpuMainThread;
48 class RenderWidgetHostViewFrameSubscriber;
49 class ShaderDiskCache;
50
51 typedef base::Thread* (*GpuMainThreadFactoryFunction)(const std::string& id);
52
53 class GpuProcessHost : public BrowserChildProcessHostDelegate,
54                        public IPC::Sender,
55                        public base::NonThreadSafe {
56  public:
57   enum GpuProcessKind {
58     GPU_PROCESS_KIND_UNSANDBOXED,
59     GPU_PROCESS_KIND_SANDBOXED,
60     GPU_PROCESS_KIND_COUNT
61   };
62
63   typedef base::Callback<void(const IPC::ChannelHandle&, const gpu::GPUInfo&)>
64       EstablishChannelCallback;
65
66   typedef base::Callback<void(bool)> CreateCommandBufferCallback;
67
68   typedef base::Callback<void(const gfx::Size)> CreateImageCallback;
69
70   typedef base::Callback<void(const gfx::GpuMemoryBufferHandle& handle)>
71       CreateGpuMemoryBufferCallback;
72
73   static bool gpu_enabled() { return gpu_enabled_; }
74
75   // Creates a new GpuProcessHost or gets an existing one, resulting in the
76   // launching of a GPU process if required.  Returns null on failure. It
77   // is not safe to store the pointer once control has returned to the message
78   // loop as it can be destroyed. Instead store the associated GPU host ID.
79   // This could return NULL if GPU access is not allowed (blacklisted).
80   CONTENT_EXPORT static GpuProcessHost* Get(GpuProcessKind kind,
81                                             CauseForGpuLaunch cause);
82
83   // Retrieves a list of process handles for all gpu processes.
84   static void GetProcessHandles(
85       const GpuDataManager::GetGpuProcessHandlesCallback& callback);
86
87   // Helper function to send the given message to the GPU process on the IO
88   // thread.  Calls Get and if a host is returned, sends it.  Can be called from
89   // any thread.  Deletes the message if it cannot be sent.
90   CONTENT_EXPORT static void SendOnIO(GpuProcessKind kind,
91                                       CauseForGpuLaunch cause,
92                                       IPC::Message* message);
93
94   CONTENT_EXPORT static void RegisterGpuMainThreadFactory(
95       GpuMainThreadFactoryFunction create);
96
97   // Get the GPU process host for the GPU process with the given ID. Returns
98   // null if the process no longer exists.
99   static GpuProcessHost* FromID(int host_id);
100   int host_id() const { return host_id_; }
101
102   // IPC::Sender implementation.
103   virtual bool Send(IPC::Message* msg) OVERRIDE;
104
105   // Adds a message filter to the GpuProcessHost's channel.
106   void AddFilter(IPC::MessageFilter* filter);
107
108   // Tells the GPU process to create a new channel for communication with a
109   // client. Once the GPU process responds asynchronously with the IPC handle
110   // and GPUInfo, we call the callback.
111   void EstablishGpuChannel(int client_id,
112                            bool share_context,
113                            const EstablishChannelCallback& callback);
114
115   // Tells the GPU process to create a new command buffer that draws into the
116   // given surface.
117   void CreateViewCommandBuffer(
118       const gfx::GLSurfaceHandle& compositing_surface,
119       int surface_id,
120       int client_id,
121       const GPUCreateCommandBufferConfig& init_params,
122       int route_id,
123       const CreateCommandBufferCallback& callback);
124
125   // Tells the GPU process to create a new image using the given window.
126   void CreateImage(
127       gfx::PluginWindowHandle window,
128       int client_id,
129       int image_id,
130       const CreateImageCallback& callback);
131
132     // Tells the GPU process to delete image.
133   void DeleteImage(int client_id, int image_id, int sync_point);
134
135   void CreateGpuMemoryBuffer(const gfx::GpuMemoryBufferHandle& handle,
136                              const gfx::Size& size,
137                              unsigned internalformat,
138                              unsigned usage,
139                              const CreateGpuMemoryBufferCallback& callback);
140   void DestroyGpuMemoryBuffer(const gfx::GpuMemoryBufferHandle& handle,
141                               int sync_point);
142
143   // What kind of GPU process, e.g. sandboxed or unsandboxed.
144   GpuProcessKind kind();
145
146   void ForceShutdown();
147
148   void BeginFrameSubscription(
149       int surface_id,
150       base::WeakPtr<RenderWidgetHostViewFrameSubscriber> subscriber);
151   void EndFrameSubscription(int surface_id);
152   void LoadedShader(const std::string& key, const std::string& data);
153
154  private:
155   static bool ValidateHost(GpuProcessHost* host);
156
157   GpuProcessHost(int host_id, GpuProcessKind kind);
158   virtual ~GpuProcessHost();
159
160   bool Init();
161
162   // Post an IPC message to the UI shim's message handler on the UI thread.
163   void RouteOnUIThread(const IPC::Message& message);
164
165   // BrowserChildProcessHostDelegate implementation.
166   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
167   virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
168   virtual void OnProcessLaunched() OVERRIDE;
169   virtual void OnProcessCrashed(int exit_code) OVERRIDE;
170
171   // Message handlers.
172   void OnInitialized(bool result, const gpu::GPUInfo& gpu_info);
173   void OnChannelEstablished(const IPC::ChannelHandle& channel_handle);
174   void OnCommandBufferCreated(bool succeeded);
175   void OnDestroyCommandBuffer(int32 surface_id);
176   void OnImageCreated(const gfx::Size size);
177   void OnGpuMemoryBufferCreated(const gfx::GpuMemoryBufferHandle& handle);
178   void OnDidCreateOffscreenContext(const GURL& url);
179   void OnDidLoseContext(bool offscreen,
180                         gpu::error::ContextLostReason reason,
181                         const GURL& url);
182   void OnDidDestroyOffscreenContext(const GURL& url);
183   void OnGpuMemoryUmaStatsReceived(const GPUMemoryUmaStats& stats);
184 #if defined(OS_MACOSX)
185   void OnAcceleratedSurfaceBuffersSwapped(
186       const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params);
187 #endif
188
189   void CreateChannelCache(int32 client_id);
190   void OnDestroyChannel(int32 client_id);
191   void OnCacheShader(int32 client_id, const std::string& key,
192                      const std::string& shader);
193
194   bool LaunchGpuProcess(const std::string& channel_id);
195
196   void SendOutstandingReplies();
197
198   void BlockLiveOffscreenContexts();
199
200   std::string GetShaderPrefixKey();
201
202   // The serial number of the GpuProcessHost / GpuProcessHostUIShim pair.
203   int host_id_;
204
205   // These are the channel requests that we have already sent to
206   // the GPU process, but haven't heard back about yet.
207   std::queue<EstablishChannelCallback> channel_requests_;
208
209   // The pending create command buffer requests we need to reply to.
210   std::queue<CreateCommandBufferCallback> create_command_buffer_requests_;
211
212   // The pending create image requests we need to reply to.
213   std::queue<CreateImageCallback> create_image_requests_;
214
215   // The pending create gpu memory buffer requests we need to reply to.
216   std::queue<CreateGpuMemoryBufferCallback> create_gpu_memory_buffer_requests_;
217
218   // Qeueud messages to send when the process launches.
219   std::queue<IPC::Message*> queued_messages_;
220
221   // Whether the GPU process is valid, set to false after Send() failed.
222   bool valid_;
223
224   // Whether we are running a GPU thread inside the browser process instead
225   // of a separate GPU process.
226   bool in_process_;
227
228   bool swiftshader_rendering_;
229   GpuProcessKind kind_;
230
231   scoped_ptr<base::Thread> in_process_gpu_thread_;
232
233   // Whether we actually launched a GPU process.
234   bool process_launched_;
235
236   // Whether the GPU process successfully initialized.
237   bool initialized_;
238
239   // Time Init started.  Used to log total GPU process startup time to UMA.
240   base::TimeTicks init_start_time_;
241
242   // Master switch for enabling/disabling GPU acceleration for the current
243   // browser session. It does not change the acceleration settings for
244   // existing tabs, just the future ones.
245   static bool gpu_enabled_;
246
247   static bool hardware_gpu_enabled_;
248
249   scoped_ptr<BrowserChildProcessHostImpl> process_;
250
251   // Track the URLs of the pages which have live offscreen contexts,
252   // assumed to be associated with untrusted content such as WebGL.
253   // For best robustness, when any context lost notification is
254   // received, assume all of these URLs are guilty, and block
255   // automatic execution of 3D content from those domains.
256   std::multiset<GURL> urls_with_live_offscreen_contexts_;
257
258   // Statics kept around to send to UMA histograms on GPU process lost.
259   bool uma_memory_stats_received_;
260   GPUMemoryUmaStats uma_memory_stats_;
261
262   // This map of frame subscribers are listening for frame presentation events.
263   // The key is the surface id and value is the subscriber.
264   typedef base::hash_map<int,
265                          base::WeakPtr<RenderWidgetHostViewFrameSubscriber> >
266   FrameSubscriberMap;
267   FrameSubscriberMap frame_subscribers_;
268
269   typedef std::map<int32, scoped_refptr<ShaderDiskCache> >
270       ClientIdToShaderCacheMap;
271   ClientIdToShaderCacheMap client_id_to_shader_cache_;
272
273   std::string shader_prefix_key_;
274
275   // Keep an extra reference to the SurfaceRef stored in the GpuSurfaceTracker
276   // in this map so that we don't destroy it whilst the GPU process is
277   // drawing to it.
278   typedef std::multimap<int, scoped_refptr<GpuSurfaceTracker::SurfaceRef> >
279       SurfaceRefMap;
280   SurfaceRefMap surface_refs_;
281
282   DISALLOW_COPY_AND_ASSIGN(GpuProcessHost);
283 };
284
285 }  // namespace content
286
287 #endif  // CONTENT_BROWSER_GPU_GPU_PROCESS_HOST_H_