blob: 38079ec9993eb6eb20efec0a45357d2c5aafe93c [file] [log] [blame]
sadrul53546592016-12-17 01:44:211// 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
15namespace content {
16
17GpuClient::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
23GpuClient::~GpuClient() {
24 bindings_.CloseAllBindings();
25 OnError();
26}
27
28void GpuClient::Add(ui::mojom::GpuRequest request) {
29 bindings_.AddBinding(this, std::move(request));
30}
31
32void 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
41void GpuClient::OnEstablishGpuChannel(
42 const EstablishGpuChannelCallback& callback,
43 const IPC::ChannelHandle& channel,
sadrul98c83212017-04-08 00:50:2644 const gpu::GPUInfo& gpu_info,
45 GpuProcessHost::EstablishChannelStatus status) {
sadrul53546592016-12-17 01:44:2146 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
51void GpuClient::OnCreateGpuMemoryBuffer(
52 const CreateGpuMemoryBufferCallback& callback,
53 const gfx::GpuMemoryBufferHandle& handle) {
54 callback.Run(handle);
55}
56
57void GpuClient::EstablishGpuChannel(
58 const EstablishGpuChannelCallback& callback) {
sadrulca65d0702017-04-10 18:08:1359 GpuProcessHost* host = GpuProcessHost::Get();
sadrul53546592016-12-17 01:44:2160 if (!host) {
sadrul98c83212017-04-08 00:50:2661 OnEstablishGpuChannel(
62 callback, IPC::ChannelHandle(), gpu::GPUInfo(),
63 GpuProcessHost::EstablishChannelStatus::GPU_ACCESS_DENIED);
sadrul53546592016-12-17 01:44:2164 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
79void 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
101void 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