Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / 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_instance *instance)
28 {
29 VkResult result;
30
31 memset(instance->wsi_impl, 0, sizeof(instance->wsi_impl));
32
33 result = anv_x11_init_wsi(instance);
34 if (result != VK_SUCCESS)
35 return result;
36
37 #ifdef HAVE_WAYLAND_PLATFORM
38 result = anv_wl_init_wsi(instance);
39 if (result != VK_SUCCESS) {
40 anv_x11_finish_wsi(instance);
41 return result;
42 }
43 #endif
44
45 return VK_SUCCESS;
46 }
47
48 void
49 anv_finish_wsi(struct anv_instance *instance)
50 {
51 #ifdef HAVE_WAYLAND_PLATFORM
52 anv_wl_finish_wsi(instance);
53 #endif
54 anv_x11_finish_wsi(instance);
55 }
56
57 VkResult
58 anv_GetPhysicalDeviceSurfaceSupportKHR(
59 VkPhysicalDevice physicalDevice,
60 uint32_t queueFamilyIndex,
61 const VkSurfaceDescriptionKHR* pSurfaceDescription,
62 VkBool32* pSupported)
63 {
64 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
65
66 assert(pSurfaceDescription->sType ==
67 VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR);
68
69 VkSurfaceDescriptionWindowKHR *window = (void *)pSurfaceDescription;
70
71 struct anv_wsi_implementation *impl =
72 physical_device->instance->wsi_impl[window->platform];
73
74 if (impl) {
75 return impl->get_window_supported(impl, physical_device,
76 window, pSupported);
77 } else {
78 *pSupported = false;
79 return VK_SUCCESS;
80 }
81 }
82
83 VkResult
84 anv_GetSurfacePropertiesKHR(
85 VkDevice _device,
86 const VkSurfaceDescriptionKHR* pSurfaceDescription,
87 VkSurfacePropertiesKHR* pSurfaceProperties)
88 {
89 ANV_FROM_HANDLE(anv_device, device, _device);
90
91 assert(pSurfaceDescription->sType ==
92 VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR);
93 VkSurfaceDescriptionWindowKHR *window =
94 (VkSurfaceDescriptionWindowKHR *)pSurfaceDescription;
95
96 struct anv_wsi_implementation *impl =
97 device->instance->wsi_impl[window->platform];
98
99 assert(impl);
100
101 return impl->get_surface_properties(impl, device, window,
102 pSurfaceProperties);
103 }
104
105 VkResult
106 anv_GetSurfaceFormatsKHR(
107 VkDevice _device,
108 const VkSurfaceDescriptionKHR* pSurfaceDescription,
109 uint32_t* pCount,
110 VkSurfaceFormatKHR* pSurfaceFormats)
111 {
112 ANV_FROM_HANDLE(anv_device, device, _device);
113
114 assert(pSurfaceDescription->sType ==
115 VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR);
116 VkSurfaceDescriptionWindowKHR *window =
117 (VkSurfaceDescriptionWindowKHR *)pSurfaceDescription;
118
119 struct anv_wsi_implementation *impl =
120 device->instance->wsi_impl[window->platform];
121
122 assert(impl);
123
124 return impl->get_surface_formats(impl, device, window,
125 pCount, pSurfaceFormats);
126 }
127
128 VkResult
129 anv_GetSurfacePresentModesKHR(
130 VkDevice _device,
131 const VkSurfaceDescriptionKHR* pSurfaceDescription,
132 uint32_t* pCount,
133 VkPresentModeKHR* pPresentModes)
134 {
135 ANV_FROM_HANDLE(anv_device, device, _device);
136
137 assert(pSurfaceDescription->sType ==
138 VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR);
139 VkSurfaceDescriptionWindowKHR *window =
140 (VkSurfaceDescriptionWindowKHR *)pSurfaceDescription;
141
142 struct anv_wsi_implementation *impl =
143 device->instance->wsi_impl[window->platform];
144
145 assert(impl);
146
147 return impl->get_surface_present_modes(impl, device, window,
148 pCount, pPresentModes);
149 }
150
151
152 VkResult
153 anv_CreateSwapchainKHR(
154 VkDevice _device,
155 const VkSwapchainCreateInfoKHR* pCreateInfo,
156 VkSwapchainKHR* pSwapchain)
157 {
158 ANV_FROM_HANDLE(anv_device, device, _device);
159 struct anv_swapchain *swapchain;
160 VkResult result;
161
162 assert(pCreateInfo->pSurfaceDescription->sType ==
163 VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR);
164 VkSurfaceDescriptionWindowKHR *window =
165 (VkSurfaceDescriptionWindowKHR *)pCreateInfo->pSurfaceDescription;
166
167 struct anv_wsi_implementation *impl =
168 device->instance->wsi_impl[window->platform];
169
170 assert(impl);
171
172 result = impl->create_swapchain(impl, device, pCreateInfo, &swapchain);
173
174 if (result == VK_SUCCESS)
175 *pSwapchain = anv_swapchain_to_handle(swapchain);
176
177 return result;
178 }
179
180 VkResult
181 anv_DestroySwapchainKHR(
182 VkDevice device,
183 VkSwapchainKHR swapChain)
184 {
185 ANV_FROM_HANDLE(anv_swapchain, swapchain, swapChain);
186
187 assert(swapchain->device == anv_device_from_handle(device));
188
189 return swapchain->destroy(swapchain);
190 }
191
192 VkResult
193 anv_GetSwapchainImagesKHR(
194 VkDevice device,
195 VkSwapchainKHR _swapchain,
196 uint32_t* pCount,
197 VkImage* pSwapchainImages)
198 {
199 ANV_FROM_HANDLE(anv_swapchain, swapchain, _swapchain);
200
201 assert(swapchain->device == anv_device_from_handle(device));
202
203 return swapchain->get_images(swapchain, pCount, pSwapchainImages);
204 }
205
206 VkResult
207 anv_AcquireNextImageKHR(
208 VkDevice device,
209 VkSwapchainKHR _swapchain,
210 uint64_t timeout,
211 VkSemaphore semaphore,
212 uint32_t* pImageIndex)
213 {
214 ANV_FROM_HANDLE(anv_swapchain, swapchain, _swapchain);
215
216 assert(swapchain->device == anv_device_from_handle(device));
217
218 return swapchain->acquire_next_image(swapchain,
219 timeout, semaphore, pImageIndex);
220 }
221
222 VkResult
223 anv_QueuePresentKHR(
224 VkQueue _queue,
225 VkPresentInfoKHR* pPresentInfo)
226 {
227 ANV_FROM_HANDLE(anv_queue, queue, _queue);
228 VkResult result;
229
230 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
231 ANV_FROM_HANDLE(anv_swapchain, swapchain, pPresentInfo->swapchains[i]);
232
233 assert(swapchain->device == queue->device);
234
235 result = swapchain->queue_present(swapchain, queue,
236 pPresentInfo->imageIndices[i]);
237 /* TODO: What if one of them returns OUT_OF_DATE? */
238 if (result != VK_SUCCESS)
239 return result;
240 }
241
242 return VK_SUCCESS;
243 }