Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/vulkan/wsi/wsi_common_x11.c
7417 views
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 "util/macros.h"
31
#include <stdlib.h>
32
#include <stdio.h>
33
#include <unistd.h>
34
#include <errno.h>
35
#include <string.h>
36
#include <fcntl.h>
37
#include <poll.h>
38
#include <xf86drm.h>
39
#include "drm-uapi/drm_fourcc.h"
40
#include "util/hash_table.h"
41
#include "util/u_thread.h"
42
#include "util/xmlconfig.h"
43
44
#include "vk_util.h"
45
#include "vk_enum_to_str.h"
46
#include "wsi_common_private.h"
47
#include "wsi_common_x11.h"
48
#include "wsi_common_queue.h"
49
50
struct wsi_x11_connection {
51
bool has_dri3;
52
bool has_dri3_modifiers;
53
bool has_present;
54
bool is_proprietary_x11;
55
bool is_xwayland;
56
};
57
58
struct wsi_x11 {
59
struct wsi_interface base;
60
61
pthread_mutex_t mutex;
62
/* Hash table of xcb_connection -> wsi_x11_connection mappings */
63
struct hash_table *connections;
64
};
65
66
67
/** wsi_dri3_open
68
*
69
* Wrapper around xcb_dri3_open
70
*/
71
static int
72
wsi_dri3_open(xcb_connection_t *conn,
73
xcb_window_t root,
74
uint32_t provider)
75
{
76
xcb_dri3_open_cookie_t cookie;
77
xcb_dri3_open_reply_t *reply;
78
int fd;
79
80
cookie = xcb_dri3_open(conn,
81
root,
82
provider);
83
84
reply = xcb_dri3_open_reply(conn, cookie, NULL);
85
if (!reply)
86
return -1;
87
88
if (reply->nfd != 1) {
89
free(reply);
90
return -1;
91
}
92
93
fd = xcb_dri3_open_reply_fds(conn, reply)[0];
94
free(reply);
95
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
96
97
return fd;
98
}
99
100
static bool
101
wsi_x11_check_dri3_compatible(const struct wsi_device *wsi_dev,
102
xcb_connection_t *conn)
103
{
104
xcb_screen_iterator_t screen_iter =
105
xcb_setup_roots_iterator(xcb_get_setup(conn));
106
xcb_screen_t *screen = screen_iter.data;
107
108
int dri3_fd = wsi_dri3_open(conn, screen->root, None);
109
if (dri3_fd == -1)
110
return true;
111
112
bool match = wsi_device_matches_drm_fd(wsi_dev, dri3_fd);
113
114
close(dri3_fd);
115
116
return match;
117
}
118
119
static bool
120
wsi_x11_detect_xwayland(xcb_connection_t *conn)
121
{
122
xcb_randr_query_version_cookie_t ver_cookie =
123
xcb_randr_query_version_unchecked(conn, 1, 3);
124
xcb_randr_query_version_reply_t *ver_reply =
125
xcb_randr_query_version_reply(conn, ver_cookie, NULL);
126
bool has_randr_v1_3 = ver_reply && (ver_reply->major_version > 1 ||
127
ver_reply->minor_version >= 3);
128
free(ver_reply);
129
130
if (!has_randr_v1_3)
131
return false;
132
133
const xcb_setup_t *setup = xcb_get_setup(conn);
134
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
135
136
xcb_randr_get_screen_resources_current_cookie_t gsr_cookie =
137
xcb_randr_get_screen_resources_current_unchecked(conn, iter.data->root);
138
xcb_randr_get_screen_resources_current_reply_t *gsr_reply =
139
xcb_randr_get_screen_resources_current_reply(conn, gsr_cookie, NULL);
140
141
if (!gsr_reply || gsr_reply->num_outputs == 0) {
142
free(gsr_reply);
143
return false;
144
}
145
146
xcb_randr_output_t *randr_outputs =
147
xcb_randr_get_screen_resources_current_outputs(gsr_reply);
148
xcb_randr_get_output_info_cookie_t goi_cookie =
149
xcb_randr_get_output_info(conn, randr_outputs[0], gsr_reply->config_timestamp);
150
free(gsr_reply);
151
152
xcb_randr_get_output_info_reply_t *goi_reply =
153
xcb_randr_get_output_info_reply(conn, goi_cookie, NULL);
154
if (!goi_reply) {
155
return false;
156
}
157
158
char *output_name = (char*)xcb_randr_get_output_info_name(goi_reply);
159
bool is_xwayland = output_name && strncmp(output_name, "XWAYLAND", 8) == 0;
160
free(goi_reply);
161
162
return is_xwayland;
163
}
164
165
static struct wsi_x11_connection *
166
wsi_x11_connection_create(struct wsi_device *wsi_dev,
167
xcb_connection_t *conn)
168
{
169
xcb_query_extension_cookie_t dri3_cookie, pres_cookie, randr_cookie, amd_cookie, nv_cookie;
170
xcb_query_extension_reply_t *dri3_reply, *pres_reply, *randr_reply, *amd_reply, *nv_reply;
171
bool has_dri3_v1_2 = false;
172
bool has_present_v1_2 = false;
173
174
struct wsi_x11_connection *wsi_conn =
175
vk_alloc(&wsi_dev->instance_alloc, sizeof(*wsi_conn), 8,
176
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
177
if (!wsi_conn)
178
return NULL;
179
180
dri3_cookie = xcb_query_extension(conn, 4, "DRI3");
181
pres_cookie = xcb_query_extension(conn, 7, "Present");
182
randr_cookie = xcb_query_extension(conn, 5, "RANDR");
183
184
/* We try to be nice to users and emit a warning if they try to use a
185
* Vulkan application on a system without DRI3 enabled. However, this ends
186
* up spewing the warning when a user has, for example, both Intel
187
* integrated graphics and a discrete card with proprietary drivers and are
188
* running on the discrete card with the proprietary DDX. In this case, we
189
* really don't want to print the warning because it just confuses users.
190
* As a heuristic to detect this case, we check for a couple of proprietary
191
* X11 extensions.
192
*/
193
amd_cookie = xcb_query_extension(conn, 11, "ATIFGLRXDRI");
194
nv_cookie = xcb_query_extension(conn, 10, "NV-CONTROL");
195
196
dri3_reply = xcb_query_extension_reply(conn, dri3_cookie, NULL);
197
pres_reply = xcb_query_extension_reply(conn, pres_cookie, NULL);
198
randr_reply = xcb_query_extension_reply(conn, randr_cookie, NULL);
199
amd_reply = xcb_query_extension_reply(conn, amd_cookie, NULL);
200
nv_reply = xcb_query_extension_reply(conn, nv_cookie, NULL);
201
if (!dri3_reply || !pres_reply) {
202
free(dri3_reply);
203
free(pres_reply);
204
free(randr_reply);
205
free(amd_reply);
206
free(nv_reply);
207
vk_free(&wsi_dev->instance_alloc, wsi_conn);
208
return NULL;
209
}
210
211
wsi_conn->has_dri3 = dri3_reply->present != 0;
212
#ifdef HAVE_DRI3_MODIFIERS
213
if (wsi_conn->has_dri3) {
214
xcb_dri3_query_version_cookie_t ver_cookie;
215
xcb_dri3_query_version_reply_t *ver_reply;
216
217
ver_cookie = xcb_dri3_query_version(conn, 1, 2);
218
ver_reply = xcb_dri3_query_version_reply(conn, ver_cookie, NULL);
219
has_dri3_v1_2 =
220
(ver_reply->major_version > 1 || ver_reply->minor_version >= 2);
221
free(ver_reply);
222
}
223
#endif
224
225
wsi_conn->has_present = pres_reply->present != 0;
226
#ifdef HAVE_DRI3_MODIFIERS
227
if (wsi_conn->has_present) {
228
xcb_present_query_version_cookie_t ver_cookie;
229
xcb_present_query_version_reply_t *ver_reply;
230
231
ver_cookie = xcb_present_query_version(conn, 1, 2);
232
ver_reply = xcb_present_query_version_reply(conn, ver_cookie, NULL);
233
has_present_v1_2 =
234
(ver_reply->major_version > 1 || ver_reply->minor_version >= 2);
235
free(ver_reply);
236
}
237
#endif
238
239
if (randr_reply && randr_reply->present != 0)
240
wsi_conn->is_xwayland = wsi_x11_detect_xwayland(conn);
241
else
242
wsi_conn->is_xwayland = false;
243
244
wsi_conn->has_dri3_modifiers = has_dri3_v1_2 && has_present_v1_2;
245
wsi_conn->is_proprietary_x11 = false;
246
if (amd_reply && amd_reply->present)
247
wsi_conn->is_proprietary_x11 = true;
248
if (nv_reply && nv_reply->present)
249
wsi_conn->is_proprietary_x11 = true;
250
251
free(dri3_reply);
252
free(pres_reply);
253
free(randr_reply);
254
free(amd_reply);
255
free(nv_reply);
256
257
return wsi_conn;
258
}
259
260
static void
261
wsi_x11_connection_destroy(struct wsi_device *wsi_dev,
262
struct wsi_x11_connection *conn)
263
{
264
vk_free(&wsi_dev->instance_alloc, conn);
265
}
266
267
static bool
268
wsi_x11_check_for_dri3(struct wsi_x11_connection *wsi_conn)
269
{
270
if (wsi_conn->has_dri3)
271
return true;
272
if (!wsi_conn->is_proprietary_x11) {
273
fprintf(stderr, "vulkan: No DRI3 support detected - required for presentation\n"
274
"Note: you can probably enable DRI3 in your Xorg config\n");
275
}
276
return false;
277
}
278
279
static struct wsi_x11_connection *
280
wsi_x11_get_connection(struct wsi_device *wsi_dev,
281
xcb_connection_t *conn)
282
{
283
struct wsi_x11 *wsi =
284
(struct wsi_x11 *)wsi_dev->wsi[VK_ICD_WSI_PLATFORM_XCB];
285
286
pthread_mutex_lock(&wsi->mutex);
287
288
struct hash_entry *entry = _mesa_hash_table_search(wsi->connections, conn);
289
if (!entry) {
290
/* We're about to make a bunch of blocking calls. Let's drop the
291
* mutex for now so we don't block up too badly.
292
*/
293
pthread_mutex_unlock(&wsi->mutex);
294
295
struct wsi_x11_connection *wsi_conn =
296
wsi_x11_connection_create(wsi_dev, conn);
297
if (!wsi_conn)
298
return NULL;
299
300
pthread_mutex_lock(&wsi->mutex);
301
302
entry = _mesa_hash_table_search(wsi->connections, conn);
303
if (entry) {
304
/* Oops, someone raced us to it */
305
wsi_x11_connection_destroy(wsi_dev, wsi_conn);
306
} else {
307
entry = _mesa_hash_table_insert(wsi->connections, conn, wsi_conn);
308
}
309
}
310
311
pthread_mutex_unlock(&wsi->mutex);
312
313
return entry->data;
314
}
315
316
static const VkFormat formats[] = {
317
VK_FORMAT_B8G8R8A8_SRGB,
318
VK_FORMAT_B8G8R8A8_UNORM,
319
};
320
321
static const VkPresentModeKHR present_modes[] = {
322
VK_PRESENT_MODE_IMMEDIATE_KHR,
323
VK_PRESENT_MODE_MAILBOX_KHR,
324
VK_PRESENT_MODE_FIFO_KHR,
325
VK_PRESENT_MODE_FIFO_RELAXED_KHR,
326
};
327
328
static xcb_screen_t *
329
get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
330
{
331
xcb_screen_iterator_t screen_iter =
332
xcb_setup_roots_iterator(xcb_get_setup(conn));
333
334
for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
335
if (screen_iter.data->root == root)
336
return screen_iter.data;
337
}
338
339
return NULL;
340
}
341
342
static xcb_visualtype_t *
343
screen_get_visualtype(xcb_screen_t *screen, xcb_visualid_t visual_id,
344
unsigned *depth)
345
{
346
xcb_depth_iterator_t depth_iter =
347
xcb_screen_allowed_depths_iterator(screen);
348
349
for (; depth_iter.rem; xcb_depth_next (&depth_iter)) {
350
xcb_visualtype_iterator_t visual_iter =
351
xcb_depth_visuals_iterator (depth_iter.data);
352
353
for (; visual_iter.rem; xcb_visualtype_next (&visual_iter)) {
354
if (visual_iter.data->visual_id == visual_id) {
355
if (depth)
356
*depth = depth_iter.data->depth;
357
return visual_iter.data;
358
}
359
}
360
}
361
362
return NULL;
363
}
364
365
static xcb_visualtype_t *
366
connection_get_visualtype(xcb_connection_t *conn, xcb_visualid_t visual_id,
367
unsigned *depth)
368
{
369
xcb_screen_iterator_t screen_iter =
370
xcb_setup_roots_iterator(xcb_get_setup(conn));
371
372
/* For this we have to iterate over all of the screens which is rather
373
* annoying. Fortunately, there is probably only 1.
374
*/
375
for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
376
xcb_visualtype_t *visual = screen_get_visualtype(screen_iter.data,
377
visual_id, depth);
378
if (visual)
379
return visual;
380
}
381
382
return NULL;
383
}
384
385
static xcb_visualtype_t *
386
get_visualtype_for_window(xcb_connection_t *conn, xcb_window_t window,
387
unsigned *depth)
388
{
389
xcb_query_tree_cookie_t tree_cookie;
390
xcb_get_window_attributes_cookie_t attrib_cookie;
391
xcb_query_tree_reply_t *tree;
392
xcb_get_window_attributes_reply_t *attrib;
393
394
tree_cookie = xcb_query_tree(conn, window);
395
attrib_cookie = xcb_get_window_attributes(conn, window);
396
397
tree = xcb_query_tree_reply(conn, tree_cookie, NULL);
398
attrib = xcb_get_window_attributes_reply(conn, attrib_cookie, NULL);
399
if (attrib == NULL || tree == NULL) {
400
free(attrib);
401
free(tree);
402
return NULL;
403
}
404
405
xcb_window_t root = tree->root;
406
xcb_visualid_t visual_id = attrib->visual;
407
free(attrib);
408
free(tree);
409
410
xcb_screen_t *screen = get_screen_for_root(conn, root);
411
if (screen == NULL)
412
return NULL;
413
414
return screen_get_visualtype(screen, visual_id, depth);
415
}
416
417
static bool
418
visual_has_alpha(xcb_visualtype_t *visual, unsigned depth)
419
{
420
uint32_t rgb_mask = visual->red_mask |
421
visual->green_mask |
422
visual->blue_mask;
423
424
uint32_t all_mask = 0xffffffff >> (32 - depth);
425
426
/* Do we have bits left over after RGB? */
427
return (all_mask & ~rgb_mask) != 0;
428
}
429
430
VkBool32 wsi_get_physical_device_xcb_presentation_support(
431
struct wsi_device *wsi_device,
432
uint32_t queueFamilyIndex,
433
xcb_connection_t* connection,
434
xcb_visualid_t visual_id)
435
{
436
struct wsi_x11_connection *wsi_conn =
437
wsi_x11_get_connection(wsi_device, connection);
438
439
if (!wsi_conn)
440
return false;
441
442
if (!wsi_device->sw) {
443
if (!wsi_x11_check_for_dri3(wsi_conn))
444
return false;
445
}
446
447
unsigned visual_depth;
448
if (!connection_get_visualtype(connection, visual_id, &visual_depth))
449
return false;
450
451
if (visual_depth != 24 && visual_depth != 32)
452
return false;
453
454
return true;
455
}
456
457
static xcb_connection_t*
458
x11_surface_get_connection(VkIcdSurfaceBase *icd_surface)
459
{
460
if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
461
return XGetXCBConnection(((VkIcdSurfaceXlib *)icd_surface)->dpy);
462
else
463
return ((VkIcdSurfaceXcb *)icd_surface)->connection;
464
}
465
466
static xcb_window_t
467
x11_surface_get_window(VkIcdSurfaceBase *icd_surface)
468
{
469
if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
470
return ((VkIcdSurfaceXlib *)icd_surface)->window;
471
else
472
return ((VkIcdSurfaceXcb *)icd_surface)->window;
473
}
474
475
static VkResult
476
x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
477
struct wsi_device *wsi_device,
478
uint32_t queueFamilyIndex,
479
VkBool32* pSupported)
480
{
481
xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
482
xcb_window_t window = x11_surface_get_window(icd_surface);
483
484
struct wsi_x11_connection *wsi_conn =
485
wsi_x11_get_connection(wsi_device, conn);
486
if (!wsi_conn)
487
return VK_ERROR_OUT_OF_HOST_MEMORY;
488
489
if (!wsi_device->sw) {
490
if (!wsi_x11_check_for_dri3(wsi_conn)) {
491
*pSupported = false;
492
return VK_SUCCESS;
493
}
494
}
495
496
unsigned visual_depth;
497
if (!get_visualtype_for_window(conn, window, &visual_depth)) {
498
*pSupported = false;
499
return VK_SUCCESS;
500
}
501
502
if (visual_depth != 24 && visual_depth != 32) {
503
*pSupported = false;
504
return VK_SUCCESS;
505
}
506
507
*pSupported = true;
508
return VK_SUCCESS;
509
}
510
511
static uint32_t
512
x11_get_min_image_count(struct wsi_device *wsi_device)
513
{
514
if (wsi_device->x11.override_minImageCount)
515
return wsi_device->x11.override_minImageCount;
516
517
/* For IMMEDIATE and FIFO, most games work in a pipelined manner where the
518
* can produce frames at a rate of 1/MAX(CPU duration, GPU duration), but
519
* the render latency is CPU duration + GPU duration.
520
*
521
* This means that with scanout from pageflipping we need 3 frames to run
522
* full speed:
523
* 1) CPU rendering work
524
* 2) GPU rendering work
525
* 3) scanout
526
*
527
* Once we have a nonblocking acquire that returns a semaphore we can merge
528
* 1 and 3. Hence the ideal implementation needs only 2 images, but games
529
* cannot tellwe currently do not have an ideal implementation and that
530
* hence they need to allocate 3 images. So let us do it for them.
531
*
532
* This is a tradeoff as it uses more memory than needed for non-fullscreen
533
* and non-performance intensive applications.
534
*/
535
return 3;
536
}
537
538
static VkResult
539
x11_surface_get_capabilities(VkIcdSurfaceBase *icd_surface,
540
struct wsi_device *wsi_device,
541
VkSurfaceCapabilitiesKHR *caps)
542
{
543
xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
544
xcb_window_t window = x11_surface_get_window(icd_surface);
545
xcb_get_geometry_cookie_t geom_cookie;
546
xcb_generic_error_t *err;
547
xcb_get_geometry_reply_t *geom;
548
unsigned visual_depth;
549
550
geom_cookie = xcb_get_geometry(conn, window);
551
552
/* This does a round-trip. This is why we do get_geometry first and
553
* wait to read the reply until after we have a visual.
554
*/
555
xcb_visualtype_t *visual =
556
get_visualtype_for_window(conn, window, &visual_depth);
557
558
if (!visual)
559
return VK_ERROR_SURFACE_LOST_KHR;
560
561
geom = xcb_get_geometry_reply(conn, geom_cookie, &err);
562
if (geom) {
563
VkExtent2D extent = { geom->width, geom->height };
564
caps->currentExtent = extent;
565
caps->minImageExtent = extent;
566
caps->maxImageExtent = extent;
567
} else {
568
/* This can happen if the client didn't wait for the configure event
569
* to come back from the compositor. In that case, we don't know the
570
* size of the window so we just return valid "I don't know" stuff.
571
*/
572
caps->currentExtent = (VkExtent2D) { UINT32_MAX, UINT32_MAX };
573
caps->minImageExtent = (VkExtent2D) { 1, 1 };
574
caps->maxImageExtent = (VkExtent2D) {
575
wsi_device->maxImageDimension2D,
576
wsi_device->maxImageDimension2D,
577
};
578
}
579
free(err);
580
free(geom);
581
582
if (visual_has_alpha(visual, visual_depth)) {
583
caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
584
VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
585
} else {
586
caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
587
VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
588
}
589
590
caps->minImageCount = x11_get_min_image_count(wsi_device);
591
/* There is no real maximum */
592
caps->maxImageCount = 0;
593
594
caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
595
caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
596
caps->maxImageArrayLayers = 1;
597
caps->supportedUsageFlags =
598
VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
599
VK_IMAGE_USAGE_SAMPLED_BIT |
600
VK_IMAGE_USAGE_TRANSFER_DST_BIT |
601
VK_IMAGE_USAGE_STORAGE_BIT |
602
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
603
604
return VK_SUCCESS;
605
}
606
607
static VkResult
608
x11_surface_get_capabilities2(VkIcdSurfaceBase *icd_surface,
609
struct wsi_device *wsi_device,
610
const void *info_next,
611
VkSurfaceCapabilities2KHR *caps)
612
{
613
assert(caps->sType == VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR);
614
615
VkResult result =
616
x11_surface_get_capabilities(icd_surface, wsi_device,
617
&caps->surfaceCapabilities);
618
619
vk_foreach_struct(ext, caps->pNext) {
620
switch (ext->sType) {
621
case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
622
VkSurfaceProtectedCapabilitiesKHR *protected = (void *)ext;
623
protected->supportsProtected = VK_FALSE;
624
break;
625
}
626
627
default:
628
/* Ignored */
629
break;
630
}
631
}
632
633
return result;
634
}
635
636
static void
637
get_sorted_vk_formats(struct wsi_device *wsi_device, VkFormat *sorted_formats)
638
{
639
memcpy(sorted_formats, formats, sizeof(formats));
640
641
if (wsi_device->force_bgra8_unorm_first) {
642
for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
643
if (sorted_formats[i] == VK_FORMAT_B8G8R8A8_UNORM) {
644
sorted_formats[i] = sorted_formats[0];
645
sorted_formats[0] = VK_FORMAT_B8G8R8A8_UNORM;
646
break;
647
}
648
}
649
}
650
}
651
652
static VkResult
653
x11_surface_get_formats(VkIcdSurfaceBase *surface,
654
struct wsi_device *wsi_device,
655
uint32_t *pSurfaceFormatCount,
656
VkSurfaceFormatKHR *pSurfaceFormats)
657
{
658
VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
659
660
VkFormat sorted_formats[ARRAY_SIZE(formats)];
661
get_sorted_vk_formats(wsi_device, sorted_formats);
662
663
for (unsigned i = 0; i < ARRAY_SIZE(sorted_formats); i++) {
664
vk_outarray_append(&out, f) {
665
f->format = sorted_formats[i];
666
f->colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
667
}
668
}
669
670
return vk_outarray_status(&out);
671
}
672
673
static VkResult
674
x11_surface_get_formats2(VkIcdSurfaceBase *surface,
675
struct wsi_device *wsi_device,
676
const void *info_next,
677
uint32_t *pSurfaceFormatCount,
678
VkSurfaceFormat2KHR *pSurfaceFormats)
679
{
680
VK_OUTARRAY_MAKE(out, pSurfaceFormats, pSurfaceFormatCount);
681
682
VkFormat sorted_formats[ARRAY_SIZE(formats)];
683
get_sorted_vk_formats(wsi_device, sorted_formats);
684
685
for (unsigned i = 0; i < ARRAY_SIZE(sorted_formats); i++) {
686
vk_outarray_append(&out, f) {
687
assert(f->sType == VK_STRUCTURE_TYPE_SURFACE_FORMAT_2_KHR);
688
f->surfaceFormat.format = sorted_formats[i];
689
f->surfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
690
}
691
}
692
693
return vk_outarray_status(&out);
694
}
695
696
static VkResult
697
x11_surface_get_present_modes(VkIcdSurfaceBase *surface,
698
uint32_t *pPresentModeCount,
699
VkPresentModeKHR *pPresentModes)
700
{
701
if (pPresentModes == NULL) {
702
*pPresentModeCount = ARRAY_SIZE(present_modes);
703
return VK_SUCCESS;
704
}
705
706
*pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes));
707
typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
708
709
return *pPresentModeCount < ARRAY_SIZE(present_modes) ?
710
VK_INCOMPLETE : VK_SUCCESS;
711
}
712
713
static VkResult
714
x11_surface_get_present_rectangles(VkIcdSurfaceBase *icd_surface,
715
struct wsi_device *wsi_device,
716
uint32_t* pRectCount,
717
VkRect2D* pRects)
718
{
719
xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
720
xcb_window_t window = x11_surface_get_window(icd_surface);
721
VK_OUTARRAY_MAKE(out, pRects, pRectCount);
722
723
vk_outarray_append(&out, rect) {
724
xcb_generic_error_t *err = NULL;
725
xcb_get_geometry_cookie_t geom_cookie = xcb_get_geometry(conn, window);
726
xcb_get_geometry_reply_t *geom =
727
xcb_get_geometry_reply(conn, geom_cookie, &err);
728
free(err);
729
if (geom) {
730
*rect = (VkRect2D) {
731
.offset = { 0, 0 },
732
.extent = { geom->width, geom->height },
733
};
734
} else {
735
/* This can happen if the client didn't wait for the configure event
736
* to come back from the compositor. In that case, we don't know the
737
* size of the window so we just return valid "I don't know" stuff.
738
*/
739
*rect = (VkRect2D) {
740
.offset = { 0, 0 },
741
.extent = { UINT32_MAX, UINT32_MAX },
742
};
743
}
744
free(geom);
745
}
746
747
return vk_outarray_status(&out);
748
}
749
750
VkResult wsi_create_xcb_surface(const VkAllocationCallbacks *pAllocator,
751
const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
752
VkSurfaceKHR *pSurface)
753
{
754
VkIcdSurfaceXcb *surface;
755
756
surface = vk_alloc(pAllocator, sizeof *surface, 8,
757
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
758
if (surface == NULL)
759
return VK_ERROR_OUT_OF_HOST_MEMORY;
760
761
surface->base.platform = VK_ICD_WSI_PLATFORM_XCB;
762
surface->connection = pCreateInfo->connection;
763
surface->window = pCreateInfo->window;
764
765
*pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
766
return VK_SUCCESS;
767
}
768
769
VkResult wsi_create_xlib_surface(const VkAllocationCallbacks *pAllocator,
770
const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
771
VkSurfaceKHR *pSurface)
772
{
773
VkIcdSurfaceXlib *surface;
774
775
surface = vk_alloc(pAllocator, sizeof *surface, 8,
776
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
777
if (surface == NULL)
778
return VK_ERROR_OUT_OF_HOST_MEMORY;
779
780
surface->base.platform = VK_ICD_WSI_PLATFORM_XLIB;
781
surface->dpy = pCreateInfo->dpy;
782
surface->window = pCreateInfo->window;
783
784
*pSurface = VkIcdSurfaceBase_to_handle(&surface->base);
785
return VK_SUCCESS;
786
}
787
788
struct x11_image {
789
struct wsi_image base;
790
xcb_pixmap_t pixmap;
791
bool busy;
792
bool present_queued;
793
struct xshmfence * shm_fence;
794
uint32_t sync_fence;
795
uint32_t serial;
796
};
797
798
struct x11_swapchain {
799
struct wsi_swapchain base;
800
801
bool has_dri3_modifiers;
802
803
xcb_connection_t * conn;
804
xcb_window_t window;
805
xcb_gc_t gc;
806
uint32_t depth;
807
VkExtent2D extent;
808
809
xcb_present_event_t event_id;
810
xcb_special_event_t * special_event;
811
uint64_t send_sbc;
812
uint64_t last_present_msc;
813
uint32_t stamp;
814
int sent_image_count;
815
816
bool has_present_queue;
817
bool has_acquire_queue;
818
VkResult status;
819
bool copy_is_suboptimal;
820
struct wsi_queue present_queue;
821
struct wsi_queue acquire_queue;
822
pthread_t queue_manager;
823
824
struct x11_image images[0];
825
};
826
VK_DEFINE_NONDISP_HANDLE_CASTS(x11_swapchain, base.base, VkSwapchainKHR,
827
VK_OBJECT_TYPE_SWAPCHAIN_KHR)
828
829
/**
830
* Update the swapchain status with the result of an operation, and return
831
* the combined status. The chain status will eventually be returned from
832
* AcquireNextImage and QueuePresent.
833
*
834
* We make sure to 'stick' more pessimistic statuses: an out-of-date error
835
* is permanent once seen, and every subsequent call will return this. If
836
* this has not been seen, success will be returned.
837
*/
838
static VkResult
839
_x11_swapchain_result(struct x11_swapchain *chain, VkResult result,
840
const char *file, int line)
841
{
842
/* Prioritise returning existing errors for consistency. */
843
if (chain->status < 0)
844
return chain->status;
845
846
/* If we have a new error, mark it as permanent on the chain and return. */
847
if (result < 0) {
848
#ifndef NDEBUG
849
fprintf(stderr, "%s:%d: Swapchain status changed to %s\n",
850
file, line, vk_Result_to_str(result));
851
#endif
852
chain->status = result;
853
return result;
854
}
855
856
/* Return temporary errors, but don't persist them. */
857
if (result == VK_TIMEOUT || result == VK_NOT_READY)
858
return result;
859
860
/* Suboptimal isn't an error, but is a status which sticks to the swapchain
861
* and is always returned rather than success.
862
*/
863
if (result == VK_SUBOPTIMAL_KHR) {
864
#ifndef NDEBUG
865
if (chain->status != VK_SUBOPTIMAL_KHR) {
866
fprintf(stderr, "%s:%d: Swapchain status changed to %s\n",
867
file, line, vk_Result_to_str(result));
868
}
869
#endif
870
chain->status = result;
871
return result;
872
}
873
874
/* No changes, so return the last status. */
875
return chain->status;
876
}
877
#define x11_swapchain_result(chain, result) \
878
_x11_swapchain_result(chain, result, __FILE__, __LINE__)
879
880
static struct wsi_image *
881
x11_get_wsi_image(struct wsi_swapchain *wsi_chain, uint32_t image_index)
882
{
883
struct x11_swapchain *chain = (struct x11_swapchain *)wsi_chain;
884
return &chain->images[image_index].base;
885
}
886
887
/**
888
* Process an X11 Present event. Does not update chain->status.
889
*/
890
static VkResult
891
x11_handle_dri3_present_event(struct x11_swapchain *chain,
892
xcb_present_generic_event_t *event)
893
{
894
switch (event->evtype) {
895
case XCB_PRESENT_CONFIGURE_NOTIFY: {
896
xcb_present_configure_notify_event_t *config = (void *) event;
897
898
if (config->width != chain->extent.width ||
899
config->height != chain->extent.height)
900
return VK_SUBOPTIMAL_KHR;
901
902
break;
903
}
904
905
case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
906
xcb_present_idle_notify_event_t *idle = (void *) event;
907
908
for (unsigned i = 0; i < chain->base.image_count; i++) {
909
if (chain->images[i].pixmap == idle->pixmap) {
910
chain->images[i].busy = false;
911
chain->sent_image_count--;
912
assert(chain->sent_image_count >= 0);
913
if (chain->has_acquire_queue)
914
wsi_queue_push(&chain->acquire_queue, i);
915
break;
916
}
917
}
918
919
break;
920
}
921
922
case XCB_PRESENT_EVENT_COMPLETE_NOTIFY: {
923
xcb_present_complete_notify_event_t *complete = (void *) event;
924
if (complete->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP) {
925
unsigned i;
926
for (i = 0; i < chain->base.image_count; i++) {
927
struct x11_image *image = &chain->images[i];
928
if (image->present_queued && image->serial == complete->serial)
929
image->present_queued = false;
930
}
931
chain->last_present_msc = complete->msc;
932
}
933
934
VkResult result = VK_SUCCESS;
935
switch (complete->mode) {
936
case XCB_PRESENT_COMPLETE_MODE_COPY:
937
if (chain->copy_is_suboptimal)
938
result = VK_SUBOPTIMAL_KHR;
939
break;
940
case XCB_PRESENT_COMPLETE_MODE_FLIP:
941
/* If we ever go from flipping to copying, the odds are very likely
942
* that we could reallocate in a more optimal way if we didn't have
943
* to care about scanout, so we always do this.
944
*/
945
chain->copy_is_suboptimal = true;
946
break;
947
#ifdef HAVE_DRI3_MODIFIERS
948
case XCB_PRESENT_COMPLETE_MODE_SUBOPTIMAL_COPY:
949
/* The winsys is now trying to flip directly and cannot due to our
950
* configuration. Request the user reallocate.
951
*/
952
result = VK_SUBOPTIMAL_KHR;
953
break;
954
#endif
955
default:
956
break;
957
}
958
959
return result;
960
}
961
962
default:
963
break;
964
}
965
966
return VK_SUCCESS;
967
}
968
969
970
static uint64_t wsi_get_absolute_timeout(uint64_t timeout)
971
{
972
uint64_t current_time = wsi_common_get_current_time();
973
974
timeout = MIN2(UINT64_MAX - current_time, timeout);
975
976
return current_time + timeout;
977
}
978
979
static VkResult
980
x11_acquire_next_image_poll_x11(struct x11_swapchain *chain,
981
uint32_t *image_index, uint64_t timeout)
982
{
983
xcb_generic_event_t *event;
984
struct pollfd pfds;
985
uint64_t atimeout;
986
while (1) {
987
for (uint32_t i = 0; i < chain->base.image_count; i++) {
988
if (!chain->images[i].busy) {
989
/* We found a non-busy image */
990
xshmfence_await(chain->images[i].shm_fence);
991
*image_index = i;
992
chain->images[i].busy = true;
993
return x11_swapchain_result(chain, VK_SUCCESS);
994
}
995
}
996
997
xcb_flush(chain->conn);
998
999
if (timeout == UINT64_MAX) {
1000
event = xcb_wait_for_special_event(chain->conn, chain->special_event);
1001
if (!event)
1002
return x11_swapchain_result(chain, VK_ERROR_OUT_OF_DATE_KHR);
1003
} else {
1004
event = xcb_poll_for_special_event(chain->conn, chain->special_event);
1005
if (!event) {
1006
int ret;
1007
if (timeout == 0)
1008
return x11_swapchain_result(chain, VK_NOT_READY);
1009
1010
atimeout = wsi_get_absolute_timeout(timeout);
1011
1012
pfds.fd = xcb_get_file_descriptor(chain->conn);
1013
pfds.events = POLLIN;
1014
ret = poll(&pfds, 1, timeout / 1000 / 1000);
1015
if (ret == 0)
1016
return x11_swapchain_result(chain, VK_TIMEOUT);
1017
if (ret == -1)
1018
return x11_swapchain_result(chain, VK_ERROR_OUT_OF_DATE_KHR);
1019
1020
/* If a non-special event happens, the fd will still
1021
* poll. So recalculate the timeout now just in case.
1022
*/
1023
uint64_t current_time = wsi_common_get_current_time();
1024
if (atimeout > current_time)
1025
timeout = atimeout - current_time;
1026
else
1027
timeout = 0;
1028
continue;
1029
}
1030
}
1031
1032
/* Update the swapchain status here. We may catch non-fatal errors here,
1033
* in which case we need to update the status and continue.
1034
*/
1035
VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
1036
free(event);
1037
if (result < 0)
1038
return x11_swapchain_result(chain, result);
1039
}
1040
}
1041
1042
static VkResult
1043
x11_acquire_next_image_from_queue(struct x11_swapchain *chain,
1044
uint32_t *image_index_out, uint64_t timeout)
1045
{
1046
assert(chain->has_acquire_queue);
1047
1048
uint32_t image_index;
1049
VkResult result = wsi_queue_pull(&chain->acquire_queue,
1050
&image_index, timeout);
1051
if (result < 0 || result == VK_TIMEOUT) {
1052
/* On error, the thread has shut down, so safe to update chain->status.
1053
* Calling x11_swapchain_result with VK_TIMEOUT won't modify
1054
* chain->status so that is also safe.
1055
*/
1056
return x11_swapchain_result(chain, result);
1057
} else if (chain->status < 0) {
1058
return chain->status;
1059
}
1060
1061
assert(image_index < chain->base.image_count);
1062
xshmfence_await(chain->images[image_index].shm_fence);
1063
1064
*image_index_out = image_index;
1065
1066
return chain->status;
1067
}
1068
1069
static VkResult
1070
x11_present_to_x11_dri3(struct x11_swapchain *chain, uint32_t image_index,
1071
uint64_t target_msc)
1072
{
1073
struct x11_image *image = &chain->images[image_index];
1074
1075
assert(image_index < chain->base.image_count);
1076
1077
uint32_t options = XCB_PRESENT_OPTION_NONE;
1078
1079
int64_t divisor = 0;
1080
int64_t remainder = 0;
1081
1082
struct wsi_x11_connection *wsi_conn =
1083
wsi_x11_get_connection((struct wsi_device*)chain->base.wsi, chain->conn);
1084
if (!wsi_conn)
1085
return VK_ERROR_OUT_OF_HOST_MEMORY;
1086
1087
if (chain->base.present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR ||
1088
(chain->base.present_mode == VK_PRESENT_MODE_MAILBOX_KHR &&
1089
wsi_conn->is_xwayland) ||
1090
chain->base.present_mode == VK_PRESENT_MODE_FIFO_RELAXED_KHR)
1091
options |= XCB_PRESENT_OPTION_ASYNC;
1092
1093
#ifdef HAVE_DRI3_MODIFIERS
1094
if (chain->has_dri3_modifiers)
1095
options |= XCB_PRESENT_OPTION_SUBOPTIMAL;
1096
#endif
1097
1098
/* Poll for any available event and update the swapchain status. This could
1099
* update the status of the swapchain to SUBOPTIMAL or OUT_OF_DATE if the
1100
* associated X11 surface has been resized.
1101
*/
1102
xcb_generic_event_t *event;
1103
while ((event = xcb_poll_for_special_event(chain->conn, chain->special_event))) {
1104
VkResult result = x11_handle_dri3_present_event(chain, (void *)event);
1105
free(event);
1106
if (result < 0)
1107
return x11_swapchain_result(chain, result);
1108
x11_swapchain_result(chain, result);
1109
}
1110
1111
xshmfence_reset(image->shm_fence);
1112
1113
++chain->sent_image_count;
1114
assert(chain->sent_image_count <= chain->base.image_count);
1115
1116
++chain->send_sbc;
1117
image->present_queued = true;
1118
image->serial = (uint32_t) chain->send_sbc;
1119
1120
xcb_void_cookie_t cookie =
1121
xcb_present_pixmap(chain->conn,
1122
chain->window,
1123
image->pixmap,
1124
image->serial,
1125
0, /* valid */
1126
0, /* update */
1127
0, /* x_off */
1128
0, /* y_off */
1129
XCB_NONE, /* target_crtc */
1130
XCB_NONE,
1131
image->sync_fence,
1132
options,
1133
target_msc,
1134
divisor,
1135
remainder, 0, NULL);
1136
xcb_discard_reply(chain->conn, cookie.sequence);
1137
1138
xcb_flush(chain->conn);
1139
1140
return x11_swapchain_result(chain, VK_SUCCESS);
1141
}
1142
1143
static VkResult
1144
x11_present_to_x11_sw(struct x11_swapchain *chain, uint32_t image_index,
1145
uint64_t target_msc)
1146
{
1147
struct x11_image *image = &chain->images[image_index];
1148
1149
xcb_void_cookie_t cookie;
1150
void *myptr;
1151
chain->base.wsi->MapMemory(chain->base.device,
1152
image->base.memory,
1153
0, 0, 0, &myptr);
1154
1155
cookie = xcb_put_image(chain->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
1156
chain->window,
1157
chain->gc,
1158
image->base.row_pitches[0] / 4,
1159
chain->extent.height,
1160
0,0,0,24,
1161
image->base.row_pitches[0] * chain->extent.height,
1162
myptr);
1163
1164
chain->base.wsi->UnmapMemory(chain->base.device, image->base.memory);
1165
xcb_discard_reply(chain->conn, cookie.sequence);
1166
xcb_flush(chain->conn);
1167
return x11_swapchain_result(chain, VK_SUCCESS);
1168
}
1169
static VkResult
1170
x11_present_to_x11(struct x11_swapchain *chain, uint32_t image_index,
1171
uint64_t target_msc)
1172
{
1173
if (chain->base.wsi->sw)
1174
return x11_present_to_x11_sw(chain, image_index, target_msc);
1175
return x11_present_to_x11_dri3(chain, image_index, target_msc);
1176
}
1177
1178
static VkResult
1179
x11_acquire_next_image(struct wsi_swapchain *anv_chain,
1180
const VkAcquireNextImageInfoKHR *info,
1181
uint32_t *image_index)
1182
{
1183
struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
1184
uint64_t timeout = info->timeout;
1185
1186
/* If the swapchain is in an error state, don't go any further. */
1187
if (chain->status < 0)
1188
return chain->status;
1189
1190
if (chain->base.wsi->sw) {
1191
*image_index = 0;
1192
return VK_SUCCESS;
1193
}
1194
if (chain->has_acquire_queue) {
1195
return x11_acquire_next_image_from_queue(chain, image_index, timeout);
1196
} else {
1197
return x11_acquire_next_image_poll_x11(chain, image_index, timeout);
1198
}
1199
}
1200
1201
static VkResult
1202
x11_queue_present(struct wsi_swapchain *anv_chain,
1203
uint32_t image_index,
1204
const VkPresentRegionKHR *damage)
1205
{
1206
struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
1207
1208
/* If the swapchain is in an error state, don't go any further. */
1209
if (chain->status < 0)
1210
return chain->status;
1211
1212
chain->images[image_index].busy = true;
1213
if (chain->has_present_queue) {
1214
wsi_queue_push(&chain->present_queue, image_index);
1215
return chain->status;
1216
} else {
1217
return x11_present_to_x11(chain, image_index, 0);
1218
}
1219
}
1220
1221
static void *
1222
x11_manage_fifo_queues(void *state)
1223
{
1224
struct x11_swapchain *chain = state;
1225
struct wsi_x11_connection *wsi_conn =
1226
wsi_x11_get_connection((struct wsi_device*)chain->base.wsi, chain->conn);
1227
VkResult result = VK_SUCCESS;
1228
1229
assert(chain->has_present_queue);
1230
1231
u_thread_setname("WSI swapchain queue");
1232
1233
while (chain->status >= 0) {
1234
/* We can block here unconditionally because after an image was sent to
1235
* the server (later on in this loop) we ensure at least one image is
1236
* acquirable by the consumer or wait there on such an event.
1237
*/
1238
uint32_t image_index = 0;
1239
result = wsi_queue_pull(&chain->present_queue, &image_index, INT64_MAX);
1240
assert(result != VK_TIMEOUT);
1241
if (result < 0) {
1242
goto fail;
1243
} else if (chain->status < 0) {
1244
/* The status can change underneath us if the swapchain is destroyed
1245
* from another thread.
1246
*/
1247
return NULL;
1248
}
1249
1250
if (chain->base.present_mode == VK_PRESENT_MODE_MAILBOX_KHR ||
1251
(chain->base.present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR &&
1252
wsi_conn->is_xwayland)) {
1253
result = chain->base.wsi->WaitForFences(chain->base.device, 1,
1254
&chain->base.fences[image_index],
1255
true, UINT64_MAX);
1256
if (result != VK_SUCCESS) {
1257
result = VK_ERROR_OUT_OF_DATE_KHR;
1258
goto fail;
1259
}
1260
}
1261
1262
uint64_t target_msc = 0;
1263
if (chain->has_acquire_queue)
1264
target_msc = chain->last_present_msc + 1;
1265
1266
result = x11_present_to_x11(chain, image_index, target_msc);
1267
if (result < 0)
1268
goto fail;
1269
1270
if (chain->has_acquire_queue) {
1271
/* Wait for our presentation to occur and ensure we have at least one
1272
* image that can be acquired by the client afterwards. This ensures we
1273
* can pull on the present-queue on the next loop.
1274
*/
1275
while (chain->images[image_index].present_queued ||
1276
chain->sent_image_count == chain->base.image_count) {
1277
xcb_generic_event_t *event =
1278
xcb_wait_for_special_event(chain->conn, chain->special_event);
1279
if (!event) {
1280
result = VK_ERROR_OUT_OF_DATE_KHR;
1281
goto fail;
1282
}
1283
1284
result = x11_handle_dri3_present_event(chain, (void *)event);
1285
free(event);
1286
if (result < 0)
1287
goto fail;
1288
}
1289
}
1290
}
1291
1292
fail:
1293
x11_swapchain_result(chain, result);
1294
if (chain->has_acquire_queue)
1295
wsi_queue_push(&chain->acquire_queue, UINT32_MAX);
1296
1297
return NULL;
1298
}
1299
1300
static VkResult
1301
x11_image_init(VkDevice device_h, struct x11_swapchain *chain,
1302
const VkSwapchainCreateInfoKHR *pCreateInfo,
1303
const VkAllocationCallbacks* pAllocator,
1304
const uint64_t *const *modifiers,
1305
const uint32_t *num_modifiers,
1306
int num_tranches, struct x11_image *image)
1307
{
1308
xcb_void_cookie_t cookie;
1309
VkResult result;
1310
uint32_t bpp = 32;
1311
1312
if (chain->base.use_prime_blit) {
1313
bool use_modifier = num_tranches > 0;
1314
result = wsi_create_prime_image(&chain->base, pCreateInfo, use_modifier, &image->base);
1315
} else {
1316
result = wsi_create_native_image(&chain->base, pCreateInfo,
1317
num_tranches, num_modifiers, modifiers,
1318
&image->base);
1319
}
1320
if (result < 0)
1321
return result;
1322
1323
if (chain->base.wsi->sw) {
1324
image->busy = false;
1325
return VK_SUCCESS;
1326
}
1327
image->pixmap = xcb_generate_id(chain->conn);
1328
1329
#ifdef HAVE_DRI3_MODIFIERS
1330
if (image->base.drm_modifier != DRM_FORMAT_MOD_INVALID) {
1331
/* If the image has a modifier, we must have DRI3 v1.2. */
1332
assert(chain->has_dri3_modifiers);
1333
1334
cookie =
1335
xcb_dri3_pixmap_from_buffers_checked(chain->conn,
1336
image->pixmap,
1337
chain->window,
1338
image->base.num_planes,
1339
pCreateInfo->imageExtent.width,
1340
pCreateInfo->imageExtent.height,
1341
image->base.row_pitches[0],
1342
image->base.offsets[0],
1343
image->base.row_pitches[1],
1344
image->base.offsets[1],
1345
image->base.row_pitches[2],
1346
image->base.offsets[2],
1347
image->base.row_pitches[3],
1348
image->base.offsets[3],
1349
chain->depth, bpp,
1350
image->base.drm_modifier,
1351
image->base.fds);
1352
} else
1353
#endif
1354
{
1355
/* Without passing modifiers, we can't have multi-plane RGB images. */
1356
assert(image->base.num_planes == 1);
1357
1358
cookie =
1359
xcb_dri3_pixmap_from_buffer_checked(chain->conn,
1360
image->pixmap,
1361
chain->window,
1362
image->base.sizes[0],
1363
pCreateInfo->imageExtent.width,
1364
pCreateInfo->imageExtent.height,
1365
image->base.row_pitches[0],
1366
chain->depth, bpp,
1367
image->base.fds[0]);
1368
}
1369
1370
xcb_discard_reply(chain->conn, cookie.sequence);
1371
1372
/* XCB has now taken ownership of the FDs. */
1373
for (int i = 0; i < image->base.num_planes; i++)
1374
image->base.fds[i] = -1;
1375
1376
int fence_fd = xshmfence_alloc_shm();
1377
if (fence_fd < 0)
1378
goto fail_pixmap;
1379
1380
image->shm_fence = xshmfence_map_shm(fence_fd);
1381
if (image->shm_fence == NULL)
1382
goto fail_shmfence_alloc;
1383
1384
image->sync_fence = xcb_generate_id(chain->conn);
1385
xcb_dri3_fence_from_fd(chain->conn,
1386
image->pixmap,
1387
image->sync_fence,
1388
false,
1389
fence_fd);
1390
1391
image->busy = false;
1392
xshmfence_trigger(image->shm_fence);
1393
1394
return VK_SUCCESS;
1395
1396
fail_shmfence_alloc:
1397
close(fence_fd);
1398
1399
fail_pixmap:
1400
cookie = xcb_free_pixmap(chain->conn, image->pixmap);
1401
xcb_discard_reply(chain->conn, cookie.sequence);
1402
1403
wsi_destroy_image(&chain->base, &image->base);
1404
1405
return result;
1406
}
1407
1408
static void
1409
x11_image_finish(struct x11_swapchain *chain,
1410
const VkAllocationCallbacks* pAllocator,
1411
struct x11_image *image)
1412
{
1413
xcb_void_cookie_t cookie;
1414
1415
if (!chain->base.wsi->sw) {
1416
cookie = xcb_sync_destroy_fence(chain->conn, image->sync_fence);
1417
xcb_discard_reply(chain->conn, cookie.sequence);
1418
xshmfence_unmap_shm(image->shm_fence);
1419
1420
cookie = xcb_free_pixmap(chain->conn, image->pixmap);
1421
xcb_discard_reply(chain->conn, cookie.sequence);
1422
}
1423
1424
wsi_destroy_image(&chain->base, &image->base);
1425
}
1426
1427
static void
1428
wsi_x11_get_dri3_modifiers(struct wsi_x11_connection *wsi_conn,
1429
xcb_connection_t *conn, xcb_window_t window,
1430
uint8_t depth, uint8_t bpp,
1431
VkCompositeAlphaFlagsKHR vk_alpha,
1432
uint64_t **modifiers_in, uint32_t *num_modifiers_in,
1433
uint32_t *num_tranches_in,
1434
const VkAllocationCallbacks *pAllocator)
1435
{
1436
if (!wsi_conn->has_dri3_modifiers)
1437
goto out;
1438
1439
#ifdef HAVE_DRI3_MODIFIERS
1440
xcb_generic_error_t *error = NULL;
1441
xcb_dri3_get_supported_modifiers_cookie_t mod_cookie =
1442
xcb_dri3_get_supported_modifiers(conn, window, depth, bpp);
1443
xcb_dri3_get_supported_modifiers_reply_t *mod_reply =
1444
xcb_dri3_get_supported_modifiers_reply(conn, mod_cookie, &error);
1445
free(error);
1446
1447
if (!mod_reply || (mod_reply->num_window_modifiers == 0 &&
1448
mod_reply->num_screen_modifiers == 0)) {
1449
free(mod_reply);
1450
goto out;
1451
}
1452
1453
uint32_t n = 0;
1454
uint32_t counts[2];
1455
uint64_t *modifiers[2];
1456
1457
if (mod_reply->num_window_modifiers) {
1458
counts[n] = mod_reply->num_window_modifiers;
1459
modifiers[n] = vk_alloc(pAllocator,
1460
counts[n] * sizeof(uint64_t),
1461
8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1462
if (!modifiers[n]) {
1463
free(mod_reply);
1464
goto out;
1465
}
1466
1467
memcpy(modifiers[n],
1468
xcb_dri3_get_supported_modifiers_window_modifiers(mod_reply),
1469
counts[n] * sizeof(uint64_t));
1470
n++;
1471
}
1472
1473
if (mod_reply->num_screen_modifiers) {
1474
counts[n] = mod_reply->num_screen_modifiers;
1475
modifiers[n] = vk_alloc(pAllocator,
1476
counts[n] * sizeof(uint64_t),
1477
8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1478
if (!modifiers[n]) {
1479
if (n > 0)
1480
vk_free(pAllocator, modifiers[0]);
1481
free(mod_reply);
1482
goto out;
1483
}
1484
1485
memcpy(modifiers[n],
1486
xcb_dri3_get_supported_modifiers_screen_modifiers(mod_reply),
1487
counts[n] * sizeof(uint64_t));
1488
n++;
1489
}
1490
1491
for (int i = 0; i < n; i++) {
1492
modifiers_in[i] = modifiers[i];
1493
num_modifiers_in[i] = counts[i];
1494
}
1495
*num_tranches_in = n;
1496
1497
free(mod_reply);
1498
return;
1499
#endif
1500
out:
1501
*num_tranches_in = 0;
1502
}
1503
1504
static VkResult
1505
x11_swapchain_destroy(struct wsi_swapchain *anv_chain,
1506
const VkAllocationCallbacks *pAllocator)
1507
{
1508
struct x11_swapchain *chain = (struct x11_swapchain *)anv_chain;
1509
xcb_void_cookie_t cookie;
1510
1511
if (chain->has_present_queue) {
1512
chain->status = VK_ERROR_OUT_OF_DATE_KHR;
1513
/* Push a UINT32_MAX to wake up the manager */
1514
wsi_queue_push(&chain->present_queue, UINT32_MAX);
1515
pthread_join(chain->queue_manager, NULL);
1516
1517
if (chain->has_acquire_queue)
1518
wsi_queue_destroy(&chain->acquire_queue);
1519
wsi_queue_destroy(&chain->present_queue);
1520
}
1521
1522
for (uint32_t i = 0; i < chain->base.image_count; i++)
1523
x11_image_finish(chain, pAllocator, &chain->images[i]);
1524
1525
xcb_unregister_for_special_event(chain->conn, chain->special_event);
1526
cookie = xcb_present_select_input_checked(chain->conn, chain->event_id,
1527
chain->window,
1528
XCB_PRESENT_EVENT_MASK_NO_EVENT);
1529
xcb_discard_reply(chain->conn, cookie.sequence);
1530
1531
wsi_swapchain_finish(&chain->base);
1532
1533
vk_free(pAllocator, chain);
1534
1535
return VK_SUCCESS;
1536
}
1537
1538
static void
1539
wsi_x11_set_adaptive_sync_property(xcb_connection_t *conn,
1540
xcb_drawable_t drawable,
1541
uint32_t state)
1542
{
1543
static char const name[] = "_VARIABLE_REFRESH";
1544
xcb_intern_atom_cookie_t cookie;
1545
xcb_intern_atom_reply_t* reply;
1546
xcb_void_cookie_t check;
1547
1548
cookie = xcb_intern_atom(conn, 0, strlen(name), name);
1549
reply = xcb_intern_atom_reply(conn, cookie, NULL);
1550
if (reply == NULL)
1551
return;
1552
1553
if (state)
1554
check = xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE,
1555
drawable, reply->atom,
1556
XCB_ATOM_CARDINAL, 32, 1, &state);
1557
else
1558
check = xcb_delete_property_checked(conn, drawable, reply->atom);
1559
1560
xcb_discard_reply(conn, check.sequence);
1561
free(reply);
1562
}
1563
1564
1565
static VkResult
1566
x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
1567
VkDevice device,
1568
struct wsi_device *wsi_device,
1569
const VkSwapchainCreateInfoKHR *pCreateInfo,
1570
const VkAllocationCallbacks* pAllocator,
1571
struct wsi_swapchain **swapchain_out)
1572
{
1573
struct x11_swapchain *chain;
1574
xcb_void_cookie_t cookie;
1575
VkResult result;
1576
VkPresentModeKHR present_mode = wsi_swapchain_get_present_mode(wsi_device, pCreateInfo);
1577
1578
assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
1579
1580
xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
1581
struct wsi_x11_connection *wsi_conn =
1582
wsi_x11_get_connection(wsi_device, conn);
1583
if (!wsi_conn)
1584
return VK_ERROR_OUT_OF_HOST_MEMORY;
1585
1586
unsigned num_images = pCreateInfo->minImageCount;
1587
if (wsi_device->x11.strict_imageCount)
1588
num_images = pCreateInfo->minImageCount;
1589
else if (present_mode == VK_PRESENT_MODE_MAILBOX_KHR ||
1590
(present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR &&
1591
wsi_conn->is_xwayland))
1592
num_images = MAX2(num_images, 5);
1593
else if (wsi_device->x11.ensure_minImageCount)
1594
num_images = MAX2(num_images, x11_get_min_image_count(wsi_device));
1595
1596
/* Check for whether or not we have a window up-front */
1597
xcb_window_t window = x11_surface_get_window(icd_surface);
1598
xcb_get_geometry_reply_t *geometry =
1599
xcb_get_geometry_reply(conn, xcb_get_geometry(conn, window), NULL);
1600
if (geometry == NULL)
1601
return VK_ERROR_SURFACE_LOST_KHR;
1602
const uint32_t bit_depth = geometry->depth;
1603
const uint16_t cur_width = geometry->width;
1604
const uint16_t cur_height = geometry->height;
1605
free(geometry);
1606
1607
size_t size = sizeof(*chain) + num_images * sizeof(chain->images[0]);
1608
chain = vk_alloc(pAllocator, size, 8,
1609
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1610
if (chain == NULL)
1611
return VK_ERROR_OUT_OF_HOST_MEMORY;
1612
1613
result = wsi_swapchain_init(wsi_device, &chain->base, device,
1614
pCreateInfo, pAllocator);
1615
if (result != VK_SUCCESS)
1616
goto fail_alloc;
1617
1618
chain->base.destroy = x11_swapchain_destroy;
1619
chain->base.get_wsi_image = x11_get_wsi_image;
1620
chain->base.acquire_next_image = x11_acquire_next_image;
1621
chain->base.queue_present = x11_queue_present;
1622
chain->base.present_mode = present_mode;
1623
chain->base.image_count = num_images;
1624
chain->conn = conn;
1625
chain->window = window;
1626
chain->depth = bit_depth;
1627
chain->extent = pCreateInfo->imageExtent;
1628
chain->send_sbc = 0;
1629
chain->sent_image_count = 0;
1630
chain->last_present_msc = 0;
1631
chain->has_acquire_queue = false;
1632
chain->has_present_queue = false;
1633
chain->status = VK_SUCCESS;
1634
chain->has_dri3_modifiers = wsi_conn->has_dri3_modifiers;
1635
1636
if (chain->extent.width != cur_width || chain->extent.height != cur_height)
1637
chain->status = VK_SUBOPTIMAL_KHR;
1638
1639
/* We used to inherit copy_is_suboptimal from pCreateInfo->oldSwapchain.
1640
* When it was true, and when the next present was completed with copying,
1641
* we would return VK_SUBOPTIMAL_KHR and hint the app to reallocate again
1642
* for no good reason. If all following presents on the surface were
1643
* completed with copying because of some surface state change, we would
1644
* always return VK_SUBOPTIMAL_KHR no matter how many times the app had
1645
* reallocated.
1646
*/
1647
chain->copy_is_suboptimal = false;
1648
1649
if (!wsi_device->sw)
1650
if (!wsi_x11_check_dri3_compatible(wsi_device, conn))
1651
chain->base.use_prime_blit = true;
1652
1653
chain->event_id = xcb_generate_id(chain->conn);
1654
xcb_present_select_input(chain->conn, chain->event_id, chain->window,
1655
XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY |
1656
XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY |
1657
XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
1658
1659
/* Create an XCB event queue to hold present events outside of the usual
1660
* application event queue
1661
*/
1662
chain->special_event =
1663
xcb_register_for_special_xge(chain->conn, &xcb_present_id,
1664
chain->event_id, NULL);
1665
1666
chain->gc = xcb_generate_id(chain->conn);
1667
if (!chain->gc) {
1668
/* FINISHME: Choose a better error. */
1669
result = VK_ERROR_OUT_OF_HOST_MEMORY;
1670
goto fail_register;
1671
}
1672
1673
cookie = xcb_create_gc(chain->conn,
1674
chain->gc,
1675
chain->window,
1676
XCB_GC_GRAPHICS_EXPOSURES,
1677
(uint32_t []) { 0 });
1678
xcb_discard_reply(chain->conn, cookie.sequence);
1679
1680
uint64_t *modifiers[2] = {NULL, NULL};
1681
uint32_t num_modifiers[2] = {0, 0};
1682
uint32_t num_tranches = 0;
1683
if (wsi_device->supports_modifiers)
1684
wsi_x11_get_dri3_modifiers(wsi_conn, conn, window, chain->depth, 32,
1685
pCreateInfo->compositeAlpha,
1686
modifiers, num_modifiers, &num_tranches,
1687
pAllocator);
1688
1689
uint32_t image = 0;
1690
for (; image < chain->base.image_count; image++) {
1691
result = x11_image_init(device, chain, pCreateInfo, pAllocator,
1692
(const uint64_t *const *)modifiers,
1693
num_modifiers, num_tranches,
1694
&chain->images[image]);
1695
if (result != VK_SUCCESS)
1696
goto fail_init_images;
1697
}
1698
1699
if ((chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR ||
1700
chain->base.present_mode == VK_PRESENT_MODE_FIFO_RELAXED_KHR ||
1701
chain->base.present_mode == VK_PRESENT_MODE_MAILBOX_KHR ||
1702
(chain->base.present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR &&
1703
wsi_conn->is_xwayland)) && !chain->base.wsi->sw) {
1704
chain->has_present_queue = true;
1705
1706
/* Initialize our queues. We make them base.image_count + 1 because we will
1707
* occasionally use UINT32_MAX to signal the other thread that an error
1708
* has occurred and we don't want an overflow.
1709
*/
1710
int ret;
1711
ret = wsi_queue_init(&chain->present_queue, chain->base.image_count + 1);
1712
if (ret) {
1713
goto fail_init_images;
1714
}
1715
1716
if (chain->base.present_mode == VK_PRESENT_MODE_FIFO_KHR ||
1717
chain->base.present_mode == VK_PRESENT_MODE_FIFO_RELAXED_KHR) {
1718
chain->has_acquire_queue = true;
1719
1720
ret = wsi_queue_init(&chain->acquire_queue, chain->base.image_count + 1);
1721
if (ret) {
1722
wsi_queue_destroy(&chain->present_queue);
1723
goto fail_init_images;
1724
}
1725
1726
for (unsigned i = 0; i < chain->base.image_count; i++)
1727
wsi_queue_push(&chain->acquire_queue, i);
1728
}
1729
1730
ret = pthread_create(&chain->queue_manager, NULL,
1731
x11_manage_fifo_queues, chain);
1732
if (ret) {
1733
wsi_queue_destroy(&chain->present_queue);
1734
if (chain->has_acquire_queue)
1735
wsi_queue_destroy(&chain->acquire_queue);
1736
1737
goto fail_init_images;
1738
}
1739
}
1740
1741
assert(chain->has_present_queue || !chain->has_acquire_queue);
1742
1743
for (int i = 0; i < ARRAY_SIZE(modifiers); i++)
1744
vk_free(pAllocator, modifiers[i]);
1745
1746
/* It is safe to set it here as only one swapchain can be associated with
1747
* the window, and swapchain creation does the association. At this point
1748
* we know the creation is going to succeed. */
1749
wsi_x11_set_adaptive_sync_property(conn, window,
1750
wsi_device->enable_adaptive_sync);
1751
1752
*swapchain_out = &chain->base;
1753
1754
return VK_SUCCESS;
1755
1756
fail_init_images:
1757
for (uint32_t j = 0; j < image; j++)
1758
x11_image_finish(chain, pAllocator, &chain->images[j]);
1759
1760
for (int i = 0; i < ARRAY_SIZE(modifiers); i++)
1761
vk_free(pAllocator, modifiers[i]);
1762
1763
fail_register:
1764
xcb_unregister_for_special_event(chain->conn, chain->special_event);
1765
1766
wsi_swapchain_finish(&chain->base);
1767
1768
fail_alloc:
1769
vk_free(pAllocator, chain);
1770
1771
return result;
1772
}
1773
1774
VkResult
1775
wsi_x11_init_wsi(struct wsi_device *wsi_device,
1776
const VkAllocationCallbacks *alloc,
1777
const struct driOptionCache *dri_options)
1778
{
1779
struct wsi_x11 *wsi;
1780
VkResult result;
1781
1782
wsi = vk_alloc(alloc, sizeof(*wsi), 8,
1783
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1784
if (!wsi) {
1785
result = VK_ERROR_OUT_OF_HOST_MEMORY;
1786
goto fail;
1787
}
1788
1789
int ret = pthread_mutex_init(&wsi->mutex, NULL);
1790
if (ret != 0) {
1791
if (ret == ENOMEM) {
1792
result = VK_ERROR_OUT_OF_HOST_MEMORY;
1793
} else {
1794
/* FINISHME: Choose a better error. */
1795
result = VK_ERROR_OUT_OF_HOST_MEMORY;
1796
}
1797
1798
goto fail_alloc;
1799
}
1800
1801
wsi->connections = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1802
_mesa_key_pointer_equal);
1803
if (!wsi->connections) {
1804
result = VK_ERROR_OUT_OF_HOST_MEMORY;
1805
goto fail_mutex;
1806
}
1807
1808
if (dri_options) {
1809
if (driCheckOption(dri_options, "vk_x11_override_min_image_count", DRI_INT)) {
1810
wsi_device->x11.override_minImageCount =
1811
driQueryOptioni(dri_options, "vk_x11_override_min_image_count");
1812
}
1813
if (driCheckOption(dri_options, "vk_x11_strict_image_count", DRI_BOOL)) {
1814
wsi_device->x11.strict_imageCount =
1815
driQueryOptionb(dri_options, "vk_x11_strict_image_count");
1816
}
1817
if (driCheckOption(dri_options, "vk_x11_ensure_min_image_count", DRI_BOOL)) {
1818
wsi_device->x11.ensure_minImageCount =
1819
driQueryOptionb(dri_options, "vk_x11_ensure_min_image_count");
1820
}
1821
1822
}
1823
1824
wsi->base.get_support = x11_surface_get_support;
1825
wsi->base.get_capabilities2 = x11_surface_get_capabilities2;
1826
wsi->base.get_formats = x11_surface_get_formats;
1827
wsi->base.get_formats2 = x11_surface_get_formats2;
1828
wsi->base.get_present_modes = x11_surface_get_present_modes;
1829
wsi->base.get_present_rectangles = x11_surface_get_present_rectangles;
1830
wsi->base.create_swapchain = x11_surface_create_swapchain;
1831
1832
wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = &wsi->base;
1833
wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = &wsi->base;
1834
1835
return VK_SUCCESS;
1836
1837
fail_mutex:
1838
pthread_mutex_destroy(&wsi->mutex);
1839
fail_alloc:
1840
vk_free(alloc, wsi);
1841
fail:
1842
wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB] = NULL;
1843
wsi_device->wsi[VK_ICD_WSI_PLATFORM_XLIB] = NULL;
1844
1845
return result;
1846
}
1847
1848
void
1849
wsi_x11_finish_wsi(struct wsi_device *wsi_device,
1850
const VkAllocationCallbacks *alloc)
1851
{
1852
struct wsi_x11 *wsi =
1853
(struct wsi_x11 *)wsi_device->wsi[VK_ICD_WSI_PLATFORM_XCB];
1854
1855
if (wsi) {
1856
hash_table_foreach(wsi->connections, entry)
1857
wsi_x11_connection_destroy(wsi_device, entry->data);
1858
1859
_mesa_hash_table_destroy(wsi->connections, NULL);
1860
1861
pthread_mutex_destroy(&wsi->mutex);
1862
1863
vk_free(alloc, wsi);
1864
}
1865
}
1866
1867