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
, device
->local_fd
, false, 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
,
148 VkDeviceMemory
*memory_p
,
151 uint32_t *row_pitch
, int *fd_p
)
153 struct anv_device
*device
= anv_device_from_handle(device_h
);
155 struct anv_image
*image
;
158 result
= anv_image_create(anv_device_to_handle(device
),
159 &(struct anv_image_create_info
) {
160 .isl_tiling_flags
= ISL_TILING_X_BIT
,
163 &(VkImageCreateInfo
) {
164 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
165 .imageType
= VK_IMAGE_TYPE_2D
,
166 .format
= pCreateInfo
->imageFormat
,
168 .width
= pCreateInfo
->imageExtent
.width
,
169 .height
= pCreateInfo
->imageExtent
.height
,
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
),
183 if (result
!= VK_SUCCESS
)
186 image
= anv_image_from_handle(image_h
);
187 assert(vk_format_is_color(image
->vk_format
));
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,
197 NULL
/* XXX: pAllocator */,
199 if (result
!= VK_SUCCESS
)
200 goto fail_create_image
;
202 memory
= anv_device_memory_from_handle(memory_h
);
203 memory
->bo
.is_winsys_bo
= true;
205 anv_BindImageMemory(device_h
, image_h
, memory_h
, 0);
207 struct anv_surface
*surface
= &image
->color_surface
;
208 assert(surface
->isl
.tiling
== ISL_TILING_X
);
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
);
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
;
220 int fd
= anv_gem_handle_to_fd(device
, memory
->bo
.gem_handle
);
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
;
229 *memory_p
= memory_h
;
232 *offset
= image
->offset
;
235 anv_FreeMemory(device_h
, memory_h
, pAllocator
);
238 anv_DestroyImage(device_h
, image_h
, pAllocator
);
243 x11_anv_wsi_image_free(VkDevice device
,
244 const VkAllocationCallbacks
* pAllocator
,
246 VkDeviceMemory memory_h
)
248 anv_DestroyImage(device
, image_h
, pAllocator
);
250 anv_FreeMemory(device
, memory_h
, pAllocator
);
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
,
258 VkResult
anv_CreateSwapchainKHR(
260 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
261 const VkAllocationCallbacks
* pAllocator
,
262 VkSwapchainKHR
* pSwapchain
)
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
;
274 alloc
= &device
->alloc
;
275 VkResult result
= iface
->create_swapchain(surface
, _device
,
276 &device
->instance
->physicalDevice
.wsi_device
,
277 device
->instance
->physicalDevice
.local_fd
,
279 alloc
, &anv_wsi_image_fns
,
281 if (result
!= VK_SUCCESS
)
284 swapchain
->alloc
= *alloc
;
286 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++)
287 swapchain
->fences
[i
] = VK_NULL_HANDLE
;
289 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
294 void anv_DestroySwapchainKHR(
296 VkSwapchainKHR _swapchain
,
297 const VkAllocationCallbacks
* pAllocator
)
299 ANV_FROM_HANDLE(anv_device
, device
, _device
);
300 ANV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
301 const VkAllocationCallbacks
*alloc
;
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
);
315 swapchain
->destroy(swapchain
, alloc
);
318 VkResult
anv_GetSwapchainImagesKHR(
320 VkSwapchainKHR _swapchain
,
321 uint32_t* pSwapchainImageCount
,
322 VkImage
* pSwapchainImages
)
324 ANV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
326 return swapchain
->get_images(swapchain
, pSwapchainImageCount
,
330 VkResult
anv_AcquireNextImageKHR(
332 VkSwapchainKHR _swapchain
,
334 VkSemaphore semaphore
,
336 uint32_t* pImageIndex
)
338 ANV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
339 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
341 VkResult result
= swapchain
->acquire_next_image(swapchain
, timeout
,
342 semaphore
, pImageIndex
);
344 /* Thanks to implicit sync, the image is ready immediately. */
346 fence
->state
= ANV_FENCE_STATE_SIGNALED
;
351 VkResult
anv_QueuePresentKHR(
353 const VkPresentInfoKHR
* pPresentInfo
)
355 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
356 VkResult result
= VK_SUCCESS
;
358 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
359 ANV_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
360 VkResult item_result
;
362 assert(anv_device_from_handle(swapchain
->device
) == queue
->device
);
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
,
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
)
376 anv_ResetFences(anv_device_to_handle(queue
->device
),
377 1, &swapchain
->fences
[0]);
380 anv_QueueSubmit(_queue
, 0, NULL
, swapchain
->fences
[0]);
382 item_result
= swapchain
->queue_present(swapchain
,
383 pPresentInfo
->pImageIndices
[i
],
385 /* TODO: What if one of them returns OUT_OF_DATE? */
386 if (pPresentInfo
->pResults
!= NULL
)
387 pPresentInfo
->pResults
[i
] = item_result
;
388 result
= result
== VK_SUCCESS
? item_result
: result
;
389 if (item_result
!= VK_SUCCESS
)
392 VkFence last
= swapchain
->fences
[2];
393 swapchain
->fences
[2] = swapchain
->fences
[1];
394 swapchain
->fences
[1] = swapchain
->fences
[0];
395 swapchain
->fences
[0] = last
;
397 if (last
!= VK_NULL_HANDLE
) {
398 anv_WaitForFences(anv_device_to_handle(queue
->device
),