vulkan/wsi: Use VK_EXT_pci_bus_info for DRM fd matching
[mesa.git] / src / intel / vulkan / anv_wsi.c
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
24 #include "anv_private.h"
25 #include "wsi_common.h"
26 #include "vk_format_info.h"
27 #include "vk_util.h"
28
29 static PFN_vkVoidFunction
30 anv_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
31 {
32 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
33 return anv_lookup_entrypoint(&physical_device->info, pName);
34 }
35
36 static uint64_t
37 anv_wsi_image_get_modifier(VkImage _image)
38 {
39 ANV_FROM_HANDLE(anv_image, image, _image);
40 return image->drm_format_mod;
41 }
42
43 VkResult
44 anv_init_wsi(struct anv_physical_device *physical_device)
45 {
46 VkResult result;
47
48 result = wsi_device_init(&physical_device->wsi_device,
49 anv_physical_device_to_handle(physical_device),
50 anv_wsi_proc_addr,
51 &physical_device->instance->alloc,
52 physical_device->master_fd);
53 if (result != VK_SUCCESS)
54 return result;
55
56 physical_device->wsi_device.supports_modifiers = true;
57 physical_device->wsi_device.image_get_modifier = anv_wsi_image_get_modifier;
58
59 return VK_SUCCESS;
60 }
61
62 void
63 anv_finish_wsi(struct anv_physical_device *physical_device)
64 {
65 wsi_device_finish(&physical_device->wsi_device,
66 &physical_device->instance->alloc);
67 }
68
69 void anv_DestroySurfaceKHR(
70 VkInstance _instance,
71 VkSurfaceKHR _surface,
72 const VkAllocationCallbacks* pAllocator)
73 {
74 ANV_FROM_HANDLE(anv_instance, instance, _instance);
75 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
76
77 if (!surface)
78 return;
79
80 vk_free2(&instance->alloc, pAllocator, surface);
81 }
82
83 VkResult anv_GetPhysicalDeviceSurfaceSupportKHR(
84 VkPhysicalDevice physicalDevice,
85 uint32_t queueFamilyIndex,
86 VkSurfaceKHR surface,
87 VkBool32* pSupported)
88 {
89 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
90
91 return wsi_common_get_surface_support(&device->wsi_device,
92 queueFamilyIndex,
93 surface,
94 pSupported);
95 }
96
97 VkResult anv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
98 VkPhysicalDevice physicalDevice,
99 VkSurfaceKHR surface,
100 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
101 {
102 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
103
104 return wsi_common_get_surface_capabilities(&device->wsi_device,
105 surface,
106 pSurfaceCapabilities);
107 }
108
109 VkResult anv_GetPhysicalDeviceSurfaceCapabilities2KHR(
110 VkPhysicalDevice physicalDevice,
111 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
112 VkSurfaceCapabilities2KHR* pSurfaceCapabilities)
113 {
114 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
115
116 return wsi_common_get_surface_capabilities2(&device->wsi_device,
117 pSurfaceInfo,
118 pSurfaceCapabilities);
119 }
120
121 VkResult anv_GetPhysicalDeviceSurfaceCapabilities2EXT(
122 VkPhysicalDevice physicalDevice,
123 VkSurfaceKHR surface,
124 VkSurfaceCapabilities2EXT* pSurfaceCapabilities)
125 {
126 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
127
128 return wsi_common_get_surface_capabilities2ext(&device->wsi_device,
129 surface,
130 pSurfaceCapabilities);
131 }
132
133 VkResult anv_GetPhysicalDeviceSurfaceFormatsKHR(
134 VkPhysicalDevice physicalDevice,
135 VkSurfaceKHR surface,
136 uint32_t* pSurfaceFormatCount,
137 VkSurfaceFormatKHR* pSurfaceFormats)
138 {
139 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
140
141 return wsi_common_get_surface_formats(&device->wsi_device, surface,
142 pSurfaceFormatCount, pSurfaceFormats);
143 }
144
145 VkResult anv_GetPhysicalDeviceSurfaceFormats2KHR(
146 VkPhysicalDevice physicalDevice,
147 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
148 uint32_t* pSurfaceFormatCount,
149 VkSurfaceFormat2KHR* pSurfaceFormats)
150 {
151 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
152
153 return wsi_common_get_surface_formats2(&device->wsi_device, pSurfaceInfo,
154 pSurfaceFormatCount, pSurfaceFormats);
155 }
156
157 VkResult anv_GetPhysicalDeviceSurfacePresentModesKHR(
158 VkPhysicalDevice physicalDevice,
159 VkSurfaceKHR surface,
160 uint32_t* pPresentModeCount,
161 VkPresentModeKHR* pPresentModes)
162 {
163 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
164
165 return wsi_common_get_surface_present_modes(&device->wsi_device, surface,
166 pPresentModeCount,
167 pPresentModes);
168 }
169
170 VkResult anv_CreateSwapchainKHR(
171 VkDevice _device,
172 const VkSwapchainCreateInfoKHR* pCreateInfo,
173 const VkAllocationCallbacks* pAllocator,
174 VkSwapchainKHR* pSwapchain)
175 {
176 ANV_FROM_HANDLE(anv_device, device, _device);
177 struct wsi_device *wsi_device = &device->instance->physicalDevice.wsi_device;
178 const VkAllocationCallbacks *alloc;
179
180 if (pAllocator)
181 alloc = pAllocator;
182 else
183 alloc = &device->alloc;
184
185 return wsi_common_create_swapchain(wsi_device, _device,
186 pCreateInfo, alloc, pSwapchain);
187 }
188
189 void anv_DestroySwapchainKHR(
190 VkDevice _device,
191 VkSwapchainKHR swapchain,
192 const VkAllocationCallbacks* pAllocator)
193 {
194 ANV_FROM_HANDLE(anv_device, device, _device);
195 const VkAllocationCallbacks *alloc;
196
197 if (pAllocator)
198 alloc = pAllocator;
199 else
200 alloc = &device->alloc;
201
202 wsi_common_destroy_swapchain(_device, swapchain, alloc);
203 }
204
205 VkResult anv_GetSwapchainImagesKHR(
206 VkDevice device,
207 VkSwapchainKHR swapchain,
208 uint32_t* pSwapchainImageCount,
209 VkImage* pSwapchainImages)
210 {
211 return wsi_common_get_images(swapchain,
212 pSwapchainImageCount,
213 pSwapchainImages);
214 }
215
216 VkResult anv_AcquireNextImageKHR(
217 VkDevice device,
218 VkSwapchainKHR swapchain,
219 uint64_t timeout,
220 VkSemaphore semaphore,
221 VkFence fence,
222 uint32_t* pImageIndex)
223 {
224 VkAcquireNextImageInfoKHR acquire_info = {
225 .sType = VK_STRUCTURE_TYPE_ACQUIRE_NEXT_IMAGE_INFO_KHR,
226 .swapchain = swapchain,
227 .timeout = timeout,
228 .semaphore = semaphore,
229 .fence = fence,
230 .deviceMask = 0,
231 };
232
233 return anv_AcquireNextImage2KHR(device, &acquire_info, pImageIndex);
234 }
235
236 VkResult anv_AcquireNextImage2KHR(
237 VkDevice _device,
238 const VkAcquireNextImageInfoKHR* pAcquireInfo,
239 uint32_t* pImageIndex)
240 {
241 ANV_FROM_HANDLE(anv_device, device, _device);
242 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
243
244 VkResult result = wsi_common_acquire_next_image2(&pdevice->wsi_device,
245 _device,
246 pAcquireInfo,
247 pImageIndex);
248
249 /* Thanks to implicit sync, the image is ready immediately. However, we
250 * should wait for the current GPU state to finish.
251 */
252 if (pAcquireInfo->fence != VK_NULL_HANDLE) {
253 anv_QueueSubmit(anv_queue_to_handle(&device->queue), 0, NULL,
254 pAcquireInfo->fence);
255 }
256
257 return result;
258 }
259
260 VkResult anv_QueuePresentKHR(
261 VkQueue _queue,
262 const VkPresentInfoKHR* pPresentInfo)
263 {
264 ANV_FROM_HANDLE(anv_queue, queue, _queue);
265 struct anv_physical_device *pdevice =
266 &queue->device->instance->physicalDevice;
267
268 return wsi_common_queue_present(&pdevice->wsi_device,
269 anv_device_to_handle(queue->device),
270 _queue, 0,
271 pPresentInfo);
272 }
273
274 VkResult anv_GetDeviceGroupPresentCapabilitiesKHR(
275 VkDevice device,
276 VkDeviceGroupPresentCapabilitiesKHR* pCapabilities)
277 {
278 memset(pCapabilities->presentMask, 0,
279 sizeof(pCapabilities->presentMask));
280 pCapabilities->presentMask[0] = 0x1;
281 pCapabilities->modes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
282
283 return VK_SUCCESS;
284 }
285
286 VkResult anv_GetDeviceGroupSurfacePresentModesKHR(
287 VkDevice device,
288 VkSurfaceKHR surface,
289 VkDeviceGroupPresentModeFlagsKHR* pModes)
290 {
291 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
292
293 return VK_SUCCESS;
294 }
295
296 VkResult anv_GetPhysicalDevicePresentRectanglesKHR(
297 VkPhysicalDevice physicalDevice,
298 VkSurfaceKHR surface,
299 uint32_t* pRectCount,
300 VkRect2D* pRects)
301 {
302 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
303
304 return wsi_common_get_present_rectangles(&device->wsi_device,
305 surface,
306 pRectCount, pRects);
307 }