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
) OVERRIDE
;
47 // Specify a task runner and callback to be used for a set of messages. The
48 // callback will be set up on the current GpuProcessHost, identified by
49 // GpuProcessHostId().
50 virtual void SetHandlerForControlMessages(
51 const uint32
* message_ids
,
53 const base::Callback
<void(const IPC::Message
&)>& handler
,
54 base::TaskRunner
* target_task_runner
);
55 int GpuProcessHostId() { return gpu_host_id_
; }
56 GpuChannelHost
* EstablishGpuChannelSync(
57 CauseForGpuLaunch cause_for_gpu_launch
);
58 void EstablishGpuChannel(CauseForGpuLaunch cause_for_gpu_launch
,
59 const base::Closure
& callback
);
60 GpuChannelHost
* GetGpuChannel();
61 int GetGpuChannelId() { return gpu_client_id_
; }
63 // Used to skip GpuChannelHost tests when there can be no GPU process.
64 static bool CanUseForTesting();
67 struct CreateRequest
{
70 base::WaitableEvent event
;
76 class EstablishRequest
: public base::RefCountedThreadSafe
<EstablishRequest
> {
78 explicit EstablishRequest(CauseForGpuLaunch cause
,
84 int gpu_host_id() { return gpu_host_id_
; }
85 IPC::ChannelHandle
& channel_handle() { return channel_handle_
; }
86 gpu::GPUInfo
gpu_info() { return gpu_info_
; }
89 friend class base::RefCountedThreadSafe
<EstablishRequest
>;
92 void OnEstablishedOnIO(const IPC::ChannelHandle
& channel_handle
,
93 const gpu::GPUInfo
& gpu_info
);
97 base::WaitableEvent event_
;
98 CauseForGpuLaunch cause_for_gpu_launch_
;
99 const int gpu_client_id_
;
101 bool reused_gpu_process_
;
102 IPC::ChannelHandle channel_handle_
;
103 gpu::GPUInfo gpu_info_
;
105 scoped_refptr
<base::MessageLoopProxy
> main_loop_
;
108 explicit BrowserGpuChannelHostFactory(bool establish_gpu_channel
);
109 virtual ~BrowserGpuChannelHostFactory();
111 void GpuChannelEstablished();
112 void CreateViewCommandBufferOnIO(
113 CreateRequest
* request
,
115 const GPUCreateCommandBufferConfig
& init_params
);
116 static void CommandBufferCreatedOnIO(CreateRequest
* request
, bool succeeded
);
117 void CreateImageOnIO(
118 gfx::PluginWindowHandle window
,
120 const CreateImageCallback
& callback
);
121 static void ImageCreatedOnIO(
122 const CreateImageCallback
& callback
, const gfx::Size size
);
123 static void OnImageCreated(
124 const CreateImageCallback
& callback
, const gfx::Size size
);
125 void DeleteImageOnIO(int32 image_id
, int32 sync_point
);
126 static void AddFilterOnIO(int gpu_host_id
,
127 scoped_refptr
<IPC::MessageFilter
> filter
);
129 const int gpu_client_id_
;
130 scoped_ptr
<base::WaitableEvent
> shutdown_event_
;
131 scoped_refptr
<GpuChannelHost
> gpu_channel_
;
133 scoped_refptr
<EstablishRequest
> pending_request_
;
134 std::vector
<base::Closure
> established_callbacks_
;
136 static BrowserGpuChannelHostFactory
* instance_
;
138 DISALLOW_COPY_AND_ASSIGN(BrowserGpuChannelHostFactory
);
141 } // namespace content
143 #endif // CONTENT_BROWSER_GPU_BROWSER_GPU_CHANNEL_HOST_FACTORY_H_