DMA-BUF is a Linux kernel mechanism that lets independent subsystems — GPU drivers, V4L2 video decoders, camera ISPs, and display controllers — share the same physical memory without any copying. A producing subsystem exports the buffer as a file descriptor; a consuming subsystem imports that file descriptor to gain its own handle to the same pages. gbm.rs exposes two import paths:Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Smithay/gbm.rs/llms.txt
Use this file to discover all available pages before exploring further.
import_buffer_object_from_dma_buf for single-plane buffers (common for RGB and packed YUV), and import_buffer_object_from_dma_buf_with_modifiers for multi-plane buffers with explicit layout modifiers (required for formats like NV12 or P010 produced by hardware decoders). The resulting BufferObject can then be used as a DRM framebuffer or sampled in an EGL shader just like any other GBM allocation.
Single-Plane Import
Useimport_buffer_object_from_dma_buf when the external buffer has a single memory plane — for example, an XRGB8888 frame from a screen capture or a YUYV camera frame.
Method Signature
| Parameter | Type | Description |
|---|---|---|
buffer | BorrowedFd<'_> | The DMA-BUF file descriptor exported by the producer. The borrow prevents use-after-close bugs. |
width | u32 | Width of the buffer in pixels. |
height | u32 | Height of the buffer in pixels. |
stride | u32 | Row stride in bytes (bytes per line, including padding). |
format | Format | Pixel format (a DrmFourcc value, e.g. Format::Xrgb8888). |
usage | BufferObjectFlags | Intended usage: SCANOUT, RENDERING, or both. |
Example: Importing a Camera Buffer
Multi-Plane Import with Modifiers
Hardware video decoders typically produce multi-plane YUV buffers with a vendor-specific memory layout (modifier).import_buffer_object_from_dma_buf_with_modifiers accepts up to four planes, each with its own file descriptor, stride, and offset, along with an explicit Modifier describing the tiling layout.
Method Signature
| Parameter | Type | Description |
|---|---|---|
len | u32 | Number of active planes (1–4). |
buffers | [Option<BorrowedFd<'_>>; 4] | One BorrowedFd per active plane; unused slots are None. |
width | u32 | Width of the image in pixels. |
height | u32 | Height of the image in pixels (luma plane). |
format | Format | Multi-planar pixel format, e.g. Format::Nv12. |
usage | BufferObjectFlags | Intended usage flags. |
strides | [i32; 4] | Byte stride for each plane; unused entries are 0. |
offsets | [i32; 4] | Byte offset within each plane’s fd; usually 0 per plane. |
modifier | Modifier | DRM format modifier describing the memory layout (tiling, compression). Use Modifier::Linear for linear buffers. |
Example: Importing an NV12 Video Frame
NV12 is a biplanar YUV 4:2:0 format. The luma (Y) plane and chroma (UV) plane are often exported as separate file descriptors by hardware decoders.Exporting a GBM Buffer as DMA-BUF
gbm.rs can also go the other direction: export aBufferObject as a DMA-BUF file descriptor so that other subsystems (a video encoder, a Vulkan import, another GPU) can consume it.
Single-Plane Export with fd()
Per-Plane Export with fd_for_plane()
For multi-plane buffers, export each plane individually:
Every call to
fd() or fd_for_plane() creates a brand-new OwnedFd backed by a fresh kernel file descriptor (via dup internally). The returned OwnedFd is independent of the BufferObject’s lifetime — you can keep it alive after dropping the BufferObject. However, each outstanding file descriptor holds a reference to the underlying DMA-BUF, preventing the memory from being freed until all file descriptors are closed.