From 779c8d1669fa74b31e296519f6920e5479c19973 Mon Sep 17 00:00:00 2001
From: Xaver Hugl <xaver.hugl@kde.org>
Date: Thu, 27 Feb 2025 14:56:06 +0100
Subject: [PATCH] vulkan/wsi: don't use sRGB if the compositor doesn't support
 it

This could realistically happen if the compositor doesn't support parametric image
descriptions at all, in which case we'd get a protocol error for trying to use it.

Signed-off-by: Xaver Hugl <xaver.hugl@kde.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/33804>
---
 src/vulkan/wsi/wsi_common_wayland.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/vulkan/wsi/wsi_common_wayland.c b/src/vulkan/wsi/wsi_common_wayland.c
index 098ba9c80928e..3f636a5aad1a0 100644
--- a/src/vulkan/wsi/wsi_common_wayland.c
+++ b/src/vulkan/wsi/wsi_common_wayland.c
@@ -1139,8 +1139,14 @@ static const struct wp_image_description_v1_listener image_description_listener
 };
 
 static bool
-needs_color_surface(VkColorSpaceKHR colorspace)
+needs_color_surface(struct wsi_wl_display *display, VkColorSpaceKHR colorspace)
 {
+   if (colorspace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
+      /* we want to use a color surface to set sRGB if possible, but
+       * only if the compositor actually supports sRGB */
+      return vector_contains(&display->color_primaries, WP_COLOR_MANAGER_V1_PRIMARIES_SRGB)
+          && vector_contains(&display->color_transfer_funcs, WP_COLOR_MANAGER_V1_TRANSFER_FUNCTION_SRGB);
+   }
    return colorspace != VK_COLOR_SPACE_PASS_THROUGH_EXT;
 }
 
@@ -1199,8 +1205,8 @@ wsi_wl_swapchain_update_colorspace(struct wsi_wl_swapchain *chain)
    }
 
    bool new_color_surface = !surface->color.color_surface;
-   bool needs_color_surface_new = needs_color_surface(chain->color.colorspace);
-   bool needs_color_surface_old = needs_color_surface(surface->color.colorspace);
+   bool needs_color_surface_new = needs_color_surface(display, chain->color.colorspace);
+   bool needs_color_surface_old = needs_color_surface(display, surface->color.colorspace);
    if ((new_color_surface || !needs_color_surface_old) && needs_color_surface_new) {
       wsi_wl_surface_add_color_refcount(surface);
    } else if (needs_color_surface_old && !needs_color_surface_new) {
@@ -3293,7 +3299,8 @@ wsi_wl_swapchain_chain_free(struct wsi_wl_swapchain *chain,
       wl_callback_destroy(chain->frame);
    if (chain->tearing_control)
       wp_tearing_control_v1_destroy(chain->tearing_control);
-   if (needs_color_surface(chain->color.colorspace) && wsi_wl_surface->color.color_surface) {
+   if (needs_color_surface(wsi_wl_surface->display, chain->color.colorspace) &&
+       wsi_wl_surface->color.color_surface) {
       wsi_wl_surface_remove_color_refcount(wsi_wl_surface);
    }
 
-- 
GitLab

