anv: resolve wayland-only build
[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_wsi.h"
25
26 VkResult
27 anv_init_wsi(struct anv_physical_device *physical_device)
28 {
29 VkResult result;
30
31 memset(physical_device->wsi, 0, sizeof(physical_device->wsi));
32
33 #ifdef VK_USE_PLATFORM_XCB_KHR
34 result = anv_x11_init_wsi(physical_device);
35 if (result != VK_SUCCESS)
36 return result;
37 #endif
38
39 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
40 result = anv_wl_init_wsi(physical_device);
41 if (result != VK_SUCCESS) {
42 #ifdef VK_USE_PLATFORM_XCB_KHR
43 anv_x11_finish_wsi(physical_device);
44 #endif
45 return result;
46 }
47 #endif
48
49 return VK_SUCCESS;
50 }
51
52 void
53 anv_finish_wsi(struct anv_physical_device *physical_device)
54 {
55 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
56 anv_wl_finish_wsi(physical_device);
57 #endif
58 #ifdef VK_USE_PLATFORM_XCB_KHR
59 anv_x11_finish_wsi(physical_device);
60 #endif
61 }
62
63 void anv_DestroySurfaceKHR(
64 VkInstance _instance,
65 VkSurfaceKHR _surface,
66 const VkAllocationCallbacks* pAllocator)
67 {
68 ANV_FROM_HANDLE(anv_instance, instance, _instance);
69 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
70
71 anv_free2(&instance->alloc, pAllocator, surface);
72 }
73
74 VkResult anv_GetPhysicalDeviceSurfaceSupportKHR(
75 VkPhysicalDevice physicalDevice,
76 uint32_t queueFamilyIndex,
77 VkSurfaceKHR _surface,
78 VkBool32* pSupported)
79 {
80 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
81 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
82 struct anv_wsi_interface *iface = device->wsi[surface->platform];
83
84 return iface->get_support(surface, device, queueFamilyIndex, pSupported);
85 }
86
87 VkResult anv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
88 VkPhysicalDevice physicalDevice,
89 VkSurfaceKHR _surface,
90 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
91 {
92 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
93 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
94 struct anv_wsi_interface *iface = device->wsi[surface->platform];
95
96 return iface->get_capabilities(surface, device, pSurfaceCapabilities);
97 }
98
99 VkResult anv_GetPhysicalDeviceSurfaceFormatsKHR(
100 VkPhysicalDevice physicalDevice,
101 VkSurfaceKHR _surface,
102 uint32_t* pSurfaceFormatCount,
103 VkSurfaceFormatKHR* pSurfaceFormats)
104 {
105 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
106 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
107 struct anv_wsi_interface *iface = device->wsi[surface->platform];
108
109 return iface->get_formats(surface, device, pSurfaceFormatCount,
110 pSurfaceFormats);
111 }
112
113 VkResult anv_GetPhysicalDeviceSurfacePresentModesKHR(
114 VkPhysicalDevice physicalDevice,
115 VkSurfaceKHR _surface,
116 uint32_t* pPresentModeCount,
117 VkPresentModeKHR* pPresentModes)
118 {
119 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
120 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
121 struct anv_wsi_interface *iface = device->wsi[surface->platform];
122
123 return iface->get_present_modes(surface, device, pPresentModeCount,
124 pPresentModes);
125 }
126
127 VkResult anv_CreateSwapchainKHR(
128 VkDevice _device,
129 const VkSwapchainCreateInfoKHR* pCreateInfo,
130 const VkAllocationCallbacks* pAllocator,
131 VkSwapchainKHR* pSwapchain)
132 {
133 ANV_FROM_HANDLE(anv_device, device, _device);
134 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, pCreateInfo->surface);
135 struct anv_wsi_interface *iface =
136 device->instance->physicalDevice.wsi[surface->platform];
137 struct anv_swapchain *swapchain;
138
139 VkResult result = iface->create_swapchain(surface, device, pCreateInfo,
140 pAllocator, &swapchain);
141 if (result != VK_SUCCESS)
142 return result;
143
144 if (pAllocator)
145 swapchain->alloc = *pAllocator;
146 else
147 swapchain->alloc = device->alloc;
148
149 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++)
150 swapchain->fences[i] = VK_NULL_HANDLE;
151
152 *pSwapchain = anv_swapchain_to_handle(swapchain);
153
154 return VK_SUCCESS;
155 }
156
157 void anv_DestroySwapchainKHR(
158 VkDevice device,
159 VkSwapchainKHR _swapchain,
160 const VkAllocationCallbacks* pAllocator)
161 {
162 ANV_FROM_HANDLE(anv_swapchain, swapchain, _swapchain);
163
164 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++) {
165 if (swapchain->fences[i] != VK_NULL_HANDLE)
166 anv_DestroyFence(device, swapchain->fences[i], pAllocator);
167 }
168
169 swapchain->destroy(swapchain, pAllocator);
170 }
171
172 VkResult anv_GetSwapchainImagesKHR(
173 VkDevice device,
174 VkSwapchainKHR _swapchain,
175 uint32_t* pSwapchainImageCount,
176 VkImage* pSwapchainImages)
177 {
178 ANV_FROM_HANDLE(anv_swapchain, swapchain, _swapchain);
179
180 return swapchain->get_images(swapchain, pSwapchainImageCount,
181 pSwapchainImages);
182 }
183
184 VkResult anv_AcquireNextImageKHR(
185 VkDevice device,
186 VkSwapchainKHR _swapchain,
187 uint64_t timeout,
188 VkSemaphore semaphore,
189 VkFence fence,
190 uint32_t* pImageIndex)
191 {
192 ANV_FROM_HANDLE(anv_swapchain, swapchain, _swapchain);
193
194 return swapchain->acquire_next_image(swapchain, timeout, semaphore,
195 pImageIndex);
196 }
197
198 VkResult anv_QueuePresentKHR(
199 VkQueue _queue,
200 const VkPresentInfoKHR* pPresentInfo)
201 {
202 ANV_FROM_HANDLE(anv_queue, queue, _queue);
203 VkResult result;
204
205 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
206 ANV_FROM_HANDLE(anv_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
207
208 assert(swapchain->device == queue->device);
209
210 if (swapchain->fences[0] == VK_NULL_HANDLE) {
211 result = anv_CreateFence(anv_device_to_handle(queue->device),
212 &(VkFenceCreateInfo) {
213 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
214 .flags = 0,
215 }, &swapchain->alloc, &swapchain->fences[0]);
216 if (result != VK_SUCCESS)
217 return result;
218 } else {
219 anv_ResetFences(anv_device_to_handle(queue->device),
220 1, &swapchain->fences[0]);
221 }
222
223 anv_QueueSubmit(_queue, 0, NULL, swapchain->fences[0]);
224
225 result = swapchain->queue_present(swapchain, queue,
226 pPresentInfo->pImageIndices[i]);
227 /* TODO: What if one of them returns OUT_OF_DATE? */
228 if (result != VK_SUCCESS)
229 return result;
230
231 VkFence last = swapchain->fences[2];
232 swapchain->fences[2] = swapchain->fences[1];
233 swapchain->fences[1] = swapchain->fences[0];
234 swapchain->fences[0] = last;
235
236 if (last != VK_NULL_HANDLE) {
237 anv_WaitForFences(anv_device_to_handle(queue->device),
238 1, &last, true, 1);
239 }
240 }
241
242 return VK_SUCCESS;
243 }