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 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
79 vk_free2(&instance
->alloc
, pAllocator
, surface
);
82 VkResult
anv_GetPhysicalDeviceSurfaceSupportKHR(
83 VkPhysicalDevice physicalDevice
,
84 uint32_t queueFamilyIndex
,
85 VkSurfaceKHR _surface
,
88 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
89 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
90 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
92 return iface
->get_support(surface
, &device
->wsi_device
,
93 &device
->instance
->alloc
,
94 queueFamilyIndex
, pSupported
);
97 VkResult
anv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
98 VkPhysicalDevice physicalDevice
,
99 VkSurfaceKHR _surface
,
100 VkSurfaceCapabilitiesKHR
* pSurfaceCapabilities
)
102 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
103 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
104 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
106 return iface
->get_capabilities(surface
, pSurfaceCapabilities
);
109 VkResult
anv_GetPhysicalDeviceSurfaceFormatsKHR(
110 VkPhysicalDevice physicalDevice
,
111 VkSurfaceKHR _surface
,
112 uint32_t* pSurfaceFormatCount
,
113 VkSurfaceFormatKHR
* pSurfaceFormats
)
115 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
116 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
117 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
119 return iface
->get_formats(surface
, &device
->wsi_device
, pSurfaceFormatCount
,
123 VkResult
anv_GetPhysicalDeviceSurfacePresentModesKHR(
124 VkPhysicalDevice physicalDevice
,
125 VkSurfaceKHR _surface
,
126 uint32_t* pPresentModeCount
,
127 VkPresentModeKHR
* pPresentModes
)
129 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
130 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
131 struct wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
133 return iface
->get_present_modes(surface
, pPresentModeCount
,
139 x11_anv_wsi_image_create(VkDevice device_h
,
140 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
141 const VkAllocationCallbacks
* pAllocator
,
143 VkDeviceMemory
*memory_p
,
146 uint32_t *row_pitch
, int *fd_p
)
148 struct anv_device
*device
= anv_device_from_handle(device_h
);
150 struct anv_image
*image
;
153 result
= anv_image_create(anv_device_to_handle(device
),
154 &(struct anv_image_create_info
) {
155 .isl_tiling_flags
= ISL_TILING_X_BIT
,
158 &(VkImageCreateInfo
) {
159 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
160 .imageType
= VK_IMAGE_TYPE_2D
,
161 .format
= pCreateInfo
->imageFormat
,
163 .width
= pCreateInfo
->imageExtent
.width
,
164 .height
= pCreateInfo
->imageExtent
.height
,
170 /* FIXME: Need a way to use X tiling to allow scanout */
171 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
172 .usage
= (pCreateInfo
->imageUsage
|
173 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
),
178 if (result
!= VK_SUCCESS
)
181 image
= anv_image_from_handle(image_h
);
182 assert(vk_format_is_color(image
->vk_format
));
184 VkDeviceMemory memory_h
;
185 struct anv_device_memory
*memory
;
186 result
= anv_AllocateMemory(anv_device_to_handle(device
),
187 &(VkMemoryAllocateInfo
) {
188 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
189 .allocationSize
= image
->size
,
190 .memoryTypeIndex
= 0,
192 NULL
/* XXX: pAllocator */,
194 if (result
!= VK_SUCCESS
)
195 goto fail_create_image
;
197 memory
= anv_device_memory_from_handle(memory_h
);
198 memory
->bo
.is_winsys_bo
= true;
200 anv_BindImageMemory(VK_NULL_HANDLE
, image_h
, memory_h
, 0);
202 struct anv_surface
*surface
= &image
->color_surface
;
203 assert(surface
->isl
.tiling
== ISL_TILING_X
);
205 *row_pitch
= surface
->isl
.row_pitch
;
206 int ret
= anv_gem_set_tiling(device
, memory
->bo
.gem_handle
,
207 surface
->isl
.row_pitch
, I915_TILING_X
);
209 /* FINISHME: Choose a better error. */
210 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
211 "set_tiling failed: %m");
212 goto fail_alloc_memory
;
215 int fd
= anv_gem_handle_to_fd(device
, memory
->bo
.gem_handle
);
217 /* FINISHME: Choose a better error. */
218 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
219 "handle_to_fd failed: %m");
220 goto fail_alloc_memory
;
224 *memory_p
= memory_h
;
227 *offset
= image
->offset
;
230 anv_FreeMemory(device_h
, memory_h
, pAllocator
);
233 anv_DestroyImage(device_h
, image_h
, pAllocator
);
238 x11_anv_wsi_image_free(VkDevice device
,
239 const VkAllocationCallbacks
* pAllocator
,
241 VkDeviceMemory memory_h
)
243 anv_DestroyImage(device
, image_h
, pAllocator
);
245 anv_FreeMemory(device
, memory_h
, pAllocator
);
248 static const struct wsi_image_fns anv_wsi_image_fns
= {
249 .create_wsi_image
= x11_anv_wsi_image_create
,
250 .free_wsi_image
= x11_anv_wsi_image_free
,
253 VkResult
anv_CreateSwapchainKHR(
255 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
256 const VkAllocationCallbacks
* pAllocator
,
257 VkSwapchainKHR
* pSwapchain
)
259 ANV_FROM_HANDLE(anv_device
, device
, _device
);
260 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
261 struct wsi_interface
*iface
=
262 device
->instance
->physicalDevice
.wsi_device
.wsi
[surface
->platform
];
263 struct wsi_swapchain
*swapchain
;
264 const VkAllocationCallbacks
*alloc
;
269 alloc
= &device
->alloc
;
270 VkResult result
= iface
->create_swapchain(surface
, _device
,
271 &device
->instance
->physicalDevice
.wsi_device
,
273 alloc
, &anv_wsi_image_fns
,
275 if (result
!= VK_SUCCESS
)
278 swapchain
->alloc
= *alloc
;
280 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++)
281 swapchain
->fences
[i
] = VK_NULL_HANDLE
;
283 *pSwapchain
= wsi_swapchain_to_handle(swapchain
);
288 void anv_DestroySwapchainKHR(
290 VkSwapchainKHR _swapchain
,
291 const VkAllocationCallbacks
* pAllocator
)
293 ANV_FROM_HANDLE(anv_device
, device
, _device
);
294 ANV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
295 const VkAllocationCallbacks
*alloc
;
300 alloc
= &device
->alloc
;
301 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++) {
302 if (swapchain
->fences
[i
] != VK_NULL_HANDLE
)
303 anv_DestroyFence(_device
, swapchain
->fences
[i
], pAllocator
);
306 swapchain
->destroy(swapchain
, alloc
);
309 VkResult
anv_GetSwapchainImagesKHR(
311 VkSwapchainKHR _swapchain
,
312 uint32_t* pSwapchainImageCount
,
313 VkImage
* pSwapchainImages
)
315 ANV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
317 return swapchain
->get_images(swapchain
, pSwapchainImageCount
,
321 VkResult
anv_AcquireNextImageKHR(
323 VkSwapchainKHR _swapchain
,
325 VkSemaphore semaphore
,
327 uint32_t* pImageIndex
)
329 ANV_FROM_HANDLE(wsi_swapchain
, swapchain
, _swapchain
);
330 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
332 VkResult result
= swapchain
->acquire_next_image(swapchain
, timeout
,
333 semaphore
, pImageIndex
);
335 /* Thanks to implicit sync, the image is ready immediately. */
337 fence
->state
= ANV_FENCE_STATE_SIGNALED
;
342 VkResult
anv_QueuePresentKHR(
344 const VkPresentInfoKHR
* pPresentInfo
)
346 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
349 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
350 ANV_FROM_HANDLE(wsi_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
352 assert(anv_device_from_handle(swapchain
->device
) == queue
->device
);
354 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
355 result
= anv_CreateFence(anv_device_to_handle(queue
->device
),
356 &(VkFenceCreateInfo
) {
357 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
359 }, &swapchain
->alloc
, &swapchain
->fences
[0]);
360 if (result
!= VK_SUCCESS
)
363 anv_ResetFences(anv_device_to_handle(queue
->device
),
364 1, &swapchain
->fences
[0]);
367 anv_QueueSubmit(_queue
, 0, NULL
, swapchain
->fences
[0]);
369 result
= swapchain
->queue_present(swapchain
,
370 pPresentInfo
->pImageIndices
[i
]);
371 /* TODO: What if one of them returns OUT_OF_DATE? */
372 if (result
!= VK_SUCCESS
)
375 VkFence last
= swapchain
->fences
[2];
376 swapchain
->fences
[2] = swapchain
->fences
[1];
377 swapchain
->fences
[1] = swapchain
->fences
[0];
378 swapchain
->fences
[0] = last
;
380 if (last
!= VK_NULL_HANDLE
) {
381 anv_WaitForFences(anv_device_to_handle(queue
->device
),