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_BROWSER_GPU_CHANNEL_HOST_FACTORY_H_
      6 #define CONTENT_BROWSER_GPU_BROWSER_GPU_CHANNEL_HOST_FACTORY_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/process/process.h"
     13 #include "base/synchronization/waitable_event.h"
     14 #include "content/common/gpu/client/gpu_channel_host.h"
     15 #include "ipc/ipc_channel_handle.h"
     16 
     17 namespace content {
     18 
     19 class CONTENT_EXPORT BrowserGpuChannelHostFactory
     20     : public GpuChannelHostFactory {
     21  public:
     22   static void Initialize(bool establish_gpu_channel);
     23   static void Terminate();
     24   static BrowserGpuChannelHostFactory* instance() { return instance_; }
     25 
     26   // GpuChannelHostFactory implementation.
     27   virtual bool IsMainThread() OVERRIDE;
     28   virtual base::MessageLoop* GetMainLoop() OVERRIDE;
     29   virtual scoped_refptr<base::MessageLoopProxy> GetIOLoopProxy() OVERRIDE;
     30   virtual base::WaitableEvent* GetShutDownEvent() OVERRIDE;
     31   virtual scoped_ptr<base::SharedMemory> AllocateSharedMemory(
     32       size_t size) OVERRIDE;
     33   virtual int32 CreateViewCommandBuffer(
     34       int32 surface_id,
     35       const GPUCreateCommandBufferConfig& init_params) OVERRIDE;
     36   virtual void CreateImage(
     37       gfx::PluginWindowHandle window,
     38       int32 image_id,
     39       const CreateImageCallback& callback) OVERRIDE;
     40   virtual void DeleteImage(int32 image_idu, int32 sync_point) OVERRIDE;
     41   virtual scoped_ptr<gfx::GpuMemoryBuffer> AllocateGpuMemoryBuffer(
     42       size_t width,
     43       size_t height,
     44       unsigned internalformat) OVERRIDE;
     45 
     46   // Specify a task runner and callback to be used for a set of messages. The
     47   // callback will be set up on the current GpuProcessHost, identified by
     48   // GpuProcessHostId().
     49   virtual void SetHandlerForControlMessages(
     50       const uint32* message_ids,
     51       size_t num_messages,
     52       const base::Callback<void(const IPC::Message&)>& handler,
     53       base::TaskRunner* target_task_runner);
     54   int GpuProcessHostId() { return gpu_host_id_; }
     55   GpuChannelHost* EstablishGpuChannelSync(
     56       CauseForGpuLaunch cause_for_gpu_launch);
     57   void EstablishGpuChannel(CauseForGpuLaunch cause_for_gpu_launch,
     58                            const base::Closure& callback);
     59   GpuChannelHost* GetGpuChannel();
     60   int GetGpuChannelId() { return gpu_client_id_; }
     61 
     62   // Used to skip GpuChannelHost tests when there can be no GPU process.
     63   static bool CanUseForTesting();
     64 
     65  private:
     66   struct CreateRequest {
     67     CreateRequest();
     68     ~CreateRequest();
     69     base::WaitableEvent event;
     70     int gpu_host_id;
     71     int32 route_id;
     72   };
     73 
     74   class EstablishRequest : public base::RefCountedThreadSafe<EstablishRequest> {
     75    public:
     76     explicit EstablishRequest(CauseForGpuLaunch cause,
     77                               int gpu_client_id,
     78                               int gpu_host_id);
     79     void Wait();
     80     void Cancel();
     81 
     82     int gpu_host_id() { return gpu_host_id_; }
     83     IPC::ChannelHandle& channel_handle() { return channel_handle_; }
     84     gpu::GPUInfo gpu_info() { return gpu_info_; }
     85 
     86    private:
     87     friend class base::RefCountedThreadSafe<EstablishRequest>;
     88     ~EstablishRequest();
     89     void EstablishOnIO();
     90     void OnEstablishedOnIO(const IPC::ChannelHandle& channel_handle,
     91                            const gpu::GPUInfo& gpu_info);
     92     void FinishOnIO();
     93     void FinishOnMain();
     94 
     95     base::WaitableEvent event_;
     96     CauseForGpuLaunch cause_for_gpu_launch_;
     97     const int gpu_client_id_;
     98     int gpu_host_id_;
     99     bool reused_gpu_process_;
    100     IPC::ChannelHandle channel_handle_;
    101     gpu::GPUInfo gpu_info_;
    102     bool finished_;
    103     scoped_refptr<base::MessageLoopProxy> main_loop_;
    104   };
    105 
    106   explicit BrowserGpuChannelHostFactory(bool establish_gpu_channel);
    107   virtual ~BrowserGpuChannelHostFactory();
    108 
    109   void GpuChannelEstablished();
    110   void CreateViewCommandBufferOnIO(
    111       CreateRequest* request,
    112       int32 surface_id,
    113       const GPUCreateCommandBufferConfig& init_params);
    114   static void CommandBufferCreatedOnIO(CreateRequest* request, int32 route_id);
    115   void CreateImageOnIO(
    116       gfx::PluginWindowHandle window,
    117       int32 image_id,
    118       const CreateImageCallback& callback);
    119   static void ImageCreatedOnIO(
    120       const CreateImageCallback& callback, const gfx::Size size);
    121   static void OnImageCreated(
    122       const CreateImageCallback& callback, const gfx::Size size);
    123   void DeleteImageOnIO(int32 image_id, int32 sync_point);
    124   static void AddFilterOnIO(
    125       int gpu_host_id,
    126       scoped_refptr<IPC::ChannelProxy::MessageFilter> filter);
    127 
    128   const int gpu_client_id_;
    129   scoped_ptr<base::WaitableEvent> shutdown_event_;
    130   scoped_refptr<GpuChannelHost> gpu_channel_;
    131   int gpu_host_id_;
    132   scoped_refptr<EstablishRequest> pending_request_;
    133   std::vector<base::Closure> established_callbacks_;
    134 
    135   static BrowserGpuChannelHostFactory* instance_;
    136 
    137   DISALLOW_COPY_AND_ASSIGN(BrowserGpuChannelHostFactory);
    138 };
    139 
    140 }  // namespace content
    141 
    142 #endif  // CONTENT_BROWSER_GPU_BROWSER_GPU_CHANNEL_HOST_FACTORY_H_
    143