| 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/browser/gpu/browser_gpu_channel_host_factory.h" | 5 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/profiler/scoped_tracker.h" | 10 #include "base/profiler/scoped_tracker.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "content/browser/gpu/shader_disk_cache.h" | 21 #include "content/browser/gpu/shader_disk_cache.h" |
| 22 #include "content/common/child_process_host_impl.h" | 22 #include "content/common/child_process_host_impl.h" |
| 23 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/content_browser_client.h" | 24 #include "content/public/browser/content_browser_client.h" |
| 25 #include "content/public/browser/gpu_data_manager.h" | 25 #include "content/public/browser/gpu_data_manager.h" |
| 26 #include "content/public/common/content_client.h" | 26 #include "content/public/common/content_client.h" |
| 27 #include "gpu/command_buffer/service/gpu_switches.h" | 27 #include "gpu/command_buffer/service/gpu_switches.h" |
| 28 #include "gpu/ipc/common/gpu_messages.h" | 28 #include "gpu/ipc/common/gpu_messages.h" |
| 29 #include "ipc/ipc_channel_handle.h" | 29 #include "ipc/ipc_channel_handle.h" |
| 30 #include "ipc/message_filter.h" | 30 #include "ipc/message_filter.h" |
| 31 | |
| 32 #if defined(MOJO_RUNNER_CLIENT) | |
| 33 #include "services/shell/runner/common/client_util.h" | 31 #include "services/shell/runner/common/client_util.h" |
| 34 #endif | |
| 35 | 32 |
| 36 namespace content { | 33 namespace content { |
| 37 | 34 |
| 38 BrowserGpuChannelHostFactory* BrowserGpuChannelHostFactory::instance_ = NULL; | 35 BrowserGpuChannelHostFactory* BrowserGpuChannelHostFactory::instance_ = NULL; |
| 39 | 36 |
| 40 class BrowserGpuChannelHostFactory::EstablishRequest | 37 class BrowserGpuChannelHostFactory::EstablishRequest |
| 41 : public base::RefCountedThreadSafe<EstablishRequest> { | 38 : public base::RefCountedThreadSafe<EstablishRequest> { |
| 42 public: | 39 public: |
| 43 static scoped_refptr<EstablishRequest> Create(int gpu_client_id, | 40 static scoped_refptr<EstablishRequest> Create(int gpu_client_id, |
| 44 uint64_t gpu_client_tracing_id, | 41 uint64_t gpu_client_tracing_id, |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 } | 199 } |
| 203 | 200 |
| 204 bool BrowserGpuChannelHostFactory::CanUseForTesting() { | 201 bool BrowserGpuChannelHostFactory::CanUseForTesting() { |
| 205 return GpuDataManager::GetInstance()->GpuAccessAllowed(NULL); | 202 return GpuDataManager::GetInstance()->GpuAccessAllowed(NULL); |
| 206 } | 203 } |
| 207 | 204 |
| 208 void BrowserGpuChannelHostFactory::Initialize(bool establish_gpu_channel) { | 205 void BrowserGpuChannelHostFactory::Initialize(bool establish_gpu_channel) { |
| 209 DCHECK(!instance_); | 206 DCHECK(!instance_); |
| 210 instance_ = new BrowserGpuChannelHostFactory(); | 207 instance_ = new BrowserGpuChannelHostFactory(); |
| 211 if (establish_gpu_channel) { | 208 if (establish_gpu_channel) { |
| 212 instance_->EstablishGpuChannel(base::Closure()); | 209 instance_->EstablishGpuChannel(gpu::GpuChannelEstablishedCallback()); |
| 213 } | 210 } |
| 214 } | 211 } |
| 215 | 212 |
| 216 void BrowserGpuChannelHostFactory::Terminate() { | 213 void BrowserGpuChannelHostFactory::Terminate() { |
| 217 DCHECK(instance_); | 214 DCHECK(instance_); |
| 218 delete instance_; | 215 delete instance_; |
| 219 instance_ = NULL; | 216 instance_ = NULL; |
| 220 } | 217 } |
| 221 | 218 |
| 222 BrowserGpuChannelHostFactory::BrowserGpuChannelHostFactory() | 219 BrowserGpuChannelHostFactory::BrowserGpuChannelHostFactory() |
| (...skipping 19 matching lines...) Expand all Loading... |
| 242 gpu_client_id_, cache_dir)); | 239 gpu_client_id_, cache_dir)); |
| 243 } | 240 } |
| 244 } | 241 } |
| 245 } | 242 } |
| 246 | 243 |
| 247 BrowserGpuChannelHostFactory::~BrowserGpuChannelHostFactory() { | 244 BrowserGpuChannelHostFactory::~BrowserGpuChannelHostFactory() { |
| 248 DCHECK(IsMainThread()); | 245 DCHECK(IsMainThread()); |
| 249 if (pending_request_.get()) | 246 if (pending_request_.get()) |
| 250 pending_request_->Cancel(); | 247 pending_request_->Cancel(); |
| 251 for (size_t n = 0; n < established_callbacks_.size(); n++) | 248 for (size_t n = 0; n < established_callbacks_.size(); n++) |
| 252 established_callbacks_[n].Run(); | 249 established_callbacks_[n].Run(nullptr); |
| 253 shutdown_event_->Signal(); | 250 shutdown_event_->Signal(); |
| 254 if (gpu_channel_) { | 251 if (gpu_channel_) { |
| 255 gpu_channel_->DestroyChannel(); | 252 gpu_channel_->DestroyChannel(); |
| 256 gpu_channel_ = NULL; | 253 gpu_channel_ = NULL; |
| 257 } | 254 } |
| 258 } | 255 } |
| 259 | 256 |
| 260 bool BrowserGpuChannelHostFactory::IsMainThread() { | 257 bool BrowserGpuChannelHostFactory::IsMainThread() { |
| 261 return BrowserThread::CurrentlyOn(BrowserThread::UI); | 258 return BrowserThread::CurrentlyOn(BrowserThread::UI); |
| 262 } | 259 } |
| 263 | 260 |
| 264 scoped_refptr<base::SingleThreadTaskRunner> | 261 scoped_refptr<base::SingleThreadTaskRunner> |
| 265 BrowserGpuChannelHostFactory::GetIOThreadTaskRunner() { | 262 BrowserGpuChannelHostFactory::GetIOThreadTaskRunner() { |
| 266 return BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); | 263 return BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
| 267 } | 264 } |
| 268 | 265 |
| 269 std::unique_ptr<base::SharedMemory> | 266 std::unique_ptr<base::SharedMemory> |
| 270 BrowserGpuChannelHostFactory::AllocateSharedMemory(size_t size) { | 267 BrowserGpuChannelHostFactory::AllocateSharedMemory(size_t size) { |
| 271 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | 268 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); |
| 272 if (!shm->CreateAnonymous(size)) | 269 if (!shm->CreateAnonymous(size)) |
| 273 return std::unique_ptr<base::SharedMemory>(); | 270 return std::unique_ptr<base::SharedMemory>(); |
| 274 return shm; | 271 return shm; |
| 275 } | 272 } |
| 276 | 273 |
| 277 // Blocking the UI thread to open a GPU channel is not supported on Android. | |
| 278 // (Opening the initial channel to a child process involves handling a reply | |
| 279 // task on the UI thread first, so we cannot block here.) | |
| 280 #if !defined(OS_ANDROID) | |
| 281 scoped_refptr<gpu::GpuChannelHost> | |
| 282 BrowserGpuChannelHostFactory::EstablishGpuChannelSync() { | |
| 283 EstablishGpuChannel(base::Closure()); | |
| 284 | |
| 285 if (pending_request_.get()) | |
| 286 pending_request_->Wait(); | |
| 287 | |
| 288 return gpu_channel_; | |
| 289 } | |
| 290 #endif | |
| 291 | |
| 292 void BrowserGpuChannelHostFactory::EstablishGpuChannel( | 274 void BrowserGpuChannelHostFactory::EstablishGpuChannel( |
| 293 const base::Closure& callback) { | 275 const gpu::GpuChannelEstablishedCallback& callback) { |
| 294 #if defined(MOJO_RUNNER_CLIENT) | |
| 295 DCHECK(!shell::ShellIsRemote()); | 276 DCHECK(!shell::ShellIsRemote()); |
| 296 #endif | |
| 297 if (gpu_channel_.get() && gpu_channel_->IsLost()) { | 277 if (gpu_channel_.get() && gpu_channel_->IsLost()) { |
| 298 DCHECK(!pending_request_.get()); | 278 DCHECK(!pending_request_.get()); |
| 299 // Recreate the channel if it has been lost. | 279 // Recreate the channel if it has been lost. |
| 300 gpu_channel_->DestroyChannel(); | 280 gpu_channel_->DestroyChannel(); |
| 301 gpu_channel_ = NULL; | 281 gpu_channel_ = NULL; |
| 302 } | 282 } |
| 303 | 283 |
| 304 if (!gpu_channel_.get() && !pending_request_.get()) { | 284 if (!gpu_channel_.get() && !pending_request_.get()) { |
| 305 // We should only get here if the context was lost. | 285 // We should only get here if the context was lost. |
| 306 pending_request_ = EstablishRequest::Create( | 286 pending_request_ = EstablishRequest::Create( |
| 307 gpu_client_id_, gpu_client_tracing_id_, gpu_host_id_); | 287 gpu_client_id_, gpu_client_tracing_id_, gpu_host_id_); |
| 308 } | 288 } |
| 309 | 289 |
| 310 if (!callback.is_null()) { | 290 if (!callback.is_null()) { |
| 311 if (gpu_channel_.get()) | 291 if (gpu_channel_.get()) |
| 312 callback.Run(); | 292 callback.Run(gpu_channel_); |
| 313 else | 293 else |
| 314 established_callbacks_.push_back(callback); | 294 established_callbacks_.push_back(callback); |
| 315 } | 295 } |
| 316 } | 296 } |
| 317 | 297 |
| 298 // Blocking the UI thread to open a GPU channel is not supported on Android. |
| 299 // (Opening the initial channel to a child process involves handling a reply |
| 300 // task on the UI thread first, so we cannot block here.) |
| 301 scoped_refptr<gpu::GpuChannelHost> |
| 302 BrowserGpuChannelHostFactory::EstablishGpuChannelSync() { |
| 303 #if defined(OS_ANDROID) |
| 304 NOTREACHED(); |
| 305 return nullptr; |
| 306 #endif |
| 307 EstablishGpuChannel(gpu::GpuChannelEstablishedCallback()); |
| 308 |
| 309 if (pending_request_.get()) |
| 310 pending_request_->Wait(); |
| 311 |
| 312 return gpu_channel_; |
| 313 } |
| 314 |
| 318 gpu::GpuChannelHost* BrowserGpuChannelHostFactory::GetGpuChannel() { | 315 gpu::GpuChannelHost* BrowserGpuChannelHostFactory::GetGpuChannel() { |
| 319 if (gpu_channel_.get() && !gpu_channel_->IsLost()) | 316 if (gpu_channel_.get() && !gpu_channel_->IsLost()) |
| 320 return gpu_channel_.get(); | 317 return gpu_channel_.get(); |
| 321 | 318 |
| 322 return NULL; | 319 return NULL; |
| 323 } | 320 } |
| 324 | 321 |
| 325 void BrowserGpuChannelHostFactory::GpuChannelEstablished() { | 322 void BrowserGpuChannelHostFactory::GpuChannelEstablished() { |
| 326 DCHECK(IsMainThread()); | 323 DCHECK(IsMainThread()); |
| 327 DCHECK(pending_request_.get()); | 324 DCHECK(pending_request_.get()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 342 gpu_host_id_ = pending_request_->gpu_host_id(); | 339 gpu_host_id_ = pending_request_->gpu_host_id(); |
| 343 pending_request_ = NULL; | 340 pending_request_ = NULL; |
| 344 | 341 |
| 345 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/466866 is | 342 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/466866 is |
| 346 // fixed. | 343 // fixed. |
| 347 tracked_objects::ScopedTracker tracking_profile2( | 344 tracked_objects::ScopedTracker tracking_profile2( |
| 348 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 345 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 349 "466866 BrowserGpuChannelHostFactory::GpuChannelEstablished2")); | 346 "466866 BrowserGpuChannelHostFactory::GpuChannelEstablished2")); |
| 350 | 347 |
| 351 for (size_t n = 0; n < established_callbacks_.size(); n++) | 348 for (size_t n = 0; n < established_callbacks_.size(); n++) |
| 352 established_callbacks_[n].Run(); | 349 established_callbacks_[n].Run(gpu_channel_); |
| 353 | 350 |
| 354 established_callbacks_.clear(); | 351 established_callbacks_.clear(); |
| 355 } | 352 } |
| 356 | 353 |
| 357 // static | 354 // static |
| 358 void BrowserGpuChannelHostFactory::AddFilterOnIO( | 355 void BrowserGpuChannelHostFactory::AddFilterOnIO( |
| 359 int host_id, | 356 int host_id, |
| 360 scoped_refptr<IPC::MessageFilter> filter) { | 357 scoped_refptr<IPC::MessageFilter> filter) { |
| 361 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 358 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 362 | 359 |
| 363 GpuProcessHost* host = GpuProcessHost::FromID(host_id); | 360 GpuProcessHost* host = GpuProcessHost::FromID(host_id); |
| 364 if (host) | 361 if (host) |
| 365 host->AddFilter(filter.get()); | 362 host->AddFilter(filter.get()); |
| 366 } | 363 } |
| 367 | 364 |
| 368 // static | 365 // static |
| 369 void BrowserGpuChannelHostFactory::InitializeShaderDiskCacheOnIO( | 366 void BrowserGpuChannelHostFactory::InitializeShaderDiskCacheOnIO( |
| 370 int gpu_client_id, | 367 int gpu_client_id, |
| 371 const base::FilePath& cache_dir) { | 368 const base::FilePath& cache_dir) { |
| 372 ShaderCacheFactory::GetInstance()->SetCacheInfo(gpu_client_id, cache_dir); | 369 ShaderCacheFactory::GetInstance()->SetCacheInfo(gpu_client_id, cache_dir); |
| 373 } | 370 } |
| 374 | 371 |
| 375 } // namespace content | 372 } // namespace content |
| OLD | NEW |