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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
gpu / command_buffer / client / shared_image_interface.h [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef GPU_COMMAND_BUFFER_CLIENT_SHARED_IMAGE_INTERFACE_H_
#define GPU_COMMAND_BUFFER_CLIENT_SHARED_IMAGE_INTERFACE_H_
#include <cstdint>
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "build/build_config.h"
#include "components/viz/common/resources/shared_image_format.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/common/shared_image_pool_id.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/common/sync_token.h"
#include "gpu/gpu_export.h"
#include "gpu/ipc/common/surface_handle.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/gpu/ganesh/GrTypes.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gfx/color_space.h"
#include "ui/gfx/gpu_memory_buffer.h"
#if !BUILDFLAG(IS_NACL)
#include "ui/gfx/native_pixmap.h"
#include "ui/gfx/native_pixmap_handle.h"
#endif
#if BUILDFLAG(IS_FUCHSIA)
#include <lib/zx/channel.h>
#include <lib/zx/eventpair.h>
#endif // BUILDFLAG(IS_FUCHSIA)
namespace gfx {
class GpuFence;
class Size;
#if BUILDFLAG(IS_WIN)
class D3DSharedFence;
#endif
} // namespace gfx
namespace media {
class MockSharedImageInterface;
}
namespace gpu {
class ClientSharedImage;
class ClientSharedImageInterface;
struct ExportedSharedImage;
class GpuChannelSharedImageInterface;
struct SharedImageCapabilities;
class SharedImageInterfaceHolder;
class SharedImageInterfaceInProcess;
class TestSharedImageInterface;
struct SharedImageMetadata {
viz::SharedImageFormat format;
gfx::Size size;
gfx::ColorSpace color_space;
GrSurfaceOrigin surface_origin;
SkAlphaType alpha_type;
SharedImageUsageSet usage;
};
struct SharedImageInfo {
SharedImageInfo(const viz::SharedImageFormat& format,
gfx::Size size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SharedImageUsageSet usage,
std::string_view debug_label)
: meta(format, size, color_space, surface_origin, alpha_type, usage),
debug_label(debug_label) {}
SharedImageInfo(const viz::SharedImageFormat& format,
gfx::Size size,
const gfx::ColorSpace& color_space,
SharedImageUsageSet usage,
std::string_view debug_label)
: meta(format,
size,
color_space,
kTopLeft_GrSurfaceOrigin,
kPremul_SkAlphaType,
usage),
debug_label(debug_label) {}
SharedImageMetadata meta;
std::string debug_label;
};
// An interface to create shared images and swap chains that can be imported
// into other APIs. This interface is thread-safe and (essentially) stateless.
// It is asynchronous in the same sense as GLES2Interface or RasterInterface in
// that commands are executed asynchronously on the service side, but can be
// synchronized using SyncTokens. See //docs/design/gpu_synchronization.md.
class GPU_EXPORT SharedImageInterface
: public base::RefCountedThreadSafe<SharedImageInterface> {
public:
// Creates a shared image of requested |format|, |size| and |color_space|.
// |usage| is a combination of |SharedImageUsage| bits that describes which
// API(s) the image will be used with.
// Returns a non-null scoped_refptr to ClientSharedImage. The
// ClientSharedImage struct contains a mailbox that can be imported into said
// APIs using their corresponding shared image functions (e.g.
// GLES2Interface::CreateAndTexStorage2DSharedImageCHROMIUM or
// RasterInterface::CopySharedImage).
// The |SharedImageInterface| keeps ownership of the image until
// |DestroySharedImage| is called or the interface itself is destroyed (e.g.
// the GPU channel is lost).
// |debug_label| is retained for heap dumps and passed to graphics APIs for
// tracing tools. Pick a name that is unique to the allocation site.
virtual scoped_refptr<ClientSharedImage> CreateSharedImage(
const SharedImageInfo& si_info,
gpu::SurfaceHandle surface_handle,
std::optional<SharedImagePoolId> pool_id = std::nullopt) = 0;
// Same behavior as the above, except that this version takes |pixel_data|
// which is used to populate the SharedImage. |pixel_data| should have the
// same format which would be passed to glTexImage2D to populate a similarly
// specified texture.
// May return null if |pixel_data| is too big for IPC.
// TODO(crbug.com/40268891): Have the caller specify a row span for
// |pixel_data| explicitly. Some backings have different row alignment
// requirements which the caller has to match exactly or it won't work.
virtual scoped_refptr<ClientSharedImage> CreateSharedImage(
const SharedImageInfo& si_info,
base::span<const uint8_t> pixel_data) = 0;
// Same behavior as above methods, except that this version is specifically
// used by clients which intend to create a shared image back by either a
// native buffer (if supported) or shared memory which are CPU mappable.
// We are currently passing BufferUsage to this method for simplicity since
// as of now we dont have a clear way to map BufferUsage to SharedImageUsage.
// May return null if GPU memory buffer creation fails.
// TODO(crbug.com/40276844): Merge this method to above existing methods once
// we figure out mapping between BufferUsage and SharedImageUsage and
// eliminate all usages of BufferUsage.
virtual scoped_refptr<ClientSharedImage> CreateSharedImage(
const SharedImageInfo& si_info,
gpu::SurfaceHandle surface_handle,
gfx::BufferUsage buffer_usage,
std::optional<SharedImagePoolId> pool_id = std::nullopt);
// Creates a shared image out an existing buffer. The buffer described by
// `buffer_handle` must hold all planes based on `format` and `size`. This
// version is specifically used by clients that need access to the buffer on
// the client side. It ensures that
// ClientSharedImage::CloneGpuMemoryBufferHandle() can be invoked on the
// returned ClientSharedImage.
// NOTE: We are currently passing BufferUsage to this method for simplicity
// since as of now we dont have a clear way to map BufferUsage to
// SharedImageUsage.
// TODO(crbug.com/40276844): Merge this method to above existing methods once
// we figure out mapping between BufferUsage and SharedImageUsage and
// eliminate all usages of BufferUsage.
virtual scoped_refptr<ClientSharedImage> CreateSharedImage(
const SharedImageInfo& si_info,
gpu::SurfaceHandle surface_handle,
gfx::BufferUsage buffer_usage,
gfx::GpuMemoryBufferHandle buffer_handle) = 0;
// Creates a shared image out an existing buffer. The buffer described by
// `buffer_handle` must hold all planes based `format` and `size. `usage` is a
// combination of |SharedImageUsage| bits that describes which API(s) the
// image will be used with.
//
// SharedImageInterface keeps ownership of the image until
// `DestroySharedImage()` is called or the interface itself is destroyed (e.g.
// the GPU channel is lost).
virtual scoped_refptr<ClientSharedImage> CreateSharedImage(
const SharedImageInfo& si_info,
gfx::GpuMemoryBufferHandle buffer_handle) = 0;
struct GPU_EXPORT SharedImageMapping {
SharedImageMapping(SharedImageMapping& mapped) = delete;
SharedImageMapping& operator=(SharedImageMapping& mapped) = delete;
SharedImageMapping();
SharedImageMapping(SharedImageMapping&& mapped);
SharedImageMapping(scoped_refptr<ClientSharedImage> shared_image,
base::WritableSharedMemoryMapping mapping);
SharedImageMapping& operator=(SharedImageMapping&& mapped);
~SharedImageMapping();
scoped_refptr<ClientSharedImage> shared_image;
base::WritableSharedMemoryMapping mapping;
};
// Creates a shared image with the usage of
// gpu::SHARED_IMAGE_USAGE_CPU_WRITE_ONLY only. A shared memory buffer is
// created internally and a shared image is created out this buffer. This
// method is used by the software compositor only.
virtual SharedImageMapping CreateSharedImage(
const SharedImageInfo& si_info) = 0;
// Creates a shared image with the usage of
// gpu::SHARED_IMAGE_USAGE_CPU_WRITE_ONLY only. A shared memory buffer is
// created internally and a shared image is created out of this buffer. This
// method is used by the software compositor only.
virtual scoped_refptr<ClientSharedImage>
CreateSharedImageForSoftwareCompositor(const SharedImageInfo& si_info) = 0;
// Updates a shared image after its GpuMemoryBuffer (if any) was modified on
// the CPU or through external devices, after |sync_token| has been released.
virtual void UpdateSharedImage(const SyncToken& sync_token,
const Mailbox& mailbox) = 0;
// Updates a shared image after its GpuMemoryBuffer (if any) was modified on
// the CPU or through external devices, after |sync_token| has been released.
// If |acquire_fence| is not null, the fence is inserted in the GPU command
// stream and a server side wait is issued before any GPU command referring
// to this shared imaged is executed on the GPU.
virtual void UpdateSharedImage(const SyncToken& sync_token,
std::unique_ptr<gfx::GpuFence> acquire_fence,
const Mailbox& mailbox) = 0;
// Update the GpuMemoryBuffer associated with the shared image |mailbox| after
// |sync_token| is released. This needed when the GpuMemoryBuffer is backed by
// shared memory on platforms like Windows where the renderer cannot create
// native GMBs.
virtual void CopyToGpuMemoryBuffer(const SyncToken& sync_token,
const Mailbox& mailbox);
// Update the GpuMemoryBuffer associated with the shared image |mailbox| after
// |sync_token| is released. The |callback| is run denoting if the copy was
// successful and the GpuMemoryBuffer is ready to be mapped by the client.
// This is needed when the GpuMemoryBuffer is backed by shared memory on
// platforms like Windows where the renderer cannot create native GMBs.
virtual void CopyToGpuMemoryBufferAsync(
const SyncToken& sync_token,
const Mailbox& mailbox,
base::OnceCallback<void(bool)> callback);
// On windows, native GMB can not be mapped in any process other than the GPU
// process. So during GpuMemoryBuffer::Map() operation, an IPC to GPU process
// is done to copy the content of |buffer_handle| into a shared memory
// |memory_region| via below methods. This shared memory is mappable in any
// process and is used internally during GpuMemoryBuffer::Map().
// This will block on calling client thread.
virtual bool CopyNativeGmbToSharedMemorySync(
gfx::GpuMemoryBufferHandle buffer_handle,
base::UnsafeSharedMemoryRegion memory_region);
// This is non-blocking version of above method. The |callback| will be run
// when the copy is done.
virtual void CopyNativeGmbToSharedMemoryAsync(
gfx::GpuMemoryBufferHandle buffer_handle,
base::UnsafeSharedMemoryRegion memory_region,
base::OnceCallback<void(bool)> callback);
// Checks if the GpuChannel is connected to this interface. This is
// used on windows to find if SII is still connected to the GPU service so
// that GpuMemoryBufferManager can use it.
virtual bool IsConnected();
// Destroys the shared image, unregistering its mailbox, after |sync_token|
// has been released. After this call, the mailbox can't be used to reference
// the image any more, however if the image was imported into other APIs,
// those may keep a reference to the underlying data.
virtual void DestroySharedImage(const SyncToken& sync_token,
const Mailbox& mailbox) = 0;
// Same behavior as the above, except that this version takes
// a |client_shared_image| parameter (which holds a mailbox).
virtual void DestroySharedImage(
const SyncToken& sync_token,
scoped_refptr<ClientSharedImage> client_shared_image) = 0;
// Adds another owning reference to the SharedImage. It must be released via
// DestroySharedImage in the same way as for SharedImages created via
// CreateSharedImage(). Note: The image must have been created on different
// gpu channel and each can have only single reference.
// Note: `usage` must be the same value as passed to CreateSharedImage call
// and is just stored without validation.
// Note: `texture_target` is the texture target that should be used for this
// SharedImage.
virtual scoped_refptr<ClientSharedImage> AddReferenceToSharedImage(
const SyncToken& sync_token,
const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SharedImageUsageSet usage,
uint32_t texture_target);
// Imports SharedImage to this interface and returns an owning reference. It
// must be released via DestroySharedImage in the same way as for SharedImages
// created via CreateSharedImage().
virtual scoped_refptr<ClientSharedImage> ImportSharedImage(
const ExportedSharedImage& exported_shared_image) = 0;
struct GPU_EXPORT SwapChainSharedImages {
SwapChainSharedImages(scoped_refptr<gpu::ClientSharedImage> front_buffer,
scoped_refptr<gpu::ClientSharedImage> back_buffer);
SwapChainSharedImages(const SwapChainSharedImages& shared_images);
~SwapChainSharedImages();
scoped_refptr<gpu::ClientSharedImage> front_buffer;
scoped_refptr<gpu::ClientSharedImage> back_buffer;
};
// Creates a swap chain.
// Returns shared images for front and back buffers of a DXGI Swap Chain that
// can be imported into GL command buffer using shared image functions (e.g.
// GLES2Interface::CreateAndTexStorage2DSharedImageCHROMIUM).
virtual SwapChainSharedImages CreateSwapChain(
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
gpu::SharedImageUsageSet usage) = 0;
// Swaps front and back buffer of a swap chain. Back buffer mailbox still
// refers to the back buffer of the swap chain after calling PresentSwapChain.
// The mailbox argument should be back buffer mailbox. Sync token is required
// for synchronization between shared image stream and command buffer stream,
// to ensure that all the rendering commands to a frame are executed before
// presenting the swap chain.
virtual void PresentSwapChain(const SyncToken& sync_token,
const Mailbox& mailbox) = 0;
#if BUILDFLAG(IS_FUCHSIA)
// Registers a sysmem buffer collection. `service_handle` contains a handle
// for the eventpair that controls the lifetime of the collection. The
// collection will be destroyed when all peer handles for that eventpair are
// destroyed (i.e. when `ZX_EVENTPAIR_PEER_CLOSED` is signaled on that
// handle). The caller can use CreateSharedImage() to create shared images
// from the buffer in the collection by setting `buffer_collection_handle` and
// `buffer_index` fields in NativePixmapHandle, wrapping it in
// GpuMemoryBufferHandle and then creating a GpuMemoryBuffer from that handle.
// If `register_with_image_pipe` field is set, the collection is shared with a
// new ImagePipe, which allows it to display these images as overlays.
virtual void RegisterSysmemBufferCollection(
zx::eventpair service_handle,
zx::channel sysmem_token,
const viz::SharedImageFormat& format,
gfx::BufferUsage usage,
bool register_with_image_pipe) = 0;
#endif // BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_WIN)
// Update fence between processes. Register D3DSharedFence in GPU process
// first and then use DXGIHandleToken to identify the fence between processes
// and pass signaled fence value from current process to GPU process.
virtual void UpdateSharedImage(
const SyncToken& sync_token,
scoped_refptr<gfx::D3DSharedFence> d3d_shared_fence,
const Mailbox& mailbox);
#endif // BUILDFLAG(IS_WIN)
// Generates an unverified SyncToken that is released after all previous
// commands on this interface have executed on the service side.
virtual SyncToken GenUnverifiedSyncToken() = 0;
// Generates a verified SyncToken that is released after all previous
// commands on this interface have executed on the service side.
virtual SyncToken GenVerifiedSyncToken() = 0;
// Verifies the SyncToken.
virtual void VerifySyncToken(gpu::SyncToken& sync_token) = 0;
// Wait on this SyncToken to be released before executing new commands on
// this interface on the service side. This is an async wait for all the
// previous commands which will be sent to server on the next flush().
virtual void WaitSyncToken(const gpu::SyncToken& sync_token) = 0;
// Flush the SharedImageInterface, issuing any deferred IPCs.
virtual void Flush() = 0;
#if !BUILDFLAG(IS_NACL)
// Returns the NativePixmap backing |mailbox|. This is a privileged API. Only
// the callers living inside the GPU process are able to retrieve the
// NativePixmap; otherwise null is returned. Also returns null if the
// SharedImage doesn't exist or is not backed by a NativePixmap. The caller is
// not expected to read from or write into the provided NativePixmap because
// it can be modified at any time. The primary purpose of this method is to
// facilitate pageflip testing on the viz thread.
virtual scoped_refptr<gfx::NativePixmap> GetNativePixmap(
const gpu::Mailbox& mailbox) = 0;
#endif
// Informs that existing |mailbox| with the specified metadata can be passed
// to DestroySharedImage().
virtual scoped_refptr<ClientSharedImage> NotifyMailboxAdded(
const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SharedImageUsageSet usage);
virtual scoped_refptr<ClientSharedImage> NotifyMailboxAdded(
const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SharedImageUsageSet usage,
uint32_t texture_target);
virtual const SharedImageCapabilities& GetCapabilities() = 0;
void Release() const;
protected:
friend class base::RefCountedThreadSafe<SharedImageInterface>;
virtual ~SharedImageInterface();
// Creates a WritableSharedMemoryRegion corresponding to the format/size
// passed in `si_info` and populates `mapping` and `handle` from the created
// shmem region. Fails if the shmem region cannot be created or mapped. For
// usage in implementing APIs that create mappable SharedImages without
// holding on to a handle on the client side for usage with the software
// compositor. As such, verifies that `si_info`'s usage is
// `SHARED_IMAGE_USAGE_CPU_WRITE_ONLY`.
static void CreateSharedMemoryRegionFromSIInfo(
const SharedImageInfo& si_info,
base::WritableSharedMemoryMapping& mapping,
gfx::GpuMemoryBufferHandle& handle);
scoped_refptr<SharedImageInterfaceHolder> holder_;
private:
friend class ClientSharedImageInterface;
friend class GpuChannelSharedImageInterface;
friend class SharedImageInterfaceInProcess;
friend class TestSharedImageInterface;
friend class media::MockSharedImageInterface;
// Make the constructor private to ensure that any new subclassing of this
// interface gets explicit approval from //gpu OWNERS (by adding to the list
// of friends above). In particular, do not subclass this interface for
// testing purposes - use (and extend if necessary) TestSharedImageInterface
// instead.
SharedImageInterface();
};
// |SharedImageInterfaceHolder| provides thread-safe access to
// |SharedImageInterface| via a weak reference.
class GPU_EXPORT SharedImageInterfaceHolder
: public base::RefCountedThreadSafe<SharedImageInterfaceHolder> {
public:
SharedImageInterfaceHolder(SharedImageInterface* sii);
scoped_refptr<SharedImageInterface> Get();
private:
friend base::RefCountedThreadSafe<SharedImageInterfaceHolder>;
friend SharedImageInterface;
~SharedImageInterfaceHolder();
void OnDestroy();
mutable base::Lock lock_;
raw_ptr<SharedImageInterface> sii_ GUARDED_BY(lock_);
};
} // namespace gpu
#endif // GPU_COMMAND_BUFFER_CLIENT_SHARED_IMAGE_INTERFACE_H_