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.
5 #ifndef CONTENT_BROWSER_GPU_BROWSER_GPU_CHANNEL_HOST_FACTORY_H_
6 #define CONTENT_BROWSER_GPU_BROWSER_GPU_CHANNEL_HOST_FACTORY_H_
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"
20 class CONTENT_EXPORT BrowserGpuChannelHostFactory
21 : public GpuChannelHostFactory {
23 static void Initialize(bool establish_gpu_channel);
24 static void Terminate();
25 static BrowserGpuChannelHostFactory* instance() { return instance_; }
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(
35 const GPUCreateCommandBufferConfig& init_params,
36 int32 route_id) OVERRIDE;
37 virtual void CreateImage(
38 gfx::PluginWindowHandle window,
40 const CreateImageCallback& callback) OVERRIDE;
41 virtual void DeleteImage(int32 image_idu, int32 sync_point) OVERRIDE;
42 virtual scoped_ptr<gfx::GpuMemoryBuffer> AllocateGpuMemoryBuffer(
45 unsigned internalformat,
46 unsigned usage) OVERRIDE;
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,
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_; }
64 // Used to skip GpuChannelHost tests when there can be no GPU process.
65 static bool CanUseForTesting();
68 struct CreateRequest {
71 base::WaitableEvent event;
77 class EstablishRequest : public base::RefCountedThreadSafe<EstablishRequest> {
79 explicit EstablishRequest(CauseForGpuLaunch cause,
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_; }
90 friend class base::RefCountedThreadSafe<EstablishRequest>;
93 void OnEstablishedOnIO(const IPC::ChannelHandle& channel_handle,
94 const gpu::GPUInfo& gpu_info);
98 base::WaitableEvent event_;
99 CauseForGpuLaunch cause_for_gpu_launch_;
100 const int gpu_client_id_;
102 bool reused_gpu_process_;
103 IPC::ChannelHandle channel_handle_;
104 gpu::GPUInfo gpu_info_;
106 scoped_refptr<base::MessageLoopProxy> main_loop_;
109 explicit BrowserGpuChannelHostFactory(bool establish_gpu_channel);
110 virtual ~BrowserGpuChannelHostFactory();
112 void GpuChannelEstablished();
113 void CreateViewCommandBufferOnIO(
114 CreateRequest* request,
116 const GPUCreateCommandBufferConfig& init_params);
117 static void CommandBufferCreatedOnIO(CreateRequest* request, bool succeeded);
118 void CreateImageOnIO(
119 gfx::PluginWindowHandle window,
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);
130 const int gpu_client_id_;
131 scoped_ptr<base::WaitableEvent> shutdown_event_;
132 scoped_refptr<GpuChannelHost> gpu_channel_;
134 scoped_refptr<EstablishRequest> pending_request_;
135 std::vector<base::Closure> established_callbacks_;
137 static BrowserGpuChannelHostFactory* instance_;
139 DISALLOW_COPY_AND_ASSIGN(BrowserGpuChannelHostFactory);
142 } // namespace content
144 #endif // CONTENT_BROWSER_GPU_BROWSER_GPU_CHANNEL_HOST_FACTORY_H_