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
|
// Copyright 2013 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_H_
#define UI_GFX_GPU_MEMORY_BUFFER_H_
#include <stddef.h>
#include <stdint.h>
#include <utility>
#include "base/component_export.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "build/build_config.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gfx/generic_shared_memory_id.h"
#include "ui/gfx/geometry/rect.h"
#if BUILDFLAG(IS_OZONE) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#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 base {
namespace trace_event {
class ProcessMemoryDump;
class MemoryAllocatorDumpGuid;
} // namespace trace_event
} // namespace base
namespace mojo {
template <typename, typename>
struct StructTraits;
template <typename, typename>
struct UnionTraits;
} // namespace mojo
namespace gfx {
namespace mojom {
class DXGIHandleDataView;
class GpuMemoryBufferPlatformHandleDataView;
} // namespace mojom
class ColorSpace;
enum GpuMemoryBufferType {
EMPTY_BUFFER,
SHARED_MEMORY_BUFFER,
#if BUILDFLAG(IS_APPLE)
IO_SURFACE_BUFFER,
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
NATIVE_PIXMAP,
#endif
#if BUILDFLAG(IS_WIN)
DXGI_SHARED_HANDLE,
#endif
#if 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
// 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);
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
explicit GpuMemoryBufferHandle(gfx::NativePixmapHandle native_pixmap_handle);
#endif
#if 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_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
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
#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;
#elif BUILDFLAG(IS_ANDROID)
base::android::ScopedHardwareBufferHandle android_hardware_buffer;
#endif
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_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
NativePixmapHandle native_pixmap_handle_;
#endif
#if BUILDFLAG(IS_WIN)
DXGIHandle dxgi_handle_;
#endif // BUILDFLAG(IS_WIN)
};
// This interface typically correspond to a type of shared memory that is also
// shared with the GPU. A GPU memory buffer can be written to directly by
// regular CPU code, but can also be read by the GPU.
class COMPONENT_EXPORT(GFX) GpuMemoryBuffer {
public:
virtual ~GpuMemoryBuffer() {}
// Maps each plane of the buffer into the client's address space so it can be
// written to by the CPU. This call may block, for instance if the GPU needs
// to finish accessing the buffer or if CPU caches need to be synchronized.
// Returns false on failure.
virtual bool Map() = 0;
// Maps each plane of the buffer into the client's address space so it can be
// written to by the CPU. The default implementation is blocking and just
// calls Map(). However, on some platforms the implementations are
// non-blocking. In that case the result callback will be executed on the
// GpuMemoryThread if some work in the GPU service is required for mapping, or
// will be executed immediately in the current sequence. Warning: Make sure
// the GMB isn't destroyed before the callback is run otherwise GPU process
// might try to write in destroyed shared memory region. Don't attempt to
// Unmap or get memory before the callback is executed. Otherwise a CHECK will
// fire.
virtual void MapAsync(base::OnceCallback<void(bool)> result_cb);
// Indicates if the `MapAsync` is non-blocking. Otherwise it's just calling
// `Map()` directly.
virtual bool AsyncMappingIsNonBlocking() const;
// Returns a pointer to the memory address of a plane. Buffer must have been
// successfully mapped using a call to Map() before calling this function.
virtual void* memory(size_t plane) = 0;
// Unmaps the buffer. It's illegal to use any pointer returned by memory()
// after this has been called.
virtual void Unmap() = 0;
// Returns the size in pixels of the first plane of the buffer.
virtual Size GetSize() const = 0;
// Returns the format for the buffer.
virtual BufferFormat GetFormat() const = 0;
// Fills the stride in bytes for each plane of the buffer. The stride of
// plane K is stored at index K-1 of the |stride| array.
virtual int stride(size_t plane) const = 0;
#if BUILDFLAG(IS_APPLE)
// Set the color space in which this buffer should be interpreted when used
// as an overlay. Note that this will not impact texturing from the buffer.
// Used only for GMBs backed by an IOSurface.
virtual void SetColorSpace(const ColorSpace& color_space) {}
#endif
// Returns a unique identifier associated with buffer.
virtual GpuMemoryBufferId GetId() const = 0;
// Returns the type of this buffer.
virtual GpuMemoryBufferType GetType() const = 0;
// Returns a platform specific handle for this buffer which in particular can
// be sent over IPC. This duplicates file handles as appropriate, so that a
// caller takes ownership of the returned handle.
virtual GpuMemoryBufferHandle CloneHandle() const = 0;
#if BUILDFLAG(IS_WIN)
// Used to set the use_premapped_memory flag in the GpuMemoryBufferImplDXGI to
// indicate whether to use the premapped memory or not. It is only used with
// MappableSI. See GpuMemoryBufferImplDXGI override for more details.
virtual void SetUsePreMappedMemory(bool use_premapped_memory) {}
#endif
// Dumps information about the memory backing the GpuMemoryBuffer to |pmd|.
// The memory usage is attributed to |buffer_dump_guid|.
// |tracing_process_id| uniquely identifies the process owning the memory.
// |importance| is relevant only for the cases of co-ownership, the memory
// gets attributed to the owner with the highest importance.
virtual void OnMemoryDump(
base::trace_event::ProcessMemoryDump* pmd,
const base::trace_event::MemoryAllocatorDumpGuid& buffer_dump_guid,
uint64_t tracing_process_id,
int importance) const = 0;
};
} // namespace gfx
#endif // UI_GFX_GPU_MEMORY_BUFFER_H_
|