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