2012-07-20 08:37:00 -06:00
|
|
|
/*
|
|
|
|
* DRM based mode setting test program
|
|
|
|
* Copyright 2008 Tungsten Graphics
|
|
|
|
* Jakob Bornecrantz <jakob@tungstengraphics.com>
|
|
|
|
* Copyright 2008 Intel Corporation
|
|
|
|
* Jesse Barnes <jesse.barnes@intel.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2014-12-09 13:00:58 -07:00
|
|
|
#include <sys/ioctl.h>
|
2012-07-20 08:37:00 -06:00
|
|
|
|
2014-12-09 13:00:58 -07:00
|
|
|
#include "drm.h"
|
2012-07-20 08:37:00 -06:00
|
|
|
#include "drm_fourcc.h"
|
2014-12-09 13:00:58 -07:00
|
|
|
|
2015-04-05 08:51:59 -06:00
|
|
|
#include "libdrm_macros.h"
|
2014-12-09 13:00:58 -07:00
|
|
|
#include "xf86drm.h"
|
2022-10-27 11:13:06 -06:00
|
|
|
#include "xf86drmMode.h"
|
2012-07-20 08:37:00 -06:00
|
|
|
|
|
|
|
#include "buffers.h"
|
|
|
|
|
2014-12-09 13:00:58 -07:00
|
|
|
struct bo
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
void *ptr;
|
2022-11-02 11:43:58 -06:00
|
|
|
uint64_t size;
|
|
|
|
uint32_t pitch;
|
|
|
|
uint32_t handle;
|
2014-12-09 13:00:58 -07:00
|
|
|
};
|
|
|
|
|
2012-07-20 08:37:00 -06:00
|
|
|
/* -----------------------------------------------------------------------------
|
|
|
|
* Buffers management
|
|
|
|
*/
|
|
|
|
|
2014-12-09 13:00:58 -07:00
|
|
|
static struct bo *
|
|
|
|
bo_create_dumb(int fd, unsigned int width, unsigned int height, unsigned int bpp)
|
2012-07-20 08:37:00 -06:00
|
|
|
{
|
2014-12-09 13:00:58 -07:00
|
|
|
struct bo *bo;
|
2012-07-20 08:37:00 -06:00
|
|
|
int ret;
|
|
|
|
|
2015-04-28 06:25:24 -06:00
|
|
|
bo = calloc(1, sizeof(*bo));
|
2014-12-09 13:00:58 -07:00
|
|
|
if (bo == NULL) {
|
|
|
|
fprintf(stderr, "failed to allocate buffer object\n");
|
2012-07-20 08:37:00 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-10-27 11:13:06 -06:00
|
|
|
ret = drmModeCreateDumbBuffer(fd, width, height, bpp, 0, &bo->handle,
|
|
|
|
&bo->pitch, &bo->size);
|
2012-07-20 08:37:00 -06:00
|
|
|
if (ret) {
|
2014-12-09 13:00:58 -07:00
|
|
|
fprintf(stderr, "failed to create dumb buffer: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
free(bo);
|
2012-07-20 08:37:00 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-09 13:00:58 -07:00
|
|
|
bo->fd = fd;
|
|
|
|
|
2012-07-20 08:37:00 -06:00
|
|
|
return bo;
|
|
|
|
}
|
|
|
|
|
2014-12-09 13:00:58 -07:00
|
|
|
static int bo_map(struct bo *bo, void **out)
|
|
|
|
{
|
|
|
|
void *map;
|
|
|
|
int ret;
|
2022-10-27 11:13:06 -06:00
|
|
|
uint64_t offset;
|
2014-12-09 13:00:58 -07:00
|
|
|
|
2022-10-27 11:13:06 -06:00
|
|
|
ret = drmModeMapDumbBuffer(bo->fd, bo->handle, &offset);
|
2014-12-09 13:00:58 -07:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
|
2022-10-27 11:13:06 -06:00
|
|
|
bo->fd, offset);
|
2014-12-09 13:00:58 -07:00
|
|
|
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],
|
2015-12-09 10:37:39 -07:00
|
|
|
unsigned int offsets[4], enum util_fill_pattern pattern)
|
2012-07-20 08:37:00 -06:00
|
|
|
{
|
2013-06-14 14:57:49 -06:00
|
|
|
unsigned int virtual_height;
|
2014-12-09 13:00:58 -07:00
|
|
|
struct bo *bo;
|
|
|
|
unsigned int bpp;
|
2013-02-11 13:36:30 -07:00
|
|
|
void *planes[3] = { 0, };
|
2012-07-20 08:37:00 -06:00
|
|
|
void *virtual;
|
2013-02-11 13:36:30 -07:00
|
|
|
int ret;
|
2012-07-20 08:37:00 -06:00
|
|
|
|
2014-12-09 13:00:58 -07:00
|
|
|
switch (format) {
|
2022-03-03 10:13:07 -07:00
|
|
|
case DRM_FORMAT_C1:
|
|
|
|
bpp = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DRM_FORMAT_C2:
|
|
|
|
bpp = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DRM_FORMAT_C4:
|
|
|
|
bpp = 4;
|
|
|
|
break;
|
|
|
|
|
2017-11-17 21:52:46 -07:00
|
|
|
case DRM_FORMAT_C8:
|
2014-12-09 13:00:58 -07:00
|
|
|
case DRM_FORMAT_NV12:
|
|
|
|
case DRM_FORMAT_NV21:
|
|
|
|
case DRM_FORMAT_NV16:
|
|
|
|
case DRM_FORMAT_NV61:
|
2023-07-05 07:24:02 -06:00
|
|
|
case DRM_FORMAT_NV24:
|
|
|
|
case DRM_FORMAT_NV42:
|
2014-12-09 13:00:58 -07:00
|
|
|
case DRM_FORMAT_YUV420:
|
|
|
|
case DRM_FORMAT_YVU420:
|
|
|
|
bpp = 8;
|
|
|
|
break;
|
|
|
|
|
2023-10-25 10:15:31 -06:00
|
|
|
case DRM_FORMAT_NV15:
|
|
|
|
case DRM_FORMAT_NV20:
|
|
|
|
case DRM_FORMAT_NV30:
|
|
|
|
bpp = 10;
|
|
|
|
break;
|
|
|
|
|
2014-12-09 13:00:58 -07:00
|
|
|
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;
|
|
|
|
|
2019-06-02 15:54:38 -06:00
|
|
|
case DRM_FORMAT_XRGB16161616F:
|
|
|
|
case DRM_FORMAT_XBGR16161616F:
|
|
|
|
case DRM_FORMAT_ARGB16161616F:
|
|
|
|
case DRM_FORMAT_ABGR16161616F:
|
|
|
|
bpp = 64;
|
|
|
|
break;
|
|
|
|
|
2014-12-09 13:00:58 -07:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "unsupported format 0x%08x\n", format);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-14 14:57:49 -06:00
|
|
|
switch (format) {
|
|
|
|
case DRM_FORMAT_NV12:
|
|
|
|
case DRM_FORMAT_NV21:
|
2023-10-25 10:15:31 -06:00
|
|
|
case DRM_FORMAT_NV15:
|
2015-05-05 09:16:23 -06:00
|
|
|
case DRM_FORMAT_YUV420:
|
|
|
|
case DRM_FORMAT_YVU420:
|
2013-06-14 14:57:49 -06:00
|
|
|
virtual_height = height * 3 / 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DRM_FORMAT_NV16:
|
|
|
|
case DRM_FORMAT_NV61:
|
2023-10-25 10:15:31 -06:00
|
|
|
case DRM_FORMAT_NV20:
|
2013-06-14 14:57:49 -06:00
|
|
|
virtual_height = height * 2;
|
|
|
|
break;
|
|
|
|
|
2023-07-05 07:24:02 -06:00
|
|
|
case DRM_FORMAT_NV24:
|
|
|
|
case DRM_FORMAT_NV42:
|
2023-10-25 10:15:31 -06:00
|
|
|
case DRM_FORMAT_NV30:
|
2023-07-05 07:24:02 -06:00
|
|
|
virtual_height = height * 3;
|
|
|
|
break;
|
|
|
|
|
2013-06-14 14:57:49 -06:00
|
|
|
default:
|
|
|
|
virtual_height = height;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-09 13:00:58 -07:00
|
|
|
bo = bo_create_dumb(fd, width, virtual_height, bpp);
|
2012-07-20 08:37:00 -06:00
|
|
|
if (!bo)
|
|
|
|
return NULL;
|
|
|
|
|
2014-12-09 13:00:58 -07:00
|
|
|
ret = bo_map(bo, &virtual);
|
2012-07-20 08:37:00 -06:00
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "failed to map buffer: %s\n",
|
2014-12-09 13:00:58 -07:00
|
|
|
strerror(-errno));
|
|
|
|
bo_destroy(bo);
|
2012-07-20 08:37:00 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* just testing a limited # of formats to test single
|
|
|
|
* and multi-planar path.. would be nice to add more..
|
|
|
|
*/
|
|
|
|
switch (format) {
|
|
|
|
case DRM_FORMAT_UYVY:
|
|
|
|
case DRM_FORMAT_VYUY:
|
|
|
|
case DRM_FORMAT_YUYV:
|
|
|
|
case DRM_FORMAT_YVYU:
|
|
|
|
offsets[0] = 0;
|
2014-12-09 13:00:58 -07:00
|
|
|
handles[0] = bo->handle;
|
|
|
|
pitches[0] = bo->pitch;
|
2012-07-20 08:37:00 -06:00
|
|
|
|
|
|
|
planes[0] = virtual;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DRM_FORMAT_NV12:
|
|
|
|
case DRM_FORMAT_NV21:
|
|
|
|
case DRM_FORMAT_NV16:
|
|
|
|
case DRM_FORMAT_NV61:
|
2023-10-25 10:15:31 -06:00
|
|
|
case DRM_FORMAT_NV15:
|
|
|
|
case DRM_FORMAT_NV20:
|
2012-07-20 08:37:00 -06:00
|
|
|
offsets[0] = 0;
|
2014-12-09 13:00:58 -07:00
|
|
|
handles[0] = bo->handle;
|
|
|
|
pitches[0] = bo->pitch;
|
2013-04-17 13:18:02 -06:00
|
|
|
pitches[1] = pitches[0];
|
|
|
|
offsets[1] = pitches[0] * height;
|
2014-12-09 13:00:58 -07:00
|
|
|
handles[1] = bo->handle;
|
2012-07-20 08:37:00 -06:00
|
|
|
|
|
|
|
planes[0] = virtual;
|
|
|
|
planes[1] = virtual + offsets[1];
|
|
|
|
break;
|
|
|
|
|
2023-07-05 07:24:02 -06:00
|
|
|
case DRM_FORMAT_NV24:
|
|
|
|
case DRM_FORMAT_NV42:
|
2023-10-25 10:15:31 -06:00
|
|
|
case DRM_FORMAT_NV30:
|
2023-07-05 07:24:02 -06:00
|
|
|
offsets[0] = 0;
|
|
|
|
handles[0] = bo->handle;
|
|
|
|
pitches[0] = bo->pitch;
|
|
|
|
pitches[1] = pitches[0] * 2;
|
|
|
|
offsets[1] = pitches[0] * height;
|
|
|
|
handles[1] = bo->handle;
|
|
|
|
|
|
|
|
planes[0] = virtual;
|
|
|
|
planes[1] = virtual + offsets[1];
|
|
|
|
break;
|
|
|
|
|
2013-04-18 09:26:59 -06:00
|
|
|
case DRM_FORMAT_YUV420:
|
2012-07-20 08:37:00 -06:00
|
|
|
case DRM_FORMAT_YVU420:
|
|
|
|
offsets[0] = 0;
|
2014-12-09 13:00:58 -07:00
|
|
|
handles[0] = bo->handle;
|
|
|
|
pitches[0] = bo->pitch;
|
2013-04-17 13:18:02 -06:00
|
|
|
pitches[1] = pitches[0] / 2;
|
|
|
|
offsets[1] = pitches[0] * height;
|
2014-12-09 13:00:58 -07:00
|
|
|
handles[1] = bo->handle;
|
2013-04-17 13:18:02 -06:00
|
|
|
pitches[2] = pitches[1];
|
|
|
|
offsets[2] = offsets[1] + pitches[1] * height / 2;
|
2014-12-09 13:00:58 -07:00
|
|
|
handles[2] = bo->handle;
|
2012-07-20 08:37:00 -06:00
|
|
|
|
|
|
|
planes[0] = virtual;
|
|
|
|
planes[1] = virtual + offsets[1];
|
|
|
|
planes[2] = virtual + offsets[2];
|
|
|
|
break;
|
|
|
|
|
2022-03-03 10:13:07 -07:00
|
|
|
case DRM_FORMAT_C1:
|
|
|
|
case DRM_FORMAT_C2:
|
|
|
|
case DRM_FORMAT_C4:
|
2017-11-17 21:52:46 -07:00
|
|
|
case DRM_FORMAT_C8:
|
2013-04-17 13:18:03 -06:00
|
|
|
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:
|
2012-07-20 08:37:00 -06:00
|
|
|
case DRM_FORMAT_ARGB1555:
|
|
|
|
case DRM_FORMAT_XRGB1555:
|
2013-04-17 13:18:03 -06:00
|
|
|
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:
|
2012-07-20 08:37:00 -06:00
|
|
|
case DRM_FORMAT_BGR888:
|
|
|
|
case DRM_FORMAT_RGB888:
|
|
|
|
case DRM_FORMAT_ARGB8888:
|
|
|
|
case DRM_FORMAT_XRGB8888:
|
2013-04-17 13:18:03 -06:00
|
|
|
case DRM_FORMAT_ABGR8888:
|
|
|
|
case DRM_FORMAT_XBGR8888:
|
|
|
|
case DRM_FORMAT_RGBA8888:
|
|
|
|
case DRM_FORMAT_RGBX8888:
|
|
|
|
case DRM_FORMAT_BGRA8888:
|
2012-07-20 08:37:00 -06:00
|
|
|
case DRM_FORMAT_BGRX8888:
|
2013-04-17 13:18:03 -06:00
|
|
|
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:
|
2019-06-02 15:54:38 -06:00
|
|
|
case DRM_FORMAT_XRGB16161616F:
|
|
|
|
case DRM_FORMAT_XBGR16161616F:
|
|
|
|
case DRM_FORMAT_ARGB16161616F:
|
|
|
|
case DRM_FORMAT_ABGR16161616F:
|
2012-07-20 08:37:00 -06:00
|
|
|
offsets[0] = 0;
|
2014-12-09 13:00:58 -07:00
|
|
|
handles[0] = bo->handle;
|
|
|
|
pitches[0] = bo->pitch;
|
2012-07-20 08:37:00 -06:00
|
|
|
|
|
|
|
planes[0] = virtual;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-12-09 10:37:39 -07:00
|
|
|
util_fill_pattern(format, pattern, planes, width, height, pitches[0]);
|
2014-12-09 13:00:58 -07:00
|
|
|
bo_unmap(bo);
|
2012-07-20 08:37:00 -06:00
|
|
|
|
|
|
|
return bo;
|
|
|
|
}
|
2014-12-09 13:00:58 -07:00
|
|
|
|
|
|
|
void bo_destroy(struct bo *bo)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2022-10-27 11:13:06 -06:00
|
|
|
ret = drmModeDestroyDumbBuffer(bo->fd, bo->handle);
|
2014-12-09 13:00:58 -07:00
|
|
|
if (ret)
|
|
|
|
fprintf(stderr, "failed to destroy dumb buffer: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
free(bo);
|
|
|
|
}
|
2022-07-25 11:11:41 -06:00
|
|
|
|
|
|
|
void bo_dump(struct bo *bo, const char *filename)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if (!bo || !filename)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fp = fopen(filename, "wb");
|
|
|
|
if (fp) {
|
|
|
|
void *addr;
|
|
|
|
|
|
|
|
bo_map(bo, &addr);
|
|
|
|
printf("Dumping buffer %p to file %s.\n", bo->ptr, filename);
|
|
|
|
fwrite(bo->ptr, 1, bo->size, fp);
|
|
|
|
bo_unmap(bo);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|