tu: Move our image layout into a freedreno_layout struct.
[mesa.git] / src / freedreno / vulkan / tu_wsi.c
1 /*
2 * Copyright © 2016 Red Hat
3 * based on intel anv code:
4 * Copyright © 2015 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "tu_private.h"
27
28 #include "vk_util.h"
29 #include "wsi_common.h"
30 #include "drm-uapi/drm_fourcc.h"
31
32 static PFN_vkVoidFunction
33 tu_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
34 {
35 return tu_lookup_entrypoint_unchecked(pName);
36 }
37
38 static uint64_t
39 tu_wsi_image_get_modifier(VkImage _image)
40 {
41 TU_FROM_HANDLE(tu_image, image, _image);
42
43 if (!image->layout.tile_mode)
44 return DRM_FORMAT_MOD_LINEAR;
45
46 if (image->layout.ubwc_size)
47 return DRM_FORMAT_MOD_QCOM_COMPRESSED;
48
49 /* TODO invent a modifier for tiled but not UBWC buffers: */
50 return DRM_FORMAT_MOD_INVALID;
51 }
52
53 VkResult
54 tu_wsi_init(struct tu_physical_device *physical_device)
55 {
56 VkResult result;
57
58 result = wsi_device_init(&physical_device->wsi_device,
59 tu_physical_device_to_handle(physical_device),
60 tu_wsi_proc_addr,
61 &physical_device->instance->alloc,
62 physical_device->master_fd, NULL);
63 if (result != VK_SUCCESS)
64 return result;
65
66 physical_device->wsi_device.supports_modifiers = true;
67 physical_device->wsi_device.image_get_modifier = tu_wsi_image_get_modifier;
68
69 return VK_SUCCESS;
70 }
71
72 void
73 tu_wsi_finish(struct tu_physical_device *physical_device)
74 {
75 wsi_device_finish(&physical_device->wsi_device,
76 &physical_device->instance->alloc);
77 }
78
79 void
80 tu_DestroySurfaceKHR(VkInstance _instance,
81 VkSurfaceKHR _surface,
82 const VkAllocationCallbacks *pAllocator)
83 {
84 TU_FROM_HANDLE(tu_instance, instance, _instance);
85 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
86
87 vk_free2(&instance->alloc, pAllocator, surface);
88 }
89
90 VkResult
91 tu_GetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice physicalDevice,
92 uint32_t queueFamilyIndex,
93 VkSurfaceKHR surface,
94 VkBool32 *pSupported)
95 {
96 TU_FROM_HANDLE(tu_physical_device, device, physicalDevice);
97
98 return wsi_common_get_surface_support(
99 &device->wsi_device, queueFamilyIndex, surface, pSupported);
100 }
101
102 VkResult
103 tu_GetPhysicalDeviceSurfaceCapabilitiesKHR(
104 VkPhysicalDevice physicalDevice,
105 VkSurfaceKHR surface,
106 VkSurfaceCapabilitiesKHR *pSurfaceCapabilities)
107 {
108 TU_FROM_HANDLE(tu_physical_device, device, physicalDevice);
109
110 return wsi_common_get_surface_capabilities(&device->wsi_device, surface,
111 pSurfaceCapabilities);
112 }
113
114 VkResult
115 tu_GetPhysicalDeviceSurfaceCapabilities2KHR(
116 VkPhysicalDevice physicalDevice,
117 const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
118 VkSurfaceCapabilities2KHR *pSurfaceCapabilities)
119 {
120 TU_FROM_HANDLE(tu_physical_device, device, physicalDevice);
121
122 return wsi_common_get_surface_capabilities2(
123 &device->wsi_device, pSurfaceInfo, pSurfaceCapabilities);
124 }
125
126 VkResult
127 tu_GetPhysicalDeviceSurfaceCapabilities2EXT(
128 VkPhysicalDevice physicalDevice,
129 VkSurfaceKHR surface,
130 VkSurfaceCapabilities2EXT *pSurfaceCapabilities)
131 {
132 TU_FROM_HANDLE(tu_physical_device, device, physicalDevice);
133
134 return wsi_common_get_surface_capabilities2ext(
135 &device->wsi_device, surface, pSurfaceCapabilities);
136 }
137
138 VkResult
139 tu_GetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice,
140 VkSurfaceKHR surface,
141 uint32_t *pSurfaceFormatCount,
142 VkSurfaceFormatKHR *pSurfaceFormats)
143 {
144 TU_FROM_HANDLE(tu_physical_device, device, physicalDevice);
145
146 return wsi_common_get_surface_formats(
147 &device->wsi_device, surface, pSurfaceFormatCount, pSurfaceFormats);
148 }
149
150 VkResult
151 tu_GetPhysicalDeviceSurfaceFormats2KHR(
152 VkPhysicalDevice physicalDevice,
153 const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
154 uint32_t *pSurfaceFormatCount,
155 VkSurfaceFormat2KHR *pSurfaceFormats)
156 {
157 TU_FROM_HANDLE(tu_physical_device, device, physicalDevice);
158
159 return wsi_common_get_surface_formats2(&device->wsi_device, pSurfaceInfo,
160 pSurfaceFormatCount,
161 pSurfaceFormats);
162 }
163
164 VkResult
165 tu_GetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice,
166 VkSurfaceKHR surface,
167 uint32_t *pPresentModeCount,
168 VkPresentModeKHR *pPresentModes)
169 {
170 TU_FROM_HANDLE(tu_physical_device, device, physicalDevice);
171
172 return wsi_common_get_surface_present_modes(
173 &device->wsi_device, surface, pPresentModeCount, pPresentModes);
174 }
175
176 VkResult
177 tu_CreateSwapchainKHR(VkDevice _device,
178 const VkSwapchainCreateInfoKHR *pCreateInfo,
179 const VkAllocationCallbacks *pAllocator,
180 VkSwapchainKHR *pSwapchain)
181 {
182 TU_FROM_HANDLE(tu_device, device, _device);
183 const VkAllocationCallbacks *alloc;
184 if (pAllocator)
185 alloc = pAllocator;
186 else
187 alloc = &device->alloc;
188
189 return wsi_common_create_swapchain(&device->physical_device->wsi_device,
190 tu_device_to_handle(device),
191 pCreateInfo, alloc, pSwapchain);
192 }
193
194 void
195 tu_DestroySwapchainKHR(VkDevice _device,
196 VkSwapchainKHR swapchain,
197 const VkAllocationCallbacks *pAllocator)
198 {
199 TU_FROM_HANDLE(tu_device, device, _device);
200 const VkAllocationCallbacks *alloc;
201
202 if (pAllocator)
203 alloc = pAllocator;
204 else
205 alloc = &device->alloc;
206
207 wsi_common_destroy_swapchain(_device, swapchain, alloc);
208 }
209
210 VkResult
211 tu_GetSwapchainImagesKHR(VkDevice device,
212 VkSwapchainKHR swapchain,
213 uint32_t *pSwapchainImageCount,
214 VkImage *pSwapchainImages)
215 {
216 return wsi_common_get_images(swapchain, pSwapchainImageCount,
217 pSwapchainImages);
218 }
219
220 VkResult
221 tu_AcquireNextImageKHR(VkDevice device,
222 VkSwapchainKHR swapchain,
223 uint64_t timeout,
224 VkSemaphore semaphore,
225 VkFence fence,
226 uint32_t *pImageIndex)
227 {
228 VkAcquireNextImageInfoKHR acquire_info = {
229 .sType = VK_STRUCTURE_TYPE_ACQUIRE_NEXT_IMAGE_INFO_KHR,
230 .swapchain = swapchain,
231 .timeout = timeout,
232 .semaphore = semaphore,
233 .fence = fence,
234 .deviceMask = 0,
235 };
236
237 return tu_AcquireNextImage2KHR(device, &acquire_info, pImageIndex);
238 }
239
240 VkResult
241 tu_AcquireNextImage2KHR(VkDevice _device,
242 const VkAcquireNextImageInfoKHR *pAcquireInfo,
243 uint32_t *pImageIndex)
244 {
245 TU_FROM_HANDLE(tu_device, device, _device);
246 struct tu_physical_device *pdevice = device->physical_device;
247
248 VkResult result = wsi_common_acquire_next_image2(
249 &pdevice->wsi_device, _device, pAcquireInfo, pImageIndex);
250
251 /* TODO signal fence and semaphore */
252
253 return result;
254 }
255
256 VkResult
257 tu_QueuePresentKHR(VkQueue _queue, const VkPresentInfoKHR *pPresentInfo)
258 {
259 TU_FROM_HANDLE(tu_queue, queue, _queue);
260 return wsi_common_queue_present(
261 &queue->device->physical_device->wsi_device,
262 tu_device_to_handle(queue->device), _queue, queue->queue_family_index,
263 pPresentInfo);
264 }
265
266 VkResult
267 tu_GetDeviceGroupPresentCapabilitiesKHR(
268 VkDevice device, VkDeviceGroupPresentCapabilitiesKHR *pCapabilities)
269 {
270 memset(pCapabilities->presentMask, 0, sizeof(pCapabilities->presentMask));
271 pCapabilities->presentMask[0] = 0x1;
272 pCapabilities->modes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
273
274 return VK_SUCCESS;
275 }
276
277 VkResult
278 tu_GetDeviceGroupSurfacePresentModesKHR(
279 VkDevice device,
280 VkSurfaceKHR surface,
281 VkDeviceGroupPresentModeFlagsKHR *pModes)
282 {
283 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
284
285 return VK_SUCCESS;
286 }
287
288 VkResult
289 tu_GetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice physicalDevice,
290 VkSurfaceKHR surface,
291 uint32_t *pRectCount,
292 VkRect2D *pRects)
293 {
294 TU_FROM_HANDLE(tu_physical_device, device, physicalDevice);
295
296 return wsi_common_get_present_rectangles(&device->wsi_device, surface,
297 pRectCount, pRects);
298 }