sadrul | 5354659 | 2016-12-17 01:44:21 | [diff] [blame] | 1 | // Copyright 2016 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. |
| 4 | |
| 5 | #include "content/browser/gpu/gpu_client.h" |
| 6 | |
| 7 | #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h" |
| 8 | #include "content/browser/gpu/gpu_process_host.h" |
| 9 | #include "content/common/child_process_host_impl.h" |
| 10 | #include "content/public/browser/browser_thread.h" |
| 11 | #include "gpu/ipc/client/gpu_channel_host.h" |
| 12 | #include "gpu/ipc/client/gpu_memory_buffer_impl.h" |
| 13 | #include "gpu/ipc/client/gpu_memory_buffer_impl_shared_memory.h" |
| 14 | |
| 15 | namespace content { |
| 16 | |
| 17 | GpuClient::GpuClient(int render_process_id) |
| 18 | : render_process_id_(render_process_id), weak_factory_(this) { |
| 19 | bindings_.set_connection_error_handler( |
| 20 | base::Bind(&GpuClient::OnError, base::Unretained(this))); |
| 21 | } |
| 22 | |
| 23 | GpuClient::~GpuClient() { |
| 24 | bindings_.CloseAllBindings(); |
| 25 | OnError(); |
| 26 | } |
| 27 | |
| 28 | void GpuClient::Add(ui::mojom::GpuRequest request) { |
| 29 | bindings_.AddBinding(this, std::move(request)); |
| 30 | } |
| 31 | |
| 32 | void GpuClient::OnError() { |
| 33 | if (!bindings_.empty()) |
| 34 | return; |
| 35 | BrowserGpuMemoryBufferManager* gpu_memory_buffer_manager = |
| 36 | BrowserGpuMemoryBufferManager::current(); |
| 37 | if (gpu_memory_buffer_manager) |
| 38 | gpu_memory_buffer_manager->ProcessRemoved(render_process_id_); |
| 39 | } |
| 40 | |
| 41 | void GpuClient::OnEstablishGpuChannel( |
| 42 | const EstablishGpuChannelCallback& callback, |
| 43 | const IPC::ChannelHandle& channel, |
sadrul | 98c8321 | 2017-04-08 00:50:26 | [diff] [blame] | 44 | const gpu::GPUInfo& gpu_info, |
| 45 | GpuProcessHost::EstablishChannelStatus status) { |
sadrul | 5354659 | 2016-12-17 01:44:21 | [diff] [blame] | 46 | mojo::ScopedMessagePipeHandle channel_handle; |
| 47 | channel_handle.reset(channel.mojo_handle); |
| 48 | callback.Run(render_process_id_, std::move(channel_handle), gpu_info); |
| 49 | } |
| 50 | |
| 51 | void GpuClient::OnCreateGpuMemoryBuffer( |
| 52 | const CreateGpuMemoryBufferCallback& callback, |
| 53 | const gfx::GpuMemoryBufferHandle& handle) { |
| 54 | callback.Run(handle); |
| 55 | } |
| 56 | |
| 57 | void GpuClient::EstablishGpuChannel( |
| 58 | const EstablishGpuChannelCallback& callback) { |
sadrul | ca65d070 | 2017-04-10 18:08:13 | [diff] [blame] | 59 | GpuProcessHost* host = GpuProcessHost::Get(); |
sadrul | 5354659 | 2016-12-17 01:44:21 | [diff] [blame] | 60 | if (!host) { |
sadrul | 98c8321 | 2017-04-08 00:50:26 | [diff] [blame] | 61 | OnEstablishGpuChannel( |
| 62 | callback, IPC::ChannelHandle(), gpu::GPUInfo(), |
| 63 | GpuProcessHost::EstablishChannelStatus::GPU_ACCESS_DENIED); |
sadrul | 5354659 | 2016-12-17 01:44:21 | [diff] [blame] | 64 | return; |
| 65 | } |
| 66 | |
| 67 | bool preempts = false; |
| 68 | bool allow_view_command_buffers = false; |
| 69 | bool allow_real_time_streams = false; |
| 70 | host->EstablishGpuChannel( |
| 71 | render_process_id_, |
| 72 | ChildProcessHostImpl::ChildProcessUniqueIdToTracingProcessId( |
| 73 | render_process_id_), |
| 74 | preempts, allow_view_command_buffers, allow_real_time_streams, |
| 75 | base::Bind(&GpuClient::OnEstablishGpuChannel, weak_factory_.GetWeakPtr(), |
| 76 | callback)); |
| 77 | } |
| 78 | |
| 79 | void GpuClient::CreateGpuMemoryBuffer( |
| 80 | gfx::GpuMemoryBufferId id, |
| 81 | const gfx::Size& size, |
| 82 | gfx::BufferFormat format, |
| 83 | gfx::BufferUsage usage, |
| 84 | const ui::mojom::Gpu::CreateGpuMemoryBufferCallback& callback) { |
| 85 | DCHECK(BrowserGpuMemoryBufferManager::current()); |
| 86 | |
| 87 | base::CheckedNumeric<int> bytes = size.width(); |
| 88 | bytes *= size.height(); |
| 89 | if (!bytes.IsValid()) { |
| 90 | OnCreateGpuMemoryBuffer(callback, gfx::GpuMemoryBufferHandle()); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | BrowserGpuMemoryBufferManager::current() |
| 95 | ->AllocateGpuMemoryBufferForChildProcess( |
| 96 | id, size, format, usage, render_process_id_, |
| 97 | base::Bind(&GpuClient::OnCreateGpuMemoryBuffer, |
| 98 | weak_factory_.GetWeakPtr(), callback)); |
| 99 | } |
| 100 | |
| 101 | void GpuClient::DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id, |
| 102 | const gpu::SyncToken& sync_token) { |
| 103 | DCHECK(BrowserGpuMemoryBufferManager::current()); |
| 104 | |
| 105 | BrowserGpuMemoryBufferManager::current()->ChildProcessDeletedGpuMemoryBuffer( |
| 106 | id, render_process_id_, sync_token); |
| 107 | } |
| 108 | |
| 109 | } // namespace content |