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