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-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;
|
2019-05-20 16:08:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (MetaDrmBufferDumb, meta_drm_buffer_dumb, META_TYPE_DRM_BUFFER)
|
|
|
|
|
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-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 },
|
|
|
|
};
|
|
|
|
if (!meta_drm_buffer_ensure_fb_id (buffer, FALSE, &fb_args, error))
|
|
|
|
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;
|
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,
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
buffer_class->get_format = meta_drm_buffer_dumb_get_format;
|
2019-05-20 16:08:35 -04:00
|
|
|
}
|