676050531da3a02aa94a4a574c29e80b91304932
[mesa.git] / src / amd / vulkan / radv_wsi.c
1 /*
2 * Copyright © 2016 Red Hat
3 * based on intel anv code:
4 * Copyright © 2015 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 */
25
26 #include "radv_private.h"
27 #include "radv_meta.h"
28 #include "wsi_common.h"
29 #include "vk_util.h"
30 #include "util/macros.h"
31
32 #define WSI_CB(x) .x = radv_##x
33 MAYBE_UNUSED static const struct wsi_callbacks wsi_cbs = {
34 WSI_CB(GetPhysicalDeviceFormatProperties),
35 };
36
37 static PFN_vkVoidFunction
38 radv_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
39 {
40 return radv_lookup_entrypoint(pName);
41 }
42
43 static uint32_t
44 radv_wsi_queue_get_family_index(VkQueue _queue)
45 {
46 RADV_FROM_HANDLE(radv_queue, queue, _queue);
47 return queue->queue_family_index;
48 }
49
50 VkResult
51 radv_init_wsi(struct radv_physical_device *physical_device)
52 {
53 VkResult result;
54
55 wsi_device_init(&physical_device->wsi_device,
56 radv_physical_device_to_handle(physical_device),
57 radv_wsi_proc_addr);
58
59 physical_device->wsi_device.queue_get_family_index =
60 radv_wsi_queue_get_family_index;
61
62 #ifdef VK_USE_PLATFORM_XCB_KHR
63 result = wsi_x11_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
64 if (result != VK_SUCCESS)
65 return result;
66 #endif
67
68 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
69 result = wsi_wl_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc,
70 radv_physical_device_to_handle(physical_device),
71 &wsi_cbs);
72 if (result != VK_SUCCESS) {
73 #ifdef VK_USE_PLATFORM_XCB_KHR
74 wsi_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
75 #endif
76 return result;
77 }
78 #endif
79
80 return VK_SUCCESS;
81 }
82
83 void
84 radv_finish_wsi(struct radv_physical_device *physical_device)
85 {
86 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
87 wsi_wl_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
88 #endif
89 #ifdef VK_USE_PLATFORM_XCB_KHR
90 wsi_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
91 #endif
92 }
93
94 void radv_DestroySurfaceKHR(
95 VkInstance _instance,
96 VkSurfaceKHR _surface,
97 const VkAllocationCallbacks* pAllocator)
98 {
99 RADV_FROM_HANDLE(radv_instance, instance, _instance);
100 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
101
102 vk_free2(&instance->alloc, pAllocator, surface);
103 }
104
105 VkResult radv_GetPhysicalDeviceSurfaceSupportKHR(
106 VkPhysicalDevice physicalDevice,
107 uint32_t queueFamilyIndex,
108 VkSurfaceKHR surface,
109 VkBool32* pSupported)
110 {
111 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
112
113 return wsi_common_get_surface_support(&device->wsi_device,
114 device->local_fd,
115 queueFamilyIndex,
116 surface,
117 &device->instance->alloc,
118 pSupported);
119 }
120
121 VkResult radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
122 VkPhysicalDevice physicalDevice,
123 VkSurfaceKHR surface,
124 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
125 {
126 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
127
128 return wsi_common_get_surface_capabilities(&device->wsi_device,
129 surface,
130 pSurfaceCapabilities);
131 }
132
133 VkResult radv_GetPhysicalDeviceSurfaceFormatsKHR(
134 VkPhysicalDevice physicalDevice,
135 VkSurfaceKHR surface,
136 uint32_t* pSurfaceFormatCount,
137 VkSurfaceFormatKHR* pSurfaceFormats)
138 {
139 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
140
141 return wsi_common_get_surface_formats(&device->wsi_device,
142 surface,
143 pSurfaceFormatCount,
144 pSurfaceFormats);
145 }
146
147 VkResult radv_GetPhysicalDeviceSurfacePresentModesKHR(
148 VkPhysicalDevice physicalDevice,
149 VkSurfaceKHR surface,
150 uint32_t* pPresentModeCount,
151 VkPresentModeKHR* pPresentModes)
152 {
153 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
154
155 return wsi_common_get_surface_present_modes(&device->wsi_device,
156 surface,
157 pPresentModeCount,
158 pPresentModes);
159 }
160
161 VkResult radv_CreateSwapchainKHR(
162 VkDevice _device,
163 const VkSwapchainCreateInfoKHR* pCreateInfo,
164 const VkAllocationCallbacks* pAllocator,
165 VkSwapchainKHR* pSwapchain)
166 {
167 RADV_FROM_HANDLE(radv_device, device, _device);
168 const VkAllocationCallbacks *alloc;
169 if (pAllocator)
170 alloc = pAllocator;
171 else
172 alloc = &device->alloc;
173
174 return wsi_common_create_swapchain(&device->physical_device->wsi_device,
175 radv_device_to_handle(device),
176 device->physical_device->local_fd,
177 pCreateInfo,
178 alloc,
179 pSwapchain);
180 }
181
182 void radv_DestroySwapchainKHR(
183 VkDevice _device,
184 VkSwapchainKHR swapchain,
185 const VkAllocationCallbacks* pAllocator)
186 {
187 RADV_FROM_HANDLE(radv_device, device, _device);
188 const VkAllocationCallbacks *alloc;
189
190 if (pAllocator)
191 alloc = pAllocator;
192 else
193 alloc = &device->alloc;
194
195 wsi_common_destroy_swapchain(_device, swapchain, alloc);
196 }
197
198 VkResult radv_GetSwapchainImagesKHR(
199 VkDevice device,
200 VkSwapchainKHR swapchain,
201 uint32_t* pSwapchainImageCount,
202 VkImage* pSwapchainImages)
203 {
204 return wsi_common_get_images(swapchain,
205 pSwapchainImageCount,
206 pSwapchainImages);
207 }
208
209 VkResult radv_AcquireNextImageKHR(
210 VkDevice _device,
211 VkSwapchainKHR swapchain,
212 uint64_t timeout,
213 VkSemaphore semaphore,
214 VkFence _fence,
215 uint32_t* pImageIndex)
216 {
217 RADV_FROM_HANDLE(radv_device, device, _device);
218 struct radv_physical_device *pdevice = device->physical_device;
219 RADV_FROM_HANDLE(radv_fence, fence, _fence);
220
221 VkResult result = wsi_common_acquire_next_image(&pdevice->wsi_device,
222 _device,
223 swapchain,
224 timeout,
225 semaphore,
226 pImageIndex);
227
228 if (fence && (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR)) {
229 fence->submitted = true;
230 fence->signalled = true;
231 }
232 return result;
233 }
234
235 VkResult radv_QueuePresentKHR(
236 VkQueue _queue,
237 const VkPresentInfoKHR* pPresentInfo)
238 {
239 RADV_FROM_HANDLE(radv_queue, queue, _queue);
240 return wsi_common_queue_present(&queue->device->physical_device->wsi_device,
241 radv_device_to_handle(queue->device),
242 _queue,
243 queue->queue_family_index,
244 pPresentInfo);
245 }