#include <stdlib.h>
#include <stdint.h>
#include <string.h>
+#include <sys/ioctl.h>
+#include "drm.h"
#include "drm_fourcc.h"
-#include "libkms.h"
+
+#include "libdrm.h"
+#include "xf86drm.h"
#include "buffers.h"
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+struct bo
+{
+ int fd;
+ void *ptr;
+ size_t size;
+ size_t offset;
+ size_t pitch;
+ unsigned handle;
+};
+
/* -----------------------------------------------------------------------------
* Formats
*/
* Buffers management
*/
-static struct kms_bo *
-allocate_buffer(struct kms_driver *kms, unsigned int width, unsigned int height,
- unsigned int *stride)
+static struct bo *
+bo_create_dumb(int fd, unsigned int width, unsigned int height, unsigned int bpp)
{
- struct kms_bo *bo;
- unsigned bo_attribs[] = {
- KMS_WIDTH, 0,
- KMS_HEIGHT, 0,
- KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
- KMS_TERMINATE_PROP_LIST
- };
+ struct drm_mode_create_dumb arg;
+ struct bo *bo;
int ret;
- bo_attribs[1] = width;
- bo_attribs[3] = height;
-
- ret = kms_bo_create(kms, bo_attribs, &bo);
- if (ret) {
- fprintf(stderr, "failed to alloc buffer: %s\n",
- strerror(-ret));
+ bo = malloc(sizeof(*bo));
+ if (bo == NULL) {
+ fprintf(stderr, "failed to allocate buffer object\n");
return NULL;
}
- ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
+ memset(&arg, 0, sizeof(arg));
+ arg.bpp = bpp;
+ arg.width = width;
+ arg.height = height;
+
+ ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg);
if (ret) {
- fprintf(stderr, "failed to retreive buffer stride: %s\n",
- strerror(-ret));
- kms_bo_destroy(&bo);
+ fprintf(stderr, "failed to create dumb buffer: %s\n",
+ strerror(errno));
+ free(bo);
return NULL;
}
+ bo->fd = fd;
+ bo->handle = arg.handle;
+ bo->size = arg.size;
+ bo->pitch = arg.pitch;
+
return bo;
}
-struct kms_bo *
-create_test_buffer(struct kms_driver *kms, unsigned int format,
- unsigned int width, unsigned int height,
- unsigned int handles[4], unsigned int pitches[4],
- unsigned int offsets[4], enum fill_pattern pattern)
+static int bo_map(struct bo *bo, void **out)
+{
+ struct drm_mode_map_dumb arg;
+ void *map;
+ int ret;
+
+ memset(&arg, 0, sizeof(arg));
+ arg.handle = bo->handle;
+
+ ret = drmIoctl(bo->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
+ if (ret)
+ return ret;
+
+ map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
+ bo->fd, arg.offset);
+ if (map == MAP_FAILED)
+ return -EINVAL;
+
+ bo->ptr = map;
+ *out = map;
+
+ return 0;
+}
+
+static void bo_unmap(struct bo *bo)
+{
+ if (!bo->ptr)
+ return;
+
+ drm_munmap(bo->ptr, bo->size);
+ bo->ptr = NULL;
+}
+
+struct bo *
+bo_create(int fd, unsigned int format,
+ unsigned int width, unsigned int height,
+ unsigned int handles[4], unsigned int pitches[4],
+ unsigned int offsets[4], enum fill_pattern pattern)
{
unsigned int virtual_height;
- struct kms_bo *bo;
+ struct bo *bo;
+ unsigned int bpp;
void *planes[3] = { 0, };
void *virtual;
int ret;
switch (format) {
case DRM_FORMAT_NV12:
case DRM_FORMAT_NV21:
+ case DRM_FORMAT_NV16:
+ case DRM_FORMAT_NV61:
+ case DRM_FORMAT_YUV420:
+ case DRM_FORMAT_YVU420:
+ bpp = 8;
+ break;
+
+ case DRM_FORMAT_ARGB4444:
+ case DRM_FORMAT_XRGB4444:
+ case DRM_FORMAT_ABGR4444:
+ case DRM_FORMAT_XBGR4444:
+ case DRM_FORMAT_RGBA4444:
+ case DRM_FORMAT_RGBX4444:
+ case DRM_FORMAT_BGRA4444:
+ case DRM_FORMAT_BGRX4444:
+ case DRM_FORMAT_ARGB1555:
+ case DRM_FORMAT_XRGB1555:
+ case DRM_FORMAT_ABGR1555:
+ case DRM_FORMAT_XBGR1555:
+ case DRM_FORMAT_RGBA5551:
+ case DRM_FORMAT_RGBX5551:
+ case DRM_FORMAT_BGRA5551:
+ case DRM_FORMAT_BGRX5551:
+ case DRM_FORMAT_RGB565:
+ case DRM_FORMAT_BGR565:
+ case DRM_FORMAT_UYVY:
+ case DRM_FORMAT_VYUY:
+ case DRM_FORMAT_YUYV:
+ case DRM_FORMAT_YVYU:
+ bpp = 16;
+ break;
+
+ case DRM_FORMAT_BGR888:
+ case DRM_FORMAT_RGB888:
+ bpp = 24;
+ break;
+
+ case DRM_FORMAT_ARGB8888:
+ case DRM_FORMAT_XRGB8888:
+ case DRM_FORMAT_ABGR8888:
+ case DRM_FORMAT_XBGR8888:
+ case DRM_FORMAT_RGBA8888:
+ case DRM_FORMAT_RGBX8888:
+ case DRM_FORMAT_BGRA8888:
+ case DRM_FORMAT_BGRX8888:
+ case DRM_FORMAT_ARGB2101010:
+ case DRM_FORMAT_XRGB2101010:
+ case DRM_FORMAT_ABGR2101010:
+ case DRM_FORMAT_XBGR2101010:
+ case DRM_FORMAT_RGBA1010102:
+ case DRM_FORMAT_RGBX1010102:
+ case DRM_FORMAT_BGRA1010102:
+ case DRM_FORMAT_BGRX1010102:
+ bpp = 32;
+ break;
+
+ default:
+ fprintf(stderr, "unsupported format 0x%08x\n", format);
+ return NULL;
+ }
+
+ switch (format) {
+ case DRM_FORMAT_NV12:
+ case DRM_FORMAT_NV21:
virtual_height = height * 3 / 2;
break;
break;
}
- bo = allocate_buffer(kms, width, virtual_height, &pitches[0]);
+ bo = bo_create_dumb(fd, width, virtual_height, bpp);
if (!bo)
return NULL;
- ret = kms_bo_map(bo, &virtual);
+ ret = bo_map(bo, &virtual);
if (ret) {
fprintf(stderr, "failed to map buffer: %s\n",
- strerror(-ret));
- kms_bo_destroy(&bo);
+ strerror(-errno));
+ bo_destroy(bo);
return NULL;
}
case DRM_FORMAT_YUYV:
case DRM_FORMAT_YVYU:
offsets[0] = 0;
- kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
- kms_bo_get_prop(bo, KMS_PITCH, &pitches[0]);
+ handles[0] = bo->handle;
+ pitches[0] = bo->pitch;
planes[0] = virtual;
break;
case DRM_FORMAT_NV16:
case DRM_FORMAT_NV61:
offsets[0] = 0;
- kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
- kms_bo_get_prop(bo, KMS_PITCH, &pitches[0]);
+ handles[0] = bo->handle;
+ pitches[0] = bo->pitch;
pitches[1] = pitches[0];
offsets[1] = pitches[0] * height;
- kms_bo_get_prop(bo, KMS_HANDLE, &handles[1]);
+ handles[1] = bo->handle;
planes[0] = virtual;
planes[1] = virtual + offsets[1];
case DRM_FORMAT_YUV420:
case DRM_FORMAT_YVU420:
offsets[0] = 0;
- kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
- kms_bo_get_prop(bo, KMS_PITCH, &pitches[0]);
+ handles[0] = bo->handle;
+ pitches[0] = bo->pitch;
pitches[1] = pitches[0] / 2;
offsets[1] = pitches[0] * height;
- kms_bo_get_prop(bo, KMS_HANDLE, &handles[1]);
+ handles[1] = bo->handle;
pitches[2] = pitches[1];
offsets[2] = offsets[1] + pitches[1] * height / 2;
- kms_bo_get_prop(bo, KMS_HANDLE, &handles[2]);
+ handles[2] = bo->handle;
planes[0] = virtual;
planes[1] = virtual + offsets[1];
case DRM_FORMAT_BGRA1010102:
case DRM_FORMAT_BGRX1010102:
offsets[0] = 0;
- kms_bo_get_prop(bo, KMS_HANDLE, &handles[0]);
- kms_bo_get_prop(bo, KMS_PITCH, &pitches[0]);
+ handles[0] = bo->handle;
+ pitches[0] = bo->pitch;
planes[0] = virtual;
break;
}
fill_pattern(format, pattern, planes, width, height, pitches[0]);
- kms_bo_unmap(bo);
+ bo_unmap(bo);
return bo;
}
+
+void bo_destroy(struct bo *bo)
+{
+ struct drm_mode_destroy_dumb arg;
+ int ret;
+
+ memset(&arg, 0, sizeof(arg));
+ arg.handle = bo->handle;
+
+ ret = drmIoctl(bo->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
+ if (ret)
+ fprintf(stderr, "failed to destroy dumb buffer: %s\n",
+ strerror(errno));
+
+ free(bo);
+}
#include "xf86drm.h"
#include "xf86drmMode.h"
#include "drm_fourcc.h"
-#include "libkms.h"
#include "buffers.h"
#include "cursor.h"
int fd;
struct resources *resources;
- struct kms_driver *kms;
struct {
unsigned int width;
unsigned int height;
unsigned int fb_id;
- struct kms_bo *bo;
+ struct bo *bo;
} mode;
};
drmModePlane *ovr;
uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
uint32_t plane_id = 0;
- struct kms_bo *plane_bo;
+ struct bo *plane_bo;
uint32_t plane_flags = 0;
int crtc_x, crtc_y, crtc_w, crtc_h;
struct crtc *crtc = NULL;
fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
p->w, p->h, p->format_str, plane_id);
- plane_bo = create_test_buffer(dev->kms, p->fourcc, p->w, p->h, handles,
- pitches, offsets, PATTERN_TILES);
+ plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles,
+ pitches, offsets, PATTERN_TILES);
if (plane_bo == NULL)
return -1;
{
uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
unsigned int fb_id;
- struct kms_bo *bo;
+ struct bo *bo;
unsigned int i;
unsigned int j;
int ret, x;
dev->mode.height = pipe->mode->vdisplay;
}
- bo = create_test_buffer(dev->kms, pipes[0].fourcc,
- dev->mode.width, dev->mode.height,
- handles, pitches, offsets, PATTERN_SMPTE);
+ bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width, dev->mode.height,
+ handles, pitches, offsets, PATTERN_SMPTE);
if (bo == NULL)
return;
static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
{
uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
- struct kms_bo *bo;
+ struct bo *bo;
unsigned int i;
int ret;
/* create cursor bo.. just using PATTERN_PLAIN as it has
* translucent alpha
*/
- bo = create_test_buffer(dev->kms, DRM_FORMAT_ARGB8888,
- cw, ch, handles, pitches, offsets, PATTERN_PLAIN);
+ bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches,
+ offsets, PATTERN_PLAIN);
if (bo == NULL)
return;
{
uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
unsigned int other_fb_id;
- struct kms_bo *other_bo;
+ struct bo *other_bo;
drmEventContext evctx;
unsigned int i;
int ret;
- other_bo = create_test_buffer(dev->kms, pipes[0].fourcc,
- dev->mode.width, dev->mode.height,
- handles, pitches, offsets, PATTERN_PLAIN);
+ other_bo = bo_create(dev->fd, pipes[0].fourcc,
+ dev->mode.width, dev->mode.height,
+ handles, pitches, offsets, PATTERN_PLAIN);
if (other_bo == NULL)
return;
drmHandleEvent(dev->fd, &evctx);
}
- kms_bo_destroy(&other_bo);
+ bo_destroy(other_bo);
}
#define min(a, b) ((a) < (b) ? (a) : (b))
set_property(&dev, &prop_args[i]);
if (count || plane_count) {
- ret = kms_create(dev.fd, &dev.kms);
- if (ret) {
- fprintf(stderr, "failed to create kms driver: %s\n",
- strerror(-ret));
+ uint64_t cap = 0;
+
+ ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap);
+ if (ret || cap == 0) {
+ fprintf(stderr, "driver doesn't support the dumb buffer API\n");
return 1;
}
if (test_cursor)
clear_cursors(&dev);
- kms_bo_destroy(&dev.mode.bo);
- kms_destroy(&dev.kms);
+ bo_destroy(dev.mode.bo);
}
free_resources(dev.resources);