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 scoped_ptr
<gfx::GpuMemoryBuffer
> AllocateGpuMemoryBuffer(
39 unsigned internalformat
,
40 unsigned usage
) OVERRIDE
;
41 virtual void DeleteGpuMemoryBuffer(
42 scoped_ptr
<gfx::GpuMemoryBuffer
> buffer
) OVERRIDE
{}
44 // GpuMemoryBufferFactoryHost implementation.
45 virtual void CreateGpuMemoryBuffer(
46 const gfx::GpuMemoryBufferHandle
& handle
,
47 const gfx::Size
& size
,
48 unsigned internalformat
,
50 const CreateGpuMemoryBufferCallback
& callback
) OVERRIDE
;
51 virtual void DestroyGpuMemoryBuffer(const gfx::GpuMemoryBufferHandle
& handle
,
52 int32 sync_point
) OVERRIDE
;
54 // Specify a task runner and callback to be used for a set of messages. The
55 // callback will be set up on the current GpuProcessHost, identified by
56 // GpuProcessHostId().
57 virtual void SetHandlerForControlMessages(
58 const uint32
* message_ids
,
60 const base::Callback
<void(const IPC::Message
&)>& handler
,
61 base::TaskRunner
* target_task_runner
);
62 int GpuProcessHostId() { return gpu_host_id_
; }
63 GpuChannelHost
* EstablishGpuChannelSync(
64 CauseForGpuLaunch cause_for_gpu_launch
);
65 void EstablishGpuChannel(CauseForGpuLaunch cause_for_gpu_launch
,
66 const base::Closure
& callback
);
67 GpuChannelHost
* GetGpuChannel();
68 int GetGpuChannelId() { return gpu_client_id_
; }
70 // Used to skip GpuChannelHost tests when there can be no GPU process.
71 static bool CanUseForTesting();
75 class EstablishRequest
;
77 BrowserGpuChannelHostFactory();
78 virtual ~BrowserGpuChannelHostFactory();
80 void GpuChannelEstablished();
81 void CreateViewCommandBufferOnIO(
82 CreateRequest
* request
,
84 const GPUCreateCommandBufferConfig
& init_params
);
85 static void CommandBufferCreatedOnIO(CreateRequest
* request
,
86 CreateCommandBufferResult result
);
87 static void AddFilterOnIO(int gpu_host_id
,
88 scoped_refptr
<IPC::MessageFilter
> filter
);
90 void CreateGpuMemoryBufferOnIO(const gfx::GpuMemoryBufferHandle
& handle
,
91 const gfx::Size
& size
,
92 unsigned internalformat
,
95 void GpuMemoryBufferCreatedOnIO(
97 const gfx::GpuMemoryBufferHandle
& handle
);
98 void OnGpuMemoryBufferCreated(
100 const gfx::GpuMemoryBufferHandle
& handle
);
101 void DestroyGpuMemoryBufferOnIO(const gfx::GpuMemoryBufferHandle
& handle
,
104 const int gpu_client_id_
;
105 scoped_ptr
<base::WaitableEvent
> shutdown_event_
;
106 scoped_refptr
<GpuChannelHost
> gpu_channel_
;
108 scoped_refptr
<EstablishRequest
> pending_request_
;
109 std::vector
<base::Closure
> established_callbacks_
;
111 uint32 next_create_gpu_memory_buffer_request_id_
;
112 typedef std::map
<uint32
, CreateGpuMemoryBufferCallback
>
113 CreateGpuMemoryBufferCallbackMap
;
114 CreateGpuMemoryBufferCallbackMap create_gpu_memory_buffer_requests_
;
116 static BrowserGpuChannelHostFactory
* instance_
;
118 DISALLOW_COPY_AND_ASSIGN(BrowserGpuChannelHostFactory
);
121 } // namespace content
123 #endif // CONTENT_BROWSER_GPU_BROWSER_GPU_CHANNEL_HOST_FACTORY_H_