anv/x11: Add support for Xlib platform
[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(struct anv_physical_device *device,
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 anv_alloc(&device->instance->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 anv_free(&device->instance->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(struct anv_physical_device *device,
84 struct wsi_x11_connection *conn)
85 {
86 anv_free(&device->instance->alloc, conn);
87 }
88
89 static struct wsi_x11_connection *
90 wsi_x11_get_connection(struct anv_physical_device *device,
91 xcb_connection_t *conn)
92 {
93 struct wsi_x11 *wsi =
94 (struct wsi_x11 *)device->wsi[VK_ICD_WSI_PLATFORM_XCB];
95
96 pthread_mutex_lock(&wsi->mutex);
97
98 struct hash_entry *entry = _mesa_hash_table_search(wsi->connections, conn);
99 if (!entry) {
100 /* We're about to make a bunch of blocking calls. Let's drop the
101 * mutex for now so we don't block up too badly.
102 */
103 pthread_mutex_unlock(&wsi->mutex);
104
105 struct wsi_x11_connection *wsi_conn =
106 wsi_x11_connection_create(device, conn);
107
108 pthread_mutex_lock(&wsi->mutex);
109
110 entry = _mesa_hash_table_search(wsi->connections, conn);
111 if (entry) {
112 /* Oops, someone raced us to it */
113 wsi_x11_connection_destroy(device, wsi_conn);
114 } else {
115 entry = _mesa_hash_table_insert(wsi->connections, conn, wsi_conn);
116 }
117 }
118
119 pthread_mutex_unlock(&wsi->mutex);
120
121 return entry->data;
122 }
123
124 static const VkSurfaceFormatKHR formats[] = {
125 { .format = VK_FORMAT_B8G8R8A8_SRGB, },
126 };
127
128 static const VkPresentModeKHR present_modes[] = {
129 VK_PRESENT_MODE_MAILBOX_KHR,
130 };
131
132 static xcb_screen_t *
133 get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
134 {
135 xcb_screen_iterator_t screen_iter =
136 xcb_setup_roots_iterator(xcb_get_setup(conn));
137
138 for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
139 if (screen_iter.data->root == root)
140 return screen_iter.data;
141 }
142
143 return NULL;
144 }
145
146 static xcb_visualtype_t *
147 screen_get_visualtype(xcb_screen_t *screen, xcb_visualid_t visual_id,
148 unsigned *depth)
149 {
150 xcb_depth_iterator_t depth_iter =
151 xcb_screen_allowed_depths_iterator(screen);
152
153 for (; depth_iter.rem; xcb_depth_next (&depth_iter)) {
154 xcb_visualtype_iterator_t visual_iter =
155 xcb_depth_visuals_iterator (depth_iter.data);
156
157 for (; visual_iter.rem; xcb_visualtype_next (&visual_iter)) {
158 if (visual_iter.data->visual_id == visual_id) {
159 if (depth)
160 *depth = depth_iter.data->depth;
161 return visual_iter.data;
162 }
163 }
164 }
165
166 return NULL;
167 }
168
169 static xcb_visualtype_t *
170 connection_get_visualtype(xcb_connection_t *conn, xcb_visualid_t visual_id,
171 unsigned *depth)
172 {
173 xcb_screen_iterator_t screen_iter =
174 xcb_setup_roots_iterator(xcb_get_setup(conn));
175
176 /* For this we have to iterate over all of the screens which is rather
177 * annoying. Fortunately, there is probably only 1.
178 */
179 for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
180 xcb_visualtype_t *visual = screen_get_visualtype(screen_iter.data,
181 visual_id, depth);
182 if (visual)
183 return visual;
184 }
185
186 return NULL;
187 }
188
189 static xcb_visualtype_t *
190 get_visualtype_for_window(xcb_connection_t *conn, xcb_window_t window,
191 unsigned *depth)
192 {
193 xcb_query_tree_cookie_t tree_cookie;
194 xcb_get_window_attributes_cookie_t attrib_cookie;
195 xcb_query_tree_reply_t *tree;
196 xcb_get_window_attributes_reply_t *attrib;
197
198 tree_cookie = xcb_query_tree(conn, window);
199 attrib_cookie = xcb_get_window_attributes(conn, window);
200
201 tree = xcb_query_tree_reply(conn, tree_cookie, NULL);
202 attrib = xcb_get_window_attributes_reply(conn, attrib_cookie, NULL);
203 if (attrib == NULL || tree == NULL) {
204 free(attrib);
205 free(tree);
206 return NULL;
207 }
208
209 xcb_window_t root = tree->root;
210 xcb_visualid_t visual_id = attrib->visual;
211 free(attrib);
212 free(tree);
213
214 xcb_screen_t *screen = get_screen_for_root(conn, root);
215 if (screen == NULL)
216 return NULL;
217
218 return screen_get_visualtype(screen, visual_id, depth);
219 }
220
221 static bool
222 visual_has_alpha(xcb_visualtype_t *visual, unsigned depth)
223 {
224 uint32_t rgb_mask = visual->red_mask |
225 visual->green_mask |
226 visual->blue_mask;
227
228 uint32_t all_mask = 0xffffffff >> (32 - depth);
229
230 /* Do we have bits left over after RGB? */
231 return (all_mask & ~rgb_mask) != 0;
232 }
233
234 VkBool32 anv_GetPhysicalDeviceXcbPresentationSupportKHR(
235 VkPhysicalDevice physicalDevice,
236 uint32_t queueFamilyIndex,
237 xcb_connection_t* connection,
238 xcb_visualid_t visual_id)
239 {
240 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
241
242 struct wsi_x11_connection *wsi_conn =
243 wsi_x11_get_connection(device, connection);
244
245 if (!wsi_conn->has_dri3) {
246 fprintf(stderr, "vulkan: No DRI3 support\n");
247 return false;
248 }
249
250 unsigned visual_depth;
251 if (!connection_get_visualtype(connection, visual_id, &visual_depth))
252 return false;
253
254 if (visual_depth != 24 && visual_depth != 32)
255 return false;
256
257 return true;
258 }
259
260 VkBool32 anv_GetPhysicalDeviceXlibPresentationSupportKHR(
261 VkPhysicalDevice physicalDevice,
262 uint32_t queueFamilyIndex,
263 Display* dpy,
264 VisualID visualID)
265 {
266 return anv_GetPhysicalDeviceXcbPresentationSupportKHR(physicalDevice,
267 queueFamilyIndex,
268 XGetXCBConnection(dpy),
269 visualID);
270 }
271
272 static xcb_connection_t*
273 x11_surface_get_connection(VkIcdSurfaceBase *icd_surface)
274 {
275 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
276 return XGetXCBConnection(((VkIcdSurfaceXlib *)icd_surface)->dpy);
277 else
278 return ((VkIcdSurfaceXcb *)icd_surface)->connection;
279 }
280
281 static xcb_window_t
282 x11_surface_get_window(VkIcdSurfaceBase *icd_surface)
283 {
284 if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
285 return ((VkIcdSurfaceXlib *)icd_surface)->window;
286 else
287 return ((VkIcdSurfaceXcb *)icd_surface)->window;
288 }
289
290 static VkResult
291 x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
292 struct anv_physical_device *device,
293 uint32_t queueFamilyIndex,
294 VkBool32* pSupported)
295 {
296 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
297 xcb_window_t window = x11_surface_get_window(icd_surface);
298
299 struct wsi_x11_connection *wsi_conn =
300 wsi_x11_get_connection(device, conn);
301 if (!wsi_conn)
302 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
303
304 if (!wsi_conn->has_dri3) {
305 fprintf(stderr, "vulkan: No DRI3 support\n");
306 *pSupported = false;
307 return VK_SUCCESS;
308 }
309
310 unsigned visual_depth;
311 if (!get_visualtype_for_window(conn, window, &visual_depth)) {
312 *pSupported = false;
313 return VK_SUCCESS;
314 }
315
316 if (visual_depth != 24 && visual_depth != 32) {
317 *pSupported = false;
318 return VK_SUCCESS;
319 }
320
321 *pSupported = true;
322 return VK_SUCCESS;
323 }
324
325 static VkResult
326 x11_surface_get_capabilities(VkIcdSurfaceBase *icd_surface,
327 struct anv_physical_device *device,
328 VkSurfaceCapabilitiesKHR *caps)
329 {
330 xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
331 xcb_window_t window = x11_surface_get_window(icd_surface);
332 xcb_get_geometry_cookie_t geom_cookie;
333 xcb_generic_error_t *err;
334 xcb_get_geometry_reply_t *geom;
335 unsigned visual_depth;
336
337 geom_cookie = xcb_get_geometry(conn, window);
338
339 /* This does a round-trip. This is why we do get_geometry first and
340 * wait to read the reply until after we have a visual.
341 */
342 xcb_visualtype_t *visual =
343 get_visualtype_for_window(conn, window, &visual_depth);
344
345 geom = xcb_get_geometry_reply(conn, geom_cookie, &err);
346 if (geom) {
347 VkExtent2D extent = { geom->width, geom->height };
348 caps->currentExtent = extent;
349 caps->minImageExtent = extent;
350 caps->maxImageExtent = extent;
351 } else {
352 /* This can happen if the client didn't wait for the configure event
353 * to come back from the compositor. In that case, we don't know the
354 * size of the window so we just return valid "I don't know" stuff.
355 */
356 caps->currentExtent = (VkExtent2D) { -1, -1 };
357 caps->minImageExtent = (VkExtent2D) { 1, 1 };
358 caps->maxImageExtent = (VkExtent2D) { INT16_MAX, INT16_MAX };
359 }
360 free(err);
361 free(geom);
362
363 if (visual_has_alpha(visual, visual_depth)) {
364 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
365 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
366 } else {
367 caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
368 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
369 }
370
371 caps->minImageCount = 2;
372 caps->maxImageCount = 4;
373 caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
374 caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
375 caps->maxImageArrayLayers = 1;
376 caps->supportedUsageFlags =
377 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
378 VK_IMAGE_USAGE_SAMPLED_BIT |
379 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
380 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
381
382 return VK_SUCCESS;
383 }
384
385 static VkResult
386 x11_surface_get_formats(VkIcdSurfaceBase *surface,
387 struct anv_physical_device *device,
388 uint32_t *pSurfaceFormatCount,
389 VkSurfaceFormatKHR *pSurfaceFormats)
390 {
391 if (pSurfaceFormats == NULL) {
392 *pSurfaceFormatCount = ARRAY_SIZE(formats);
393 return VK_SUCCESS;
394 }
395
396 assert(*pSurfaceFormatCount >= ARRAY_SIZE(formats));
397 typed_memcpy(pSurfaceFormats, formats, *pSurfaceFormatCount);
398 *pSurfaceFormatCount = ARRAY_SIZE(formats);
399
400 return VK_SUCCESS;
401 }
402
403 static VkResult
404 x11_surface_get_present_modes(VkIcdSurfaceBase *surface,
405 struct anv_physical_device *device,
406 uint32_t *pPresentModeCount,
407 VkPresentModeKHR *pPresentModes)
408 {
409 if (pPresentModes == NULL) {
410 *pPresentModeCount = ARRAY_SIZE(present_modes);
411 return VK_SUCCESS;
412 }
413
414 assert(*pPresentModeCount >= ARRAY_SIZE(present_modes));
415 typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
416 *pPresentModeCount = ARRAY_SIZE(present_modes);
417
418 return VK_SUCCESS;
419 }
420
421 static VkResult
422 x11_surface_create_swapchain(VkIcdSurfaceBase *surface,
423 struct anv_device *device,
424 const VkSwapchainCreateInfoKHR* pCreateInfo,
425 const VkAllocationCallbacks* pAllocator,
426 struct anv_swapchain **swapchain);
427
428 VkResult anv_CreateXcbSurfaceKHR(
429 VkInstance _instance,
430 const VkXcbSurfaceCreateInfoKHR* pCreateInfo,
431 const VkAllocationCallbacks* pAllocator,
432 VkSurfaceKHR* pSurface)
433 {
434 ANV_FROM_HANDLE(anv_instance, instance, _instance);
435
436 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR);
437
438 VkIcdSurfaceXcb *surface;
439
440 surface = anv_alloc2(&instance->alloc, pAllocator, sizeof *surface, 8,
441 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
442 if (surface == NULL)
443 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
444
445 surface->base.platform = VK_ICD_WSI_PLATFORM_XCB;
446 surface->connection = pCreateInfo->connection;
447 surface->window = pCreateInfo->window;
448
449 *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
450
451 return VK_SUCCESS;
452 }
453
454 VkResult anv_CreateXlibSurfaceKHR(
455 VkInstance _instance,
456 const VkXlibSurfaceCreateInfoKHR* pCreateInfo,
457 const VkAllocationCallbacks* pAllocator,
458 VkSurfaceKHR* pSurface)
459 {
460 ANV_FROM_HANDLE(anv_instance, instance, _instance);
461
462 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR);
463
464 VkIcdSurfaceXlib *surface;
465
466 surface = anv_alloc2(&instance->alloc, pAllocator, sizeof *surface, 8,
467 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
468 if (surface == NULL)
469 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
470
471 surface->base.platform = VK_ICD_WSI_PLATFORM_XLIB;
472 surface->dpy = pCreateInfo->dpy;
473 surface->window = pCreateInfo->window;
474
475 *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
476
477 return VK_SUCCESS;
478 }
479
480 struct x11_image {
481 struct anv_image * image;
482 struct anv_device_memory * memory;
483 xcb_pixmap_t pixmap;
484 bool busy;
485 struct xshmfence * shm_fence;
486 uint32_t sync_fence;
487 };
488
489 struct x11_swapchain {
490 struct anv_swapchain base;
491
492 xcb_connection_t * conn;
493 xcb_window_t window;
494 xcb_gc_t gc;
495 VkExtent2D extent;
496 uint32_t image_count;
497
498 xcb_present_event_t event_id;
499 xcb_special_event_t * special_event;
500 uint64_t send_sbc;
501 uint32_t stamp;
502
503 struct x11_image images[0];
504 };
505
506 static VkResult
507 x11_get_images(struct anv_swapchain *anv_chain,
508 uint32_t* pCount, VkImage *pSwapchainImages)
509 {
510 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
511
512 if (pSwapchainImages == NULL) {
513 *pCount = chain->image_count;
514 return VK_SUCCESS;
515 }
516
517 assert(chain->image_count <= *pCount);
518 for (uint32_t i = 0; i < chain->image_count; i++)
519 pSwapchainImages[i] = anv_image_to_handle(chain->images[i].image);
520
521 *pCount = chain->image_count;
522
523 return VK_SUCCESS;
524 }
525
526 static VkResult
527 x11_handle_dri3_present_event(struct x11_swapchain *chain,
528 xcb_present_generic_event_t *event)
529 {
530 switch (event->evtype) {
531 case XCB_PRESENT_CONFIGURE_NOTIFY: {
532 xcb_present_configure_notify_event_t *config = (void *) event;
533
534 if (config->width != chain->extent.width ||
535 config->height != chain->extent.height)
536 return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
537
538 break;
539 }
540
541 case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
542 xcb_present_idle_notify_event_t *idle = (void *) event;
543
544 for (unsigned i = 0; i < chain->image_count; i++) {
545 if (chain->images[i].pixmap == idle->pixmap) {
546 chain->images[i].busy = false;
547 break;
548 }
549 }
550
551 break;
552 }
553
554 case XCB_PRESENT_COMPLETE_NOTIFY:
555 default:
556 break;
557 }
558
559 return VK_SUCCESS;
560 }
561
562 static VkResult
563 x11_acquire_next_image(struct anv_swapchain *anv_chain,
564 uint64_t timeout,
565 VkSemaphore semaphore,
566 uint32_t *image_index)
567 {
568 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
569
570 while (1) {
571 for (uint32_t i = 0; i < chain->image_count; i++) {
572 if (!chain->images[i].busy) {
573 /* We found a non-busy image */
574 xshmfence_await(chain->images[i].shm_fence);
575 *image_index = i;
576 return VK_SUCCESS;
577 }
578 }
579
580 xcb_flush(chain->conn);
581 xcb_generic_event_t *event =
582 xcb_wait_for_special_event(chain->conn, chain->special_event);
583 if (!event)
584 return vk_error(VK_ERROR_OUT_OF_DATE_KHR);
585
586 VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
587 free(event);
588 if (result != VK_SUCCESS)
589 return result;
590 }
591 }
592
593 static VkResult
594 x11_queue_present(struct anv_swapchain *anv_chain,
595 struct anv_queue *queue,
596 uint32_t image_index)
597 {
598 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
599 struct x11_image *image = &chain->images[image_index];
600
601 assert(image_index < chain->image_count);
602
603 uint32_t options = XCB_PRESENT_OPTION_NONE;
604
605 int64_t target_msc = 0;
606 int64_t divisor = 0;
607 int64_t remainder = 0;
608
609 options |= XCB_PRESENT_OPTION_ASYNC;
610
611 xshmfence_reset(image->shm_fence);
612
613 xcb_void_cookie_t cookie =
614 xcb_present_pixmap(chain->conn,
615 chain->window,
616 image->pixmap,
617 (uint32_t) chain->send_sbc,
618 0, /* valid */
619 0, /* update */
620 0, /* x_off */
621 0, /* y_off */
622 XCB_NONE, /* target_crtc */
623 XCB_NONE,
624 image->sync_fence,
625 options,
626 target_msc,
627 divisor,
628 remainder, 0, NULL);
629 xcb_discard_reply(chain->conn, cookie.sequence);
630 image->busy = true;
631
632 xcb_flush(chain->conn);
633
634 return VK_SUCCESS;
635 }
636
637 static VkResult
638 x11_image_init(struct anv_device *device, struct x11_swapchain *chain,
639 const VkSwapchainCreateInfoKHR *pCreateInfo,
640 const VkAllocationCallbacks* pAllocator,
641 struct x11_image *image)
642 {
643 xcb_void_cookie_t cookie;
644 VkResult result;
645
646 VkImage image_h;
647 result = anv_image_create(anv_device_to_handle(device),
648 &(struct anv_image_create_info) {
649 .isl_tiling_flags = ISL_TILING_X_BIT,
650 .stride = 0,
651 .vk_info =
652 &(VkImageCreateInfo) {
653 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
654 .imageType = VK_IMAGE_TYPE_2D,
655 .format = pCreateInfo->imageFormat,
656 .extent = {
657 .width = pCreateInfo->imageExtent.width,
658 .height = pCreateInfo->imageExtent.height,
659 .depth = 1
660 },
661 .mipLevels = 1,
662 .arrayLayers = 1,
663 .samples = 1,
664 /* FIXME: Need a way to use X tiling to allow scanout */
665 .tiling = VK_IMAGE_TILING_OPTIMAL,
666 .usage = (pCreateInfo->imageUsage |
667 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
668 .flags = 0,
669 }},
670 NULL,
671 &image_h);
672 if (result != VK_SUCCESS)
673 return result;
674
675 image->image = anv_image_from_handle(image_h);
676 assert(vk_format_is_color(image->image->vk_format));
677
678 VkDeviceMemory memory_h;
679 result = anv_AllocateMemory(anv_device_to_handle(device),
680 &(VkMemoryAllocateInfo) {
681 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
682 .allocationSize = image->image->size,
683 .memoryTypeIndex = 0,
684 },
685 NULL /* XXX: pAllocator */,
686 &memory_h);
687 if (result != VK_SUCCESS)
688 goto fail_create_image;
689
690 image->memory = anv_device_memory_from_handle(memory_h);
691 image->memory->bo.is_winsys_bo = true;
692
693 anv_BindImageMemory(VK_NULL_HANDLE, image_h, memory_h, 0);
694
695 struct anv_surface *surface = &image->image->color_surface;
696 assert(surface->isl.tiling == ISL_TILING_X);
697
698 int ret = anv_gem_set_tiling(device, image->memory->bo.gem_handle,
699 surface->isl.row_pitch, I915_TILING_X);
700 if (ret) {
701 /* FINISHME: Choose a better error. */
702 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
703 "set_tiling failed: %m");
704 goto fail_alloc_memory;
705 }
706
707 int fd = anv_gem_handle_to_fd(device, image->memory->bo.gem_handle);
708 if (fd == -1) {
709 /* FINISHME: Choose a better error. */
710 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
711 "handle_to_fd failed: %m");
712 goto fail_alloc_memory;
713 }
714
715 uint32_t bpp = 32;
716 uint32_t depth = 24;
717 image->pixmap = xcb_generate_id(chain->conn);
718
719 cookie =
720 xcb_dri3_pixmap_from_buffer_checked(chain->conn,
721 image->pixmap,
722 chain->window,
723 image->image->size,
724 pCreateInfo->imageExtent.width,
725 pCreateInfo->imageExtent.height,
726 surface->isl.row_pitch,
727 depth, bpp, fd);
728 xcb_discard_reply(chain->conn, cookie.sequence);
729
730 int fence_fd = xshmfence_alloc_shm();
731 if (fence_fd < 0)
732 goto fail_pixmap;
733
734 image->shm_fence = xshmfence_map_shm(fence_fd);
735 if (image->shm_fence == NULL)
736 goto fail_shmfence_alloc;
737
738 image->sync_fence = xcb_generate_id(chain->conn);
739 xcb_dri3_fence_from_fd(chain->conn,
740 image->pixmap,
741 image->sync_fence,
742 false,
743 fence_fd);
744
745 image->busy = false;
746 xshmfence_trigger(image->shm_fence);
747
748 return VK_SUCCESS;
749
750 fail_shmfence_alloc:
751 close(fence_fd);
752
753 fail_pixmap:
754 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
755 xcb_discard_reply(chain->conn, cookie.sequence);
756
757 fail_alloc_memory:
758 anv_FreeMemory(anv_device_to_handle(chain->base.device),
759 anv_device_memory_to_handle(image->memory), pAllocator);
760
761 fail_create_image:
762 anv_DestroyImage(anv_device_to_handle(chain->base.device),
763 anv_image_to_handle(image->image), pAllocator);
764
765 return result;
766 }
767
768 static void
769 x11_image_finish(struct x11_swapchain *chain,
770 const VkAllocationCallbacks* pAllocator,
771 struct x11_image *image)
772 {
773 xcb_void_cookie_t cookie;
774
775 cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
776 xcb_discard_reply(chain->conn, cookie.sequence);
777 xshmfence_unmap_shm(image->shm_fence);
778
779 cookie = xcb_free_pixmap(chain->conn, image->pixmap);
780 xcb_discard_reply(chain->conn, cookie.sequence);
781
782 anv_DestroyImage(anv_device_to_handle(chain->base.device),
783 anv_image_to_handle(image->image), pAllocator);
784
785 anv_FreeMemory(anv_device_to_handle(chain->base.device),
786 anv_device_memory_to_handle(image->memory), pAllocator);
787 }
788
789 static VkResult
790 x11_swapchain_destroy(struct anv_swapchain *anv_chain,
791 const VkAllocationCallbacks *pAllocator)
792 {
793 struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
794
795 for (uint32_t i = 0; i < chain->image_count; i++)
796 x11_image_finish(chain, pAllocator, &chain->images[i]);
797
798 xcb_unregister_for_special_event(chain->conn, chain->special_event);
799
800 anv_free2(&chain->base.device->alloc, pAllocator, chain);
801
802 return VK_SUCCESS;
803 }
804
805 static VkResult
806 x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
807 struct anv_device *device,
808 const VkSwapchainCreateInfoKHR *pCreateInfo,
809 const VkAllocationCallbacks* pAllocator,
810 struct anv_swapchain **swapchain_out)
811 {
812 struct x11_swapchain *chain;
813 xcb_void_cookie_t cookie;
814 VkResult result;
815
816 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
817
818 int num_images = pCreateInfo->minImageCount;
819
820 /* For true mailbox mode, we need at least 4 images:
821 * 1) One to scan out from
822 * 2) One to have queued for scan-out
823 * 3) One to be currently held by the Wayland compositor
824 * 4) One to render to
825 */
826 if (pCreateInfo->presentMode == VK_PRESENT_MODE_MAILBOX_KHR)
827 num_images = MAX2(num_images, 4);
828
829 size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
830 chain = anv_alloc2(&device->alloc, pAllocator, size, 8,
831 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
832 if (chain == NULL)
833 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
834
835 chain->base.device = device;
836 chain->base.destroy = x11_swapchain_destroy;
837 chain->base.get_images = x11_get_images;
838 chain->base.acquire_next_image = x11_acquire_next_image;
839 chain->base.queue_present = x11_queue_present;
840
841 chain->conn = x11_surface_get_connection(icd_surface);
842 chain->window = x11_surface_get_window(icd_surface);
843 chain->extent = pCreateInfo->imageExtent;
844 chain->image_count = num_images;
845
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 anv_free2(&device->alloc, pAllocator, chain);
893
894 return result;
895 }
896
897 VkResult
898 anv_x11_init_wsi(struct anv_physical_device *device)
899 {
900 struct wsi_x11 *wsi;
901 VkResult result;
902
903 wsi = anv_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 anv_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 anv_x11_finish_wsi(struct anv_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 anv_free(&device->instance->alloc, wsi);
963 }
964 }