From 692057de05dcf90ea9ac180a23d4996ebc1e7206 Mon Sep 17 00:00:00 2001
From: Xaver Hugl <xaver.hugl@kde.org>
Date: Thu, 7 Nov 2024 13:58:49 +0100
Subject: [PATCH 4/4] vulkan/wsi: handle the compositor not supporting extended
 target volume better

Instead of unconditionally ignoring the HDR metadata, just attempt to create the image
description, and if it fails, fall back to creating it without HDR metadata.

Signed-off-by: Xaver Hugl <xaver.hugl@kde.org>
Reviewed-by: Sebastian Wick <sebastian.wick@redhat.com>
Acked-by: Daniel Stone <daniels@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/32038>
---
 src/vulkan/wsi/wsi_common_wayland.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/vulkan/wsi/wsi_common_wayland.c b/src/vulkan/wsi/wsi_common_wayland.c
index de12f524891..4c364401226 100644
--- a/src/vulkan/wsi/wsi_common_wayland.c
+++ b/src/vulkan/wsi/wsi_common_wayland.c
@@ -1250,7 +1250,7 @@ wsi_wl_swapchain_update_colorspace(struct wsi_wl_swapchain *chain)
 
    wp_image_description_creator_params_v1_set_primaries_named(creator, primaries);
    wp_image_description_creator_params_v1_set_tf_named(creator, tf);
-   if (should_use_hdr_metadata && display->color_features.extended_target_volume) {
+   if (should_use_hdr_metadata) {
       uint32_t max_cll = round(chain->color.hdr_metadata.maxContentLightLevel);
       uint32_t max_fall = round(chain->color.hdr_metadata.maxFrameAverageLightLevel);
       wp_image_description_creator_params_v1_set_max_cll(creator, max_cll);
@@ -1289,8 +1289,17 @@ wsi_wl_swapchain_update_colorspace(struct wsi_wl_swapchain *chain)
       if (ret < 0)
          return VK_ERROR_OUT_OF_DATE_KHR;
    }
-   if (status == failed)
-      return VK_ERROR_SURFACE_LOST_KHR;
+   if (status == failed) {
+      wp_image_description_v1_destroy(image_desc);
+      if (!display->color_features.extended_target_volume && should_use_hdr_metadata) {
+         /* VK_EXT_hdr_metadata doesn't specify if or how the metadata is used,
+          * so it's fine to try again without it. */
+         chain->color.has_hdr_metadata = false;
+         return wsi_wl_swapchain_update_colorspace(chain);
+      } else {
+         return VK_ERROR_SURFACE_LOST_KHR;
+      }
+   }
 
    wp_color_management_surface_v1_set_image_description(chain->wsi_wl_surface->color.color_surface,
                                                         image_desc,
-- 
2.49.0

