32a7e731321df6d6849e3398d939d1a576a8751a
[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 struct wsi_swapchain;
53
54 struct wsi_interface {
55 VkResult (*get_support)(VkIcdSurfaceBase *surface,
56 struct wsi_device *wsi_device,
57 const VkAllocationCallbacks *alloc,
58 uint32_t queueFamilyIndex,
59 int local_fd,
60 VkBool32* pSupported);
61 VkResult (*get_capabilities)(VkIcdSurfaceBase *surface,
62 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities);
63 VkResult (*get_capabilities2)(VkIcdSurfaceBase *surface,
64 const void *info_next,
65 VkSurfaceCapabilities2KHR* pSurfaceCapabilities);
66 VkResult (*get_formats)(VkIcdSurfaceBase *surface,
67 struct wsi_device *wsi_device,
68 uint32_t* pSurfaceFormatCount,
69 VkSurfaceFormatKHR* pSurfaceFormats);
70 VkResult (*get_formats2)(VkIcdSurfaceBase *surface,
71 struct wsi_device *wsi_device,
72 const void *info_next,
73 uint32_t* pSurfaceFormatCount,
74 VkSurfaceFormat2KHR* pSurfaceFormats);
75 VkResult (*get_present_modes)(VkIcdSurfaceBase *surface,
76 uint32_t* pPresentModeCount,
77 VkPresentModeKHR* pPresentModes);
78 VkResult (*create_swapchain)(VkIcdSurfaceBase *surface,
79 VkDevice device,
80 struct wsi_device *wsi_device,
81 int local_fd,
82 const VkSwapchainCreateInfoKHR* pCreateInfo,
83 const VkAllocationCallbacks* pAllocator,
84 struct wsi_swapchain **swapchain);
85 };
86
87 #define VK_ICD_WSI_PLATFORM_MAX 5
88
89 struct wsi_device {
90 VkPhysicalDeviceMemoryProperties memory_props;
91 uint32_t queue_family_count;
92
93 uint32_t (*queue_get_family_index)(VkQueue queue);
94
95 #define WSI_CB(cb) PFN_vk##cb cb
96 WSI_CB(AllocateMemory);
97 WSI_CB(AllocateCommandBuffers);
98 WSI_CB(BindBufferMemory);
99 WSI_CB(BindImageMemory);
100 WSI_CB(BeginCommandBuffer);
101 WSI_CB(CmdCopyImageToBuffer);
102 WSI_CB(CreateBuffer);
103 WSI_CB(CreateCommandPool);
104 WSI_CB(CreateFence);
105 WSI_CB(CreateImage);
106 WSI_CB(DestroyBuffer);
107 WSI_CB(DestroyCommandPool);
108 WSI_CB(DestroyFence);
109 WSI_CB(DestroyImage);
110 WSI_CB(EndCommandBuffer);
111 WSI_CB(FreeMemory);
112 WSI_CB(FreeCommandBuffers);
113 WSI_CB(GetBufferMemoryRequirements);
114 WSI_CB(GetImageMemoryRequirements);
115 WSI_CB(GetImageSubresourceLayout);
116 WSI_CB(GetMemoryFdKHR);
117 WSI_CB(ResetFences);
118 WSI_CB(QueueSubmit);
119 WSI_CB(WaitForFences);
120 #undef WSI_CB
121
122 struct wsi_interface * wsi[VK_ICD_WSI_PLATFORM_MAX];
123 };
124
125 typedef PFN_vkVoidFunction (VKAPI_PTR *WSI_FN_GetPhysicalDeviceProcAddr)(VkPhysicalDevice physicalDevice, const char* pName);
126
127 void
128 wsi_device_init(struct wsi_device *wsi,
129 VkPhysicalDevice pdevice,
130 WSI_FN_GetPhysicalDeviceProcAddr proc_addr);
131
132 #define WSI_CB(cb) PFN_vk##cb cb
133 struct wsi_callbacks {
134 VkPhysicalDevice (*device_get_physical)(VkDevice);
135
136 WSI_CB(GetDeviceProcAddr);
137 WSI_CB(GetPhysicalDeviceFormatProperties);
138 WSI_CB(GetPhysicalDeviceMemoryProperties);
139 WSI_CB(GetPhysicalDeviceQueueFamilyProperties);
140 };
141 #undef WSI_CB
142
143 #define ICD_DEFINE_NONDISP_HANDLE_CASTS(__VkIcdType, __VkType) \
144 \
145 static inline __VkIcdType * \
146 __VkIcdType ## _from_handle(__VkType _handle) \
147 { \
148 return (__VkIcdType *)(uintptr_t) _handle; \
149 } \
150 \
151 static inline __VkType \
152 __VkIcdType ## _to_handle(__VkIcdType *_obj) \
153 { \
154 return (__VkType)(uintptr_t) _obj; \
155 }
156
157 #define ICD_FROM_HANDLE(__VkIcdType, __name, __handle) \
158 __VkIcdType *__name = __VkIcdType ## _from_handle(__handle)
159
160 ICD_DEFINE_NONDISP_HANDLE_CASTS(VkIcdSurfaceBase, VkSurfaceKHR)
161
162 VkResult wsi_x11_init_wsi(struct wsi_device *wsi_device,
163 const VkAllocationCallbacks *alloc);
164 void wsi_x11_finish_wsi(struct wsi_device *wsi_device,
165 const VkAllocationCallbacks *alloc);
166 VkResult wsi_wl_init_wsi(struct wsi_device *wsi_device,
167 const VkAllocationCallbacks *alloc,
168 VkPhysicalDevice physical_device,
169 const struct wsi_callbacks *cbs);
170 void wsi_wl_finish_wsi(struct wsi_device *wsi_device,
171 const VkAllocationCallbacks *alloc);
172
173 VkResult
174 wsi_common_get_images(VkSwapchainKHR _swapchain,
175 uint32_t *pSwapchainImageCount,
176 VkImage *pSwapchainImages);
177
178 VkResult
179 wsi_common_acquire_next_image(const struct wsi_device *wsi,
180 VkDevice device,
181 VkSwapchainKHR swapchain,
182 uint64_t timeout,
183 VkSemaphore semaphore,
184 uint32_t *pImageIndex);
185
186 VkResult
187 wsi_common_create_swapchain(struct wsi_device *wsi,
188 VkDevice device,
189 int fd,
190 const VkSwapchainCreateInfoKHR *pCreateInfo,
191 const VkAllocationCallbacks *pAllocator,
192 VkSwapchainKHR *pSwapchain);
193 void
194 wsi_common_destroy_swapchain(VkDevice device,
195 VkSwapchainKHR swapchain,
196 const VkAllocationCallbacks *pAllocator);
197
198 VkResult
199 wsi_common_queue_present(const struct wsi_device *wsi,
200 VkDevice device_h,
201 VkQueue queue_h,
202 int queue_family_index,
203 const VkPresentInfoKHR *pPresentInfo);
204
205 #endif