vulkan: Add KHR_display extension using DRM [v10]
[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 &device->instance->alloc,
79 pSupported);
80 }
81
82 VkResult radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
83 VkPhysicalDevice physicalDevice,
84 VkSurfaceKHR surface,
85 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
86 {
87 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
88
89 return wsi_common_get_surface_capabilities(&device->wsi_device,
90 surface,
91 pSurfaceCapabilities);
92 }
93
94 VkResult radv_GetPhysicalDeviceSurfaceCapabilities2KHR(
95 VkPhysicalDevice physicalDevice,
96 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
97 VkSurfaceCapabilities2KHR* pSurfaceCapabilities)
98 {
99 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
100
101 return wsi_common_get_surface_capabilities2(&device->wsi_device,
102 pSurfaceInfo,
103 pSurfaceCapabilities);
104 }
105
106 VkResult radv_GetPhysicalDeviceSurfaceFormatsKHR(
107 VkPhysicalDevice physicalDevice,
108 VkSurfaceKHR surface,
109 uint32_t* pSurfaceFormatCount,
110 VkSurfaceFormatKHR* pSurfaceFormats)
111 {
112 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
113
114 return wsi_common_get_surface_formats(&device->wsi_device,
115 surface,
116 pSurfaceFormatCount,
117 pSurfaceFormats);
118 }
119
120 VkResult radv_GetPhysicalDeviceSurfaceFormats2KHR(
121 VkPhysicalDevice physicalDevice,
122 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
123 uint32_t* pSurfaceFormatCount,
124 VkSurfaceFormat2KHR* pSurfaceFormats)
125 {
126 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
127
128 return wsi_common_get_surface_formats2(&device->wsi_device,
129 pSurfaceInfo,
130 pSurfaceFormatCount,
131 pSurfaceFormats);
132 }
133
134 VkResult radv_GetPhysicalDeviceSurfacePresentModesKHR(
135 VkPhysicalDevice physicalDevice,
136 VkSurfaceKHR surface,
137 uint32_t* pPresentModeCount,
138 VkPresentModeKHR* pPresentModes)
139 {
140 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
141
142 return wsi_common_get_surface_present_modes(&device->wsi_device,
143 surface,
144 pPresentModeCount,
145 pPresentModes);
146 }
147
148 VkResult radv_CreateSwapchainKHR(
149 VkDevice _device,
150 const VkSwapchainCreateInfoKHR* pCreateInfo,
151 const VkAllocationCallbacks* pAllocator,
152 VkSwapchainKHR* pSwapchain)
153 {
154 RADV_FROM_HANDLE(radv_device, device, _device);
155 const VkAllocationCallbacks *alloc;
156 if (pAllocator)
157 alloc = pAllocator;
158 else
159 alloc = &device->alloc;
160
161 return wsi_common_create_swapchain(&device->physical_device->wsi_device,
162 radv_device_to_handle(device),
163 device->physical_device->local_fd,
164 pCreateInfo,
165 alloc,
166 pSwapchain);
167 }
168
169 void radv_DestroySwapchainKHR(
170 VkDevice _device,
171 VkSwapchainKHR swapchain,
172 const VkAllocationCallbacks* pAllocator)
173 {
174 RADV_FROM_HANDLE(radv_device, device, _device);
175 const VkAllocationCallbacks *alloc;
176
177 if (pAllocator)
178 alloc = pAllocator;
179 else
180 alloc = &device->alloc;
181
182 wsi_common_destroy_swapchain(_device, swapchain, alloc);
183 }
184
185 VkResult radv_GetSwapchainImagesKHR(
186 VkDevice device,
187 VkSwapchainKHR swapchain,
188 uint32_t* pSwapchainImageCount,
189 VkImage* pSwapchainImages)
190 {
191 return wsi_common_get_images(swapchain,
192 pSwapchainImageCount,
193 pSwapchainImages);
194 }
195
196 VkResult radv_AcquireNextImageKHR(
197 VkDevice _device,
198 VkSwapchainKHR swapchain,
199 uint64_t timeout,
200 VkSemaphore semaphore,
201 VkFence _fence,
202 uint32_t* pImageIndex)
203 {
204 RADV_FROM_HANDLE(radv_device, device, _device);
205 struct radv_physical_device *pdevice = device->physical_device;
206 RADV_FROM_HANDLE(radv_fence, fence, _fence);
207
208 VkResult result = wsi_common_acquire_next_image(&pdevice->wsi_device,
209 _device,
210 swapchain,
211 timeout,
212 semaphore,
213 pImageIndex);
214
215 if (fence && (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR)) {
216 fence->submitted = true;
217 fence->signalled = true;
218 if (fence->temp_syncobj) {
219 device->ws->signal_syncobj(device->ws, fence->temp_syncobj);
220 } else if (fence->syncobj) {
221 device->ws->signal_syncobj(device->ws, fence->syncobj);
222 }
223 }
224 return result;
225 }
226
227 VkResult radv_QueuePresentKHR(
228 VkQueue _queue,
229 const VkPresentInfoKHR* pPresentInfo)
230 {
231 RADV_FROM_HANDLE(radv_queue, queue, _queue);
232 return wsi_common_queue_present(&queue->device->physical_device->wsi_device,
233 radv_device_to_handle(queue->device),
234 _queue,
235 queue->queue_family_index,
236 pPresentInfo);
237 }
238
239
240 VkResult radv_GetDeviceGroupPresentCapabilitiesKHR(
241 VkDevice device,
242 VkDeviceGroupPresentCapabilitiesKHR* pCapabilities)
243 {
244 memset(pCapabilities->presentMask, 0,
245 sizeof(pCapabilities->presentMask));
246 pCapabilities->presentMask[0] = 0x1;
247 pCapabilities->modes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
248
249 return VK_SUCCESS;
250 }
251
252 VkResult radv_GetDeviceGroupSurfacePresentModesKHR(
253 VkDevice device,
254 VkSurfaceKHR surface,
255 VkDeviceGroupPresentModeFlagsKHR* pModes)
256 {
257 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
258
259 return VK_SUCCESS;
260 }