radv: drop local MIN/MAX macros.
[mesa.git] / src / amd / vulkan / radv_wsi_x11.c
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based mostly on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 */
27
28 #include <X11/Xlib-xcb.h>
29 #include <X11/xshmfence.h>
30 #include <xcb/xcb.h>
31 #include <xcb/dri3.h>
32 #include <xcb/present.h>
33 #include <errno.h>
34
35 #include <unistd.h>
36 #include "radv_wsi.h"
37 #include "vk_format.h"
38 #include "util/hash_table.h"
39
40 struct wsi_x11_connection {
41 bool has_dri3;
42 bool has_present;
43 };
44
45 struct wsi_x11 {
46 struct radv_wsi_interface base;
47
48 pthread_mutex_t mutex;
49 /* Hash table of xcb_connection -> wsi_x11_connection mappings */
50 struct hash_table *connections;
51 };
52
53 static struct wsi_x11_connection *
54 wsi_x11_connection_create(struct radv_physical_device *device,
55 xcb_connection_t *conn)
56 {
57 xcb_query_extension_cookie_t dri3_cookie, pres_cookie;
58 xcb_query_extension_reply_t *dri3_reply, *pres_reply;
59
60 struct wsi_x11_connection *wsi_conn =
61 radv_alloc(&device->instance->alloc, sizeof(*wsi_conn), 8,
62 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
63 if (!wsi_conn)
64 return NULL;
65
66 dri3_cookie = xcb_query_extension(conn, 4, "DRI3");
67 pres_cookie = xcb_query_extension(conn, 7, "PRESENT");
68
69 dri3_reply = xcb_query_extension_reply(conn, dri3_cookie, NULL);
70 pres_reply = xcb_query_extension_reply(conn, pres_cookie, NULL);
71 if (dri3_reply == NULL || pres_reply == NULL) {
72 free(dri3_reply);
73 free(pres_reply);
74 radv_free(&device->instance->alloc, wsi_conn);
75 return NULL;
76 }
77
78 wsi_conn->has_dri3 = dri3_reply->present != 0;
79 wsi_conn->has_present = pres_reply->present != 0;
80
81 free(dri3_reply);
82 free(pres_reply);
83
84 return wsi_conn;
85 }
86
87 static void
88 wsi_x11_connection_destroy(struct radv_physical_device *device,
89 struct wsi_x11_connection *conn)
90 {
91 radv_free(&device->instance->alloc, conn);
92 }
93
94 static struct wsi_x11_connection *
95 wsi_x11_get_connection(struct radv_physical_device *device,
96 xcb_connection_t *conn)
97 {
98 struct wsi_x11 *wsi =
99 (struct wsi_x11 *)device->wsi[VK_ICD_WSI_PLATFORM_XCB];
100
101 pthread_mutex_lock(&wsi->mutex);
102
103 struct hash_entry *entry = _mesa_hash_table_search(wsi->connections, conn);
104 if (!entry) {
105 /* We're about to make a bunch of blocking calls. Let's drop the
106 * mutex for now so we don't block up too badly.
107 */
108 pthread_mutex_unlock(&wsi->mutex);
109
110 struct wsi_x11_connection *wsi_conn =
111 wsi_x11_connection_create(device, conn);
112
113 pthread_mutex_lock(&wsi->mutex);
114
115 entry = _mesa_hash_table_search(wsi->connections, conn);
116 if (entry) {
117 /* Oops, someone raced us to it */
118 wsi_x11_connection_destroy(device, wsi_conn);
119 } else {
120 entry = _mesa_hash_table_insert(wsi->connections, conn, wsi_conn);
121 }
122 }
123
124 pthread_mutex_unlock(&wsi->mutex);
125
126 return entry->data;
127 }
128
129 static const VkSurfaceFormatKHR formats[] = {
130 { .format = VK_FORMAT_B8G8R8A8_UNORM, },
131 { .format = VK_FORMAT_B8G8R8A8_SRGB, },
132 };
133
134 static const VkPresentModeKHR present_modes[] = {
135 VK_PRESENT_MODE_MAILBOX_KHR,
136 };
137
138 static xcb_screen_t *
139 get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
140 {
141 xcb_screen_iterator_t screen_iter =
142 xcb_setup_roots_iterator(xcb_get_setup(conn));
143
144 for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
145 if (screen_iter.data->root == root)
146 return screen_iter.data;
147 }
148
149 return NULL;
150 }
151
152 static xcb_visualtype_t *
153 screen_get_visualtype(xcb_screen_t *screen, xcb_visualid_t visual_id,
154 unsigned *depth)
155 {
156 xcb_depth_iterator_t depth_iter =
157 xcb_screen_allowed_depths_iterator(screen);
158
159 for (; depth_iter.rem; xcb_depth_next (&depth_iter)) {
160 xcb_visualtype_iterator_t visual_iter =
161 xcb_depth_visuals_iterator (depth_iter.data);
162
163 for (; visual_iter.rem; xcb_visualtype_next (&visual_iter)) {
164 if (visual_iter.data->visual_id == visual_id) {
165 if (depth)
166 *depth = depth_iter.data->depth;
167 return visual_iter.data;
168 }
169 }
170 }
171
172 return NULL;
173 }
174
175 static xcb_visualtype_t *
176 connection_get_visualtype(xcb_connection_t *conn, xcb_visualid_t visual_id,
177 unsigned *depth)
178 {
179 xcb_screen_iterator_t screen_iter =
180 xcb_setup_roots_iterator(xcb_get_setup(conn));
181
182 /* For this we have to iterate over all of the screens which is rather
183 * annoying. Fortunately, there is probably only 1.
184 */
185 for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
186 xcb_visualtype_t *visual = screen_get_visualtype(screen_iter.data,
187 visual_id, depth);
188 if (visual)
189 return visual;
190 }
191
192 return NULL;
193 }
194
195 static xcb_visualtype_t *
196 get_visualtype_for_window(xcb_connection_t *conn, xcb_window_t window,
197 unsigned *depth)
198 {
199 xcb_query_tree_cookie_t tree_cookie;
200 xcb_get_window_attributes_cookie_t attrib_cookie;
201 xcb_query_tree_reply_t *tree;
202 xcb_get_window_attributes_reply_t *attrib;
203
204 tree_cookie = xcb_query_tree(conn, window);
205 attrib_cookie = xcb_get_window_attributes(conn, window);
206
207 tree = xcb_query_tree_reply(conn, tree_cookie, NULL);
208 attrib = xcb_get_window_attributes_reply(conn, attrib_cookie, NULL);
209 if (attrib == NULL || tree == NULL) {
210 free(attrib);
211 free(tree);
212 return NULL;
213 }
214
215 xcb_window_t root = tree->root;
216 xcb_visualid_t visual_id = attrib->visual;
217 free(attrib);
218 free(tree);
219
220 xcb_screen_t *screen = get_screen_for_root(conn, root);
221 if (screen == NULL)
222 return NULL;
223
224 return screen_get_visualtype(screen, visual_id, depth);
225 }
226
227 static bool
228 visual_has_alpha(xcb_visualtype_t *visual, unsigned depth)
229 {
230 uint32_t rgb_mask = visual->red_mask |
231 visual->green_mask |
232 visual->blue_mask;
233
234 uint32_t all_mask = 0xffffffff >> (32 - depth);
235
236 /* Do we have bits left over after RGB? */
237 return (all_mask & ~rgb_mask) != 0;
238 }
239
240 VkBool32 radv_GetPhysicalDeviceXcbPresentationSupportKHR(
241 VkPhysicalDevice physicalDevice,
242 uint32_t queueFamilyIndex,
243 xcb_connection_t* connection,
244 xcb_visualid_t visual_id)
245 {
246 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
247
248 struct wsi_x11_connection *wsi_conn =
249 wsi_x11_get_connection(device, connection);
250
251 if (!wsi_conn->has_dri3) {
252 fprintf(stderr, "vulkan: No DRI3 support\n");
253 return false;
254 }
255
256 unsigned visual_depth;
257 if (!connection_get_visualtype(connection, visual_id, &visual_depth))
258 return false;
259
260 if (visual_depth != 24 && visual_depth != 32)
261 return false;
262
263 return true;
264 }
265
266 VkBool32 radv_GetPhysicalDeviceXlibPresentationSupportKHR(
267 VkPhysicalDevice physicalDevice,
268 uint32_t queueFamilyIndex,
269 Display* dpy,
270 VisualID visualID)
271 {
272 return radv_GetPhysicalDeviceXcbPresentationSupportKHR(physicalDevice,
273 queueFamilyIndex,
274 XGetXCBConnection(dpy),
275 visualID);
276 }
277
278 static xcb_connection_t*
279 x11_surface_get_connection(VkIcdSurfaceBase *icd_surface)
280 {
281 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
282 return XGetXCBConnection(((VkIcdSurfaceXlib *)icd_surface)->dpy);
283 else
284 return ((VkIcdSurfaceXcb *)icd_surface)->connection;
285 }
286
287 static xcb_window_t
288 x11_surface_get_window(VkIcdSurfaceBase *icd_surface)
289 {
290 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
291 return ((VkIcdSurfaceXlib *)icd_surface)->window;
292 else
293 return ((VkIcdSurfaceXcb *)icd_surface)->window;
294 }
295
296 static VkResult
297 x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
298 struct radv_physical_device *device,
299 uint32_t queueFamilyIndex,
300 VkBool32* pSupported)
301 {
302 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
303 xcb_window_t window = x11_surface_get_window(icd_surface);
304
305 struct wsi_x11_connection *wsi_conn =
306 wsi_x11_get_connection(device, conn);
307 if (!wsi_conn)
308 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
309
310 if (!wsi_conn->has_dri3) {
311 fprintf(stderr, "vulkan: No DRI3 support\n");
312 *pSupported = false;
313 return VK_SUCCESS;
314 }
315
316 unsigned visual_depth;
317 if (!get_visualtype_for_window(conn, window, &visual_depth)) {
318 *pSupported = false;
319 return VK_SUCCESS;
320 }
321
322 if (visual_depth != 24 && visual_depth != 32) {
323 *pSupported = false;
324 return VK_SUCCESS;
325 }
326
327 *pSupported = true;
328 return VK_SUCCESS;
329 }
330
331 static VkResult
332 x11_surface_get_capabilities(VkIcdSurfaceBase *icd_surface,
333 struct radv_physical_device *device,
334 VkSurfaceCapabilitiesKHR *caps)
335 {
336 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
337 xcb_window_t window = x11_surface_get_window(icd_surface);
338 xcb_get_geometry_cookie_t geom_cookie;
339 xcb_generic_error_t *err;
340 xcb_get_geometry_reply_t *geom;
341 unsigned visual_depth;
342
343 geom_cookie = xcb_get_geometry(conn, window);
344
345 /* This does a round-trip. This is why we do get_geometry first and
346 * wait to read the reply until after we have a visual.
347 */
348 xcb_visualtype_t *visual =
349 get_visualtype_for_window(conn, window, &visual_depth);
350
351 geom = xcb_get_geometry_reply(conn, geom_cookie, &err);
352 if (geom) {
353 VkExtent2D extent = { geom->width, geom->height };
354 caps->currentExtent = extent;
355 caps->minImageExtent = extent;
356 caps->maxImageExtent = extent;
357 } else {
358 /* This can happen if the client didn't wait for the configure event
359 * to come back from the compositor. In that case, we don't know the
360 * size of the window so we just return valid "I don't know" stuff.
361 */
362 caps->currentExtent = (VkExtent2D) { -1, -1 };
363 caps->minImageExtent = (VkExtent2D) { 1, 1 };
364 caps->maxImageExtent = (VkExtent2D) { INT16_MAX, INT16_MAX };
365 }
366 free(err);
367 free(geom);
368
369 if (visual_has_alpha(visual, visual_depth)) {
370 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
371 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
372 } else {
373 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
374 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
375 }
376
377 caps->minImageCount = 2;
378 caps->maxImageCount = 4;
379 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
380 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
381 caps->maxImageArrayLayers = 1;
382 caps->supportedUsageFlags =
383 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
384 VK_IMAGE_USAGE_SAMPLED_BIT |
385 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
386 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
387
388 return VK_SUCCESS;
389 }
390
391 static VkResult
392 x11_surface_get_formats(VkIcdSurfaceBase *surface,
393 struct radv_physical_device *device,
394 uint32_t *pSurfaceFormatCount,
395 VkSurfaceFormatKHR *pSurfaceFormats)
396 {
397 if (pSurfaceFormats == NULL) {
398 *pSurfaceFormatCount = ARRAY_SIZE(formats);
399 return VK_SUCCESS;
400 }
401
402 assert(*pSurfaceFormatCount >= ARRAY_SIZE(formats));
403 typed_memcpy(pSurfaceFormats, formats, *pSurfaceFormatCount);
404 *pSurfaceFormatCount = ARRAY_SIZE(formats);
405
406 return VK_SUCCESS;
407 }
408
409 static VkResult
410 x11_surface_get_present_modes(VkIcdSurfaceBase *surface,
411 struct radv_physical_device *device,
412 uint32_t *pPresentModeCount,
413 VkPresentModeKHR *pPresentModes)
414 {
415 if (pPresentModes == NULL) {
416 *pPresentModeCount = ARRAY_SIZE(present_modes);
417 return VK_SUCCESS;
418 }
419
420 assert(*pPresentModeCount >= ARRAY_SIZE(present_modes));
421 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
422 *pPresentModeCount = ARRAY_SIZE(present_modes);
423
424 return VK_SUCCESS;
425 }
426
427 static VkResult
428 x11_surface_create_swapchain(VkIcdSurfaceBase *surface,
429 struct radv_device *device,
430 const VkSwapchainCreateInfoKHR* pCreateInfo,
431 const VkAllocationCallbacks* pAllocator,
432 struct radv_swapchain **swapchain);
433
434 VkResult radv_CreateXcbSurfaceKHR(
435 VkInstance _instance,
436 const VkXcbSurfaceCreateInfoKHR* pCreateInfo,
437 const VkAllocationCallbacks* pAllocator,
438 VkSurfaceKHR* pSurface)
439 {
440 RADV_FROM_HANDLE(radv_instance, instance, _instance);
441
442 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR);
443
444 VkIcdSurfaceXcb *surface;
445
446 surface = radv_alloc2(&instance->alloc, pAllocator, sizeof *surface, 8,
447 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
448 if (surface == NULL)
449 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
450
451 surface->base.platform = VK_ICD_WSI_PLATFORM_XCB;
452 surface->connection = pCreateInfo->connection;
453 surface->window = pCreateInfo->window;
454
455 *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
456
457 return VK_SUCCESS;
458 }
459
460 VkResult radv_CreateXlibSurfaceKHR(
461 VkInstance _instance,
462 const VkXlibSurfaceCreateInfoKHR* pCreateInfo,
463 const VkAllocationCallbacks* pAllocator,
464 VkSurfaceKHR* pSurface)
465 {
466 RADV_FROM_HANDLE(radv_instance, instance, _instance);
467
468 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR);
469
470 VkIcdSurfaceXlib *surface;
471
472 surface = radv_alloc2(&instance->alloc, pAllocator, sizeof *surface, 8,
473 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
474 if (surface == NULL)
475 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
476
477 surface->base.platform = VK_ICD_WSI_PLATFORM_XLIB;
478 surface->dpy = pCreateInfo->dpy;
479 surface->window = pCreateInfo->window;
480
481 *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
482
483 return VK_SUCCESS;
484 }
485
486 struct x11_image {
487 struct radv_image * image;
488 struct radv_device_memory * memory;
489 xcb_pixmap_t pixmap;
490 bool busy;
491 struct xshmfence * shm_fence;
492 uint32_t sync_fence;
493 };
494
495 struct x11_swapchain {
496 struct radv_swapchain base;
497
498 xcb_connection_t * conn;
499 xcb_window_t window;
500 xcb_gc_t gc;
501 VkExtent2D extent;
502 uint32_t image_count;
503
504 xcb_present_event_t event_id;
505 xcb_special_event_t * special_event;
506 uint64_t send_sbc;
507 uint32_t stamp;
508
509 struct x11_image images[0];
510 };
511
512 static VkResult
513 x11_get_images(struct radv_swapchain *radv_chain,
514 uint32_t* pCount, VkImage *pSwapchainImages)
515 {
516 struct x11_swapchain *chain = (struct x11_swapchain *)radv_chain;
517
518 if (pSwapchainImages == NULL) {
519 *pCount = chain->image_count;
520 return VK_SUCCESS;
521 }
522
523 assert(chain->image_count <= *pCount);
524 for (uint32_t i = 0; i < chain->image_count; i++)
525 pSwapchainImages[i] = radv_image_to_handle(chain->images[i].image);
526
527 *pCount = chain->image_count;
528
529 return VK_SUCCESS;
530 }
531
532 static VkResult
533 x11_handle_dri3_present_event(struct x11_swapchain *chain,
534 xcb_present_generic_event_t *event)
535 {
536 switch (event->evtype) {
537 case XCB_PRESENT_CONFIGURE_NOTIFY: {
538 xcb_present_configure_notify_event_t *config = (void *) event;
539
540 if (config->width != chain->extent.width ||
541 config->height != chain->extent.height)
542 return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
543
544 break;
545 }
546
547 case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
548 xcb_present_idle_notify_event_t *idle = (void *) event;
549
550 for (unsigned i = 0; i < chain->image_count; i++) {
551 if (chain->images[i].pixmap == idle->pixmap) {
552 chain->images[i].busy = false;
553 break;
554 }
555 }
556
557 break;
558 }
559
560 case XCB_PRESENT_COMPLETE_NOTIFY:
561 default:
562 break;
563 }
564
565 return VK_SUCCESS;
566 }
567
568 static VkResult
569 x11_acquire_next_image(struct radv_swapchain *radv_chain,
570 uint64_t timeout,
571 VkSemaphore semaphore,
572 uint32_t *image_index)
573 {
574 struct x11_swapchain *chain = (struct x11_swapchain *)radv_chain;
575
576 while (1) {
577 for (uint32_t i = 0; i < chain->image_count; i++) {
578 if (!chain->images[i].busy) {
579 /* We found a non-busy image */
580 xshmfence_await(chain->images[i].shm_fence);
581 *image_index = i;
582 chain->images[i].busy = true;
583 return VK_SUCCESS;
584 }
585 }
586
587 xcb_flush(chain->conn);
588 xcb_generic_event_t *event =
589 xcb_wait_for_special_event(chain->conn, chain->special_event);
590 if (!event)
591 return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
592
593 VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
594 free(event);
595 if (result != VK_SUCCESS)
596 return result;
597 }
598 }
599
600 static VkResult
601 x11_queue_present(struct radv_swapchain *radv_chain,
602 struct radv_queue *queue,
603 uint32_t image_index)
604 {
605 struct x11_swapchain *chain = (struct x11_swapchain *)radv_chain;
606 struct x11_image *image = &chain->images[image_index];
607
608 assert(image_index < chain->image_count);
609
610 uint32_t options = XCB_PRESENT_OPTION_NONE;
611
612 int64_t target_msc = 0;
613 int64_t divisor = 0;
614 int64_t remainder = 0;
615
616 options |= XCB_PRESENT_OPTION_ASYNC;
617
618 xshmfence_reset(image->shm_fence);
619
620 ++chain->send_sbc;
621 xcb_void_cookie_t cookie =
622 xcb_present_pixmap(chain->conn,
623 chain->window,
624 image->pixmap,
625 (uint32_t) chain->send_sbc,
626 0, /* valid */
627 0, /* update */
628 0, /* x_off */
629 0, /* y_off */
630 XCB_NONE, /* target_crtc */
631 XCB_NONE,
632 image->sync_fence,
633 options,
634 target_msc,
635 divisor,
636 remainder, 0, NULL);
637 xcb_discard_reply(chain->conn, cookie.sequence);
638 image->busy = true;
639
640 xcb_flush(chain->conn);
641
642 return VK_SUCCESS;
643 }
644
645 static VkResult
646 x11_image_init(struct radv_device *device, struct x11_swapchain *chain,
647 const VkSwapchainCreateInfoKHR *pCreateInfo,
648 const VkAllocationCallbacks* pAllocator,
649 struct x11_image *image)
650 {
651 xcb_void_cookie_t cookie;
652 VkResult result = VK_SUCCESS;
653 int fd;
654 VkImage image_h;
655 bool bret;
656 struct radeon_surf *surface;
657 result = radv_image_create(radv_device_to_handle(device),
658 &(struct radv_image_create_info) {
659 .vk_info =
660 &(VkImageCreateInfo) {
661 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
662 .imageType = VK_IMAGE_TYPE_2D,
663 .format = pCreateInfo->imageFormat,
664 .extent = {
665 .width = pCreateInfo->imageExtent.width,
666 .height = pCreateInfo->imageExtent.height,
667 .depth = 1
668 },
669 .mipLevels = 1,
670 .arrayLayers = 1,
671 .samples = 1,
672 /* FIXME: Need a way to use X tiling to allow scanout */
673 .tiling = VK_IMAGE_TILING_OPTIMAL,
674 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
675 .flags = 0,
676 },
677 .scanout = true},
678 NULL,
679 &image_h);
680 if (result != VK_SUCCESS)
681 return result;
682
683 image->image = radv_image_from_handle(image_h);
684
685 VkDeviceMemory memory_h;
686 result = radv_AllocateMemory(radv_device_to_handle(device),
687 &(VkMemoryAllocateInfo) {
688 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
689 .allocationSize = image->image->size,
690 .memoryTypeIndex = 0,
691 },
692 NULL /* XXX: pAllocator */,
693 &memory_h);
694 if (result != VK_SUCCESS)
695 goto fail_create_image;
696
697 image->memory = radv_device_memory_from_handle(memory_h);
698 // image->memory->bo.is_winsys_bo = true;
699
700 radv_BindImageMemory(VK_NULL_HANDLE, image_h, memory_h, 0);
701
702 bret = device->ws->buffer_get_fd(device->ws,
703 image->memory->bo, &fd);
704 if (bret == false)
705 goto fail_alloc_memory;
706
707 {
708 struct radeon_bo_metadata metadata;
709 radv_init_metadata(device, image->image, &metadata);
710 device->ws->buffer_set_metadata(image->memory->bo, &metadata);
711 }
712 surface = &image->image->surface;
713 uint32_t bpp = 32;
714 uint32_t depth = 24;
715 image->pixmap = xcb_generate_id(chain->conn);
716
717 cookie =
718 xcb_dri3_pixmap_from_buffer_checked(chain->conn,
719 image->pixmap,
720 chain->window,
721 image->image->size,
722 pCreateInfo->imageExtent.width,
723 pCreateInfo->imageExtent.height,
724 surface->level[0].pitch_bytes,
725 depth, bpp, fd);
726 xcb_discard_reply(chain->conn, cookie.sequence);
727
728 int fence_fd = xshmfence_alloc_shm();
729 if (fence_fd < 0)
730 goto fail_pixmap;
731
732 image->shm_fence = xshmfence_map_shm(fence_fd);
733 if (image->shm_fence == NULL)
734 goto fail_shmfence_alloc;
735
736 image->sync_fence = xcb_generate_id(chain->conn);
737 xcb_dri3_fence_from_fd(chain->conn,
738 image->pixmap,
739 image->sync_fence,
740 false,
741 fence_fd);
742
743 image->busy = false;
744 xshmfence_trigger(image->shm_fence);
745
746 return VK_SUCCESS;
747
748 fail_shmfence_alloc:
749 close(fence_fd);
750
751 fail_pixmap:
752 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
753 xcb_discard_reply(chain->conn, cookie.sequence);
754
755 fail_alloc_memory:
756 radv_FreeMemory(radv_device_to_handle(chain->base.device),
757 radv_device_memory_to_handle(image->memory), pAllocator);
758
759 fail_create_image:
760 radv_DestroyImage(radv_device_to_handle(chain->base.device),
761 radv_image_to_handle(image->image), pAllocator);
762
763 return result;
764 }
765
766 static void
767 x11_image_finish(struct x11_swapchain *chain,
768 const VkAllocationCallbacks* pAllocator,
769 struct x11_image *image)
770 {
771 xcb_void_cookie_t cookie;
772
773 cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
774 xcb_discard_reply(chain->conn, cookie.sequence);
775 xshmfence_unmap_shm(image->shm_fence);
776
777 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
778 xcb_discard_reply(chain->conn, cookie.sequence);
779
780 radv_DestroyImage(radv_device_to_handle(chain->base.device),
781 radv_image_to_handle(image->image), pAllocator);
782
783 radv_FreeMemory(radv_device_to_handle(chain->base.device),
784 radv_device_memory_to_handle(image->memory), pAllocator);
785
786 }
787
788 static VkResult
789 x11_swapchain_destroy(struct radv_swapchain *radv_chain,
790 const VkAllocationCallbacks *pAllocator)
791 {
792 struct x11_swapchain *chain = (struct x11_swapchain *)radv_chain;
793
794 for (uint32_t i = 0; i < chain->image_count; i++)
795 x11_image_finish(chain, pAllocator, &chain->images[i]);
796
797 xcb_unregister_for_special_event(chain->conn, chain->special_event);
798
799 radv_free2(&chain->base.device->alloc, pAllocator, chain);
800
801 return VK_SUCCESS;
802 }
803
804 static VkResult
805 x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
806 struct radv_device *device,
807 const VkSwapchainCreateInfoKHR *pCreateInfo,
808 const VkAllocationCallbacks* pAllocator,
809 struct radv_swapchain **swapchain_out)
810 {
811 struct x11_swapchain *chain;
812 xcb_void_cookie_t cookie;
813 VkResult result;
814
815 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
816
817 int num_images = pCreateInfo->minImageCount;
818
819 /* For true mailbox mode, we need at least 4 images:
820 * 1) One to scan out from
821 * 2) One to have queued for scan-out
822 * 3) One to be currently held by the Wayland compositor
823 * 4) One to render to
824 */
825 if (pCreateInfo->presentMode == VK_PRESENT_MODE_MAILBOX_KHR)
826 num_images = MAX2(num_images, 4);
827
828 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
829 chain = radv_alloc2(&device->alloc, pAllocator, size, 8,
830 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
831 if (chain == NULL)
832 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
833
834 chain->base.device = device;
835 chain->base.destroy = x11_swapchain_destroy;
836 chain->base.get_images = x11_get_images;
837 chain->base.acquire_next_image = x11_acquire_next_image;
838 chain->base.queue_present = x11_queue_present;
839
840 chain->conn = x11_surface_get_connection(icd_surface);
841 chain->window = x11_surface_get_window(icd_surface);
842 chain->extent = pCreateInfo->imageExtent;
843 chain->image_count = num_images;
844
845 chain->send_sbc = 0;
846 chain->event_id = xcb_generate_id(chain->conn);
847 xcb_present_select_input(chain->conn, chain->event_id, chain->window,
848 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
849 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
850 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
851
852 /* Create an XCB event queue to hold present events outside of the usual
853 * application event queue
854 */
855 chain->special_event =
856 xcb_register_for_special_xge(chain->conn, &xcb_present_id,
857 chain->event_id, NULL);
858
859 chain->gc = xcb_generate_id(chain->conn);
860 if (!chain->gc) {
861 /* FINISHME: Choose a better error. */
862 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
863 goto fail_register;
864 }
865
866 cookie = xcb_create_gc(chain->conn,
867 chain->gc,
868 chain->window,
869 XCB_GC_GRAPHICS_EXPOSURES,
870 (uint32_t []) { 0 });
871 xcb_discard_reply(chain->conn, cookie.sequence);
872
873 uint32_t image = 0;
874 for (; image < chain->image_count; image++) {
875 result = x11_image_init(device, chain, pCreateInfo, pAllocator,
876 &chain->images[image]);
877 if (result != VK_SUCCESS)
878 goto fail_init_images;
879 }
880
881 *swapchain_out = &chain->base;
882
883 return VK_SUCCESS;
884
885 fail_init_images:
886 for (uint32_t j = 0; j < image; j++)
887 x11_image_finish(chain, pAllocator, &chain->images[j]);
888
889 fail_register:
890 xcb_unregister_for_special_event(chain->conn, chain->special_event);
891
892 radv_free2(&device->alloc, pAllocator, chain);
893
894 return result;
895 }
896
897 VkResult
898 radv_x11_init_wsi(struct radv_physical_device *device)
899 {
900 struct wsi_x11 *wsi;
901 VkResult result;
902
903 wsi = radv_alloc(&device->instance->alloc, sizeof(*wsi), 8,
904 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
905 if (!wsi) {
906 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
907 goto fail;
908 }
909
910 int ret = pthread_mutex_init(&wsi->mutex, NULL);
911 if (ret != 0) {
912 if (ret == ENOMEM) {
913 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
914 } else {
915 /* FINISHME: Choose a better error. */
916 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
917 }
918
919 goto fail_alloc;
920 }
921
922 wsi->connections = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
923 _mesa_key_pointer_equal);
924 if (!wsi->connections) {
925 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
926 goto fail_mutex;
927 }
928
929 wsi->base.get_support = x11_surface_get_support;
930 wsi->base.get_capabilities = x11_surface_get_capabilities;
931 wsi->base.get_formats = x11_surface_get_formats;
932 wsi->base.get_present_modes = x11_surface_get_present_modes;
933 wsi->base.create_swapchain = x11_surface_create_swapchain;
934
935 device->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
936 device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = &wsi->base;
937
938 return VK_SUCCESS;
939
940 fail_mutex:
941 pthread_mutex_destroy(&wsi->mutex);
942 fail_alloc:
943 radv_free(&device->instance->alloc, wsi);
944 fail:
945 device->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
946 device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = NULL;
947
948 return result;
949 }
950
951 void
952 radv_x11_finish_wsi(struct radv_physical_device *device)
953 {
954 struct wsi_x11 *wsi =
955 (struct wsi_x11 *)device->wsi[VK_ICD_WSI_PLATFORM_XCB];
956
957 if (wsi) {
958 _mesa_hash_table_destroy(wsi->connections, NULL);
959
960 pthread_mutex_destroy(&wsi->mutex);
961
962 radv_free(&device->instance->alloc, wsi);
963 }
964 }