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_SURFACE_SUPPORTED_COUNTERS_MESA (VkStructureType)1000001005
39 #define VK_STRUCTURE_TYPE_WSI_MEMORY_SIGNAL_SUBMIT_INFO_MESA (VkStructureType)1000001006
40 
41 struct wsi_image_create_info {
42     VkStructureType sType;
43     const void *pNext;
44     bool scanout;
45 };
46 
47 struct wsi_memory_allocate_info {
48     VkStructureType sType;
49     const void *pNext;
50     bool implicit_sync;
51 };
52 
53 /* To be chained into VkSurfaceCapabilities2KHR */
54 struct wsi_surface_supported_counters {
55    VkStructureType sType;
56    const void *pNext;
57 
58    VkSurfaceCounterFlagsEXT supported_surface_counters;
59 
60 };
61 
62 /* To be chained into VkSubmitInfo */
63 struct wsi_memory_signal_submit_info {
64     VkStructureType sType;
65     const void *pNext;
66     VkDeviceMemory memory;
67 };
68 
69 struct wsi_fence {
70    VkDevice                     device;
71    const struct wsi_device      *wsi_device;
72    VkDisplayKHR                 display;
73    const VkAllocationCallbacks  *alloc;
74    VkResult                     (*wait)(struct wsi_fence *fence, uint64_t abs_timeout);
75    void                         (*destroy)(struct wsi_fence *fence);
76 };
77 
78 struct wsi_interface;
79 
80 struct driOptionCache;
81 
82 #define VK_ICD_WSI_PLATFORM_MAX (VK_ICD_WSI_PLATFORM_DISPLAY + 1)
83 
84 struct wsi_device {
85    /* Allocator for the instance */
86    VkAllocationCallbacks instance_alloc;
87 
88    VkPhysicalDevice pdevice;
89    VkPhysicalDeviceMemoryProperties memory_props;
90    uint32_t queue_family_count;
91 
92    VkPhysicalDevicePCIBusInfoPropertiesEXT pci_bus_info;
93 
94    bool supports_modifiers;
95    uint32_t maxImageDimension2D;
96    VkPresentModeKHR override_present_mode;
97    bool force_bgra8_unorm_first;
98 
99    /* Whether to enable adaptive sync for a swapchain if implemented and
100     * available. Not all window systems might support this. */
101    bool enable_adaptive_sync;
102 
103    struct {
104       /* Override the minimum number of images on the swapchain.
105        * 0 = no override */
106       uint32_t override_minImageCount;
107 
108       /* Forces strict number of image on the swapchain using application
109        * provided VkSwapchainCreateInfoKH::RminImageCount.
110        */
111       bool strict_imageCount;
112 
113       /* Ensures to create at least the number of image specified by the
114        * driver in VkSurfaceCapabilitiesKHR::minImageCount.
115        */
116       bool ensure_minImageCount;
117    } x11;
118 
119    bool sw;
120 
121    /* Signals the semaphore such that any wait on the semaphore will wait on
122     * any reads or writes on the give memory object.  This is used to
123     * implement the semaphore signal operation in vkAcquireNextImage.
124     */
125    void (*signal_semaphore_for_memory)(VkDevice device,
126                                        VkSemaphore semaphore,
127                                        VkDeviceMemory memory);
128 
129    /* Signals the fence such that any wait on the fence will wait on any reads
130     * or writes on the give memory object.  This is used to implement the
131     * semaphore signal operation in vkAcquireNextImage.
132     */
133    void (*signal_fence_for_memory)(VkDevice device,
134                                    VkFence fence,
135                                    VkDeviceMemory memory);
136 
137    /*
138     * This sets the ownership for a WSI memory object:
139     *
140     * The ownership is true if and only if the application is allowed to submit
141     * command buffers that reference the buffer.
142     *
143     * This can be used to prune BO lists without too many adverse affects on
144     * implicit sync.
145     *
146     * Side note: care needs to be taken for internally delayed submissions wrt
147     * timeline semaphores.
148     */
149    void (*set_memory_ownership)(VkDevice device,
150                                 VkDeviceMemory memory,
151                                 VkBool32 ownership);
152 
153 #define WSI_CB(cb) PFN_vk##cb cb
154    WSI_CB(AllocateMemory);
155    WSI_CB(AllocateCommandBuffers);
156    WSI_CB(BindBufferMemory);
157    WSI_CB(BindImageMemory);
158    WSI_CB(BeginCommandBuffer);
159    WSI_CB(CmdCopyImageToBuffer);
160    WSI_CB(CreateBuffer);
161    WSI_CB(CreateCommandPool);
162    WSI_CB(CreateFence);
163    WSI_CB(CreateImage);
164    WSI_CB(DestroyBuffer);
165    WSI_CB(DestroyCommandPool);
166    WSI_CB(DestroyFence);
167    WSI_CB(DestroyImage);
168    WSI_CB(EndCommandBuffer);
169    WSI_CB(FreeMemory);
170    WSI_CB(FreeCommandBuffers);
171    WSI_CB(GetBufferMemoryRequirements);
172    WSI_CB(GetImageDrmFormatModifierPropertiesEXT);
173    WSI_CB(GetImageMemoryRequirements);
174    WSI_CB(GetImageSubresourceLayout);
175    WSI_CB(GetMemoryFdKHR);
176    WSI_CB(GetPhysicalDeviceFormatProperties);
177    WSI_CB(GetPhysicalDeviceFormatProperties2KHR);
178    WSI_CB(GetPhysicalDeviceImageFormatProperties2);
179    WSI_CB(ResetFences);
180    WSI_CB(QueueSubmit);
181    WSI_CB(WaitForFences);
182    WSI_CB(MapMemory);
183    WSI_CB(UnmapMemory);
184 #undef WSI_CB
185 
186     struct wsi_interface *                  wsi[VK_ICD_WSI_PLATFORM_MAX];
187 };
188 
189 typedef PFN_vkVoidFunction (VKAPI_PTR *WSI_FN_GetPhysicalDeviceProcAddr)(VkPhysicalDevice physicalDevice, const char* pName);
190 
191 VkResult
192 wsi_device_init(struct wsi_device *wsi,
193                 VkPhysicalDevice pdevice,
194                 WSI_FN_GetPhysicalDeviceProcAddr proc_addr,
195                 const VkAllocationCallbacks *alloc,
196                 int display_fd,
197                 const struct driOptionCache *dri_options,
198                 bool sw_device);
199 
200 void
201 wsi_device_finish(struct wsi_device *wsi,
202                   const VkAllocationCallbacks *alloc);
203 
204 #define ICD_DEFINE_NONDISP_HANDLE_CASTS(__VkIcdType, __VkType)             \
205                                                                            \
206    static inline __VkIcdType *                                             \
207    __VkIcdType ## _from_handle(__VkType _handle)                           \
208    {                                                                       \
209       return (__VkIcdType *)(uintptr_t) _handle;                           \
210    }                                                                       \
211                                                                            \
212    static inline __VkType                                                  \
213    __VkIcdType ## _to_handle(__VkIcdType *_obj)                            \
214    {                                                                       \
215       return (__VkType)(uintptr_t) _obj;                                   \
216    }
217 
218 #define ICD_FROM_HANDLE(__VkIcdType, __name, __handle) \
219    __VkIcdType *__name = __VkIcdType ## _from_handle(__handle)
220 
221 ICD_DEFINE_NONDISP_HANDLE_CASTS(VkIcdSurfaceBase, VkSurfaceKHR)
222 
223 VkResult
224 wsi_common_get_surface_support(struct wsi_device *wsi_device,
225                                uint32_t queueFamilyIndex,
226                                VkSurfaceKHR surface,
227                                VkBool32* pSupported);
228 
229 VkResult
230 wsi_common_get_surface_capabilities(struct wsi_device *wsi_device,
231                                     VkSurfaceKHR surface,
232                                     VkSurfaceCapabilitiesKHR *pSurfaceCapabilities);
233 
234 VkResult
235 wsi_common_get_surface_capabilities2(struct wsi_device *wsi_device,
236                                      const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
237                                      VkSurfaceCapabilities2KHR *pSurfaceCapabilities);
238 
239 VkResult
240 wsi_common_get_surface_formats(struct wsi_device *wsi_device,
241                                VkSurfaceKHR surface,
242                                uint32_t *pSurfaceFormatCount,
243                                VkSurfaceFormatKHR *pSurfaceFormats);
244 
245 VkResult
246 wsi_common_get_surface_formats2(struct wsi_device *wsi_device,
247                                 const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
248                                 uint32_t *pSurfaceFormatCount,
249                                 VkSurfaceFormat2KHR *pSurfaceFormats);
250 
251 VkResult
252 wsi_common_get_surface_present_modes(struct wsi_device *wsi_device,
253                                      VkSurfaceKHR surface,
254                                      uint32_t *pPresentModeCount,
255                                      VkPresentModeKHR *pPresentModes);
256 
257 VkResult
258 wsi_common_get_present_rectangles(struct wsi_device *wsi,
259                                   VkSurfaceKHR surface,
260                                   uint32_t* pRectCount,
261                                   VkRect2D* pRects);
262 
263 VkResult
264 wsi_common_get_surface_capabilities2ext(
265    struct wsi_device *wsi_device,
266    VkSurfaceKHR surface,
267    VkSurfaceCapabilities2EXT *pSurfaceCapabilities);
268 
269 VkResult
270 wsi_common_get_images(VkSwapchainKHR _swapchain,
271                       uint32_t *pSwapchainImageCount,
272                       VkImage *pSwapchainImages);
273 
274 VkResult
275 wsi_common_acquire_next_image2(const struct wsi_device *wsi,
276                                VkDevice device,
277                                const VkAcquireNextImageInfoKHR *pAcquireInfo,
278                                uint32_t *pImageIndex);
279 
280 VkResult
281 wsi_common_create_swapchain(struct wsi_device *wsi,
282                             VkDevice device,
283                             const VkSwapchainCreateInfoKHR *pCreateInfo,
284                             const VkAllocationCallbacks *pAllocator,
285                             VkSwapchainKHR *pSwapchain);
286 void
287 wsi_common_destroy_swapchain(VkDevice device,
288                              VkSwapchainKHR swapchain,
289                              const VkAllocationCallbacks *pAllocator);
290 
291 VkResult
292 wsi_common_queue_present(const struct wsi_device *wsi,
293                          VkDevice device_h,
294                          VkQueue queue_h,
295                          int queue_family_index,
296                          const VkPresentInfoKHR *pPresentInfo);
297 
298 uint64_t
299 wsi_common_get_current_time(void);
300 
301 #endif
302