2 * Copyright © 2015 Intel Corporation
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:
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
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
24 #include "anv_private.h"
25 #include "wsi_common.h"
26 #include "vk_format_info.h"
28 static const struct wsi_callbacks wsi_cbs = {
29 .get_phys_device_format_properties = anv_GetPhysicalDeviceFormatProperties,
33 anv_init_wsi(struct anv_physical_device *physical_device)
37 memset(physical_device->wsi_device.wsi, 0, sizeof(physical_device->wsi_device.wsi));
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)
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),
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);
61 anv_finish_wsi(struct anv_physical_device *physical_device)
63 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
64 wsi_wl_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
66 #ifdef VK_USE_PLATFORM_XCB_KHR
67 wsi_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
71 void anv_DestroySurfaceKHR(
73 VkSurfaceKHR _surface,
74 const VkAllocationCallbacks* pAllocator)
76 ANV_FROM_HANDLE(anv_instance, instance, _instance);
77 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
82 vk_free2(&instance->alloc, pAllocator, surface);
85 VkResult anv_GetPhysicalDeviceSurfaceSupportKHR(
86 VkPhysicalDevice physicalDevice,
87 uint32_t queueFamilyIndex,
88 VkSurfaceKHR _surface,
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];
95 return iface->get_support(surface, &device->wsi_device,
96 &device->instance->alloc,
97 queueFamilyIndex, pSupported);
100 VkResult anv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
101 VkPhysicalDevice physicalDevice,
102 VkSurfaceKHR _surface,
103 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
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];
109 return iface->get_capabilities(surface, pSurfaceCapabilities);
112 VkResult anv_GetPhysicalDeviceSurfaceFormatsKHR(
113 VkPhysicalDevice physicalDevice,
114 VkSurfaceKHR _surface,
115 uint32_t* pSurfaceFormatCount,
116 VkSurfaceFormatKHR* pSurfaceFormats)
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];
122 return iface->get_formats(surface, &device->wsi_device, pSurfaceFormatCount,
126 VkResult anv_GetPhysicalDeviceSurfacePresentModesKHR(
127 VkPhysicalDevice physicalDevice,
128 VkSurfaceKHR _surface,
129 uint32_t* pPresentModeCount,
130 VkPresentModeKHR* pPresentModes)
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];
136 return iface->get_present_modes(surface, pPresentModeCount,
142 x11_anv_wsi_image_create(VkDevice device_h,
143 const VkSwapchainCreateInfoKHR *pCreateInfo,
144 const VkAllocationCallbacks* pAllocator,
146 VkDeviceMemory *memory_p,
149 uint32_t *row_pitch, int *fd_p)
151 struct anv_device *device = anv_device_from_handle(device_h);
153 struct anv_image *image;
156 result = anv_image_create(anv_device_to_handle(device),
157 &(struct anv_image_create_info) {
158 .isl_tiling_flags = ISL_TILING_X_BIT,
161 &(VkImageCreateInfo) {
162 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
163 .imageType = VK_IMAGE_TYPE_2D,
164 .format = pCreateInfo->imageFormat,
166 .width = pCreateInfo->imageExtent.width,
167 .height = pCreateInfo->imageExtent.height,
173 /* FIXME: Need a way to use X tiling to allow scanout */
174 .tiling = VK_IMAGE_TILING_OPTIMAL,
175 .usage = (pCreateInfo->imageUsage |
176 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
181 if (result != VK_SUCCESS)
184 image = anv_image_from_handle(image_h);
185 assert(vk_format_is_color(image->vk_format));
187 VkDeviceMemory memory_h;
188 struct anv_device_memory *memory;
189 result = anv_AllocateMemory(anv_device_to_handle(device),
190 &(VkMemoryAllocateInfo) {
191 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
192 .allocationSize = image->size,
193 .memoryTypeIndex = 0,
195 NULL /* XXX: pAllocator */,
197 if (result != VK_SUCCESS)
198 goto fail_create_image;
200 memory = anv_device_memory_from_handle(memory_h);
201 memory->bo.is_winsys_bo = true;
203 anv_BindImageMemory(device_h, image_h, memory_h, 0);
205 struct anv_surface *surface = &image->color_surface;
206 assert(surface->isl.tiling == ISL_TILING_X);
208 *row_pitch = surface->isl.row_pitch;
209 int ret = anv_gem_set_tiling(device, memory->bo.gem_handle,
210 surface->isl.row_pitch, I915_TILING_X);
212 /* FINISHME: Choose a better error. */
213 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
214 "set_tiling failed: %m");
215 goto fail_alloc_memory;
218 int fd = anv_gem_handle_to_fd(device, memory->bo.gem_handle);
220 /* FINISHME: Choose a better error. */
221 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
222 "handle_to_fd failed: %m");
223 goto fail_alloc_memory;
227 *memory_p = memory_h;
230 *offset = image->offset;
233 anv_FreeMemory(device_h, memory_h, pAllocator);
236 anv_DestroyImage(device_h, image_h, pAllocator);
241 x11_anv_wsi_image_free(VkDevice device,
242 const VkAllocationCallbacks* pAllocator,
244 VkDeviceMemory memory_h)
246 anv_DestroyImage(device, image_h, pAllocator);
248 anv_FreeMemory(device, memory_h, pAllocator);
251 static const struct wsi_image_fns anv_wsi_image_fns = {
252 .create_wsi_image = x11_anv_wsi_image_create,
253 .free_wsi_image = x11_anv_wsi_image_free,
256 VkResult anv_CreateSwapchainKHR(
258 const VkSwapchainCreateInfoKHR* pCreateInfo,
259 const VkAllocationCallbacks* pAllocator,
260 VkSwapchainKHR* pSwapchain)
262 ANV_FROM_HANDLE(anv_device, device, _device);
263 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pCreateInfo->surface);
264 struct wsi_interface *iface =
265 device->instance->physicalDevice.wsi_device.wsi[surface->platform];
266 struct wsi_swapchain *swapchain;
267 const VkAllocationCallbacks *alloc;
272 alloc = &device->alloc;
273 VkResult result = iface->create_swapchain(surface, _device,
274 &device->instance->physicalDevice.wsi_device,
276 alloc, &anv_wsi_image_fns,
278 if (result != VK_SUCCESS)
281 swapchain->alloc = *alloc;
283 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++)
284 swapchain->fences[i] = VK_NULL_HANDLE;
286 *pSwapchain = wsi_swapchain_to_handle(swapchain);
291 void anv_DestroySwapchainKHR(
293 VkSwapchainKHR _swapchain,
294 const VkAllocationCallbacks* pAllocator)
296 ANV_FROM_HANDLE(anv_device, device, _device);
297 ANV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
298 const VkAllocationCallbacks *alloc;
306 alloc = &device->alloc;
307 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++) {
308 if (swapchain->fences[i] != VK_NULL_HANDLE)
309 anv_DestroyFence(_device, swapchain->fences[i], pAllocator);
312 swapchain->destroy(swapchain, alloc);
315 VkResult anv_GetSwapchainImagesKHR(
317 VkSwapchainKHR _swapchain,
318 uint32_t* pSwapchainImageCount,
319 VkImage* pSwapchainImages)
321 ANV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
323 return swapchain->get_images(swapchain, pSwapchainImageCount,
327 VkResult anv_AcquireNextImageKHR(
329 VkSwapchainKHR _swapchain,
331 VkSemaphore semaphore,
333 uint32_t* pImageIndex)
335 ANV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
336 ANV_FROM_HANDLE(anv_fence, fence, _fence);
338 VkResult result = swapchain->acquire_next_image(swapchain, timeout,
339 semaphore, pImageIndex);
341 /* Thanks to implicit sync, the image is ready immediately. */
343 fence->state = ANV_FENCE_STATE_SIGNALED;
348 VkResult anv_QueuePresentKHR(
350 const VkPresentInfoKHR* pPresentInfo)
352 ANV_FROM_HANDLE(anv_queue, queue, _queue);
353 VkResult result = VK_SUCCESS;
355 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
356 ANV_FROM_HANDLE(wsi_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
357 VkResult item_result;
359 assert(anv_device_from_handle(swapchain->device) == queue->device);
361 if (swapchain->fences[0] == VK_NULL_HANDLE) {
362 item_result = anv_CreateFence(anv_device_to_handle(queue->device),
363 &(VkFenceCreateInfo) {
364 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
366 }, &swapchain->alloc, &swapchain->fences[0]);
367 if (pPresentInfo->pResults != NULL)
368 pPresentInfo->pResults[i] = item_result;
369 result = result == VK_SUCCESS ? item_result : result;
370 if (item_result != VK_SUCCESS)
373 anv_ResetFences(anv_device_to_handle(queue->device),
374 1, &swapchain->fences[0]);
377 anv_QueueSubmit(_queue, 0, NULL, swapchain->fences[0]);
379 item_result = swapchain->queue_present(swapchain,
380 pPresentInfo->pImageIndices[i]);
381 /* TODO: What if one of them returns OUT_OF_DATE? */
382 if (pPresentInfo->pResults != NULL)
383 pPresentInfo->pResults[i] = item_result;
384 result = result == VK_SUCCESS ? item_result : result;
385 if (item_result != VK_SUCCESS)
388 VkFence last = swapchain->fences[2];
389 swapchain->fences[2] = swapchain->fences[1];
390 swapchain->fences[1] = swapchain->fences[0];
391 swapchain->fences[0] = last;
393 if (last != VK_NULL_HANDLE) {
394 anv_WaitForFences(anv_device_to_handle(queue->device),