5ed1d711689b4e152738492b4017e8c0b5ff018a
[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 device->local_fd,
93 queueFamilyIndex,
94 surface,
95 &device->instance->alloc,
96 pSupported);
97 }
98
99 VkResult anv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
100 VkPhysicalDevice physicalDevice,
101 VkSurfaceKHR surface,
102 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
103 {
104 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
105
106 return wsi_common_get_surface_capabilities(&device->wsi_device,
107 surface,
108 pSurfaceCapabilities);
109 }
110
111 VkResult anv_GetPhysicalDeviceSurfaceCapabilities2KHR(
112 VkPhysicalDevice physicalDevice,
113 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
114 VkSurfaceCapabilities2KHR* pSurfaceCapabilities)
115 {
116 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
117
118 return wsi_common_get_surface_capabilities2(&device->wsi_device,
119 pSurfaceInfo,
120 pSurfaceCapabilities);
121 }
122
123 VkResult anv_GetPhysicalDeviceSurfaceCapabilities2EXT(
124 VkPhysicalDevice physicalDevice,
125 VkSurfaceKHR surface,
126 VkSurfaceCapabilities2EXT* pSurfaceCapabilities)
127 {
128 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
129
130 return wsi_common_get_surface_capabilities2ext(&device->wsi_device,
131 surface,
132 pSurfaceCapabilities);
133 }
134
135 VkResult anv_GetPhysicalDeviceSurfaceFormatsKHR(
136 VkPhysicalDevice physicalDevice,
137 VkSurfaceKHR surface,
138 uint32_t* pSurfaceFormatCount,
139 VkSurfaceFormatKHR* pSurfaceFormats)
140 {
141 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
142
143 return wsi_common_get_surface_formats(&device->wsi_device, surface,
144 pSurfaceFormatCount, pSurfaceFormats);
145 }
146
147 VkResult anv_GetPhysicalDeviceSurfaceFormats2KHR(
148 VkPhysicalDevice physicalDevice,
149 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
150 uint32_t* pSurfaceFormatCount,
151 VkSurfaceFormat2KHR* pSurfaceFormats)
152 {
153 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
154
155 return wsi_common_get_surface_formats2(&device->wsi_device, pSurfaceInfo,
156 pSurfaceFormatCount, pSurfaceFormats);
157 }
158
159 VkResult anv_GetPhysicalDeviceSurfacePresentModesKHR(
160 VkPhysicalDevice physicalDevice,
161 VkSurfaceKHR surface,
162 uint32_t* pPresentModeCount,
163 VkPresentModeKHR* pPresentModes)
164 {
165 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
166
167 return wsi_common_get_surface_present_modes(&device->wsi_device, surface,
168 pPresentModeCount,
169 pPresentModes);
170 }
171
172 VkResult anv_CreateSwapchainKHR(
173 VkDevice _device,
174 const VkSwapchainCreateInfoKHR* pCreateInfo,
175 const VkAllocationCallbacks* pAllocator,
176 VkSwapchainKHR* pSwapchain)
177 {
178 ANV_FROM_HANDLE(anv_device, device, _device);
179 struct wsi_device *wsi_device = &device->instance->physicalDevice.wsi_device;
180 const VkAllocationCallbacks *alloc;
181
182 if (pAllocator)
183 alloc = pAllocator;
184 else
185 alloc = &device->alloc;
186
187 return wsi_common_create_swapchain(wsi_device, _device, device->fd,
188 pCreateInfo, alloc, pSwapchain);
189 }
190
191 void anv_DestroySwapchainKHR(
192 VkDevice _device,
193 VkSwapchainKHR swapchain,
194 const VkAllocationCallbacks* pAllocator)
195 {
196 ANV_FROM_HANDLE(anv_device, device, _device);
197 const VkAllocationCallbacks *alloc;
198
199 if (pAllocator)
200 alloc = pAllocator;
201 else
202 alloc = &device->alloc;
203
204 wsi_common_destroy_swapchain(_device, swapchain, alloc);
205 }
206
207 VkResult anv_GetSwapchainImagesKHR(
208 VkDevice device,
209 VkSwapchainKHR swapchain,
210 uint32_t* pSwapchainImageCount,
211 VkImage* pSwapchainImages)
212 {
213 return wsi_common_get_images(swapchain,
214 pSwapchainImageCount,
215 pSwapchainImages);
216 }
217
218 VkResult anv_AcquireNextImageKHR(
219 VkDevice device,
220 VkSwapchainKHR swapchain,
221 uint64_t timeout,
222 VkSemaphore semaphore,
223 VkFence fence,
224 uint32_t* pImageIndex)
225 {
226 VkAcquireNextImageInfoKHR acquire_info = {
227 .sType = VK_STRUCTURE_TYPE_ACQUIRE_NEXT_IMAGE_INFO_KHR,
228 .swapchain = swapchain,
229 .timeout = timeout,
230 .semaphore = semaphore,
231 .fence = fence,
232 .deviceMask = 0,
233 };
234
235 return anv_AcquireNextImage2KHR(device, &acquire_info, pImageIndex);
236 }
237
238 VkResult anv_AcquireNextImage2KHR(
239 VkDevice _device,
240 const VkAcquireNextImageInfoKHR* pAcquireInfo,
241 uint32_t* pImageIndex)
242 {
243 ANV_FROM_HANDLE(anv_device, device, _device);
244 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
245
246 VkResult result = wsi_common_acquire_next_image2(&pdevice->wsi_device,
247 _device,
248 pAcquireInfo,
249 pImageIndex);
250
251 /* Thanks to implicit sync, the image is ready immediately. However, we
252 * should wait for the current GPU state to finish.
253 */
254 if (pAcquireInfo->fence != VK_NULL_HANDLE) {
255 anv_QueueSubmit(anv_queue_to_handle(&device->queue), 0, NULL,
256 pAcquireInfo->fence);
257 }
258
259 return result;
260 }
261
262 VkResult anv_QueuePresentKHR(
263 VkQueue _queue,
264 const VkPresentInfoKHR* pPresentInfo)
265 {
266 ANV_FROM_HANDLE(anv_queue, queue, _queue);
267 struct anv_physical_device *pdevice =
268 &queue->device->instance->physicalDevice;
269
270 return wsi_common_queue_present(&pdevice->wsi_device,
271 anv_device_to_handle(queue->device),
272 _queue, 0,
273 pPresentInfo);
274 }
275
276 VkResult anv_GetDeviceGroupPresentCapabilitiesKHR(
277 VkDevice device,
278 VkDeviceGroupPresentCapabilitiesKHR* pCapabilities)
279 {
280 memset(pCapabilities->presentMask, 0,
281 sizeof(pCapabilities->presentMask));
282 pCapabilities->presentMask[0] = 0x1;
283 pCapabilities->modes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
284
285 return VK_SUCCESS;
286 }
287
288 VkResult anv_GetDeviceGroupSurfacePresentModesKHR(
289 VkDevice device,
290 VkSurfaceKHR surface,
291 VkDeviceGroupPresentModeFlagsKHR* pModes)
292 {
293 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
294
295 return VK_SUCCESS;
296 }