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