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_HEADLESS + 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 116 bool has_import_memory_host; 117 118 /** Indicates if wsi_image_create_info::scanout is supported 119 * 120 * If false, WSI will always use either modifiers or the prime blit path. 121 */ 122 bool supports_scanout; 123 bool supports_modifiers; 124 uint32_t maxImageDimension2D; 125 uint32_t optimalBufferCopyRowPitchAlignment; 126 VkPresentModeKHR override_present_mode; 127 bool force_bgra8_unorm_first; 128 129 /* Whether to enable adaptive sync for a swapchain if implemented and 130 * available. Not all window systems might support this. */ 131 bool enable_adaptive_sync; 132 133 /* List of fences to signal when hotplug event happens. */ 134 struct list_head hotplug_fences; 135 136 /* Create headless swapchains. */ 137 bool force_headless_swapchain; 138 139 bool force_swapchain_to_currentExtent; 140 141 struct { 142 /* Override the minimum number of images on the swapchain. 143 * 0 = no override */ 144 uint32_t override_minImageCount; 145 146 /* Forces strict number of image on the swapchain using application 147 * provided VkSwapchainCreateInfoKH::RminImageCount. 148 */ 149 bool strict_imageCount; 150 151 /* Ensures to create at least the number of image specified by the 152 * driver in VkSurfaceCapabilitiesKHR::minImageCount. 153 */ 154 bool ensure_minImageCount; 155 156 /* Wait for fences before submitting buffers to Xwayland. Defaults to 157 * true. 158 */ 159 bool xwaylandWaitReady; 160 161 /* adds an extra minImageCount when running under xwayland */ 162 bool extra_xwayland_image; 163 164 /* Never report VK_SUBOPTIMAL_KHR. Used to workaround 165 * games that cannot handle SUBOPTIMAL correctly. */ 166 bool ignore_suboptimal; 167 } x11; 168 169 struct { 170 void *(*get_d3d12_command_queue)(VkDevice device); 171 /* Needs to be per VkDevice, not VkPhysicalDevice, depends on queue config */ 172 bool (*requires_blits)(VkDevice device); 173 VkResult (*create_image_memory)(VkDevice device, void *resource, 174 const VkAllocationCallbacks *alloc, 175 VkDeviceMemory *out); 176 } win32; 177 178 bool sw; 179 180 /* Set to true if the implementation is ok with linear WSI images. */ 181 bool wants_linear; 182 183 /* Signals the semaphore such that any wait on the semaphore will wait on 184 * any reads or writes on the give memory object. This is used to 185 * implement the semaphore signal operation in vkAcquireNextImage. This 186 * requires the driver to implement vk_device::create_sync_for_memory. 187 */ 188 bool signal_semaphore_with_memory; 189 190 /* Signals the fence such that any wait on the fence will wait on any reads 191 * or writes on the give memory object. This is used to implement the 192 * semaphore signal operation in vkAcquireNextImage. This requires the 193 * driver to implement vk_device::create_sync_for_memory. The resulting 194 * vk_sync must support CPU waits. 195 */ 196 bool signal_fence_with_memory; 197 198 /* Whether present_wait functionality is enabled on the device. 199 * In this case, we have to create an extra timeline semaphore 200 * to be able to synchronize with the WSI present semaphore being unsignalled. 201 * This requires VK_KHR_timeline_semaphore. */ 202 bool khr_present_wait; 203 204 /* 205 * This sets the ownership for a WSI memory object: 206 * 207 * The ownership is true if and only if the application is allowed to submit 208 * command buffers that reference the buffer. 209 * 210 * This can be used to prune BO lists without too many adverse affects on 211 * implicit sync. 212 * 213 * Side note: care needs to be taken for internally delayed submissions wrt 214 * timeline semaphores. 215 */ 216 void (*set_memory_ownership)(VkDevice device, 217 VkDeviceMemory memory, 218 VkBool32 ownership); 219 220 /* 221 * If this is set, the WSI device will call it to let the driver backend 222 * decide if it can present images directly on the given device fd. 223 */ 224 bool (*can_present_on_device)(VkPhysicalDevice pdevice, int fd); 225 226 /* 227 * A driver can implement this callback to return a special queue to execute 228 * buffer blits. 229 */ 230 VkQueue (*get_blit_queue)(VkDevice device); 231 232 #define WSI_CB(cb) PFN_vk##cb cb 233 WSI_CB(AllocateMemory); 234 WSI_CB(AllocateCommandBuffers); 235 WSI_CB(BindBufferMemory); 236 WSI_CB(BindImageMemory); 237 WSI_CB(BeginCommandBuffer); 238 WSI_CB(CmdPipelineBarrier); 239 WSI_CB(CmdCopyImage); 240 WSI_CB(CmdCopyImageToBuffer); 241 WSI_CB(CreateBuffer); 242 WSI_CB(CreateCommandPool); 243 WSI_CB(CreateFence); 244 WSI_CB(CreateImage); 245 WSI_CB(CreateSemaphore); 246 WSI_CB(DestroyBuffer); 247 WSI_CB(DestroyCommandPool); 248 WSI_CB(DestroyFence); 249 WSI_CB(DestroyImage); 250 WSI_CB(DestroySemaphore); 251 WSI_CB(EndCommandBuffer); 252 WSI_CB(FreeMemory); 253 WSI_CB(FreeCommandBuffers); 254 WSI_CB(GetBufferMemoryRequirements); 255 WSI_CB(GetFenceStatus); 256 WSI_CB(GetImageDrmFormatModifierPropertiesEXT); 257 WSI_CB(GetImageMemoryRequirements); 258 WSI_CB(GetImageSubresourceLayout); 259 WSI_CB(GetMemoryFdKHR); 260 WSI_CB(GetPhysicalDeviceFormatProperties); 261 WSI_CB(GetPhysicalDeviceFormatProperties2); 262 WSI_CB(GetPhysicalDeviceImageFormatProperties2); 263 WSI_CB(GetSemaphoreFdKHR); 264 WSI_CB(ResetFences); 265 WSI_CB(QueueSubmit); 266 WSI_CB(WaitForFences); 267 WSI_CB(MapMemory); 268 WSI_CB(UnmapMemory); 269 WSI_CB(WaitSemaphores); 270 #undef WSI_CB 271 272 struct wsi_interface * wsi[VK_ICD_WSI_PLATFORM_MAX]; 273 }; 274 275 typedef PFN_vkVoidFunction (VKAPI_PTR *WSI_FN_GetPhysicalDeviceProcAddr)(VkPhysicalDevice physicalDevice, const char* pName); 276 277 struct wsi_device_options { 278 bool sw_device; 279 bool extra_xwayland_image; 280 }; 281 282 VkResult 283 wsi_device_init(struct wsi_device *wsi, 284 VkPhysicalDevice pdevice, 285 WSI_FN_GetPhysicalDeviceProcAddr proc_addr, 286 const VkAllocationCallbacks *alloc, 287 int display_fd, 288 const struct driOptionCache *dri_options, 289 const struct wsi_device_options *device_options); 290 291 void 292 wsi_device_finish(struct wsi_device *wsi, 293 const VkAllocationCallbacks *alloc); 294 295 /* Setup file descriptor to be used with imported sync_fd's in wsi fences. */ 296 void 297 wsi_device_setup_syncobj_fd(struct wsi_device *wsi_device, 298 int fd); 299 300 #define ICD_DEFINE_NONDISP_HANDLE_CASTS(__VkIcdType, __VkType) \ 301 \ 302 static inline __VkIcdType * \ 303 __VkIcdType ## _from_handle(__VkType _handle) \ 304 { \ 305 return (__VkIcdType *)(uintptr_t) _handle; \ 306 } \ 307 \ 308 static inline __VkType \ 309 __VkIcdType ## _to_handle(__VkIcdType *_obj) \ 310 { \ 311 return (__VkType)(uintptr_t) _obj; \ 312 } 313 314 #define ICD_FROM_HANDLE(__VkIcdType, __name, __handle) \ 315 __VkIcdType *__name = __VkIcdType ## _from_handle(__handle) 316 317 ICD_DEFINE_NONDISP_HANDLE_CASTS(VkIcdSurfaceBase, VkSurfaceKHR) 318 319 VkResult 320 wsi_common_get_images(VkSwapchainKHR _swapchain, 321 uint32_t *pSwapchainImageCount, 322 VkImage *pSwapchainImages); 323 324 VkImage 325 wsi_common_get_image(VkSwapchainKHR _swapchain, uint32_t index); 326 327 VkResult 328 wsi_common_acquire_next_image2(const struct wsi_device *wsi, 329 VkDevice device, 330 const VkAcquireNextImageInfoKHR *pAcquireInfo, 331 uint32_t *pImageIndex); 332 333 VkResult 334 wsi_common_queue_present(const struct wsi_device *wsi, 335 VkDevice device_h, 336 VkQueue queue_h, 337 int queue_family_index, 338 const VkPresentInfoKHR *pPresentInfo); 339 340 VkResult 341 wsi_common_create_swapchain_image(const struct wsi_device *wsi, 342 const VkImageCreateInfo *pCreateInfo, 343 VkSwapchainKHR _swapchain, 344 VkImage *pImage); 345 VkResult 346 wsi_common_bind_swapchain_image(const struct wsi_device *wsi, 347 VkImage vk_image, 348 VkSwapchainKHR _swapchain, 349 uint32_t image_idx); 350 351 bool 352 wsi_common_vk_instance_supports_present_wait(const struct vk_instance *instance); 353 354 VkImageUsageFlags 355 wsi_caps_get_image_usage(void); 356 357 #define wsi_common_vk_warn_once(warning) \ 358 do { \ 359 static int warned = false; \ 360 if (!warned) { \ 361 mesa_loge(warning); \ 362 warned = true; \ 363 } \ 364 } while (0) 365 366 #ifdef __cplusplus 367 } 368 #endif 369 370 #endif 371