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/browser/gpu/browser_gpu_channel_host_factory.h"
8 #include "base/debug/trace_event.h"
9 #include "base/threading/thread_restrictions.h"
10 #include "content/browser/gpu/gpu_data_manager_impl.h"
11 #include "content/browser/gpu/gpu_process_host.h"
12 #include "content/browser/gpu/gpu_surface_tracker.h"
13 #include "content/common/child_process_host_impl.h"
14 #include "content/common/gpu/client/gpu_memory_buffer_impl.h"
15 #include "content/common/gpu/gpu_messages.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/gpu_data_manager.h"
18 #include "content/public/common/content_client.h"
19 #include "ipc/ipc_forwarding_message_filter.h"
20 #include "ipc/message_filter.h"
24 BrowserGpuChannelHostFactory* BrowserGpuChannelHostFactory::instance_ = NULL;
26 BrowserGpuChannelHostFactory::CreateRequest::CreateRequest()
29 route_id(MSG_ROUTING_NONE) {
32 BrowserGpuChannelHostFactory::CreateRequest::~CreateRequest() {
35 BrowserGpuChannelHostFactory::EstablishRequest::EstablishRequest(
36 CauseForGpuLaunch cause,
39 : event_(false, false),
40 cause_for_gpu_launch_(cause),
41 gpu_client_id_(gpu_client_id),
42 gpu_host_id_(gpu_host_id),
43 reused_gpu_process_(false),
45 main_loop_(base::MessageLoopProxy::current()) {
46 scoped_refptr<base::MessageLoopProxy> loop =
47 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
50 base::Bind(&BrowserGpuChannelHostFactory::EstablishRequest::EstablishOnIO,
54 BrowserGpuChannelHostFactory::EstablishRequest::~EstablishRequest() {
57 void BrowserGpuChannelHostFactory::EstablishRequest::EstablishOnIO() {
58 GpuProcessHost* host = GpuProcessHost::FromID(gpu_host_id_);
60 host = GpuProcessHost::Get(GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED,
61 cause_for_gpu_launch_);
63 LOG(ERROR) << "Failed to launch GPU process.";
67 gpu_host_id_ = host->host_id();
68 reused_gpu_process_ = false;
70 if (reused_gpu_process_) {
71 // We come here if we retried to establish the channel because of a
72 // failure in ChannelEstablishedOnIO, but we ended up with the same
73 // process ID, meaning the failure was not because of a channel error,
74 // but another reason. So fail now.
75 LOG(ERROR) << "Failed to create channel.";
79 reused_gpu_process_ = true;
82 host->EstablishGpuChannel(
86 &BrowserGpuChannelHostFactory::EstablishRequest::OnEstablishedOnIO,
90 void BrowserGpuChannelHostFactory::EstablishRequest::OnEstablishedOnIO(
91 const IPC::ChannelHandle& channel_handle,
92 const gpu::GPUInfo& gpu_info) {
93 if (channel_handle.name.empty() && reused_gpu_process_) {
94 // We failed after re-using the GPU process, but it may have died in the
95 // mean time. Retry to have a chance to create a fresh GPU process.
96 DVLOG(1) << "Failed to create channel on existing GPU process. Trying to "
97 "restart GPU process.";
100 channel_handle_ = channel_handle;
101 gpu_info_ = gpu_info;
106 void BrowserGpuChannelHostFactory::EstablishRequest::FinishOnIO() {
108 main_loop_->PostTask(
110 base::Bind(&BrowserGpuChannelHostFactory::EstablishRequest::FinishOnMain,
114 void BrowserGpuChannelHostFactory::EstablishRequest::FinishOnMain() {
116 BrowserGpuChannelHostFactory* factory =
117 BrowserGpuChannelHostFactory::instance();
118 factory->GpuChannelEstablished();
123 void BrowserGpuChannelHostFactory::EstablishRequest::Wait() {
124 DCHECK(main_loop_->BelongsToCurrentThread());
126 // We're blocking the UI thread, which is generally undesirable.
127 // In this case we need to wait for this before we can show any UI
128 // /anyway/, so it won't cause additional jank.
129 // TODO(piman): Make this asynchronous (http://crbug.com/125248).
130 TRACE_EVENT0("browser",
131 "BrowserGpuChannelHostFactory::EstablishGpuChannelSync");
132 base::ThreadRestrictions::ScopedAllowWait allow_wait;
138 void BrowserGpuChannelHostFactory::EstablishRequest::Cancel() {
139 DCHECK(main_loop_->BelongsToCurrentThread());
143 bool BrowserGpuChannelHostFactory::CanUseForTesting() {
144 return GpuDataManager::GetInstance()->GpuAccessAllowed(NULL);
147 void BrowserGpuChannelHostFactory::Initialize(bool establish_gpu_channel) {
149 instance_ = new BrowserGpuChannelHostFactory(establish_gpu_channel);
152 void BrowserGpuChannelHostFactory::Terminate() {
158 BrowserGpuChannelHostFactory::BrowserGpuChannelHostFactory(
159 bool establish_gpu_channel)
160 : gpu_client_id_(ChildProcessHostImpl::GenerateChildProcessUniqueId()),
161 shutdown_event_(new base::WaitableEvent(true, false)),
163 if (establish_gpu_channel) {
164 pending_request_ = new EstablishRequest(
165 CAUSE_FOR_GPU_LAUNCH_BROWSER_STARTUP, gpu_client_id_, gpu_host_id_);
169 BrowserGpuChannelHostFactory::~BrowserGpuChannelHostFactory() {
170 DCHECK(IsMainThread());
171 if (pending_request_)
172 pending_request_->Cancel();
173 for (size_t n = 0; n < established_callbacks_.size(); n++)
174 established_callbacks_[n].Run();
175 shutdown_event_->Signal();
178 bool BrowserGpuChannelHostFactory::IsMainThread() {
179 return BrowserThread::CurrentlyOn(BrowserThread::UI);
182 base::MessageLoop* BrowserGpuChannelHostFactory::GetMainLoop() {
183 return BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::UI);
186 scoped_refptr<base::MessageLoopProxy>
187 BrowserGpuChannelHostFactory::GetIOLoopProxy() {
188 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
191 scoped_ptr<base::SharedMemory>
192 BrowserGpuChannelHostFactory::AllocateSharedMemory(size_t size) {
193 scoped_ptr<base::SharedMemory> shm(new base::SharedMemory());
194 if (!shm->CreateAnonymous(size))
195 return scoped_ptr<base::SharedMemory>();
199 void BrowserGpuChannelHostFactory::CreateViewCommandBufferOnIO(
200 CreateRequest* request,
202 const GPUCreateCommandBufferConfig& init_params) {
203 GpuProcessHost* host = GpuProcessHost::FromID(gpu_host_id_);
205 request->event.Signal();
209 gfx::GLSurfaceHandle surface =
210 GpuSurfaceTracker::Get()->GetSurfaceHandle(surface_id);
212 host->CreateViewCommandBuffer(
218 base::Bind(&BrowserGpuChannelHostFactory::CommandBufferCreatedOnIO,
223 void BrowserGpuChannelHostFactory::CommandBufferCreatedOnIO(
224 CreateRequest* request, bool succeeded) {
225 request->succeeded = succeeded;
226 request->event.Signal();
229 bool BrowserGpuChannelHostFactory::CreateViewCommandBuffer(
231 const GPUCreateCommandBufferConfig& init_params,
233 CreateRequest request;
234 request.route_id = route_id;
235 GetIOLoopProxy()->PostTask(FROM_HERE, base::Bind(
236 &BrowserGpuChannelHostFactory::CreateViewCommandBufferOnIO,
237 base::Unretained(this),
241 // We're blocking the UI thread, which is generally undesirable.
242 // In this case we need to wait for this before we can show any UI /anyway/,
243 // so it won't cause additional jank.
244 // TODO(piman): Make this asynchronous (http://crbug.com/125248).
245 TRACE_EVENT0("browser",
246 "BrowserGpuChannelHostFactory::CreateViewCommandBuffer");
247 base::ThreadRestrictions::ScopedAllowWait allow_wait;
248 request.event.Wait();
249 return request.succeeded;
252 void BrowserGpuChannelHostFactory::CreateImageOnIO(
253 gfx::PluginWindowHandle window,
255 const CreateImageCallback& callback) {
256 GpuProcessHost* host = GpuProcessHost::FromID(gpu_host_id_);
258 ImageCreatedOnIO(callback, gfx::Size());
266 base::Bind(&BrowserGpuChannelHostFactory::ImageCreatedOnIO, callback));
270 void BrowserGpuChannelHostFactory::ImageCreatedOnIO(
271 const CreateImageCallback& callback, const gfx::Size size) {
272 BrowserThread::PostTask(
275 base::Bind(&BrowserGpuChannelHostFactory::OnImageCreated,
280 void BrowserGpuChannelHostFactory::OnImageCreated(
281 const CreateImageCallback& callback, const gfx::Size size) {
285 void BrowserGpuChannelHostFactory::CreateImage(
286 gfx::PluginWindowHandle window,
288 const CreateImageCallback& callback) {
289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
290 GetIOLoopProxy()->PostTask(FROM_HERE, base::Bind(
291 &BrowserGpuChannelHostFactory::CreateImageOnIO,
292 base::Unretained(this),
298 void BrowserGpuChannelHostFactory::DeleteImageOnIO(
299 int32 image_id, int32 sync_point) {
300 GpuProcessHost* host = GpuProcessHost::FromID(gpu_host_id_);
305 host->DeleteImage(gpu_client_id_, image_id, sync_point);
308 void BrowserGpuChannelHostFactory::DeleteImage(
309 int32 image_id, int32 sync_point) {
310 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
311 GetIOLoopProxy()->PostTask(FROM_HERE, base::Bind(
312 &BrowserGpuChannelHostFactory::DeleteImageOnIO,
313 base::Unretained(this),
318 GpuChannelHost* BrowserGpuChannelHostFactory::EstablishGpuChannelSync(
319 CauseForGpuLaunch cause_for_gpu_launch) {
320 EstablishGpuChannel(cause_for_gpu_launch, base::Closure());
322 if (pending_request_)
323 pending_request_->Wait();
325 return gpu_channel_.get();
328 void BrowserGpuChannelHostFactory::EstablishGpuChannel(
329 CauseForGpuLaunch cause_for_gpu_launch,
330 const base::Closure& callback) {
331 if (gpu_channel_.get() && gpu_channel_->IsLost()) {
332 DCHECK(!pending_request_);
333 // Recreate the channel if it has been lost.
337 if (!gpu_channel_ && !pending_request_) {
338 // We should only get here if the context was lost.
339 pending_request_ = new EstablishRequest(
340 cause_for_gpu_launch, gpu_client_id_, gpu_host_id_);
343 if (!callback.is_null()) {
347 established_callbacks_.push_back(callback);
351 GpuChannelHost* BrowserGpuChannelHostFactory::GetGpuChannel() {
352 if (gpu_channel_ && !gpu_channel_->IsLost())
358 void BrowserGpuChannelHostFactory::GpuChannelEstablished() {
359 DCHECK(IsMainThread());
360 DCHECK(pending_request_);
361 if (pending_request_->channel_handle().name.empty())
364 GetContentClient()->SetGpuInfo(pending_request_->gpu_info());
365 gpu_channel_ = GpuChannelHost::Create(this,
366 pending_request_->gpu_info(),
367 pending_request_->channel_handle(),
368 shutdown_event_.get());
369 gpu_host_id_ = pending_request_->gpu_host_id();
370 pending_request_ = NULL;
372 for (size_t n = 0; n < established_callbacks_.size(); n++)
373 established_callbacks_[n].Run();
375 established_callbacks_.clear();
378 scoped_ptr<gfx::GpuMemoryBuffer>
379 BrowserGpuChannelHostFactory::AllocateGpuMemoryBuffer(size_t width,
381 unsigned internalformat,
383 if (!GpuMemoryBufferImpl::IsFormatValid(internalformat) ||
384 !GpuMemoryBufferImpl::IsUsageValid(usage))
385 return scoped_ptr<gfx::GpuMemoryBuffer>();
387 return GpuMemoryBufferImpl::Create(gfx::Size(width, height),
389 usage).PassAs<gfx::GpuMemoryBuffer>();
393 void BrowserGpuChannelHostFactory::AddFilterOnIO(
395 scoped_refptr<IPC::MessageFilter> filter) {
396 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
398 GpuProcessHost* host = GpuProcessHost::FromID(host_id);
400 host->AddFilter(filter.get());
403 void BrowserGpuChannelHostFactory::SetHandlerForControlMessages(
404 const uint32* message_ids,
406 const base::Callback<void(const IPC::Message&)>& handler,
407 base::TaskRunner* target_task_runner) {
410 << " BrowserGpuChannelHostFactory::SetHandlerForControlMessages()"
411 << " until the GpuProcessHost has been set up.";
413 scoped_refptr<IPC::ForwardingMessageFilter> filter =
414 new IPC::ForwardingMessageFilter(message_ids,
417 filter->AddRoute(MSG_ROUTING_CONTROL, handler);
419 GetIOLoopProxy()->PostTask(
421 base::Bind(&BrowserGpuChannelHostFactory::AddFilterOnIO,
426 } // namespace content