0aae2a3530d1b315bf12fe764aaa1f7ef48bda2a
[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 return VK_SUCCESS;
583 }
584 }
585
586 xcb_flush(chain->conn);
587 xcb_generic_event_t *event =
588 xcb_wait_for_special_event(chain->conn, chain->special_event);
589 if (!event)
590 return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
591
592 VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
593 free(event);
594 if (result != VK_SUCCESS)
595 return result;
596 }
597 }
598
599 static VkResult
600 x11_queue_present(struct radv_swapchain *radv_chain,
601 struct radv_queue *queue,
602 uint32_t image_index)
603 {
604 struct x11_swapchain *chain = (struct x11_swapchain *)radv_chain;
605 struct x11_image *image = &chain->images[image_index];
606
607 assert(image_index < chain->image_count);
608
609 uint32_t options = XCB_PRESENT_OPTION_NONE;
610
611 int64_t target_msc = 0;
612 int64_t divisor = 0;
613 int64_t remainder = 0;
614
615 options |= XCB_PRESENT_OPTION_ASYNC;
616
617 xshmfence_reset(image->shm_fence);
618
619 ++chain->send_sbc;
620 xcb_void_cookie_t cookie =
621 xcb_present_pixmap(chain->conn,
622 chain->window,
623 image->pixmap,
624 (uint32_t) chain->send_sbc,
625 0, /* valid */
626 0, /* update */
627 0, /* x_off */
628 0, /* y_off */
629 XCB_NONE, /* target_crtc */
630 XCB_NONE,
631 image->sync_fence,
632 options,
633 target_msc,
634 divisor,
635 remainder, 0, NULL);
636 xcb_discard_reply(chain->conn, cookie.sequence);
637 image->busy = true;
638
639 xcb_flush(chain->conn);
640
641 return VK_SUCCESS;
642 }
643
644 static VkResult
645 x11_image_init(struct radv_device *device, struct x11_swapchain *chain,
646 const VkSwapchainCreateInfoKHR *pCreateInfo,
647 const VkAllocationCallbacks* pAllocator,
648 struct x11_image *image)
649 {
650 xcb_void_cookie_t cookie;
651 VkResult result = VK_SUCCESS;
652 int fd;
653 VkImage image_h;
654 bool bret;
655 struct radeon_surf *surface;
656 result = radv_image_create(radv_device_to_handle(device),
657 &(struct radv_image_create_info) {
658 .vk_info =
659 &(VkImageCreateInfo) {
660 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
661 .imageType = VK_IMAGE_TYPE_2D,
662 .format = pCreateInfo->imageFormat,
663 .extent = {
664 .width = pCreateInfo->imageExtent.width,
665 .height = pCreateInfo->imageExtent.height,
666 .depth = 1
667 },
668 .mipLevels = 1,
669 .arrayLayers = 1,
670 .samples = 1,
671 /* FIXME: Need a way to use X tiling to allow scanout */
672 .tiling = VK_IMAGE_TILING_OPTIMAL,
673 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
674 .flags = 0,
675 },
676 .scanout = true},
677 NULL,
678 &image_h);
679 if (result != VK_SUCCESS)
680 return result;
681
682 image->image = radv_image_from_handle(image_h);
683
684 VkDeviceMemory memory_h;
685 result = radv_AllocateMemory(radv_device_to_handle(device),
686 &(VkMemoryAllocateInfo) {
687 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
688 .allocationSize = image->image->size,
689 .memoryTypeIndex = 0,
690 },
691 NULL /* XXX: pAllocator */,
692 &memory_h);
693 if (result != VK_SUCCESS)
694 goto fail_create_image;
695
696 image->memory = radv_device_memory_from_handle(memory_h);
697 // image->memory->bo.is_winsys_bo = true;
698
699 radv_BindImageMemory(VK_NULL_HANDLE, image_h, memory_h, 0);
700
701 bret = device->ws->buffer_get_fd(device->ws,
702 image->memory->bo, &fd);
703 if (bret == false)
704 goto fail_alloc_memory;
705
706 {
707 struct radeon_bo_metadata metadata;
708 radv_init_metadata(device, image->image, &metadata);
709 device->ws->buffer_set_metadata(image->memory->bo, &metadata);
710 }
711 surface = &image->image->surface;
712 uint32_t bpp = 32;
713 uint32_t depth = 24;
714 image->pixmap = xcb_generate_id(chain->conn);
715
716 cookie =
717 xcb_dri3_pixmap_from_buffer_checked(chain->conn,
718 image->pixmap,
719 chain->window,
720 image->image->size,
721 pCreateInfo->imageExtent.width,
722 pCreateInfo->imageExtent.height,
723 surface->level[0].pitch_bytes,
724 depth, bpp, fd);
725 xcb_discard_reply(chain->conn, cookie.sequence);
726
727 int fence_fd = xshmfence_alloc_shm();
728 if (fence_fd < 0)
729 goto fail_pixmap;
730
731 image->shm_fence = xshmfence_map_shm(fence_fd);
732 if (image->shm_fence == NULL)
733 goto fail_shmfence_alloc;
734
735 image->sync_fence = xcb_generate_id(chain->conn);
736 xcb_dri3_fence_from_fd(chain->conn,
737 image->pixmap,
738 image->sync_fence,
739 false,
740 fence_fd);
741
742 image->busy = false;
743 xshmfence_trigger(image->shm_fence);
744
745 return VK_SUCCESS;
746
747 fail_shmfence_alloc:
748 close(fence_fd);
749
750 fail_pixmap:
751 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
752 xcb_discard_reply(chain->conn, cookie.sequence);
753
754 fail_alloc_memory:
755 radv_FreeMemory(radv_device_to_handle(chain->base.device),
756 radv_device_memory_to_handle(image->memory), pAllocator);
757
758 fail_create_image:
759 radv_DestroyImage(radv_device_to_handle(chain->base.device),
760 radv_image_to_handle(image->image), pAllocator);
761
762 return result;
763 }
764
765 static void
766 x11_image_finish(struct x11_swapchain *chain,
767 const VkAllocationCallbacks* pAllocator,
768 struct x11_image *image)
769 {
770 xcb_void_cookie_t cookie;
771
772 cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
773 xcb_discard_reply(chain->conn, cookie.sequence);
774 xshmfence_unmap_shm(image->shm_fence);
775
776 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
777 xcb_discard_reply(chain->conn, cookie.sequence);
778
779 radv_DestroyImage(radv_device_to_handle(chain->base.device),
780 radv_image_to_handle(image->image), pAllocator);
781
782 radv_FreeMemory(radv_device_to_handle(chain->base.device),
783 radv_device_memory_to_handle(image->memory), pAllocator);
784
785 }
786
787 static VkResult
788 x11_swapchain_destroy(struct radv_swapchain *radv_chain,
789 const VkAllocationCallbacks *pAllocator)
790 {
791 struct x11_swapchain *chain = (struct x11_swapchain *)radv_chain;
792
793 for (uint32_t i = 0; i < chain->image_count; i++)
794 x11_image_finish(chain, pAllocator, &chain->images[i]);
795
796 xcb_unregister_for_special_event(chain->conn, chain->special_event);
797
798 radv_free2(&chain->base.device->alloc, pAllocator, chain);
799
800 return VK_SUCCESS;
801 }
802
803 static VkResult
804 x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
805 struct radv_device *device,
806 const VkSwapchainCreateInfoKHR *pCreateInfo,
807 const VkAllocationCallbacks* pAllocator,
808 struct radv_swapchain **swapchain_out)
809 {
810 struct x11_swapchain *chain;
811 xcb_void_cookie_t cookie;
812 VkResult result;
813
814 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
815
816 int num_images = pCreateInfo->minImageCount;
817
818 /* For true mailbox mode, we need at least 4 images:
819 * 1) One to scan out from
820 * 2) One to have queued for scan-out
821 * 3) One to be currently held by the Wayland compositor
822 * 4) One to render to
823 */
824 if (pCreateInfo->presentMode == VK_PRESENT_MODE_MAILBOX_KHR)
825 num_images = MAX(num_images, 4);
826
827 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
828 chain = radv_alloc2(&device->alloc, pAllocator, size, 8,
829 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
830 if (chain == NULL)
831 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
832
833 chain->base.device = device;
834 chain->base.destroy = x11_swapchain_destroy;
835 chain->base.get_images = x11_get_images;
836 chain->base.acquire_next_image = x11_acquire_next_image;
837 chain->base.queue_present = x11_queue_present;
838
839 chain->conn = x11_surface_get_connection(icd_surface);
840 chain->window = x11_surface_get_window(icd_surface);
841 chain->extent = pCreateInfo->imageExtent;
842 chain->image_count = num_images;
843
844 chain->send_sbc = 0;
845 chain->event_id = xcb_generate_id(chain->conn);
846 xcb_present_select_input(chain->conn, chain->event_id, chain->window,
847 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
848 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
849 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
850
851 /* Create an XCB event queue to hold present events outside of the usual
852 * application event queue
853 */
854 chain->special_event =
855 xcb_register_for_special_xge(chain->conn, &xcb_present_id,
856 chain->event_id, NULL);
857
858 chain->gc = xcb_generate_id(chain->conn);
859 if (!chain->gc) {
860 /* FINISHME: Choose a better error. */
861 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
862 goto fail_register;
863 }
864
865 cookie = xcb_create_gc(chain->conn,
866 chain->gc,
867 chain->window,
868 XCB_GC_GRAPHICS_EXPOSURES,
869 (uint32_t []) { 0 });
870 xcb_discard_reply(chain->conn, cookie.sequence);
871
872 uint32_t image = 0;
873 for (; image < chain->image_count; image++) {
874 result = x11_image_init(device, chain, pCreateInfo, pAllocator,
875 &chain->images[image]);
876 if (result != VK_SUCCESS)
877 goto fail_init_images;
878 }
879
880 *swapchain_out = &chain->base;
881
882 return VK_SUCCESS;
883
884 fail_init_images:
885 for (uint32_t j = 0; j < image; j++)
886 x11_image_finish(chain, pAllocator, &chain->images[j]);
887
888 fail_register:
889 xcb_unregister_for_special_event(chain->conn, chain->special_event);
890
891 radv_free2(&device->alloc, pAllocator, chain);
892
893 return result;
894 }
895
896 VkResult
897 radv_x11_init_wsi(struct radv_physical_device *device)
898 {
899 struct wsi_x11 *wsi;
900 VkResult result;
901
902 wsi = radv_alloc(&device->instance->alloc, sizeof(*wsi), 8,
903 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
904 if (!wsi) {
905 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
906 goto fail;
907 }
908
909 int ret = pthread_mutex_init(&wsi->mutex, NULL);
910 if (ret != 0) {
911 if (ret == ENOMEM) {
912 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
913 } else {
914 /* FINISHME: Choose a better error. */
915 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
916 }
917
918 goto fail_alloc;
919 }
920
921 wsi->connections = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
922 _mesa_key_pointer_equal);
923 if (!wsi->connections) {
924 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
925 goto fail_mutex;
926 }
927
928 wsi->base.get_support = x11_surface_get_support;
929 wsi->base.get_capabilities = x11_surface_get_capabilities;
930 wsi->base.get_formats = x11_surface_get_formats;
931 wsi->base.get_present_modes = x11_surface_get_present_modes;
932 wsi->base.create_swapchain = x11_surface_create_swapchain;
933
934 device->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
935 device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = &wsi->base;
936
937 return VK_SUCCESS;
938
939 fail_mutex:
940 pthread_mutex_destroy(&wsi->mutex);
941 fail_alloc:
942 radv_free(&device->instance->alloc, wsi);
943 fail:
944 device->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
945 device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = NULL;
946
947 return result;
948 }
949
950 void
951 radv_x11_finish_wsi(struct radv_physical_device *device)
952 {
953 struct wsi_x11 *wsi =
954 (struct wsi_x11 *)device->wsi[VK_ICD_WSI_PLATFORM_XCB];
955
956 if (wsi) {
957 _mesa_hash_table_destroy(wsi->connections, NULL);
958
959 pthread_mutex_destroy(&wsi->mutex);
960
961 radv_free(&device->instance->alloc, wsi);
962 }
963 }