OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/gpu/gpu_child_thread.h" | 5 #include "content/gpu/gpu_child_thread.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/threading/worker_pool.h" | 12 #include "base/threading/worker_pool.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "content/child/child_process.h" | 14 #include "content/child/child_process.h" |
15 #include "content/child/thread_safe_sender.h" | 15 #include "content/child/thread_safe_sender.h" |
| 16 #include "content/common/gpu/establish_channel_params.h" |
| 17 #include "content/common/gpu/gpu_host_messages.h" |
16 #include "content/common/gpu/gpu_memory_buffer_factory.h" | 18 #include "content/common/gpu/gpu_memory_buffer_factory.h" |
17 #include "content/common/gpu/gpu_messages.h" | |
18 #include "content/common/gpu/media/gpu_video_decode_accelerator.h" | 19 #include "content/common/gpu/media/gpu_video_decode_accelerator.h" |
19 #include "content/gpu/gpu_process_control_impl.h" | 20 #include "content/gpu/gpu_process_control_impl.h" |
20 #include "content/gpu/gpu_watchdog_thread.h" | 21 #include "content/gpu/gpu_watchdog_thread.h" |
21 #include "content/public/common/content_client.h" | 22 #include "content/public/common/content_client.h" |
22 #include "content/public/common/content_switches.h" | 23 #include "content/public/common/content_switches.h" |
23 #include "content/public/gpu/content_gpu_client.h" | 24 #include "content/public/gpu/content_gpu_client.h" |
24 #include "gpu/config/gpu_info_collector.h" | 25 #include "gpu/config/gpu_info_collector.h" |
25 #include "ipc/ipc_channel_handle.h" | 26 #include "ipc/ipc_channel_handle.h" |
26 #include "ipc/ipc_sync_message_filter.h" | 27 #include "ipc/ipc_sync_message_filter.h" |
27 #include "ui/gl/gl_implementation.h" | 28 #include "ui/gl/gl_implementation.h" |
(...skipping 10 matching lines...) Expand all Loading... |
38 static base::LazyInstance<scoped_refptr<ThreadSafeSender> > | 39 static base::LazyInstance<scoped_refptr<ThreadSafeSender> > |
39 g_thread_safe_sender = LAZY_INSTANCE_INITIALIZER; | 40 g_thread_safe_sender = LAZY_INSTANCE_INITIALIZER; |
40 | 41 |
41 bool GpuProcessLogMessageHandler(int severity, | 42 bool GpuProcessLogMessageHandler(int severity, |
42 const char* file, int line, | 43 const char* file, int line, |
43 size_t message_start, | 44 size_t message_start, |
44 const std::string& str) { | 45 const std::string& str) { |
45 std::string header = str.substr(0, message_start); | 46 std::string header = str.substr(0, message_start); |
46 std::string message = str.substr(message_start); | 47 std::string message = str.substr(message_start); |
47 | 48 |
48 g_thread_safe_sender.Get()->Send(new GpuHostMsg_OnLogMessage( | 49 g_thread_safe_sender.Get()->Send( |
49 severity, header, message)); | 50 new GpuHostMsg_OnLogMessage(severity, header, message)); |
50 | 51 |
51 return false; | 52 return false; |
52 } | 53 } |
53 | 54 |
54 // Message filter used to to handle GpuMsg_CreateGpuMemoryBuffer messages on | 55 // Message filter used to to handle GpuMsg_CreateGpuMemoryBuffer messages |
55 // the IO thread. This allows the UI thread in the browser process to remain | 56 // on the IO thread. This allows the UI thread in the browser process to remain |
56 // fast at all times. | 57 // fast at all times. |
57 class GpuMemoryBufferMessageFilter : public IPC::MessageFilter { | 58 class GpuMemoryBufferMessageFilter : public IPC::MessageFilter { |
58 public: | 59 public: |
59 explicit GpuMemoryBufferMessageFilter( | 60 explicit GpuMemoryBufferMessageFilter( |
60 GpuMemoryBufferFactory* gpu_memory_buffer_factory) | 61 GpuMemoryBufferFactory* gpu_memory_buffer_factory) |
61 : gpu_memory_buffer_factory_(gpu_memory_buffer_factory), | 62 : gpu_memory_buffer_factory_(gpu_memory_buffer_factory), |
62 sender_(nullptr) {} | 63 sender_(nullptr) {} |
63 | 64 |
64 // Overridden from IPC::MessageFilter: | 65 // Overridden from IPC::MessageFilter: |
65 void OnFilterAdded(IPC::Sender* sender) override { | 66 void OnFilterAdded(IPC::Sender* sender) override { |
66 DCHECK(!sender_); | 67 DCHECK(!sender_); |
67 sender_ = sender; | 68 sender_ = sender; |
68 } | 69 } |
69 void OnFilterRemoved() override { | 70 void OnFilterRemoved() override { |
70 DCHECK(sender_); | 71 DCHECK(sender_); |
71 sender_ = nullptr; | 72 sender_ = nullptr; |
72 } | 73 } |
73 bool OnMessageReceived(const IPC::Message& message) override { | 74 bool OnMessageReceived(const IPC::Message& message) override { |
74 DCHECK(sender_); | 75 DCHECK(sender_); |
75 bool handled = true; | 76 bool handled = true; |
76 IPC_BEGIN_MESSAGE_MAP(GpuMemoryBufferMessageFilter, message) | 77 IPC_BEGIN_MESSAGE_MAP(GpuMemoryBufferMessageFilter, message) |
77 IPC_MESSAGE_HANDLER(GpuMsg_CreateGpuMemoryBuffer, OnCreateGpuMemoryBuffer) | 78 IPC_MESSAGE_HANDLER(GpuMsg_CreateGpuMemoryBuffer, OnCreateGpuMemoryBuffer) |
78 IPC_MESSAGE_HANDLER(GpuMsg_CreateGpuMemoryBufferFromHandle, | 79 IPC_MESSAGE_HANDLER(GpuMsg_CreateGpuMemoryBufferFromHandle, |
79 OnCreateGpuMemoryBufferFromHandle) | 80 OnCreateGpuMemoryBufferFromHandle) |
80 IPC_MESSAGE_UNHANDLED(handled = false) | 81 IPC_MESSAGE_UNHANDLED(handled = false) |
81 IPC_END_MESSAGE_MAP() | 82 IPC_END_MESSAGE_MAP() |
82 return handled; | 83 return handled; |
83 } | 84 } |
84 | 85 |
85 protected: | 86 protected: |
86 ~GpuMemoryBufferMessageFilter() override {} | 87 ~GpuMemoryBufferMessageFilter() override {} |
87 | 88 |
88 void OnCreateGpuMemoryBuffer( | 89 void OnCreateGpuMemoryBuffer( |
89 const GpuMsg_CreateGpuMemoryBuffer_Params& params) { | 90 const GpuMsg_CreateGpuMemoryBuffer_Params& params) { |
90 TRACE_EVENT2("gpu", "GpuMemoryBufferMessageFilter::OnCreateGpuMemoryBuffer", | 91 TRACE_EVENT2("gpu", "GpuMemoryBufferMessageFilter::OnCreateGpuMemoryBuffer", |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 return true; | 246 return true; |
246 #endif | 247 #endif |
247 | 248 |
248 return false; | 249 return false; |
249 } | 250 } |
250 | 251 |
251 bool GpuChildThread::OnMessageReceived(const IPC::Message& msg) { | 252 bool GpuChildThread::OnMessageReceived(const IPC::Message& msg) { |
252 if (ChildThreadImpl::OnMessageReceived(msg)) | 253 if (ChildThreadImpl::OnMessageReceived(msg)) |
253 return true; | 254 return true; |
254 | 255 |
255 return gpu_channel_manager_.get() && | 256 bool handled = true; |
256 gpu_channel_manager_->OnMessageReceived(msg); | 257 IPC_BEGIN_MESSAGE_MAP(GpuChildThread, msg) |
| 258 #if defined(OS_MACOSX) |
| 259 IPC_MESSAGE_HANDLER(AcceleratedSurfaceMsg_BufferPresented, |
| 260 OnBufferPresented) |
| 261 #endif |
| 262 IPC_MESSAGE_HANDLER(GpuMsg_EstablishChannel, OnEstablishChannel) |
| 263 IPC_MESSAGE_HANDLER(GpuMsg_CloseChannel, OnCloseChannel) |
| 264 IPC_MESSAGE_HANDLER(GpuMsg_DestroyGpuMemoryBuffer, OnDestroyGpuMemoryBuffer) |
| 265 IPC_MESSAGE_HANDLER(GpuMsg_LoadedShader, OnLoadedShader) |
| 266 IPC_MESSAGE_HANDLER(GpuMsg_UpdateValueState, OnUpdateValueState) |
| 267 #if defined(OS_ANDROID) |
| 268 IPC_MESSAGE_HANDLER(GpuMsg_WakeUpGpu, OnWakeUpGpu); |
| 269 #endif |
| 270 IPC_MESSAGE_UNHANDLED(handled = false) |
| 271 IPC_END_MESSAGE_MAP() |
| 272 if (handled) |
| 273 return true; |
| 274 |
| 275 return false; |
| 276 } |
| 277 |
| 278 void GpuChildThread::AddSubscription(int32_t client_id, unsigned int target) { |
| 279 Send(new GpuHostMsg_AddSubscription(client_id, target)); |
| 280 } |
| 281 |
| 282 void GpuChildThread::ChannelEstablished( |
| 283 const IPC::ChannelHandle& channel_handle) { |
| 284 Send(new GpuHostMsg_ChannelEstablished(channel_handle)); |
| 285 } |
| 286 |
| 287 void GpuChildThread::DidCreateOffscreenContext(const GURL& active_url) { |
| 288 Send(new GpuHostMsg_DidCreateOffscreenContext(active_url)); |
| 289 } |
| 290 |
| 291 void GpuChildThread::DidDestroyChannel(int client_id) { |
| 292 Send(new GpuHostMsg_DestroyChannel(client_id)); |
| 293 } |
| 294 |
| 295 void GpuChildThread::DidDestroyOffscreenContext(const GURL& active_url) { |
| 296 Send(new GpuHostMsg_DidDestroyOffscreenContext(active_url)); |
| 297 } |
| 298 |
| 299 void GpuChildThread::DidLoseContext(bool offscreen, |
| 300 gpu::error::ContextLostReason reason, |
| 301 const GURL& active_url) { |
| 302 Send(new GpuHostMsg_DidLoseContext(offscreen, reason, active_url)); |
| 303 } |
| 304 |
| 305 void GpuChildThread::GpuMemoryUmaStats(const GPUMemoryUmaStats& params) { |
| 306 Send(new GpuHostMsg_GpuMemoryUmaStats(params)); |
| 307 } |
| 308 |
| 309 void GpuChildThread::RemoveSubscription(int32_t client_id, |
| 310 unsigned int target) { |
| 311 Send(new GpuHostMsg_RemoveSubscription(client_id, target)); |
| 312 } |
| 313 |
| 314 #if defined(OS_MACOSX) |
| 315 void GpuChildThread::SendAcceleratedSurfaceBuffersSwapped( |
| 316 const AcceleratedSurfaceBuffersSwappedParams& params) { |
| 317 Send(new GpuHostMsg_AcceleratedSurfaceBuffersSwapped(params)); |
| 318 } |
| 319 #endif |
| 320 |
| 321 #if defined(OS_WIN) |
| 322 void GpuChildThread::SendAcceleratedSurfaceCreatedChildWindow( |
| 323 const gfx::PluginWindowHandle& parent_window, |
| 324 const gfx::PluginWindowHandle& child_window) { |
| 325 Send(new GpuHostMsg_AcceleratedSurfaceCreatedChildWindow(parent_window, |
| 326 child_window)); |
| 327 } |
| 328 #endif |
| 329 |
| 330 void GpuChildThread::StoreShaderToDisk(int32_t client_id, |
| 331 const std::string& key, |
| 332 const std::string& shader) { |
| 333 Send(new GpuHostMsg_CacheShader(client_id, key, shader)); |
257 } | 334 } |
258 | 335 |
259 void GpuChildThread::OnInitialize() { | 336 void GpuChildThread::OnInitialize() { |
260 // Record initialization only after collecting the GPU info because that can | 337 // Record initialization only after collecting the GPU info because that can |
261 // take a significant amount of time. | 338 // take a significant amount of time. |
262 gpu_info_.initialization_time = base::Time::Now() - process_start_time_; | 339 gpu_info_.initialization_time = base::Time::Now() - process_start_time_; |
263 Send(new GpuHostMsg_Initialized(!dead_on_arrival_, gpu_info_)); | 340 Send(new GpuHostMsg_Initialized(!dead_on_arrival_, gpu_info_)); |
264 while (!deferred_messages_.empty()) { | 341 while (!deferred_messages_.empty()) { |
265 Send(deferred_messages_.front()); | 342 Send(deferred_messages_.front()); |
266 deferred_messages_.pop(); | 343 deferred_messages_.pop(); |
267 } | 344 } |
268 | 345 |
269 if (dead_on_arrival_) { | 346 if (dead_on_arrival_) { |
270 LOG(ERROR) << "Exiting GPU process due to errors during initialization"; | 347 LOG(ERROR) << "Exiting GPU process due to errors during initialization"; |
271 base::MessageLoop::current()->QuitWhenIdle(); | 348 base::MessageLoop::current()->QuitWhenIdle(); |
272 return; | 349 return; |
273 } | 350 } |
274 | 351 |
275 // We don't need to pipe log messages if we are running the GPU thread in | 352 // We don't need to pipe log messages if we are running the GPU thread in |
276 // the browser process. | 353 // the browser process. |
277 if (!in_browser_process_) | 354 if (!in_browser_process_) |
278 logging::SetLogMessageHandler(GpuProcessLogMessageHandler); | 355 logging::SetLogMessageHandler(GpuProcessLogMessageHandler); |
279 | 356 |
280 // Defer creation of the render thread. This is to prevent it from handling | 357 // Defer creation of the render thread. This is to prevent it from handling |
281 // IPC messages before the sandbox has been enabled and all other necessary | 358 // IPC messages before the sandbox has been enabled and all other necessary |
282 // initialization has succeeded. | 359 // initialization has succeeded. |
283 gpu_channel_manager_.reset( | 360 gpu_channel_manager_.reset(new GpuChannelManager( |
284 new GpuChannelManager(channel(), watchdog_thread_.get(), | 361 this, watchdog_thread_.get(), base::ThreadTaskRunnerHandle::Get().get(), |
285 base::ThreadTaskRunnerHandle::Get().get(), | 362 ChildProcess::current()->io_task_runner(), |
286 ChildProcess::current()->io_task_runner(), | 363 ChildProcess::current()->GetShutDownEvent(), sync_point_manager_, |
287 ChildProcess::current()->GetShutDownEvent(), | 364 gpu_memory_buffer_factory_)); |
288 sync_point_manager_, gpu_memory_buffer_factory_)); | |
289 | 365 |
290 #if defined(USE_OZONE) | 366 #if defined(USE_OZONE) |
291 ui::OzonePlatform::GetInstance() | 367 ui::OzonePlatform::GetInstance() |
292 ->GetGpuPlatformSupport() | 368 ->GetGpuPlatformSupport() |
293 ->OnChannelEstablished(this); | 369 ->OnChannelEstablished(this); |
294 #endif | 370 #endif |
295 } | 371 } |
296 | 372 |
297 void GpuChildThread::OnFinalize() { | 373 void GpuChildThread::OnFinalize() { |
298 // Quit the GPU process | 374 // Quit the GPU process |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 #if defined(OS_WIN) | 421 #if defined(OS_WIN) |
346 if (!in_browser_process_) { | 422 if (!in_browser_process_) { |
347 // The unsandboxed GPU process fulfilled its duty. Rest in peace. | 423 // The unsandboxed GPU process fulfilled its duty. Rest in peace. |
348 base::MessageLoop::current()->QuitWhenIdle(); | 424 base::MessageLoop::current()->QuitWhenIdle(); |
349 } | 425 } |
350 #endif // OS_WIN | 426 #endif // OS_WIN |
351 } | 427 } |
352 | 428 |
353 void GpuChildThread::OnGetVideoMemoryUsageStats() { | 429 void GpuChildThread::OnGetVideoMemoryUsageStats() { |
354 GPUVideoMemoryUsageStats video_memory_usage_stats; | 430 GPUVideoMemoryUsageStats video_memory_usage_stats; |
355 if (gpu_channel_manager_) | 431 if (gpu_channel_manager_) { |
356 gpu_channel_manager_->gpu_memory_manager()->GetVideoMemoryUsageStats( | 432 gpu_channel_manager_->gpu_memory_manager()->GetVideoMemoryUsageStats( |
357 &video_memory_usage_stats); | 433 &video_memory_usage_stats); |
| 434 } |
358 Send(new GpuHostMsg_VideoMemoryUsageStats(video_memory_usage_stats)); | 435 Send(new GpuHostMsg_VideoMemoryUsageStats(video_memory_usage_stats)); |
359 } | 436 } |
360 | 437 |
361 void GpuChildThread::OnClean() { | 438 void GpuChildThread::OnClean() { |
362 DVLOG(1) << "GPU: Removing all contexts"; | 439 DVLOG(1) << "GPU: Removing all contexts"; |
363 if (gpu_channel_manager_) | 440 if (gpu_channel_manager_) |
364 gpu_channel_manager_->LoseAllContexts(); | 441 gpu_channel_manager_->DestroyAllChannels(); |
365 } | 442 } |
366 | 443 |
367 void GpuChildThread::OnCrash() { | 444 void GpuChildThread::OnCrash() { |
368 DVLOG(1) << "GPU: Simulating GPU crash"; | 445 DVLOG(1) << "GPU: Simulating GPU crash"; |
369 // Good bye, cruel world. | 446 // Good bye, cruel world. |
370 volatile int* it_s_the_end_of_the_world_as_we_know_it = NULL; | 447 volatile int* it_s_the_end_of_the_world_as_we_know_it = NULL; |
371 *it_s_the_end_of_the_world_as_we_know_it = 0xdead; | 448 *it_s_the_end_of_the_world_as_we_know_it = 0xdead; |
372 } | 449 } |
373 | 450 |
374 void GpuChildThread::OnHang() { | 451 void GpuChildThread::OnHang() { |
(...skipping 15 matching lines...) Expand all Loading... |
390 watchdog_thread_->Stop(); | 467 watchdog_thread_->Stop(); |
391 } | 468 } |
392 } | 469 } |
393 | 470 |
394 void GpuChildThread::OnGpuSwitched() { | 471 void GpuChildThread::OnGpuSwitched() { |
395 DVLOG(1) << "GPU: GPU has switched"; | 472 DVLOG(1) << "GPU: GPU has switched"; |
396 // Notify observers in the GPU process. | 473 // Notify observers in the GPU process. |
397 ui::GpuSwitchingManager::GetInstance()->NotifyGpuSwitched(); | 474 ui::GpuSwitchingManager::GetInstance()->NotifyGpuSwitched(); |
398 } | 475 } |
399 | 476 |
| 477 #if defined(OS_MACOSX) |
| 478 void GpuChildThread::OnBufferPresented(const BufferPresentedParams& params) { |
| 479 if (gpu_channel_manager_) |
| 480 gpu_channel_manager_->BufferPresented(params); |
| 481 } |
| 482 #endif |
| 483 |
| 484 void GpuChildThread::OnEstablishChannel(const EstablishChannelParams& params) { |
| 485 if (gpu_channel_manager_) |
| 486 gpu_channel_manager_->EstablishChannel(params); |
| 487 } |
| 488 |
| 489 void GpuChildThread::OnCloseChannel(const IPC::ChannelHandle& channel_handle) { |
| 490 if (gpu_channel_manager_) |
| 491 gpu_channel_manager_->CloseChannel(channel_handle); |
| 492 } |
| 493 |
| 494 void GpuChildThread::OnLoadedShader(const std::string& shader) { |
| 495 if (gpu_channel_manager_) |
| 496 gpu_channel_manager_->PopulateShaderCache(shader); |
| 497 } |
| 498 |
| 499 void GpuChildThread::OnDestroyGpuMemoryBuffer( |
| 500 gfx::GpuMemoryBufferId id, |
| 501 int client_id, |
| 502 const gpu::SyncToken& sync_token) { |
| 503 if (gpu_channel_manager_) |
| 504 gpu_channel_manager_->DestroyGpuMemoryBuffer(id, client_id, sync_token); |
| 505 } |
| 506 |
| 507 void GpuChildThread::OnUpdateValueState(int client_id, |
| 508 unsigned int target, |
| 509 const gpu::ValueState& state) { |
| 510 if (gpu_channel_manager_) |
| 511 gpu_channel_manager_->UpdateValueState(client_id, target, state); |
| 512 } |
| 513 |
| 514 #if defined(OS_ANDROID) |
| 515 void GpuChildThread::OnWakeUpGpu() { |
| 516 if (gpu_channel_manager_) |
| 517 gpu_channel_manager_->WakeUpGpu(); |
| 518 } |
| 519 #endif |
| 520 |
| 521 void GpuChildThread::OnLoseAllContexts() { |
| 522 if (gpu_channel_manager_) |
| 523 gpu_channel_manager_->DestroyAllChannels(); |
| 524 } |
| 525 |
400 void GpuChildThread::BindProcessControlRequest( | 526 void GpuChildThread::BindProcessControlRequest( |
401 mojo::InterfaceRequest<ProcessControl> request) { | 527 mojo::InterfaceRequest<ProcessControl> request) { |
402 DVLOG(1) << "GPU: Binding ProcessControl request"; | 528 DVLOG(1) << "GPU: Binding ProcessControl request"; |
403 DCHECK(process_control_); | 529 DCHECK(process_control_); |
404 process_control_bindings_.AddBinding(process_control_.get(), | 530 process_control_bindings_.AddBinding(process_control_.get(), |
405 std::move(request)); | 531 std::move(request)); |
406 } | 532 } |
407 | 533 |
408 } // namespace content | 534 } // namespace content |
OLD | NEW |