-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsi_common_x11.c
2994 lines (2563 loc) · 106 KB
/
wsi_common_x11.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2015 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <X11/Xlib-xcb.h>
#include <X11/xshmfence.h>
#define XK_MISCELLANY
#define XK_LATIN1
#include <X11/keysymdef.h>
#include <xcb/xcb.h>
#ifdef XCB_KEYSYMS_AVAILABLE
#include <xcb/xcb_keysyms.h>
#endif
#include <xcb/dri3.h>
#include <xcb/present.h>
#include <xcb/shm.h>
#include "util/macros.h"
#include <stdatomic.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <poll.h>
#include <xf86drm.h>
#include "drm-uapi/drm_fourcc.h"
#include "util/hash_table.h"
#include "util/os_file.h"
#include "util/os_time.h"
#include "util/u_debug.h"
#include "util/u_thread.h"
#include "util/xmlconfig.h"
#include "util/timespec.h"
#include "vk_format.h"
#include "vk_instance.h"
#include "vk_physical_device.h"
#include "vk_device.h"
#include "vk_util.h"
#include "vk_enum_to_str.h"
#include "wsi_common_entrypoints.h"
#include "wsi_common_private.h"
#include "wsi_common_queue.h"
#ifdef HAVE_SYS_SHM_H
#include <sys/ipc.h>
#include <sys/shm.h>
#endif
struct wsi_x11_connection {
bool has_dri3;
bool has_dri3_modifiers;
bool has_present;
bool is_proprietary_x11;
bool is_xwayland;
bool has_mit_shm;
bool has_xfixes;
};
struct wsi_x11 {
struct wsi_interface base;
pthread_mutex_t mutex;
/* Hash table of xcb_connection -> wsi_x11_connection mappings */
struct hash_table *connections;
};
struct wsi_x11_vk_surface {
union {
VkIcdSurfaceXlib xlib;
VkIcdSurfaceXcb xcb;
};
VkExtent2D extent;
bool changed;
bool has_alpha;
};
/**
* Wrapper around xcb_dri3_open. Returns the opened fd or -1 on error.
*/
static int
wsi_dri3_open(xcb_connection_t *conn,
xcb_window_t root,
uint32_t provider)
{
xcb_dri3_open_cookie_t cookie;
xcb_dri3_open_reply_t *reply;
int fd;
cookie = xcb_dri3_open(conn,
root,
provider);
reply = xcb_dri3_open_reply(conn, cookie, NULL);
if (!reply)
return -1;
/* According to DRI3 extension nfd must equal one. */
if (reply->nfd != 1) {
free(reply);
return -1;
}
fd = xcb_dri3_open_reply_fds(conn, reply)[0];
free(reply);
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
return fd;
}
/**
* Checks compatibility of the device wsi_dev with the device the X server
* provides via DRI3.
*
* This returns true when no device could be retrieved from the X server or when
* the information for the X server device indicate that it is the same device.
*/
static bool
wsi_x11_check_dri3_compatible(const struct wsi_device *wsi_dev,
xcb_connection_t *conn)
{
xcb_screen_iterator_t screen_iter =
xcb_setup_roots_iterator(xcb_get_setup(conn));
xcb_screen_t *screen = screen_iter.data;
/* Open the DRI3 device from the X server. If we do not retrieve one we
* assume our local device is compatible.
*/
int dri3_fd = wsi_dri3_open(conn, screen->root, None);
if (dri3_fd == -1)
return true;
bool match = wsi_device_matches_drm_fd(wsi_dev, dri3_fd);
close(dri3_fd);
return match;
}
static bool
wsi_x11_detect_xwayland(xcb_connection_t *conn,
xcb_query_extension_reply_t *randr_reply,
xcb_query_extension_reply_t *xwl_reply)
{
/* Newer Xwayland exposes an X11 extension we can check for */
if (xwl_reply && xwl_reply->present)
return true;
/* Older Xwayland uses the word "XWAYLAND" in the RandR output names */
if (!randr_reply || !randr_reply->present)
return false;
xcb_randr_query_version_cookie_t ver_cookie =
xcb_randr_query_version_unchecked(conn, 1, 3);
xcb_randr_query_version_reply_t *ver_reply =
xcb_randr_query_version_reply(conn, ver_cookie, NULL);
bool has_randr_v1_3 = ver_reply && (ver_reply->major_version > 1 ||
ver_reply->minor_version >= 3);
free(ver_reply);
if (!has_randr_v1_3)
return false;
const xcb_setup_t *setup = xcb_get_setup(conn);
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
xcb_randr_get_screen_resources_current_cookie_t gsr_cookie =
xcb_randr_get_screen_resources_current_unchecked(conn, iter.data->root);
xcb_randr_get_screen_resources_current_reply_t *gsr_reply =
xcb_randr_get_screen_resources_current_reply(conn, gsr_cookie, NULL);
if (!gsr_reply || gsr_reply->num_outputs == 0) {
free(gsr_reply);
return false;
}
xcb_randr_output_t *randr_outputs =
xcb_randr_get_screen_resources_current_outputs(gsr_reply);
xcb_randr_get_output_info_cookie_t goi_cookie =
xcb_randr_get_output_info(conn, randr_outputs[0], gsr_reply->config_timestamp);
free(gsr_reply);
xcb_randr_get_output_info_reply_t *goi_reply =
xcb_randr_get_output_info_reply(conn, goi_cookie, NULL);
if (!goi_reply) {
return false;
}
char *output_name = (char*)xcb_randr_get_output_info_name(goi_reply);
bool is_xwayland = output_name && strncmp(output_name, "XWAYLAND", 8) == 0;
free(goi_reply);
return is_xwayland;
}
static struct wsi_x11_connection *
wsi_x11_connection_create(struct wsi_device *wsi_dev,
xcb_connection_t *conn)
{
xcb_query_extension_cookie_t dri3_cookie, pres_cookie, randr_cookie,
amd_cookie, nv_cookie, shm_cookie, sync_cookie,
xfixes_cookie, xwl_cookie;
xcb_query_extension_reply_t *dri3_reply, *pres_reply, *randr_reply,
*amd_reply, *nv_reply, *shm_reply = NULL,
*xfixes_reply, *xwl_reply;
bool wants_shm = wsi_dev->sw && !(WSI_DEBUG & WSI_DEBUG_NOSHM) &&
wsi_dev->has_import_memory_host;
bool has_dri3_v1_2 = false;
bool has_present_v1_2 = false;
struct wsi_x11_connection *wsi_conn =
vk_alloc(&wsi_dev->instance_alloc, sizeof(*wsi_conn), 8,
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
if (!wsi_conn)
return NULL;
sync_cookie = xcb_query_extension(conn, 4, "SYNC");
dri3_cookie = xcb_query_extension(conn, 4, "DRI3");
pres_cookie = xcb_query_extension(conn, 7, "Present");
randr_cookie = xcb_query_extension(conn, 5, "RANDR");
xfixes_cookie = xcb_query_extension(conn, 6, "XFIXES");
xwl_cookie = xcb_query_extension(conn, 8, "XWAYLAND");
if (wants_shm)
shm_cookie = xcb_query_extension(conn, 7, "MIT-SHM");
/* We try to be nice to users and emit a warning if they try to use a
* Vulkan application on a system without DRI3 enabled. However, this ends
* up spewing the warning when a user has, for example, both Intel
* integrated graphics and a discrete card with proprietary drivers and are
* running on the discrete card with the proprietary DDX. In this case, we
* really don't want to print the warning because it just confuses users.
* As a heuristic to detect this case, we check for a couple of proprietary
* X11 extensions.
*/
amd_cookie = xcb_query_extension(conn, 11, "ATIFGLRXDRI");
nv_cookie = xcb_query_extension(conn, 10, "NV-CONTROL");
xcb_discard_reply(conn, sync_cookie.sequence);
dri3_reply = xcb_query_extension_reply(conn, dri3_cookie, NULL);
pres_reply = xcb_query_extension_reply(conn, pres_cookie, NULL);
randr_reply = xcb_query_extension_reply(conn, randr_cookie, NULL);
amd_reply = xcb_query_extension_reply(conn, amd_cookie, NULL);
nv_reply = xcb_query_extension_reply(conn, nv_cookie, NULL);
xfixes_reply = xcb_query_extension_reply(conn, xfixes_cookie, NULL);
xwl_reply = xcb_query_extension_reply(conn, xwl_cookie, NULL);
if (wants_shm)
shm_reply = xcb_query_extension_reply(conn, shm_cookie, NULL);
if (!dri3_reply || !pres_reply || !xfixes_reply) {
free(dri3_reply);
free(pres_reply);
free(xfixes_reply);
free(xwl_reply);
free(randr_reply);
free(amd_reply);
free(nv_reply);
if (wants_shm)
free(shm_reply);
vk_free(&wsi_dev->instance_alloc, wsi_conn);
return NULL;
}
wsi_conn->has_dri3 = dri3_reply->present != 0;
#ifdef HAVE_DRI3_MODIFIERS
if (wsi_conn->has_dri3) {
xcb_dri3_query_version_cookie_t ver_cookie;
xcb_dri3_query_version_reply_t *ver_reply;
ver_cookie = xcb_dri3_query_version(conn, 1, 2);
ver_reply = xcb_dri3_query_version_reply(conn, ver_cookie, NULL);
has_dri3_v1_2 = ver_reply != NULL &&
(ver_reply->major_version > 1 || ver_reply->minor_version >= 2);
free(ver_reply);
}
#endif
wsi_conn->has_present = pres_reply->present != 0;
#ifdef HAVE_DRI3_MODIFIERS
if (wsi_conn->has_present) {
xcb_present_query_version_cookie_t ver_cookie;
xcb_present_query_version_reply_t *ver_reply;
ver_cookie = xcb_present_query_version(conn, 1, 2);
ver_reply = xcb_present_query_version_reply(conn, ver_cookie, NULL);
has_present_v1_2 =
(ver_reply->major_version > 1 || ver_reply->minor_version >= 2);
free(ver_reply);
}
#endif
wsi_conn->has_xfixes = xfixes_reply->present != 0;
if (wsi_conn->has_xfixes) {
xcb_xfixes_query_version_cookie_t ver_cookie;
xcb_xfixes_query_version_reply_t *ver_reply;
ver_cookie = xcb_xfixes_query_version(conn, 6, 0);
ver_reply = xcb_xfixes_query_version_reply(conn, ver_cookie, NULL);
wsi_conn->has_xfixes = (ver_reply->major_version >= 2);
free(ver_reply);
}
wsi_conn->is_xwayland = wsi_x11_detect_xwayland(conn, randr_reply,
xwl_reply);
wsi_conn->has_dri3_modifiers = has_dri3_v1_2 && has_present_v1_2;
wsi_conn->is_proprietary_x11 = false;
if (amd_reply && amd_reply->present)
wsi_conn->is_proprietary_x11 = true;
if (nv_reply && nv_reply->present)
wsi_conn->is_proprietary_x11 = true;
wsi_conn->has_mit_shm = false;
if (wsi_conn->has_dri3 && wsi_conn->has_present && wants_shm) {
bool has_mit_shm = shm_reply->present != 0;
xcb_shm_query_version_cookie_t ver_cookie;
xcb_shm_query_version_reply_t *ver_reply;
ver_cookie = xcb_shm_query_version(conn);
ver_reply = xcb_shm_query_version_reply(conn, ver_cookie, NULL);
has_mit_shm = ver_reply->shared_pixmaps;
free(ver_reply);
xcb_void_cookie_t cookie;
xcb_generic_error_t *error;
if (has_mit_shm) {
cookie = xcb_shm_detach_checked(conn, 0);
if ((error = xcb_request_check(conn, cookie))) {
if (error->error_code != BadRequest)
wsi_conn->has_mit_shm = true;
free(error);
}
}
}
free(dri3_reply);
free(pres_reply);
free(randr_reply);
free(xwl_reply);
free(amd_reply);
free(nv_reply);
free(xfixes_reply);
if (wants_shm)
free(shm_reply);
return wsi_conn;
}
static void
wsi_x11_connection_destroy(struct wsi_device *wsi_dev,
struct wsi_x11_connection *conn)
{
vk_free(&wsi_dev->instance_alloc, conn);
}
static bool
wsi_x11_check_for_dri3(struct wsi_x11_connection *wsi_conn)
{
if (wsi_conn->has_dri3)
return true;
if (!wsi_conn->is_proprietary_x11) {
fprintf(stderr, "vulkan: No DRI3 support detected - required for presentation\n"
"Note: you can probably enable DRI3 in your Xorg config\n");
}
return false;
}
/**
* Get internal struct representing an xcb_connection_t.
*
* This can allocate the struct but the caller does not own the struct. It is
* deleted on wsi_x11_finish_wsi by the hash table it is inserted.
*
* If the allocation fails NULL is returned.
*/
static struct wsi_x11_connection *
wsi_x11_get_connection(struct wsi_device *wsi_dev,
xcb_connection_t *conn)
{
struct wsi_x11 *wsi =
(struct wsi_x11 *)wsi_dev->wsi[VK_ICD_WSI_PLATFORM_XCB];
pthread_mutex_lock(&wsi->mutex);
struct hash_entry *entry = _mesa_hash_table_search(wsi->connections, conn);
if (!entry) {
/* We're about to make a bunch of blocking calls. Let's drop the
* mutex for now so we don't block up too badly.
*/
pthread_mutex_unlock(&wsi->mutex);
struct wsi_x11_connection *wsi_conn =
wsi_x11_connection_create(wsi_dev, conn);
if (!wsi_conn)
return NULL;
pthread_mutex_lock(&wsi->mutex);
entry = _mesa_hash_table_search(wsi->connections, conn);
if (entry) {
/* Oops, someone raced us to it */
wsi_x11_connection_destroy(wsi_dev, wsi_conn);
} else {
entry = _mesa_hash_table_insert(wsi->connections, conn, wsi_conn);
}
}
pthread_mutex_unlock(&wsi->mutex);
return entry->data;
}
static const VkFormat formats[] = {
VK_FORMAT_R5G6B5_UNORM_PACK16,
VK_FORMAT_B8G8R8A8_SRGB,
VK_FORMAT_B8G8R8A8_UNORM,
VK_FORMAT_A2R10G10B10_UNORM_PACK32,
};
static const VkPresentModeKHR present_modes[] = {
VK_PRESENT_MODE_IMMEDIATE_KHR,
VK_PRESENT_MODE_MAILBOX_KHR,
VK_PRESENT_MODE_FIFO_KHR,
VK_PRESENT_MODE_FIFO_RELAXED_KHR,
};
static xcb_screen_t *
get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
{
xcb_screen_iterator_t screen_iter =
xcb_setup_roots_iterator(xcb_get_setup(conn));
for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
if (screen_iter.data->root == root)
return screen_iter.data;
}
return NULL;
}
static xcb_visualtype_t *
screen_get_visualtype(xcb_screen_t *screen, xcb_visualid_t visual_id,
unsigned *depth)
{
xcb_depth_iterator_t depth_iter =
xcb_screen_allowed_depths_iterator(screen);
for (; depth_iter.rem; xcb_depth_next (&depth_iter)) {
xcb_visualtype_iterator_t visual_iter =
xcb_depth_visuals_iterator (depth_iter.data);
for (; visual_iter.rem; xcb_visualtype_next (&visual_iter)) {
if (visual_iter.data->visual_id == visual_id) {
if (depth)
*depth = depth_iter.data->depth;
return visual_iter.data;
}
}
}
return NULL;
}
static xcb_visualtype_t *
connection_get_visualtype(xcb_connection_t *conn, xcb_visualid_t visual_id)
{
xcb_screen_iterator_t screen_iter =
xcb_setup_roots_iterator(xcb_get_setup(conn));
/* For this we have to iterate over all of the screens which is rather
* annoying. Fortunately, there is probably only 1.
*/
for (; screen_iter.rem; xcb_screen_next (&screen_iter)) {
xcb_visualtype_t *visual = screen_get_visualtype(screen_iter.data,
visual_id, NULL);
if (visual)
return visual;
}
return NULL;
}
static xcb_visualtype_t *
get_visualtype_for_window(xcb_connection_t *conn, xcb_window_t window,
unsigned *depth, xcb_visualtype_t **rootvis)
{
xcb_query_tree_cookie_t tree_cookie;
xcb_get_window_attributes_cookie_t attrib_cookie;
xcb_query_tree_reply_t *tree;
xcb_get_window_attributes_reply_t *attrib;
tree_cookie = xcb_query_tree(conn, window);
attrib_cookie = xcb_get_window_attributes(conn, window);
tree = xcb_query_tree_reply(conn, tree_cookie, NULL);
attrib = xcb_get_window_attributes_reply(conn, attrib_cookie, NULL);
if (attrib == NULL || tree == NULL) {
free(attrib);
free(tree);
return NULL;
}
xcb_window_t root = tree->root;
xcb_visualid_t visual_id = attrib->visual;
free(attrib);
free(tree);
xcb_screen_t *screen = get_screen_for_root(conn, root);
if (screen == NULL)
return NULL;
if (rootvis)
*rootvis = screen_get_visualtype(screen, screen->root_visual, depth);
return screen_get_visualtype(screen, visual_id, depth);
}
static bool
visual_has_alpha(xcb_visualtype_t *visual, unsigned depth)
{
uint32_t rgb_mask = visual->red_mask |
visual->green_mask |
visual->blue_mask;
uint32_t all_mask = 0xffffffff >> (32 - depth);
/* Do we have bits left over after RGB? */
return (all_mask & ~rgb_mask) != 0;
}
static bool
visual_supported(xcb_visualtype_t *visual)
{
if (!visual)
return false;
return visual->_class == XCB_VISUAL_CLASS_TRUE_COLOR ||
visual->_class == XCB_VISUAL_CLASS_DIRECT_COLOR;
}
VKAPI_ATTR VkBool32 VKAPI_CALL
wsi_GetPhysicalDeviceXcbPresentationSupportKHR(VkPhysicalDevice physicalDevice,
uint32_t queueFamilyIndex,
xcb_connection_t *connection,
xcb_visualid_t visual_id)
{
VK_FROM_HANDLE(vk_physical_device, pdevice, physicalDevice);
struct wsi_device *wsi_device = pdevice->wsi_device;
struct wsi_x11_connection *wsi_conn =
wsi_x11_get_connection(wsi_device, connection);
if (!wsi_conn)
return false;
if (!wsi_device->sw) {
if (!wsi_x11_check_for_dri3(wsi_conn))
return false;
}
if (!visual_supported(connection_get_visualtype(connection, visual_id)))
return false;
return true;
}
VKAPI_ATTR VkBool32 VKAPI_CALL
wsi_GetPhysicalDeviceXlibPresentationSupportKHR(VkPhysicalDevice physicalDevice,
uint32_t queueFamilyIndex,
Display *dpy,
VisualID visualID)
{
return wsi_GetPhysicalDeviceXcbPresentationSupportKHR(physicalDevice,
queueFamilyIndex,
XGetXCBConnection(dpy),
visualID);
}
static xcb_connection_t*
x11_surface_get_connection(VkIcdSurfaceBase *icd_surface)
{
if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
return XGetXCBConnection(((VkIcdSurfaceXlib *)icd_surface)->dpy);
else
return ((VkIcdSurfaceXcb *)icd_surface)->connection;
}
static xcb_window_t
x11_surface_get_window(VkIcdSurfaceBase *icd_surface)
{
if (icd_surface->platform == VK_ICD_WSI_PLATFORM_XLIB)
return ((VkIcdSurfaceXlib *)icd_surface)->window;
else
return ((VkIcdSurfaceXcb *)icd_surface)->window;
}
static VkResult
x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
struct wsi_device *wsi_device,
uint32_t queueFamilyIndex,
VkBool32* pSupported)
{
xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
xcb_window_t window = x11_surface_get_window(icd_surface);
struct wsi_x11_connection *wsi_conn =
wsi_x11_get_connection(wsi_device, conn);
if (!wsi_conn)
return VK_ERROR_OUT_OF_HOST_MEMORY;
if (!wsi_device->sw) {
if (!wsi_x11_check_for_dri3(wsi_conn)) {
*pSupported = false;
return VK_SUCCESS;
}
}
if (!visual_supported(get_visualtype_for_window(conn, window, NULL, NULL))) {
*pSupported = false;
return VK_SUCCESS;
}
*pSupported = true;
return VK_SUCCESS;
}
static uint32_t
x11_get_min_image_count(const struct wsi_device *wsi_device, bool is_xwayland)
{
if (wsi_device->x11.override_minImageCount)
return wsi_device->x11.override_minImageCount;
/* For IMMEDIATE and FIFO, most games work in a pipelined manner where the
* can produce frames at a rate of 1/MAX(CPU duration, GPU duration), but
* the render latency is CPU duration + GPU duration.
*
* This means that with scanout from pageflipping we need 3 frames to run
* full speed:
* 1) CPU rendering work
* 2) GPU rendering work
* 3) scanout
*
* Once we have a nonblocking acquire that returns a semaphore we can merge
* 1 and 3. Hence the ideal implementation needs only 2 images, but games
* cannot tellwe currently do not have an ideal implementation and that
* hence they need to allocate 3 images. So let us do it for them.
*
* This is a tradeoff as it uses more memory than needed for non-fullscreen
* and non-performance intensive applications.
*
* For Xwayland Venus reports four images as described in
* wsi_wl_surface_get_capabilities
*/
return is_xwayland && wsi_device->x11.extra_xwayland_image ? 4 : 3;
}
static unsigned
x11_get_min_image_count_for_present_mode(struct wsi_device *wsi_device,
struct wsi_x11_connection *wsi_conn,
VkPresentModeKHR present_mode);
static VkResult
x11_surface_get_capabilities(VkIcdSurfaceBase *icd_surface,
struct wsi_device *wsi_device,
const VkSurfacePresentModeEXT *present_mode,
VkSurfaceCapabilitiesKHR *caps)
{
xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
xcb_window_t window = x11_surface_get_window(icd_surface);
struct wsi_x11_vk_surface *surface = (struct wsi_x11_vk_surface*)icd_surface;
struct wsi_x11_connection *wsi_conn =
wsi_x11_get_connection(wsi_device, conn);
if (surface->changed) {
surface->changed = false;
xcb_get_geometry_cookie_t geom_cookie = xcb_get_geometry(conn, window);
xcb_get_geometry_reply_t *geom;
geom = xcb_get_geometry_reply(conn, geom_cookie, NULL);
if (!geom)
return VK_ERROR_SURFACE_LOST_KHR;
VkExtent2D extent = { geom->width, geom->height };
caps->currentExtent = extent;
caps->minImageExtent = extent;
caps->maxImageExtent = extent;
surface->extent = extent;
free(geom);
} else {
caps->currentExtent = surface->extent;
caps->minImageExtent = surface->extent;
caps->maxImageExtent = surface->extent;
}
if (surface->has_alpha) {
caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
} else {
caps->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
}
if (present_mode) {
caps->minImageCount = x11_get_min_image_count_for_present_mode(wsi_device, wsi_conn, present_mode->presentMode);
} else {
caps->minImageCount = x11_get_min_image_count(wsi_device, wsi_conn->is_xwayland);
}
/* There is no real maximum */
caps->maxImageCount = 0;
caps->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
caps->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
caps->maxImageArrayLayers = 1;
caps->supportedUsageFlags =
VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
VK_IMAGE_USAGE_SAMPLED_BIT |
VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_STORAGE_BIT |
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
VK_FROM_HANDLE(vk_physical_device, pdevice, wsi_device->pdevice);
if (pdevice->supported_extensions.EXT_attachment_feedback_loop_layout)
caps->supportedUsageFlags |= VK_IMAGE_USAGE_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT;
return VK_SUCCESS;
}
static VkResult
x11_surface_get_capabilities2(VkIcdSurfaceBase *icd_surface,
struct wsi_device *wsi_device,
const void *info_next,
VkSurfaceCapabilities2KHR *caps)
{
assert(caps->sType == VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR);
const VkSurfacePresentModeEXT *present_mode = vk_find_struct_const(info_next, SURFACE_PRESENT_MODE_EXT);
VkResult result =
x11_surface_get_capabilities(icd_surface, wsi_device, present_mode,
&caps->surfaceCapabilities);
if (result != VK_SUCCESS)
return result;
vk_foreach_struct(ext, caps->pNext) {
switch (ext->sType) {
case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR: {
VkSurfaceProtectedCapabilitiesKHR *protected = (void *)ext;
protected->supportsProtected = VK_FALSE;
break;
}
case VK_STRUCTURE_TYPE_SURFACE_PRESENT_SCALING_CAPABILITIES_EXT: {
/* Unsupported. */
VkSurfacePresentScalingCapabilitiesEXT *scaling = (void *)ext;
scaling->supportedPresentScaling = 0;
scaling->supportedPresentGravityX = 0;
scaling->supportedPresentGravityY = 0;
scaling->minScaledImageExtent = caps->surfaceCapabilities.minImageExtent;
scaling->maxScaledImageExtent = caps->surfaceCapabilities.maxImageExtent;
break;
}
case VK_STRUCTURE_TYPE_SURFACE_PRESENT_MODE_COMPATIBILITY_EXT: {
/* To be able to toggle between FIFO and non-FIFO, we would need a rewrite to always use FIFO thread
* mechanism. For now, only return the input, making this effectively unsupported. */
VkSurfacePresentModeCompatibilityEXT *compat = (void *)ext;
if (compat->pPresentModes) {
if (compat->presentModeCount) {
assert(present_mode);
compat->pPresentModes[0] = present_mode->presentMode;
compat->presentModeCount = 1;
}
} else {
compat->presentModeCount = 1;
}
break;
}
default:
/* Ignored */
break;
}
}
return result;
}
static int
format_get_component_bits(VkFormat format, int comp)
{
return vk_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, comp);
}
static bool
rgb_component_bits_are_equal(VkFormat format, const xcb_visualtype_t* type)
{
return format_get_component_bits(format, 0) == util_bitcount(type->red_mask) &&
format_get_component_bits(format, 1) == util_bitcount(type->green_mask) &&
format_get_component_bits(format, 2) == util_bitcount(type->blue_mask);
}
static bool
get_sorted_vk_formats(VkIcdSurfaceBase *surface, struct wsi_device *wsi_device,
VkFormat *sorted_formats, unsigned *count)
{
xcb_connection_t *conn = x11_surface_get_connection(surface);
xcb_window_t window = x11_surface_get_window(surface);
xcb_visualtype_t *rootvis = NULL;
xcb_visualtype_t *visual = get_visualtype_for_window(conn, window, NULL, &rootvis);
if (!visual)
return false;
/* use the root window's visual to set the default */
*count = 0;
for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
if (rgb_component_bits_are_equal(formats[i], rootvis))
sorted_formats[(*count)++] = formats[i];
}
for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
for (unsigned j = 0; j < *count; j++)
if (formats[i] == sorted_formats[j])
goto next_format;
if (rgb_component_bits_are_equal(formats[i], visual))
sorted_formats[(*count)++] = formats[i];
next_format:;
}
if (wsi_device->force_bgra8_unorm_first) {
for (unsigned i = 0; i < *count; i++) {
if (sorted_formats[i] == VK_FORMAT_B8G8R8A8_UNORM) {
sorted_formats[i] = sorted_formats[0];
sorted_formats[0] = VK_FORMAT_B8G8R8A8_UNORM;
break;
}
}
}
return true;
}
static VkResult
x11_surface_get_formats(VkIcdSurfaceBase *surface,
struct wsi_device *wsi_device,
uint32_t *pSurfaceFormatCount,
VkSurfaceFormatKHR *pSurfaceFormats)
{
VK_OUTARRAY_MAKE_TYPED(VkSurfaceFormatKHR, out,
pSurfaceFormats, pSurfaceFormatCount);
unsigned count;
VkFormat sorted_formats[ARRAY_SIZE(formats)];
if (!get_sorted_vk_formats(surface, wsi_device, sorted_formats, &count))
return VK_ERROR_SURFACE_LOST_KHR;
for (unsigned i = 0; i < count; i++) {
vk_outarray_append_typed(VkSurfaceFormatKHR, &out, f) {
f->format = sorted_formats[i];
f->colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
}
}
return vk_outarray_status(&out);
}
static VkResult
x11_surface_get_formats2(VkIcdSurfaceBase *surface,
struct wsi_device *wsi_device,
const void *info_next,
uint32_t *pSurfaceFormatCount,
VkSurfaceFormat2KHR *pSurfaceFormats)
{
VK_OUTARRAY_MAKE_TYPED(VkSurfaceFormat2KHR, out,
pSurfaceFormats, pSurfaceFormatCount);
unsigned count;
VkFormat sorted_formats[ARRAY_SIZE(formats)];
if (!get_sorted_vk_formats(surface, wsi_device, sorted_formats, &count))
return VK_ERROR_SURFACE_LOST_KHR;
for (unsigned i = 0; i < count; i++) {
vk_outarray_append_typed(VkSurfaceFormat2KHR, &out, f) {
assert(f->sType == VK_STRUCTURE_TYPE_SURFACE_FORMAT_2_KHR);
f->surfaceFormat.format = sorted_formats[i];
f->surfaceFormat.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
}
}
return vk_outarray_status(&out);
}
static VkResult
x11_surface_get_present_modes(VkIcdSurfaceBase *surface,
struct wsi_device *wsi_device,
uint32_t *pPresentModeCount,
VkPresentModeKHR *pPresentModes)
{
if (pPresentModes == NULL) {
*pPresentModeCount = ARRAY_SIZE(present_modes);
return VK_SUCCESS;
}
*pPresentModeCount = MIN2(*pPresentModeCount, ARRAY_SIZE(present_modes));
typed_memcpy(pPresentModes, present_modes, *pPresentModeCount);
return *pPresentModeCount < ARRAY_SIZE(present_modes) ?
VK_INCOMPLETE : VK_SUCCESS;
}
static VkResult
x11_surface_get_present_rectangles(VkIcdSurfaceBase *icd_surface,
struct wsi_device *wsi_device,
uint32_t* pRectCount,
VkRect2D* pRects)
{
xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
xcb_window_t window = x11_surface_get_window(icd_surface);
VK_OUTARRAY_MAKE_TYPED(VkRect2D, out, pRects, pRectCount);
vk_outarray_append_typed(VkRect2D, &out, rect) {
xcb_generic_error_t *err = NULL;
xcb_get_geometry_cookie_t geom_cookie = xcb_get_geometry(conn, window);
xcb_get_geometry_reply_t *geom =
xcb_get_geometry_reply(conn, geom_cookie, &err);
free(err);
if (geom) {
*rect = (VkRect2D) {
.offset = { 0, 0 },
.extent = { geom->width, geom->height },
};
}
free(geom);
if (!geom)
return VK_ERROR_SURFACE_LOST_KHR;
}
return vk_outarray_status(&out);
}
VKAPI_ATTR VkResult VKAPI_CALL
wsi_CreateXcbSurfaceKHR(VkInstance _instance,
const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
const VkAllocationCallbacks *pAllocator,
VkSurfaceKHR *pSurface)
{
VK_FROM_HANDLE(vk_instance, instance, _instance);
struct wsi_x11_vk_surface *surface;
assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR);
unsigned visual_depth;
xcb_visualtype_t *visual =
get_visualtype_for_window(pCreateInfo->connection, pCreateInfo->window, &visual_depth, NULL);
if (!visual)
return VK_ERROR_OUT_OF_HOST_MEMORY;
surface = vk_alloc2(&instance->alloc, pAllocator, sizeof(struct wsi_x11_vk_surface), 8,
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
if (surface == NULL)
return VK_ERROR_OUT_OF_HOST_MEMORY;
surface->xcb.base.platform = VK_ICD_WSI_PLATFORM_XCB;
surface->xcb.connection = pCreateInfo->connection;
surface->xcb.window = pCreateInfo->window;
surface->has_alpha = visual_has_alpha(visual, visual_depth);
surface->changed = true;
*pSurface = VkIcdSurfaceBase_to_handle(&surface->xcb.base);
return VK_SUCCESS;
}
VKAPI_ATTR VkResult VKAPI_CALL
wsi_CreateXlibSurfaceKHR(VkInstance _instance,
const VkXlibSurfaceCreateInfoKHR *pCreateInfo,