vulkan/wsi: Add a wsi_device_init function
[mesa.git] / src / vulkan / wsi / wsi_common.h
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 struct wsi_image {
34 VkImage image;
35 VkDeviceMemory memory;
36 uint32_t size;
37 uint32_t offset;
38 uint32_t row_pitch;
39 int fd;
40 };
41
42 struct wsi_device;
43 struct wsi_image_fns {
44 VkResult (*create_wsi_image)(VkDevice device_h,
45 const VkSwapchainCreateInfoKHR *pCreateInfo,
46 const VkAllocationCallbacks *pAllocator,
47 bool needs_linear_copy,
48 bool linear,
49 struct wsi_image *image_p);
50 void (*free_wsi_image)(VkDevice device,
51 const VkAllocationCallbacks *pAllocator,
52 struct wsi_image *image);
53 };
54
55 struct wsi_swapchain {
56
57 VkDevice device;
58 VkAllocationCallbacks alloc;
59 const struct wsi_image_fns *image_fns;
60 VkFence fences[3];
61 VkCommandBuffer *cmd_buffers;
62 VkCommandPool cmd_pools[3];
63 VkPresentModeKHR present_mode;
64 uint32_t image_count;
65 bool needs_linear_copy;
66
67 VkResult (*destroy)(struct wsi_swapchain *swapchain,
68 const VkAllocationCallbacks *pAllocator);
69 VkResult (*get_images)(struct wsi_swapchain *swapchain,
70 uint32_t *pCount, VkImage *pSwapchainImages);
71 VkResult (*acquire_next_image)(struct wsi_swapchain *swap_chain,
72 uint64_t timeout, VkSemaphore semaphore,
73 uint32_t *image_index);
74 VkResult (*queue_present)(struct wsi_swapchain *swap_chain,
75 uint32_t image_index,
76 const VkPresentRegionKHR *damage);
77 void (*get_image_and_linear)(struct wsi_swapchain *swapchain,
78 int imageIndex,
79 VkImage *image,
80 VkImage *linear_image);
81 };
82
83 struct wsi_interface {
84 VkResult (*get_support)(VkIcdSurfaceBase *surface,
85 struct wsi_device *wsi_device,
86 const VkAllocationCallbacks *alloc,
87 uint32_t queueFamilyIndex,
88 int local_fd,
89 bool can_handle_different_gpu,
90 VkBool32* pSupported);
91 VkResult (*get_capabilities)(VkIcdSurfaceBase *surface,
92 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities);
93 VkResult (*get_capabilities2)(VkIcdSurfaceBase *surface,
94 const void *info_next,
95 VkSurfaceCapabilities2KHR* pSurfaceCapabilities);
96 VkResult (*get_formats)(VkIcdSurfaceBase *surface,
97 struct wsi_device *wsi_device,
98 uint32_t* pSurfaceFormatCount,
99 VkSurfaceFormatKHR* pSurfaceFormats);
100 VkResult (*get_formats2)(VkIcdSurfaceBase *surface,
101 struct wsi_device *wsi_device,
102 const void *info_next,
103 uint32_t* pSurfaceFormatCount,
104 VkSurfaceFormat2KHR* pSurfaceFormats);
105 VkResult (*get_present_modes)(VkIcdSurfaceBase *surface,
106 uint32_t* pPresentModeCount,
107 VkPresentModeKHR* pPresentModes);
108 VkResult (*create_swapchain)(VkIcdSurfaceBase *surface,
109 VkDevice device,
110 struct wsi_device *wsi_device,
111 int local_fd,
112 const VkSwapchainCreateInfoKHR* pCreateInfo,
113 const VkAllocationCallbacks* pAllocator,
114 const struct wsi_image_fns *image_fns,
115 struct wsi_swapchain **swapchain);
116 };
117
118 #define VK_ICD_WSI_PLATFORM_MAX 5
119
120 struct wsi_device {
121 struct wsi_interface * wsi[VK_ICD_WSI_PLATFORM_MAX];
122 };
123
124 typedef PFN_vkVoidFunction (VKAPI_PTR *WSI_FN_GetPhysicalDeviceProcAddr)(VkPhysicalDevice physicalDevice, const char* pName);
125
126 void
127 wsi_device_init(struct wsi_device *wsi,
128 VkPhysicalDevice pdevice,
129 WSI_FN_GetPhysicalDeviceProcAddr proc_addr);
130
131 #define WSI_CB(cb) PFN_vk##cb cb
132 struct wsi_callbacks {
133 WSI_CB(GetPhysicalDeviceFormatProperties);
134 };
135 #undef WSI_CB
136
137 #define WSI_DEFINE_NONDISP_HANDLE_CASTS(__wsi_type, __VkType) \
138 \
139 static inline struct __wsi_type * \
140 __wsi_type ## _from_handle(__VkType _handle) \
141 { \
142 return (struct __wsi_type *)(uintptr_t) _handle; \
143 } \
144 \
145 static inline __VkType \
146 __wsi_type ## _to_handle(struct __wsi_type *_obj) \
147 { \
148 return (__VkType)(uintptr_t) _obj; \
149 }
150
151 WSI_DEFINE_NONDISP_HANDLE_CASTS(wsi_swapchain, VkSwapchainKHR)
152
153 #define ICD_DEFINE_NONDISP_HANDLE_CASTS(__VkIcdType, __VkType) \
154 \
155 static inline __VkIcdType * \
156 __VkIcdType ## _from_handle(__VkType _handle) \
157 { \
158 return (__VkIcdType *)(uintptr_t) _handle; \
159 } \
160 \
161 static inline __VkType \
162 __VkIcdType ## _to_handle(__VkIcdType *_obj) \
163 { \
164 return (__VkType)(uintptr_t) _obj; \
165 }
166
167 #define ICD_FROM_HANDLE(__VkIcdType, __name, __handle) \
168 __VkIcdType *__name = __VkIcdType ## _from_handle(__handle)
169
170 ICD_DEFINE_NONDISP_HANDLE_CASTS(VkIcdSurfaceBase, VkSurfaceKHR)
171
172 VkResult wsi_x11_init_wsi(struct wsi_device *wsi_device,
173 const VkAllocationCallbacks *alloc);
174 void wsi_x11_finish_wsi(struct wsi_device *wsi_device,
175 const VkAllocationCallbacks *alloc);
176 VkResult wsi_wl_init_wsi(struct wsi_device *wsi_device,
177 const VkAllocationCallbacks *alloc,
178 VkPhysicalDevice physical_device,
179 const struct wsi_callbacks *cbs);
180 void wsi_wl_finish_wsi(struct wsi_device *wsi_device,
181 const VkAllocationCallbacks *alloc);
182
183
184 #endif