vulkan/wsi: Store the instance allocator in wsi_device
[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 static PFN_vkVoidFunction
33 radv_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
34 {
35 return radv_lookup_entrypoint_unchecked(pName);
36 }
37
38 VkResult
39 radv_init_wsi(struct radv_physical_device *physical_device)
40 {
41 return wsi_device_init(&physical_device->wsi_device,
42 radv_physical_device_to_handle(physical_device),
43 radv_wsi_proc_addr,
44 &physical_device->instance->alloc,
45 physical_device->master_fd);
46 }
47
48 void
49 radv_finish_wsi(struct radv_physical_device *physical_device)
50 {
51 wsi_device_finish(&physical_device->wsi_device,
52 &physical_device->instance->alloc);
53 }
54
55 void radv_DestroySurfaceKHR(
56 VkInstance _instance,
57 VkSurfaceKHR _surface,
58 const VkAllocationCallbacks* pAllocator)
59 {
60 RADV_FROM_HANDLE(radv_instance, instance, _instance);
61 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
62
63 vk_free2(&instance->alloc, pAllocator, surface);
64 }
65
66 VkResult radv_GetPhysicalDeviceSurfaceSupportKHR(
67 VkPhysicalDevice physicalDevice,
68 uint32_t queueFamilyIndex,
69 VkSurfaceKHR surface,
70 VkBool32* pSupported)
71 {
72 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
73
74 return wsi_common_get_surface_support(&device->wsi_device,
75 device->local_fd,
76 queueFamilyIndex,
77 surface,
78 pSupported);
79 }
80
81 VkResult radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
82 VkPhysicalDevice physicalDevice,
83 VkSurfaceKHR surface,
84 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
85 {
86 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
87
88 return wsi_common_get_surface_capabilities(&device->wsi_device,
89 surface,
90 pSurfaceCapabilities);
91 }
92
93 VkResult radv_GetPhysicalDeviceSurfaceCapabilities2KHR(
94 VkPhysicalDevice physicalDevice,
95 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
96 VkSurfaceCapabilities2KHR* pSurfaceCapabilities)
97 {
98 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
99
100 return wsi_common_get_surface_capabilities2(&device->wsi_device,
101 pSurfaceInfo,
102 pSurfaceCapabilities);
103 }
104
105 VkResult radv_GetPhysicalDeviceSurfaceCapabilities2EXT(
106 VkPhysicalDevice physicalDevice,
107 VkSurfaceKHR surface,
108 VkSurfaceCapabilities2EXT* pSurfaceCapabilities)
109 {
110 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
111
112 return wsi_common_get_surface_capabilities2ext(&device->wsi_device,
113 surface,
114 pSurfaceCapabilities);
115 }
116
117 VkResult radv_GetPhysicalDeviceSurfaceFormatsKHR(
118 VkPhysicalDevice physicalDevice,
119 VkSurfaceKHR surface,
120 uint32_t* pSurfaceFormatCount,
121 VkSurfaceFormatKHR* pSurfaceFormats)
122 {
123 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
124
125 return wsi_common_get_surface_formats(&device->wsi_device,
126 surface,
127 pSurfaceFormatCount,
128 pSurfaceFormats);
129 }
130
131 VkResult radv_GetPhysicalDeviceSurfaceFormats2KHR(
132 VkPhysicalDevice physicalDevice,
133 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
134 uint32_t* pSurfaceFormatCount,
135 VkSurfaceFormat2KHR* pSurfaceFormats)
136 {
137 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
138
139 return wsi_common_get_surface_formats2(&device->wsi_device,
140 pSurfaceInfo,
141 pSurfaceFormatCount,
142 pSurfaceFormats);
143 }
144
145 VkResult radv_GetPhysicalDeviceSurfacePresentModesKHR(
146 VkPhysicalDevice physicalDevice,
147 VkSurfaceKHR surface,
148 uint32_t* pPresentModeCount,
149 VkPresentModeKHR* pPresentModes)
150 {
151 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
152
153 return wsi_common_get_surface_present_modes(&device->wsi_device,
154 surface,
155 pPresentModeCount,
156 pPresentModes);
157 }
158
159 VkResult radv_CreateSwapchainKHR(
160 VkDevice _device,
161 const VkSwapchainCreateInfoKHR* pCreateInfo,
162 const VkAllocationCallbacks* pAllocator,
163 VkSwapchainKHR* pSwapchain)
164 {
165 RADV_FROM_HANDLE(radv_device, device, _device);
166 const VkAllocationCallbacks *alloc;
167 if (pAllocator)
168 alloc = pAllocator;
169 else
170 alloc = &device->alloc;
171
172 return wsi_common_create_swapchain(&device->physical_device->wsi_device,
173 radv_device_to_handle(device),
174 device->physical_device->local_fd,
175 pCreateInfo,
176 alloc,
177 pSwapchain);
178 }
179
180 void radv_DestroySwapchainKHR(
181 VkDevice _device,
182 VkSwapchainKHR swapchain,
183 const VkAllocationCallbacks* pAllocator)
184 {
185 RADV_FROM_HANDLE(radv_device, device, _device);
186 const VkAllocationCallbacks *alloc;
187
188 if (pAllocator)
189 alloc = pAllocator;
190 else
191 alloc = &device->alloc;
192
193 wsi_common_destroy_swapchain(_device, swapchain, alloc);
194 }
195
196 VkResult radv_GetSwapchainImagesKHR(
197 VkDevice device,
198 VkSwapchainKHR swapchain,
199 uint32_t* pSwapchainImageCount,
200 VkImage* pSwapchainImages)
201 {
202 return wsi_common_get_images(swapchain,
203 pSwapchainImageCount,
204 pSwapchainImages);
205 }
206
207 VkResult radv_AcquireNextImageKHR(
208 VkDevice device,
209 VkSwapchainKHR swapchain,
210 uint64_t timeout,
211 VkSemaphore semaphore,
212 VkFence fence,
213 uint32_t* pImageIndex)
214 {
215 VkAcquireNextImageInfoKHR acquire_info = {
216 .sType = VK_STRUCTURE_TYPE_ACQUIRE_NEXT_IMAGE_INFO_KHR,
217 .swapchain = swapchain,
218 .timeout = timeout,
219 .semaphore = semaphore,
220 .fence = fence,
221 .deviceMask = 0,
222 };
223
224 return radv_AcquireNextImage2KHR(device, &acquire_info, pImageIndex);
225 }
226
227 VkResult radv_AcquireNextImage2KHR(
228 VkDevice _device,
229 const VkAcquireNextImageInfoKHR* pAcquireInfo,
230 uint32_t* pImageIndex)
231 {
232 RADV_FROM_HANDLE(radv_device, device, _device);
233 struct radv_physical_device *pdevice = device->physical_device;
234 RADV_FROM_HANDLE(radv_fence, fence, pAcquireInfo->fence);
235
236 VkResult result = wsi_common_acquire_next_image2(&pdevice->wsi_device,
237 _device,
238 pAcquireInfo,
239 pImageIndex);
240
241 if (fence && (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR)) {
242 fence->submitted = true;
243 fence->signalled = true;
244 if (fence->temp_syncobj) {
245 device->ws->signal_syncobj(device->ws, fence->temp_syncobj);
246 } else if (fence->syncobj) {
247 device->ws->signal_syncobj(device->ws, fence->syncobj);
248 }
249 }
250 return result;
251 }
252
253 VkResult radv_QueuePresentKHR(
254 VkQueue _queue,
255 const VkPresentInfoKHR* pPresentInfo)
256 {
257 RADV_FROM_HANDLE(radv_queue, queue, _queue);
258 return wsi_common_queue_present(&queue->device->physical_device->wsi_device,
259 radv_device_to_handle(queue->device),
260 _queue,
261 queue->queue_family_index,
262 pPresentInfo);
263 }
264
265
266 VkResult radv_GetDeviceGroupPresentCapabilitiesKHR(
267 VkDevice device,
268 VkDeviceGroupPresentCapabilitiesKHR* pCapabilities)
269 {
270 memset(pCapabilities->presentMask, 0,
271 sizeof(pCapabilities->presentMask));
272 pCapabilities->presentMask[0] = 0x1;
273 pCapabilities->modes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
274
275 return VK_SUCCESS;
276 }
277
278 VkResult radv_GetDeviceGroupSurfacePresentModesKHR(
279 VkDevice device,
280 VkSurfaceKHR surface,
281 VkDeviceGroupPresentModeFlagsKHR* pModes)
282 {
283 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
284
285 return VK_SUCCESS;
286 }