vulkan/wsi: Add a helper for AcquireNextImage
[mesa.git] / src / vulkan / wsi / wsi_common.h
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 "vk_alloc.h"
30 #include <vulkan/vulkan.h>
31 #include <vulkan/vk_icd.h>
32
33 /* This is guaranteed to not collide with anything because it's in the
34 * VK_KHR_swapchain namespace but not actually used by the extension.
35 */
36 #define VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA (VkStructureType)1000001002
37 #define VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA (VkStructureType)1000001003
38
39 struct wsi_image_create_info {
40 VkStructureType sType;
41 const void *pNext;
42 bool scanout;
43 };
44
45 struct wsi_memory_allocate_info {
46 VkStructureType sType;
47 const void *pNext;
48 bool implicit_sync;
49 };
50
51 struct wsi_device;
52
53 struct wsi_swapchain {
54 const struct wsi_device *wsi;
55
56 VkDevice device;
57 VkAllocationCallbacks alloc;
58 VkFence fences[3];
59 VkPresentModeKHR present_mode;
60 uint32_t image_count;
61
62 bool use_prime_blit;
63
64 /* Command pools, one per queue family */
65 VkCommandPool *cmd_pools;
66
67 VkResult (*destroy)(struct wsi_swapchain *swapchain,
68 const VkAllocationCallbacks *pAllocator);
69 struct wsi_image *(*get_wsi_image)(struct wsi_swapchain *swapchain,
70 uint32_t image_index);
71 VkResult (*acquire_next_image)(struct wsi_swapchain *swap_chain,
72 uint64_t timeout, VkSemaphore semaphore,
73 uint32_t *image_index);
74 VkResult (*queue_present)(struct wsi_swapchain *swap_chain,
75 uint32_t image_index,
76 const VkPresentRegionKHR *damage);
77 };
78
79 struct wsi_interface {
80 VkResult (*get_support)(VkIcdSurfaceBase *surface,
81 struct wsi_device *wsi_device,
82 const VkAllocationCallbacks *alloc,
83 uint32_t queueFamilyIndex,
84 int local_fd,
85 bool can_handle_different_gpu,
86 VkBool32* pSupported);
87 VkResult (*get_capabilities)(VkIcdSurfaceBase *surface,
88 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities);
89 VkResult (*get_capabilities2)(VkIcdSurfaceBase *surface,
90 const void *info_next,
91 VkSurfaceCapabilities2KHR* pSurfaceCapabilities);
92 VkResult (*get_formats)(VkIcdSurfaceBase *surface,
93 struct wsi_device *wsi_device,
94 uint32_t* pSurfaceFormatCount,
95 VkSurfaceFormatKHR* pSurfaceFormats);
96 VkResult (*get_formats2)(VkIcdSurfaceBase *surface,
97 struct wsi_device *wsi_device,
98 const void *info_next,
99 uint32_t* pSurfaceFormatCount,
100 VkSurfaceFormat2KHR* pSurfaceFormats);
101 VkResult (*get_present_modes)(VkIcdSurfaceBase *surface,
102 uint32_t* pPresentModeCount,
103 VkPresentModeKHR* pPresentModes);
104 VkResult (*create_swapchain)(VkIcdSurfaceBase *surface,
105 VkDevice device,
106 struct wsi_device *wsi_device,
107 int local_fd,
108 const VkSwapchainCreateInfoKHR* pCreateInfo,
109 const VkAllocationCallbacks* pAllocator,
110 struct wsi_swapchain **swapchain);
111 };
112
113 #define VK_ICD_WSI_PLATFORM_MAX 5
114
115 struct wsi_device {
116 VkPhysicalDeviceMemoryProperties memory_props;
117 uint32_t queue_family_count;
118
119 uint32_t (*queue_get_family_index)(VkQueue queue);
120
121 #define WSI_CB(cb) PFN_vk##cb cb
122 WSI_CB(AllocateMemory);
123 WSI_CB(AllocateCommandBuffers);
124 WSI_CB(BindBufferMemory);
125 WSI_CB(BindImageMemory);
126 WSI_CB(BeginCommandBuffer);
127 WSI_CB(CmdCopyImageToBuffer);
128 WSI_CB(CreateBuffer);
129 WSI_CB(CreateCommandPool);
130 WSI_CB(CreateFence);
131 WSI_CB(CreateImage);
132 WSI_CB(DestroyBuffer);
133 WSI_CB(DestroyCommandPool);
134 WSI_CB(DestroyFence);
135 WSI_CB(DestroyImage);
136 WSI_CB(EndCommandBuffer);
137 WSI_CB(FreeMemory);
138 WSI_CB(FreeCommandBuffers);
139 WSI_CB(GetBufferMemoryRequirements);
140 WSI_CB(GetImageMemoryRequirements);
141 WSI_CB(GetImageSubresourceLayout);
142 WSI_CB(GetMemoryFdKHR);
143 WSI_CB(ResetFences);
144 WSI_CB(QueueSubmit);
145 WSI_CB(WaitForFences);
146 #undef WSI_CB
147
148 struct wsi_interface * wsi[VK_ICD_WSI_PLATFORM_MAX];
149 };
150
151 typedef PFN_vkVoidFunction (VKAPI_PTR *WSI_FN_GetPhysicalDeviceProcAddr)(VkPhysicalDevice physicalDevice, const char* pName);
152
153 void
154 wsi_device_init(struct wsi_device *wsi,
155 VkPhysicalDevice pdevice,
156 WSI_FN_GetPhysicalDeviceProcAddr proc_addr);
157
158 #define WSI_CB(cb) PFN_vk##cb cb
159 struct wsi_callbacks {
160 VkPhysicalDevice (*device_get_physical)(VkDevice);
161
162 WSI_CB(GetDeviceProcAddr);
163 WSI_CB(GetPhysicalDeviceFormatProperties);
164 WSI_CB(GetPhysicalDeviceMemoryProperties);
165 WSI_CB(GetPhysicalDeviceQueueFamilyProperties);
166 };
167 #undef WSI_CB
168
169 #define WSI_DEFINE_NONDISP_HANDLE_CASTS(__wsi_type, __VkType) \
170 \
171 static inline struct __wsi_type * \
172 __wsi_type ## _from_handle(__VkType _handle) \
173 { \
174 return (struct __wsi_type *)(uintptr_t) _handle; \
175 } \
176 \
177 static inline __VkType \
178 __wsi_type ## _to_handle(struct __wsi_type *_obj) \
179 { \
180 return (__VkType)(uintptr_t) _obj; \
181 }
182
183 #define WSI_FROM_HANDLE(__wsi_type, __name, __handle) \
184 struct __wsi_type *__name = __wsi_type ## _from_handle(__handle)
185
186 WSI_DEFINE_NONDISP_HANDLE_CASTS(wsi_swapchain, VkSwapchainKHR)
187
188 #define ICD_DEFINE_NONDISP_HANDLE_CASTS(__VkIcdType, __VkType) \
189 \
190 static inline __VkIcdType * \
191 __VkIcdType ## _from_handle(__VkType _handle) \
192 { \
193 return (__VkIcdType *)(uintptr_t) _handle; \
194 } \
195 \
196 static inline __VkType \
197 __VkIcdType ## _to_handle(__VkIcdType *_obj) \
198 { \
199 return (__VkType)(uintptr_t) _obj; \
200 }
201
202 #define ICD_FROM_HANDLE(__VkIcdType, __name, __handle) \
203 __VkIcdType *__name = __VkIcdType ## _from_handle(__handle)
204
205 ICD_DEFINE_NONDISP_HANDLE_CASTS(VkIcdSurfaceBase, VkSurfaceKHR)
206
207 VkResult wsi_x11_init_wsi(struct wsi_device *wsi_device,
208 const VkAllocationCallbacks *alloc);
209 void wsi_x11_finish_wsi(struct wsi_device *wsi_device,
210 const VkAllocationCallbacks *alloc);
211 VkResult wsi_wl_init_wsi(struct wsi_device *wsi_device,
212 const VkAllocationCallbacks *alloc,
213 VkPhysicalDevice physical_device,
214 const struct wsi_callbacks *cbs);
215 void wsi_wl_finish_wsi(struct wsi_device *wsi_device,
216 const VkAllocationCallbacks *alloc);
217
218 VkResult
219 wsi_common_get_images(VkSwapchainKHR _swapchain,
220 uint32_t *pSwapchainImageCount,
221 VkImage *pSwapchainImages);
222
223 VkResult
224 wsi_common_acquire_next_image(const struct wsi_device *wsi,
225 VkDevice device,
226 VkSwapchainKHR swapchain,
227 uint64_t timeout,
228 VkSemaphore semaphore,
229 uint32_t *pImageIndex);
230
231 VkResult
232 wsi_common_create_swapchain(struct wsi_device *wsi,
233 VkDevice device,
234 int fd,
235 const VkSwapchainCreateInfoKHR *pCreateInfo,
236 const VkAllocationCallbacks *pAllocator,
237 VkSwapchainKHR *pSwapchain);
238 void
239 wsi_common_destroy_swapchain(VkDevice device,
240 VkSwapchainKHR swapchain,
241 const VkAllocationCallbacks *pAllocator);
242
243 VkResult
244 wsi_common_queue_present(const struct wsi_device *wsi,
245 VkDevice device_h,
246 VkQueue queue_h,
247 int queue_family_index,
248 const VkPresentInfoKHR *pPresentInfo);
249
250 #endif