Compare commits

...

11 Commits

Author SHA1 Message Date
Matthias Clasen
30025daf29 glcontext: debug spew 2023-10-17 22:29:20 -04:00
Matthias Clasen
fc479fbfff wip: Try to make a plain GL downloader 2023-10-17 21:17:52 -04:00
Matthias Clasen
e33fad05c3 Add a few dmabuf texture tests
Test some basic import and export of dmabufs.
2023-10-17 21:06:39 -04:00
Matthias Clasen
40408b0d0d Make a test app with dma heaps
This uses the dma-heap kernel api to create a dma-buf
and use it for a GdkDmabufTexture. It supports a few
formats to test how well GL conversion of YUV works.

The YUV code is adapted from weston tests.
2023-10-17 12:22:54 -04:00
Matthias Clasen
bd5be14dfa gsk: Support dmabuf textures
Defer handling of dmabuf textures to the driver,
and add the necessary code in load_texture.
2023-10-17 11:59:52 -04:00
Matthias Clasen
fae496aad5 display: Add the egl downloader 2023-10-17 11:59:52 -04:00
Matthias Clasen
149e47cf26 dmabuf: Add an egl downloader
Add an implementation of GdkDmabufDownloader
that uses EGL and gsk.
2023-10-17 11:59:52 -04:00
Matthias Clasen
fb9f06f7e5 display: Add egl downloader data
We want to cache a GSK renderer, since these
are expensive to create. Instead of using GObject
data, add a little private API for this.
2023-10-17 11:59:52 -04:00
Matthias Clasen
da30d4db36 gl context: Add private dmabuf API
Add API to import a dmabuf into a texture,
and to export a texture to a dmabuf.

All this is implemented using the relevant
EGL and GL extensions.
2023-10-17 11:59:52 -04:00
Matthias Clasen
9a580b3db1 gl context: Check for more GL extensions
These will be used in future commits.
2023-10-17 07:23:51 -04:00
Matthias Clasen
3406599942 display: Check for more EGL extensions
We are going to rely on this in future commits.
2023-10-17 07:23:51 -04:00
14 changed files with 1927 additions and 19 deletions

View File

@@ -391,6 +391,8 @@ gdk_display_dispose (GObject *object)
g_queue_clear (&display->queued_events);
gdk_display_set_egl_downloader_data (display, NULL, NULL);
g_clear_object (&priv->gl_context);
#ifdef HAVE_EGL
g_clear_pointer (&priv->egl_display, eglTerminate);
@@ -1770,6 +1772,10 @@ gdk_display_init_egl (GdkDisplay *self,
epoxy_has_egl_extension (priv->egl_display, "EGL_KHR_no_config_context");
self->have_egl_pixel_format_float =
epoxy_has_egl_extension (priv->egl_display, "EGL_EXT_pixel_format_float");
self->have_egl_dma_buf_import =
epoxy_has_egl_extension (priv->egl_display, "EGL_EXT_image_dma_buf_import_modifiers");
self->have_egl_dma_buf_export =
epoxy_has_egl_extension (priv->egl_display, "EGL_MESA_image_dma_buf_export");
if (self->have_egl_no_config_context)
priv->egl_config_high_depth = gdk_display_create_egl_config (self,
@@ -1879,14 +1885,22 @@ init_dmabuf_formats (GdkDisplay *display)
#ifdef HAVE_LINUX_DMA_BUF_H
if (!GDK_DEBUG_CHECK (DMABUF_DISABLE))
{
gdk_display_prepare_gl (display, NULL);
gdk_display_add_dmabuf_downloader (display, gdk_dmabuf_get_direct_downloader (), builder);
#ifdef HAVE_EGL
if (gdk_display_prepare_gl (display, NULL))
{
if (g_strcmp0 (g_getenv ("DMABUF_DOWNLOAD_METHOD"), "gl") == 0)
gdk_display_add_dmabuf_downloader (display, gdk_dmabuf_get_gl_downloader (), builder);
else
gdk_display_add_dmabuf_downloader (display, gdk_dmabuf_get_egl_downloader (), builder);
}
#endif
}
#endif
display->dmabuf_formats = gdk_dmabuf_formats_builder_free_to_formats (builder);
}
}
/**
* gdk_display_get_dmabuf_formats:
@@ -2368,3 +2382,21 @@ gdk_display_translate_key (GdkDisplay *display,
level,
consumed);
}
gpointer
gdk_display_get_egl_downloader_data (GdkDisplay *display)
{
return display->egl_downloader_data;
}
void
gdk_display_set_egl_downloader_data (GdkDisplay *display,
gpointer data,
GDestroyNotify destroy)
{
if (display->egl_downloader_data_destroy)
display->egl_downloader_data_destroy (display->egl_downloader_data);
display->egl_downloader_data = data;
display->egl_downloader_data_destroy = destroy;
}

View File

@@ -114,9 +114,14 @@ struct _GdkDisplay
guint have_egl_buffer_age : 1;
guint have_egl_no_config_context : 1;
guint have_egl_pixel_format_float : 1;
guint have_egl_dma_buf_import : 1;
guint have_egl_dma_buf_export : 1;
GdkDmabufFormats *dmabuf_formats;
const GdkDmabufDownloader *dmabuf_downloaders[4];
/* Private data for the EGL dmabuf downloader */
gpointer egl_downloader_data;
GDestroyNotify egl_downloader_data_destroy;
};
struct _GdkDisplayClass
@@ -265,5 +270,11 @@ void gdk_display_set_cursor_theme (GdkDisplay *display,
const char *theme,
int size);
gpointer gdk_display_get_egl_downloader_data (GdkDisplay *display);
void gdk_display_set_egl_downloader_data (GdkDisplay *display,
gpointer data,
GDestroyNotify destroy);
G_END_DECLS

325
gdk/gdkdmabufegl.c Normal file
View File

