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 "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "content/common/gpu/gpu_channel.h"
13 #include "content/common/gpu/gpu_memory_buffer_factory.h"
14 #include "content/common/gpu/gpu_memory_manager.h"
15 #include "content/common/gpu/gpu_messages.h"
16 #include "content/common/message_router.h"
17 #include "gpu/command_buffer/common/value_state.h"
18 #include "gpu/command_buffer/service/feature_info.h"
19 #include "gpu/command_buffer/service/gpu_switches.h"
20 #include "gpu/command_buffer/service/mailbox_manager.h"
21 #include "gpu/command_buffer/service/memory_program_cache.h"
22 #include "gpu/command_buffer/service/shader_translator_cache.h"
23 #include "gpu/command_buffer/service/sync_point_manager.h"
24 #include "ipc/message_filter.h"
25 #include "ui/gl/gl_bindings.h"
26 #include "ui/gl/gl_share_group.h"
27 #if defined(USE_OZONE)
28 #include "ui/ozone/public/gpu_platform_support.h"
29 #include "ui/ozone/public/ozone_platform.h"
34 GpuChannelManager::GpuChannelManager(
35 MessageRouter
* router
,
36 GpuWatchdog
* watchdog
,
37 base::SingleThreadTaskRunner
* io_task_runner
,
38 base::WaitableEvent
* shutdown_event
,
39 IPC::SyncChannel
* channel
,
40 IPC::AttachmentBroker
* broker
,
41 gpu::SyncPointManager
* sync_point_manager
,
42 GpuMemoryBufferFactory
* gpu_memory_buffer_factory
)
43 : io_task_runner_(io_task_runner
),
44 shutdown_event_(shutdown_event
),
48 GpuMemoryManager::kDefaultMaxSurfacesWithFrontbufferSoftLimit
),
50 sync_point_manager_(sync_point_manager
),
51 gpu_memory_buffer_factory_(gpu_memory_buffer_factory
),
53 relinquish_resources_pending_(false),
54 attachment_broker_(broker
),
57 DCHECK(io_task_runner
);
58 DCHECK(shutdown_event
);
61 GpuChannelManager::~GpuChannelManager() {
62 gpu_channels_
.clear();
63 if (default_offscreen_surface_
.get()) {
64 default_offscreen_surface_
->Destroy();
65 default_offscreen_surface_
= NULL
;
69 gpu::gles2::ProgramCache
* GpuChannelManager::program_cache() {
70 if (!program_cache_
.get() &&
71 (gfx::g_driver_gl
.ext
.b_GL_ARB_get_program_binary
||
72 gfx::g_driver_gl
.ext
.b_GL_OES_get_program_binary
) &&
73 !base::CommandLine::ForCurrentProcess()->HasSwitch(
74 switches::kDisableGpuProgramCache
)) {
75 program_cache_
.reset(new gpu::gles2::MemoryProgramCache());
77 return program_cache_
.get();
80 gpu::gles2::ShaderTranslatorCache
*
81 GpuChannelManager::shader_translator_cache() {
82 if (!shader_translator_cache_
.get())
83 shader_translator_cache_
= new gpu::gles2::ShaderTranslatorCache
;
84 return shader_translator_cache_
.get();
87 void GpuChannelManager::RemoveChannel(int client_id
) {
88 Send(new GpuHostMsg_DestroyChannel(client_id
));
89 gpu_channels_
.erase(client_id
);
90 CheckRelinquishGpuResources();
93 int GpuChannelManager::GenerateRouteID() {
94 static int last_id
= 0;
98 void GpuChannelManager::AddRoute(int32 routing_id
, IPC::Listener
* listener
) {
99 router_
->AddRoute(routing_id
, listener
);
102 void GpuChannelManager::RemoveRoute(int32 routing_id
) {
103 router_
->RemoveRoute(routing_id
);
106 GpuChannel
* GpuChannelManager::LookupChannel(int32 client_id
) {
107 GpuChannelMap::const_iterator iter
= gpu_channels_
.find(client_id
);
108 if (iter
== gpu_channels_
.end())
114 bool GpuChannelManager::OnMessageReceived(const IPC::Message
& msg
) {
116 IPC_BEGIN_MESSAGE_MAP(GpuChannelManager
, msg
)
117 IPC_MESSAGE_HANDLER(GpuMsg_EstablishChannel
, OnEstablishChannel
)
118 IPC_MESSAGE_HANDLER(GpuMsg_CloseChannel
, OnCloseChannel
)
119 IPC_MESSAGE_HANDLER(GpuMsg_CreateViewCommandBuffer
,
120 OnCreateViewCommandBuffer
)
121 IPC_MESSAGE_HANDLER(GpuMsg_DestroyGpuMemoryBuffer
, OnDestroyGpuMemoryBuffer
)
122 IPC_MESSAGE_HANDLER(GpuMsg_LoadedShader
, OnLoadedShader
)
123 IPC_MESSAGE_HANDLER(GpuMsg_RelinquishResources
, OnRelinquishResources
)
124 IPC_MESSAGE_HANDLER(GpuMsg_UpdateValueState
, OnUpdateValueState
)
125 IPC_MESSAGE_UNHANDLED(handled
= false)
126 IPC_END_MESSAGE_MAP()
130 bool GpuChannelManager::Send(IPC::Message
* msg
) { return router_
->Send(msg
); }
132 void GpuChannelManager::OnEstablishChannel(int client_id
,
134 bool allow_future_sync_points
) {
135 IPC::ChannelHandle channel_handle
;
137 gfx::GLShareGroup
* share_group
= NULL
;
138 gpu::gles2::MailboxManager
* mailbox_manager
= NULL
;
140 if (!share_group_
.get()) {
141 share_group_
= new gfx::GLShareGroup
;
142 DCHECK(!mailbox_manager_
.get());
143 mailbox_manager_
= gpu::gles2::MailboxManager::Create();
145 share_group
= share_group_
.get();
146 mailbox_manager
= mailbox_manager_
.get();
149 scoped_ptr
<GpuChannel
> channel(new GpuChannel(this,
155 allow_future_sync_points
));
156 channel
->Init(io_task_runner_
.get(), shutdown_event_
, attachment_broker_
);
157 channel_handle
.name
= channel
->GetChannelName();
159 #if defined(OS_POSIX)
160 // On POSIX, pass the renderer-side FD. Also mark it as auto-close so
161 // that it gets closed after it has been sent.
162 base::ScopedFD renderer_fd
= channel
->TakeRendererFileDescriptor();
163 DCHECK(renderer_fd
.is_valid());
164 channel_handle
.socket
= base::FileDescriptor(renderer_fd
.Pass());
167 gpu_channels_
.set(client_id
, channel
.Pass());
169 Send(new GpuHostMsg_ChannelEstablished(channel_handle
));
172 void GpuChannelManager::OnCloseChannel(
173 const IPC::ChannelHandle
& channel_handle
) {
174 for (GpuChannelMap::iterator iter
= gpu_channels_
.begin();
175 iter
!= gpu_channels_
.end(); ++iter
) {
176 if (iter
->second
->GetChannelName() == channel_handle
.name
) {
177 gpu_channels_
.erase(iter
);
178 CheckRelinquishGpuResources();
184 void GpuChannelManager::OnCreateViewCommandBuffer(
185 const gfx::GLSurfaceHandle
& window
,
188 const GPUCreateCommandBufferConfig
& init_params
,
191 CreateCommandBufferResult result
= CREATE_COMMAND_BUFFER_FAILED
;
193 GpuChannelMap::const_iterator iter
= gpu_channels_
.find(client_id
);
194 if (iter
!= gpu_channels_
.end()) {
195 result
= iter
->second
->CreateViewCommandBuffer(
196 window
, surface_id
, init_params
, route_id
);
199 Send(new GpuHostMsg_CommandBufferCreated(result
));
202 void GpuChannelManager::DestroyGpuMemoryBuffer(
203 gfx::GpuMemoryBufferId id
,
205 io_task_runner_
->PostTask(
206 FROM_HERE
, base::Bind(&GpuChannelManager::DestroyGpuMemoryBufferOnIO
,
207 base::Unretained(this), id
, client_id
));
210 void GpuChannelManager::DestroyGpuMemoryBufferOnIO(
211 gfx::GpuMemoryBufferId id
,
213 gpu_memory_buffer_factory_
->DestroyGpuMemoryBuffer(id
, client_id
);
216 void GpuChannelManager::OnDestroyGpuMemoryBuffer(
217 gfx::GpuMemoryBufferId id
,
221 DestroyGpuMemoryBuffer(id
, client_id
);
223 sync_point_manager()->AddSyncPointCallback(
225 base::Bind(&GpuChannelManager::DestroyGpuMemoryBuffer
,
226 base::Unretained(this),
232 void GpuChannelManager::OnUpdateValueState(
233 int client_id
, unsigned int target
, const gpu::ValueState
& state
) {
234 // Only pass updated state to the channel corresponding to the
235 // render_widget_host where the event originated.
236 GpuChannelMap::const_iterator iter
= gpu_channels_
.find(client_id
);
237 if (iter
!= gpu_channels_
.end()) {
238 iter
->second
->HandleUpdateValueState(target
, state
);
242 void GpuChannelManager::OnLoadedShader(std::string program_proto
) {
244 program_cache()->LoadProgram(program_proto
);
247 bool GpuChannelManager::HandleMessagesScheduled() {
248 for (GpuChannelMap::iterator iter
= gpu_channels_
.begin();
249 iter
!= gpu_channels_
.end(); ++iter
) {
250 if (iter
->second
->handle_messages_scheduled())
256 uint64
GpuChannelManager::MessagesProcessed() {
257 uint64 messages_processed
= 0;
259 for (GpuChannelMap::iterator iter
= gpu_channels_
.begin();
260 iter
!= gpu_channels_
.end(); ++iter
) {
261 messages_processed
+= iter
->second
->messages_processed();
263 return messages_processed
;
266 void GpuChannelManager::LoseAllContexts() {
267 for (GpuChannelMap::iterator iter
= gpu_channels_
.begin();
268 iter
!= gpu_channels_
.end(); ++iter
) {
269 iter
->second
->MarkAllContextsLost();
271 base::ThreadTaskRunnerHandle::Get()->PostTask(
272 FROM_HERE
, base::Bind(&GpuChannelManager::OnLoseAllContexts
,
273 weak_factory_
.GetWeakPtr()));
276 void GpuChannelManager::OnLoseAllContexts() {
277 gpu_channels_
.clear();
278 CheckRelinquishGpuResources();
281 gfx::GLSurface
* GpuChannelManager::GetDefaultOffscreenSurface() {
282 if (!default_offscreen_surface_
.get()) {
283 default_offscreen_surface_
=
284 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size());
286 return default_offscreen_surface_
.get();
289 void GpuChannelManager::OnRelinquishResources() {
290 relinquish_resources_pending_
= true;
291 CheckRelinquishGpuResources();
294 void GpuChannelManager::CheckRelinquishGpuResources() {
295 if (relinquish_resources_pending_
&& gpu_channels_
.size() <= 1) {
296 relinquish_resources_pending_
= false;
297 if (default_offscreen_surface_
.get()) {
298 default_offscreen_surface_
->DestroyAndTerminateDisplay();
299 default_offscreen_surface_
= NULL
;
301 #if defined(USE_OZONE)
302 ui::OzonePlatform::GetInstance()
303 ->GetGpuPlatformSupport()
304 ->RelinquishGpuResources(
305 base::Bind(&GpuChannelManager::OnResourcesRelinquished
,
306 weak_factory_
.GetWeakPtr()));
308 OnResourcesRelinquished();
313 void GpuChannelManager::OnResourcesRelinquished() {
314 Send(new GpuHostMsg_ResourcesRelinquished());
317 } // namespace content