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 #include "content/common/gpu/gpu_channel_manager.h"
8 #include "base/command_line.h"
9 #include "content/common/gpu/gpu_channel.h"
10 #include "content/common/gpu/gpu_memory_buffer_factory.h"
11 #include "content/common/gpu/gpu_memory_manager.h"
12 #include "content/common/gpu/gpu_messages.h"
13 #include "content/common/message_router.h"
14 #include "gpu/command_buffer/common/value_state.h"
15 #include "gpu/command_buffer/service/feature_info.h"
16 #include "gpu/command_buffer/service/gpu_switches.h"
17 #include "gpu/command_buffer/service/mailbox_manager_impl.h"
18 #include "gpu/command_buffer/service/memory_program_cache.h"
19 #include "gpu/command_buffer/service/shader_translator_cache.h"
20 #include "gpu/command_buffer/service/sync_point_manager.h"
21 #include "ipc/message_filter.h"
22 #include "ui/gl/gl_bindings.h"
23 #include "ui/gl/gl_share_group.h"
24 #if defined(USE_OZONE)
25 #include "ui/ozone/public/gpu_platform_support.h"
26 #include "ui/ozone/public/ozone_platform.h"
33 class GpuChannelManagerMessageFilter
: public IPC::MessageFilter
{
35 GpuChannelManagerMessageFilter(
36 GpuMemoryBufferFactory
* gpu_memory_buffer_factory
)
37 : sender_(NULL
), gpu_memory_buffer_factory_(gpu_memory_buffer_factory
) {}
39 void OnFilterAdded(IPC::Sender
* sender
) override
{
44 void OnFilterRemoved() override
{
49 bool OnMessageReceived(const IPC::Message
& message
) override
{
52 IPC_BEGIN_MESSAGE_MAP(GpuChannelManagerMessageFilter
, message
)
53 IPC_MESSAGE_HANDLER(GpuMsg_CreateGpuMemoryBuffer
, OnCreateGpuMemoryBuffer
)
54 IPC_MESSAGE_UNHANDLED(handled
= false)
60 ~GpuChannelManagerMessageFilter() override
{}
62 // GPU IO thread bounces off GpuMsg_CreateGpuMemoryBuffer message, because
63 // the UI thread in the browser process must remain fast at all times.
64 void OnCreateGpuMemoryBuffer(
65 const GpuMsg_CreateGpuMemoryBuffer_Params
& params
) {
67 "GpuChannelManagerMessageFilter::OnCreateGpuMemoryBuffer",
68 "id", params
.id
, "client_id", params
.client_id
);
69 sender_
->Send(new GpuHostMsg_GpuMemoryBufferCreated(
70 gpu_memory_buffer_factory_
->CreateGpuMemoryBuffer(
71 params
.id
, params
.size
, params
.format
, params
.usage
,
72 params
.client_id
, params
.surface_handle
)));
76 GpuMemoryBufferFactory
* gpu_memory_buffer_factory_
;
79 gfx::GpuMemoryBufferType
GetGpuMemoryBufferFactoryType() {
80 std::vector
<gfx::GpuMemoryBufferType
> supported_types
;
81 GpuMemoryBufferFactory::GetSupportedTypes(&supported_types
);
82 DCHECK(!supported_types
.empty());
83 return supported_types
[0];
88 GpuChannelManager::GpuChannelManager(MessageRouter
* router
,
89 GpuWatchdog
* watchdog
,
90 base::MessageLoopProxy
* io_message_loop
,
91 base::WaitableEvent
* shutdown_event
,
92 IPC::SyncChannel
* channel
)
93 : io_message_loop_(io_message_loop
),
94 shutdown_event_(shutdown_event
),
98 GpuMemoryManager::kDefaultMaxSurfacesWithFrontbufferSoftLimit
),
100 sync_point_manager_(gpu::SyncPointManager::Create(false)),
101 gpu_memory_buffer_factory_(
102 GpuMemoryBufferFactory::Create(GetGpuMemoryBufferFactoryType())),
105 new GpuChannelManagerMessageFilter(gpu_memory_buffer_factory_
.get())),
106 relinquish_resources_pending_(false),
107 weak_factory_(this) {
109 DCHECK(io_message_loop
);
110 DCHECK(shutdown_event
);
111 channel_
->AddFilter(filter_
.get());
114 GpuChannelManager::~GpuChannelManager() {
115 gpu_channels_
.clear();
116 if (default_offscreen_surface_
.get()) {
117 default_offscreen_surface_
->Destroy();
118 default_offscreen_surface_
= NULL
;
122 gpu::gles2::ProgramCache
* GpuChannelManager::program_cache() {
123 if (!program_cache_
.get() &&
124 (gfx::g_driver_gl
.ext
.b_GL_ARB_get_program_binary
||
125 gfx::g_driver_gl
.ext
.b_GL_OES_get_program_binary
) &&
126 !base::CommandLine::ForCurrentProcess()->HasSwitch(
127 switches::kDisableGpuProgramCache
)) {
128 program_cache_
.reset(new gpu::gles2::MemoryProgramCache());
130 return program_cache_
.get();
133 gpu::gles2::ShaderTranslatorCache
*
134 GpuChannelManager::shader_translator_cache() {
135 if (!shader_translator_cache_
.get())
136 shader_translator_cache_
= new gpu::gles2::ShaderTranslatorCache
;
137 return shader_translator_cache_
.get();
140 void GpuChannelManager::RemoveChannel(int client_id
) {
141 Send(new GpuHostMsg_DestroyChannel(client_id
));
142 gpu_channels_
.erase(client_id
);
143 CheckRelinquishGpuResources();
146 int GpuChannelManager::GenerateRouteID() {
147 static int last_id
= 0;
151 void GpuChannelManager::AddRoute(int32 routing_id
, IPC::Listener
* listener
) {
152 router_
->AddRoute(routing_id
, listener
);
155 void GpuChannelManager::RemoveRoute(int32 routing_id
) {
156 router_
->RemoveRoute(routing_id
);
159 GpuChannel
* GpuChannelManager::LookupChannel(int32 client_id
) {
160 GpuChannelMap::const_iterator iter
= gpu_channels_
.find(client_id
);
161 if (iter
== gpu_channels_
.end())
167 bool GpuChannelManager::OnMessageReceived(const IPC::Message
& msg
) {
169 IPC_BEGIN_MESSAGE_MAP(GpuChannelManager
, msg
)
170 IPC_MESSAGE_HANDLER(GpuMsg_EstablishChannel
, OnEstablishChannel
)
171 IPC_MESSAGE_HANDLER(GpuMsg_CloseChannel
, OnCloseChannel
)
172 IPC_MESSAGE_HANDLER(GpuMsg_CreateViewCommandBuffer
,
173 OnCreateViewCommandBuffer
)
174 IPC_MESSAGE_HANDLER(GpuMsg_DestroyGpuMemoryBuffer
, OnDestroyGpuMemoryBuffer
)
175 IPC_MESSAGE_HANDLER(GpuMsg_LoadedShader
, OnLoadedShader
)
176 IPC_MESSAGE_HANDLER(GpuMsg_RelinquishResources
, OnRelinquishResources
)
177 IPC_MESSAGE_HANDLER(GpuMsg_UpdateValueState
, OnUpdateValueState
)
178 IPC_MESSAGE_UNHANDLED(handled
= false)
179 IPC_END_MESSAGE_MAP()
183 bool GpuChannelManager::Send(IPC::Message
* msg
) { return router_
->Send(msg
); }
185 void GpuChannelManager::OnEstablishChannel(int client_id
,
187 bool allow_future_sync_points
) {
188 IPC::ChannelHandle channel_handle
;
190 gfx::GLShareGroup
* share_group
= NULL
;
191 gpu::gles2::MailboxManager
* mailbox_manager
= NULL
;
193 if (!share_group_
.get()) {
194 share_group_
= new gfx::GLShareGroup
;
195 DCHECK(!mailbox_manager_
.get());
196 mailbox_manager_
= new gpu::gles2::MailboxManagerImpl
;
198 share_group
= share_group_
.get();
199 mailbox_manager
= mailbox_manager_
.get();
202 scoped_ptr
<GpuChannel
> channel(new GpuChannel(this,
208 allow_future_sync_points
));
209 channel
->Init(io_message_loop_
.get(), shutdown_event_
);
210 channel_handle
.name
= channel
->GetChannelName();
212 #if defined(OS_POSIX)
213 // On POSIX, pass the renderer-side FD. Also mark it as auto-close so
214 // that it gets closed after it has been sent.
215 base::ScopedFD renderer_fd
= channel
->TakeRendererFileDescriptor();
216 DCHECK(renderer_fd
.is_valid());
217 channel_handle
.socket
= base::FileDescriptor(renderer_fd
.Pass());
220 gpu_channels_
.set(client_id
, channel
.Pass());
222 Send(new GpuHostMsg_ChannelEstablished(channel_handle
));
225 void GpuChannelManager::OnCloseChannel(
226 const IPC::ChannelHandle
& channel_handle
) {
227 for (GpuChannelMap::iterator iter
= gpu_channels_
.begin();
228 iter
!= gpu_channels_
.end(); ++iter
) {
229 if (iter
->second
->GetChannelName() == channel_handle
.name
) {
230 gpu_channels_
.erase(iter
);
231 CheckRelinquishGpuResources();
237 void GpuChannelManager::OnCreateViewCommandBuffer(
238 const gfx::GLSurfaceHandle
& window
,
241 const GPUCreateCommandBufferConfig
& init_params
,
244 CreateCommandBufferResult result
= CREATE_COMMAND_BUFFER_FAILED
;
246 GpuChannelMap::const_iterator iter
= gpu_channels_
.find(client_id
);
247 if (iter
!= gpu_channels_
.end()) {
248 result
= iter
->second
->CreateViewCommandBuffer(
249 window
, surface_id
, init_params
, route_id
);
252 Send(new GpuHostMsg_CommandBufferCreated(result
));
255 void GpuChannelManager::DestroyGpuMemoryBuffer(
256 gfx::GpuMemoryBufferId id
,
258 io_message_loop_
->PostTask(
260 base::Bind(&GpuChannelManager::DestroyGpuMemoryBufferOnIO
,
261 base::Unretained(this),
266 void GpuChannelManager::DestroyGpuMemoryBufferOnIO(
267 gfx::GpuMemoryBufferId id
,
269 gpu_memory_buffer_factory_
->DestroyGpuMemoryBuffer(id
, client_id
);
272 void GpuChannelManager::OnDestroyGpuMemoryBuffer(
273 gfx::GpuMemoryBufferId id
,
277 DestroyGpuMemoryBuffer(id
, client_id
);
279 sync_point_manager()->AddSyncPointCallback(
281 base::Bind(&GpuChannelManager::DestroyGpuMemoryBuffer
,
282 base::Unretained(this),
288 void GpuChannelManager::OnUpdateValueState(
289 int client_id
, unsigned int target
, const gpu::ValueState
& state
) {
290 // Only pass updated state to the channel corresponding to the
291 // render_widget_host where the event originated.
292 GpuChannelMap::const_iterator iter
= gpu_channels_
.find(client_id
);
293 if (iter
!= gpu_channels_
.end()) {
294 iter
->second
->HandleUpdateValueState(target
, state
);
298 void GpuChannelManager::OnLoadedShader(std::string program_proto
) {
300 program_cache()->LoadProgram(program_proto
);
303 bool GpuChannelManager::HandleMessagesScheduled() {
304 for (GpuChannelMap::iterator iter
= gpu_channels_
.begin();
305 iter
!= gpu_channels_
.end(); ++iter
) {
306 if (iter
->second
->handle_messages_scheduled())
312 uint64
GpuChannelManager::MessagesProcessed() {
313 uint64 messages_processed
= 0;
315 for (GpuChannelMap::iterator iter
= gpu_channels_
.begin();
316 iter
!= gpu_channels_
.end(); ++iter
) {
317 messages_processed
+= iter
->second
->messages_processed();
319 return messages_processed
;
322 void GpuChannelManager::LoseAllContexts() {
323 for (GpuChannelMap::iterator iter
= gpu_channels_
.begin();
324 iter
!= gpu_channels_
.end(); ++iter
) {
325 iter
->second
->MarkAllContextsLost();
327 base::MessageLoop::current()->PostTask(
329 base::Bind(&GpuChannelManager::OnLoseAllContexts
,
330 weak_factory_
.GetWeakPtr()));
333 void GpuChannelManager::OnLoseAllContexts() {
334 gpu_channels_
.clear();
335 CheckRelinquishGpuResources();
338 gfx::GLSurface
* GpuChannelManager::GetDefaultOffscreenSurface() {
339 if (!default_offscreen_surface_
.get()) {
340 default_offscreen_surface_
=
341 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size());
343 return default_offscreen_surface_
.get();
346 void GpuChannelManager::OnRelinquishResources() {
347 relinquish_resources_pending_
= true;
348 CheckRelinquishGpuResources();
351 void GpuChannelManager::CheckRelinquishGpuResources() {
352 if (relinquish_resources_pending_
&& gpu_channels_
.size() <= 1) {
353 relinquish_resources_pending_
= false;
354 if (default_offscreen_surface_
.get()) {
355 default_offscreen_surface_
->DestroyAndTerminateDisplay();
356 default_offscreen_surface_
= NULL
;
358 #if defined(USE_OZONE)
359 ui::OzonePlatform::GetInstance()
360 ->GetGpuPlatformSupport()
361 ->RelinquishGpuResources(
362 base::Bind(&GpuChannelManager::OnResourcesRelinquished
,
363 weak_factory_
.GetWeakPtr()));
365 OnResourcesRelinquished();
370 void GpuChannelManager::OnResourcesRelinquished() {
371 Send(new GpuHostMsg_ResourcesRelinquished());
374 } // namespace content