1/*
2 * Copyright © 2015 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23#ifndef WSI_COMMON_H
24#define WSI_COMMON_H
25
26#include <stdint.h>
27#include <stdbool.h>
28
29#include "vk_alloc.h"
30#include <vulkan/vulkan.h>
31#include <vulkan/vk_icd.h>
32
33/* This is guaranteed to not collide with anything because it's in the
34 * VK_KHR_swapchain namespace but not actually used by the extension.
35 */
36#define VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA (VkStructureType)1000001002
37#define VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA (VkStructureType)1000001003
38#define VK_STRUCTURE_TYPE_WSI_FORMAT_MODIFIER_PROPERTIES_LIST_MESA (VkStructureType)1000001004
39#define VK_STRUCTURE_TYPE_WSI_SURFACE_SUPPORTED_COUNTERS_MESA (VkStructureType)1000001005
40
41struct wsi_image_create_info {
42    VkStructureType sType;
43    const void *pNext;
44    bool scanout;
45
46    uint32_t modifier_count;
47    const uint64_t *modifiers;
48};
49
50struct wsi_memory_allocate_info {
51    VkStructureType sType;
52    const void *pNext;
53    bool implicit_sync;
54};
55
56struct wsi_format_modifier_properties {
57   uint64_t modifier;
58   uint32_t modifier_plane_count;
59};
60
61/* Chain in for vkGetPhysicalDeviceFormatProperties2KHR */
62struct wsi_format_modifier_properties_list {
63   VkStructureType sType;
64   const void *pNext;
65
66   uint32_t modifier_count;
67   struct wsi_format_modifier_properties *modifier_properties;
68};
69
70/* To be chained into VkSurfaceCapabilities2KHR */
71struct wsi_surface_supported_counters {
72   VkStructureType sType;
73   const void *pNext;
74
75   VkSurfaceCounterFlagsEXT supported_surface_counters;
76
77};
78
79struct wsi_fence {
80   VkDevice                     device;
81   const struct wsi_device      *wsi_device;
82   VkDisplayKHR                 display;
83   const VkAllocationCallbacks  *alloc;
84   VkResult                     (*wait)(struct wsi_fence *fence, uint64_t abs_timeout);
85   void                         (*destroy)(struct wsi_fence *fence);
86};
87
88struct wsi_interface;
89
90struct driOptionCache;
91
92#define VK_ICD_WSI_PLATFORM_MAX (VK_ICD_WSI_PLATFORM_DISPLAY + 1)
93
94struct wsi_device {
95   /* Allocator for the instance */
96   VkAllocationCallbacks instance_alloc;
97
98   VkPhysicalDevice pdevice;
99   VkPhysicalDeviceMemoryProperties memory_props;
100   uint32_t queue_family_count;
101
102   VkPhysicalDevicePCIBusInfoPropertiesEXT pci_bus_info;
103
104   bool supports_modifiers;
105   uint32_t maxImageDimension2D;
106   VkPresentModeKHR override_present_mode;
107
108   /* Whether to enable adaptive sync for a swapchain if implemented and
109    * available. Not all window systems might support this. */
110   bool enable_adaptive_sync;
111
112   struct {
113      /* Override the minimum number of images on the swapchain.
114       * 0 = no override */
115      uint32_t override_minImageCount;
116   } x11;
117
118   uint64_t (*image_get_modifier)(VkImage image);
119
120#define WSI_CB(cb) PFN_vk##cb cb
121   WSI_CB(AllocateMemory);
122   WSI_CB(AllocateCommandBuffers);
123   WSI_CB(BindBufferMemory);
124   WSI_CB(BindImageMemory);
125   WSI_CB(BeginCommandBuffer);
126   WSI_CB(CmdCopyImageToBuffer);
127   WSI_CB(CreateBuffer);
128   WSI_CB(CreateCommandPool);
129   WSI_CB(CreateFence);
130   WSI_CB(CreateImage);
131   WSI_CB(DestroyBuffer);
132   WSI_CB(DestroyCommandPool);
133   WSI_CB(DestroyFence);
134   WSI_CB(DestroyImage);
135   WSI_CB(EndCommandBuffer);
136   WSI_CB(FreeMemory);
137   WSI_CB(FreeCommandBuffers);
138   WSI_CB(GetBufferMemoryRequirements);
139   WSI_CB(GetImageMemoryRequirements);
140   WSI_CB(GetImageSubresourceLayout);
141   WSI_CB(GetMemoryFdKHR);
142   WSI_CB(GetPhysicalDeviceFormatProperties);
143   WSI_CB(GetPhysicalDeviceFormatProperties2KHR);
144   WSI_CB(ResetFences);
145   WSI_CB(QueueSubmit);
146   WSI_CB(WaitForFences);
147#undef WSI_CB
148
149    struct wsi_interface *                  wsi[VK_ICD_WSI_PLATFORM_MAX];
150};
151
152typedef PFN_vkVoidFunction (VKAPI_PTR *WSI_FN_GetPhysicalDeviceProcAddr)(VkPhysicalDevice physicalDevice, const char* pName);
153
154VkResult
155wsi_device_init(struct wsi_device *wsi,
156                VkPhysicalDevice pdevice,
157                WSI_FN_GetPhysicalDeviceProcAddr proc_addr,
158                const VkAllocationCallbacks *alloc,
159                int display_fd,
160                const struct driOptionCache *dri_options);
161
162void
163wsi_device_finish(struct wsi_device *wsi,
164                  const VkAllocationCallbacks *alloc);
165
166#define ICD_DEFINE_NONDISP_HANDLE_CASTS(__VkIcdType, __VkType)             \
167                                                                           \
168   static inline __VkIcdType *                                             \
169   __VkIcdType ## _from_handle(__VkType _handle)                           \
170   {                                                                       \
171      return (__VkIcdType *)(uintptr_t) _handle;                           \
172   }                                                                       \
173                                                                           \
174   static inline __VkType                                                  \
175   __VkIcdType ## _to_handle(__VkIcdType *_obj)                            \
176   {                                                                       \
177      return (__VkType)(uintptr_t) _obj;                                   \
178   }
179
180#define ICD_FROM_HANDLE(__VkIcdType, __name, __handle) \
181   __VkIcdType *__name = __VkIcdType ## _from_handle(__handle)
182
183ICD_DEFINE_NONDISP_HANDLE_CASTS(VkIcdSurfaceBase, VkSurfaceKHR)
184
185VkResult
186wsi_common_get_surface_support(struct wsi_device *wsi_device,
187                               uint32_t queueFamilyIndex,
188                               VkSurfaceKHR surface,
189                               VkBool32* pSupported);
190
191VkResult
192wsi_common_get_surface_capabilities(struct wsi_device *wsi_device,
193                                    VkSurfaceKHR surface,
194                                    VkSurfaceCapabilitiesKHR *pSurfaceCapabilities);
195
196VkResult
197wsi_common_get_surface_capabilities2(struct wsi_device *wsi_device,
198                                     const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
199                                     VkSurfaceCapabilities2KHR *pSurfaceCapabilities);
200
201VkResult
202wsi_common_get_surface_formats(struct wsi_device *wsi_device,
203                               VkSurfaceKHR surface,
204                               uint32_t *pSurfaceFormatCount,
205                               VkSurfaceFormatKHR *pSurfaceFormats);
206
207VkResult
208wsi_common_get_surface_formats2(struct wsi_device *wsi_device,
209                                const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
210                                uint32_t *pSurfaceFormatCount,
211                                VkSurfaceFormat2KHR *pSurfaceFormats);
212
213VkResult
214wsi_common_get_surface_present_modes(struct wsi_device *wsi_device,
215                                     VkSurfaceKHR surface,
216                                     uint32_t *pPresentModeCount,
217                                     VkPresentModeKHR *pPresentModes);
218
219VkResult
220wsi_common_get_present_rectangles(struct wsi_device *wsi,
221                                  VkSurfaceKHR surface,
222                                  uint32_t* pRectCount,
223                                  VkRect2D* pRects);
224
225VkResult
226wsi_common_get_surface_capabilities2ext(
227   struct wsi_device *wsi_device,
228   VkSurfaceKHR surface,
229   VkSurfaceCapabilities2EXT *pSurfaceCapabilities);
230
231VkResult
232wsi_common_get_images(VkSwapchainKHR _swapchain,
233                      uint32_t *pSwapchainImageCount,
234                      VkImage *pSwapchainImages);
235
236VkResult
237wsi_common_acquire_next_image2(const struct wsi_device *wsi,
238                               VkDevice device,
239                               const VkAcquireNextImageInfoKHR *pAcquireInfo,
240                               uint32_t *pImageIndex);
241
242VkResult
243wsi_common_create_swapchain(struct wsi_device *wsi,
244                            VkDevice device,
245                            const VkSwapchainCreateInfoKHR *pCreateInfo,
246                            const VkAllocationCallbacks *pAllocator,
247                            VkSwapchainKHR *pSwapchain);
248void
249wsi_common_destroy_swapchain(VkDevice device,
250                             VkSwapchainKHR swapchain,
251                             const VkAllocationCallbacks *pAllocator);
252
253VkResult
254wsi_common_queue_present(const struct wsi_device *wsi,
255                         VkDevice device_h,
256                         VkQueue queue_h,
257                         int queue_family_index,
258                         const VkPresentInfoKHR *pPresentInfo);
259
260uint64_t
261wsi_common_get_current_time(void);
262
263#endif
264