xf86drmMode: add helpers for dumb buffers

Up until now, DRM clients had to hand-roll their code to create,
destroy and map dumb buffers. This is slightly inconvenient,
a bit error-prone, and not easily discoverable.

Introduce wrappers for these operations, just like we have for
other KMS IOCTLs.

Signed-off-by: Simon Ser <contact@emersion.fr>
main
Simon Ser 2022-10-27 19:11:20 +02:00
parent 82b2b1e898
commit 3be3b1a83f
3 changed files with 84 additions and 0 deletions

View File

@ -105,10 +105,12 @@ drmModeAtomicSetCursor
drmModeAttachMode drmModeAttachMode
drmModeConnectorGetPossibleCrtcs drmModeConnectorGetPossibleCrtcs
drmModeConnectorSetProperty drmModeConnectorSetProperty
drmModeCreateDumbBuffer
drmModeCreateLease drmModeCreateLease
drmModeCreatePropertyBlob drmModeCreatePropertyBlob
drmModeCrtcGetGamma drmModeCrtcGetGamma
drmModeCrtcSetGamma drmModeCrtcSetGamma
drmModeDestroyDumbBuffer
drmModeDestroyPropertyBlob drmModeDestroyPropertyBlob
drmModeDetachMode drmModeDetachMode
drmModeDirtyFB drmModeDirtyFB
@ -139,6 +141,7 @@ drmModeGetProperty
drmModeGetPropertyBlob drmModeGetPropertyBlob
drmModeGetResources drmModeGetResources
drmModeListLessees drmModeListLessees
drmModeMapDumbBuffer
drmModeMoveCursor drmModeMoveCursor
drmModeObjectGetProperties drmModeObjectGetProperties
drmModeObjectSetProperty drmModeObjectSetProperty

View File

@ -1823,3 +1823,52 @@ drmModeGetConnectorTypeName(uint32_t connector_type)
return NULL; return NULL;
} }
} }
drm_public int
drmModeCreateDumbBuffer(int fd, uint32_t width, uint32_t height, uint32_t bpp,
uint32_t flags, uint32_t *handle, uint32_t *pitch,
uint64_t *size)
{
int ret;
struct drm_mode_create_dumb create = {
.width = width,
.height = height,
.bpp = bpp,
.flags = flags,
};
ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
if (ret != 0)
return ret;
*handle = create.handle;
*pitch = create.pitch;
*size = create.size;
return 0;
}
drm_public int
drmModeDestroyDumbBuffer(int fd, uint32_t handle)
{
struct drm_mode_destroy_dumb destroy = {
.handle = handle,
};
return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy);
}
drm_public int
drmModeMapDumbBuffer(int fd, uint32_t handle, uint64_t *offset)
{
int ret;
struct drm_mode_map_dumb map = {
.handle = handle,
};
ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
if (ret != 0)
return ret;
*offset = map.offset;
return 0;
}

View File

@ -497,6 +497,38 @@ extern int drmModeRevokeLease(int fd, uint32_t lessee_id);
extern const char * extern const char *
drmModeGetConnectorTypeName(uint32_t connector_type); drmModeGetConnectorTypeName(uint32_t connector_type);
/**
* Create a dumb buffer.
*
* Given a width, height and bits-per-pixel, the kernel will return a buffer
* handle, pitch and size. The flags must be zero.
*
* Returns 0 on success, negative errno on error.
*/
extern int
drmModeCreateDumbBuffer(int fd, uint32_t width, uint32_t height, uint32_t bpp,
uint32_t flags, uint32_t *handle, uint32_t *pitch,
uint64_t *size);
/**
* Destroy a dumb buffer.
*
* Returns 0 on success, negative errno on error.
*/
extern int
drmModeDestroyDumbBuffer(int fd, uint32_t handle);
/**
* Prepare a dumb buffer for mapping.
*
* The kernel returns an offset which can be used as an argument to mmap(2) on
* the DRM FD.
*
* Returns 0 on success, negative errno on error.
*/
extern int
drmModeMapDumbBuffer(int fd, uint32_t handle, uint64_t *offset);
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif