backend/cogl-utils: Move, rename to cogl-drm-formats and clean up

1. Move into the new 'common' folder and build for Wayland as well
   so we will be able to share the code in follow-up commits.

2. Rename to cogl-drm-formats to make it more obvious that the format
   map is more than an utility these days.

3. Drop the unused CoglTextureComponents part (see also previous
   commit).

4. Move the map to the header, simplifying some future use-cases.

5. Sync formats with MetaWaylandBuffer and MetaWaylandDmaBufBuffer and
   also use newly introduced opaque formats where appropriate.
   This avoids duplicated code, ensures that new drm-formats added to
   the dmabuf protocol have an adequate representation in Cogl from which
   information like alpha support can be easily derived and finally
   ensures we don't crash if the mappings got out of sync.

6. Remove some likely untested formats. In case some of these are
   actually needed on certain hardware, we can test whether we got
   the correct mapping by also adding support for the corresponding
   wl_shm_format in MetaWaylandBuffer by extending the gradient test in
   https://gitlab.freedesktop.org/jadahl/wayland-test-clients

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3065>
This commit is contained in:
Robert Mader 2023-06-09 08:03:53 +02:00 committed by Marge Bot
parent ae88e13c5a
commit 192d6686cf
8 changed files with 127 additions and 126 deletions

View File

@ -244,7 +244,6 @@ libsystemd_dep = dependency('libsystemd', required: have_libsystemd)
have_native_backend = get_option('native_backend')
if have_native_backend
libdrm_dep = dependency('libdrm')
libgbm_dep = dependency('gbm', version: gbm_req)
libinput_dep = dependency('libinput', version: libinput_req)
@ -267,6 +266,10 @@ if have_native_backend
endif
endif
if have_wayland or have_native_backend
libdrm_dep = dependency('libdrm')
endif
have_egl_device = get_option('egl_device')
have_wayland_eglstream = get_option('wayland_eglstream')

View File