@@ -0,0 +1,325 @@
/* gdkdmabufegl.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"
#if defined(HAVE_LINUX_DMA_BUF_H) && defined (HAVE_EGL)
#include "gdkdmabufprivate.h"
#include "gdkdebugprivate.h"
#include "gdkdmabuftextureprivate.h"
#include "gdkmemoryformatprivate.h"
#include "gdkdisplayprivate.h"
#include "gdkglcontextprivate.h"
#include "gdktexturedownloader.h"
#include <graphene.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/dma-buf.h>
#include <drm/drm_fourcc.h>
#include <epoxy/egl.h>
static void
gdk_dmabuf_egl_downloader_add_formats (const GdkDmabufDownloader *downloader,
GdkDisplay *display,
GdkDmabufFormatsBuilder *builder)
{
GdkGLContext *context = gdk_display_get_gl_context (display);
EGLDisplay egl_display = gdk_display_get_egl_display (display);
gdk_gl_context_make_current (context);
if (egl_display != EGL_NO_DISPLAY &&
display->have_egl_dma_buf_import &&
gdk_gl_context_has_image_storage (context))
{
int num_fourccs;
int *fourccs;
guint64 *modifiers;
unsigned int *external_only;
int n_mods;
eglQueryDmaBufFormatsEXT (egl_display, 0, NULL, &num_fourccs);
fourccs = g_new (int, num_fourccs);
eglQueryDmaBufFormatsEXT (egl_display, num_fourccs, fourccs, &num_fourccs);
n_mods = 80;
modifiers = g_new (guint64, n_mods);
external_only = g_new (unsigned int, n_mods);
for (int i = 0; i < num_fourccs; i++)
{
int num_modifiers;
eglQueryDmaBufModifiersEXT (egl_display,
fourccs[i],
0,
NULL,
NULL,
&num_modifiers);
if (num_modifiers > n_mods)
{
n_mods = num_modifiers;
modifiers = g_renew (guint64, modifiers, n_mods);
external_only = g_renew (unsigned int, external_only, n_mods);
}
eglQueryDmaBufModifiersEXT (egl_display,
fourccs[i],
num_modifiers,
modifiers,
external_only,
&num_modifiers);
for (int j = 0; j < num_modifiers; j++)
{
GDK_DEBUG (DMABUF, "%ssupported EGL dmabuf format %.4s:%#lx %s",
external_only[j] ? "un" : "",
(char *) &fourccs[i],
modifiers[j],
external_only[j] ? "EXT" : "");
if (!external_only[j])
gdk_dmabuf_formats_builder_add_format (builder, fourccs[i], modifiers[j]);
}
}
g_free (modifiers);
g_free (external_only);
g_free (fourccs);
}
}
static GdkMemoryFormat
get_memory_format (guint32 fourcc,
gboolean premultiplied)
{
switch (fourcc)
{
case DRM_FORMAT_ARGB8888:
case DRM_FORMAT_ABGR8888:
case DRM_FORMAT_XRGB8888_A8:
case DRM_FORMAT_XBGR8888_A8:
return premultiplied ? GDK_MEMORY_A8R8G8B8_PREMULTIPLIED : GDK_MEMORY_A8R8G8B8;
case DRM_FORMAT_RGBA8888:
case DRM_FORMAT_RGBX8888_A8:
return premultiplied ? GDK_MEMORY_R8G8B8A8_PREMULTIPLIED : GDK_MEMORY_R8G8B8A8;
case DRM_FORMAT_BGRA8888:
return premultiplied ? GDK_MEMORY_B8G8R8A8_PREMULTIPLIED : GDK_MEMORY_B8G8R8A8;
case DRM_FORMAT_RGB888:
case DRM_FORMAT_XRGB8888:
case DRM_FORMAT_XBGR8888:
case DRM_FORMAT_RGBX8888:
case DRM_FORMAT_BGRX8888:
return GDK_MEMORY_R8G8B8;
case DRM_FORMAT_BGR888:
return GDK_MEMORY_B8G8R8;
case DRM_FORMAT_XRGB2101010:
case DRM_FORMAT_XBGR2101010:
case DRM_FORMAT_RGBX1010102:
case DRM_FORMAT_BGRX1010102:
case DRM_FORMAT_XRGB16161616:
case DRM_FORMAT_XBGR16161616:
return GDK_MEMORY_R16G16B16;
case DRM_FORMAT_ARGB2101010:
case DRM_FORMAT_ABGR2101010:
case DRM_FORMAT_RGBA1010102:
case DRM_FORMAT_BGRA1010102:
case DRM_FORMAT_ARGB16161616:
case DRM_FORMAT_ABGR16161616:
return premultiplied ? GDK_MEMORY_R16G16B16A16_PREMULTIPLIED : GDK_MEMORY_R16G16B16A16;
case DRM_FORMAT_ARGB16161616F:
case DRM_FORMAT_ABGR16161616F:
return premultiplied ? GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED : GDK_MEMORY_R16G16B16A16_FLOAT;
case DRM_FORMAT_XRGB16161616F:
case DRM_FORMAT_XBGR16161616F:
return GDK_MEMORY_R16G16B16_FLOAT;
case DRM_FORMAT_YUYV:
case DRM_FORMAT_YVYU:
case DRM_FORMAT_UYVY:
case DRM_FORMAT_VYUY:
case DRM_FORMAT_XYUV8888:
case DRM_FORMAT_XVUY8888:
case DRM_FORMAT_VUY888:
return GDK_MEMORY_R8G8B8;
/* Add more formats here */
default:
return premultiplied ? GDK_MEMORY_A8R8G8B8_PREMULTIPLIED : GDK_MEMORY_A8R8G8B8;
}
}
static gboolean
gdk_dmabuf_egl_downloader_supports (const GdkDmabufDownloader *downloader,
GdkDisplay *display,
const GdkDmabuf *dmabuf,
gboolean premultiplied,
GdkMemoryFormat *out_format,
GError **error)
{
EGLDisplay egl_display;
GdkGLContext *context;
int num_modifiers;
guint64 *modifiers;
unsigned int *external_only;
egl_display = gdk_display_get_egl_display (display);
if (egl_display == EGL_NO_DISPLAY)
{
g_set_error_literal (error,
GDK_DMABUF_ERROR, GDK_DMABUF_ERROR_UNSUPPORTED_FORMAT,
"EGL not available");
return FALSE;
}
context = gdk_display_get_gl_context (display);
gdk_gl_context_make_current (context);
eglQueryDmaBufModifiersEXT (egl_display,
dmabuf->fourcc,
0,
NULL,
NULL,
&num_modifiers);
modifiers = g_newa (uint64_t, num_modifiers);
external_only = g_newa (unsigned int, num_modifiers);
eglQueryDmaBufModifiersEXT (egl_display,
dmabuf->fourcc,
num_modifiers,
modifiers,
external_only,
&num_modifiers);
for (int i = 0; i < num_modifiers; i++)
{
if (external_only[i])
continue;
if (modifiers[i] == dmabuf->modifier)
{
*out_format = get_memory_format (dmabuf->fourcc, premultiplied);
return TRUE;
}
}
g_set_error (error,
GDK_DMABUF_ERROR, GDK_DMABUF_ERROR_UNSUPPORTED_FORMAT,
"Unsupported dmabuf format: %.4s:%#lx",
(char *) &dmabuf->fourcc, dmabuf->modifier);
return FALSE;
}
/* Hack. We don't include gsk/gsk.h here to avoid a build order problem
* with the generated header gskenumtypes.h, so we need to hack around
* a bit to access the gsk api we need.
*/
typedef gpointer GskRenderer;
typedef gpointer GskRenderNode;
extern GskRenderer * gsk_gl_renderer_new (void);
extern gboolean gsk_renderer_realize (GskRenderer *renderer,
GdkSurface *surface,
GError **error);
extern void gsk_renderer_unrealize (GskRenderer *renderer);
extern GdkTexture * gsk_renderer_render_texture (GskRenderer *renderer,
GskRenderNode *node,
const graphene_rect_t *bounds);
extern GskRenderNode * gsk_texture_node_new (GdkTexture *texture,
const graphene_rect_t *viewport);
extern void gsk_render_node_unref (GskRenderNode *node);
static void
gdk_dmabuf_egl_downloader_download (const GdkDmabufDownloader *downloader,
GdkTexture *texture,
GdkMemoryFormat format,
guchar *data,
gsize stride)
{
GdkDmabufTexture *self = GDK_DMABUF_TEXTURE (texture);
GdkDisplay *display;
GskRenderer *renderer;
int width, height;
GskRenderNode *node;
GdkTexture *gl_texture;
GdkTextureDownloader *tex_downloader;
GError *error = NULL;
GDK_DEBUG (DMABUF, "Using gsk for downloading a dmabuf");
display = gdk_dmabuf_texture_get_display (self);
renderer = gdk_display_get_egl_downloader_data (display);
if (!renderer)
{
renderer = gsk_gl_renderer_new ();
if (!gsk_renderer_realize (renderer, NULL, &error))
{
g_warning ("Failed to realize GL renderer: %s", error->message);
g_error_free (error);
g_object_unref (renderer);
return;
}
gdk_display_set_egl_downloader_data (display, renderer, g_object_unref);
}
width = gdk_texture_get_width (GDK_TEXTURE (self));
height = gdk_texture_get_height (GDK_TEXTURE (self));
node = gsk_texture_node_new (GDK_TEXTURE (self), &GRAPHENE_RECT_INIT (0, 0, width, height));
gl_texture = gsk_renderer_render_texture (renderer, node, &GRAPHENE_RECT_INIT (0, 0, width, height));
gsk_render_node_unref (node);
tex_downloader = gdk_texture_downloader_new (gl_texture);
gdk_texture_downloader_set_format (tex_downloader, format);
gdk_texture_downloader_download_into (tex_downloader, data, stride);
gdk_texture_downloader_free (tex_downloader);
g_object_unref (gl_texture);
}
const GdkDmabufDownloader *
gdk_dmabuf_get_egl_downloader (void)
{
static const GdkDmabufDownloader downloader = {
gdk_dmabuf_egl_downloader_add_formats,
gdk_dmabuf_egl_downloader_supports,
gdk_dmabuf_egl_downloader_download,
};
return &downloader;
}
#endif /* HAVE_LINUX_DMA_BUF_H && HAVE_EGL */

583
gdk/gdkdmabufgl.c Normal file
View File

