• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "vk_dispatch_table.h"
31 #include <vulkan/vulkan.h>
32 #include <vulkan/vk_icd.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #ifndef WSI_ENTRYPOINTS_H
39 extern const struct vk_instance_entrypoint_table wsi_instance_entrypoints;
40 extern const struct vk_physical_device_entrypoint_table wsi_physical_device_entrypoints;
41 extern const struct vk_device_entrypoint_table wsi_device_entrypoints;
42 #endif
43 
44 #include <util/list.h>
45 
46 /* This is guaranteed to not collide with anything because it's in the
47  * VK_KHR_swapchain namespace but not actually used by the extension.
48  */
49 #define VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA (VkStructureType)1000001002
50 #define VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA (VkStructureType)1000001003
51 #define VK_STRUCTURE_TYPE_WSI_SURFACE_SUPPORTED_COUNTERS_MESA (VkStructureType)1000001005
52 #define VK_STRUCTURE_TYPE_WSI_MEMORY_SIGNAL_SUBMIT_INFO_MESA (VkStructureType)1000001006
53 
54 /* This is always chained to VkImageCreateInfo when a wsi image is created.
55  * It indicates that the image can be transitioned to/from
56  * VK_IMAGE_LAYOUT_PRESENT_SRC_KHR.
57  */
58 struct wsi_image_create_info {
59     VkStructureType sType;
60     const void *pNext;
61     bool scanout;
62 
63     /* if true, the image is a buffer blit source */
64     bool buffer_blit_src;
65 };
66 
67 struct wsi_memory_allocate_info {
68     VkStructureType sType;
69     const void *pNext;
70     bool implicit_sync;
71 };
72 
73 /* To be chained into VkSurfaceCapabilities2KHR */
74 struct wsi_surface_supported_counters {
75    VkStructureType sType;
76    const void *pNext;
77 
78    VkSurfaceCounterFlagsEXT supported_surface_counters;
79 
80 };
81 
82 /* To be chained into VkSubmitInfo */
83 struct wsi_memory_signal_submit_info {
84     VkStructureType sType;
85     const void *pNext;
86     VkDeviceMemory memory;
87 };
88 
89 struct wsi_interface;
90 
91 struct driOptionCache;
92 
93 #define VK_ICD_WSI_PLATFORM_MAX (VK_ICD_WSI_PLATFORM_DISPLAY + 1)
94 
95 struct wsi_device {
96    /* Allocator for the instance */
97    VkAllocationCallbacks instance_alloc;
98 
99    VkPhysicalDevice pdevice;
100    VkPhysicalDeviceMemoryProperties memory_props;
101    uint32_t queue_family_count;
102 
103    VkPhysicalDevicePCIBusInfoPropertiesEXT pci_bus_info;
104 
105    VkExternalSemaphoreHandleTypeFlags semaphore_export_handle_types;
106 
107    bool has_import_memory_host;
108 
109    bool supports_modifiers;
110    uint32_t maxImageDimension2D;
111    uint32_t optimalBufferCopyRowPitchAlignment;
112    VkPresentModeKHR override_present_mode;
113    bool force_bgra8_unorm_first;
114 
115    /* Whether to enable adaptive sync for a swapchain if implemented and
116     * available. Not all window systems might support this. */
117    bool enable_adaptive_sync;
118 
119    /* List of fences to signal when hotplug event happens. */
120    struct list_head hotplug_fences;
121 
122    struct {
123       /* Override the minimum number of images on the swapchain.
124        * 0 = no override */
125       uint32_t override_minImageCount;
126 
127       /* Forces strict number of image on the swapchain using application
128        * provided VkSwapchainCreateInfoKH::RminImageCount.
129        */
130       bool strict_imageCount;
131 
132       /* Ensures to create at least the number of image specified by the
133        * driver in VkSurfaceCapabilitiesKHR::minImageCount.
134        */
135       bool ensure_minImageCount;
136 
137       /* Wait for fences before submitting buffers to Xwayland. Defaults to
138        * true.
139        */
140       bool xwaylandWaitReady;
141    } x11;
142 
143    bool sw;
144 
145    /* Set to true if the implementation is ok with linear WSI images. */
146    bool wants_linear;
147 
148    /* Signals the semaphore such that any wait on the semaphore will wait on
149     * any reads or writes on the give memory object.  This is used to
150     * implement the semaphore signal operation in vkAcquireNextImage.  This
151     * requires the driver to implement vk_device::create_sync_for_memory.
152     */
153    bool signal_semaphore_with_memory;
154 
155    /* Signals the fence such that any wait on the fence will wait on any reads
156     * or writes on the give memory object.  This is used to implement the
157     * semaphore signal operation in vkAcquireNextImage.  This requires the
158     * driver to implement vk_device::create_sync_for_memory.  The resulting
159     * vk_sync must support CPU waits.
160     */
161    bool signal_fence_with_memory;
162 
163    /*
164     * This sets the ownership for a WSI memory object:
165     *
166     * The ownership is true if and only if the application is allowed to submit
167     * command buffers that reference the buffer.
168     *
169     * This can be used to prune BO lists without too many adverse affects on
170     * implicit sync.
171     *
172     * Side note: care needs to be taken for internally delayed submissions wrt
173     * timeline semaphores.
174     */
175    void (*set_memory_ownership)(VkDevice device,
176                                 VkDeviceMemory memory,
177                                 VkBool32 ownership);
178 
179    /*
180     * If this is set, the WSI device will call it to let the driver backend
181     * decide if it can present images directly on the given device fd.
182     */
183    bool (*can_present_on_device)(VkPhysicalDevice pdevice, int fd);
184 
185    /*
186     * A driver can implement this callback to return a special queue to execute
187     * buffer blits.
188     */
189    VkQueue (*get_buffer_blit_queue)(VkDevice device);
190 
191 #define WSI_CB(cb) PFN_vk##cb cb
192    WSI_CB(AllocateMemory);
193    WSI_CB(AllocateCommandBuffers);
194    WSI_CB(BindBufferMemory);
195    WSI_CB(BindImageMemory);
196    WSI_CB(BeginCommandBuffer);
197    WSI_CB(CmdPipelineBarrier);
198    WSI_CB(CmdCopyImageToBuffer);
199    WSI_CB(CreateBuffer);
200    WSI_CB(CreateCommandPool);
201    WSI_CB(CreateFence);
202    WSI_CB(CreateImage);
203    WSI_CB(CreateSemaphore);
204    WSI_CB(DestroyBuffer);
205    WSI_CB(DestroyCommandPool);
206    WSI_CB(DestroyFence);
207    WSI_CB(DestroyImage);
208    WSI_CB(DestroySemaphore);
209    WSI_CB(EndCommandBuffer);
210    WSI_CB(FreeMemory);
211    WSI_CB(FreeCommandBuffers);
212    WSI_CB(GetBufferMemoryRequirements);
213    WSI_CB(GetImageDrmFormatModifierPropertiesEXT);
214    WSI_CB(GetImageMemoryRequirements);
215    WSI_CB(GetImageSubresourceLayout);
216    WSI_CB(GetMemoryFdKHR);
217    WSI_CB(GetPhysicalDeviceFormatProperties);
218    WSI_CB(GetPhysicalDeviceFormatProperties2KHR);
219    WSI_CB(GetPhysicalDeviceImageFormatProperties2);
220    WSI_CB(GetSemaphoreFdKHR);
221    WSI_CB(ResetFences);
222    WSI_CB(QueueSubmit);
223    WSI_CB(WaitForFences);
224    WSI_CB(MapMemory);
225    WSI_CB(UnmapMemory);
226 #undef WSI_CB
227 
228     struct wsi_interface *                  wsi[VK_ICD_WSI_PLATFORM_MAX];
229 };
230 
231 typedef PFN_vkVoidFunction (VKAPI_PTR *WSI_FN_GetPhysicalDeviceProcAddr)(VkPhysicalDevice physicalDevice, const char* pName);
232 
233 VkResult
234 wsi_device_init(struct wsi_device *wsi,
235                 VkPhysicalDevice pdevice,
236                 WSI_FN_GetPhysicalDeviceProcAddr proc_addr,
237                 const VkAllocationCallbacks *alloc,
238                 int display_fd,
239                 const struct driOptionCache *dri_options,
240                 bool sw_device);
241 
242 void
243 wsi_device_finish(struct wsi_device *wsi,
244                   const VkAllocationCallbacks *alloc);
245 
246 /* Setup file descriptor to be used with imported sync_fd's in wsi fences. */
247 void
248 wsi_device_setup_syncobj_fd(struct wsi_device *wsi_device,
249                             int fd);
250 
251 #define ICD_DEFINE_NONDISP_HANDLE_CASTS(__VkIcdType, __VkType)             \
252                                                                            \
253    static inline __VkIcdType *                                             \
254    __VkIcdType ## _from_handle(__VkType _handle)                           \
255    {                                                                       \
256       return (__VkIcdType *)(uintptr_t) _handle;                           \
257    }                                                                       \
258                                                                            \
259    static inline __VkType                                                  \
260    __VkIcdType ## _to_handle(__VkIcdType *_obj)                            \
261    {                                                                       \
262       return (__VkType)(uintptr_t) _obj;                                   \
263    }
264 
265 #define ICD_FROM_HANDLE(__VkIcdType, __name, __handle) \
266    __VkIcdType *__name = __VkIcdType ## _from_handle(__handle)
267 
268 ICD_DEFINE_NONDISP_HANDLE_CASTS(VkIcdSurfaceBase, VkSurfaceKHR)
269 
270 VkResult
271 wsi_common_get_images(VkSwapchainKHR _swapchain,
272                       uint32_t *pSwapchainImageCount,
273                       VkImage *pSwapchainImages);
274 
275 VkImage
276 wsi_common_get_image(VkSwapchainKHR _swapchain, uint32_t index);
277 
278 VkResult
279 wsi_common_acquire_next_image2(const struct wsi_device *wsi,
280                                VkDevice device,
281                                const VkAcquireNextImageInfoKHR *pAcquireInfo,
282                                uint32_t *pImageIndex);
283 
284 VkResult
285 wsi_common_queue_present(const struct wsi_device *wsi,
286                          VkDevice device_h,
287                          VkQueue queue_h,
288                          int queue_family_index,
289                          const VkPresentInfoKHR *pPresentInfo);
290 
291 VkResult
292 wsi_common_create_swapchain_image(const struct wsi_device *wsi,
293                                   const VkImageCreateInfo *pCreateInfo,
294                                   VkSwapchainKHR _swapchain,
295                                   VkImage *pImage);
296 VkResult
297 wsi_common_bind_swapchain_image(const struct wsi_device *wsi,
298                                 VkImage vk_image,
299                                 VkSwapchainKHR _swapchain,
300                                 uint32_t image_idx);
301 
302 #ifdef __cplusplus
303 }
304 #endif
305 
306 #endif
307