The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

c/main: Use enumeration helpers and tidy NVIDIA target code

+72 -26
+72 -26
src/xrt/compositor/main/comp_window_direct_nvidia.c
··· 1 - // Copyright 2019-2020, Collabora, Ltd. 1 + // Copyright 2019-2023, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file ··· 9 9 */ 10 10 11 11 #include "util/u_misc.h" 12 + #include "util/u_pretty_print.h" 12 13 13 14 #include "main/comp_window_direct.h" 15 + 14 16 15 17 /* 16 18 * 17 - * Private structs 19 + * Private structs and defines. 18 20 * 19 21 */ 22 + 23 + //! NVIDIA Vendor ID. 24 + #define NVIDIA_VENDOR_ID (0x10DE) 20 25 21 26 /*! 22 27 * Probed display. ··· 42 47 struct comp_window_direct_nvidia_display *displays; 43 48 uint16_t display_count; 44 49 }; 50 + 45 51 46 52 /* 47 53 * ··· 85 91 { 86 92 (void)ct; 87 93 (void)title; 94 + } 95 + 96 + static VkResult 97 + enumerate_physical_device_display_properties(struct vk_bundle *vk, 98 + VkPhysicalDevice physical_device, 99 + VkDisplayPropertiesKHR **out_props, 100 + uint32_t *out_prop_count) 101 + { 102 + VkDisplayPropertiesKHR *props = NULL; 103 + uint32_t prop_count = 0; 104 + VkResult ret; 105 + 106 + ret = vk->vkGetPhysicalDeviceDisplayPropertiesKHR(NULL, &prop_count, NULL); 107 + vk_check_error("vkGetPhysicalDeviceDisplayPropertiesKHR", ret, ret); 108 + if (prop_count == 0) { 109 + *out_props = props; 110 + *out_prop_count = prop_count; 111 + return VK_SUCCESS; 112 + } 113 + 114 + props = U_TYPED_ARRAY_CALLOC(VkDisplayPropertiesKHR, prop_count); 115 + ret = vk->vkGetPhysicalDeviceDisplayPropertiesKHR(NULL, &prop_count, props); 116 + vk_check_error_with_free("vkGetPhysicalDeviceDisplayPropertiesKHR", ret, ret, props); 117 + 118 + *out_props = props; 119 + *out_prop_count = prop_count; 120 + 121 + return VK_SUCCESS; 88 122 } 89 123 90 124 struct comp_target * ··· 294 328 static bool 295 329 _test_for_nvidia(struct comp_compositor *c, struct vk_bundle *vk) 296 330 { 331 + VkDisplayPropertiesKHR *display_props; 332 + uint32_t display_count; 297 333 VkResult ret; 298 334 299 335 VkPhysicalDeviceProperties physical_device_properties; 300 336 vk->vkGetPhysicalDeviceProperties(vk->physical_device, &physical_device_properties); 301 337 302 - if (physical_device_properties.vendorID != 0x10DE) 303 - return false; 304 - 305 - // get a list of attached displays 306 - uint32_t display_count; 307 - 308 - ret = vk->vkGetPhysicalDeviceDisplayPropertiesKHR(vk->physical_device, &display_count, NULL); 309 - if (ret != VK_SUCCESS) { 310 - CVK_ERROR(c, "vkGetPhysicalDeviceDisplayPropertiesKHR", "Failed to get vulkan display count", ret); 338 + // Only run this code on NVIDIA hardware. 339 + if (physical_device_properties.vendorID != NVIDIA_VENDOR_ID) { 311 340 return false; 312 341 } 313 342 314 - VkDisplayPropertiesKHR *display_props = U_TYPED_ARRAY_CALLOC(VkDisplayPropertiesKHR, display_count); 315 - 316 - if (display_props && vk->vkGetPhysicalDeviceDisplayPropertiesKHR(vk->physical_device, &display_count, 317 - display_props) != VK_SUCCESS) { 318 - CVK_ERROR(c, "vkGetPhysicalDeviceDisplayPropertiesKHR", "Failed to get display properties", ret); 319 - free(display_props); 343 + // Get a list of attached displays. 344 + ret = enumerate_physical_device_display_properties( // 345 + vk, // 346 + vk->physical_device, // 347 + &display_props, // 348 + &display_count); // 349 + if (ret != VK_SUCCESS) { 350 + CVK_ERROR(c, "enumerate_physical_device_display_properties", "Failed to get display properties ", ret); 320 351 return false; 321 352 } 322 353 323 354 for (uint32_t i = 0; i < display_count; i++) { 324 355 VkDisplayPropertiesKHR *disp = display_props + i; 325 - // check this display against our allowlist 356 + 357 + // Check this display against our allowlist. 326 358 for (uint32_t j = 0; j < ARRAY_SIZE(NV_DIRECT_ALLOWLIST); j++) { 327 359 if (_match_allowlist_entry(NV_DIRECT_ALLOWLIST[j], disp)) { 328 360 free(display_props); ··· 330 362 } 331 363 } 332 364 365 + // Also check against any extra displays given by the user. 333 366 if (c->settings.nvidia_display && _match_allowlist_entry(c->settings.nvidia_display, disp)) { 334 367 free(display_props); 335 368 return true; 336 369 } 337 370 } 338 371 339 - COMP_ERROR(c, "NVIDIA: No allowlisted displays found!"); 372 + struct u_pp_sink_stack_only sink; 373 + u_pp_delegate_t dg = u_pp_sink_stack_only_init(&sink); 374 + 375 + u_pp(dg, "NVIDIA: No allowlisted displays found!"); 376 + 377 + u_pp(dg, "\n\t== Current Allowlist (%u) ==", (uint32_t)ARRAY_SIZE(NV_DIRECT_ALLOWLIST)); 378 + for (uint32_t i = 0; i < ARRAY_SIZE(NV_DIRECT_ALLOWLIST); i++) { 379 + u_pp(dg, "\n\t\t%s", NV_DIRECT_ALLOWLIST[i]); 380 + } 340 381 341 - COMP_ERROR(c, "== Current Allowlist =="); 342 - for (uint32_t i = 0; i < ARRAY_SIZE(NV_DIRECT_ALLOWLIST); i++) 343 - COMP_ERROR(c, "%s", NV_DIRECT_ALLOWLIST[i]); 382 + if (c->settings.nvidia_display != NULL) { 383 + u_pp(dg, "\n\t\t%s (extra)", c->settings.nvidia_display); 384 + } 344 385 345 - COMP_ERROR(c, "== Found Displays =="); 346 - for (uint32_t i = 0; i < display_count; i++) 347 - COMP_ERROR(c, "%s", display_props[i].displayName); 386 + u_pp(dg, "\n\t== Found Displays (%u) ==", display_count); 387 + for (uint32_t i = 0; i < display_count; i++) { 388 + u_pp(dg, "\n\t\t%s", display_props[i].displayName); 389 + } 348 390 391 + COMP_ERROR(c, "%s", sink.buffer); 349 392 350 393 free(display_props); 351 394 ··· 376 419 return false; 377 420 } 378 421 379 - const char *extension_names[] = {COMP_INSTANCE_EXTENSIONS_COMMON, VK_KHR_DISPLAY_EXTENSION_NAME}; 422 + const char *extension_names[] = { 423 + COMP_INSTANCE_EXTENSIONS_COMMON, 424 + VK_KHR_DISPLAY_EXTENSION_NAME, 425 + }; 380 426 381 427 VkInstanceCreateInfo instance_create_info = { 382 428 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,