xf86drmMode: add helpers for dumb buffers
authorSimon Ser <contact@emersion.fr>
Thu, 27 Oct 2022 17:11:20 +0000 (19:11 +0200)
committerSimon Ser <contact@emersion.fr>
Thu, 27 Oct 2022 20:18:22 +0000 (22:18 +0200)
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>
core-symbols.txt
xf86drmMode.c
xf86drmMode.h

index 6c5a4af..9f8a323 100644 (file)
@@ -105,10 +105,12 @@ drmModeAtomicSetCursor
 drmModeAttachMode
 drmModeConnectorGetPossibleCrtcs
 drmModeConnectorSetProperty
+drmModeCreateDumbBuffer
 drmModeCreateLease
 drmModeCreatePropertyBlob
 drmModeCrtcGetGamma
 drmModeCrtcSetGamma
+drmModeDestroyDumbBuffer
 drmModeDestroyPropertyBlob
 drmModeDetachMode
 drmModeDirtyFB
@@ -139,6 +141,7 @@ drmModeGetProperty
 drmModeGetPropertyBlob
 drmModeGetResources
 drmModeListLessees
+drmModeMapDumbBuffer
 drmModeMoveCursor
 drmModeObjectGetProperties
 drmModeObjectSetProperty
index 9dc4245..22a8a7c 100644 (file)
@@ -1823,3 +1823,52 @@ drmModeGetConnectorTypeName(uint32_t connector_type)
                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;
+}
index 4617d1e..d911c9a 100644 (file)
@@ -497,6 +497,38 @@ extern int drmModeRevokeLease(int fd, uint32_t lessee_id);
 extern const char *
 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)
 }
 #endif