1 /* 2 * Copyright © 2015 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 #ifndef WSI_COMMON_H 24 #define WSI_COMMON_H 25 26 #include <stdint.h> 27 #include <stdbool.h> 28 29 #include "util/log.h" 30 #include "vk_alloc.h" 31 #include "vk_dispatch_table.h" 32 #include <vulkan/vulkan.h> 33 #include <vulkan/vk_icd.h> 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 #ifndef WSI_ENTRYPOINTS_H 40 extern const struct vk_instance_entrypoint_table wsi_instance_entrypoints; 41 extern const struct vk_physical_device_entrypoint_table wsi_physical_device_entrypoints; 42 extern const struct vk_device_entrypoint_table wsi_device_entrypoints; 43 #endif 44 45 #include <util/list.h> 46 47 /* This is guaranteed to not collide with anything because it's in the 48 * VK_KHR_swapchain namespace but not actually used by the extension. 49 */ 50 #define VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA (VkStructureType)1000001002 51 #define VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA (VkStructureType)1000001003 52 #define VK_STRUCTURE_TYPE_WSI_SURFACE_SUPPORTED_COUNTERS_MESA (VkStructureType)1000001005 53 #define VK_STRUCTURE_TYPE_WSI_MEMORY_SIGNAL_SUBMIT_INFO_MESA (VkStructureType)1000001006 54 55 #define VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA_cast struct wsi_image_create_info 56 #define VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA_cast struct wsi_memory_allocate_info 57 #define VK_STRUCTURE_TYPE_WSI_SURFACE_SUPPORTED_COUNTERS_MESA_cast struct wsi_surface_supported_counters 58 #define VK_STRUCTURE_TYPE_WSI_MEMORY_SIGNAL_SUBMIT_INFO_MESA_cast struct wsi_memory_signal_submit_info 59 60 /* This is always chained to VkImageCreateInfo when a wsi image is created. 61 * It indicates that the image can be transitioned to/from 62 * VK_IMAGE_LAYOUT_PRESENT_SRC_KHR. 63 */ 64 struct wsi_image_create_info { 65 VkStructureType sType; 66 const void *pNext; 67 bool scanout; 68 69 /* if true, the image is a blit source */ 70 bool blit_src; 71 }; 72 73 struct wsi_memory_allocate_info { 74 VkStructureType sType; 75 const void *pNext; 76 bool implicit_sync; 77 }; 78 79 /* To be chained into VkSurfaceCapabilities2KHR */ 80 struct wsi_surface_supported_counters { 81 VkStructureType sType; 82 const void *pNext; 83 84 VkSurfaceCounterFlagsEXT supported_surface_counters; 85 86 }; 87 88 /* To be chained into VkSubmitInfo */ 89 struct wsi_memory_signal_submit_info { 90 VkStructureType sType; 91 const void *pNext; 92 VkDeviceMemory memory; 93 }; 94 95 struct wsi_interface; 96 struct vk_instance; 97 98 struct driOptionCache; 99 100 #define VK_ICD_WSI_PLATFORM_MAX (VK_ICD_WSI_PLATFORM_METAL + 1) 101 102 struct wsi_device { 103 /* Allocator for the instance */ 104 VkAllocationCallbacks instance_alloc; 105 106 VkPhysicalDevice pdevice; 107 VkPhysicalDeviceMemoryProperties memory_props; 108 uint32_t queue_family_count; 109 uint64_t queue_supports_blit; 110 111 VkPhysicalDeviceDrmPropertiesEXT drm_info; 112 VkPhysicalDevicePCIBusInfoPropertiesEXT pci_bus_info; 113 114 VkExternalSemaphoreHandleTypeFlags semaphore_export_handle_types; 115 VkExternalSemaphoreHandleTypeFlags timeline_semaphore_export_handle_types; 116 117 bool has_import_memory_host; 118 bool has_timeline_semaphore; 119 120 /** Indicates if wsi_image_create_info::scanout is supported 121 * 122 * If false, WSI will always use either modifiers or the prime blit path. 123 */ 124 bool supports_scanout; 125 bool supports_modifiers; 126 uint32_t maxImageDimension2D; 127 uint32_t optimalBufferCopyRowPitchAlignment; 128 VkPresentModeKHR override_present_mode; 129 bool force_bgra8_unorm_first; 130 131 /* Whether to enable adaptive sync for a swapchain if implemented and 132 * available. Not all window systems might support this. */ 133 bool enable_adaptive_sync; 134 135 /* List of fences to signal when hotplug event happens. */ 136 struct list_head hotplug_fences; 137 138 /* Create headless swapchains. */ 139 bool force_headless_swapchain; 140 141 bool force_swapchain_to_currentExtent; 142 143 struct { 144 /* Override the minimum number of images on the swapchain. 145 * 0 = no override */ 146 uint32_t override_minImageCount; 147 148 /* Forces strict number of image on the swapchain using application 149 * provided VkSwapchainCreateInfoKH::RminImageCount. 150 */ 151 bool strict_imageCount; 152 153 /* Ensures to create at least the number of image specified by the 154 * driver in VkSurfaceCapabilitiesKHR::minImageCount. 155 */ 156 bool ensure_minImageCount; 157 158 /* Wait for fences before submitting buffers to Xwayland. Defaults to 159 * true. 160 */ 161 bool xwaylandWaitReady; 162 163 /* adds an extra minImageCount when running under xwayland */ 164 bool extra_xwayland_image; 165 166 /* Never report VK_SUBOPTIMAL_KHR. Used to workaround 167 * games that cannot handle SUBOPTIMAL correctly. */ 168 bool ignore_suboptimal; 169 } x11; 170 171 struct { 172 void *(*get_d3d12_command_queue)(VkDevice device); 173 /* Needs to be per VkDevice, not VkPhysicalDevice, depends on queue config */ 174 bool (*requires_blits)(VkDevice device); 175 VkResult (*create_image_memory)(VkDevice device, void *resource, 176 const VkAllocationCallbacks *alloc, 177 VkDeviceMemory *out); 178 } win32; 179 180 bool sw; 181 182 /* Set to true if the implementation is ok with linear WSI images. */ 183 bool wants_linear; 184 185 /* Signals the semaphore such that any wait on the semaphore will wait on 186 * any reads or writes on the give memory object. This is used to 187 * implement the semaphore signal operation in vkAcquireNextImage. This 188 * requires the driver to implement vk_device::create_sync_for_memory. 189 */ 190 bool signal_semaphore_with_memory; 191 192 /* Signals the fence such that any wait on the fence will wait on any reads 193 * or writes on the give memory object. This is used to implement the 194 * semaphore signal operation in vkAcquireNextImage. This requires the 195 * driver to implement vk_device::create_sync_for_memory. The resulting 196 * vk_sync must support CPU waits. 197 */ 198 bool signal_fence_with_memory; 199 200 /* Whether present_wait functionality is enabled on the device. 201 * In this case, we have to create an extra timeline semaphore 202 * to be able to synchronize with the WSI present semaphore being unsignalled. 203 * This requires VK_KHR_timeline_semaphore. */ 204 bool khr_present_wait; 205 206 struct { 207 /* Don't use the commit-timing protocol for pacing */ 208 bool disable_timestamps; 209 } wayland; 210 211 /* 212 * This sets the ownership for a WSI memory object: 213 * 214 * The ownership is true if and only if the application is allowed to submit 215 * command buffers that reference the buffer. 216 * 217 * This can be used to prune BO lists without too many adverse affects on 218 * implicit sync. 219 * 220 * Side note: care needs to be taken for internally delayed submissions wrt 221 * timeline semaphores. 222 */ 223 void (*set_memory_ownership)(VkDevice device, 224 VkDeviceMemory memory, 225 VkBool32 ownership); 226 227 /* 228 * If this is set, the WSI device will call it to let the driver backend 229 * decide if it can present images directly on the given device fd. 230 */ 231 bool (*can_present_on_device)(VkPhysicalDevice pdevice, int fd); 232 233 /* 234 * A driver can implement this callback to return a special queue to execute 235 * buffer blits. 236 */ 237 VkQueue (*get_blit_queue)(VkDevice device); 238 239 #define WSI_CB(cb) PFN_vk##cb cb 240 WSI_CB(AllocateMemory); 241 WSI_CB(AllocateCommandBuffers); 242 WSI_CB(BindBufferMemory); 243 WSI_CB(BindImageMemory); 244 WSI_CB(BeginCommandBuffer); 245 WSI_CB(CmdPipelineBarrier); 246 WSI_CB(CmdCopyImage); 247 WSI_CB(CmdCopyImageToBuffer); 248 WSI_CB(CreateBuffer); 249 WSI_CB(CreateCommandPool); 250 WSI_CB(CreateFence); 251 WSI_CB(CreateImage); 252 WSI_CB(CreateSemaphore); 253 WSI_CB(DestroyBuffer); 254 WSI_CB(DestroyCommandPool); 255 WSI_CB(DestroyFence); 256 WSI_CB(DestroyImage); 257 WSI_CB(DestroySemaphore); 258 WSI_CB(EndCommandBuffer); 259 WSI_CB(FreeMemory); 260 WSI_CB(FreeCommandBuffers); 261 WSI_CB(GetBufferMemoryRequirements); 262 WSI_CB(GetFenceStatus); 263 WSI_CB(GetImageDrmFormatModifierPropertiesEXT); 264 WSI_CB(GetImageMemoryRequirements); 265 WSI_CB(GetImageSubresourceLayout); 266 WSI_CB(GetMemoryFdKHR); 267 WSI_CB(GetPhysicalDeviceFormatProperties); 268 WSI_CB(GetPhysicalDeviceFormatProperties2); 269 WSI_CB(GetPhysicalDeviceImageFormatProperties2); 270 WSI_CB(GetSemaphoreFdKHR); 271 WSI_CB(ResetFences); 272 WSI_CB(QueueSubmit); 273 WSI_CB(WaitForFences); 274 WSI_CB(MapMemory); 275 WSI_CB(UnmapMemory); 276 WSI_CB(WaitSemaphores); 277 #undef WSI_CB 278 279 struct wsi_interface * wsi[VK_ICD_WSI_PLATFORM_MAX]; 280 }; 281 282 typedef PFN_vkVoidFunction (VKAPI_PTR *WSI_FN_GetPhysicalDeviceProcAddr)(VkPhysicalDevice physicalDevice, const char* pName); 283 284 struct wsi_device_options { 285 bool sw_device; 286 bool extra_xwayland_image; 287 }; 288 289 VkResult 290 wsi_device_init(struct wsi_device *wsi, 291 VkPhysicalDevice pdevice, 292 WSI_FN_GetPhysicalDeviceProcAddr proc_addr, 293 const VkAllocationCallbacks *alloc, 294 int display_fd, 295 const struct driOptionCache *dri_options, 296 const struct wsi_device_options *device_options); 297 298 void 299 wsi_device_finish(struct wsi_device *wsi, 300 const VkAllocationCallbacks *alloc); 301 302 /* Setup file descriptor to be used with imported sync_fd's in wsi fences. */ 303 void 304 wsi_device_setup_syncobj_fd(struct wsi_device *wsi_device, 305 int fd); 306 307 #define ICD_DEFINE_NONDISP_HANDLE_CASTS(__VkIcdType, __VkType) \ 308 \ 309 static inline __VkIcdType * \ 310 __VkIcdType ## _from_handle(__VkType _handle) \ 311 { \ 312 return (__VkIcdType *)(uintptr_t) _handle; \ 313 } \ 314 \ 315 static inline __VkType \ 316 __VkIcdType ## _to_handle(__VkIcdType *_obj) \ 317 { \ 318 return (__VkType)(uintptr_t) _obj; \ 319 } 320 321 #define ICD_FROM_HANDLE(__VkIcdType, __name, __handle) \ 322 __VkIcdType *__name = __VkIcdType ## _from_handle(__handle) 323 324 ICD_DEFINE_NONDISP_HANDLE_CASTS(VkIcdSurfaceBase, VkSurfaceKHR) 325 326 VkResult 327 wsi_common_get_images(VkSwapchainKHR _swapchain, 328 uint32_t *pSwapchainImageCount, 329 VkImage *pSwapchainImages); 330 331 VkImage 332 wsi_common_get_image(VkSwapchainKHR _swapchain, uint32_t index); 333 334 VkResult 335 wsi_common_acquire_next_image2(const struct wsi_device *wsi, 336 VkDevice device, 337 const VkAcquireNextImageInfoKHR *pAcquireInfo, 338 uint32_t *pImageIndex); 339 340 VkResult 341 wsi_common_queue_present(const struct wsi_device *wsi, 342 VkDevice device_h, 343 VkQueue queue_h, 344 int queue_family_index, 345 const VkPresentInfoKHR *pPresentInfo); 346 347 VkResult 348 wsi_common_create_swapchain_image(const struct wsi_device *wsi, 349 const VkImageCreateInfo *pCreateInfo, 350 VkSwapchainKHR _swapchain, 351 VkImage *pImage); 352 VkResult 353 wsi_common_bind_swapchain_image(const struct wsi_device *wsi, 354 VkImage vk_image, 355 VkSwapchainKHR _swapchain, 356 uint32_t image_idx); 357 358 bool 359 wsi_common_vk_instance_supports_present_wait(const struct vk_instance *instance); 360 361 VkImageUsageFlags 362 wsi_caps_get_image_usage(void); 363 364 bool 365 wsi_device_supports_explicit_sync(struct wsi_device *device); 366 367 #define wsi_common_vk_warn_once(warning) \ 368 do { \ 369 static int warned = false; \ 370 if (!warned) { \ 371 mesa_loge(warning); \ 372 warned = true; \ 373 } \ 374 } while (0) 375 376 #ifdef __cplusplus 377 } 378 #endif 379 380 #endif 381