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 "content/common/gpu/client/gpu_channel_host.h"
13 #include "content/common/gpu/client/gpu_memory_buffer_factory_host.h"
14 #include "ipc/message_filter.h"
18 class CONTENT_EXPORT BrowserGpuChannelHostFactory
19 : public GpuChannelHostFactory
,
20 public GpuMemoryBufferFactoryHost
{
22 static void Initialize(bool establish_gpu_channel
);
23 static void Terminate();
24 static BrowserGpuChannelHostFactory
* instance() { return instance_
; }
26 // GpuChannelHostFactory implementation.
27 virtual bool IsMainThread() OVERRIDE
;
28 virtual base::MessageLoop
* GetMainLoop() OVERRIDE
;
29 virtual scoped_refptr
<base::MessageLoopProxy
> GetIOLoopProxy() OVERRIDE
;
30 virtual scoped_ptr
<base::SharedMemory
> AllocateSharedMemory(
31 size_t size
) OVERRIDE
;
32 virtual CreateCommandBufferResult
CreateViewCommandBuffer(
34 const GPUCreateCommandBufferConfig
& init_params
,
35 int32 route_id
) OVERRIDE
;
36 virtual void CreateImage(
37 gfx::PluginWindowHandle window
,
39 const CreateImageCallback
& callback
) OVERRIDE
;
40 virtual void DeleteImage(int32 image_idu
, int32 sync_point
) OVERRIDE
;
41 virtual scoped_ptr
<gfx::GpuMemoryBuffer
> AllocateGpuMemoryBuffer(
44 unsigned internalformat
,
45 unsigned usage
) OVERRIDE
;
47 // GpuMemoryBufferFactoryHost implementation.
48 virtual void CreateGpuMemoryBuffer(
49 const gfx::GpuMemoryBufferHandle
& handle
,
50 const gfx::Size
& size
,
51 unsigned internalformat
,
53 const CreateGpuMemoryBufferCallback
& callback
) OVERRIDE
;
54 virtual void DestroyGpuMemoryBuffer(const gfx::GpuMemoryBufferHandle
& handle
,
55 int32 sync_point
) OVERRIDE
;
57 // Specify a task runner and callback to be used for a set of messages. The
58 // callback will be set up on the current GpuProcessHost, identified by
59 // GpuProcessHostId().
60 virtual void SetHandlerForControlMessages(
61 const uint32
* message_ids
,
63 const base::Callback
<void(const IPC::Message
&)>& handler
,
64 base::TaskRunner
* target_task_runner
);
65 int GpuProcessHostId() { return gpu_host_id_
; }
66 GpuChannelHost
* EstablishGpuChannelSync(
67 CauseForGpuLaunch cause_for_gpu_launch
);
68 void EstablishGpuChannel(CauseForGpuLaunch cause_for_gpu_launch
,
69 const base::Closure
& callback
);
70 GpuChannelHost
* GetGpuChannel();
71 int GetGpuChannelId() { return gpu_client_id_
; }
73 // Used to skip GpuChannelHost tests when there can be no GPU process.
74 static bool CanUseForTesting();
78 class EstablishRequest
;
80 BrowserGpuChannelHostFactory();
81 virtual ~BrowserGpuChannelHostFactory();
83 void GpuChannelEstablished();
84 void CreateViewCommandBufferOnIO(
85 CreateRequest
* request
,
87 const GPUCreateCommandBufferConfig
& init_params
);
88 static void CommandBufferCreatedOnIO(CreateRequest
* request
,
89 CreateCommandBufferResult result
);
91 gfx::PluginWindowHandle window
,
93 const CreateImageCallback
& callback
);
94 static void ImageCreatedOnIO(
95 const CreateImageCallback
& callback
, const gfx::Size size
);
96 static void OnImageCreated(
97 const CreateImageCallback
& callback
, const gfx::Size size
);
98 void DeleteImageOnIO(int32 image_id
, int32 sync_point
);
99 static void AddFilterOnIO(int gpu_host_id
,
100 scoped_refptr
<IPC::MessageFilter
> filter
);
102 void CreateGpuMemoryBufferOnIO(const gfx::GpuMemoryBufferHandle
& handle
,
103 const gfx::Size
& size
,
104 unsigned internalformat
,
107 void GpuMemoryBufferCreatedOnIO(
109 const gfx::GpuMemoryBufferHandle
& handle
);
110 void OnGpuMemoryBufferCreated(
112 const gfx::GpuMemoryBufferHandle
& handle
);
113 void DestroyGpuMemoryBufferOnIO(const gfx::GpuMemoryBufferHandle
& handle
,
116 const int gpu_client_id_
;
117 scoped_ptr
<base::WaitableEvent
> shutdown_event_
;
118 scoped_refptr
<GpuChannelHost
> gpu_channel_
;
120 scoped_refptr
<EstablishRequest
> pending_request_
;
121 std::vector
<base::Closure
> established_callbacks_
;
123 uint32 next_create_gpu_memory_buffer_request_id_
;
124 typedef std::map
<uint32
, CreateGpuMemoryBufferCallback
>
125 CreateGpuMemoryBufferCallbackMap
;
126 CreateGpuMemoryBufferCallbackMap create_gpu_memory_buffer_requests_
;
128 static BrowserGpuChannelHostFactory
* instance_
;
130 DISALLOW_COPY_AND_ASSIGN(BrowserGpuChannelHostFactory
);
133 } // namespace content
135 #endif // CONTENT_BROWSER_GPU_BROWSER_GPU_CHANNEL_HOST_FACTORY_H_