1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
gpu / ipc / client / client_shared_image_interface.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/ipc/client/client_shared_image_interface.h"
#include "build/build_config.h"
#include "components/viz/common/resources/shared_image_format_utils.h"
#include "gpu/command_buffer/client/client_shared_image.h"
#include "gpu/command_buffer/common/shared_image_capabilities.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/ipc/client/gpu_channel_host.h"
#include "gpu/ipc/client/shared_image_interface_proxy.h"
#include "ui/gfx/gpu_fence.h"
#include "ui/gfx/gpu_memory_buffer.h"
#if BUILDFLAG(IS_WIN)
#include "ui/gfx/win/d3d_shared_fence.h"
#endif
namespace gpu {
ClientSharedImageInterface::ClientSharedImageInterface(
SharedImageInterfaceProxy* proxy,
scoped_refptr<gpu::GpuChannelHost> channel)
: gpu_channel_(std::move(channel)),
proxy_(proxy),
shared_memory_pool_(
#if BUILDFLAG(IS_WIN)
base::MakeRefCounted<base::UnsafeSharedMemoryPool>()
#else
nullptr
#endif
) {
}
ClientSharedImageInterface::~ClientSharedImageInterface() {
gpu::SyncToken sync_token;
auto mailboxes_to_delete = mailboxes_;
for (const auto& mailbox : mailboxes_to_delete)
DestroySharedImage(sync_token, mailbox);
}
void ClientSharedImageInterface::UpdateSharedImage(const SyncToken& sync_token,
const Mailbox& mailbox) {
proxy_->UpdateSharedImage(sync_token, mailbox);
}
void ClientSharedImageInterface::UpdateSharedImage(
const SyncToken& sync_token,
std::unique_ptr<gfx::GpuFence> acquire_fence,
const Mailbox& mailbox) {
proxy_->UpdateSharedImage(sync_token, std::move(acquire_fence), mailbox);
}
void ClientSharedImageInterface::PresentSwapChain(const SyncToken& sync_token,
const Mailbox& mailbox) {
proxy_->PresentSwapChain(sync_token, mailbox);
}
#if BUILDFLAG(IS_FUCHSIA)
void ClientSharedImageInterface::RegisterSysmemBufferCollection(
zx::eventpair service_handle,
zx::channel sysmem_token,
const viz::SharedImageFormat& format,
gfx::BufferUsage usage,
bool register_with_image_pipe) {
proxy_->RegisterSysmemBufferCollection(std::move(service_handle),
std::move(sysmem_token), format, usage,
register_with_image_pipe);
}
#endif // BUILDFLAG(IS_FUCHSIA)
SyncToken ClientSharedImageInterface::GenUnverifiedSyncToken() {
return proxy_->GenUnverifiedSyncToken();
}
SyncToken ClientSharedImageInterface::GenVerifiedSyncToken() {
return proxy_->GenVerifiedSyncToken();
}
void ClientSharedImageInterface::VerifySyncToken(gpu::SyncToken& sync_token) {
proxy_->VerifySyncToken(sync_token);
}
void ClientSharedImageInterface::WaitSyncToken(
const gpu::SyncToken& sync_token) {
proxy_->WaitSyncToken(sync_token);
}
void ClientSharedImageInterface::Flush() {
proxy_->Flush();
}
scoped_refptr<gfx::NativePixmap> ClientSharedImageInterface::GetNativePixmap(
const gpu::Mailbox& mailbox) {
return proxy_->GetNativePixmap(mailbox);
}
scoped_refptr<ClientSharedImage> ClientSharedImageInterface::CreateSharedImage(
const SharedImageInfo& si_info,
gpu::SurfaceHandle surface_handle,
std::optional<SharedImagePoolId> pool_id) {
DCHECK_EQ(surface_handle, kNullSurfaceHandle);
DCHECK(gpu::IsValidClientUsage(si_info.meta.usage))
<< uint32_t(si_info.meta.usage);
auto mailbox = proxy_->CreateSharedImage(si_info, std::move(pool_id));
return base::MakeRefCounted<ClientSharedImage>(
AddMailbox(mailbox), si_info.meta, GenUnverifiedSyncToken(), holder_,
gfx::EMPTY_BUFFER);
}
scoped_refptr<ClientSharedImage> ClientSharedImageInterface::CreateSharedImage(
const SharedImageInfo& si_info,
base::span<const uint8_t> pixel_data) {
// Pixel upload path only supports single-planar formats.
DCHECK(si_info.meta.format.is_single_plane())
<< si_info.meta.format.ToString();
DCHECK(gpu::IsValidClientUsage(si_info.meta.usage))
<< uint32_t(si_info.meta.usage);
// EstimatedSizeInBytes() returns the minimum size in bytes needed to store
// `format` at `size` so if span is smaller there is a problem.
CHECK_GE(pixel_data.size(),
si_info.meta.format.EstimatedSizeInBytes(si_info.meta.size));
auto mailbox = proxy_->CreateSharedImage(si_info, pixel_data);
if (mailbox.IsZero()) {
return nullptr;
}
return base::MakeRefCounted<ClientSharedImage>(
AddMailbox(mailbox), si_info.meta, GenUnverifiedSyncToken(), holder_,
gfx::EMPTY_BUFFER);
}
scoped_refptr<ClientSharedImage> ClientSharedImageInterface::CreateSharedImage(
const SharedImageInfo& si_info,
gpu::SurfaceHandle surface_handle,
gfx::BufferUsage buffer_usage,
std::optional<SharedImagePoolId> pool_id) {
DCHECK_EQ(surface_handle, kNullSurfaceHandle);
DCHECK(gpu::IsValidClientUsage(si_info.meta.usage))
<< uint32_t(si_info.meta.usage);
gfx::GpuMemoryBufferHandle buffer_handle;
// Copy which can be modified.
SharedImageInfo si_info_copy = si_info;
auto mailbox = proxy_->CreateSharedImage(si_info_copy, buffer_usage,
std::move(pool_id), &buffer_handle);
if (mailbox.IsZero()) {
return nullptr;
}
CHECK(!buffer_handle.is_null());
return base::MakeRefCounted<ClientSharedImage>(
AddMailbox(mailbox), si_info_copy.meta, GenUnverifiedSyncToken(),
GpuMemoryBufferHandleInfo(std::move(buffer_handle),
si_info_copy.meta.format,
si_info_copy.meta.size, buffer_usage),
holder_, shared_memory_pool_);
}
scoped_refptr<ClientSharedImage> ClientSharedImageInterface::CreateSharedImage(
const SharedImageInfo& si_info,
gpu::SurfaceHandle surface_handle,
gfx::BufferUsage buffer_usage,
gfx::GpuMemoryBufferHandle buffer_handle) {
DCHECK(gpu::IsValidClientUsage(si_info.meta.usage))
<< uint32_t(si_info.meta.usage);
DCHECK(viz::HasEquivalentBufferFormat(si_info.meta.format))
<< si_info.meta.format.ToString();
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
CHECK(!si_info.meta.format.PrefersExternalSampler())
<< si_info.meta.format.ToString();
#endif
auto client_buffer_handle = buffer_handle.Clone();
auto mailbox = proxy_->CreateSharedImage(si_info, std::move(buffer_handle));
return base::MakeRefCounted<ClientSharedImage>(
AddMailbox(mailbox), si_info.meta, GenUnverifiedSyncToken(),
GpuMemoryBufferHandleInfo(std::move(client_buffer_handle),
si_info.meta.format, si_info.meta.size,
buffer_usage),
holder_, shared_memory_pool_);
}
scoped_refptr<ClientSharedImage> ClientSharedImageInterface::CreateSharedImage(
const SharedImageInfo& si_info,
gfx::GpuMemoryBufferHandle buffer_handle) {
DCHECK(gpu::IsValidClientUsage(si_info.meta.usage))
<< uint32_t(si_info.meta.usage);
DCHECK(viz::HasEquivalentBufferFormat(si_info.meta.format))
<< si_info.meta.format.ToString();
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
CHECK(!si_info.meta.format.PrefersExternalSampler())
<< si_info.meta.format.ToString();
#endif
auto buffer_handle_type = buffer_handle.type;
auto mailbox = proxy_->CreateSharedImage(si_info, std::move(buffer_handle));
return base::MakeRefCounted<ClientSharedImage>(
AddMailbox(mailbox), si_info.meta, GenUnverifiedSyncToken(), holder_,
buffer_handle_type);
}
SharedImageInterface::SharedImageMapping
ClientSharedImageInterface::CreateSharedImage(const SharedImageInfo& si_info) {
base::WritableSharedMemoryMapping mapping;
gfx::GpuMemoryBufferHandle handle;
CreateSharedMemoryRegionFromSIInfo(si_info, mapping, handle);
auto mailbox = proxy_->CreateSharedImage(si_info, std::move(handle));
SharedImageInterface::SharedImageMapping shared_image_mapping;
shared_image_mapping.mapping = std::move(mapping);
shared_image_mapping.shared_image = base::MakeRefCounted<ClientSharedImage>(
AddMailbox(mailbox), si_info.meta, GenUnverifiedSyncToken(), holder_,
gfx::SHARED_MEMORY_BUFFER);
return shared_image_mapping;
}
scoped_refptr<ClientSharedImage>
ClientSharedImageInterface::CreateSharedImageForSoftwareCompositor(
const SharedImageInfo& si_info) {
base::WritableSharedMemoryMapping mapping;
gfx::GpuMemoryBufferHandle handle;
CreateSharedMemoryRegionFromSIInfo(si_info, mapping, handle);
auto mailbox = proxy_->CreateSharedImage(si_info, std::move(handle));
return base::MakeRefCounted<ClientSharedImage>(
AddMailbox(mailbox), si_info.meta, GenUnverifiedSyncToken(), holder_,
std::move(mapping));
}
void ClientSharedImageInterface::CopyToGpuMemoryBuffer(
const SyncToken& sync_token,
const Mailbox& mailbox) {
proxy_->CopyToGpuMemoryBuffer(sync_token, mailbox);
}
#if BUILDFLAG(IS_WIN)
void ClientSharedImageInterface::CopyToGpuMemoryBufferAsync(
const SyncToken& sync_token,
const Mailbox& mailbox,
base::OnceCallback<void(bool)> callback) {
proxy_->CopyToGpuMemoryBufferAsync(sync_token, mailbox, std::move(callback));
}
void ClientSharedImageInterface::UpdateSharedImage(
const SyncToken& sync_token,
scoped_refptr<gfx::D3DSharedFence> d3d_shared_fence,
const Mailbox& mailbox) {
proxy_->UpdateSharedImage(sync_token, std::move(d3d_shared_fence), mailbox);
}
bool ClientSharedImageInterface::CopyNativeGmbToSharedMemorySync(
gfx::GpuMemoryBufferHandle buffer_handle,
base::UnsafeSharedMemoryRegion memory_region) {
CHECK_EQ(buffer_handle.type, gfx::GpuMemoryBufferType::DXGI_SHARED_HANDLE);
CHECK(memory_region.IsValid());
bool status = false;
proxy_->CopyNativeGmbToSharedMemorySync(std::move(buffer_handle),
std::move(memory_region), &status);
return status;
}
void ClientSharedImageInterface::CopyNativeGmbToSharedMemoryAsync(
gfx::GpuMemoryBufferHandle buffer_handle,
base::UnsafeSharedMemoryRegion memory_region,
base::OnceCallback<void(bool)> callback) {
CHECK_EQ(buffer_handle.type, gfx::GpuMemoryBufferType::DXGI_SHARED_HANDLE);
CHECK(memory_region.IsValid());
proxy_->CopyNativeGmbToSharedMemoryAsync(
std::move(buffer_handle), std::move(memory_region), std::move(callback));
}
bool ClientSharedImageInterface::IsConnected() {
return proxy_->IsConnected();
}
#endif
ClientSharedImageInterface::SwapChainSharedImages
ClientSharedImageInterface::CreateSwapChain(viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
gpu::SharedImageUsageSet usage) {
DCHECK(gpu::IsValidClientUsage(usage));
auto mailboxes = proxy_->CreateSwapChain(format, size, color_space,
surface_origin, alpha_type, usage);
AddMailbox(mailboxes.front_buffer);
AddMailbox(mailboxes.back_buffer);
SyncToken sync_token = GenUnverifiedSyncToken();
return ClientSharedImageInterface::SwapChainSharedImages(
base::MakeRefCounted<ClientSharedImage>(
mailboxes.front_buffer,
SharedImageMetadata(format, size, color_space, surface_origin,
alpha_type, usage),
sync_token, holder_, gfx::EMPTY_BUFFER),
base::MakeRefCounted<ClientSharedImage>(
mailboxes.back_buffer,
SharedImageMetadata(format, size, color_space, surface_origin,
alpha_type, usage),
sync_token, holder_, gfx::EMPTY_BUFFER));
}
void ClientSharedImageInterface::DestroySharedImage(const SyncToken& sync_token,
const Mailbox& mailbox) {
DCHECK(!mailbox.IsZero());
{
base::AutoLock lock(lock_);
auto it = mailboxes_.find(mailbox);
CHECK(it != mailboxes_.end());
mailboxes_.erase(it);
}
proxy_->DestroySharedImage(sync_token, mailbox);
}
void ClientSharedImageInterface::DestroySharedImage(
const SyncToken& sync_token,
scoped_refptr<ClientSharedImage> client_shared_image) {
CHECK(client_shared_image->HasOneRef());
CHECK(client_shared_image->HasHolder());
client_shared_image->UpdateDestructionSyncToken(sync_token);
}
scoped_refptr<ClientSharedImage> ClientSharedImageInterface::ImportSharedImage(
const ExportedSharedImage& exported_shared_image) {
const auto& mailbox = exported_shared_image.mailbox_;
const auto& metadata = exported_shared_image.metadata_;
const auto& sync_token = exported_shared_image.creation_sync_token_;
uint32_t texture_target = exported_shared_image.texture_target_;
DCHECK(!mailbox.IsZero());
AddMailbox(mailbox);
proxy_->AddReferenceToSharedImage(sync_token, mailbox);
return base::WrapRefCounted<ClientSharedImage>(new ClientSharedImage(
mailbox, metadata, sync_token, holder_, texture_target));
}
scoped_refptr<ClientSharedImage> ClientSharedImageInterface::NotifyMailboxAdded(
const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
gpu::SharedImageUsageSet usage) {
AddMailbox(mailbox);
proxy_->NotifyMailboxAdded(mailbox, usage);
return base::MakeRefCounted<ClientSharedImage>(
mailbox,
SharedImageMetadata(format, size, color_space, surface_origin, alpha_type,
usage),
GenUnverifiedSyncToken(), holder_, gfx::EMPTY_BUFFER);
}
scoped_refptr<ClientSharedImage> ClientSharedImageInterface::NotifyMailboxAdded(
const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
gpu::SharedImageUsageSet usage,
uint32_t texture_target) {
AddMailbox(mailbox);
proxy_->NotifyMailboxAdded(mailbox, usage);
return base::WrapRefCounted<ClientSharedImage>(new ClientSharedImage(
mailbox,
SharedImageMetadata(format, size, color_space, surface_origin, alpha_type,
usage),
GenUnverifiedSyncToken(), holder_, texture_target));
}
Mailbox ClientSharedImageInterface::AddMailbox(const gpu::Mailbox& mailbox) {
if (mailbox.IsZero())
return mailbox;
base::AutoLock lock(lock_);
mailboxes_.insert(mailbox);
return mailbox;
}
const SharedImageCapabilities& ClientSharedImageInterface::GetCapabilities() {
return proxy_->GetCapabilities();
}
} // namespace gpu