2018-01-30 04:47:17 -05: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.
|
|
|
|
*
|
|
|
|
* Author: Daniel van Vugt <daniel.van.vugt@canonical.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
#include "backends/native/meta-drm-buffer.h"
|
2019-05-20 15:01:38 -04:00
|
|
|
|
2018-01-30 04:47:17 -05:00
|
|
|
#include <drm_fourcc.h>
|
|
|
|
#include <errno.h>
|
2019-05-20 15:01:38 -04:00
|
|
|
#include <xf86drm.h>
|
|
|
|
#include <xf86drmMode.h>
|
2018-01-30 04:47:17 -05:00
|
|
|
|
|
|
|
#define INVALID_FB_ID 0U
|
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
typedef enum _MetaDrmBufferType
|
2018-01-30 04:47:17 -05:00
|
|
|
{
|
2019-05-20 15:20:17 -04:00
|
|
|
META_DRM_BUFFER_TYPE_GBM,
|
|
|
|
META_DRM_BUFFER_TYPE_WRAPPED_DUMB
|
|
|
|
} MetaDrmBufferType;
|
2018-01-30 04:47:17 -05:00
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
struct _MetaDrmBuffer
|
2018-01-30 04:47:17 -05:00
|
|
|
{
|
|
|
|
GObject parent;
|
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
MetaDrmBufferType type;
|
2018-01-30 04:47:17 -05:00
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
uint32_t fb_id;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
uint32_t fb_id;
|
|
|
|
struct gbm_surface *surface;
|
|
|
|
struct gbm_bo *bo;
|
|
|
|
MetaGpuKms *gpu_kms;
|
|
|
|
} gbm;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
uint32_t fb_id;
|
|
|
|
} wrapped_dumb;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
G_DEFINE_TYPE (MetaDrmBuffer, meta_drm_buffer, G_TYPE_OBJECT)
|
2018-01-30 04:47:17 -05:00
|
|
|
|
|
|
|
static gboolean
|
2019-05-20 15:20:17 -04:00
|
|
|
meta_drm_buffer_acquire_swapped_buffer (MetaDrmBuffer *buffer,
|
2018-01-30 04:47:17 -05:00
|
|
|
gboolean use_modifiers,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
uint32_t handles[4] = {0, 0, 0, 0};
|
|
|
|
uint32_t strides[4] = {0, 0, 0, 0};
|
|
|
|
uint32_t offsets[4] = {0, 0, 0, 0};
|
|
|
|
uint64_t modifiers[4] = {0, 0, 0, 0};
|
|
|
|
uint32_t width, height, format;
|
|
|
|
struct gbm_bo *bo;
|
|
|
|
int i;
|
2019-05-20 15:27:34 -04:00
|
|
|
int kms_fd;
|
2018-01-30 04:47:17 -05:00
|
|
|
|
2019-05-20 15:28:04 -04:00
|
|
|
g_assert (buffer->type == META_DRM_BUFFER_TYPE_GBM);
|
|
|
|
g_assert (!buffer->gbm.bo);
|
|
|
|
g_assert (buffer->gbm.surface);
|
|
|
|
g_assert (buffer->gbm.gpu_kms);
|
2018-01-30 04:47:17 -05:00
|
|
|
|
2019-05-20 15:27:34 -04:00
|
|
|
kms_fd = meta_gpu_kms_get_fd (buffer->gbm.gpu_kms);
|
2018-01-30 04:47:17 -05:00
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
bo = gbm_surface_lock_front_buffer (buffer->gbm.surface);
|
2018-01-30 04:47:17 -05:00
|
|
|
if (!bo)
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_FAILED,
|
|
|
|
"gbm_surface_lock_front_buffer failed");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gbm_bo_get_handle_for_plane (bo, 0).s32 == -1)
|
|
|
|
{
|
|
|
|
/* Failed to fetch handle to plane, falling back to old method */
|
|
|
|
strides[0] = gbm_bo_get_stride (bo);
|
|
|
|
handles[0] = gbm_bo_get_handle (bo).u32;
|
|
|
|
offsets[0] = 0;
|
|
|
|
modifiers[0] = DRM_FORMAT_MOD_INVALID;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < gbm_bo_get_plane_count (bo); i++)
|
|
|
|
{
|
|
|
|
strides[i] = gbm_bo_get_stride_for_plane (bo, i);
|
|
|
|
handles[i] = gbm_bo_get_handle_for_plane (bo, i).u32;
|
|
|
|
offsets[i] = gbm_bo_get_offset (bo, i);
|
|
|
|
modifiers[i] = gbm_bo_get_modifier (bo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
width = gbm_bo_get_width (bo);
|
|
|
|
height = gbm_bo_get_height (bo);
|
|
|
|
format = gbm_bo_get_format (bo);
|
|
|
|
|
|
|
|
if (use_modifiers && modifiers[0] != DRM_FORMAT_MOD_INVALID)
|
|
|
|
{
|
2019-05-20 15:27:34 -04:00
|
|
|
if (drmModeAddFB2WithModifiers (kms_fd,
|
2018-01-30 04:47:17 -05:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
format,
|
|
|
|
handles,
|
|
|
|
strides,
|
|
|
|
offsets,
|
|
|
|
modifiers,
|
2019-05-20 15:20:17 -04:00
|
|
|
&buffer->fb_id,
|
2018-01-30 04:47:17 -05:00
|
|
|
DRM_MODE_FB_MODIFIERS))
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
g_io_error_from_errno (errno),
|
|
|
|
"drmModeAddFB2WithModifiers failed: %s",
|
|
|
|
g_strerror (errno));
|
2019-05-20 15:20:17 -04:00
|
|
|
gbm_surface_release_buffer (buffer->gbm.surface, bo);
|
2018-01-30 04:47:17 -05:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2019-05-20 15:27:34 -04:00
|
|
|
else if (drmModeAddFB2 (kms_fd,
|
2018-01-30 04:47:17 -05:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
format,
|
|
|
|
handles,
|
|
|
|
strides,
|
|
|
|
offsets,
|
2019-05-20 15:20:17 -04:00
|
|
|
&buffer->fb_id,
|
2018-01-30 04:47:17 -05:00
|
|
|
0))
|
|
|
|
{
|
|
|
|
if (format != DRM_FORMAT_XRGB8888)
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_FAILED,
|
|
|
|
"drmModeAddFB does not support format 0x%x",
|
|
|
|
format);
|
2019-05-20 15:20:17 -04:00
|
|
|
gbm_surface_release_buffer (buffer->gbm.surface, bo);
|
2018-01-30 04:47:17 -05:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-05-20 15:27:34 -04:00
|
|
|
if (drmModeAddFB (kms_fd,
|
2018-01-30 04:47:17 -05:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
24,
|
|
|
|
32,
|
|
|
|
strides[0],
|
|
|
|
handles[0],
|
2019-05-20 15:20:17 -04:00
|
|
|
&buffer->fb_id))
|
2018-01-30 04:47:17 -05:00
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
g_io_error_from_errno (errno),
|
|
|
|
"drmModeAddFB failed: %s",
|
|
|
|
g_strerror (errno));
|
2019-05-20 15:20:17 -04:00
|
|
|
gbm_surface_release_buffer (buffer->gbm.surface, bo);
|
2018-01-30 04:47:17 -05:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
buffer->gbm.bo = bo;
|
2018-01-30 04:47:17 -05:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-05-20 15:20:17 -04:00
|
|
|
meta_drm_buffer_init (MetaDrmBuffer *buffer)
|
2018-01-30 04:47:17 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-05-20 15:20:17 -04:00
|
|
|
meta_drm_buffer_finalize (GObject *object)
|
2018-01-30 04:47:17 -05:00
|
|
|
{
|
2019-05-20 15:20:17 -04:00
|
|
|
MetaDrmBuffer *buffer = META_DRM_BUFFER (object);
|
2018-01-30 04:47:17 -05:00
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
if (buffer->type == META_DRM_BUFFER_TYPE_GBM)
|
2018-01-30 04:47:17 -05:00
|
|
|
{
|
2019-05-20 15:20:17 -04:00
|
|
|
if (buffer->gbm.gpu_kms != NULL &&
|
|
|
|
buffer->gbm.fb_id != INVALID_FB_ID)
|
2018-01-30 04:47:17 -05:00
|
|
|
{
|
2019-05-20 15:27:34 -04:00
|
|
|
int kms_fd;
|
|
|
|
|
|
|
|
kms_fd = meta_gpu_kms_get_fd (buffer->gbm.gpu_kms);
|
|
|
|
drmModeRmFB (kms_fd, buffer->fb_id);
|
2019-05-20 15:20:17 -04:00
|
|
|
buffer->fb_id = INVALID_FB_ID;
|
2018-01-30 04:47:17 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
if (buffer->gbm.surface &&
|
|
|
|
buffer->gbm.bo)
|
2018-01-30 04:47:17 -05:00
|
|
|
{
|
2019-05-20 15:20:17 -04:00
|
|
|
gbm_surface_release_buffer (buffer->gbm.surface,
|
|
|
|
buffer->gbm.bo);
|
2018-01-30 04:47:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
G_OBJECT_CLASS (meta_drm_buffer_parent_class)->finalize (object);
|
2018-01-30 04:47:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-05-20 15:20:17 -04:00
|
|
|
meta_drm_buffer_class_init (MetaDrmBufferClass *klass)
|
2018-01-30 04:47:17 -05:00
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
object_class->finalize = meta_drm_buffer_finalize;
|
2018-01-30 04:47:17 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
MetaDrmBuffer *
|
|
|
|
meta_drm_buffer_new_from_gbm (MetaGpuKms *gpu_kms,
|
2018-01-30 04:47:17 -05:00
|
|
|
struct gbm_surface *gbm_surface,
|
|
|
|
gboolean use_modifiers,
|
|
|
|
GError **error)
|
|
|
|
{
|
2019-05-20 15:20:17 -04:00
|
|
|
MetaDrmBuffer *buffer;
|
2018-01-30 04:47:17 -05:00
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
buffer = g_object_new (META_TYPE_DRM_BUFFER, NULL);
|
|
|
|
buffer->type = META_DRM_BUFFER_TYPE_GBM;
|
|
|
|
buffer->gbm.gpu_kms = gpu_kms;
|
|
|
|
buffer->gbm.surface = gbm_surface;
|
2018-01-30 04:47:17 -05:00
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
if (!meta_drm_buffer_acquire_swapped_buffer (buffer,
|
2018-01-30 04:47:17 -05:00
|
|
|
use_modifiers,
|
|
|
|
error))
|
|
|
|
{
|
2019-05-20 15:20:17 -04:00
|
|
|
g_object_unref (buffer);
|
2018-01-30 04:47:17 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
return buffer;
|
2018-01-30 04:47:17 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
MetaDrmBuffer *
|
|
|
|
meta_drm_buffer_new_from_dumb (uint32_t dumb_fb_id)
|
2018-01-30 04:47:17 -05:00
|
|
|
{
|
2019-05-20 15:20:17 -04:00
|
|
|
MetaDrmBuffer *buffer;
|
2018-01-30 04:47:17 -05:00
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
buffer = g_object_new (META_TYPE_DRM_BUFFER, NULL);
|
|
|
|
buffer->type = META_DRM_BUFFER_TYPE_WRAPPED_DUMB;
|
|
|
|
buffer->wrapped_dumb.fb_id = dumb_fb_id;
|
2018-01-30 04:47:17 -05:00
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
return buffer;
|
2018-01-30 04:47:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2019-05-20 15:20:17 -04:00
|
|
|
meta_drm_buffer_get_fb_id (const MetaDrmBuffer *buffer)
|
2018-01-30 04:47:17 -05:00
|
|
|
{
|
2019-05-20 15:20:17 -04:00
|
|
|
return buffer->fb_id;
|
2018-01-30 04:47:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct gbm_bo *
|
2019-05-20 15:20:17 -04:00
|
|
|
meta_drm_buffer_get_bo (const MetaDrmBuffer *buffer)
|
2018-01-30 04:47:17 -05:00
|
|
|
{
|
2019-05-20 15:20:17 -04:00
|
|
|
g_return_val_if_fail (buffer->type == META_DRM_BUFFER_TYPE_GBM, NULL);
|
2018-01-30 04:47:17 -05:00
|
|
|
|
2019-05-20 15:20:17 -04:00
|
|
|
return buffer->gbm.bo;
|
2018-01-30 04:47:17 -05:00
|
|
|
}
|