vulkan: Add KHR_display extension using DRM [v10]
[mesa.git] / src / intel / 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_private.h"
25 #include "wsi_common.h"
26 #include "vk_format_info.h"
27 #include "vk_util.h"
28
29 static PFN_vkVoidFunction
30 anv_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
31 {
32 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
33 return anv_lookup_entrypoint(&physical_device->info, pName);
34 }
35
36 static uint64_t
37 anv_wsi_image_get_modifier(VkImage _image)
38 {
39 ANV_FROM_HANDLE(anv_image, image, _image);
40 return image->drm_format_mod;
41 }
42
43 VkResult
44 anv_init_wsi(struct anv_physical_device *physical_device)
45 {
46 VkResult result;
47
48 result = wsi_device_init(&physical_device->wsi_device,
49 anv_physical_device_to_handle(physical_device),
50 anv_wsi_proc_addr,
51 &physical_device->instance->alloc,
52 physical_device->master_fd);
53 if (result != VK_SUCCESS)
54 return result;
55
56 physical_device->wsi_device.supports_modifiers = true;
57 physical_device->wsi_device.image_get_modifier = anv_wsi_image_get_modifier;
58
59 return VK_SUCCESS;
60 }
61
62 void
63 anv_finish_wsi(struct anv_physical_device *physical_device)
64 {
65 wsi_device_finish(&physical_device->wsi_device,
66 &physical_device->instance->alloc);
67 }
68
69 void anv_DestroySurfaceKHR(
70 VkInstance _instance,
71 VkSurfaceKHR _surface,
72 const VkAllocationCallbacks* pAllocator)
73 {
74 ANV_FROM_HANDLE(anv_instance, instance, _instance);
75 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
76
77 if (!surface)
78 return;
79
80 vk_free2(&instance->alloc, pAllocator, surface);
81 }
82
83 VkResult anv_GetPhysicalDeviceSurfaceSupportKHR(
84 VkPhysicalDevice physicalDevice,
85 uint32_t queueFamilyIndex,
86 VkSurfaceKHR surface,
87 VkBool32* pSupported)
88 {
89 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
90
91 return wsi_common_get_surface_support(&device->wsi_device,
92 device->local_fd,
93 queueFamilyIndex,
94 surface,
95 &device->instance->alloc,
96 pSupported);
97 }
98
99 VkResult anv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
100 VkPhysicalDevice physicalDevice,
101 VkSurfaceKHR surface,
102 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
103 {
104 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
105
106 return wsi_common_get_surface_capabilities(&device->wsi_device,
107 surface,
108 pSurfaceCapabilities);
109 }
110
111 VkResult anv_GetPhysicalDeviceSurfaceCapabilities2KHR(
112 VkPhysicalDevice physicalDevice,
113 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
114 VkSurfaceCapabilities2KHR* pSurfaceCapabilities)
115 {
116 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
117
118 return wsi_common_get_surface_capabilities2(&device->wsi_device,
119 pSurfaceInfo,
120 pSurfaceCapabilities);
121 }
122
123 VkResult anv_GetPhysicalDeviceSurfaceFormatsKHR(
124 VkPhysicalDevice physicalDevice,
125 VkSurfaceKHR surface,
126 uint32_t* pSurfaceFormatCount,
127 VkSurfaceFormatKHR* pSurfaceFormats)
128 {
129 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
130
131 return wsi_common_get_surface_formats(&device->wsi_device, surface,
132 pSurfaceFormatCount, pSurfaceFormats);
133 }
134
135 VkResult anv_GetPhysicalDeviceSurfaceFormats2KHR(
136 VkPhysicalDevice physicalDevice,
137 const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
138 uint32_t* pSurfaceFormatCount,
139 VkSurfaceFormat2KHR* pSurfaceFormats)
140 {
141 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
142
143 return wsi_common_get_surface_formats2(&device->wsi_device, pSurfaceInfo,
144 pSurfaceFormatCount, pSurfaceFormats);
145 }
146
147 VkResult anv_GetPhysicalDeviceSurfacePresentModesKHR(
148 VkPhysicalDevice physicalDevice,
149 VkSurfaceKHR surface,
150 uint32_t* pPresentModeCount,
151 VkPresentModeKHR* pPresentModes)
152 {
153 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
154
155 return wsi_common_get_surface_present_modes(&device->wsi_device, surface,
156 pPresentModeCount,
157 pPresentModes);
158 }
159
160 VkResult anv_CreateSwapchainKHR(
161 VkDevice _device,
162 const VkSwapchainCreateInfoKHR* pCreateInfo,
163 const VkAllocationCallbacks* pAllocator,
164 VkSwapchainKHR* pSwapchain)
165 {
166 ANV_FROM_HANDLE(anv_device, device, _device);
167 struct wsi_device *wsi_device = &device->instance->physicalDevice.wsi_device;
168 const VkAllocationCallbacks *alloc;
169
170 if (pAllocator)
171 alloc = pAllocator;
172 else
173 alloc = &device->alloc;
174
175 return wsi_common_create_swapchain(wsi_device, _device, device->fd,
176 pCreateInfo, alloc, pSwapchain);
177 }
178
179 void anv_DestroySwapchainKHR(
180 VkDevice _device,
181 VkSwapchainKHR swapchain,
182 const VkAllocationCallbacks* pAllocator)
183 {
184 ANV_FROM_HANDLE(anv_device, device, _device);
185 const VkAllocationCallbacks *alloc;
186
187 if (pAllocator)
188 alloc = pAllocator;
189 else
190 alloc = &device->alloc;
191
192 wsi_common_destroy_swapchain(_device, swapchain, alloc);
193 }
194
195 VkResult anv_GetSwapchainImagesKHR(
196 VkDevice device,
197 VkSwapchainKHR swapchain,
198 uint32_t* pSwapchainImageCount,
199 VkImage* pSwapchainImages)
200 {
201 return wsi_common_get_images(swapchain,
202 pSwapchainImageCount,
203 pSwapchainImages);
204 }
205
206 VkResult anv_AcquireNextImageKHR(
207 VkDevice _device,
208 VkSwapchainKHR swapchain,
209 uint64_t timeout,
210 VkSemaphore semaphore,
211 VkFence fence,
212 uint32_t* pImageIndex)
213 {
214 ANV_FROM_HANDLE(anv_device, device, _device);
215 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
216
217 VkResult result = wsi_common_acquire_next_image(&pdevice->wsi_device,
218 _device,
219 swapchain,
220 timeout,
221 semaphore,
222 pImageIndex);
223
224 /* Thanks to implicit sync, the image is ready immediately. However, we
225 * should wait for the current GPU state to finish.
226 */
227 if (fence != VK_NULL_HANDLE)
228 anv_QueueSubmit(anv_queue_to_handle(&device->queue), 0, NULL, fence);
229
230 return result;
231 }
232
233 VkResult anv_QueuePresentKHR(
234 VkQueue _queue,
235 const VkPresentInfoKHR* pPresentInfo)
236 {
237 ANV_FROM_HANDLE(anv_queue, queue, _queue);
238 struct anv_physical_device *pdevice =
239 &queue->device->instance->physicalDevice;
240
241 return wsi_common_queue_present(&pdevice->wsi_device,
242 anv_device_to_handle(queue->device),
243 _queue, 0,
244 pPresentInfo);
245 }
246
247 VkResult anv_GetDeviceGroupPresentCapabilitiesKHR(
248 VkDevice device,
249 VkDeviceGroupPresentCapabilitiesKHR* pCapabilities)
250 {
251 memset(pCapabilities->presentMask, 0,
252 sizeof(pCapabilities->presentMask));
253 pCapabilities->presentMask[0] = 0x1;
254 pCapabilities->modes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
255
256 return VK_SUCCESS;
257 }
258
259 VkResult anv_GetDeviceGroupSurfacePresentModesKHR(
260 VkDevice device,
261 VkSurfaceKHR surface,
262 VkDeviceGroupPresentModeFlagsKHR* pModes)
263 {
264 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
265
266 return VK_SUCCESS;
267 }