From 6f1edfc77621b8740a7168fc062e7371c13f609f Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Thu, 14 Sep 2023 17:36:39 +0200 Subject: [PATCH] common/drm-formats: Rename to MetaFormatInfo and simplify API Every format kind has it's own function to search for a MetaFormatInfo from which contains all the information. Part-of: --- src/backends/native/meta-drm-buffer-gbm.c | 9 ++--- src/backends/native/meta-onscreen-native.c | 20 +++++------ src/backends/native/meta-renderer-native.c | 12 ++++--- src/common/meta-cogl-drm-formats.c | 42 +++++++--------------- src/common/meta-cogl-drm-formats.h | 13 +++---- src/wayland/meta-wayland-dma-buf.c | 40 ++++++++++----------- 6 files changed, 57 insertions(+), 79 deletions(-) diff --git a/src/backends/native/meta-drm-buffer-gbm.c b/src/backends/native/meta-drm-buffer-gbm.c index 0d785695c..5c7e5f79e 100644 --- a/src/backends/native/meta-drm-buffer-gbm.c +++ b/src/backends/native/meta-drm-buffer-gbm.c @@ -266,6 +266,7 @@ meta_drm_buffer_gbm_blit_to_framebuffer (CoglScanout *scanout, gboolean result; int dmabuf_fd = -1; uint32_t i; + const MetaFormatInfo *format_info; dmabuf_fd = gbm_bo_get_fd (buffer_gbm->bo); if (dmabuf_fd == -1) @@ -277,10 +278,10 @@ 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); - g_assert (result); + + format_info = meta_format_info_from_drm_format (drm_format); + g_assert (format_info); + cogl_format = format_info->cogl_format; width = gbm_bo_get_width (buffer_gbm->bo); height = gbm_bo_get_height (buffer_gbm->bo); diff --git a/src/backends/native/meta-onscreen-native.c b/src/backends/native/meta-onscreen-native.c index 69177f3c1..9d61985fe 100644 --- a/src/backends/native/meta-onscreen-native.c +++ b/src/backends/native/meta-onscreen-native.c @@ -719,8 +719,7 @@ copy_shared_framebuffer_primary_gpu (CoglOnscreen *onscre CoglFramebuffer *dmabuf_fb; int dmabuf_fd; g_autoptr (GError) error = NULL; - CoglPixelFormat cogl_format; - int ret; + const MetaFormatInfo *format_info; COGL_TRACE_BEGIN_SCOPED (CopySharedFramebufferPrimaryGpu, "copy_shared_framebuffer_primary_gpu()"); @@ -746,8 +745,8 @@ 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); - g_assert (ret); + format_info = meta_format_info_from_drm_format (drm_format); + g_assert (format_info); dmabuf_fd = meta_drm_buffer_dumb_ensure_dmabuf_fd (buffer_dumb, &error); if (!dmabuf_fd) @@ -831,7 +830,7 @@ copy_shared_framebuffer_cpu (CoglOnscreen *onscreen, void *buffer_data; CoglBitmap *dumb_bitmap; CoglPixelFormat cogl_format; - gboolean ret; + const MetaFormatInfo *format_info; COGL_TRACE_BEGIN_SCOPED (CopySharedFramebufferCpu, "copy_shared_framebuffer_cpu()"); @@ -848,8 +847,9 @@ 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); - g_assert (ret); + format_info = meta_format_info_from_drm_format (drm_format); + g_assert (format_info); + cogl_format = format_info->cogl_format; dumb_bitmap = cogl_bitmap_new_for_data (cogl_context, width, @@ -2116,9 +2116,7 @@ pick_secondary_gpu_framebuffer_format_for_cpu (CoglOnscreen *onscreen) /* Check if any of our preferred formats are supported. */ for (k = 0; k < G_N_ELEMENTS (preferred_formats); k++) { - g_assert (meta_cogl_pixel_format_from_drm_format (preferred_formats[k], - NULL, - NULL)); + g_assert (meta_format_info_from_drm_format (preferred_formats[k])); for (i = 0; i < formats->len; i++) { @@ -2138,7 +2136,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_format_info_from_drm_format (drm_format)) return drm_format; } diff --git a/src/backends/native/meta-renderer-native.c b/src/backends/native/meta-renderer-native.c index f6aeb0f75..54e3203b4 100644 --- a/src/backends/native/meta-renderer-native.c +++ b/src/backends/native/meta-renderer-native.c @@ -615,10 +615,11 @@ meta_renderer_native_create_dma_buf_framebuffer (MetaRendererNative *renderer_n CoglEglImageFlags flags; CoglTexture *cogl_tex; CoglOffscreen *cogl_fbo; - int ret; + const MetaFormatInfo *format_info; - ret = meta_cogl_pixel_format_from_drm_format (drm_format, &cogl_format, NULL); - g_assert (ret); + format_info = meta_format_info_from_drm_format (drm_format); + g_assert (format_info); + cogl_format = format_info->cogl_format; strides[0] = stride; offsets[0] = offset; @@ -930,8 +931,10 @@ meta_renderer_native_create_dma_buf (CoglRenderer *cogl_renderer, uint32_t drm_format; CoglFramebuffer *dmabuf_fb; CoglDmaBufHandle *dmabuf_handle; + const MetaFormatInfo *format_info; - if (!meta_drm_format_from_cogl_pixel_format (format, &drm_format)) + format_info = meta_format_info_from_cogl_format (format); + if (!format_info) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Native renderer doesn't support creating DMA buffer with format %s", @@ -939,6 +942,7 @@ meta_renderer_native_create_dma_buf (CoglRenderer *cogl_renderer, return NULL; } + drm_format = format_info->drm_format; render_device = renderer_gpu_data->render_device; flags = META_DRM_BUFFER_FLAG_NONE; buffer = meta_render_device_allocate_dma_buf (render_device, diff --git a/src/common/meta-cogl-drm-formats.c b/src/common/meta-cogl-drm-formats.c index 2ab7c1774..dee5c5134 100644 --- a/src/common/meta-cogl-drm-formats.c +++ b/src/common/meta-cogl-drm-formats.c @@ -24,50 +24,32 @@ #include "common/meta-cogl-drm-formats.h" -gboolean -meta_cogl_pixel_format_from_drm_format (uint32_t drm_format, - CoglPixelFormat *out_format, - MetaMultiTextureFormat *out_multi_texture_format) +const MetaFormatInfo * +meta_format_info_from_drm_format (uint32_t drm_format) { - const size_t n = G_N_ELEMENTS (meta_cogl_drm_format_map); + const size_t n = G_N_ELEMENTS (meta_format_info); size_t i; for (i = 0; i < n; i++) { - if (meta_cogl_drm_format_map[i].drm_format == drm_format) - break; + if (meta_format_info[i].drm_format == drm_format) + return &meta_format_info[i]; } - if (i == n) - return FALSE; - - if (out_format) - *out_format = meta_cogl_drm_format_map[i].cogl_format; - - if (out_multi_texture_format) - *out_multi_texture_format = meta_cogl_drm_format_map[i].multi_texture_format; - - return TRUE; + return NULL; } -gboolean -meta_drm_format_from_cogl_pixel_format (CoglPixelFormat cogl_format, - uint32_t *out_drm_format) +const MetaFormatInfo * +meta_format_info_from_cogl_format (CoglPixelFormat cogl_format) { - const size_t n = G_N_ELEMENTS (meta_cogl_drm_format_map); + const size_t n = G_N_ELEMENTS (meta_format_info); size_t i; for (i = 0; i < n; i++) { - if (meta_cogl_drm_format_map[i].cogl_format == cogl_format) - break; + if (meta_format_info[i].cogl_format == cogl_format) + return &meta_format_info[i]; } - if (i == n) - return FALSE; - - if (out_drm_format) - *out_drm_format = meta_cogl_drm_format_map[i].drm_format; - - return TRUE; + return NULL; } diff --git a/src/common/meta-cogl-drm-formats.h b/src/common/meta-cogl-drm-formats.h index 9eb865d98..d64bbc241 100644 --- a/src/common/meta-cogl-drm-formats.h +++ b/src/common/meta-cogl-drm-formats.h @@ -29,14 +29,14 @@ G_BEGIN_DECLS -typedef struct _CoglDrmFormatMap +typedef struct _MetaFormatInfo { uint32_t drm_format; CoglPixelFormat cogl_format; MetaMultiTextureFormat multi_texture_format; -} CoglDrmFormatMap; +} MetaFormatInfo; -static const CoglDrmFormatMap meta_cogl_drm_format_map[] = { +static const MetaFormatInfo meta_format_info[] = { /* DRM formats are defined as little-endian, not machine endian. */ #if G_BYTE_ORDER == G_LITTLE_ENDIAN { DRM_FORMAT_R8, COGL_PIXEL_FORMAT_R_8, META_MULTI_TEXTURE_FORMAT_INVALID }, @@ -78,11 +78,8 @@ static const CoglDrmFormatMap meta_cogl_drm_format_map[] = { #endif }; -gboolean meta_cogl_pixel_format_from_drm_format (uint32_t drm_format, - CoglPixelFormat *out_format, - MetaMultiTextureFormat *out_multi_texture_format); +const MetaFormatInfo *meta_format_info_from_drm_format (uint32_t drm_format); -gboolean meta_drm_format_from_cogl_pixel_format (CoglPixelFormat cogl_format, - uint32_t *out_drm_format); +const MetaFormatInfo *meta_format_info_from_cogl_format (CoglPixelFormat cogl_format); G_END_DECLS diff --git a/src/wayland/meta-wayland-dma-buf.c b/src/wayland/meta-wayland-dma-buf.c index 04d3ad84d..d1f540e90 100644 --- a/src/wayland/meta-wayland-dma-buf.c +++ b/src/wayland/meta-wayland-dma-buf.c @@ -356,6 +356,7 @@ meta_wayland_dma_buf_realize_texture (MetaWaylandBuffer *buffer, MetaWaylandDmaBufBuffer *dma_buf = buffer->dma_buf.dma_buf; MetaMultiTextureFormat multi_format; CoglPixelFormat cogl_format; + const MetaFormatInfo *format_info; #ifdef HAVE_NATIVE_BACKEND MetaDrmFormatBuf format_buf; #endif @@ -363,9 +364,8 @@ meta_wayland_dma_buf_realize_texture (MetaWaylandBuffer *buffer, if (buffer->dma_buf.texture) return TRUE; - if (!meta_cogl_pixel_format_from_drm_format (dma_buf->drm_format, - &cogl_format, - &multi_format)) + format_info = meta_format_info_from_drm_format (dma_buf->drm_format); + if (!format_info) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, @@ -373,6 +373,9 @@ meta_wayland_dma_buf_realize_texture (MetaWaylandBuffer *buffer, return FALSE; } + cogl_format = format_info->cogl_format; + multi_format = format_info->multi_texture_format; + #ifdef HAVE_NATIVE_BACKEND meta_topic (META_DEBUG_WAYLAND, "[dma-buf] wl_buffer@%u DRM format %s " @@ -456,17 +459,12 @@ meta_wayland_dma_buf_realize_texture (MetaWaylandBuffer *buffer, CoglEglImageFlags flags; CoglTexture *cogl_texture; uint32_t drm_format = 0; - int plane_index, j; + int plane_index; + const MetaFormatInfo *format_info; - for (j = 0; j < G_N_ELEMENTS (meta_cogl_drm_format_map); j++) - { - if (meta_cogl_drm_format_map[j].cogl_format == subformats[i]) - { - drm_format = meta_cogl_drm_format_map[j].drm_format; - break; - } - } - g_return_val_if_fail (drm_format != 0, FALSE); + format_info = meta_format_info_from_cogl_format (subformats[i]); + g_return_val_if_fail (format_info != NULL, FALSE); + drm_format = format_info->drm_format; plane_index = plane_indices[i]; @@ -1638,7 +1636,8 @@ init_formats (MetaWaylandDmaBufManager *dma_buf_manager, MetaEgl *egl = meta_backend_get_egl (backend); EGLint num_formats; g_autofree EGLint *driver_formats = NULL; - int i, j; + int i; + const MetaFormatInfo *format_info; dma_buf_manager->formats = g_array_new (FALSE, FALSE, sizeof (MetaWaylandDmaBufFormat)); @@ -1659,15 +1658,12 @@ init_formats (MetaWaylandDmaBufManager *dma_buf_manager, driver_formats, &num_formats, error)) return FALSE; - for (i = 0; i < G_N_ELEMENTS (meta_cogl_drm_format_map); i++) + for (i = 0; i < num_formats; i++) { - for (j = 0; j < num_formats; j++) - { - if ((meta_cogl_drm_format_map[i].drm_format == driver_formats[j]) && - (meta_cogl_drm_format_map[i].multi_texture_format != - META_MULTI_TEXTURE_FORMAT_INVALID)) - add_format (dma_buf_manager, egl_display, driver_formats[j]); - } + format_info = meta_format_info_from_drm_format (driver_formats[i]); + if (format_info && format_info->multi_texture_format != + META_MULTI_TEXTURE_FORMAT_INVALID) + add_format (dma_buf_manager, egl_display, driver_formats[i]); } if (dma_buf_manager->formats->len == 0)