@ -1,93 +0,0 @@
/* meta-cogl-utils.c
*
* Copyright 2020 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
*
* 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.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#include "backends/native/meta-cogl-utils.h"
#include <drm_fourcc.h>
typedef struct _PixelFormatMap {
uint32_t drm_format;
CoglPixelFormat cogl_format;
CoglTextureComponents cogl_components;
} PixelFormatMap;
static const PixelFormatMap pixel_format_map[] = {
/* DRM formats are defined as little-endian, not machine endian. */
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
{ DRM_FORMAT_RGB565, COGL_PIXEL_FORMAT_RGB_565, COGL_TEXTURE_COMPONENTS_RGB },
{ DRM_FORMAT_ABGR8888, COGL_PIXEL_FORMAT_RGBA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
{ DRM_FORMAT_XBGR8888, COGL_PIXEL_FORMAT_RGBA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB },
{ DRM_FORMAT_ARGB8888, COGL_PIXEL_FORMAT_BGRA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
{ DRM_FORMAT_XRGB8888, COGL_PIXEL_FORMAT_BGRA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB },
{ DRM_FORMAT_BGRA8888, COGL_PIXEL_FORMAT_ARGB_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
{ DRM_FORMAT_BGRX8888, COGL_PIXEL_FORMAT_ARGB_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB },
{ DRM_FORMAT_RGBA8888, COGL_PIXEL_FORMAT_ABGR_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
{ DRM_FORMAT_RGBX8888, COGL_PIXEL_FORMAT_ABGR_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB },
{ DRM_FORMAT_ABGR2101010, COGL_PIXEL_FORMAT_RGBA_1010102_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
{ DRM_FORMAT_XBGR2101010, COGL_PIXEL_FORMAT_RGBA_1010102_PRE, COGL_TEXTURE_COMPONENTS_RGB },
{ DRM_FORMAT_ARGB2101010, COGL_PIXEL_FORMAT_BGRA_1010102_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
{ DRM_FORMAT_XRGB2101010, COGL_PIXEL_FORMAT_BGRA_1010102_PRE, COGL_TEXTURE_COMPONENTS_RGB },
{ DRM_FORMAT_BGRA1010102, COGL_PIXEL_FORMAT_ARGB_2101010_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
{ DRM_FORMAT_BGRX1010102, COGL_PIXEL_FORMAT_ARGB_2101010_PRE, COGL_TEXTURE_COMPONENTS_RGB },
{ DRM_FORMAT_RGBA1010102, COGL_PIXEL_FORMAT_ABGR_2101010_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
{ DRM_FORMAT_RGBX1010102, COGL_PIXEL_FORMAT_ABGR_2101010_PRE, COGL_TEXTURE_COMPONENTS_RGB },
#elif G_BYTE_ORDER == G_BIG_ENDIAN
/* DRM_FORMAT_RGB565 cannot be expressed. */
{ DRM_FORMAT_ABGR8888, COGL_PIXEL_FORMAT_ABGR_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
{ DRM_FORMAT_XBGR8888, COGL_PIXEL_FORMAT_ABGR_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB },
{ DRM_FORMAT_ARGB8888, COGL_PIXEL_FORMAT_ARGB_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
{ DRM_FORMAT_XRGB8888, COGL_PIXEL_FORMAT_ARGB_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB },
{ DRM_FORMAT_BGRA8888, COGL_PIXEL_FORMAT_BGRA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
{ DRM_FORMAT_BGRX8888, COGL_PIXEL_FORMAT_BGRA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB },
{ DRM_FORMAT_RGBA8888, COGL_PIXEL_FORMAT_RGBA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
{ DRM_FORMAT_RGBX8888, COGL_PIXEL_FORMAT_RGBA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB },
#else
#error "unexpected G_BYTE_ORDER"
#endif
};
gboolean
meta_cogl_pixel_format_from_drm_format (uint32_t drm_format,
CoglPixelFormat *out_format,
CoglTextureComponents *out_components)
{
const size_t n = G_N_ELEMENTS (pixel_format_map);
size_t i;
for (i = 0; i < n; i++)
{
if (pixel_format_map[i].drm_format == drm_format)
break;
}
if (i == n)
return FALSE;
if (out_format)
*out_format = pixel_format_map[i].cogl_format;
if (out_components)
*out_components = pixel_format_map[i].cogl_components;
return TRUE;
}

View File

@ -32,9 +32,9 @@
#include <xf86drmMode.h>
#include "backends/meta-backend-private.h"
#include "backends/native/meta-cogl-utils.h"
#include "backends/native/meta-device-pool.h"
#include "backends/native/meta-drm-buffer-private.h"
#include "common/meta-cogl-drm-formats.h"
struct _MetaDrmBufferGbm
{
@ -279,9 +279,7 @@ meta_drm_buffer_gbm_blit_to_framebuffer (CoglScanout *scanout,
}
drm_format = gbm_bo_get_format (buffer_gbm->bo);
result = meta_cogl_pixel_format_from_drm_format (drm_format,
&cogl_format,
NULL);
result = meta_cogl_pixel_format_from_drm_format (drm_format, &cogl_format);
g_assert (result);
width = gbm_bo_get_width (buffer_gbm->bo);

View File

@ -32,7 +32,6 @@
#include <drm_fourcc.h>
#include "backends/meta-egl-ext.h"
#include "backends/native/meta-cogl-utils.h"
#include "backends/native/meta-crtc-kms.h"
#include "backends/native/meta-device-pool.h"
#include "backends/native/meta-drm-buffer-dumb.h"
@ -48,6 +47,7 @@
#include "backends/native/meta-render-device.h"
#include "backends/native/meta-renderer-native-gles3.h"
#include "backends/native/meta-renderer-native-private.h"
#include "common/meta-cogl-drm-formats.h"
typedef enum _MetaSharedFramebufferImportStatus
{
@ -746,9 +746,7 @@ copy_shared_framebuffer_primary_gpu (CoglOnscreen *onscre
g_assert (cogl_framebuffer_get_width (framebuffer) == width);
g_assert (cogl_framebuffer_get_height (framebuffer) == height);
ret = meta_cogl_pixel_format_from_drm_format (drm_format,
&cogl_format,
NULL);
ret = meta_cogl_pixel_format_from_drm_format (drm_format, &cogl_format);
g_assert (ret);
dmabuf_fd = meta_drm_buffer_dumb_ensure_dmabuf_fd (buffer_dumb, &error);
@ -850,9 +848,7 @@ copy_shared_framebuffer_cpu (CoglOnscreen *onscreen,
g_assert (cogl_framebuffer_get_width (framebuffer) == width);
g_assert (cogl_framebuffer_get_height (framebuffer) == height);
ret = meta_cogl_pixel_format_from_drm_format (drm_format,
&cogl_format,
NULL);
ret = meta_cogl_pixel_format_from_drm_format (drm_format, &cogl_format);
g_assert (ret);
dumb_bitmap = cogl_bitmap_new_for_data (cogl_context,
@ -2115,7 +2111,6 @@ pick_secondary_gpu_framebuffer_format_for_cpu (CoglOnscreen *onscreen)
for (k = 0; k < G_N_ELEMENTS (preferred_formats); k++)
{
g_assert (meta_cogl_pixel_format_from_drm_format (preferred_formats[k],
NULL,
NULL));
for (i = 0; i < formats->len; i++)
@ -2136,7 +2131,7 @@ pick_secondary_gpu_framebuffer_format_for_cpu (CoglOnscreen *onscreen)
{
drm_format = g_array_index (formats, uint32_t, i);
if (meta_cogl_pixel_format_from_drm_format (drm_format, NULL, NULL))
if (meta_cogl_pixel_format_from_drm_format (drm_format, NULL))
return drm_format;
}

View File

@ -51,7 +51,6 @@
#include "backends/meta-logical-monitor.h"
#include "backends/native/meta-backend-native-private.h"
#include "backends/native/meta-cursor-renderer-native.h"
#include "backends/native/meta-cogl-utils.h"
#include "backends/native/meta-crtc-kms.h"
#include "backends/native/meta-crtc-virtual.h"
#include "backends/native/meta-device-pool.h"
@ -65,6 +64,7 @@
#include "backends/native/meta-renderer-native-private.h"
#include "backends/native/meta-renderer-view-native.h"
#include "cogl/cogl.h"
#include "common/meta-cogl-drm-formats.h"
#include "core/boxes-private.h"
#ifdef HAVE_EGL_DEVICE
@ -615,9 +615,7 @@ meta_renderer_native_create_dma_buf_framebuffer (MetaRendererNative *renderer_n
CoglOffscreen *cogl_fbo;
int ret;
ret = meta_cogl_pixel_format_from_drm_format (drm_format,
&cogl_format,
NULL);
ret = meta_cogl_pixel_format_from_drm_format (drm_format, &cogl_format);
g_assert (ret);
strides[0] = stride;

View File

@ -1,6 +1,7 @@
/* meta-cogl-utils.h
/* meta-cogl-drm-formats.c
*
* Copyright 2020 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
* Copyright (C) 2020 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
* Copyright (C) 2023 Collabora Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@ -21,18 +22,28 @@
*
*/
#ifndef META_COGL_UTILS_H
#define META_COGL_UTILS_H
#include "config.h"
#include "cogl/cogl.h"
G_BEGIN_DECLS
#include "common/meta-cogl-drm-formats.h"
gboolean
meta_cogl_pixel_format_from_drm_format (uint32_t drm_format,
CoglPixelFormat *out_format,
CoglTextureComponents *out_components);
meta_cogl_pixel_format_from_drm_format (uint32_t drm_format,
CoglPixelFormat *out_format)
{
const size_t n = G_N_ELEMENTS (meta_cogl_drm_format_map);
size_t i;
G_END_DECLS
for (i = 0; i < n; i++)
{
if (meta_cogl_drm_format_map[i].drm_format == drm_format)
break;
}
#endif /* META_COGL_UTILS_H */
if (i == n)
return FALSE;
if (out_format)
*out_format = meta_cogl_drm_format_map[i].cogl_format;
return TRUE;
}

View File

@ -0,0 +1,79 @@
/* meta-cogl-drm-formats.h
*
* Copyright (C) 2020 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
* Copyright (C) 2023 Collabora 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.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#ifndef META_COGL_DRM_FORMATS_H
#define META_COGL_DRM_FORMATS_H
#include <drm_fourcc.h>
#include "cogl/cogl.h"
G_BEGIN_DECLS
typedef struct _CoglDrmFormatMap
{
uint32_t drm_format;
CoglPixelFormat cogl_format;
} CoglDrmFormatMap;
static const CoglDrmFormatMap meta_cogl_drm_format_map[] = {
/* DRM formats are defined as little-endian, not machine endian. */
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
{ DRM_FORMAT_RGB565, COGL_PIXEL_FORMAT_RGB_565 },
{ DRM_FORMAT_RGBX8888, COGL_PIXEL_FORMAT_XBGR_8888 },
{ DRM_FORMAT_RGBA8888, COGL_PIXEL_FORMAT_ABGR_8888_PRE },
{ DRM_FORMAT_BGRX8888, COGL_PIXEL_FORMAT_XRGB_8888 },
{ DRM_FORMAT_BGRA8888, COGL_PIXEL_FORMAT_ARGB_8888_PRE },
{ DRM_FORMAT_XRGB8888, COGL_PIXEL_FORMAT_BGRX_8888 },
{ DRM_FORMAT_ARGB8888, COGL_PIXEL_FORMAT_BGRA_8888_PRE },
{ DRM_FORMAT_XBGR8888, COGL_PIXEL_FORMAT_RGBX_8888 },
{ DRM_FORMAT_ABGR8888, COGL_PIXEL_FORMAT_RGBA_8888_PRE },
{ DRM_FORMAT_XRGB2101010, COGL_PIXEL_FORMAT_XRGB_2101010 },
{ DRM_FORMAT_ARGB2101010, COGL_PIXEL_FORMAT_ARGB_2101010_PRE },
{ DRM_FORMAT_XBGR2101010, COGL_PIXEL_FORMAT_XBGR_2101010 },
{ DRM_FORMAT_ABGR2101010, COGL_PIXEL_FORMAT_ABGR_2101010_PRE },
{ DRM_FORMAT_XRGB16161616F, COGL_PIXEL_FORMAT_BGRX_FP_16161616 },
{ DRM_FORMAT_ARGB16161616F, COGL_PIXEL_FORMAT_BGRA_FP_16161616_PRE },
{ DRM_FORMAT_XBGR16161616F, COGL_PIXEL_FORMAT_RGBX_FP_16161616 },
{ DRM_FORMAT_ABGR16161616F, COGL_PIXEL_FORMAT_RGBA_FP_16161616_PRE },
#elif G_BYTE_ORDER == G_BIG_ENDIAN
{ DRM_FORMAT_RGBX8888, COGL_PIXEL_FORMAT_RGBX_8888 },
{ DRM_FORMAT_RGBA8888, COGL_PIXEL_FORMAT_RGBA_8888_PRE },
{ DRM_FORMAT_BGRX8888, COGL_PIXEL_FORMAT_BGRX_8888 },
{ DRM_FORMAT_BGRA8888, COGL_PIXEL_FORMAT_BGRA_8888_PRE },
{ DRM_FORMAT_XRGB8888, COGL_PIXEL_FORMAT_XRGB_8888 },
{ DRM_FORMAT_ARGB8888, COGL_PIXEL_FORMAT_ARGB_8888_PRE },
{ DRM_FORMAT_XBGR8888, COGL_PIXEL_FORMAT_XBGR_8888 },
{ DRM_FORMAT_ABGR8888, COGL_PIXEL_FORMAT_ABGR_8888_PRE },
#else
#error "unexpected G_BYTE_ORDER"
#endif
};
gboolean meta_cogl_pixel_format_from_drm_format (uint32_t drm_format,
CoglPixelFormat *out_format);
G_END_DECLS
#endif /* META_COGL_DRM_FORMATS_H */

View File

@ -145,7 +145,6 @@ endif
if have_native_backend
mutter_pkg_private_deps += [
libdrm_dep,
libinput_dep,
gudev_dep,
libgbm_dep,
@ -155,6 +154,12 @@ if have_native_backend
]
endif
if have_wayland or have_native_backend
mutter_pkg_private_deps += [
libdrm_dep,
]
endif
if have_wayland_eglstream
mutter_lib_deps += [
dl_dep,
@ -730,8 +735,6 @@ if have_native_backend
'backends/native/meta-barrier-native.h',
'backends/native/meta-clutter-backend-native.c',
'backends/native/meta-clutter-backend-native.h',
'backends/native/meta-cogl-utils.c',
'backends/native/meta-cogl-utils.h',
'backends/native/meta-crtc-kms.c',
'backends/native/meta-crtc-kms.h',
'backends/native/meta-crtc-native.c',
@ -861,6 +864,13 @@ if have_native_backend
]
endif
if have_wayland or have_native_backend
mutter_sources += [
'common/meta-cogl-drm-formats.c',
'common/meta-cogl-drm-formats.h',
]
endif
if have_wayland_eglstream
mutter_sources += [
'wayland/meta-wayland-egl-stream.c',