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