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
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_GFX_GPU_MEMORY_BUFFER_HANDLE_H_
#define UI_GFX_GPU_MEMORY_BUFFER_HANDLE_H_
#include <stddef.h>
#include <stdint.h>
#include <utility>
#include "base/check_op.h"
#include "base/component_export.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/notreached.h"
#include "build/build_config.h"
#include "ui/gfx/generic_shared_memory_id.h"
#include "ui/gfx/geometry/rect.h"
#if BUILDFLAG(IS_OZONE)
#include "ui/gfx/native_pixmap_handle.h"
#elif BUILDFLAG(IS_APPLE)
#include "ui/gfx/mac/io_surface.h"
#elif BUILDFLAG(IS_WIN)
#include <optional>
#include "base/types/token_type.h"
#include "base/win/scoped_handle.h"
#elif BUILDFLAG(IS_ANDROID)
#include "base/android/scoped_hardware_buffer_handle.h"
#endif
namespace mojo {
template <typename, typename>
struct StructTraits;
template <typename, typename>
struct UnionTraits;
} // namespace mojo
namespace gfx {
namespace mojom {
class DXGIHandleDataView;
class GpuMemoryBufferPlatformHandleDataView;
} // namespace mojom
enum GpuMemoryBufferType {
EMPTY_BUFFER,
SHARED_MEMORY_BUFFER,
#if BUILDFLAG(IS_APPLE)
IO_SURFACE_BUFFER,
#elif BUILDFLAG(IS_OZONE)
NATIVE_PIXMAP,
#elif BUILDFLAG(IS_WIN)
DXGI_SHARED_HANDLE,
#elif BUILDFLAG(IS_ANDROID)
ANDROID_HARDWARE_BUFFER,
#endif
};
using GpuMemoryBufferId = GenericSharedMemoryId;
#if BUILDFLAG(IS_WIN)
using DXGIHandleToken = base::TokenType<class DXGIHandleTokenTypeMarker>;
// A simple type that bundles together the various bits for working with DXGI
// handles in Chrome. It consists of:
// - `buffer_handle`: the shared handle to the DXGI resource
// - `token`: A strongly-typed UnguessableToken used to determine if two
// instances of `DXGIHandle` represent the same underlying shared
// handle. Needed because the handle itself may be duplicated virtual
// `DuplicateHandle()`.
// - `region`: An optional shared memory region. A DXGI handle's buffer can only
// be read in the GPU process; this is used to provide support for
// `Map()`ing the buffer in other processes. Under the hood, this is
// implemented by having the GPU process copy the buffer into a
// shared memory region that other processes can read.
class COMPONENT_EXPORT(GFX) DXGIHandle {
public:
// Creates a DXGIHandle suitable for use in a barebones unit test. The
// `buffer_handle` won't actually usable as a DXGI shared handle, so this
// helper is not suitable for integration tests.
static DXGIHandle CreateFakeForTest();
// Constructs an instance where `IsValid() == false`.
DXGIHandle();
~DXGIHandle();
// Constructs an instance, taking ownership of `scoped_handle` and associating
// it with a new DXGIHandleToken. `scoped_handle` must be a valid handle.
explicit DXGIHandle(base::win::ScopedHandle scoped_handle);
// Typically only used by IPC deserialization. `buffer_handle` must be valid,
// but `region` may be invalid.
DXGIHandle(base::win::ScopedHandle buffer_handle,
const DXGIHandleToken& token,
base::UnsafeSharedMemoryRegion region);
DXGIHandle(DXGIHandle&&);
DXGIHandle& operator=(DXGIHandle&&);
DXGIHandle(const DXGIHandle&) = delete;
DXGIHandle& operator=(const DXGIHandle&) = delete;
// Whether or not `this` has a valid underlying platform handle. This method
// can return true even if `region()` is not a valid shmem region.
bool IsValid() const;
// Creates a copy of `this`. The underlying `buffer_handle` is duplicated into
// a new handle, but `token()` will be preserved, so callers should use
// `token()` to check if two `DXGIHandle`s actually refer to the same
// resource.
DXGIHandle Clone() const;
// Similar to above but also associate `region` with the cloned `DXGIHandle`.
//
// Precondition: `this` must not have an associated region, i.e.
// `region().IsValid()` is false.
DXGIHandle CloneWithRegion(base::UnsafeSharedMemoryRegion region) const;
HANDLE buffer_handle() const { return buffer_handle_.Get(); }
base::win::ScopedHandle TakeBufferHandle();
const DXGIHandleToken& token() const { return token_; }
const base::UnsafeSharedMemoryRegion& region() const { return region_; }
base::UnsafeSharedMemoryRegion TakeRegion() { return std::move(region_); }
private:
friend mojo::StructTraits<mojom::DXGIHandleDataView, DXGIHandle>;
base::win::ScopedHandle buffer_handle_;
DXGIHandleToken token_;
base::UnsafeSharedMemoryRegion region_;
};
#endif // BUILDFLAG(IS_WIN)
// TODO(crbug.com/40584691): Convert this to a proper class to ensure the state
// is always consistent, particularly that the only one handle is set at the
// same time and it corresponds to |type|.
struct COMPONENT_EXPORT(GFX) GpuMemoryBufferHandle {
static constexpr GpuMemoryBufferId kInvalidId = GpuMemoryBufferId(-1);
GpuMemoryBufferHandle();
explicit GpuMemoryBufferHandle(base::UnsafeSharedMemoryRegion region);
#if BUILDFLAG(IS_WIN)
explicit GpuMemoryBufferHandle(DXGIHandle handle);
#elif BUILDFLAG(IS_OZONE)
explicit GpuMemoryBufferHandle(gfx::NativePixmapHandle native_pixmap_handle);
#elif BUILDFLAG(IS_ANDROID)
explicit GpuMemoryBufferHandle(
base::android::ScopedHardwareBufferHandle handle);
#endif
GpuMemoryBufferHandle(GpuMemoryBufferHandle&& other);
GpuMemoryBufferHandle& operator=(GpuMemoryBufferHandle&& other);
~GpuMemoryBufferHandle();
GpuMemoryBufferHandle Clone() const;
bool is_null() const { return type == EMPTY_BUFFER; }
// The shared memory region may only be used with SHARED_MEMORY_BUFFER and
// DXGI_SHARED_HANDLE. In the case of DXGI handles, the actual contents of the
// buffer can only be accessed from the GPU process, so `Map()`ing the buffer
// into memory actually requires an IPC to the GPU process, which then copies
// the contents into the shmem region so it can be accessed from other
// processes.
const base::UnsafeSharedMemoryRegion& region() const& {
// For now, allow this to transparently forward as appropriate for DXGI or
// shmem handles in production. However, this will be a hard CHECK() in the
// future.
CHECK_EQ(type, SHARED_MEMORY_BUFFER, base::NotFatalUntil::M138);
switch (type) {
case SHARED_MEMORY_BUFFER:
return region_;
#if BUILDFLAG(IS_WIN)
case DXGI_SHARED_HANDLE:
return dxgi_handle_.region();
#endif // BUILDFLAG(IS_WIN)
default:
NOTREACHED();
}
}
base::UnsafeSharedMemoryRegion region() && {
CHECK_EQ(type, SHARED_MEMORY_BUFFER);
type = EMPTY_BUFFER;
return std::move(region_);
}
#if BUILDFLAG(IS_OZONE)
const NativePixmapHandle& native_pixmap_handle() const& {
CHECK_EQ(type, NATIVE_PIXMAP);
return native_pixmap_handle_;
}
NativePixmapHandle native_pixmap_handle() && {
CHECK_EQ(type, NATIVE_PIXMAP);
type = EMPTY_BUFFER;
return std::move(native_pixmap_handle_);
}
#endif // BUILDFLAG(IS_OZONE)
#if BUILDFLAG(IS_WIN)
const DXGIHandle& dxgi_handle() const& {
CHECK_EQ(type, DXGI_SHARED_HANDLE);
return dxgi_handle_;
}
DXGIHandle dxgi_handle() && {
CHECK_EQ(type, DXGI_SHARED_HANDLE);
type = EMPTY_BUFFER;
return std::move(dxgi_handle_);
}
#endif // BUILDFLAG(IS_WIN)
GpuMemoryBufferType type = GpuMemoryBufferType::EMPTY_BUFFER;
GpuMemoryBufferId id{0};
uint32_t offset = 0;
uint32_t stride = 0;
#if BUILDFLAG(IS_APPLE)
ScopedIOSurface io_surface;
#if BUILDFLAG(IS_IOS)
// On iOS, we can't use IOKit to access IOSurfaces in the renderer process, so
// we share the memory segment backing the IOSurface as shared memory which is
// then mapped in the renderer process.
ScopedRefCountedIOSurfaceMachPort io_surface_mach_port;
base::UnsafeSharedMemoryRegion io_surface_shared_memory_region;
// We have to pass the plane strides and offsets since we can't use IOSurface
// helper methods to get them.
static constexpr size_t kMaxIOSurfacePlanes = 3;
std::array<uint32_t, kMaxIOSurfacePlanes> io_surface_plane_strides;
std::array<uint32_t, kMaxIOSurfacePlanes> io_surface_plane_offsets;
#endif // BUILDFLAG(IS_IOS)
#endif // BUILDFLAG(IS_APPLE)
#if BUILDFLAG(IS_ANDROID)
base::android::ScopedHardwareBufferHandle android_hardware_buffer;
#endif // BUILDFLAG(IS_ANDROID)
private:
friend mojo::UnionTraits<mojom::GpuMemoryBufferPlatformHandleDataView,
GpuMemoryBufferHandle>;
// This naming isn't entirely styleguide-compliant, but per the TODO, the end
// goal is to make `this` an encapsulated class.
base::UnsafeSharedMemoryRegion region_;
#if BUILDFLAG(IS_OZONE)
NativePixmapHandle native_pixmap_handle_;
#endif // BUILDFLAG(IS_OZONE)
#if BUILDFLAG(IS_WIN)
DXGIHandle dxgi_handle_;
#endif // BUILDFLAG(IS_WIN)
};
} // namespace gfx
#endif // UI_GFX_GPU_MEMORY_BUFFER_HANDLE_H_
|