2019-05-20 16:08:35 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Intel Corporation.
|
|
|
|
* Copyright (C) 2016 Red Hat
|
|
|
|
* Copyright (C) 2018 DisplayLink (UK) Ltd.
|
|
|
|
* Copyright (C) 2018 Canonical Ltd.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
* 02111-1307, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "backends/native/meta-drm-buffer-dumb.h"
|
|
|
|
|
2021-05-05 04:06:20 -04:00
|
|
|
#include <drm_fourcc.h>
|
2021-04-12 10:13:29 -04:00
|
|
|
#include <gio/gio.h>
|
2020-09-29 03:40:53 -04:00
|
|
|
#include <xf86drm.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2021-04-12 10:13:29 -04:00
|
|
|
#include "backends/native/meta-device-pool.h"
|
2020-09-29 03:40:53 -04:00
|
|
|
|
2019-05-20 16:08:35 -04:00
|
|
|
struct _MetaDrmBufferDumb
|
|
|
|
{
|
|
|
|
MetaDrmBuffer parent;
|
|
|
|
|
2020-09-29 03:40:53 -04:00
|
|
|
uint32_t handle;
|
|
|
|
void *map;
|
|
|
|
uint64_t map_size;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
int stride_bytes;
|
|
|
|
uint32_t drm_format;
|
|
|
|
int dmabuf_fd;
|
2021-05-05 04:06:20 -04:00
|
|
|
int offset;
|
2019-05-20 16:08:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (MetaDrmBufferDumb, meta_drm_buffer_dumb, META_TYPE_DRM_BUFFER)
|
|
|
|
|
2021-05-05 05:19:13 -04:00
|
|
|
static int
|
|
|
|
meta_drm_buffer_dumb_export_fd (MetaDrmBuffer *buffer,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
|
|
|
"Can't export fd for dumb buffer");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-09-29 03:40:53 -04:00
|
|
|
static int
|
|
|
|
meta_drm_buffer_dumb_get_width (MetaDrmBuffer *buffer)
|
|
|
|
{
|
|
|
|
MetaDrmBufferDumb *buffer_dumb = META_DRM_BUFFER_DUMB (buffer);
|
|
|
|
|
|
|
|
return buffer_dumb->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
meta_drm_buffer_dumb_get_height (MetaDrmBuffer *buffer)
|
|
|
|
{
|
|
|
|
MetaDrmBufferDumb *buffer_dumb = META_DRM_BUFFER_DUMB (buffer);
|
|
|
|
|
|
|
|
return buffer_dumb->height;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
meta_drm_buffer_dumb_get_stride (MetaDrmBuffer *buffer)
|
|
|
|
{
|
|
|
|
MetaDrmBufferDumb *buffer_dumb = META_DRM_BUFFER_DUMB (buffer);
|
|
|
|
|
|
|
|
return buffer_dumb->stride_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
meta_drm_buffer_dumb_get_format (MetaDrmBuffer *buffer)
|
|
|
|
{
|
|
|
|
MetaDrmBufferDumb *buffer_dumb = META_DRM_BUFFER_DUMB (buffer);
|
|
|
|
|
|
|
|
return buffer_dumb->drm_format;
|
|
|
|
}
|
|
|
|
|
2021-05-05 04:06:20 -04:00
|
|
|
static int
|
|
|
|
meta_drm_buffer_dumb_get_bpp (MetaDrmBuffer *buffer)
|
|
|
|
{
|
|
|
|
MetaDrmBufferDumb *buffer_dumb = META_DRM_BUFFER_DUMB (buffer);
|
|
|
|
|
|
|
|
switch (buffer_dumb->drm_format)
|
|
|
|
{
|
|
|
|
case DRM_FORMAT_C8:
|
|
|
|
case DRM_FORMAT_R8:
|
|
|
|
case DRM_FORMAT_RGB332:
|
|
|
|
case DRM_FORMAT_BGR233:
|
|
|
|
return 8;
|
|
|
|
case DRM_FORMAT_GR88:
|
|
|
|
case DRM_FORMAT_XRGB4444:
|
|
|
|
case DRM_FORMAT_XBGR4444:
|
|
|
|
case DRM_FORMAT_RGBX4444:
|
|
|
|
case DRM_FORMAT_BGRX4444:
|
|
|
|
case DRM_FORMAT_ARGB4444:
|
|
|
|
case DRM_FORMAT_ABGR4444:
|
|
|
|
case DRM_FORMAT_RGBA4444:
|
|
|
|
case DRM_FORMAT_BGRA4444:
|
|
|
|
case DRM_FORMAT_XRGB1555:
|
|
|
|
case DRM_FORMAT_XBGR1555:
|
|
|
|
case DRM_FORMAT_RGBX5551:
|
|
|
|
case DRM_FORMAT_BGRX5551:
|
|
|
|
case DRM_FORMAT_ARGB1555:
|
|
|
|
case DRM_FORMAT_ABGR1555:
|
|
|
|
case DRM_FORMAT_RGBA5551:
|
|
|
|
case DRM_FORMAT_BGRA5551:
|
|
|
|
case DRM_FORMAT_RGB565:
|
|
|
|
case DRM_FORMAT_BGR565:
|
|
|
|
return 16;
|
|
|
|
case DRM_FORMAT_RGB888:
|
|
|
|
case DRM_FORMAT_BGR888:
|
|
|
|
return 24;
|
|
|
|
case DRM_FORMAT_XRGB8888:
|
|
|
|
case DRM_FORMAT_XBGR8888:
|
|
|
|
case DRM_FORMAT_RGBX8888:
|
|
|
|
case DRM_FORMAT_BGRX8888:
|
|
|
|
case DRM_FORMAT_ARGB8888:
|
|
|
|
case DRM_FORMAT_ABGR8888:
|
|
|
|
case DRM_FORMAT_RGBA8888:
|
|
|
|
case DRM_FORMAT_BGRA8888:
|
|
|
|
case DRM_FORMAT_XRGB2101010:
|
|
|
|
case DRM_FORMAT_XBGR2101010:
|
|
|
|
case DRM_FORMAT_RGBX1010102:
|
|
|
|
case DRM_FORMAT_BGRX1010102:
|
|
|
|
case DRM_FORMAT_ARGB2101010:
|
|
|
|
case DRM_FORMAT_ABGR2101010:
|
|
|
|
case DRM_FORMAT_RGBA1010102:
|
|
|
|
case DRM_FORMAT_BGRA1010102:
|
|
|
|
return 32;
|
|
|
|
case DRM_FORMAT_XBGR16161616F:
|
|
|
|
case DRM_FORMAT_ABGR16161616F:
|
|
|
|
return 64;
|
|
|
|
default:
|
|
|
|
g_warn_if_reached ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
meta_drm_buffer_dumb_get_offset (MetaDrmBuffer *buffer,
|
|
|
|
int plane)
|
|
|
|
{
|
|
|
|
MetaDrmBufferDumb *buffer_dumb = META_DRM_BUFFER_DUMB (buffer);
|
|
|
|
|
|
|
|
g_warn_if_fail (plane == 0);
|
|
|
|
|
|
|
|
return buffer_dumb->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
meta_drm_buffer_dumb_get_modifier (MetaDrmBuffer *buffer)
|
|
|
|
{
|
|
|
|
return DRM_FORMAT_MOD_LINEAR;
|
|
|
|
}
|
|
|
|
|
2021-04-12 10:13:29 -04:00
|
|
|
static int
|
|
|
|
handle_to_dmabuf_fd (MetaDrmBufferDumb *buffer_dumb,
|
|
|
|
GError **error)
|
2020-09-29 03:40:53 -04:00
|
|
|
{
|
|
|
|
MetaDrmBuffer *buffer = META_DRM_BUFFER (buffer_dumb);
|
2021-04-12 10:13:29 -04:00
|
|
|
MetaDeviceFile *device_file;
|
2020-09-29 03:40:53 -04:00
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
int dmabuf_fd;
|
|
|
|
|
2021-04-12 10:13:29 -04:00
|
|
|
device_file = meta_drm_buffer_get_device_file (buffer);
|
|
|
|
fd = meta_device_file_get_fd (device_file);
|
2020-09-29 03:40:53 -04:00
|
|
|
|
|
|
|
ret = drmPrimeHandleToFD (fd, buffer_dumb->handle, DRM_CLOEXEC,
|
|
|
|
&dmabuf_fd);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_IO_ERROR, g_io_error_from_errno (-ret),
|
|
|
|
"drmPrimeHandleToFd: %s", g_strerror (-ret));
|
2021-04-12 10:13:29 -04:00
|
|
|
return -1;
|
2020-09-29 03:40:53 -04:00
|
|
|
}
|
|
|
|
|
2021-04-12 10:13:29 -04:00
|
|
|
return dmabuf_fd;
|
2020-09-29 03:40:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
meta_drm_buffer_dumb_ensure_dmabuf_fd (MetaDrmBufferDumb *buffer_dumb,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
if (buffer_dumb->dmabuf_fd != -1)
|
|
|
|
return buffer_dumb->dmabuf_fd;
|
|
|
|
|
2021-04-12 10:13:29 -04:00
|
|
|
buffer_dumb->dmabuf_fd = handle_to_dmabuf_fd (buffer_dumb, error);
|
2020-09-29 03:40:53 -04:00
|
|
|
return buffer_dumb->dmabuf_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
meta_drm_buffer_dumb_get_data (MetaDrmBufferDumb *buffer_dumb)
|
|
|
|
{
|
|
|
|
return buffer_dumb->map;
|
|
|
|
}
|
|
|
|
|
2021-04-12 10:13:29 -04:00
|
|
|
static gboolean
|
|
|
|
init_dumb_buffer (MetaDrmBufferDumb *buffer_dumb,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
uint32_t format,
|
|
|
|
GError **error)
|
2020-09-29 03:40:53 -04:00
|
|
|
{
|
|
|
|
MetaDrmBuffer *buffer = META_DRM_BUFFER (buffer_dumb);
|
2021-04-12 10:13:29 -04:00
|
|
|
MetaDeviceFile *device_file;
|
2020-09-29 03:40:53 -04:00
|
|
|
int fd;
|
|
|
|
struct drm_mode_create_dumb create_arg;
|
|
|
|
struct drm_mode_destroy_dumb destroy_arg;
|
|
|
|
struct drm_mode_map_dumb map_arg;
|
|
|
|
void *map;
|
|
|
|
MetaDrmFbArgs fb_args;
|
|
|
|
|
2021-04-12 10:13:29 -04:00
|
|
|
device_file = meta_drm_buffer_get_device_file (buffer);
|
|
|
|
fd = meta_device_file_get_fd (device_file);
|
2020-09-29 03:40:53 -04:00
|
|
|
|
|
|
|
create_arg = (struct drm_mode_create_dumb) {
|
|
|
|
.bpp = 32, /* RGBX8888 */
|
2021-04-12 10:13:29 -04:00
|
|
|
.width = width,
|
|
|
|
.height = height
|
2020-09-29 03:40:53 -04:00
|
|
|
};
|
|
|
|
if (drmIoctl (fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg) != 0)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_IO_ERROR,
|
|
|
|
G_IO_ERROR_FAILED,
|
|
|
|
"Failed to create dumb drm buffer: %s",
|
|
|
|
g_strerror (errno));
|
|
|
|
goto err_ioctl;
|
|
|
|
}
|
|
|
|
|
|
|
|
fb_args = (MetaDrmFbArgs) {
|
2021-04-12 10:13:29 -04:00
|
|
|
.width = width,
|
|
|
|
.height = height,
|
|
|
|
.format = format,
|
2020-09-29 03:40:53 -04:00
|
|
|
.handles = { create_arg.handle },
|
|
|
|
.strides = { create_arg.pitch },
|
|
|
|
};
|
2021-05-05 08:35:48 -04:00
|
|
|
if (!meta_drm_buffer_do_ensure_fb_id (buffer, &fb_args, error))
|
2020-09-29 03:40:53 -04:00
|
|
|
goto err_add_fb;
|
|
|
|
|
|
|
|
map_arg = (struct drm_mode_map_dumb) {
|
|
|
|
.handle = create_arg.handle
|
|
|
|
};
|
|
|
|
if (drmIoctl (fd, DRM_IOCTL_MODE_MAP_DUMB,
|
|
|
|
&map_arg) != 0)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_IO_ERROR,
|
|
|
|
G_IO_ERROR_FAILED,
|
|
|
|
"Failed to map dumb drm buffer: %s",
|
|
|
|
g_strerror (errno));
|
|
|
|
goto err_map_dumb;
|
|
|
|
}
|
|
|
|
|
|
|
|
map = mmap (NULL, create_arg.size, PROT_WRITE, MAP_SHARED,
|
|
|
|
fd, map_arg.offset);
|
|
|
|
if (map == MAP_FAILED)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_IO_ERROR,
|
|
|
|
G_IO_ERROR_FAILED,
|
|
|
|
"Failed to mmap dumb drm buffer memory: %s",
|
|
|
|
g_strerror (errno));
|
|
|
|
goto err_mmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_dumb->handle = create_arg.handle;
|
|
|
|
buffer_dumb->map = map;
|
|
|
|
buffer_dumb->map_size = create_arg.size;
|
2021-04-12 10:13:29 -04:00
|
|
|
buffer_dumb->width = width;
|
|
|
|
buffer_dumb->height = height;
|
2020-09-29 03:40:53 -04:00
|
|
|
buffer_dumb->stride_bytes = create_arg.pitch;
|
2021-04-12 10:13:29 -04:00
|
|
|
buffer_dumb->drm_format = format;
|
2021-05-05 04:06:20 -04:00
|
|
|
buffer_dumb->offset = map_arg.offset;
|
2020-09-29 03:40:53 -04:00
|
|
|
|
2021-07-16 11:06:05 -04:00
|
|
|
return TRUE;
|
2020-09-29 03:40:53 -04:00
|
|
|
|
|
|
|
err_mmap:
|
|
|
|
err_map_dumb:
|
|
|
|
err_add_fb:
|
|
|
|
destroy_arg = (struct drm_mode_destroy_dumb) {
|
|
|
|
.handle = create_arg.handle
|
|
|
|
};
|
|
|
|
drmIoctl (fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
|
|
|
|
|
|
|
|
err_ioctl:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-05-20 16:08:35 -04:00
|
|
|
MetaDrmBufferDumb *
|
2021-04-12 10:13:29 -04:00
|
|
|
meta_drm_buffer_dumb_new (MetaDeviceFile *device_file,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
uint32_t format,
|
|
|
|
GError **error)
|
2019-05-20 16:08:35 -04:00
|
|
|
{
|
|
|
|
MetaDrmBufferDumb *buffer_dumb;
|
2020-09-29 03:40:53 -04:00
|
|
|
|
|
|
|
buffer_dumb = g_object_new (META_TYPE_DRM_BUFFER_DUMB,
|
2021-04-12 10:13:29 -04:00
|
|
|
"device-file", device_file,
|
2021-05-05 04:15:18 -04:00
|
|
|
"flags", META_DRM_BUFFER_FLAG_DISABLE_MODIFIERS,
|
2020-09-29 03:40:53 -04:00
|
|
|
NULL);
|
|
|
|
|
2021-04-12 10:13:29 -04:00
|
|
|
if (!init_dumb_buffer (buffer_dumb, width, height, format, error))
|
2020-09-29 03:40:53 -04:00
|
|
|
{
|
|
|
|
g_object_unref (buffer_dumb);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-20 16:08:35 -04:00
|
|
|
|
|
|
|
return buffer_dumb;
|
|
|
|
}
|
|
|
|
|
2021-04-12 10:13:29 -04:00
|
|
|
static void
|
|
|
|
destroy_dumb_buffer (MetaDrmBufferDumb *buffer_dumb)
|
2019-05-20 16:08:35 -04:00
|
|
|
{
|
2020-09-29 03:40:53 -04:00
|
|
|
MetaDrmBuffer *buffer = META_DRM_BUFFER (buffer_dumb);
|
2021-04-12 10:13:29 -04:00
|
|
|
MetaDeviceFile *device_file;
|
2020-09-29 03:40:53 -04:00
|
|
|
int fd;
|
|
|
|
struct drm_mode_destroy_dumb destroy_arg;
|
|
|
|
|
2021-04-12 10:13:29 -04:00
|
|
|
device_file = meta_drm_buffer_get_device_file (buffer);
|
|
|
|
fd = meta_device_file_get_fd (device_file);
|
2020-09-29 03:40:53 -04:00
|
|
|
|
|
|
|
munmap (buffer_dumb->map, buffer_dumb->map_size);
|
|
|
|
|
|
|
|
destroy_arg = (struct drm_mode_destroy_dumb) {
|
|
|
|
.handle = buffer_dumb->handle
|
|
|
|
};
|
|
|
|
drmIoctl (fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
|
|
|
|
|
|
|
|
if (buffer_dumb->dmabuf_fd != -1)
|
|
|
|
close (buffer_dumb->dmabuf_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
meta_drm_buffer_dumb_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
MetaDrmBufferDumb *buffer_dumb = META_DRM_BUFFER_DUMB (object);
|
|
|
|
|
|
|
|
if (buffer_dumb->handle)
|
2021-04-12 10:13:29 -04:00
|
|
|
destroy_dumb_buffer (buffer_dumb);
|
2020-09-29 03:40:53 -04:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (meta_drm_buffer_dumb_parent_class)->finalize (object);
|
2019-05-20 16:08:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
meta_drm_buffer_dumb_init (MetaDrmBufferDumb *buffer_dumb)
|
|
|
|
{
|
2020-09-29 03:40:53 -04:00
|
|
|
buffer_dumb->dmabuf_fd = -1;
|
2019-05-20 16:08:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
meta_drm_buffer_dumb_class_init (MetaDrmBufferDumbClass *klass)
|
|
|
|
{
|
2020-09-29 03:40:53 -04:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2019-05-20 16:08:35 -04:00
|
|
|
MetaDrmBufferClass *buffer_class = META_DRM_BUFFER_CLASS (klass);
|
|
|
|
|
2020-09-29 03:40:53 -04:00
|
|
|
object_class->finalize = meta_drm_buffer_dumb_finalize;
|
|
|
|
|
2021-05-05 05:19:13 -04:00
|
|
|
buffer_class->export_fd = meta_drm_buffer_dumb_export_fd;
|
2020-09-29 03:40:53 -04:00
|
|
|
buffer_class->get_width = meta_drm_buffer_dumb_get_width;
|
|
|
|
buffer_class->get_height = meta_drm_buffer_dumb_get_height;
|
|
|
|
buffer_class->get_stride = meta_drm_buffer_dumb_get_stride;
|
2021-05-05 04:06:20 -04:00
|
|
|
buffer_class->get_bpp = meta_drm_buffer_dumb_get_bpp;
|
2020-09-29 03:40:53 -04:00
|
|
|
buffer_class->get_format = meta_drm_buffer_dumb_get_format;
|
2021-05-05 04:06:20 -04:00
|
|
|
buffer_class->get_offset = meta_drm_buffer_dumb_get_offset;
|
|
|
|
buffer_class->get_modifier = meta_drm_buffer_dumb_get_modifier;
|
2019-05-20 16:08:35 -04:00
|
|
|
}
|