From ad6fd451fbe3da28ce41e553d31e6bb37f8483ab Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 17 Jun 2024 07:56:06 -0400 Subject: [PATCH] gdk: Create our egl surface with srgb colorspace For GDK_MEMORY_U8_SRGB depth, try to create an SRGB surface. This requires the EXT_KHR_gl_colorspace extension, which isn't super-common in the wild (37%), so we fall back to regular U8 if that fails. But if we have the extension, create our egl surface with the srgb colorspace, and report that fact in gdk_surface_gl_is_srgb(). --- gdk/gdkdisplay.c | 2 ++ gdk/gdkdisplayprivate.h | 1 + gdk/gdksurface.c | 24 +++++++++++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/gdk/gdkdisplay.c b/gdk/gdkdisplay.c index df2a939c40..fb7467faee 100644 --- a/gdk/gdkdisplay.c +++ b/gdk/gdkdisplay.c @@ -1870,6 +1870,8 @@ gdk_display_init_egl (GdkDisplay *self, 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"); + self->have_egl_gl_colorspace = + epoxy_has_egl_extension (priv->egl_display, "EGL_KHR_gl_colorspace"); if (self->have_egl_no_config_context) priv->egl_config_high_depth = gdk_display_create_egl_config (self, diff --git a/gdk/gdkdisplayprivate.h b/gdk/gdkdisplayprivate.h index b9b794d3a2..d0f02f2cba 100644 --- a/gdk/gdkdisplayprivate.h +++ b/gdk/gdkdisplayprivate.h @@ -131,6 +131,7 @@ struct _GdkDisplay guint have_egl_pixel_format_float : 1; guint have_egl_dma_buf_import : 1; guint have_egl_dma_buf_export : 1; + guint have_egl_gl_colorspace : 1; GdkDmabufFormats *dmabuf_formats; GdkDmabufDownloader *dmabuf_downloaders[4]; diff --git a/gdk/gdksurface.c b/gdk/gdksurface.c index 1ee40fce0c..aa1cdefd53 100644 --- a/gdk/gdksurface.c +++ b/gdk/gdksurface.c @@ -1159,10 +1159,32 @@ gdk_surface_ensure_egl_surface (GdkSurface *self, if (priv->egl_surface == NULL) { + EGLint attribs[4]; + int i; + + i = 0; + if (depth == GDK_MEMORY_U8_SRGB && display->have_egl_gl_colorspace) + { + attribs[i++] = EGL_GL_COLORSPACE_KHR; + attribs[i++] = EGL_GL_COLORSPACE_SRGB_KHR; + self->is_srgb = TRUE; + } + g_assert (i < G_N_ELEMENTS (attribs)); + attribs[i++] = EGL_NONE; + priv->egl_surface = eglCreateWindowSurface (gdk_display_get_egl_display (display), gdk_display_get_egl_config (display, depth), (EGLNativeWindowType) priv->egl_native_window, - NULL); + attribs); + if (priv->egl_surface == EGL_NO_SURFACE) + { + /* just assume the error is no srgb support and try again without */ + self->is_srgb = FALSE; + priv->egl_surface = eglCreateWindowSurface (gdk_display_get_egl_display (display), + gdk_display_get_egl_config (display, depth), + (EGLNativeWindowType) priv->egl_native_window, + NULL); + } priv->egl_surface_depth = depth; } #endif