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.
Device<T> is the entry point for everything in gbm.rs. It wraps an open DRM file descriptor — typically a File or OwnedFd pointing to /dev/dri/card0 or a render node like /dev/dri/renderD128 — and calls gbm_create_device to initialise the GBM context for that GPU. Every buffer object and surface you allocate is produced by a Device, and the device keeps the underlying gbm_device alive for as long as any object it created is still in scope.
The Generic Parameter T
Device<T> is generic over T: AsFd. The AsFd bound means gbm.rs can borrow the file descriptor from any standard owner without taking ownership of it permanently. Common choices are:
| Type | Notes |
|---|---|
std::fs::File | Typical choice; opened with OpenOptions. Closed when the File drops. |
std::os::unix::io::OwnedFd | Bare owned descriptor with no File overhead. |
| Custom newtype | Any struct that wraps a fd and implements AsFd (e.g. a drm-rs Card). |
Device<T> implements Deref<Target = T> and DerefMut<Target = T>, so you can call methods on the inner type directly through a Device reference. If T also implements drm::Device or drm::control::Device, those method sets become available on the Device<T> reference without any casting.
Creating a Device
Device::new(fd) is the only constructor. It calls gbm_create_device with the raw file descriptor obtained from fd.as_fd(). On success it returns Ok(Device<T>); on failure it returns Err with the last OS error set by libgbm.
Cloning a Device
WhenT: Clone, Device<T> also implements Clone. Cloning shares the underlying gbm_device — both the original and the clone hold an Arc reference to the same raw pointer, and gbm_device_destroy is called exactly once when the last clone is dropped. The inner T is cloned independently (producing a second file descriptor if T is File).
Querying the Backend
backend_name()
Returns the name of the GBM backend that libgbm selected for this device. On Mesa systems this is typically "drm".
is_format_supported(format, usage)
Asks libgbm whether the driver supports allocating a buffer with the given pixel format and usage flags. Always call this before allocating a buffer with a format/usage combination you are not certain the driver handles.
format_modifier_plane_count(format, modifier)
Returns the number of planes required for a given format and modifier combination, or None if the combination does not have a fixed plane count (for example when Modifier::Invalid is used).
Allocating Buffers and Surfaces
Device is the factory for both BufferObject and Surface. Allocation methods are covered in detail on their own pages, but here is a brief overview:
create_buffer_object
Allocates a buffer with a given width, height, format, and usage flags.
Use for cursor planes, import targets, or any buffer you manage manually.
create_buffer_object_with_modifiers
Same as above but accepts an explicit list of DRM format modifiers,
letting the driver choose the optimal tiling layout.
create_buffer_object_with_modifiers2
Like
with_modifiers but also accepts usage flags, combining the
flexibility of both previous variants.create_surface
Allocates a GBM surface suitable for use as an EGL native window.
Returns a
Surface<U> that manages an internal buffer pool.Importing Foreign Buffers
Device can wrap external GPU memory as a BufferObject through several import paths:
import_buffer_object_from_wayland— imports awl_buffer(requiresimport-waylandfeature).import_buffer_object_from_egl— imports anEGLImage(requiresimport-eglfeature,unsafe).import_buffer_object_from_dma_buf_with_modifiers— multi-plane DMA-BUF import with explicit modifier.