vulkan/wsi/radv: add initial prime support (v1.1)
[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
28 static const struct wsi_callbacks wsi_cbs = {
29 .get_phys_device_format_properties = anv_GetPhysicalDeviceFormatProperties,
30 };
31
32 VkResult
33 anv_init_wsi(struct anv_physical_device *physical_device)
34 {
35 VkResult result;
36
37 memset(physical_device->wsi_device.wsi, 0, sizeof(physical_device->wsi_device.wsi));
38
39 #ifdef VK_USE_PLATFORM_XCB_KHR
40 result = wsi_x11_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
41 if (result != VK_SUCCESS)
42 return result;
43 #endif
44
45 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
46 result = wsi_wl_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc,
47 anv_physical_device_to_handle(physical_device),
48 &wsi_cbs);
49 if (result != VK_SUCCESS) {
50 #ifdef VK_USE_PLATFORM_XCB_KHR
51 wsi_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
52 #endif
53 return result;
54 }
55 #endif
56
57 return VK_SUCCESS;
58 }
59
60 void
61 anv_finish_wsi(struct anv_physical_device *physical_device)
62 {
63 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
64 wsi_wl_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
65 #endif
66 #ifdef VK_USE_PLATFORM_XCB_KHR
67 wsi_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
68 #endif
69 }
70
71 void anv_DestroySurfaceKHR(
72 VkInstance _instance,
73 VkSurfaceKHR _surface,
74 const VkAllocationCallbacks* pAllocator)
75 {
76 ANV_FROM_HANDLE(anv_instance, instance, _instance);
77 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
78
79 if (!surface)
80 return;
81
82 vk_free2(&instance->alloc, pAllocator, surface);
83 }
84
85 VkResult anv_GetPhysicalDeviceSurfaceSupportKHR(
86 VkPhysicalDevice physicalDevice,
87 uint32_t queueFamilyIndex,
88 VkSurfaceKHR _surface,
89 VkBool32* pSupported)
90 {
91 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
92 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
93 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
94
95 return iface->get_support(surface, &device->wsi_device,
96 &device->instance->alloc,
97 queueFamilyIndex, device->local_fd, false, pSupported);
98 }
99
100 VkResult anv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
101 VkPhysicalDevice physicalDevice,
102 VkSurfaceKHR _surface,
103 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
104 {
105 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
106 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
107 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
108
109 return iface->get_capabilities(surface, pSurfaceCapabilities);
110 }
111
112 VkResult anv_GetPhysicalDeviceSurfaceFormatsKHR(
113 VkPhysicalDevice physicalDevice,
114 VkSurfaceKHR _surface,
115 uint32_t* pSurfaceFormatCount,
116 VkSurfaceFormatKHR* pSurfaceFormats)
117 {
118 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
119 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
120 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
121
122 return iface->get_formats(surface, &device->wsi_device, pSurfaceFormatCount,
123 pSurfaceFormats);
124 }
125
126 VkResult anv_GetPhysicalDeviceSurfacePresentModesKHR(
127 VkPhysicalDevice physicalDevice,
128 VkSurfaceKHR _surface,
129 uint32_t* pPresentModeCount,
130 VkPresentModeKHR* pPresentModes)
131 {
132 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
133 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
134 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
135
136 return iface->get_present_modes(surface, pPresentModeCount,
137 pPresentModes);
138 }
139
140
141 static VkResult
142 x11_anv_wsi_image_create(VkDevice device_h,
143 const VkSwapchainCreateInfoKHR *pCreateInfo,
144 const VkAllocationCallbacks* pAllocator,
145 bool different_gpu,
146 bool linear,
147 VkImage *image_p,
148 VkDeviceMemory *memory_p,
149 uint32_t *size,
150 uint32_t *offset,
151 uint32_t *row_pitch, int *fd_p)
152 {
153 struct anv_device *device = anv_device_from_handle(device_h);
154 VkImage image_h;
155 struct anv_image *image;
156
157 VkResult result;
158 result = anv_image_create(anv_device_to_handle(device),
159 &(struct anv_image_create_info) {
160 .isl_tiling_flags = ISL_TILING_X_BIT,
161 .stride = 0,
162 .vk_info =
163 &(VkImageCreateInfo) {
164 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
165 .imageType = VK_IMAGE_TYPE_2D,
166 .format = pCreateInfo->imageFormat,
167 .extent = {
168 .width = pCreateInfo->imageExtent.width,
169 .height = pCreateInfo->imageExtent.height,
170 .depth = 1
171 },
172 .mipLevels = 1,
173 .arrayLayers = 1,
174 .samples = 1,
175 /* FIXME: Need a way to use X tiling to allow scanout */
176 .tiling = VK_IMAGE_TILING_OPTIMAL,
177 .usage = (pCreateInfo->imageUsage |
178 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
179 .flags = 0,
180 }},
181 NULL,
182 &image_h);
183 if (result != VK_SUCCESS)
184 return result;
185
186 image = anv_image_from_handle(image_h);
187 assert(vk_format_is_color(image->vk_format));
188
189 VkDeviceMemory memory_h;
190 struct anv_device_memory *memory;
191 result = anv_AllocateMemory(anv_device_to_handle(device),
192 &(VkMemoryAllocateInfo) {
193 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
194 .allocationSize = image->size,
195 .memoryTypeIndex = 0,
196 },
197 NULL /* XXX: pAllocator */,
198 &memory_h);
199 if (result != VK_SUCCESS)
200 goto fail_create_image;
201
202 memory = anv_device_memory_from_handle(memory_h);
203 memory->bo.is_winsys_bo = true;
204
205 anv_BindImageMemory(device_h, image_h, memory_h, 0);
206
207 struct anv_surface *surface = &image->color_surface;
208 assert(surface->isl.tiling == ISL_TILING_X);
209
210 *row_pitch = surface->isl.row_pitch;
211 int ret = anv_gem_set_tiling(device, memory->bo.gem_handle,
212 surface->isl.row_pitch, I915_TILING_X);
213 if (ret) {
214 /* FINISHME: Choose a better error. */
215 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
216 "set_tiling failed: %m");
217 goto fail_alloc_memory;
218 }
219
220 int fd = anv_gem_handle_to_fd(device, memory->bo.gem_handle);
221 if (fd == -1) {
222 /* FINISHME: Choose a better error. */
223 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
224 "handle_to_fd failed: %m");
225 goto fail_alloc_memory;
226 }
227
228 *image_p = image_h;
229 *memory_p = memory_h;
230 *fd_p = fd;
231 *size = image->size;
232 *offset = image->offset;
233 return VK_SUCCESS;
234 fail_alloc_memory:
235 anv_FreeMemory(device_h, memory_h, pAllocator);
236
237 fail_create_image:
238 anv_DestroyImage(device_h, image_h, pAllocator);
239 return result;
240 }
241
242 static void
243 x11_anv_wsi_image_free(VkDevice device,
244 const VkAllocationCallbacks* pAllocator,
245 VkImage image_h,
246 VkDeviceMemory memory_h)
247 {
248 anv_DestroyImage(device, image_h, pAllocator);
249
250 anv_FreeMemory(device, memory_h, pAllocator);
251 }
252
253 static const struct wsi_image_fns anv_wsi_image_fns = {
254 .create_wsi_image = x11_anv_wsi_image_create,
255 .free_wsi_image = x11_anv_wsi_image_free,
256 };
257
258 VkResult anv_CreateSwapchainKHR(
259 VkDevice _device,
260 const VkSwapchainCreateInfoKHR* pCreateInfo,
261 const VkAllocationCallbacks* pAllocator,
262 VkSwapchainKHR* pSwapchain)
263 {
264 ANV_FROM_HANDLE(anv_device, device, _device);
265 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pCreateInfo->surface);
266 struct wsi_interface *iface =
267 device->instance->physicalDevice.wsi_device.wsi[surface->platform];
268 struct wsi_swapchain *swapchain;
269 const VkAllocationCallbacks *alloc;
270
271 if (pAllocator)
272 alloc = pAllocator;
273 else
274 alloc = &device->alloc;
275 VkResult result = iface->create_swapchain(surface, _device,
276 &device->instance->physicalDevice.wsi_device,
277 device->instance->physicalDevice.local_fd,
278 pCreateInfo,
279 alloc, &anv_wsi_image_fns,
280 &swapchain);
281 if (result != VK_SUCCESS)
282 return result;
283
284 swapchain->alloc = *alloc;
285
286 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++)
287 swapchain->fences[i] = VK_NULL_HANDLE;
288
289 *pSwapchain = wsi_swapchain_to_handle(swapchain);
290
291 return VK_SUCCESS;
292 }
293
294 void anv_DestroySwapchainKHR(
295 VkDevice _device,
296 VkSwapchainKHR _swapchain,
297 const VkAllocationCallbacks* pAllocator)
298 {
299 ANV_FROM_HANDLE(anv_device, device, _device);
300 ANV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
301 const VkAllocationCallbacks *alloc;
302
303 if (!swapchain)
304 return;
305
306 if (pAllocator)
307 alloc = pAllocator;
308 else
309 alloc = &device->alloc;
310 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++) {
311 if (swapchain->fences[i] != VK_NULL_HANDLE)
312 anv_DestroyFence(_device, swapchain->fences[i], pAllocator);
313 }
314
315 swapchain->destroy(swapchain, alloc);
316 }
317
318 VkResult anv_GetSwapchainImagesKHR(
319 VkDevice device,
320 VkSwapchainKHR _swapchain,
321 uint32_t* pSwapchainImageCount,
322 VkImage* pSwapchainImages)
323 {
324 ANV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
325
326 return swapchain->get_images(swapchain, pSwapchainImageCount,
327 pSwapchainImages);
328 }
329
330 VkResult anv_AcquireNextImageKHR(
331 VkDevice device,
332 VkSwapchainKHR _swapchain,
333 uint64_t timeout,
334 VkSemaphore semaphore,
335 VkFence _fence,
336 uint32_t* pImageIndex)
337 {
338 ANV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
339 ANV_FROM_HANDLE(anv_fence, fence, _fence);
340
341 VkResult result = swapchain->acquire_next_image(swapchain, timeout,
342 semaphore, pImageIndex);
343
344 /* Thanks to implicit sync, the image is ready immediately. */
345 if (fence)
346 fence->state = ANV_FENCE_STATE_SIGNALED;
347
348 return result;
349 }
350
351 VkResult anv_QueuePresentKHR(
352 VkQueue _queue,
353 const VkPresentInfoKHR* pPresentInfo)
354 {
355 ANV_FROM_HANDLE(anv_queue, queue, _queue);
356 VkResult result = VK_SUCCESS;
357
358 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
359 ANV_FROM_HANDLE(wsi_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
360 VkResult item_result;
361
362 assert(anv_device_from_handle(swapchain->device) == queue->device);
363
364 if (swapchain->fences[0] == VK_NULL_HANDLE) {
365 item_result = anv_CreateFence(anv_device_to_handle(queue->device),
366 &(VkFenceCreateInfo) {
367 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
368 .flags = 0,
369 }, &swapchain->alloc, &swapchain->fences[0]);
370 if (pPresentInfo->pResults != NULL)
371 pPresentInfo->pResults[i] = item_result;
372 result = result == VK_SUCCESS ? item_result : result;
373 if (item_result != VK_SUCCESS)
374 continue;
375 } else {
376 anv_ResetFences(anv_device_to_handle(queue->device),
377 1, &swapchain->fences[0]);
378 }
379
380 anv_QueueSubmit(_queue, 0, NULL, swapchain->fences[0]);
381
382 item_result = swapchain->queue_present(swapchain,
383 pPresentInfo->pImageIndices[i]);
384 /* TODO: What if one of them returns OUT_OF_DATE? */
385 if (pPresentInfo->pResults != NULL)
386 pPresentInfo->pResults[i] = item_result;
387 result = result == VK_SUCCESS ? item_result : result;
388 if (item_result != VK_SUCCESS)
389 continue;
390
391 VkFence last = swapchain->fences[2];
392 swapchain->fences[2] = swapchain->fences[1];
393 swapchain->fences[1] = swapchain->fences[0];
394 swapchain->fences[0] = last;
395
396 if (last != VK_NULL_HANDLE) {
397 anv_WaitForFences(anv_device_to_handle(queue->device),
398 1, &last, true, 1);
399 }
400 }
401
402 return VK_SUCCESS;
403 }