radv: use the common base object type for VkDevice
[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(pName);
36 }
37
38 static void
39 radv_wsi_set_memory_ownership(VkDevice _device,
40 VkDeviceMemory _mem,
41 VkBool32 ownership)
42 {
43 RADV_FROM_HANDLE(radv_device, device, _device);
44 RADV_FROM_HANDLE(radv_device_memory, mem, _mem);
45
46 if (ownership)
47 radv_bo_list_add(device, mem->bo);
48 else
49 radv_bo_list_remove(device, mem->bo);
50 }
51
52 VkResult
53 radv_init_wsi(struct radv_physical_device *physical_device)
54 {
55 VkResult result = wsi_device_init(&physical_device->wsi_device,
56 radv_physical_device_to_handle(physical_device),
57 radv_wsi_proc_addr,
58 &physical_device->instance->alloc,
59 physical_device->master_fd,
60 &physical_device->instance->dri_options);
61 if (result != VK_SUCCESS)
62 return result;
63
64 physical_device->wsi_device.set_memory_ownership = radv_wsi_set_memory_ownership;
65 return VK_SUCCESS;
66 }
67
68 void
69 radv_finish_wsi(struct radv_physical_device *physical_device)
70 {
71 wsi_device_finish(&physical_device->wsi_device,
72 &physical_device->instance->alloc);
73 }
74
75 void radv_DestroySurfaceKHR(
76 VkInstance _instance,
77 VkSurfaceKHR _surface,
78 const VkAllocationCallbacks* pAllocator)
79 {
80 RADV_FROM_HANDLE(radv_instance, instance, _instance);
81 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
82
83 vk_free2(&instance->alloc, pAllocator, surface);
84 }
85
86 VkResult radv_GetPhysicalDeviceSurfaceSupportKHR(
87 VkPhysicalDevice physicalDevice,
88 uint32_t queueFamilyIndex,
89 VkSurfaceKHR surface,
90 VkBool32* pSupported)
91 {
92 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
93
94 return wsi_common_get_surface_support(&device->wsi_device,
95 queueFamilyIndex,
96 surface,
97 pSupported);
98 }
99
100 VkResult radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
101 VkPhysicalDevice physicalDevice,
102 VkSurfaceKHR surface,
103 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
104 {
105 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
106
107 return wsi_common_get_surface_capabilities(&device->wsi_device,
108 surface,
109 pSurfaceCapabilities);
110 }
111
112 VkResult radv_GetPhysicalDeviceSurfaceCapabilities2KHR(
113 VkPhysicalDevice physicalDevice,
114 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
115 VkSurfaceCapabilities2KHR* pSurfaceCapabilities)
116 {
117 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
118
119 return wsi_common_get_surface_capabilities2(&device->wsi_device,
120 pSurfaceInfo,
121 pSurfaceCapabilities);
122 }
123
124 VkResult radv_GetPhysicalDeviceSurfaceCapabilities2EXT(
125 VkPhysicalDevice physicalDevice,
126 VkSurfaceKHR surface,
127 VkSurfaceCapabilities2EXT* pSurfaceCapabilities)
128 {
129 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
130
131 return wsi_common_get_surface_capabilities2ext(&device->wsi_device,
132 surface,
133 pSurfaceCapabilities);
134 }
135
136 VkResult radv_GetPhysicalDeviceSurfaceFormatsKHR(
137 VkPhysicalDevice physicalDevice,
138 VkSurfaceKHR surface,
139 uint32_t* pSurfaceFormatCount,
140 VkSurfaceFormatKHR* pSurfaceFormats)
141 {
142 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
143
144 return wsi_common_get_surface_formats(&device->wsi_device,
145 surface,
146 pSurfaceFormatCount,
147 pSurfaceFormats);
148 }
149
150 VkResult radv_GetPhysicalDeviceSurfaceFormats2KHR(
151 VkPhysicalDevice physicalDevice,
152 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
153 uint32_t* pSurfaceFormatCount,
154 VkSurfaceFormat2KHR* pSurfaceFormats)
155 {
156 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
157
158 return wsi_common_get_surface_formats2(&device->wsi_device,
159 pSurfaceInfo,
160 pSurfaceFormatCount,
161 pSurfaceFormats);
162 }
163
164 VkResult radv_GetPhysicalDeviceSurfacePresentModesKHR(
165 VkPhysicalDevice physicalDevice,
166 VkSurfaceKHR surface,
167 uint32_t* pPresentModeCount,
168 VkPresentModeKHR* pPresentModes)
169 {
170 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
171
172 return wsi_common_get_surface_present_modes(&device->wsi_device,
173 surface,
174 pPresentModeCount,
175 pPresentModes);
176 }
177
178 VkResult radv_CreateSwapchainKHR(
179 VkDevice _device,
180 const VkSwapchainCreateInfoKHR* pCreateInfo,
181 const VkAllocationCallbacks* pAllocator,
182 VkSwapchainKHR* pSwapchain)
183 {
184 RADV_FROM_HANDLE(radv_device, device, _device);
185 const VkAllocationCallbacks *alloc;
186 if (pAllocator)
187 alloc = pAllocator;
188 else
189 alloc = &device->vk.alloc;
190
191 return wsi_common_create_swapchain(&device->physical_device->wsi_device,
192 radv_device_to_handle(device),
193 pCreateInfo,
194 alloc,
195 pSwapchain);
196 }
197
198 void radv_DestroySwapchainKHR(
199 VkDevice _device,
200 VkSwapchainKHR swapchain,
201 const VkAllocationCallbacks* pAllocator)
202 {
203 RADV_FROM_HANDLE(radv_device, device, _device);
204 const VkAllocationCallbacks *alloc;
205
206 if (pAllocator)
207 alloc = pAllocator;
208 else
209 alloc = &device->vk.alloc;
210
211 wsi_common_destroy_swapchain(_device, swapchain, alloc);
212 }
213
214 VkResult radv_GetSwapchainImagesKHR(
215 VkDevice device,
216 VkSwapchainKHR swapchain,
217 uint32_t* pSwapchainImageCount,
218 VkImage* pSwapchainImages)
219 {
220 return wsi_common_get_images(swapchain,
221 pSwapchainImageCount,
222 pSwapchainImages);
223 }
224
225 VkResult radv_AcquireNextImageKHR(
226 VkDevice device,
227 VkSwapchainKHR swapchain,
228 uint64_t timeout,
229 VkSemaphore semaphore,
230 VkFence fence,
231 uint32_t* pImageIndex)
232 {
233 VkAcquireNextImageInfoKHR acquire_info = {
234 .sType = VK_STRUCTURE_TYPE_ACQUIRE_NEXT_IMAGE_INFO_KHR,
235 .swapchain = swapchain,
236 .timeout = timeout,
237 .semaphore = semaphore,
238 .fence = fence,
239 .deviceMask = 0,
240 };
241
242 return radv_AcquireNextImage2KHR(device, &acquire_info, pImageIndex);
243 }
244
245 VkResult radv_AcquireNextImage2KHR(
246 VkDevice _device,
247 const VkAcquireNextImageInfoKHR* pAcquireInfo,
248 uint32_t* pImageIndex)
249 {
250 RADV_FROM_HANDLE(radv_device, device, _device);
251 struct radv_physical_device *pdevice = device->physical_device;
252 RADV_FROM_HANDLE(radv_fence, fence, pAcquireInfo->fence);
253 RADV_FROM_HANDLE(radv_semaphore, semaphore, pAcquireInfo->semaphore);
254
255 VkResult result = wsi_common_acquire_next_image2(&pdevice->wsi_device,
256 _device,
257 pAcquireInfo,
258 pImageIndex);
259
260 if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) {
261 if (fence) {
262 if (fence->fence)
263 device->ws->signal_fence(fence->fence);
264 if (fence->temp_syncobj) {
265 device->ws->signal_syncobj(device->ws, fence->temp_syncobj);
266 } else if (fence->syncobj) {
267 device->ws->signal_syncobj(device->ws, fence->syncobj);
268 }
269 }
270 if (semaphore) {
271 struct radv_semaphore_part *part =
272 semaphore->temporary.kind != RADV_SEMAPHORE_NONE ?
273 &semaphore->temporary : &semaphore->permanent;
274
275 switch (part->kind) {
276 case RADV_SEMAPHORE_NONE:
277 case RADV_SEMAPHORE_WINSYS:
278 /* Do not need to do anything. */
279 break;
280 case RADV_SEMAPHORE_TIMELINE:
281 unreachable("WSI only allows binary semaphores.");
282 case RADV_SEMAPHORE_SYNCOBJ:
283 device->ws->signal_syncobj(device->ws, part->syncobj);
284 break;
285 }
286 }
287 }
288 return result;
289 }
290
291 VkResult radv_QueuePresentKHR(
292 VkQueue _queue,
293 const VkPresentInfoKHR* pPresentInfo)
294 {
295 RADV_FROM_HANDLE(radv_queue, queue, _queue);
296 return wsi_common_queue_present(&queue->device->physical_device->wsi_device,
297 radv_device_to_handle(queue->device),
298 _queue,
299 queue->queue_family_index,
300 pPresentInfo);
301 }
302
303
304 VkResult radv_GetDeviceGroupPresentCapabilitiesKHR(
305 VkDevice device,
306 VkDeviceGroupPresentCapabilitiesKHR* pCapabilities)
307 {
308 memset(pCapabilities->presentMask, 0,
309 sizeof(pCapabilities->presentMask));
310 pCapabilities->presentMask[0] = 0x1;
311 pCapabilities->modes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
312
313 return VK_SUCCESS;
314 }
315
316 VkResult radv_GetDeviceGroupSurfacePresentModesKHR(
317 VkDevice device,
318 VkSurfaceKHR surface,
319 VkDeviceGroupPresentModeFlagsKHR* pModes)
320 {
321 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
322
323 return VK_SUCCESS;
324 }
325
326 VkResult radv_GetPhysicalDevicePresentRectanglesKHR(
327 VkPhysicalDevice physicalDevice,
328 VkSurfaceKHR surface,
329 uint32_t* pRectCount,
330 VkRect2D* pRects)
331 {
332 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
333
334 return wsi_common_get_present_rectangles(&device->wsi_device,
335 surface,
336 pRectCount, pRects);
337 }