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