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"
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
),
37 anv_init_wsi(struct anv_physical_device
*physical_device
)
41 memset(physical_device
->wsi_device
.wsi
, 0, sizeof(physical_device
->wsi_device
.wsi
));
43 #ifdef VK_USE_PLATFORM_XCB_KHR
44 result
= wsi_x11_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
45 if (result
!= VK_SUCCESS
)
49 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
50 result
= wsi_wl_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
,
51 anv_physical_device_to_handle(physical_device
),
53 if (result
!= VK_SUCCESS
) {
54 #ifdef VK_USE_PLATFORM_XCB_KHR
55 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
65 anv_finish_wsi(struct anv_physical_device
*physical_device
)
67 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
68 wsi_wl_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
70 #ifdef VK_USE_PLATFORM_XCB_KHR
71 wsi_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
75 void anv_DestroySurfaceKHR(
77 VkSurfaceKHR _surface
,
78 const VkAllocationCallbacks
* pAllocator
)
80 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
81 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
86 vk_free2(&instance
->alloc
, pAllocator
, surface
);
89 VkResult
anv_GetPhysicalDeviceSurfaceSupportKHR(
90 VkPhysicalDevice physicalDevice
,
91 uint32_t queueFamilyIndex
,
92 VkSurfaceKHR _surface
,
95 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
96 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
97 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
99 return iface
->get_support(surface
, &device
->wsi_device
,
100 &device
->instance
->alloc
,
101 queueFamilyIndex
, device
->local_fd
, false, pSupported
);
104 VkResult
anv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
105 VkPhysicalDevice physicalDevice
,
106 VkSurfaceKHR _surface
,
107 VkSurfaceCapabilitiesKHR
* pSurfaceCapabilities
)
109 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
110 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
111 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
113 return iface
->get_capabilities(surface
, pSurfaceCapabilities
);
116 VkResult
anv_GetPhysicalDeviceSurfaceCapabilities2KHR(
117 VkPhysicalDevice physicalDevice
,
118 const VkPhysicalDeviceSurfaceInfo2KHR
* pSurfaceInfo
,
119 VkSurfaceCapabilities2KHR
* pSurfaceCapabilities
)
121 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
122 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
123 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
125 return iface
->get_capabilities2(surface
, pSurfaceInfo
->pNext
,
126 pSurfaceCapabilities
);
129 VkResult
anv_GetPhysicalDeviceSurfaceFormatsKHR(
130 VkPhysicalDevice physicalDevice
,
131 VkSurfaceKHR _surface
,
132 uint32_t* pSurfaceFormatCount
,
133 VkSurfaceFormatKHR
* pSurfaceFormats
)
135 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
136 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
137 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
139 return iface
->get_formats(surface
, &device
->wsi_device
, pSurfaceFormatCount
,
143 VkResult
anv_GetPhysicalDeviceSurfaceFormats2KHR(
144 VkPhysicalDevice physicalDevice
,
145 const VkPhysicalDeviceSurfaceInfo2KHR
* pSurfaceInfo
,
146 uint32_t* pSurfaceFormatCount
,
147 VkSurfaceFormat2KHR
* pSurfaceFormats
)
149 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
150 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pSurfaceInfo
->surface
);
151 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
153 return iface
->get_formats2(surface
, &device
->wsi_device
, pSurfaceInfo
->pNext
,
154 pSurfaceFormatCount
, pSurfaceFormats
);
157 VkResult
anv_GetPhysicalDeviceSurfacePresentModesKHR(
158 VkPhysicalDevice physicalDevice
,
159 VkSurfaceKHR _surface
,
160 uint32_t* pPresentModeCount
,
161 VkPresentModeKHR
* pPresentModes
)
163 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
164 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, _surface
);
165 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
167 return iface
->get_present_modes(surface
, pPresentModeCount
,
173 anv_wsi_image_create(VkDevice device_h
,
174 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
175 const VkAllocationCallbacks
* pAllocator
,
178 struct wsi_image
*wsi_image
)
180 struct anv_device
*device
= anv_device_from_handle(device_h
);
182 struct anv_image
*image
;
185 result
= anv_image_create(anv_device_to_handle(device
),
186 &(struct anv_image_create_info
) {
187 .isl_tiling_flags
= ISL_TILING_X_BIT
,
190 &(VkImageCreateInfo
) {
191 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
192 .imageType
= VK_IMAGE_TYPE_2D
,
193 .format
= pCreateInfo
->imageFormat
,
195 .width
= pCreateInfo
->imageExtent
.width
,
196 .height
= pCreateInfo
->imageExtent
.height
,
202 /* FIXME: Need a way to use X tiling to allow scanout */
203 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
204 .usage
= (pCreateInfo
->imageUsage
|
205 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
),
210 if (result
!= VK_SUCCESS
)
213 image
= anv_image_from_handle(image_h
);
214 assert(vk_format_is_color(image
->vk_format
));
216 VkDeviceMemory memory_h
;
217 struct anv_device_memory
*memory
;
218 result
= anv_AllocateMemory(anv_device_to_handle(device
),
219 &(VkMemoryAllocateInfo
) {
220 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
221 .allocationSize
= image
->size
,
222 .memoryTypeIndex
= 0,
224 NULL
/* XXX: pAllocator */,
226 if (result
!= VK_SUCCESS
)
227 goto fail_create_image
;
229 memory
= anv_device_memory_from_handle(memory_h
);
231 /* We need to set the WRITE flag on window system buffers so that GEM will
232 * know we're writing to them and synchronize uses on other rings (eg if
233 * the display server uses the blitter ring).
235 memory
->bo
->flags
&= ~EXEC_OBJECT_ASYNC
;
236 memory
->bo
->flags
|= EXEC_OBJECT_WRITE
;
238 anv_BindImageMemory(device_h
, image_h
, memory_h
, 0);
239 assert(image
->planes
[0].offset
== 0);
241 struct anv_surface
*surface
= &image
->planes
[0].surface
;
242 assert(surface
->isl
.tiling
== ISL_TILING_X
);
244 int ret
= anv_gem_set_tiling(device
, memory
->bo
->gem_handle
,
245 surface
->isl
.row_pitch
, I915_TILING_X
);
247 /* FINISHME: Choose a better error. */
248 result
= vk_errorf(device
->instance
, device
,
249 VK_ERROR_OUT_OF_DEVICE_MEMORY
,
250 "set_tiling failed: %m");
251 goto fail_alloc_memory
;
254 int fd
= anv_gem_handle_to_fd(device
, memory
->bo
->gem_handle
);
256 /* FINISHME: Choose a better error. */
257 result
= vk_errorf(device
->instance
, device
,
258 VK_ERROR_OUT_OF_DEVICE_MEMORY
,
259 "handle_to_fd failed: %m");
260 goto fail_alloc_memory
;
263 wsi_image
->image
= image_h
;
264 wsi_image
->memory
= memory_h
;
266 wsi_image
->size
= image
->size
;
267 wsi_image
->offset
= 0;
268 wsi_image
->row_pitch
= surface
->isl
.row_pitch
;
271 anv_FreeMemory(device_h
, memory_h
, pAllocator
);
274 anv_DestroyImage(device_h
, image_h
, pAllocator
);
279 anv_wsi_image_free(VkDevice device
,
280 const VkAllocationCallbacks
* pAllocator
,
281 struct wsi_image
*wsi_image
)
283 anv_DestroyImage(device
, wsi_image
->image
, pAllocator
);
285 anv_FreeMemory(device
, wsi_image
->memory
, pAllocator
);
288 static const struct wsi_image_fns anv_wsi_image_fns
= {
289 .create_wsi_image
= anv_wsi_image_create
,
290 .free_wsi_image
= anv_wsi_image_free
,
293 VkResult
anv_CreateSwapchainKHR(
295 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
296 const VkAllocationCallbacks
* pAllocator
,
297 VkSwapchainKHR
* pSwapchain
)
299 ANV_FROM_HANDLE(anv_device
, device
, _device
);
300 ICD_FROM_HANDLE(VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
301 struct wsi_interface
*iface
=
302 device
->instance
->physicalDevice
.wsi_device
.wsi
[surface
->platform
];
303 struct wsi_swapchain
*swapchain
;
304 const VkAllocationCallbacks
*alloc
;
309 alloc
= &device
->alloc
;
310 VkResult result
= iface
->create_swapchain(surface
, _device
,
311 &device
->instance
->physicalDevice
.wsi_device
,
312 device
->instance
->physicalDevice
.local_fd
,
314 alloc
, &anv_wsi_image_fns
,
316 if (result
!= VK_SUCCESS
)
319 swapchain
->alloc
= *alloc
;
321 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++)
322 swapchain
->fences
[i
] = VK_NULL_HANDLE
;
324 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
329 void anv_DestroySwapchainKHR(
331 VkSwapchainKHR _swapchain
,
332 const VkAllocationCallbacks
* pAllocator
)
334 ANV_FROM_HANDLE(anv_device
, device
, _device
);
335 ANV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
336 const VkAllocationCallbacks
*alloc
;
344 alloc
= &device
->alloc
;
345 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++) {
346 if (swapchain
->fences
[i
] != VK_NULL_HANDLE
)
347 anv_DestroyFence(_device
, swapchain
->fences
[i
], pAllocator
);
350 swapchain
->destroy(swapchain
, alloc
);
353 VkResult
anv_GetSwapchainImagesKHR(
355 VkSwapchainKHR _swapchain
,
356 uint32_t* pSwapchainImageCount
,
357 VkImage
* pSwapchainImages
)
359 ANV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
361 return swapchain
->get_images(swapchain
, pSwapchainImageCount
,
365 VkResult
anv_AcquireNextImageKHR(
367 VkSwapchainKHR _swapchain
,
369 VkSemaphore semaphore
,
371 uint32_t* pImageIndex
)
373 ANV_FROM_HANDLE(anv_device
, device
, _device
);
374 ANV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
375 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
377 VkResult result
= swapchain
->acquire_next_image(swapchain
, timeout
,
378 semaphore
, pImageIndex
);
380 /* Thanks to implicit sync, the image is ready immediately. However, we
381 * should wait for the current GPU state to finish.
384 anv_QueueSubmit(anv_queue_to_handle(&device
->queue
), 0, NULL
, _fence
);
389 VkResult
anv_QueuePresentKHR(
391 const VkPresentInfoKHR
* pPresentInfo
)
393 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
394 VkResult result
= VK_SUCCESS
;
396 const VkPresentRegionsKHR
*regions
=
397 vk_find_struct_const(pPresentInfo
->pNext
, PRESENT_REGIONS_KHR
);
399 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
400 ANV_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
401 VkResult item_result
;
403 const VkPresentRegionKHR
*region
= NULL
;
404 if (regions
&& regions
->pRegions
)
405 region
= ®ions
->pRegions
[i
];
407 assert(anv_device_from_handle(swapchain
->device
) == queue
->device
);
409 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
410 item_result
= anv_CreateFence(anv_device_to_handle(queue
->device
),
411 &(VkFenceCreateInfo
) {
412 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
414 }, &swapchain
->alloc
, &swapchain
->fences
[0]);
415 if (pPresentInfo
->pResults
!= NULL
)
416 pPresentInfo
->pResults
[i
] = item_result
;
417 result
= result
== VK_SUCCESS
? item_result
: result
;
418 if (item_result
!= VK_SUCCESS
)
421 anv_ResetFences(anv_device_to_handle(queue
->device
),
422 1, &swapchain
->fences
[0]);
425 anv_QueueSubmit(_queue
, 0, NULL
, swapchain
->fences
[0]);
427 item_result
= swapchain
->queue_present(swapchain
,
428 pPresentInfo
->pImageIndices
[i
],
430 /* TODO: What if one of them returns OUT_OF_DATE? */
431 if (pPresentInfo
->pResults
!= NULL
)
432 pPresentInfo
->pResults
[i
] = item_result
;
433 result
= result
== VK_SUCCESS
? item_result
: result
;
434 if (item_result
!= VK_SUCCESS
)
437 VkFence last
= swapchain
->fences
[2];
438 swapchain
->fences
[2] = swapchain
->fences
[1];
439 swapchain
->fences
[1] = swapchain
->fences
[0];
440 swapchain
->fences
[0] = last
;
442 if (last
!= VK_NULL_HANDLE
) {
443 anv_WaitForFences(anv_device_to_handle(queue
->device
),