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