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