Merge branch 'nir-spirv' 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 result = anv_wl_init_wsi(instance);
38 if (result != VK_SUCCESS) {
39 anv_x11_finish_wsi(instance);
40 return result;
41 }
42
43 return VK_SUCCESS;
44 }
45
46 void
47 anv_finish_wsi(struct anv_instance *instance)
48 {
49 anv_wl_finish_wsi(instance);
50 anv_x11_finish_wsi(instance);
51 }
52
53 VkResult
54 anv_GetPhysicalDeviceSurfaceSupportWSI(
55 VkPhysicalDevice physicalDevice,
56 uint32_t queueFamilyIndex,
57 const VkSurfaceDescriptionWSI* pSurfaceDescription,
58 VkBool32* pSupported)
59 {
60 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
61
62 assert(pSurfaceDescription->sType ==
63 VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI);
64
65 VkSurfaceDescriptionWindowWSI *window = (void *)pSurfaceDescription;
66
67 struct anv_wsi_implementation *impl =
68 physical_device->instance->wsi_impl[window->platform];
69
70 if (impl) {
71 return impl->get_window_supported(impl, physical_device,
72 window, pSupported);
73 } else {
74 *pSupported = false;
75 return VK_SUCCESS;
76 }
77 }
78
79 VkResult
80 anv_GetSurfaceInfoWSI(
81 VkDevice _device,
82 const VkSurfaceDescriptionWSI* pSurfaceDescription,
83 VkSurfaceInfoTypeWSI infoType,
84 size_t* pDataSize,
85 void* pData)
86 {
87 ANV_FROM_HANDLE(anv_device, device, _device);
88
89 assert(pSurfaceDescription->sType ==
90 VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI);
91 VkSurfaceDescriptionWindowWSI *window =
92 (VkSurfaceDescriptionWindowWSI *)pSurfaceDescription;
93
94 struct anv_wsi_implementation *impl =
95 device->instance->wsi_impl[window->platform];
96
97 assert(impl);
98
99 return impl->get_surface_info(impl, device, window, infoType,
100 pDataSize, pData);
101 }
102
103 VkResult
104 anv_CreateSwapChainWSI(
105 VkDevice _device,
106 const VkSwapChainCreateInfoWSI* pCreateInfo,
107 VkSwapChainWSI* pSwapChain)
108 {
109 ANV_FROM_HANDLE(anv_device, device, _device);
110 struct anv_swap_chain *swap_chain;
111 VkResult result;
112
113 assert(pCreateInfo->pSurfaceDescription->sType ==
114 VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI);
115 VkSurfaceDescriptionWindowWSI *window =
116 (VkSurfaceDescriptionWindowWSI *)pCreateInfo->pSurfaceDescription;
117
118 struct anv_wsi_implementation *impl =
119 device->instance->wsi_impl[window->platform];
120
121 assert(impl);
122
123 result = impl->create_swap_chain(impl, device, pCreateInfo, &swap_chain);
124
125 if (result == VK_SUCCESS)
126 *pSwapChain = anv_swap_chain_to_handle(swap_chain);
127
128 return result;
129 }
130
131 VkResult
132 anv_DestroySwapChainWSI(
133 VkDevice device,
134 VkSwapChainWSI swapChain)
135 {
136 ANV_FROM_HANDLE(anv_swap_chain, swap_chain, swapChain);
137
138 assert(swap_chain->device == anv_device_from_handle(device));
139
140 return swap_chain->destroy(swap_chain);
141 }
142
143 VkResult
144 anv_GetSwapChainInfoWSI(
145 VkDevice device,
146 VkSwapChainWSI swapChain,
147 VkSwapChainInfoTypeWSI infoType,
148 size_t* pDataSize,
149 void* pData)
150 {
151 ANV_FROM_HANDLE(anv_swap_chain, swap_chain, swapChain);
152
153 assert(swap_chain->device == anv_device_from_handle(device));
154
155 return swap_chain->get_swap_chain_info(swap_chain, infoType,
156 pDataSize, pData);
157 }
158
159 VkResult
160 anv_AcquireNextImageWSI(
161 VkDevice device,
162 VkSwapChainWSI swapChain,
163 uint64_t timeout,
164 VkSemaphore semaphore,
165 uint32_t* pImageIndex)
166 {
167 ANV_FROM_HANDLE(anv_swap_chain, swap_chain, swapChain);
168
169 assert(swap_chain->device == anv_device_from_handle(device));
170
171 return swap_chain->acquire_next_image(swap_chain,
172 timeout, semaphore, pImageIndex);
173 }
174
175 VkResult
176 anv_QueuePresentWSI(
177 VkQueue _queue,
178 VkPresentInfoWSI* pPresentInfo)
179 {
180 ANV_FROM_HANDLE(anv_queue, queue, _queue);
181 VkResult result;
182
183 for (uint32_t i = 0; i < pPresentInfo->swapChainCount; i++) {
184 ANV_FROM_HANDLE(anv_swap_chain, swap_chain, pPresentInfo->swapChains[i]);
185
186 assert(swap_chain->device == queue->device);
187
188 result = swap_chain->queue_present(swap_chain, queue,
189 pPresentInfo->imageIndices[i]);
190 /* TODO: What if one of them returns OUT_OF_DATE? */
191 if (result != VK_SUCCESS)
192 return result;
193 }
194
195 return VK_SUCCESS;
196 }