1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2015 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci#ifndef WSI_COMMON_H 24bf215546Sopenharmony_ci#define WSI_COMMON_H 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci#include <stdint.h> 27bf215546Sopenharmony_ci#include <stdbool.h> 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci#include "vk_alloc.h" 30bf215546Sopenharmony_ci#include "vk_dispatch_table.h" 31bf215546Sopenharmony_ci#include <vulkan/vulkan.h> 32bf215546Sopenharmony_ci#include <vulkan/vk_icd.h> 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_ci#ifdef __cplusplus 35bf215546Sopenharmony_ciextern "C" { 36bf215546Sopenharmony_ci#endif 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_ci#ifndef WSI_ENTRYPOINTS_H 39bf215546Sopenharmony_ciextern const struct vk_instance_entrypoint_table wsi_instance_entrypoints; 40bf215546Sopenharmony_ciextern const struct vk_physical_device_entrypoint_table wsi_physical_device_entrypoints; 41bf215546Sopenharmony_ciextern const struct vk_device_entrypoint_table wsi_device_entrypoints; 42bf215546Sopenharmony_ci#endif 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci#include <util/list.h> 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci/* This is guaranteed to not collide with anything because it's in the 47bf215546Sopenharmony_ci * VK_KHR_swapchain namespace but not actually used by the extension. 48bf215546Sopenharmony_ci */ 49bf215546Sopenharmony_ci#define VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA (VkStructureType)1000001002 50bf215546Sopenharmony_ci#define VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA (VkStructureType)1000001003 51bf215546Sopenharmony_ci#define VK_STRUCTURE_TYPE_WSI_SURFACE_SUPPORTED_COUNTERS_MESA (VkStructureType)1000001005 52bf215546Sopenharmony_ci#define VK_STRUCTURE_TYPE_WSI_MEMORY_SIGNAL_SUBMIT_INFO_MESA (VkStructureType)1000001006 53bf215546Sopenharmony_ci 54bf215546Sopenharmony_ci/* This is always chained to VkImageCreateInfo when a wsi image is created. 55bf215546Sopenharmony_ci * It indicates that the image can be transitioned to/from 56bf215546Sopenharmony_ci * VK_IMAGE_LAYOUT_PRESENT_SRC_KHR. 57bf215546Sopenharmony_ci */ 58bf215546Sopenharmony_cistruct wsi_image_create_info { 59bf215546Sopenharmony_ci VkStructureType sType; 60bf215546Sopenharmony_ci const void *pNext; 61bf215546Sopenharmony_ci bool scanout; 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci /* if true, the image is a buffer blit source */ 64bf215546Sopenharmony_ci bool buffer_blit_src; 65bf215546Sopenharmony_ci}; 66bf215546Sopenharmony_ci 67bf215546Sopenharmony_cistruct wsi_memory_allocate_info { 68bf215546Sopenharmony_ci VkStructureType sType; 69bf215546Sopenharmony_ci const void *pNext; 70bf215546Sopenharmony_ci bool implicit_sync; 71bf215546Sopenharmony_ci}; 72bf215546Sopenharmony_ci 73bf215546Sopenharmony_ci/* To be chained into VkSurfaceCapabilities2KHR */ 74bf215546Sopenharmony_cistruct wsi_surface_supported_counters { 75bf215546Sopenharmony_ci VkStructureType sType; 76bf215546Sopenharmony_ci const void *pNext; 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_ci VkSurfaceCounterFlagsEXT supported_surface_counters; 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci}; 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_ci/* To be chained into VkSubmitInfo */ 83bf215546Sopenharmony_cistruct wsi_memory_signal_submit_info { 84bf215546Sopenharmony_ci VkStructureType sType; 85bf215546Sopenharmony_ci const void *pNext; 86bf215546Sopenharmony_ci VkDeviceMemory memory; 87bf215546Sopenharmony_ci}; 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_cistruct wsi_interface; 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_cistruct driOptionCache; 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ci#define VK_ICD_WSI_PLATFORM_MAX (VK_ICD_WSI_PLATFORM_DISPLAY + 1) 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_cistruct wsi_device { 96bf215546Sopenharmony_ci /* Allocator for the instance */ 97bf215546Sopenharmony_ci VkAllocationCallbacks instance_alloc; 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_ci VkPhysicalDevice pdevice; 100bf215546Sopenharmony_ci VkPhysicalDeviceMemoryProperties memory_props; 101bf215546Sopenharmony_ci uint32_t queue_family_count; 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci VkPhysicalDevicePCIBusInfoPropertiesEXT pci_bus_info; 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_ci VkExternalSemaphoreHandleTypeFlags semaphore_export_handle_types; 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci bool has_import_memory_host; 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci bool supports_modifiers; 110bf215546Sopenharmony_ci uint32_t maxImageDimension2D; 111bf215546Sopenharmony_ci uint32_t optimalBufferCopyRowPitchAlignment; 112bf215546Sopenharmony_ci VkPresentModeKHR override_present_mode; 113bf215546Sopenharmony_ci bool force_bgra8_unorm_first; 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci /* Whether to enable adaptive sync for a swapchain if implemented and 116bf215546Sopenharmony_ci * available. Not all window systems might support this. */ 117bf215546Sopenharmony_ci bool enable_adaptive_sync; 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_ci /* List of fences to signal when hotplug event happens. */ 120bf215546Sopenharmony_ci struct list_head hotplug_fences; 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci struct { 123bf215546Sopenharmony_ci /* Override the minimum number of images on the swapchain. 124bf215546Sopenharmony_ci * 0 = no override */ 125bf215546Sopenharmony_ci uint32_t override_minImageCount; 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ci /* Forces strict number of image on the swapchain using application 128bf215546Sopenharmony_ci * provided VkSwapchainCreateInfoKH::RminImageCount. 129bf215546Sopenharmony_ci */ 130bf215546Sopenharmony_ci bool strict_imageCount; 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ci /* Ensures to create at least the number of image specified by the 133bf215546Sopenharmony_ci * driver in VkSurfaceCapabilitiesKHR::minImageCount. 134bf215546Sopenharmony_ci */ 135bf215546Sopenharmony_ci bool ensure_minImageCount; 136bf215546Sopenharmony_ci 137bf215546Sopenharmony_ci /* Wait for fences before submitting buffers to Xwayland. Defaults to 138bf215546Sopenharmony_ci * true. 139bf215546Sopenharmony_ci */ 140bf215546Sopenharmony_ci bool xwaylandWaitReady; 141bf215546Sopenharmony_ci } x11; 142bf215546Sopenharmony_ci 143bf215546Sopenharmony_ci bool sw; 144bf215546Sopenharmony_ci 145bf215546Sopenharmony_ci /* Set to true if the implementation is ok with linear WSI images. */ 146bf215546Sopenharmony_ci bool wants_linear; 147bf215546Sopenharmony_ci 148bf215546Sopenharmony_ci /* Signals the semaphore such that any wait on the semaphore will wait on 149bf215546Sopenharmony_ci * any reads or writes on the give memory object. This is used to 150bf215546Sopenharmony_ci * implement the semaphore signal operation in vkAcquireNextImage. This 151bf215546Sopenharmony_ci * requires the driver to implement vk_device::create_sync_for_memory. 152bf215546Sopenharmony_ci */ 153bf215546Sopenharmony_ci bool signal_semaphore_with_memory; 154bf215546Sopenharmony_ci 155bf215546Sopenharmony_ci /* Signals the fence such that any wait on the fence will wait on any reads 156bf215546Sopenharmony_ci * or writes on the give memory object. This is used to implement the 157bf215546Sopenharmony_ci * semaphore signal operation in vkAcquireNextImage. This requires the 158bf215546Sopenharmony_ci * driver to implement vk_device::create_sync_for_memory. The resulting 159bf215546Sopenharmony_ci * vk_sync must support CPU waits. 160bf215546Sopenharmony_ci */ 161bf215546Sopenharmony_ci bool signal_fence_with_memory; 162bf215546Sopenharmony_ci 163bf215546Sopenharmony_ci /* 164bf215546Sopenharmony_ci * This sets the ownership for a WSI memory object: 165bf215546Sopenharmony_ci * 166bf215546Sopenharmony_ci * The ownership is true if and only if the application is allowed to submit 167bf215546Sopenharmony_ci * command buffers that reference the buffer. 168bf215546Sopenharmony_ci * 169bf215546Sopenharmony_ci * This can be used to prune BO lists without too many adverse affects on 170bf215546Sopenharmony_ci * implicit sync. 171bf215546Sopenharmony_ci * 172bf215546Sopenharmony_ci * Side note: care needs to be taken for internally delayed submissions wrt 173bf215546Sopenharmony_ci * timeline semaphores. 174bf215546Sopenharmony_ci */ 175bf215546Sopenharmony_ci void (*set_memory_ownership)(VkDevice device, 176bf215546Sopenharmony_ci VkDeviceMemory memory, 177bf215546Sopenharmony_ci VkBool32 ownership); 178bf215546Sopenharmony_ci 179bf215546Sopenharmony_ci /* 180bf215546Sopenharmony_ci * If this is set, the WSI device will call it to let the driver backend 181bf215546Sopenharmony_ci * decide if it can present images directly on the given device fd. 182bf215546Sopenharmony_ci */ 183bf215546Sopenharmony_ci bool (*can_present_on_device)(VkPhysicalDevice pdevice, int fd); 184bf215546Sopenharmony_ci 185bf215546Sopenharmony_ci /* 186bf215546Sopenharmony_ci * A driver can implement this callback to return a special queue to execute 187bf215546Sopenharmony_ci * buffer blits. 188bf215546Sopenharmony_ci */ 189bf215546Sopenharmony_ci VkQueue (*get_buffer_blit_queue)(VkDevice device); 190bf215546Sopenharmony_ci 191bf215546Sopenharmony_ci#define WSI_CB(cb) PFN_vk##cb cb 192bf215546Sopenharmony_ci WSI_CB(AllocateMemory); 193bf215546Sopenharmony_ci WSI_CB(AllocateCommandBuffers); 194bf215546Sopenharmony_ci WSI_CB(BindBufferMemory); 195bf215546Sopenharmony_ci WSI_CB(BindImageMemory); 196bf215546Sopenharmony_ci WSI_CB(BeginCommandBuffer); 197bf215546Sopenharmony_ci WSI_CB(CmdPipelineBarrier); 198bf215546Sopenharmony_ci WSI_CB(CmdCopyImageToBuffer); 199bf215546Sopenharmony_ci WSI_CB(CreateBuffer); 200bf215546Sopenharmony_ci WSI_CB(CreateCommandPool); 201bf215546Sopenharmony_ci WSI_CB(CreateFence); 202bf215546Sopenharmony_ci WSI_CB(CreateImage); 203bf215546Sopenharmony_ci WSI_CB(CreateSemaphore); 204bf215546Sopenharmony_ci WSI_CB(DestroyBuffer); 205bf215546Sopenharmony_ci WSI_CB(DestroyCommandPool); 206bf215546Sopenharmony_ci WSI_CB(DestroyFence); 207bf215546Sopenharmony_ci WSI_CB(DestroyImage); 208bf215546Sopenharmony_ci WSI_CB(DestroySemaphore); 209bf215546Sopenharmony_ci WSI_CB(EndCommandBuffer); 210bf215546Sopenharmony_ci WSI_CB(FreeMemory); 211bf215546Sopenharmony_ci WSI_CB(FreeCommandBuffers); 212bf215546Sopenharmony_ci WSI_CB(GetBufferMemoryRequirements); 213bf215546Sopenharmony_ci WSI_CB(GetImageDrmFormatModifierPropertiesEXT); 214bf215546Sopenharmony_ci WSI_CB(GetImageMemoryRequirements); 215bf215546Sopenharmony_ci WSI_CB(GetImageSubresourceLayout); 216bf215546Sopenharmony_ci WSI_CB(GetMemoryFdKHR); 217bf215546Sopenharmony_ci WSI_CB(GetPhysicalDeviceFormatProperties); 218bf215546Sopenharmony_ci WSI_CB(GetPhysicalDeviceFormatProperties2KHR); 219bf215546Sopenharmony_ci WSI_CB(GetPhysicalDeviceImageFormatProperties2); 220bf215546Sopenharmony_ci WSI_CB(GetSemaphoreFdKHR); 221bf215546Sopenharmony_ci WSI_CB(ResetFences); 222bf215546Sopenharmony_ci WSI_CB(QueueSubmit); 223bf215546Sopenharmony_ci WSI_CB(WaitForFences); 224bf215546Sopenharmony_ci WSI_CB(MapMemory); 225bf215546Sopenharmony_ci WSI_CB(UnmapMemory); 226bf215546Sopenharmony_ci#undef WSI_CB 227bf215546Sopenharmony_ci 228bf215546Sopenharmony_ci struct wsi_interface * wsi[VK_ICD_WSI_PLATFORM_MAX]; 229bf215546Sopenharmony_ci}; 230bf215546Sopenharmony_ci 231bf215546Sopenharmony_citypedef PFN_vkVoidFunction (VKAPI_PTR *WSI_FN_GetPhysicalDeviceProcAddr)(VkPhysicalDevice physicalDevice, const char* pName); 232bf215546Sopenharmony_ci 233bf215546Sopenharmony_ciVkResult 234bf215546Sopenharmony_ciwsi_device_init(struct wsi_device *wsi, 235bf215546Sopenharmony_ci VkPhysicalDevice pdevice, 236bf215546Sopenharmony_ci WSI_FN_GetPhysicalDeviceProcAddr proc_addr, 237bf215546Sopenharmony_ci const VkAllocationCallbacks *alloc, 238bf215546Sopenharmony_ci int display_fd, 239bf215546Sopenharmony_ci const struct driOptionCache *dri_options, 240bf215546Sopenharmony_ci bool sw_device); 241bf215546Sopenharmony_ci 242bf215546Sopenharmony_civoid 243bf215546Sopenharmony_ciwsi_device_finish(struct wsi_device *wsi, 244bf215546Sopenharmony_ci const VkAllocationCallbacks *alloc); 245bf215546Sopenharmony_ci 246bf215546Sopenharmony_ci/* Setup file descriptor to be used with imported sync_fd's in wsi fences. */ 247bf215546Sopenharmony_civoid 248bf215546Sopenharmony_ciwsi_device_setup_syncobj_fd(struct wsi_device *wsi_device, 249bf215546Sopenharmony_ci int fd); 250bf215546Sopenharmony_ci 251bf215546Sopenharmony_ci#define ICD_DEFINE_NONDISP_HANDLE_CASTS(__VkIcdType, __VkType) \ 252bf215546Sopenharmony_ci \ 253bf215546Sopenharmony_ci static inline __VkIcdType * \ 254bf215546Sopenharmony_ci __VkIcdType ## _from_handle(__VkType _handle) \ 255bf215546Sopenharmony_ci { \ 256bf215546Sopenharmony_ci return (__VkIcdType *)(uintptr_t) _handle; \ 257bf215546Sopenharmony_ci } \ 258bf215546Sopenharmony_ci \ 259bf215546Sopenharmony_ci static inline __VkType \ 260bf215546Sopenharmony_ci __VkIcdType ## _to_handle(__VkIcdType *_obj) \ 261bf215546Sopenharmony_ci { \ 262bf215546Sopenharmony_ci return (__VkType)(uintptr_t) _obj; \ 263bf215546Sopenharmony_ci } 264bf215546Sopenharmony_ci 265bf215546Sopenharmony_ci#define ICD_FROM_HANDLE(__VkIcdType, __name, __handle) \ 266bf215546Sopenharmony_ci __VkIcdType *__name = __VkIcdType ## _from_handle(__handle) 267bf215546Sopenharmony_ci 268bf215546Sopenharmony_ciICD_DEFINE_NONDISP_HANDLE_CASTS(VkIcdSurfaceBase, VkSurfaceKHR) 269bf215546Sopenharmony_ci 270bf215546Sopenharmony_ciVkResult 271bf215546Sopenharmony_ciwsi_common_get_images(VkSwapchainKHR _swapchain, 272bf215546Sopenharmony_ci uint32_t *pSwapchainImageCount, 273bf215546Sopenharmony_ci VkImage *pSwapchainImages); 274bf215546Sopenharmony_ci 275bf215546Sopenharmony_ciVkImage 276bf215546Sopenharmony_ciwsi_common_get_image(VkSwapchainKHR _swapchain, uint32_t index); 277bf215546Sopenharmony_ci 278bf215546Sopenharmony_ciVkResult 279bf215546Sopenharmony_ciwsi_common_acquire_next_image2(const struct wsi_device *wsi, 280bf215546Sopenharmony_ci VkDevice device, 281bf215546Sopenharmony_ci const VkAcquireNextImageInfoKHR *pAcquireInfo, 282bf215546Sopenharmony_ci uint32_t *pImageIndex); 283bf215546Sopenharmony_ci 284bf215546Sopenharmony_ciVkResult 285bf215546Sopenharmony_ciwsi_common_queue_present(const struct wsi_device *wsi, 286bf215546Sopenharmony_ci VkDevice device_h, 287bf215546Sopenharmony_ci VkQueue queue_h, 288bf215546Sopenharmony_ci int queue_family_index, 289bf215546Sopenharmony_ci const VkPresentInfoKHR *pPresentInfo); 290bf215546Sopenharmony_ci 291bf215546Sopenharmony_ciVkResult 292bf215546Sopenharmony_ciwsi_common_create_swapchain_image(const struct wsi_device *wsi, 293bf215546Sopenharmony_ci const VkImageCreateInfo *pCreateInfo, 294bf215546Sopenharmony_ci VkSwapchainKHR _swapchain, 295bf215546Sopenharmony_ci VkImage *pImage); 296bf215546Sopenharmony_ciVkResult 297bf215546Sopenharmony_ciwsi_common_bind_swapchain_image(const struct wsi_device *wsi, 298bf215546Sopenharmony_ci VkImage vk_image, 299bf215546Sopenharmony_ci VkSwapchainKHR _swapchain, 300bf215546Sopenharmony_ci uint32_t image_idx); 301bf215546Sopenharmony_ci 302bf215546Sopenharmony_ci#ifdef __cplusplus 303bf215546Sopenharmony_ci} 304bf215546Sopenharmony_ci#endif 305bf215546Sopenharmony_ci 306bf215546Sopenharmony_ci#endif 307