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