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