From 1cf742a3dc4cba98e7b993f006f3d085f27d95d6 Mon Sep 17 00:00:00 2001
From: Christoph Haag <christoph.haag@collabora.com>
Date: Sat, 15 Feb 2020 03:02:11 +0100
Subject: [PATCH] comp: Add XRT_COMPOSITOR_DESIRED_MODE env var to choose mode
 for direct mode.

The variable should be set to the index in the enumeration of a modes according to VK_KHR_display.

Monado can print a list of available modes with their indices with the env var XRT_COMPOSITOR_PRINT_MODES=1.
---
 src/xrt/compositor/main/comp_settings.c       |  2 ++
 src/xrt/compositor/main/comp_settings.h       |  3 +++
 .../main/comp_window_direct_mode.cpp          | 22 +++++++++++++++++--
 3 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/xrt/compositor/main/comp_settings.c b/src/xrt/compositor/main/comp_settings.c
index 1c62c5f18..a14cd3d4d 100644
--- a/src/xrt/compositor/main/comp_settings.c
+++ b/src/xrt/compositor/main/comp_settings.c
@@ -21,6 +21,7 @@ DEBUG_GET_ONCE_BOOL_OPTION(force_wayland, "XRT_COMPOSITOR_FORCE_WAYLAND", false)
 DEBUG_GET_ONCE_BOOL_OPTION(validate_vulkan, "XRT_COMPOSITOR_VULKAN_VALIDATION", false)
 DEBUG_GET_ONCE_BOOL_OPTION(wireframe, "XRT_COMPOSITOR_WIREFRAME", false)
 DEBUG_GET_ONCE_NUM_OPTION(force_gpu_index, "XRT_COMPOSITOR_FORCE_GPU_INDEX", -1)
+DEBUG_GET_ONCE_NUM_OPTION(desired_mode, "XRT_COMPOSITOR_DESIRED_MODE", -1)
 // clang-format on
 
 void
@@ -49,6 +50,7 @@ comp_settings_init(struct comp_settings *s, struct xrt_device *xdev)
 	s->validate_vulkan = debug_get_bool_option_validate_vulkan();
 	s->gpu_index = debug_get_num_option_force_gpu_index();
 	s->debug.wireframe = debug_get_bool_option_wireframe();
+	s->desired_mode = debug_get_num_option_desired_mode();
 
 	if (debug_get_bool_option_force_nvidia()) {
 		s->window_type = WINDOW_DIRECT_NVIDIA;
diff --git a/src/xrt/compositor/main/comp_settings.h b/src/xrt/compositor/main/comp_settings.h
index a697b3c92..cb8d8223d 100644
--- a/src/xrt/compositor/main/comp_settings.h
+++ b/src/xrt/compositor/main/comp_settings.h
@@ -96,6 +96,9 @@ struct comp_settings
 
 	//! Run the compositor on this Vulkan physical device
 	int gpu_index;
+
+	//! Try to choose the mode with this index for direct mode
+	int desired_mode;
 };
 
 /*!
diff --git a/src/xrt/compositor/main/comp_window_direct_mode.cpp b/src/xrt/compositor/main/comp_window_direct_mode.cpp
index a4b044d7e..f0622585d 100644
--- a/src/xrt/compositor/main/comp_window_direct_mode.cpp
+++ b/src/xrt/compositor/main/comp_window_direct_mode.cpp
@@ -479,8 +479,26 @@ comp_window_direct_get_primary_display_mode(struct comp_window_direct *w,
 
 	print_modes(w, mode_properties, mode_count);
 
-	int chosen_mode =
-	    choose_best_vk_mode_auto(w, mode_properties, mode_count);
+
+	int chosen_mode = 0;
+
+	int desired_mode = w->base.c->settings.desired_mode;
+	if (desired_mode + 1 > (int)mode_count) {
+		COMP_ERROR(w->base.c,
+		           "Requested mode index %d, but max is %d. Falling "
+		           "back to automatic mode selection",
+		           desired_mode, mode_count);
+		chosen_mode =
+		    choose_best_vk_mode_auto(w, mode_properties, mode_count);
+	} else if (desired_mode < 0) {
+		chosen_mode =
+		    choose_best_vk_mode_auto(w, mode_properties, mode_count);
+	} else {
+		COMP_DEBUG(w->base.c, "Using manually chosen mode %d",
+		           desired_mode);
+		chosen_mode = desired_mode;
+	}
+
 	VkDisplayModePropertiesKHR props = mode_properties[chosen_mode];
 
 	COMP_DEBUG(w->base.c, "found display mode %dx%d@%.2f",
-- 
GitLab