2023-10-09 20:19:59 +00:00
|
|
|
/* gdkdmabuftexture.c
|
|
|
|
*
|
|
|
|
* Copyright 2023 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gdkdmabuftextureprivate.h"
|
|
|
|
|
|
|
|
#include "gdkdisplayprivate.h"
|
2023-10-14 19:00:09 +00:00
|
|
|
#include "gdkdmabufformatsbuilderprivate.h"
|
2023-10-20 10:08:18 +00:00
|
|
|
#include "gdkdmabufprivate.h"
|
2023-10-15 03:21:23 +00:00
|
|
|
#include "gdktextureprivate.h"
|
2023-10-09 20:19:59 +00:00
|
|
|
#include <gdk/gdkglcontext.h>
|
|
|
|
#include <gdk/gdkgltexturebuilder.h>
|
|
|
|
#include <gdk/gdktexturedownloader.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_LINUX_DMA_BUF_H
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <linux/dma-buf.h>
|
|
|
|
#include <drm/drm_fourcc.h>
|
|
|
|
#include <epoxy/egl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GdkDmabufTexture:
|
|
|
|
*
|
|
|
|
* A `GdkTexture` representing a dma-buf object.
|
|
|
|
*
|
|
|
|
* To create a `GdkDmabufTexture`, use the auxiliary
|
|
|
|
* [class@Gdk.DmabufTextureBuilder] object.
|
|
|
|
*
|
|
|
|
* Dma-buf textures can only be created on Linux.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct _GdkDmabufTexture
|
|
|
|
{
|
|
|
|
GdkTexture parent_instance;
|
|
|
|
|
|
|
|
GdkDisplay *display;
|
2023-10-15 03:21:23 +00:00
|
|
|
const GdkDmabufDownloader *downloader;
|
2023-10-09 20:19:59 +00:00
|
|
|
|
2023-10-15 03:21:23 +00:00
|
|
|
GdkDmabuf dmabuf;
|
2023-10-09 20:19:59 +00:00
|
|
|
|
|
|
|
GDestroyNotify destroy;
|
|
|
|
gpointer data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GdkDmabufTextureClass
|
|
|
|
{
|
|
|
|
GdkTextureClass parent_class;
|
|
|
|
};
|
|
|
|
|
2023-10-14 19:57:15 +00:00
|
|
|
G_DEFINE_QUARK (gdk-dmabuf-error-quark, gdk_dmabuf_error)
|
|
|
|
|
2023-10-09 20:19:59 +00:00
|
|
|
G_DEFINE_TYPE (GdkDmabufTexture, gdk_dmabuf_texture, GDK_TYPE_TEXTURE)
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_dmabuf_texture_init (GdkDmabufTexture *self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_dmabuf_texture_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GdkDmabufTexture *self = GDK_DMABUF_TEXTURE (object);
|
|
|
|
|
|
|
|
if (self->destroy)
|
|
|
|
self->destroy (self->data);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gdk_dmabuf_texture_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2023-10-15 03:21:23 +00:00
|
|
|
static void
|
|
|
|
gdk_dmabuf_texture_download (GdkTexture *texture,
|
|
|
|
GdkMemoryFormat format,
|
|
|
|
guchar *data,
|
|
|
|
gsize stride)
|
|
|
|
{
|
|
|
|
GdkDmabufTexture *self = GDK_DMABUF_TEXTURE (texture);
|
|
|
|
|
|
|
|
self->downloader->download (self->downloader,
|
|
|
|
texture,
|
|
|
|
format,
|
|
|
|
data,
|
|
|
|
stride);
|
|
|
|
}
|
2023-10-09 20:19:59 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_dmabuf_texture_class_init (GdkDmabufTextureClass *klass)
|
|
|
|
{
|
|
|
|
GdkTextureClass *texture_class = GDK_TEXTURE_CLASS (klass);
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
texture_class->download = gdk_dmabuf_texture_download;
|
|
|
|
|
|
|
|
gobject_class->dispose = gdk_dmabuf_texture_dispose;
|
|
|
|
}
|
|
|
|
|
2023-10-17 02:18:59 +00:00
|
|
|
GdkDisplay *
|
|
|
|
gdk_dmabuf_texture_get_display (GdkDmabufTexture *self)
|
|
|
|
{
|
|
|
|
return self->display;
|
|
|
|
}
|
|
|
|
|
2023-10-15 03:21:23 +00:00
|
|
|
const GdkDmabuf *
|
|
|
|
gdk_dmabuf_texture_get_dmabuf (GdkDmabufTexture *self)
|
2023-10-09 20:19:59 +00:00
|
|
|
{
|
2023-10-15 03:21:23 +00:00
|
|
|
return &self->dmabuf;
|
2023-10-09 20:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GdkTexture *
|
|
|
|
gdk_dmabuf_texture_new_from_builder (GdkDmabufTextureBuilder *builder,
|
|
|
|
GDestroyNotify destroy,
|
2023-10-14 19:57:15 +00:00
|
|
|
gpointer data,
|
|
|
|
GError **error)
|
2023-10-09 20:19:59 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_LINUX_DMA_BUF_H
|
|
|
|
GdkDmabufTexture *self;
|
|
|
|
GdkTexture *update_texture;
|
2023-10-15 03:21:23 +00:00
|
|
|
GdkDisplay *display;
|
2023-10-20 10:08:18 +00:00
|
|
|
GdkDmabuf dmabuf;
|
2023-10-15 03:21:23 +00:00
|
|
|
GdkMemoryFormat format;
|
|
|
|
GError *local_error = NULL;
|
2023-10-20 10:08:18 +00:00
|
|
|
int width, height;
|
2023-10-15 03:21:23 +00:00
|
|
|
gsize i;
|
2023-10-09 20:19:59 +00:00
|
|
|
|
2023-10-15 03:21:23 +00:00
|
|
|
display = gdk_dmabuf_texture_builder_get_display (builder);
|
2023-10-20 10:08:18 +00:00
|
|
|
width = gdk_dmabuf_texture_builder_get_width (builder);
|
|
|
|
height = gdk_dmabuf_texture_builder_get_height (builder);
|
|
|
|
|
|
|
|
if (!gdk_dmabuf_sanitize (&dmabuf,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
gdk_dmabuf_texture_builder_get_dmabuf (builder),
|
|
|
|
error))
|
|
|
|
return NULL;
|
2023-10-09 20:19:59 +00:00
|
|
|
|
2023-10-19 16:53:27 +00:00
|
|
|
gdk_display_init_dmabuf (display);
|
|
|
|
|
2023-10-15 03:21:23 +00:00
|
|
|
for (i = 0; display->dmabuf_downloaders[i] != NULL; i++)
|
2023-10-14 19:57:15 +00:00
|
|
|
{
|
2023-10-15 03:21:23 +00:00
|
|
|
if (local_error && g_error_matches (local_error, GDK_DMABUF_ERROR, GDK_DMABUF_ERROR_UNSUPPORTED_FORMAT))
|
|
|
|
g_clear_error (&local_error);
|
|
|
|
|
|
|
|
if (display->dmabuf_downloaders[i]->supports (display->dmabuf_downloaders[i],
|
|
|
|
display,
|
2023-10-20 10:08:18 +00:00
|
|
|
&dmabuf,
|
2023-10-15 03:21:23 +00:00
|
|
|
gdk_dmabuf_texture_builder_get_premultiplied (builder),
|
|
|
|
&format,
|
|
|
|
local_error ? NULL : &local_error))
|
|
|
|
break;
|
2023-10-14 19:57:15 +00:00
|
|
|
}
|
2023-10-15 03:21:23 +00:00
|
|
|
|
|
|
|
if (display->dmabuf_downloaders[i] == NULL)
|
2023-10-09 20:19:59 +00:00
|
|
|
{
|
2023-10-15 03:21:23 +00:00
|
|
|
g_propagate_error (error, local_error);
|
2023-10-09 20:19:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
self = g_object_new (GDK_TYPE_DMABUF_TEXTURE,
|
2023-10-20 10:08:18 +00:00
|
|
|
"width", width,
|
|
|
|
"height", height,
|
2023-10-09 20:19:59 +00:00
|
|
|
NULL);
|
|
|
|
|
2023-10-15 03:21:23 +00:00
|
|
|
GDK_TEXTURE (self)->format = format;
|
|
|
|
g_set_object (&self->display, display);
|
|
|
|
self->downloader = display->dmabuf_downloaders[i];
|
2023-10-20 10:08:18 +00:00
|
|
|
self->dmabuf = dmabuf;
|
2023-10-09 20:19:59 +00:00
|
|
|
self->destroy = destroy;
|
|
|
|
self->data = data;
|
|
|
|
|
|
|
|
update_texture = gdk_dmabuf_texture_builder_get_update_texture (builder);
|
|
|
|
if (update_texture)
|
|
|
|
{
|
|
|
|
cairo_region_t *update_region = gdk_dmabuf_texture_builder_get_update_region (builder);
|
|
|
|
if (update_region)
|
|
|
|
{
|
|
|
|
update_region = cairo_region_copy (update_region);
|
|
|
|
cairo_region_intersect_rectangle (update_region,
|
|
|
|
&(cairo_rectangle_int_t) {
|
|
|
|
0, 0,
|
|
|
|
update_texture->width, update_texture->height
|
|
|
|
});
|
|
|
|
gdk_texture_set_diff (GDK_TEXTURE (self), update_texture, update_region);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return GDK_TEXTURE (self);
|
|
|
|
|
|
|
|
#else /* !HAVE_LINUX_DMA_BUF_H */
|
2023-10-14 19:57:15 +00:00
|
|
|
g_set_error_literal (error, GDK_DMABUF_ERROR, GDK_DMABUF_ERROR_NOT_AVAILABLE,
|
|
|
|
"dmabuf support disabled at compile-time.");
|
2023-10-09 20:19:59 +00:00
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
2023-10-15 03:21:23 +00:00
|
|
|
|