anv/wsi/wayland: add callback to get device format properties.
[mesa.git] / src / intel / vulkan / anv_wsi.c
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
24 #include "anv_wsi.h"
25 #include "vk_format_info.h"
26
27 static const struct anv_wsi_callbacks anv_wsi_cbs = {
28 .get_phys_device_format_properties = anv_GetPhysicalDeviceFormatProperties,
29 };
30
31 VkResult
32 anv_init_wsi(struct anv_physical_device *physical_device)
33 {
34 VkResult result;
35
36 memset(physical_device->wsi_device.wsi, 0, sizeof(physical_device->wsi_device.wsi));
37
38 #ifdef VK_USE_PLATFORM_XCB_KHR
39 result = anv_x11_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
40 if (result != VK_SUCCESS)
41 return result;
42 #endif
43
44 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
45 result = anv_wl_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc,
46 anv_physical_device_to_handle(physical_device),
47 &anv_wsi_cbs);
48 if (result != VK_SUCCESS) {
49 #ifdef VK_USE_PLATFORM_XCB_KHR
50 anv_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
51 #endif
52 return result;
53 }
54 #endif
55
56 return VK_SUCCESS;
57 }
58
59 void
60 anv_finish_wsi(struct anv_physical_device *physical_device)
61 {
62 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
63 anv_wl_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
64 #endif
65 #ifdef VK_USE_PLATFORM_XCB_KHR
66 anv_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
67 #endif
68 }
69
70 void anv_DestroySurfaceKHR(
71 VkInstance _instance,
72 VkSurfaceKHR _surface,
73 const VkAllocationCallbacks* pAllocator)
74 {
75 ANV_FROM_HANDLE(anv_instance, instance, _instance);
76 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
77
78 vk_free2(&instance->alloc, pAllocator, surface);
79 }
80
81 VkResult anv_GetPhysicalDeviceSurfaceSupportKHR(
82 VkPhysicalDevice physicalDevice,
83 uint32_t queueFamilyIndex,
84 VkSurfaceKHR _surface,
85 VkBool32* pSupported)
86 {
87 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
88 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
89 struct anv_wsi_interface *iface = device->wsi_device.wsi[surface->platform];
90
91 return iface->get_support(surface, &device->wsi_device,
92 &device->instance->alloc,
93 queueFamilyIndex, pSupported);
94 }
95
96 VkResult anv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
97 VkPhysicalDevice physicalDevice,
98 VkSurfaceKHR _surface,
99 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
100 {
101 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
102 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
103 struct anv_wsi_interface *iface = device->wsi_device.wsi[surface->platform];
104
105 return iface->get_capabilities(surface, pSurfaceCapabilities);
106 }
107
108 VkResult anv_GetPhysicalDeviceSurfaceFormatsKHR(
109 VkPhysicalDevice physicalDevice,
110 VkSurfaceKHR _surface,
111 uint32_t* pSurfaceFormatCount,
112 VkSurfaceFormatKHR* pSurfaceFormats)
113 {
114 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
115 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
116 struct anv_wsi_interface *iface = device->wsi_device.wsi[surface->platform];
117
118 return iface->get_formats(surface, &device->wsi_device, pSurfaceFormatCount,
119 pSurfaceFormats);
120 }
121
122 VkResult anv_GetPhysicalDeviceSurfacePresentModesKHR(
123 VkPhysicalDevice physicalDevice,
124 VkSurfaceKHR _surface,
125 uint32_t* pPresentModeCount,
126 VkPresentModeKHR* pPresentModes)
127 {
128 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
129 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
130 struct anv_wsi_interface *iface = device->wsi_device.wsi[surface->platform];
131
132 return iface->get_present_modes(surface, pPresentModeCount,
133 pPresentModes);
134 }
135
136
137 static VkResult
138 x11_anv_wsi_image_create(VkDevice device_h,
139 const VkSwapchainCreateInfoKHR *pCreateInfo,
140 const VkAllocationCallbacks* pAllocator,
141 VkImage *image_p,
142 VkDeviceMemory *memory_p,
143 uint32_t *size,
144 uint32_t *offset,
145 uint32_t *row_pitch, int *fd_p)
146 {
147 struct anv_device *device = anv_device_from_handle(device_h);
148 VkImage image_h;
149 struct anv_image *image;
150
151 VkResult result;
152 result = anv_image_create(anv_device_to_handle(device),
153 &(struct anv_image_create_info) {
154 .isl_tiling_flags = ISL_TILING_X_BIT,
155 .stride = 0,
156 .vk_info =
157 &(VkImageCreateInfo) {
158 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
159 .imageType = VK_IMAGE_TYPE_2D,
160 .format = pCreateInfo->imageFormat,
161 .extent = {
162 .width = pCreateInfo->imageExtent.width,
163 .height = pCreateInfo->imageExtent.height,
164 .depth = 1
165 },
166 .mipLevels = 1,
167 .arrayLayers = 1,
168 .samples = 1,
169 /* FIXME: Need a way to use X tiling to allow scanout */
170 .tiling = VK_IMAGE_TILING_OPTIMAL,
171 .usage = (pCreateInfo->imageUsage |
172 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
173 .flags = 0,
174 }},
175 NULL,
176 &image_h);
177 if (result != VK_SUCCESS)
178 return result;
179
180 image = anv_image_from_handle(image_h);
181 assert(vk_format_is_color(image->vk_format));
182
183 VkDeviceMemory memory_h;
184 struct anv_device_memory *memory;
185 result = anv_AllocateMemory(anv_device_to_handle(device),
186 &(VkMemoryAllocateInfo) {
187 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
188 .allocationSize = image->size,
189 .memoryTypeIndex = 0,
190 },
191 NULL /* XXX: pAllocator */,
192 &memory_h);
193 if (result != VK_SUCCESS)
194 goto fail_create_image;
195
196 memory = anv_device_memory_from_handle(memory_h);
197 memory->bo.is_winsys_bo = true;
198
199 anv_BindImageMemory(VK_NULL_HANDLE, image_h, memory_h, 0);
200
201 struct anv_surface *surface = &image->color_surface;
202 assert(surface->isl.tiling == ISL_TILING_X);
203
204 *row_pitch = surface->isl.row_pitch;
205 int ret = anv_gem_set_tiling(device, memory->bo.gem_handle,
206 surface->isl.row_pitch, I915_TILING_X);
207 if (ret) {
208 /* FINISHME: Choose a better error. */
209 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
210 "set_tiling failed: %m");
211 goto fail_alloc_memory;
212 }
213
214 int fd = anv_gem_handle_to_fd(device, memory->bo.gem_handle);
215 if (fd == -1) {
216 /* FINISHME: Choose a better error. */
217 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
218 "handle_to_fd failed: %m");
219 goto fail_alloc_memory;
220 }
221
222 *image_p = image_h;
223 *memory_p = memory_h;
224 *fd_p = fd;
225 *size = image->size;
226 *offset = image->offset;
227 return VK_SUCCESS;
228 fail_alloc_memory:
229 anv_FreeMemory(device_h, memory_h, pAllocator);
230
231 fail_create_image:
232 anv_DestroyImage(device_h, image_h, pAllocator);
233 return result;
234 }
235
236 static void
237 x11_anv_wsi_image_free(VkDevice device,
238 const VkAllocationCallbacks* pAllocator,
239 VkImage image_h,
240 VkDeviceMemory memory_h)
241 {
242 anv_DestroyImage(device, image_h, pAllocator);
243
244 anv_FreeMemory(device, memory_h, pAllocator);
245 }
246
247 static const struct anv_wsi_image_fns anv_wsi_image_fns = {
248 .create_wsi_image = x11_anv_wsi_image_create,
249 .free_wsi_image = x11_anv_wsi_image_free,
250 };
251
252 VkResult anv_CreateSwapchainKHR(
253 VkDevice _device,
254 const VkSwapchainCreateInfoKHR* pCreateInfo,
255 const VkAllocationCallbacks* pAllocator,
256 VkSwapchainKHR* pSwapchain)
257 {
258 ANV_FROM_HANDLE(anv_device, device, _device);
259 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, pCreateInfo->surface);
260 struct anv_wsi_interface *iface =
261 device->instance->physicalDevice.wsi_device.wsi[surface->platform];
262 struct anv_swapchain *swapchain;
263 const VkAllocationCallbacks *alloc;
264
265 if (pAllocator)
266 alloc = pAllocator;
267 else
268 alloc = &device->alloc;
269 VkResult result = iface->create_swapchain(surface, _device,
270 &device->instance->physicalDevice.wsi_device,
271 pCreateInfo,
272 alloc, &anv_wsi_image_fns,
273 &swapchain);
274 if (result != VK_SUCCESS)
275 return result;
276
277 swapchain->alloc = *alloc;
278
279 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++)
280 swapchain->fences[i] = VK_NULL_HANDLE;
281
282 *pSwapchain = anv_swapchain_to_handle(swapchain);
283
284 return VK_SUCCESS;
285 }
286
287 void anv_DestroySwapchainKHR(
288 VkDevice _device,
289 VkSwapchainKHR _swapchain,
290 const VkAllocationCallbacks* pAllocator)
291 {
292 ANV_FROM_HANDLE(anv_device, device, _device);
293 ANV_FROM_HANDLE(anv_swapchain, swapchain, _swapchain);
294 const VkAllocationCallbacks *alloc;
295
296 if (pAllocator)
297 alloc = pAllocator;
298 else
299 alloc = &device->alloc;
300 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++) {
301 if (swapchain->fences[i] != VK_NULL_HANDLE)
302 anv_DestroyFence(_device, swapchain->fences[i], pAllocator);
303 }
304
305 swapchain->destroy(swapchain, alloc);
306 }
307
308 VkResult anv_GetSwapchainImagesKHR(
309 VkDevice device,
310 VkSwapchainKHR _swapchain,
311 uint32_t* pSwapchainImageCount,
312 VkImage* pSwapchainImages)
313 {
314 ANV_FROM_HANDLE(anv_swapchain, swapchain, _swapchain);
315
316 return swapchain->get_images(swapchain, pSwapchainImageCount,
317 pSwapchainImages);
318 }
319
320 VkResult anv_AcquireNextImageKHR(
321 VkDevice device,
322 VkSwapchainKHR _swapchain,
323 uint64_t timeout,
324 VkSemaphore semaphore,
325 VkFence fence,
326 uint32_t* pImageIndex)
327 {
328 ANV_FROM_HANDLE(anv_swapchain, swapchain, _swapchain);
329
330 return swapchain->acquire_next_image(swapchain, timeout, semaphore,
331 pImageIndex);
332 }
333
334 VkResult anv_QueuePresentKHR(
335 VkQueue _queue,
336 const VkPresentInfoKHR* pPresentInfo)
337 {
338 ANV_FROM_HANDLE(anv_queue, queue, _queue);
339 VkResult result;
340
341 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
342 ANV_FROM_HANDLE(anv_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
343
344 assert(anv_device_from_handle(swapchain->device) == queue->device);
345
346 if (swapchain->fences[0] == VK_NULL_HANDLE) {
347 result = anv_CreateFence(anv_device_to_handle(queue->device),
348 &(VkFenceCreateInfo) {
349 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
350 .flags = 0,
351 }, &swapchain->alloc, &swapchain->fences[0]);
352 if (result != VK_SUCCESS)
353 return result;
354 } else {
355 anv_ResetFences(anv_device_to_handle(queue->device),
356 1, &swapchain->fences[0]);
357 }
358
359 anv_QueueSubmit(_queue, 0, NULL, swapchain->fences[0]);
360
361 result = swapchain->queue_present(swapchain,
362 pPresentInfo->pImageIndices[i]);
363 /* TODO: What if one of them returns OUT_OF_DATE? */
364 if (result != VK_SUCCESS)
365 return result;
366
367 VkFence last = swapchain->fences[2];
368 swapchain->fences[2] = swapchain->fences[1];
369 swapchain->fences[1] = swapchain->fences[0];
370 swapchain->fences[0] = last;
371
372 if (last != VK_NULL_HANDLE) {
373 anv_WaitForFences(anv_device_to_handle(queue->device),
374 1, &last, true, 1);
375 }
376 }
377
378 return VK_SUCCESS;
379 }