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
25 #include "vk_format_info.h"
27 static const struct anv_wsi_callbacks anv_wsi_cbs
= {
28 .get_phys_device_format_properties
= anv_GetPhysicalDeviceFormatProperties
,
32 anv_init_wsi(struct anv_physical_device
*physical_device
)
36 memset(physical_device
->wsi_device
.wsi
, 0, sizeof(physical_device
->wsi_device
.wsi
));
38 #ifdef VK_USE_PLATFORM_XCB_KHR
39 result
= anv_x11_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
40 if (result
!= VK_SUCCESS
)
44 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
45 result
= anv_wl_init_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
,
46 anv_physical_device_to_handle(physical_device
),
48 if (result
!= VK_SUCCESS
) {
49 #ifdef VK_USE_PLATFORM_XCB_KHR
50 anv_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
60 anv_finish_wsi(struct anv_physical_device
*physical_device
)
62 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
63 anv_wl_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
65 #ifdef VK_USE_PLATFORM_XCB_KHR
66 anv_x11_finish_wsi(&physical_device
->wsi_device
, &physical_device
->instance
->alloc
);
70 void anv_DestroySurfaceKHR(
72 VkSurfaceKHR _surface
,
73 const VkAllocationCallbacks
* pAllocator
)
75 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
76 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
78 vk_free2(&instance
->alloc
, pAllocator
, surface
);
81 VkResult
anv_GetPhysicalDeviceSurfaceSupportKHR(
82 VkPhysicalDevice physicalDevice
,
83 uint32_t queueFamilyIndex
,
84 VkSurfaceKHR _surface
,
87 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
88 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
89 struct anv_wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
91 return iface
->get_support(surface
, &device
->wsi_device
,
92 &device
->instance
->alloc
,
93 queueFamilyIndex
, pSupported
);
96 VkResult
anv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
97 VkPhysicalDevice physicalDevice
,
98 VkSurfaceKHR _surface
,
99 VkSurfaceCapabilitiesKHR
* pSurfaceCapabilities
)
101 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
102 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
103 struct anv_wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
105 return iface
->get_capabilities(surface
, pSurfaceCapabilities
);
108 VkResult
anv_GetPhysicalDeviceSurfaceFormatsKHR(
109 VkPhysicalDevice physicalDevice
,
110 VkSurfaceKHR _surface
,
111 uint32_t* pSurfaceFormatCount
,
112 VkSurfaceFormatKHR
* pSurfaceFormats
)
114 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
115 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
116 struct anv_wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
118 return iface
->get_formats(surface
, &device
->wsi_device
, pSurfaceFormatCount
,
122 VkResult
anv_GetPhysicalDeviceSurfacePresentModesKHR(
123 VkPhysicalDevice physicalDevice
,
124 VkSurfaceKHR _surface
,
125 uint32_t* pPresentModeCount
,
126 VkPresentModeKHR
* pPresentModes
)
128 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
129 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, _surface
);
130 struct anv_wsi_interface
*iface
= device
->wsi_device
.wsi
[surface
->platform
];
132 return iface
->get_present_modes(surface
, pPresentModeCount
,
138 x11_anv_wsi_image_create(VkDevice device_h
,
139 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
140 const VkAllocationCallbacks
* pAllocator
,
142 VkDeviceMemory
*memory_p
,
145 uint32_t *row_pitch
, int *fd_p
)
147 struct anv_device
*device
= anv_device_from_handle(device_h
);
149 struct anv_image
*image
;
152 result
= anv_image_create(anv_device_to_handle(device
),
153 &(struct anv_image_create_info
) {
154 .isl_tiling_flags
= ISL_TILING_X_BIT
,
157 &(VkImageCreateInfo
) {
158 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
159 .imageType
= VK_IMAGE_TYPE_2D
,
160 .format
= pCreateInfo
->imageFormat
,
162 .width
= pCreateInfo
->imageExtent
.width
,
163 .height
= pCreateInfo
->imageExtent
.height
,
169 /* FIXME: Need a way to use X tiling to allow scanout */
170 .tiling
= VK_IMAGE_TILING_OPTIMAL
,
171 .usage
= (pCreateInfo
->imageUsage
|
172 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
),
177 if (result
!= VK_SUCCESS
)
180 image
= anv_image_from_handle(image_h
);
181 assert(vk_format_is_color(image
->vk_format
));
183 VkDeviceMemory memory_h
;
184 struct anv_device_memory
*memory
;
185 result
= anv_AllocateMemory(anv_device_to_handle(device
),
186 &(VkMemoryAllocateInfo
) {
187 .sType
= VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
,
188 .allocationSize
= image
->size
,
189 .memoryTypeIndex
= 0,
191 NULL
/* XXX: pAllocator */,
193 if (result
!= VK_SUCCESS
)
194 goto fail_create_image
;
196 memory
= anv_device_memory_from_handle(memory_h
);
197 memory
->bo
.is_winsys_bo
= true;
199 anv_BindImageMemory(VK_NULL_HANDLE
, image_h
, memory_h
, 0);
201 struct anv_surface
*surface
= &image
->color_surface
;
202 assert(surface
->isl
.tiling
== ISL_TILING_X
);
204 *row_pitch
= surface
->isl
.row_pitch
;
205 int ret
= anv_gem_set_tiling(device
, memory
->bo
.gem_handle
,
206 surface
->isl
.row_pitch
, I915_TILING_X
);
208 /* FINISHME: Choose a better error. */
209 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
210 "set_tiling failed: %m");
211 goto fail_alloc_memory
;
214 int fd
= anv_gem_handle_to_fd(device
, memory
->bo
.gem_handle
);
216 /* FINISHME: Choose a better error. */
217 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
218 "handle_to_fd failed: %m");
219 goto fail_alloc_memory
;
223 *memory_p
= memory_h
;
226 *offset
= image
->offset
;
229 anv_FreeMemory(device_h
, memory_h
, pAllocator
);
232 anv_DestroyImage(device_h
, image_h
, pAllocator
);
237 x11_anv_wsi_image_free(VkDevice device
,
238 const VkAllocationCallbacks
* pAllocator
,
240 VkDeviceMemory memory_h
)
242 anv_DestroyImage(device
, image_h
, pAllocator
);
244 anv_FreeMemory(device
, memory_h
, pAllocator
);
247 static const struct anv_wsi_image_fns anv_wsi_image_fns
= {
248 .create_wsi_image
= x11_anv_wsi_image_create
,
249 .free_wsi_image
= x11_anv_wsi_image_free
,
252 VkResult
anv_CreateSwapchainKHR(
254 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
255 const VkAllocationCallbacks
* pAllocator
,
256 VkSwapchainKHR
* pSwapchain
)
258 ANV_FROM_HANDLE(anv_device
, device
, _device
);
259 ANV_FROM_HANDLE(_VkIcdSurfaceBase
, surface
, pCreateInfo
->surface
);
260 struct anv_wsi_interface
*iface
=
261 device
->instance
->physicalDevice
.wsi_device
.wsi
[surface
->platform
];
262 struct anv_swapchain
*swapchain
;
263 const VkAllocationCallbacks
*alloc
;
268 alloc
= &device
->alloc
;
269 VkResult result
= iface
->create_swapchain(surface
, _device
,
270 &device
->instance
->physicalDevice
.wsi_device
,
272 alloc
, &anv_wsi_image_fns
,
274 if (result
!= VK_SUCCESS
)
277 swapchain
->alloc
= *alloc
;
279 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++)
280 swapchain
->fences
[i
] = VK_NULL_HANDLE
;
282 *pSwapchain
= anv_swapchain_to_handle(swapchain
);
287 void anv_DestroySwapchainKHR(
289 VkSwapchainKHR _swapchain
,
290 const VkAllocationCallbacks
* pAllocator
)
292 ANV_FROM_HANDLE(anv_device
, device
, _device
);
293 ANV_FROM_HANDLE(anv_swapchain
, swapchain
, _swapchain
);
294 const VkAllocationCallbacks
*alloc
;
299 alloc
= &device
->alloc
;
300 for (unsigned i
= 0; i
< ARRAY_SIZE(swapchain
->fences
); i
++) {
301 if (swapchain
->fences
[i
] != VK_NULL_HANDLE
)
302 anv_DestroyFence(_device
, swapchain
->fences
[i
], pAllocator
);
305 swapchain
->destroy(swapchain
, alloc
);
308 VkResult
anv_GetSwapchainImagesKHR(
310 VkSwapchainKHR _swapchain
,
311 uint32_t* pSwapchainImageCount
,
312 VkImage
* pSwapchainImages
)
314 ANV_FROM_HANDLE(anv_swapchain
, swapchain
, _swapchain
);
316 return swapchain
->get_images(swapchain
, pSwapchainImageCount
,
320 VkResult
anv_AcquireNextImageKHR(
322 VkSwapchainKHR _swapchain
,
324 VkSemaphore semaphore
,
326 uint32_t* pImageIndex
)
328 ANV_FROM_HANDLE(anv_swapchain
, swapchain
, _swapchain
);
330 return swapchain
->acquire_next_image(swapchain
, timeout
, semaphore
,
334 VkResult
anv_QueuePresentKHR(
336 const VkPresentInfoKHR
* pPresentInfo
)
338 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
341 for (uint32_t i
= 0; i
< pPresentInfo
->swapchainCount
; i
++) {
342 ANV_FROM_HANDLE(anv_swapchain
, swapchain
, pPresentInfo
->pSwapchains
[i
]);
344 assert(anv_device_from_handle(swapchain
->device
) == queue
->device
);
346 if (swapchain
->fences
[0] == VK_NULL_HANDLE
) {
347 result
= anv_CreateFence(anv_device_to_handle(queue
->device
),
348 &(VkFenceCreateInfo
) {
349 .sType
= VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
,
351 }, &swapchain
->alloc
, &swapchain
->fences
[0]);
352 if (result
!= VK_SUCCESS
)
355 anv_ResetFences(anv_device_to_handle(queue
->device
),
356 1, &swapchain
->fences
[0]);
359 anv_QueueSubmit(_queue
, 0, NULL
, swapchain
->fences
[0]);
361 result
= swapchain
->queue_present(swapchain
,
362 pPresentInfo
->pImageIndices
[i
]);
363 /* TODO: What if one of them returns OUT_OF_DATE? */
364 if (result
!= VK_SUCCESS
)
367 VkFence last
= swapchain
->fences
[2];
368 swapchain
->fences
[2] = swapchain
->fences
[1];
369 swapchain
->fences
[1] = swapchain
->fences
[0];
370 swapchain
->fences
[0] = last
;
372 if (last
!= VK_NULL_HANDLE
) {
373 anv_WaitForFences(anv_device_to_handle(queue
->device
),