1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2016 Red Hat
3b8e80941Smrg * based on intel anv code:
4b8e80941Smrg * Copyright © 2015 Intel Corporation
5b8e80941Smrg *
6b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
8b8e80941Smrg * to deal in the Software without restriction, including without limitation
9b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
11b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
12b8e80941Smrg *
13b8e80941Smrg * The above copyright notice and this permission notice (including the next
14b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
15b8e80941Smrg * Software.
16b8e80941Smrg *
17b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23b8e80941Smrg * IN THE SOFTWARE.
24b8e80941Smrg */
25b8e80941Smrg
26b8e80941Smrg#include "radv_private.h"
27b8e80941Smrg#include "radv_meta.h"
28b8e80941Smrg#include "wsi_common.h"
29b8e80941Smrg#include "vk_util.h"
30b8e80941Smrg#include "util/macros.h"
31b8e80941Smrg
32b8e80941Smrgstatic PFN_vkVoidFunction
33b8e80941Smrgradv_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
34b8e80941Smrg{
35b8e80941Smrg	return radv_lookup_entrypoint_unchecked(pName);
36b8e80941Smrg}
37b8e80941Smrg
38b8e80941SmrgVkResult
39b8e80941Smrgradv_init_wsi(struct radv_physical_device *physical_device)
40b8e80941Smrg{
41b8e80941Smrg	return wsi_device_init(&physical_device->wsi_device,
42b8e80941Smrg			       radv_physical_device_to_handle(physical_device),
43b8e80941Smrg			       radv_wsi_proc_addr,
44b8e80941Smrg			       &physical_device->instance->alloc,
45b8e80941Smrg			       physical_device->master_fd,
46b8e80941Smrg			       &physical_device->instance->dri_options);
47b8e80941Smrg}
48b8e80941Smrg
49b8e80941Smrgvoid
50b8e80941Smrgradv_finish_wsi(struct radv_physical_device *physical_device)
51b8e80941Smrg{
52b8e80941Smrg	wsi_device_finish(&physical_device->wsi_device,
53b8e80941Smrg			  &physical_device->instance->alloc);
54b8e80941Smrg}
55b8e80941Smrg
56b8e80941Smrgvoid radv_DestroySurfaceKHR(
57b8e80941Smrg	VkInstance                                   _instance,
58b8e80941Smrg	VkSurfaceKHR                                 _surface,
59b8e80941Smrg	const VkAllocationCallbacks*                 pAllocator)
60b8e80941Smrg{
61b8e80941Smrg	RADV_FROM_HANDLE(radv_instance, instance, _instance);
62b8e80941Smrg	ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
63b8e80941Smrg
64b8e80941Smrg	vk_free2(&instance->alloc, pAllocator, surface);
65b8e80941Smrg}
66b8e80941Smrg
67b8e80941SmrgVkResult radv_GetPhysicalDeviceSurfaceSupportKHR(
68b8e80941Smrg	VkPhysicalDevice                            physicalDevice,
69b8e80941Smrg	uint32_t                                    queueFamilyIndex,
70b8e80941Smrg	VkSurfaceKHR                                surface,
71b8e80941Smrg	VkBool32*                                   pSupported)
72b8e80941Smrg{
73b8e80941Smrg	RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
74b8e80941Smrg
75b8e80941Smrg	return wsi_common_get_surface_support(&device->wsi_device,
76b8e80941Smrg					      queueFamilyIndex,
77b8e80941Smrg					      surface,
78b8e80941Smrg					      pSupported);
79b8e80941Smrg}
80b8e80941Smrg
81b8e80941SmrgVkResult radv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
82b8e80941Smrg	VkPhysicalDevice                            physicalDevice,
83b8e80941Smrg	VkSurfaceKHR                                surface,
84b8e80941Smrg	VkSurfaceCapabilitiesKHR*                   pSurfaceCapabilities)
85b8e80941Smrg{
86b8e80941Smrg	RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
87b8e80941Smrg
88b8e80941Smrg	return wsi_common_get_surface_capabilities(&device->wsi_device,
89b8e80941Smrg						   surface,
90b8e80941Smrg						   pSurfaceCapabilities);
91b8e80941Smrg}
92b8e80941Smrg
93b8e80941SmrgVkResult radv_GetPhysicalDeviceSurfaceCapabilities2KHR(
94b8e80941Smrg	VkPhysicalDevice                            physicalDevice,
95b8e80941Smrg	const VkPhysicalDeviceSurfaceInfo2KHR*      pSurfaceInfo,
96b8e80941Smrg	VkSurfaceCapabilities2KHR*                  pSurfaceCapabilities)
97b8e80941Smrg{
98b8e80941Smrg	RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
99b8e80941Smrg
100b8e80941Smrg	return wsi_common_get_surface_capabilities2(&device->wsi_device,
101b8e80941Smrg						    pSurfaceInfo,
102b8e80941Smrg						    pSurfaceCapabilities);
103b8e80941Smrg}
104b8e80941Smrg
105b8e80941SmrgVkResult radv_GetPhysicalDeviceSurfaceCapabilities2EXT(
106b8e80941Smrg 	VkPhysicalDevice                            physicalDevice,
107b8e80941Smrg	VkSurfaceKHR                                surface,
108b8e80941Smrg	VkSurfaceCapabilities2EXT*                  pSurfaceCapabilities)
109b8e80941Smrg{
110b8e80941Smrg	RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
111b8e80941Smrg
112b8e80941Smrg	return wsi_common_get_surface_capabilities2ext(&device->wsi_device,
113b8e80941Smrg						       surface,
114b8e80941Smrg						       pSurfaceCapabilities);
115b8e80941Smrg}
116b8e80941Smrg
117b8e80941SmrgVkResult radv_GetPhysicalDeviceSurfaceFormatsKHR(
118b8e80941Smrg	VkPhysicalDevice                            physicalDevice,
119b8e80941Smrg	VkSurfaceKHR                                surface,
120b8e80941Smrg	uint32_t*                                   pSurfaceFormatCount,
121b8e80941Smrg	VkSurfaceFormatKHR*                         pSurfaceFormats)
122b8e80941Smrg{
123b8e80941Smrg	RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
124b8e80941Smrg
125b8e80941Smrg	return wsi_common_get_surface_formats(&device->wsi_device,
126b8e80941Smrg					      surface,
127b8e80941Smrg					      pSurfaceFormatCount,
128b8e80941Smrg					      pSurfaceFormats);
129b8e80941Smrg}
130b8e80941Smrg
131b8e80941SmrgVkResult radv_GetPhysicalDeviceSurfaceFormats2KHR(
132b8e80941Smrg	VkPhysicalDevice                            physicalDevice,
133b8e80941Smrg	const VkPhysicalDeviceSurfaceInfo2KHR*      pSurfaceInfo,
134b8e80941Smrg	uint32_t*                                   pSurfaceFormatCount,
135b8e80941Smrg	VkSurfaceFormat2KHR*                        pSurfaceFormats)
136b8e80941Smrg{
137b8e80941Smrg	RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
138b8e80941Smrg
139b8e80941Smrg	return wsi_common_get_surface_formats2(&device->wsi_device,
140b8e80941Smrg					       pSurfaceInfo,
141b8e80941Smrg					       pSurfaceFormatCount,
142b8e80941Smrg					       pSurfaceFormats);
143b8e80941Smrg}
144b8e80941Smrg
145b8e80941SmrgVkResult radv_GetPhysicalDeviceSurfacePresentModesKHR(
146b8e80941Smrg	VkPhysicalDevice                            physicalDevice,
147b8e80941Smrg	VkSurfaceKHR                                surface,
148b8e80941Smrg	uint32_t*                                   pPresentModeCount,
149b8e80941Smrg	VkPresentModeKHR*                           pPresentModes)
150b8e80941Smrg{
151b8e80941Smrg	RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
152b8e80941Smrg
153b8e80941Smrg	return wsi_common_get_surface_present_modes(&device->wsi_device,
154b8e80941Smrg						    surface,
155b8e80941Smrg						    pPresentModeCount,
156b8e80941Smrg						    pPresentModes);
157b8e80941Smrg}
158b8e80941Smrg
159b8e80941SmrgVkResult radv_CreateSwapchainKHR(
160b8e80941Smrg	VkDevice                                     _device,
161b8e80941Smrg	const VkSwapchainCreateInfoKHR*              pCreateInfo,
162b8e80941Smrg	const VkAllocationCallbacks*                 pAllocator,
163b8e80941Smrg	VkSwapchainKHR*                              pSwapchain)
164b8e80941Smrg{
165b8e80941Smrg	RADV_FROM_HANDLE(radv_device, device, _device);
166b8e80941Smrg	const VkAllocationCallbacks *alloc;
167b8e80941Smrg	if (pAllocator)
168b8e80941Smrg		alloc = pAllocator;
169b8e80941Smrg	else
170b8e80941Smrg		alloc = &device->alloc;
171b8e80941Smrg
172b8e80941Smrg	return wsi_common_create_swapchain(&device->physical_device->wsi_device,
173b8e80941Smrg					   radv_device_to_handle(device),
174b8e80941Smrg					   pCreateInfo,
175b8e80941Smrg					   alloc,
176b8e80941Smrg					   pSwapchain);
177b8e80941Smrg}
178b8e80941Smrg
179b8e80941Smrgvoid radv_DestroySwapchainKHR(
180b8e80941Smrg	VkDevice                                     _device,
181b8e80941Smrg	VkSwapchainKHR                               swapchain,
182b8e80941Smrg	const VkAllocationCallbacks*                 pAllocator)
183b8e80941Smrg{
184b8e80941Smrg	RADV_FROM_HANDLE(radv_device, device, _device);
185b8e80941Smrg	const VkAllocationCallbacks *alloc;
186b8e80941Smrg
187b8e80941Smrg	if (pAllocator)
188b8e80941Smrg		alloc = pAllocator;
189b8e80941Smrg	else
190b8e80941Smrg		alloc = &device->alloc;
191b8e80941Smrg
192b8e80941Smrg	wsi_common_destroy_swapchain(_device, swapchain, alloc);
193b8e80941Smrg}
194b8e80941Smrg
195b8e80941SmrgVkResult radv_GetSwapchainImagesKHR(
196b8e80941Smrg	VkDevice                                     device,
197b8e80941Smrg	VkSwapchainKHR                               swapchain,
198b8e80941Smrg	uint32_t*                                    pSwapchainImageCount,
199b8e80941Smrg	VkImage*                                     pSwapchainImages)
200b8e80941Smrg{
201b8e80941Smrg	return wsi_common_get_images(swapchain,
202b8e80941Smrg				     pSwapchainImageCount,
203b8e80941Smrg				     pSwapchainImages);
204b8e80941Smrg}
205b8e80941Smrg
206b8e80941SmrgVkResult radv_AcquireNextImageKHR(
207b8e80941Smrg	VkDevice                                     device,
208b8e80941Smrg	VkSwapchainKHR                               swapchain,
209b8e80941Smrg	uint64_t                                     timeout,
210b8e80941Smrg	VkSemaphore                                  semaphore,
211b8e80941Smrg	VkFence                                      fence,
212b8e80941Smrg	uint32_t*                                    pImageIndex)
213b8e80941Smrg{
214b8e80941Smrg	VkAcquireNextImageInfoKHR acquire_info = {
215b8e80941Smrg		.sType = VK_STRUCTURE_TYPE_ACQUIRE_NEXT_IMAGE_INFO_KHR,
216b8e80941Smrg		.swapchain = swapchain,
217b8e80941Smrg		.timeout = timeout,
218b8e80941Smrg		.semaphore = semaphore,
219b8e80941Smrg		.fence = fence,
220b8e80941Smrg		.deviceMask = 0,
221b8e80941Smrg	};
222b8e80941Smrg
223b8e80941Smrg	return radv_AcquireNextImage2KHR(device, &acquire_info, pImageIndex);
224b8e80941Smrg}
225b8e80941Smrg
226b8e80941SmrgVkResult radv_AcquireNextImage2KHR(
227b8e80941Smrg	VkDevice                                     _device,
228b8e80941Smrg	const VkAcquireNextImageInfoKHR*             pAcquireInfo,
229b8e80941Smrg	uint32_t*                                    pImageIndex)
230b8e80941Smrg{
231b8e80941Smrg	RADV_FROM_HANDLE(radv_device, device, _device);
232b8e80941Smrg	struct radv_physical_device *pdevice = device->physical_device;
233b8e80941Smrg	RADV_FROM_HANDLE(radv_fence, fence, pAcquireInfo->fence);
234b8e80941Smrg
235b8e80941Smrg	VkResult result = wsi_common_acquire_next_image2(&pdevice->wsi_device,
236b8e80941Smrg							 _device,
237b8e80941Smrg                                                         pAcquireInfo,
238b8e80941Smrg							 pImageIndex);
239b8e80941Smrg
240b8e80941Smrg	if (fence && (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR)) {
241b8e80941Smrg		fence->submitted = true;
242b8e80941Smrg		fence->signalled = true;
243b8e80941Smrg		if (fence->temp_syncobj) {
244b8e80941Smrg			device->ws->signal_syncobj(device->ws, fence->temp_syncobj);
245b8e80941Smrg		} else if (fence->syncobj) {
246b8e80941Smrg			device->ws->signal_syncobj(device->ws, fence->syncobj);
247b8e80941Smrg		}
248b8e80941Smrg	}
249b8e80941Smrg	return result;
250b8e80941Smrg}
251b8e80941Smrg
252b8e80941SmrgVkResult radv_QueuePresentKHR(
253b8e80941Smrg	VkQueue                                  _queue,
254b8e80941Smrg	const VkPresentInfoKHR*                  pPresentInfo)
255b8e80941Smrg{
256b8e80941Smrg	RADV_FROM_HANDLE(radv_queue, queue, _queue);
257b8e80941Smrg	return wsi_common_queue_present(&queue->device->physical_device->wsi_device,
258b8e80941Smrg					radv_device_to_handle(queue->device),
259b8e80941Smrg					_queue,
260b8e80941Smrg					queue->queue_family_index,
261b8e80941Smrg					pPresentInfo);
262b8e80941Smrg}
263b8e80941Smrg
264b8e80941Smrg
265b8e80941SmrgVkResult radv_GetDeviceGroupPresentCapabilitiesKHR(
266b8e80941Smrg    VkDevice                                    device,
267b8e80941Smrg    VkDeviceGroupPresentCapabilitiesKHR*        pCapabilities)
268b8e80941Smrg{
269b8e80941Smrg   memset(pCapabilities->presentMask, 0,
270b8e80941Smrg          sizeof(pCapabilities->presentMask));
271b8e80941Smrg   pCapabilities->presentMask[0] = 0x1;
272b8e80941Smrg   pCapabilities->modes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
273b8e80941Smrg
274b8e80941Smrg   return VK_SUCCESS;
275b8e80941Smrg}
276b8e80941Smrg
277b8e80941SmrgVkResult radv_GetDeviceGroupSurfacePresentModesKHR(
278b8e80941Smrg    VkDevice                                    device,
279b8e80941Smrg    VkSurfaceKHR                                surface,
280b8e80941Smrg    VkDeviceGroupPresentModeFlagsKHR*           pModes)
281b8e80941Smrg{
282b8e80941Smrg   *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
283b8e80941Smrg
284b8e80941Smrg   return VK_SUCCESS;
285b8e80941Smrg}
286b8e80941Smrg
287b8e80941SmrgVkResult radv_GetPhysicalDevicePresentRectanglesKHR(
288b8e80941Smrg	VkPhysicalDevice                            physicalDevice,
289b8e80941Smrg	VkSurfaceKHR                                surface,
290b8e80941Smrg	uint32_t*                                   pRectCount,
291b8e80941Smrg	VkRect2D*                                   pRects)
292b8e80941Smrg{
293b8e80941Smrg	RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
294b8e80941Smrg
295b8e80941Smrg	return wsi_common_get_present_rectangles(&device->wsi_device,
296b8e80941Smrg						 surface,
297b8e80941Smrg						 pRectCount, pRects);
298b8e80941Smrg}
299