anv/wsi/x11: start refactoring out the image allocation/free functionality
[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
438 x11_surface_create_swapchain(VkIcdSurfaceBase *surface,
439 struct anv_device *device,
440 const VkSwapchainCreateInfoKHR* pCreateInfo,
441 const VkAllocationCallbacks* pAllocator,
442 struct anv_swapchain **swapchain);
443
444 VkResult anv_CreateXcbSurfaceKHR(
445 VkInstance _instance,
446 const VkXcbSurfaceCreateInfoKHR* pCreateInfo,
447 const VkAllocationCallbacks* pAllocator,
448 VkSurfaceKHR* pSurface)
449 {
450 ANV_FROM_HANDLE(anv_instance, instance, _instance);
451
452 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR);
453
454 VkIcdSurfaceXcb *surface;
455
456 surface = vk_alloc2(&instance->alloc, pAllocator, sizeof *surface, 8,
457 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
458 if (surface == NULL)
459 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
460
461 surface->base.platform = VK_ICD_WSI_PLATFORM_XCB;
462 surface->connection = pCreateInfo->connection;
463 surface->window = pCreateInfo->window;
464
465 *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
466
467 return VK_SUCCESS;
468 }
469
470 VkResult anv_CreateXlibSurfaceKHR(
471 VkInstance _instance,
472 const VkXlibSurfaceCreateInfoKHR* pCreateInfo,
473 const VkAllocationCallbacks* pAllocator,
474 VkSurfaceKHR* pSurface)
475 {
476 ANV_FROM_HANDLE(anv_instance, instance, _instance);
477
478 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR);
479
480 VkIcdSurfaceXlib *surface;
481
482 surface = vk_alloc2(&instance->alloc, pAllocator, sizeof *surface, 8,
483 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
484 if (surface == NULL)
485 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
486
487 surface->base.platform = VK_ICD_WSI_PLATFORM_XLIB;
488 surface->dpy = pCreateInfo->dpy;
489 surface->window = pCreateInfo->window;
490
491 *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
492
493 return VK_SUCCESS;
494 }
495
496 struct x11_image {
497 struct anv_image * image;
498 struct anv_device_memory * memory;
499 xcb_pixmap_t pixmap;
500 bool busy;
501 struct xshmfence * shm_fence;
502 uint32_t sync_fence;
503 };
504
505 struct x11_swapchain {
506 struct anv_swapchain base;
507
508 xcb_connection_t * conn;
509 xcb_window_t window;
510 xcb_gc_t gc;
511 VkExtent2D extent;
512 uint32_t image_count;
513
514 xcb_present_event_t event_id;
515 xcb_special_event_t * special_event;
516 uint64_t send_sbc;
517 uint32_t stamp;
518
519 struct x11_image images[0];
520 };
521
522 static VkResult
523 x11_get_images(struct anv_swapchain *anv_chain,
524 uint32_t* pCount, VkImage *pSwapchainImages)
525 {
526 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
527
528 if (pSwapchainImages == NULL) {
529 *pCount = chain->image_count;
530 return VK_SUCCESS;
531 }
532
533 assert(chain->image_count <= *pCount);
534 for (uint32_t i = 0; i < chain->image_count; i++)
535 pSwapchainImages[i] = anv_image_to_handle(chain->images[i].image);
536
537 *pCount = chain->image_count;
538
539 return VK_SUCCESS;
540 }
541
542 static VkResult
543 x11_handle_dri3_present_event(struct x11_swapchain *chain,
544 xcb_present_generic_event_t *event)
545 {
546 switch (event->evtype) {
547 case XCB_PRESENT_CONFIGURE_NOTIFY: {
548 xcb_present_configure_notify_event_t *config = (void *) event;
549
550 if (config->width != chain->extent.width ||
551 config->height != chain->extent.height)
552 return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
553
554 break;
555 }
556
557 case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
558 xcb_present_idle_notify_event_t *idle = (void *) event;
559
560 for (unsigned i = 0; i < chain->image_count; i++) {
561 if (chain->images[i].pixmap == idle->pixmap) {
562 chain->images[i].busy = false;
563 break;
564 }
565 }
566
567 break;
568 }
569
570 case XCB_PRESENT_COMPLETE_NOTIFY:
571 default:
572 break;
573 }
574
575 return VK_SUCCESS;
576 }
577
578 static VkResult
579 x11_acquire_next_image(struct anv_swapchain *anv_chain,
580 uint64_t timeout,
581 VkSemaphore semaphore,
582 uint32_t *image_index)
583 {
584 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
585
586 while (1) {
587 for (uint32_t i = 0; i < chain->image_count; i++) {
588 if (!chain->images[i].busy) {
589 /* We found a non-busy image */
590 xshmfence_await(chain->images[i].shm_fence);
591 *image_index = i;
592 chain->images[i].busy = true;
593 return VK_SUCCESS;
594 }
595 }
596
597 xcb_flush(chain->conn);
598 xcb_generic_event_t *event =
599 xcb_wait_for_special_event(chain->conn, chain->special_event);
600 if (!event)
601 return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
602
603 VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
604 free(event);
605 if (result != VK_SUCCESS)
606 return result;
607 }
608 }
609
610 static VkResult
611 x11_queue_present(struct anv_swapchain *anv_chain,
612 uint32_t image_index)
613 {
614 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
615 struct x11_image *image = &chain->images[image_index];
616
617 assert(image_index < chain->image_count);
618
619 uint32_t options = XCB_PRESENT_OPTION_NONE;
620
621 int64_t target_msc = 0;
622 int64_t divisor = 0;
623 int64_t remainder = 0;
624
625 options |= XCB_PRESENT_OPTION_ASYNC;
626
627 xshmfence_reset(image->shm_fence);
628
629 ++chain->send_sbc;
630 xcb_void_cookie_t cookie =
631 xcb_present_pixmap(chain->conn,
632 chain->window,
633 image->pixmap,
634 (uint32_t) chain->send_sbc,
635 0, /* valid */
636 0, /* update */
637 0, /* x_off */
638 0, /* y_off */
639 XCB_NONE, /* target_crtc */
640 XCB_NONE,
641 image->sync_fence,
642 options,
643 target_msc,
644 divisor,
645 remainder, 0, NULL);
646 xcb_discard_reply(chain->conn, cookie.sequence);
647 image->busy = true;
648
649 xcb_flush(chain->conn);
650
651 return VK_SUCCESS;
652 }
653
654 static VkResult
655 x11_anv_create_image(VkDevice device_h,
656 struct x11_swapchain *chain,
657 const VkSwapchainCreateInfoKHR *pCreateInfo,
658 const VkAllocationCallbacks* pAllocator,
659 struct x11_image *image,
660 uint32_t *row_pitch, int *fd_p)
661 {
662 VkImage image_h;
663 struct anv_device *device = anv_device_from_handle(device_h);
664 VkResult result;
665 result = anv_image_create(anv_device_to_handle(device),
666 &(struct anv_image_create_info) {
667 .isl_tiling_flags = ISL_TILING_X_BIT,
668 .stride = 0,
669 .vk_info =
670 &(VkImageCreateInfo) {
671 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
672 .imageType = VK_IMAGE_TYPE_2D,
673 .format = pCreateInfo->imageFormat,
674 .extent = {
675 .width = pCreateInfo->imageExtent.width,
676 .height = pCreateInfo->imageExtent.height,
677 .depth = 1
678 },
679 .mipLevels = 1,
680 .arrayLayers = 1,
681 .samples = 1,
682 /* FIXME: Need a way to use X tiling to allow scanout */
683 .tiling = VK_IMAGE_TILING_OPTIMAL,
684 .usage = (pCreateInfo->imageUsage |
685 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
686 .flags = 0,
687 }},
688 NULL,
689 &image_h);
690 if (result != VK_SUCCESS)
691 return result;
692
693 image->image = anv_image_from_handle(image_h);
694 assert(vk_format_is_color(image->image->vk_format));
695
696 VkDeviceMemory memory_h;
697 result = anv_AllocateMemory(anv_device_to_handle(device),
698 &(VkMemoryAllocateInfo) {
699 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
700 .allocationSize = image->image->size,
701 .memoryTypeIndex = 0,
702 },
703 NULL /* XXX: pAllocator */,
704 &memory_h);
705 if (result != VK_SUCCESS)
706 goto fail_create_image;
707
708 image->memory = anv_device_memory_from_handle(memory_h);
709 image->memory->bo.is_winsys_bo = true;
710
711 anv_BindImageMemory(VK_NULL_HANDLE, image_h, memory_h, 0);
712
713 struct anv_surface *surface = &image->image->color_surface;
714 assert(surface->isl.tiling == ISL_TILING_X);
715
716 *row_pitch = surface->isl.row_pitch;
717 int ret = anv_gem_set_tiling(device, image->memory->bo.gem_handle,
718 surface->isl.row_pitch, I915_TILING_X);
719 if (ret) {
720 /* FINISHME: Choose a better error. */
721 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
722 "set_tiling failed: %m");
723 goto fail_alloc_memory;
724 }
725
726 int fd = anv_gem_handle_to_fd(device, image->memory->bo.gem_handle);
727 if (fd == -1) {
728 /* FINISHME: Choose a better error. */
729 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
730 "handle_to_fd failed: %m");
731 goto fail_alloc_memory;
732 }
733
734 *fd_p = fd;
735 return VK_SUCCESS;
736 fail_alloc_memory:
737 anv_FreeMemory(anv_device_to_handle(chain->base.device),
738 anv_device_memory_to_handle(image->memory), pAllocator);
739
740 fail_create_image:
741 anv_DestroyImage(anv_device_to_handle(chain->base.device),
742 anv_image_to_handle(image->image), pAllocator);
743 return result;
744 }
745
746 static void
747 x11_anv_free_image(VkDevice device,
748 struct x11_image *image,
749 const VkAllocationCallbacks* pAllocator)
750 {
751 anv_DestroyImage(device,
752 anv_image_to_handle(image->image), pAllocator);
753
754 anv_FreeMemory(device,
755 anv_device_memory_to_handle(image->memory), pAllocator);
756 }
757
758 static VkResult
759 x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
760 const VkSwapchainCreateInfoKHR *pCreateInfo,
761 const VkAllocationCallbacks* pAllocator,
762 struct x11_image *image)
763 {
764 xcb_void_cookie_t cookie;
765 VkResult result;
766 uint32_t row_pitch;
767 uint32_t bpp = 32;
768 uint32_t depth = 24;
769 int fd;
770 result = x11_anv_create_image(device_h, chain,
771 pCreateInfo, pAllocator, image, &row_pitch, &fd);
772 if (result != VK_SUCCESS)
773 return result;
774 image->pixmap = xcb_generate_id(chain->conn);
775
776 cookie =
777 xcb_dri3_pixmap_from_buffer_checked(chain->conn,
778 image->pixmap,
779 chain->window,
780 image->image->size,
781 pCreateInfo->imageExtent.width,
782 pCreateInfo->imageExtent.height,
783 row_pitch,
784 depth, bpp, fd);
785 xcb_discard_reply(chain->conn, cookie.sequence);
786
787 int fence_fd = xshmfence_alloc_shm();
788 if (fence_fd < 0)
789 goto fail_pixmap;
790
791 image->shm_fence = xshmfence_map_shm(fence_fd);
792 if (image->shm_fence == NULL)
793 goto fail_shmfence_alloc;
794
795 image->sync_fence = xcb_generate_id(chain->conn);
796 xcb_dri3_fence_from_fd(chain->conn,
797 image->pixmap,
798 image->sync_fence,
799 false,
800 fence_fd);
801
802 image->busy = false;
803 xshmfence_trigger(image->shm_fence);
804
805 return VK_SUCCESS;
806
807 fail_shmfence_alloc:
808 close(fence_fd);
809
810 fail_pixmap:
811 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
812 xcb_discard_reply(chain->conn, cookie.sequence);
813
814 x11_anv_free_image(device_h, image, pAllocator);
815 return result;
816 }
817
818 static void
819 x11_image_finish(struct x11_swapchain *chain,
820 const VkAllocationCallbacks* pAllocator,
821 struct x11_image *image)
822 {
823 xcb_void_cookie_t cookie;
824
825 cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
826 xcb_discard_reply(chain->conn, cookie.sequence);
827 xshmfence_unmap_shm(image->shm_fence);
828
829 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
830 xcb_discard_reply(chain->conn, cookie.sequence);
831
832 x11_anv_free_image(anv_device_to_handle(chain->base.device),
833 image, pAllocator);
834 }
835
836 static VkResult
837 x11_swapchain_destroy(struct anv_swapchain *anv_chain,
838 const VkAllocationCallbacks *pAllocator)
839 {
840 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
841
842 for (uint32_t i = 0; i < chain->image_count; i++)
843 x11_image_finish(chain, pAllocator, &chain->images[i]);
844
845 xcb_unregister_for_special_event(chain->conn, chain->special_event);
846
847 vk_free2(&chain->base.device->alloc, pAllocator, chain);
848
849 return VK_SUCCESS;
850 }
851
852 static VkResult
853 x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
854 struct anv_device *device,
855 const VkSwapchainCreateInfoKHR *pCreateInfo,
856 const VkAllocationCallbacks* pAllocator,
857 struct anv_swapchain **swapchain_out)
858 {
859 struct x11_swapchain *chain;
860 xcb_void_cookie_t cookie;
861 VkResult result;
862
863 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
864
865 int num_images = pCreateInfo->minImageCount;
866
867 /* For true mailbox mode, we need at least 4 images:
868 * 1) One to scan out from
869 * 2) One to have queued for scan-out
870 * 3) One to be currently held by the Wayland compositor
871 * 4) One to render to
872 */
873 if (pCreateInfo->presentMode == VK_PRESENT_MODE_MAILBOX_KHR)
874 num_images = MAX2(num_images, 4);
875
876 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
877 chain = vk_alloc2(&device->alloc, pAllocator, size, 8,
878 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
879 if (chain == NULL)
880 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
881
882 chain->base.device = device;
883 chain->base.destroy = x11_swapchain_destroy;
884 chain->base.get_images = x11_get_images;
885 chain->base.acquire_next_image = x11_acquire_next_image;
886 chain->base.queue_present = x11_queue_present;
887
888 chain->conn = x11_surface_get_connection(icd_surface);
889 chain->window = x11_surface_get_window(icd_surface);
890 chain->extent = pCreateInfo->imageExtent;
891 chain->image_count = num_images;
892 chain->send_sbc = 0;
893
894 chain->event_id = xcb_generate_id(chain->conn);
895 xcb_present_select_input(chain->conn, chain->event_id, chain->window,
896 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
897 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
898 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
899
900 /* Create an XCB event queue to hold present events outside of the usual
901 * application event queue
902 */
903 chain->special_event =
904 xcb_register_for_special_xge(chain->conn, &xcb_present_id,
905 chain->event_id, NULL);
906
907 chain->gc = xcb_generate_id(chain->conn);
908 if (!chain->gc) {
909 /* FINISHME: Choose a better error. */
910 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
911 goto fail_register;
912 }
913
914 cookie = xcb_create_gc(chain->conn,
915 chain->gc,
916 chain->window,
917 XCB_GC_GRAPHICS_EXPOSURES,
918 (uint32_t []) { 0 });
919 xcb_discard_reply(chain->conn, cookie.sequence);
920
921 uint32_t image = 0;
922 for (; image < chain->image_count; image++) {
923 result = x11_image_init(anv_device_to_handle(device), chain, pCreateInfo, pAllocator,
924 &chain->images[image]);
925 if (result != VK_SUCCESS)
926 goto fail_init_images;
927 }
928
929 *swapchain_out = &chain->base;
930
931 return VK_SUCCESS;
932
933 fail_init_images:
934 for (uint32_t j = 0; j < image; j++)
935 x11_image_finish(chain, pAllocator, &chain->images[j]);
936
937 fail_register:
938 xcb_unregister_for_special_event(chain->conn, chain->special_event);
939
940 vk_free2(&device->alloc, pAllocator, chain);
941
942 return result;
943 }
944
945 VkResult
946 anv_x11_init_wsi(struct anv_wsi_device *wsi_device,
947 const VkAllocationCallbacks *alloc)
948 {
949 struct wsi_x11 *wsi;
950 VkResult result;
951
952 wsi = vk_alloc(alloc, sizeof(*wsi), 8,
953 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
954 if (!wsi) {
955 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
956 goto fail;
957 }
958
959 int ret = pthread_mutex_init(&wsi->mutex, NULL);
960 if (ret != 0) {
961 if (ret == ENOMEM) {
962 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
963 } else {
964 /* FINISHME: Choose a better error. */
965 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
966 }
967
968 goto fail_alloc;
969 }
970
971 wsi->connections = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
972 _mesa_key_pointer_equal);
973 if (!wsi->connections) {
974 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
975 goto fail_mutex;
976 }
977
978 wsi->base.get_support = x11_surface_get_support;
979 wsi->base.get_capabilities = x11_surface_get_capabilities;
980 wsi->base.get_formats = x11_surface_get_formats;
981 wsi->base.get_present_modes = x11_surface_get_present_modes;
982 wsi->base.create_swapchain = x11_surface_create_swapchain;
983
984 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
985 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = &wsi->base;
986
987 return VK_SUCCESS;
988
989 fail_mutex:
990 pthread_mutex_destroy(&wsi->mutex);
991 fail_alloc:
992 vk_free(alloc, wsi);
993 fail:
994 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
995 wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = NULL;
996
997 return result;
998 }
999
1000 void
1001 anv_x11_finish_wsi(struct anv_wsi_device *wsi_device,
1002 const VkAllocationCallbacks *alloc)
1003 {
1004 struct wsi_x11 *wsi =
1005 (struct wsi_x11 *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB];
1006
1007 if (wsi) {
1008 _mesa_hash_table_destroy(wsi->connections, NULL);
1009
1010 pthread_mutex_destroy(&wsi->mutex);
1011
1012 vk_free(alloc, wsi);
1013 }
1014 }