@@ -0,0 +1,583 @@
/* gdkdmabufegl.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"
#if defined(HAVE_LINUX_DMA_BUF_H) && defined (HAVE_EGL)
#include "gdkdmabufprivate.h"
#include "gdkdebugprivate.h"
#include "gdkdmabuftextureprivate.h"
#include "gdkmemoryformatprivate.h"
#include "gdkdisplayprivate.h"
#include "gdkglcontextprivate.h"
#include "gdkgltexturebuilder.h"
#include "gdktexturedownloader.h"
#include <graphene.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/dma-buf.h>
#include <drm/drm_fourcc.h>
#include <epoxy/egl.h>
static void
gdk_dmabuf_gl_downloader_add_formats (const GdkDmabufDownloader *downloader,
GdkDisplay *display,
GdkDmabufFormatsBuilder *builder)
{
GdkGLContext *context = gdk_display_get_gl_context (display);
EGLDisplay egl_display = gdk_display_get_egl_display (display);
gdk_gl_context_make_current (context);
if (egl_display != EGL_NO_DISPLAY &&
display->have_egl_dma_buf_import &&
gdk_gl_context_has_image_storage (context))
{
int num_fourccs;
int *fourccs;
guint64 *modifiers;
unsigned int *external_only;
int n_mods;
eglQueryDmaBufFormatsEXT (egl_display, 0, NULL, &num_fourccs);
fourccs = g_new (int, num_fourccs);
eglQueryDmaBufFormatsEXT (egl_display, num_fourccs, fourccs, &num_fourccs);
n_mods = 80;
modifiers = g_new (guint64, n_mods);
external_only = g_new (unsigned int, n_mods);
for (int i = 0; i < num_fourccs; i++)
{
int num_modifiers;
eglQueryDmaBufModifiersEXT (egl_display,
fourccs[i],
0,
NULL,
NULL,
&num_modifiers);
if (num_modifiers > n_mods)
{
n_mods = num_modifiers;
modifiers = g_renew (guint64, modifiers, n_mods);
external_only = g_renew (unsigned int, external_only, n_mods);
}
eglQueryDmaBufModifiersEXT (egl_display,
fourccs[i],
num_modifiers,
modifiers,
external_only,
&num_modifiers);
for (int j = 0; j < num_modifiers; j++)
{
GDK_DEBUG (DMABUF, "%ssupported EGL dmabuf format %.4s:%#lx %s",
external_only[j] ? "un" : "",
(char *) &fourccs[i],
modifiers[j],
external_only[j] ? "EXT" : "");
if (!external_only[j])
gdk_dmabuf_formats_builder_add_format (builder, fourccs[i], modifiers[j]);
}
}
g_free (modifiers);
g_free (external_only);
g_free (fourccs);
}
}
static GdkMemoryFormat
get_memory_format (guint32 fourcc,
gboolean premultiplied)
{
switch (fourcc)
{
case DRM_FORMAT_ARGB8888:
case DRM_FORMAT_ABGR8888:
case DRM_FORMAT_XRGB8888_A8:
case DRM_FORMAT_XBGR8888_A8:
return premultiplied ? GDK_MEMORY_A8R8G8B8_PREMULTIPLIED : GDK_MEMORY_A8R8G8B8;
case DRM_FORMAT_RGBA8888:
case DRM_FORMAT_RGBX8888_A8:
return premultiplied ? GDK_MEMORY_R8G8B8A8_PREMULTIPLIED : GDK_MEMORY_R8G8B8A8;
case DRM_FORMAT_BGRA8888:
return premultiplied ? GDK_MEMORY_B8G8R8A8_PREMULTIPLIED : GDK_MEMORY_B8G8R8A8;
case DRM_FORMAT_RGB888:
case DRM_FORMAT_XRGB8888:
case DRM_FORMAT_XBGR8888:
case DRM_FORMAT_RGBX8888:
case DRM_FORMAT_BGRX8888:
return GDK_MEMORY_R8G8B8;
case DRM_FORMAT_BGR888:
return GDK_MEMORY_B8G8R8;
case DRM_FORMAT_XRGB2101010:
case DRM_FORMAT_XBGR2101010:
case DRM_FORMAT_RGBX1010102:
case DRM_FORMAT_BGRX1010102:
case DRM_FORMAT_XRGB16161616:
case DRM_FORMAT_XBGR16161616:
return GDK_MEMORY_R16G16B16;
case DRM_FORMAT_ARGB2101010:
case DRM_FORMAT_ABGR2101010:
case DRM_FORMAT_RGBA1010102:
case DRM_FORMAT_BGRA1010102:
case DRM_FORMAT_ARGB16161616:
case DRM_FORMAT_ABGR16161616:
return premultiplied ? GDK_MEMORY_R16G16B16A16_PREMULTIPLIED : GDK_MEMORY_R16G16B16A16;
case DRM_FORMAT_ARGB16161616F:
case DRM_FORMAT_ABGR16161616F:
return premultiplied ? GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED : GDK_MEMORY_R16G16B16A16_FLOAT;
case DRM_FORMAT_XRGB16161616F:
case DRM_FORMAT_XBGR16161616F:
return GDK_MEMORY_R16G16B16_FLOAT;
case DRM_FORMAT_YUYV:
case DRM_FORMAT_YVYU:
case DRM_FORMAT_UYVY:
case DRM_FORMAT_VYUY:
case DRM_FORMAT_XYUV8888:
case DRM_FORMAT_XVUY8888:
case DRM_FORMAT_VUY888:
return GDK_MEMORY_R8G8B8;
/* Add more formats here */
default:
return premultiplied ? GDK_MEMORY_A8R8G8B8_PREMULTIPLIED : GDK_MEMORY_A8R8G8B8;
}
}
static gboolean
gdk_dmabuf_gl_downloader_supports (const GdkDmabufDownloader *downloader,
GdkDisplay *display,
const GdkDmabuf *dmabuf,
gboolean premultiplied,
GdkMemoryFormat *out_format,
GError **error)
{
EGLDisplay egl_display;
GdkGLContext *context;
int num_modifiers;
guint64 *modifiers;
unsigned int *external_only;
egl_display = gdk_display_get_egl_display (display);
if (egl_display == EGL_NO_DISPLAY)
{
g_set_error_literal (error,
GDK_DMABUF_ERROR, GDK_DMABUF_ERROR_UNSUPPORTED_FORMAT,
"EGL not available");
return FALSE;
}
context = gdk_display_get_gl_context (display);
gdk_gl_context_make_current (context);
eglQueryDmaBufModifiersEXT (egl_display,
dmabuf->fourcc,
0,
NULL,
NULL,
&num_modifiers);
modifiers = g_newa (uint64_t, num_modifiers);
external_only = g_newa (unsigned int, num_modifiers);
eglQueryDmaBufModifiersEXT (egl_display,
dmabuf->fourcc,
num_modifiers,
modifiers,
external_only,
&num_modifiers);
for (int i = 0; i < num_modifiers; i++)
{
if (external_only[i])
continue;
if (modifiers[i] == dmabuf->modifier)
{
*out_format = get_memory_format (dmabuf->fourcc, premultiplied);
return TRUE;
}
}
g_set_error (error,
GDK_DMABUF_ERROR, GDK_DMABUF_ERROR_UNSUPPORTED_FORMAT,
"Unsupported dmabuf format: %.4s:%#lx",
(char *) &dmabuf->fourcc, dmabuf->modifier);
return FALSE;
}
static int
gl_format_for_memory_format (GdkMemoryFormat format)
{
switch (gdk_memory_format_get_depth (format))
{
case GDK_MEMORY_U8:
return GL_RGBA8;
case GDK_MEMORY_U16:
return GL_RGBA16;
case GDK_MEMORY_FLOAT16:
return GL_RGBA16F;
case GDK_MEMORY_FLOAT32:
return GL_RGBA32F;
default:
g_assert_not_reached ();
}
}
static int
gl_type_for_gl_format (int format)
{
switch (format)
{
case GL_RGBA8:
return GL_UNSIGNED_BYTE;
case GL_RGBA16:
return GL_UNSIGNED_SHORT;
case GL_RGBA16F:
return GL_HALF_FLOAT;
case GL_RGBA32F:
return GL_FLOAT;
default:
g_assert_not_reached ();
}
}
static void
create_render_target (int width,
int height,
int format,
unsigned int *fbo_id,
unsigned int *texture_id)
{
int type;
unsigned int texture, fbo;
type = gl_type_for_gl_format (format);
glGenTextures (1, &texture);
glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, texture);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D (GL_TEXTURE_2D, 0, format, width, height, 0, GL_RGBA, type, NULL);
glGenFramebuffers (1, &fbo);
glBindFramebuffer (GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
g_assert (glCheckFramebufferStatus (GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
*fbo_id = fbo;
*texture_id = texture;
}
static const char vertex_shader_source[] = ""
"#version 150\n"
"in vec2 in_position;\n"
"void main() {\n"
" gl_Position = vec4(in_position, 0.0, 1.0);\n"
"}";
static const char fragment_shader_source[] = ""
"#version 150\n"
"uniform sampler2D source;\n"
"uniform vec2 size;\n"
"out vec4 out_color;\n"
"void main() {\n"
" vec4 in_color = texture(source, gl_FragCoord.xy / size);\n"
" out_color = in_color;\n"
"}";
static unsigned int
create_shader (int type,
const char *src)
{
unsigned int shader;
int status;
shader = glCreateShader (type);
glShaderSource (shader, 1, &src, NULL);
glCompileShader (shader);
glGetShaderiv (shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE)
{
int log_len;
char *buffer;
glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &log_len);
buffer = g_malloc (log_len + 1);
glGetShaderInfoLog (shader, log_len, NULL, buffer);
g_warning ("Compile failure in %s shader:\n%s",
type == GL_VERTEX_SHADER ? "vertex" : "fragment",
buffer);
g_free (buffer);
glDeleteShader (shader);
return 0;
}
return shader;
}
static unsigned int
compile_program (const char *vs,
const char *fs,
unsigned int *source_location,
unsigned int *size_location)
{
unsigned int vertex, fragment, program;
int status;
vertex = create_shader (GL_VERTEX_SHADER, vs);
if (vertex == 0)
return 0;
fragment = create_shader (GL_FRAGMENT_SHADER, fs);
if (fragment == 0)
{
glDeleteShader (vertex);
return 0;
}
program = glCreateProgram ();
glAttachShader (program, vertex);
glAttachShader (program, fragment);
glLinkProgram (program);
glGetProgramiv (program, GL_LINK_STATUS, &status);
if (status == GL_FALSE)
{
int log_len;
char *buffer;
glGetProgramiv (program, GL_INFO_LOG_LENGTH, &log_len);
buffer = g_malloc (log_len + 1);
glGetProgramInfoLog (program, log_len, NULL, buffer);
g_warning ("Linking failure:\n%s", buffer);
g_free (buffer);
glDeleteProgram (program);
program = 0;
goto out;
}
*source_location = glGetUniformLocation (program, "source");
*size_location = glGetUniformLocation (program, "size");
glDetachShader (program, vertex);
glDetachShader (program, fragment);
out:
glDeleteShader (vertex);
glDeleteShader (fragment);
return program;
}
typedef struct {
unsigned int program;
unsigned int source_location;
unsigned int size_location;
} ProgramData;
static unsigned int
get_blit_program (GdkGLContext *context,
unsigned int *source_location,
unsigned int *size_location)
{
ProgramData *data;
data = g_object_get_data (G_OBJECT (context), "dmabuf-blit-program-data");
if (!data)
{
data = g_new0 (ProgramData, 1);
data->program = compile_program (vertex_shader_source,
fragment_shader_source,
&data->source_location,
&data->size_location);
g_object_set_data (G_OBJECT (context), "dmabuf-blit-program-data", data);
}
*source_location = data->source_location;
*size_location = data->size_location;
return data->program;
}
static void
blit_texture (GdkGLContext *context,
int width,
int height,
unsigned int texture,
unsigned int fbo)
{
unsigned int program;
unsigned int source_location;
unsigned int size_location;
unsigned int vao;
unsigned int buffer;
const float vertices[] = {
-1.0f, 1.0f,
-1.0f, -1.0f,
1.0f, 1.0f,
1.0f, -1.0f
};
program = get_blit_program (context, &source_location, &size_location);
glGenVertexArrays (1, &vao);
glBindVertexArray (vao);
glGenBuffers (1, &buffer);
glBindBuffer (GL_ARRAY_BUFFER, buffer);
glBufferData (GL_ARRAY_BUFFER, sizeof (vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer (0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray (0);
glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, texture);
glBindFramebuffer (GL_FRAMEBUFFER, fbo);
glUseProgram (program);
glUniform1i (source_location, texture);
glUniform2f (size_location, width, height);
glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
glUseProgram (0);
glDisableVertexAttribArray (0);
glBindBuffer (GL_ARRAY_BUFFER, 0);
glDeleteBuffers (1, &buffer);
glDeleteVertexArrays (1, &vao);
}
static void
gdk_dmabuf_gl_downloader_download (const GdkDmabufDownloader *downloader,
GdkTexture *texture,
GdkMemoryFormat format,
guchar *data,
gsize stride)
{
GdkDmabufTexture *self = GDK_DMABUF_TEXTURE (texture);
GdkGLContext *context;
unsigned int texture_id;
unsigned int texture_id2;
unsigned int fbo;
int width, height;
int gl_format;
GdkGLTextureBuilder *builder;
GdkTexture *gl_texture;
gpointer sync;
GdkTextureDownloader *tex_downloader;
GDK_DEBUG (DMABUF, "Using gl for downloading a dmabuf");
context = gdk_display_get_gl_context (gdk_dmabuf_texture_get_display (self));
gdk_gl_context_make_current (context);
gl_format = gl_format_for_memory_format (format),
width = gdk_texture_get_width (GDK_TEXTURE (self));
height = gdk_texture_get_height (GDK_TEXTURE (self));
/* 1. import the dmabuf as GL texture */
texture_id = gdk_gl_context_import_dmabuf (context,
width, height,
gdk_dmabuf_texture_get_dmabuf (self));
/* 2. create a texture to render into */
create_render_target (width, height, gl_format, &fbo, &texture_id2);
/* 3. copy from texture_id1 to texture_id2, using a blit shader */
glClear (GL_COLOR_BUFFER_BIT);
blit_texture (context, width, height, texture_id, fbo);
sync = glFenceSync (GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
/* 4. create a GdkGLTexture from the rendered texture */
builder = gdk_gl_texture_builder_new ();
gdk_gl_texture_builder_set_context (builder, context);
gdk_gl_texture_builder_set_id (builder, texture_id2);
gdk_gl_texture_builder_set_width (builder, width);
gdk_gl_texture_builder_set_height (builder, height);
gdk_gl_texture_builder_set_format (builder, format);
gdk_gl_texture_builder_set_sync (builder, sync);
gl_texture = gdk_gl_texture_builder_build (builder, NULL, NULL);
/* 5. download it */
tex_downloader = gdk_texture_downloader_new (gl_texture);
gdk_texture_downloader_set_format (tex_downloader, format);
gdk_texture_downloader_download_into (tex_downloader, data, stride);
gdk_texture_downloader_free (tex_downloader);
g_object_unref (gl_texture);
/* 6. cleanup */
glDeleteFramebuffers (1, &fbo);
glDeleteTextures (1, &texture_id);
glDeleteTextures (1, &texture_id2);
}
const GdkDmabufDownloader *
gdk_dmabuf_get_gl_downloader (void)
{
static const GdkDmabufDownloader downloader = {
gdk_dmabuf_gl_downloader_add_formats,
gdk_dmabuf_gl_downloader_supports,
gdk_dmabuf_gl_downloader_download,
};
return &downloader;
}
#endif /* HAVE_LINUX_DMA_BUF_H && HAVE_EGL */

View File

@@ -39,6 +39,7 @@ struct _GdkDmabufDownloader
};
#ifdef HAVE_LINUX_DMA_BUF_H
const GdkDmabufDownloader *
gdk_dmabuf_get_direct_downloader (void) G_GNUC_CONST;
const GdkDmabufDownloader * gdk_dmabuf_get_direct_downloader (void) G_GNUC_CONST;
const GdkDmabufDownloader * gdk_dmabuf_get_egl_downloader (void) G_GNUC_CONST;
const GdkDmabufDownloader * gdk_dmabuf_get_gl_downloader (void) G_GNUC_CONST;
#endif

View File

@@ -83,6 +83,7 @@
#include "gdkmemorytextureprivate.h"
#include "gdkprofilerprivate.h"
#include "gdkglversionprivate.h"
#include "gdkdmabufformatsprivate.h"
#include "gdkprivate.h"
@@ -95,6 +96,10 @@
#include <epoxy/egl.h>
#endif
#ifdef HAVE_LINUX_DMA_BUF_H
#include <drm/drm_fourcc.h>
#endif
#include <math.h>
#define DEFAULT_ALLOWED_APIS GDK_GL_API_GL | GDK_GL_API_GLES
@@ -109,6 +114,7 @@ typedef struct {
guint has_sync : 1;
guint has_unpack_subimage : 1;
guint has_debug_output : 1;
guint has_image_storage : 1;
guint extensions_checked : 1;
guint debug_enabled : 1;
guint forward_compatible : 1;
@@ -1555,6 +1561,8 @@ gdk_gl_context_check_extensions (GdkGLContext *context)
epoxy_has_gl_extension ("GL_ARB_sync") ||
epoxy_has_gl_extension ("GL_APPLE_sync");
priv->has_image_storage = epoxy_has_gl_extension ("GL_EXT_EGL_image_storage");
#ifdef G_ENABLE_DEBUG
{
int max_texture_size;
@@ -1862,6 +1870,14 @@ gdk_gl_context_has_sync (GdkGLContext *self)
return priv->has_sync;
}
gboolean
gdk_gl_context_has_image_storage (GdkGLContext *self)
{
GdkGLContextPrivate *priv = gdk_gl_context_get_instance_private (self);
return priv->has_image_storage;
}
/* This is currently private! */
/* When using GL/ES, don't flip the 'R' and 'B' bits on Windows/ANGLE for glReadPixels() */
gboolean
@@ -1942,3 +1958,182 @@ gdk_gl_backend_use (GdkGLBackend backend_type)
g_assert (the_gl_backend_type == backend_type);
}
guint
gdk_gl_context_import_dmabuf (GdkGLContext *self,
int width,
int height,
const GdkDmabuf *dmabuf)
{
#if defined(HAVE_EGL) && defined(HAVE_LINUX_DMA_BUF_H)
GdkDisplay *display = gdk_gl_context_get_display (self);
EGLDisplay egl_display = gdk_display_get_egl_display (display);
EGLint attribs[64];
int i;
EGLImage image;
guint texture_id;
g_return_val_if_fail (GDK_IS_GL_CONTEXT (self), 0);
g_return_val_if_fail (width > 0, 0);
g_return_val_if_fail (height > 0, 0);
g_return_val_if_fail (1 <= dmabuf->n_planes && dmabuf->n_planes <= 4, 0);
if (egl_display == EGL_NO_DISPLAY || !display->have_egl_dma_buf_import)
return 0;
GDK_DEBUG (DMABUF,
"Importing dmabuf (format: %.4s:%#lx, planes: %u) into GL",
(char *) &dmabuf->fourcc, dmabuf->modifier, dmabuf->n_planes);
i = 0;
attribs[i++] = EGL_WIDTH;
attribs[i++] = width;
attribs[i++] = EGL_HEIGHT;
attribs[i++] = height;
attribs[i++] = EGL_LINUX_DRM_FOURCC_EXT;
attribs[i++] = dmabuf->fourcc;
#define ADD_PLANE(plane) \
{ \
if (dmabuf->modifier != DRM_FORMAT_MOD_INVALID) \
{ \
attribs[i++] = EGL_DMA_BUF_PLANE## plane ##_MODIFIER_LO_EXT; \
attribs[i++] = dmabuf->modifier & 0xFFFFFFFF; \
attribs[i++] = EGL_DMA_BUF_PLANE## plane ## _MODIFIER_HI_EXT; \
attribs[i++] = dmabuf->modifier >> 32; \
} \
attribs[i++] = EGL_DMA_BUF_PLANE## plane ##_FD_EXT; \
attribs[i++] = dmabuf->planes[plane].fd; \
attribs[i++] = EGL_DMA_BUF_PLANE## plane ##_PITCH_EXT; \
attribs[i++] = dmabuf->planes[plane].stride; \
attribs[i++] = EGL_DMA_BUF_PLANE## plane ##_OFFSET_EXT; \
attribs[i++] = dmabuf->planes[plane].offset; \
}
ADD_PLANE (0);
if (dmabuf->n_planes > 1) ADD_PLANE (1);
if (dmabuf->n_planes > 2) ADD_PLANE (2);
if (dmabuf->n_planes > 3) ADD_PLANE (3);
attribs[i++] = EGL_NONE;
image = eglCreateImageKHR (egl_display,
EGL_NO_CONTEXT,
EGL_LINUX_DMA_BUF_EXT,
(EGLClientBuffer)NULL,
attribs);
if (image == EGL_NO_IMAGE)
{
g_warning ("Creating EGLImage for dmabuf failed: %#x", eglGetError ());
return 0;
}
glGenTextures (1, &texture_id);
glBindTexture (GL_TEXTURE_2D, texture_id);
glEGLImageTargetTexture2DOES (GL_TEXTURE_2D, image);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
eglDestroyImageKHR (egl_display, image);
return texture_id;
#else
return 0;
#endif
}
gboolean
gdk_gl_context_export_dmabuf (GdkGLContext *self,
unsigned int texture_id,
GdkDmabuf *dmabuf)
{
#if defined(HAVE_EGL) && defined(HAVE_LINUX_DMA_BUF_H)
GdkGLContextPrivate *priv = gdk_gl_context_get_instance_private (self);
GdkDisplay *display = gdk_gl_context_get_display (self);
EGLDisplay egl_display = gdk_display_get_egl_display (display);
EGLContext egl_context = priv->egl_context;
EGLint attribs[10];
int i;
EGLImage image;
gboolean result = FALSE;
int fourcc;
int n_planes;
guint64 modifier;
int fds[GDK_DMABUF_MAX_PLANES];
int strides[GDK_DMABUF_MAX_PLANES];
int offsets[GDK_DMABUF_MAX_PLANES];
g_return_val_if_fail (GDK_IS_GL_CONTEXT (self), FALSE);
g_return_val_if_fail (texture_id > 0, FALSE);
g_return_val_if_fail (dmabuf != NULL, FALSE);
if (egl_display == EGL_NO_DISPLAY || !display->have_egl_dma_buf_export)
return 0;
GDK_DEBUG (DMABUF, "Exporting GL texture to dmabuf");
i = 0;
attribs[i++] = EGL_IMAGE_PRESERVED_KHR;
attribs[i++] = EGL_TRUE;
attribs[i++] = EGL_NONE;
image = eglCreateImageKHR (egl_display,
egl_context,
EGL_GL_TEXTURE_2D_KHR,
(EGLClientBuffer)GUINT_TO_POINTER (texture_id),
attribs);
if (image == EGL_NO_IMAGE)
{
g_warning ("Failed to create EGL image");
return FALSE;
}
if (!eglExportDMABUFImageQueryMESA (egl_display,
image,
&fourcc,
&n_planes,
&modifier))
{
g_warning ("eglExportDMABUFImageQueryMESA failed");
goto out;
}
if (!eglExportDMABUFImageMESA (egl_display,
image,
fds,
strides,
offsets))
{
g_warning ("eglExportDMABUFImageMESA failed");
goto out;
}
dmabuf->fourcc = (guint32)fourcc;
dmabuf->modifier = modifier;
dmabuf->n_planes = n_planes;
for (i = 0; i < n_planes; i++)
{
dmabuf->planes[i].fd = fds[i];
dmabuf->planes[i].stride = (int) strides[i];
dmabuf->planes[i].offset = (int) offsets[i];
}
GDK_DEBUG (DMABUF,
"Exported GL texture to dmabuf (format: %.4s:%#lx, planes: %d)",
(char *)&fourcc, modifier, n_planes);
result = TRUE;
out:
eglDestroyImageKHR (egl_display, image);
return result;
#else
return FALSE;
#endif
}

View File

@@ -23,6 +23,7 @@
#include "gdkglcontext.h"
#include "gdkdrawcontextprivate.h"
#include "gdkglversionprivate.h"
#include "gdkdmabufprivate.h"
G_BEGIN_DECLS
@@ -157,7 +158,17 @@ gboolean gdk_gl_context_has_vertex_half_float (GdkGLContext
gboolean gdk_gl_context_has_sync (GdkGLContext *self) G_GNUC_PURE;
gboolean gdk_gl_context_has_image_storage (GdkGLContext *self) G_GNUC_PURE;
double gdk_gl_context_get_scale (GdkGLContext *self);
G_END_DECLS
guint gdk_gl_context_import_dmabuf (GdkGLContext *self,
int width,
int height,
const GdkDmabuf *dmabuf);
gboolean gdk_gl_context_export_dmabuf (GdkGLContext *self,
unsigned int texture_id,
GdkDmabuf *dmabuf);
G_END_DECLS

View File

@@ -18,6 +18,8 @@ gdk_public_sources = files([
'gdkdisplay.c',
'gdkdisplaymanager.c',
'gdkdmabuf.c',
'gdkdmabufgl.c',
'gdkdmabufegl.c',
'gdkdmabufformats.c',
'gdkdmabufformatsbuilder.c',
'gdkdmabuftexture.c',

View File

@@ -44,6 +44,8 @@
#include <gdk/gdktextureprivate.h>
#include <gdk/gdkmemoryformatprivate.h>
#include <gdk/gdkdmabuftextureprivate.h>
G_DEFINE_TYPE (GskGLDriver, gsk_gl_driver, G_TYPE_OBJECT)
@@ -758,7 +760,14 @@ gsk_gl_driver_load_texture (GskGLDriver *self,
return t->texture_id;
}
if (GDK_IS_GL_TEXTURE (texture))
if (GDK_IS_DMABUF_TEXTURE (texture))
{
texture_id = gdk_gl_context_import_dmabuf (context,
gdk_texture_get_width (texture),
gdk_texture_get_height (texture),
gdk_dmabuf_texture_get_dmabuf (GDK_DMABUF_TEXTURE (texture)));
}
else if (GDK_IS_GL_TEXTURE (texture))
{
GdkGLTexture *gl_texture = (GdkGLTexture *) texture;
GdkGLContext *texture_context = gdk_gl_texture_get_context (gl_texture);

View File

@@ -30,6 +30,7 @@
#include <gsk/gskglshaderprivate.h>
#include <gdk/gdktextureprivate.h>
#include <gdk/gdkmemorytextureprivate.h>
#include <gdk/gdkdmabuftexture.h>
#include <gsk/gsktransformprivate.h>
#include <gsk/gskroundedrectprivate.h>
#include <gsk/gskrectprivate.h>
@@ -47,6 +48,7 @@
#include "ninesliceprivate.h"
#include "fp16private.h"
#define ORTHO_NEAR_PLANE -10000
#define ORTHO_FAR_PLANE 10000
#define MAX_GRADIENT_STOPS 6
@@ -3630,16 +3632,12 @@ gsk_gl_render_job_upload_texture (GskGLRenderJob *job,
gboolean ensure_mipmap,
GskGLRenderOffscreen *offscreen)
{
GdkGLTexture *gl_texture = NULL;
if (GDK_IS_GL_TEXTURE (texture))
gl_texture = GDK_GL_TEXTURE (texture);
/* Don't put GL or dmabuf textures into icon caches, they are already on the GPU side */
if (!ensure_mipmap &&
gsk_gl_texture_library_can_cache ((GskGLTextureLibrary *)job->driver->icons_library,
texture->width,
texture->height) &&
!gl_texture)
!(GDK_IS_GL_TEXTURE (texture) || GDK_IS_DMABUF_TEXTURE (texture)))
{
const GskGLIconData *icon_data;
@@ -3653,16 +3651,18 @@ gsk_gl_render_job_upload_texture (GskGLRenderJob *job,
/* Only generate a mipmap if it does not make use reupload
* a GL texture which we could otherwise use directly.
*/
if (gl_texture &&
gdk_gl_context_is_shared (gdk_gl_texture_get_context (gl_texture), job->command_queue->context))
ensure_mipmap = gdk_gl_texture_has_mipmap (gl_texture);
if (GDK_IS_GL_TEXTURE (texture) &&
gdk_gl_context_is_shared (gdk_gl_texture_get_context (GDK_GL_TEXTURE (texture)),
job->command_queue->context))
ensure_mipmap = gdk_gl_texture_has_mipmap (GDK_GL_TEXTURE (texture));
offscreen->texture_id = gsk_gl_driver_load_texture (job->driver, texture, ensure_mipmap);
init_full_texture_region (offscreen);
offscreen->has_mipmap = ensure_mipmap;
if (gl_texture && offscreen->texture_id == gdk_gl_texture_get_id (gl_texture))
offscreen->sync = gdk_gl_texture_get_sync (gl_texture);
if (GDK_IS_GL_TEXTURE (texture) &&
offscreen->texture_id == gdk_gl_texture_get_id (GDK_GL_TEXTURE (texture)))
offscreen->sync = gdk_gl_texture_get_sync (GDK_GL_TEXTURE (texture));
}
}

View File

@@ -125,7 +125,10 @@ gtk_tests = [
]
if os_unix
gtk_tests += [['testfontchooserdialog']]
gtk_tests += [
['testfontchooserdialog'],
['testdmabuf'],
]
endif
if x11_enabled

443
tests/testdmabuf.c Normal file
View File

@@ -0,0 +1,443 @@
#include <gtk/gtk.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/dma-heap.h>
#include <drm/drm_fourcc.h>
/* For this to work, you may need to give /dev/dma_heap/system
* lax permissions.
*/
static int
allocate_dma_buf (gsize size)
{
static int fd = -1;
struct dma_heap_allocation_data heap_data;
int ret;
if (fd == -1)
{
fd = open ("/dev/dma_heap/system", O_RDONLY | O_CLOEXEC);
if (fd == -1)
g_error ("Failed to open /dev/dma_heap/system");
}
heap_data.len = size;
heap_data.fd = 0;
heap_data.fd_flags = O_RDWR | O_CLOEXEC;
heap_data.heap_flags = 0;
ret = ioctl (fd, DMA_HEAP_IOCTL_ALLOC, &heap_data);
if (ret)
g_error ("dma-buf allocation failed");
return heap_data.fd;
}
static void
populate_dma_buf (int fd,
guchar *data,
gsize size)
{
guchar *buf;
buf = mmap (NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
memcpy (buf, data, size);
munmap (buf, size);
}
/* The YUV conversion code is adapted from weston/tests/yuv-buffer-test.c */
/*
* Based on Rec. ITU-R BT.601-7
*
* This is intended to be obvious and accurate, not fast.
*/
static void
x8r8g8b8_to_ycbcr8_bt601 (guint32 xrgb,
guchar *y_out,
guchar *cb_out,
guchar *cr_out)
{
double y, cb, cr;
double r = (xrgb >> 16) & 0xff;
double g = (xrgb >> 8) & 0xff;
double b = (xrgb >> 0) & 0xff;
/* normalize to [0.0, 1.0] */
r /= 255.0;
g /= 255.0;
b /= 255.0;
/* Y normalized to [0.0, 1.0], Cb and Cr [-0.5, 0.5] */
y = 0.299 * r + 0.587 * g + 0.114 * b;
cr = (r - y) / 1.402;
cb = (b - y) / 1.772;
/* limited range quantization to 8 bit */
*y_out = round(219.0 * y + 16.0);
if (cr_out)
*cr_out = round(224.0 * cr + 128.0);
if (cb_out)
*cb_out = round(224.0 * cb + 128.0);
}
/*
* 3 plane YCbCr
* plane 0: Y plane, [7:0] Y
* plane 1: Cb plane, [7:0] Cb
* plane 2: Cr plane, [7:0] Cr
* YUV420: 2x2 subsampled Cb (1) and Cr (2) planes
* YUV444: no subsampling
*/
static guchar *
y_u_v_create_buffer (uint32_t drm_format,
guchar *rgb_data,
int rgb_width,
int rgb_height,
int *size,
int *u_offset,
int *v_offset)
{
gsize bytes;
int x, y;
guint32 *rgb_row;
guchar *y_base;
guchar *u_base;
guchar *v_base;
guchar *y_row;
guchar *u_row;
guchar *v_row;
guint32 argb;
int sub = (drm_format == DRM_FORMAT_YUV420) ? 2 : 1;
guchar *buf;
g_assert (drm_format == DRM_FORMAT_YUV420 ||
drm_format == DRM_FORMAT_YUV444);
/* Full size Y plus quarter U and V */
bytes = rgb_width * rgb_height +
(rgb_width / sub) * (rgb_height / sub) * 2;
buf = g_new (guchar, bytes);
*size = bytes;
*u_offset = rgb_width * rgb_height;
*v_offset = *u_offset + (rgb_width / sub) * (rgb_height / sub);
y_base = buf;
u_base = y_base + rgb_width * rgb_height;
v_base = u_base + (rgb_width / sub) * (rgb_height / sub);
for (y = 0; y < rgb_height; y++)
{
rgb_row = (guint32 *) (rgb_data + y * 4 * rgb_width);
y_row = y_base + y * rgb_width;
u_row = u_base + (y / sub) * (rgb_width / sub);
v_row = v_base + (y / sub) * (rgb_width / sub);
for (x = 0; x < rgb_width; x++)
{
/*
* Sub-sample the source image instead, so that U and V
* sub-sampling does not require proper
* filtering/averaging/siting.
*/
argb = *(rgb_row + x / 2 * 2);
/*
* A stupid way of "sub-sampling" chroma. This does not
* do the necessary filtering/averaging/siting or
* alternate Cb/Cr rows.
*/
if ((y & (sub - 1)) == 0 && (x & (sub - 1)) == 0)
x8r8g8b8_to_ycbcr8_bt601 (argb, y_row + x, u_row + x / sub, v_row + x / sub);
else
x8r8g8b8_to_ycbcr8_bt601 (argb, y_row + x, NULL, NULL);
}
}
return buf;
}
/*
* 2 plane YCbCr
* plane 0 = Y plane, [7:0] Y
* plane 1 = Cr:Cb plane, [15:0] Cr:Cb little endian
* 2x2 subsampled Cr:Cb plane
*/
static guchar *
nv12_create_buffer (uint32_t drm_format,
guchar *rgb_data,
int rgb_width,
int rgb_height,
int *size,
int *uv_offset)
{
size_t bytes;
int x, y;
uint32_t *rgb_row;
uint8_t *y_base;
uint16_t *uv_base;
uint8_t *y_row;
uint16_t *uv_row;
uint32_t argb;
uint8_t cr;
uint8_t cb;
guchar *buf;
g_assert (drm_format == DRM_FORMAT_NV12);
/* Full size Y, quarter UV */
bytes = rgb_width * rgb_height +
(rgb_width / 2) * (rgb_height / 2) * sizeof (uint16_t);
buf = g_new0 (guchar, bytes);
*uv_offset = rgb_width * rgb_height;
y_base = buf;
uv_base = (uint16_t *)(y_base + rgb_width * rgb_height);
for (y = 0; y < rgb_height; y++)
{
rgb_row = (uint32_t *) (rgb_data + y * 4 * rgb_width);
y_row = y_base + y * rgb_width;
uv_row = (uint16_t *) (uv_base + (y / 2) * (rgb_width / 2));
for (x = 0; x < rgb_width; x++)
{
/*
* Sub-sample the source image instead, so that U and V
* sub-sampling does not require proper
* filtering/averaging/siting.
*/
argb = *(rgb_row + x / 2 * 2);
/*
* A stupid way of "sub-sampling" chroma. This does not
* do the necessary filtering/averaging/siting.
*/
if ((y & 1) == 0 && (x & 1) == 0)
{
x8r8g8b8_to_ycbcr8_bt601(argb, y_row + x, &cb, &cr);
*(uv_row + x / 2) = ((uint16_t)cr << 8) | cb;
}
else
{
x8r8g8b8_to_ycbcr8_bt601(argb, y_row + x, NULL, NULL);
}
}
}
return buf;
}
static GdkTexture *
make_dmabuf_texture (const char *filename,
guint32 format)
{
GdkTexture *texture;
int width, height;
gsize rgb_stride, rgb_size;
guchar *rgb_data;
int fd;
GdkDmabufTextureBuilder *builder;
GError *error = NULL;
texture = gdk_texture_new_from_filename (filename, NULL);
width = gdk_texture_get_width (texture);
height = gdk_texture_get_height (texture);
rgb_stride = 4 * width;
rgb_size = rgb_stride * height;
rgb_data = g_new0 (guchar, rgb_size);
gdk_texture_download (texture, rgb_data, rgb_stride);
g_object_unref (texture);
builder = gdk_dmabuf_texture_builder_new ();
gdk_dmabuf_texture_builder_set_display (builder, gdk_display_get_default ());
gdk_dmabuf_texture_builder_set_width (builder, width);
gdk_dmabuf_texture_builder_set_height (builder, height);
gdk_dmabuf_texture_builder_set_fourcc (builder, format);
gdk_dmabuf_texture_builder_set_modifier (builder, DRM_FORMAT_MOD_LINEAR);
if (format == DRM_FORMAT_XRGB8888)
{
gdk_dmabuf_texture_builder_set_n_planes (builder, 1);
fd = allocate_dma_buf (rgb_size);
populate_dma_buf (fd, rgb_data, rgb_size);
gdk_dmabuf_texture_builder_set_fd (builder, 0, fd);
gdk_dmabuf_texture_builder_set_stride (builder, 0, rgb_stride);
}
else if (format == DRM_FORMAT_YUV420)
{
guchar *buf;
int size, u_offset, v_offset;
gdk_dmabuf_texture_builder_set_n_planes (builder, 3);
buf = y_u_v_create_buffer (format, rgb_data, width, height, &size, &u_offset, &v_offset);
fd = allocate_dma_buf (width * height);
populate_dma_buf (fd, buf, width * height);
gdk_dmabuf_texture_builder_set_fd (builder, 0, fd);
gdk_dmabuf_texture_builder_set_stride (builder, 0, width);
gdk_dmabuf_texture_builder_set_offset (builder, 0, 0);
fd = allocate_dma_buf ((width / 2) * (height / 2));
populate_dma_buf (fd, buf + u_offset, (width / 2) * (height / 2));
gdk_dmabuf_texture_builder_set_fd (builder, 1, fd);
gdk_dmabuf_texture_builder_set_stride (builder, 1, width / 2);
gdk_dmabuf_texture_builder_set_offset (builder, 1, 0);
fd = allocate_dma_buf ((width / 2) * (height / 2));
populate_dma_buf (fd, buf + v_offset, (width / 2) * (height / 2));
gdk_dmabuf_texture_builder_set_fd (builder, 2, fd);
gdk_dmabuf_texture_builder_set_stride (builder, 2, width / 2);
gdk_dmabuf_texture_builder_set_offset (builder, 2, 0);
g_free (buf);
}
else if (format == DRM_FORMAT_NV12)
{
guchar *buf;
int size, uv_offset;
gdk_dmabuf_texture_builder_set_n_planes (builder, 2);
buf = nv12_create_buffer (format, rgb_data, width, height, &size, &uv_offset);
fd = allocate_dma_buf (width * height);
populate_dma_buf (fd, buf, width * height);
gdk_dmabuf_texture_builder_set_fd (builder, 0, fd);
gdk_dmabuf_texture_builder_set_stride (builder, 0, width);
gdk_dmabuf_texture_builder_set_offset (builder, 0, 0);
fd = allocate_dma_buf ((width / 2) * (height / 2) * sizeof (uint16_t));
populate_dma_buf (fd, buf + uv_offset, (width / 2) * (height / 2) * sizeof (uint16_t));
gdk_dmabuf_texture_builder_set_fd (builder, 1, fd);
gdk_dmabuf_texture_builder_set_stride (builder, 1, width);
gdk_dmabuf_texture_builder_set_offset (builder, 1, 0);
g_free (buf);
}
g_free (rgb_data);
texture = gdk_dmabuf_texture_builder_build (builder, NULL, NULL, &error);
if (!texture)
g_error ("Failed to create dmabuf texture: %s", error->message);
g_object_unref (builder);
return texture;
}
static guint32 supported_formats[] = {
DRM_FORMAT_XRGB8888,
DRM_FORMAT_YUV420,
DRM_FORMAT_NV12,
};
static gboolean
format_is_supported (guint32 fmt)
{
for (int i = 0; i < G_N_ELEMENTS (supported_formats); i++)
{
if (supported_formats[i] == fmt)
return TRUE;
}
return FALSE;
}
static char *
supported_formats_to_string (void)
{
GString *s;
s = g_string_new ("");
for (int i = 0; i < G_N_ELEMENTS (supported_formats); i++)
{
if (s->len)
g_string_append (s, ", ");
g_string_append_printf (s, "%.4s", (char *)&supported_formats[i]);
}
return g_string_free (s, FALSE);
}
G_GNUC_NORETURN
static void
usage (void)
{
char *formats = supported_formats_to_string ();
g_print ("Usage: testdmabuf FORMAT FILE\n"
"Supported formats: %s\n", formats);
g_free (formats);
exit (1);
}
static guint32
parse_format (const char *a)
{
if (strlen (a) == 4)
{
guint32 format = fourcc_code (a[0], a[1], a[2], a[3]);
if (format_is_supported (format))
return format;
}
usage ();
return 0;
}
int
main (int argc, char *argv[])
{
GdkTexture *texture;
GtkWidget *window, *picture;
char *filename;
guint32 format;
if (argc != 3)
usage ();
format = parse_format (argv[1]);
filename = argv[2];
gtk_init ();
/* Get the list of supported formats with GDK_DEBUG=opengl */
gdk_display_get_dmabuf_formats (gdk_display_get_default ());
texture = make_dmabuf_texture (filename, format);
gdk_texture_save_to_png (texture, "testdmabuf.out.png");
window = gtk_window_new ();
picture = gtk_picture_new_for_paintable (GDK_PAINTABLE (texture));
gtk_window_set_child (GTK_WINDOW (window), picture);
gtk_window_present (GTK_WINDOW (window));
while (g_list_model_get_n_items (gtk_window_get_toplevels ()) > 0)
g_main_context_iteration (NULL, TRUE);
return 0;
}

View File

@@ -0,0 +1,292 @@
#include <gtk/gtk.h>
#include <epoxy/egl.h>
#include <gdk/gdkdisplayprivate.h>
#include <gdk/gdkglcontextprivate.h>
#include <gdk/gdkdmabuftextureprivate.h>
#include <drm/drm_fourcc.h>
static void
test_dmabuf_formats (void)
{
GdkDisplay *display;
GdkDmabufFormats *formats;
display = gdk_display_get_default ();
formats = gdk_display_get_dmabuf_formats (display);
/* We always have basic linear formats */
g_assert_true (gdk_dmabuf_formats_get_n_formats (formats) >= 6);
g_assert_true (gdk_dmabuf_formats_contains (formats, DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_LINEAR));
g_assert_true (gdk_dmabuf_formats_contains (formats, DRM_FORMAT_RGBA8888, DRM_FORMAT_MOD_LINEAR));
g_assert_true (gdk_dmabuf_formats_contains (formats, DRM_FORMAT_BGRA8888, DRM_FORMAT_MOD_LINEAR));
g_assert_true (gdk_dmabuf_formats_contains (formats, DRM_FORMAT_ABGR16161616F, DRM_FORMAT_MOD_LINEAR));
g_assert_true (gdk_dmabuf_formats_contains (formats, DRM_FORMAT_RGB888, DRM_FORMAT_MOD_LINEAR));
g_assert_true (gdk_dmabuf_formats_contains (formats, DRM_FORMAT_BGR888, DRM_FORMAT_MOD_LINEAR));
}
static cairo_surface_t *
make_surface (int width,
int height)
{
cairo_surface_t *surface;
cairo_t *cr;
guchar *data;
int stride;
stride = width * 4;
data = g_malloc (stride * height);
surface = cairo_image_surface_create_for_data (data,
CAIRO_FORMAT_ARGB32,
width, height, stride);
cr = cairo_create (surface);
cairo_set_source_rgb (cr, 1, 0, 0);
cairo_paint (cr);
cairo_destroy (cr);
return surface;
}
static unsigned int
upload_gl_texture (GdkGLContext *context,
cairo_surface_t *surface)
{
unsigned int id;
int width, height;
width = cairo_image_surface_get_width (surface);
height = cairo_image_surface_get_height (surface);
glGenTextures (1, &id);
glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, id);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE,
cairo_image_surface_get_data (surface));
g_assert_true (glGetError () == GL_NO_ERROR);
return id;
}
static void
export_dmabuf (GdkGLContext *context,
unsigned int texture_id,
GdkDmabuf *dmabuf)
{
gboolean ret;
ret = gdk_gl_context_export_dmabuf (context, texture_id, dmabuf);
g_assert_true (ret);
}
static void
free_dmabuf (gpointer data)
{
GdkDmabuf *dmabuf = data;
for (int i = 0; i < dmabuf->n_planes; i++)
close (dmabuf->planes[i].fd);
g_free (dmabuf);
}
static GdkTexture *
make_dmabuf_texture (GdkDisplay *display,
int width,
int height,
gboolean premultiplied,
GdkDmabuf *dmabuf)
{
GdkDmabufTextureBuilder *builder;
GdkTexture *texture;
GdkDmabuf *dmabuf2;
builder = gdk_dmabuf_texture_builder_new ();
gdk_dmabuf_texture_builder_set_display (builder, display);
gdk_dmabuf_texture_builder_set_width (builder, width);
gdk_dmabuf_texture_builder_set_premultiplied (builder, premultiplied);
gdk_dmabuf_texture_builder_set_height (builder, height);
gdk_dmabuf_texture_builder_set_fourcc (builder, dmabuf->fourcc);
gdk_dmabuf_texture_builder_set_modifier (builder, dmabuf->modifier);
gdk_dmabuf_texture_builder_set_n_planes (builder, dmabuf->n_planes);
for (int i = 0; i < dmabuf->n_planes; i++)
{
gdk_dmabuf_texture_builder_set_fd (builder, i, dmabuf->planes[i].fd);
gdk_dmabuf_texture_builder_set_stride (builder, i, dmabuf->planes[i].stride);
gdk_dmabuf_texture_builder_set_offset (builder, i, dmabuf->planes[i].offset);
}
dmabuf2 = g_new (GdkDmabuf, 1);
memcpy (dmabuf2, dmabuf, sizeof (GdkDmabuf));
texture = gdk_dmabuf_texture_builder_build (builder, free_dmabuf, dmabuf2, NULL);
g_assert_true (texture != NULL);
g_object_unref (builder);
return texture;
}
/* Make a dmabuftexture by exporting a GL texture,
* then download it and compare with the original
*/
static void
test_dmabuf_export (void)
{
GdkDisplay *display;
GdkGLContext *context;
EGLDisplay egl_display;
GError *error = NULL;
cairo_surface_t *surface;
unsigned int texture_id;
GdkDmabuf dmabuf;
GdkTexture *texture;
guchar *data;
display = gdk_display_get_default ();
if (!gdk_display_prepare_gl (display, &error))
{
g_test_skip_printf ("no GL support: %s", error->message);
g_clear_error (&error);
return;
}
egl_display = gdk_display_get_egl_display (display);
if (egl_display == EGL_NO_DISPLAY)
{
g_test_skip ("no EGL support");
return;
}
context = gdk_display_create_gl_context (display, &error);
g_assert_nonnull (context);
g_assert_no_error (error);
gdk_gl_context_realize (context, &error);
g_assert_no_error (error);
surface = make_surface (64, 64);
gdk_gl_context_make_current (context);
texture_id = upload_gl_texture (context, surface);
export_dmabuf (context, texture_id, &dmabuf);
texture = make_dmabuf_texture (display, 64, 64, TRUE, &dmabuf);
data = g_malloc (64 * 64 * 4);
gdk_texture_download (texture, data, 64 * 4);
g_assert_true (memcmp (cairo_image_surface_get_data (surface), data, 64 * 64 * 4) == 0);
g_free (data);
g_object_unref (texture);
cairo_surface_destroy (surface);
gdk_gl_context_make_current (context);
glDeleteTextures (1, &texture_id);
g_object_unref (context);
}
/* Make a dmabuftexture by exporting a GL texture,
* then import it into another GL context, download
* the resulting texture, and compare it to the original.
*/
static void
test_dmabuf_import (void)
{
GdkDisplay *display;
GdkGLContext *context;
GdkGLContext *context2;
EGLDisplay egl_display;
GError *error = NULL;
cairo_surface_t *surface;
unsigned int texture_id;
unsigned int texture_id2;
GdkDmabuf dmabuf;
const GdkDmabuf *dmabuf2;
GdkTexture *texture;
GdkTexture *texture2;
GdkGLTextureBuilder *builder;
guchar *data;
display = gdk_display_get_default ();
if (!gdk_display_prepare_gl (display, &error))
{
g_test_skip_printf ("no GL support: %s", error->message);
g_clear_error (&error);
return;
}
egl_display = gdk_display_get_egl_display (display);
if (egl_display == EGL_NO_DISPLAY)
{
g_test_skip ("no EGL support");
return;
}
context = gdk_display_create_gl_context (display, &error);
g_assert_nonnull (context);
g_assert_no_error (error);
gdk_gl_context_realize (context, &error);
g_assert_no_error (error);
surface = make_surface (64, 64);
gdk_gl_context_make_current (context);
texture_id = upload_gl_texture (context, surface);
export_dmabuf (context, texture_id, &dmabuf);
texture = make_dmabuf_texture (display, 64, 64, TRUE, &dmabuf);
context2 = gdk_display_create_gl_context (display, &error);
g_assert_nonnull (context2);
g_assert_no_error (error);
gdk_gl_context_realize (context2, &error);
g_assert_no_error (error);
dmabuf2 = gdk_dmabuf_texture_get_dmabuf (GDK_DMABUF_TEXTURE (texture));
texture_id2 = gdk_gl_context_import_dmabuf (context2, 64, 64, dmabuf2);
builder = gdk_gl_texture_builder_new ();
gdk_gl_texture_builder_set_context (builder, context2);
gdk_gl_texture_builder_set_id (builder, texture_id2);
gdk_gl_texture_builder_set_width (builder, 64);
gdk_gl_texture_builder_set_height (builder, 64);
gdk_gl_texture_builder_set_format (builder, GDK_MEMORY_A8R8G8B8_PREMULTIPLIED);
texture2 = gdk_gl_texture_builder_build (builder, NULL, NULL);
data = g_malloc (64 * 64 * 4);
gdk_texture_download (texture2, data, 64 * 4);
g_assert_true (memcmp (cairo_image_surface_get_data (surface), data, 64 * 64 * 4) == 0);
g_free (data);
g_object_unref (texture);
g_object_unref (texture2);
gdk_gl_context_make_current (context);
glDeleteTextures (1, &texture_id);
g_object_unref (context);
g_object_unref (context2);
}
int
main (int argc, char *argv[])
{
gtk_test_init (&argc, &argv, NULL);
g_test_add_func ("/dmabuf/formats", test_dmabuf_formats);
g_test_add_func ("/dmabuf/export", test_dmabuf_export);
g_test_add_func ("/dmabuf/import", test_dmabuf_import);
return g_test_run ();
}

View File

@@ -55,6 +55,7 @@ internal_tests = [
'image',
'texture',
'gltexture',
'dmabuftexture',
]
foreach t : internal_tests