summaryrefslogtreecommitdiff
path: root/hw/xwayland/xwayland-glamor-gbm.c
blob: 45f18a47ca3b075a6c06029470440c2a3d6eee30 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
/*
 * Copyright © 2011-2014 Intel Corporation
 * Copyright © 2017 Red Hat Inc.
 *
 * 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.
 *
 * Authors:
 *    Lyude Paul <lyude@redhat.com>
 *
 */

#include <xwayland-config.h>

#include <fcntl.h>
#include <errno.h>
#include <poll.h>
#ifdef DRI3
#include <sys/eventfd.h>
#endif /* DRI3 */
#include <sys/stat.h>
#include <xf86drm.h>
#include <drm_fourcc.h>
#if defined(__linux__)
#include <linux/dma-buf.h>
#include <linux/sync_file.h>
#include <sys/ioctl.h>
#endif

#define MESA_EGL_NO_X11_HEADERS
#define EGL_NO_X11
#include <gbm.h>
#include <glamor_egl.h>

#include <glamor.h>
#include <glamor_context.h>
#include <dri3.h>
#include "drm-client-protocol.h"

#include "xwayland-glamor.h"
#include "xwayland-glamor-gbm.h"
#include "xwayland-pixmap.h"
#include "xwayland-screen.h"
#include "xwayland-window-buffers.h"

#include "linux-dmabuf-unstable-v1-client-protocol.h"
#include "linux-drm-syncobj-v1-client-protocol.h"

struct xwl_gbm_private {
    drmDevice *device;
    char *device_name;
    struct gbm_device *gbm;
    int drm_fd;
    Bool fd_render_node;
    Bool drm_authenticated;
    Bool dmabuf_capable;
    Bool glamor_gles;
    Bool implicit_sync;
    Bool supports_syncobjs;

    /* Set if wl_drm is available */
    struct wl_drm *drm;
    uint32_t capabilities;
};

struct xwl_pixmap {
    struct wl_buffer *buffer;
    EGLImage image;
    unsigned int texture;
    struct gbm_bo *bo;
    Bool implicit_modifier;
#ifdef DRI3
    struct dri3_syncobj *syncobj;
    uint64_t timeline_point;
    int efd;
    struct xwl_window_buffer *xwl_window_buffer;
#endif /* DRI3 */
};

static DevPrivateKeyRec xwl_gbm_private_key;
static DevPrivateKeyRec xwl_auth_state_private_key;

static inline struct xwl_gbm_private *
xwl_gbm_get(struct xwl_screen *xwl_screen)
{
    return dixLookupPrivate(&xwl_screen->screen->devPrivates,
                            &xwl_gbm_private_key);
}

Bool
xwl_glamor_has_wl_drm(struct xwl_screen *xwl_screen)
{
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);

    return !!(xwl_gbm->drm != NULL);
}

/* There is a workaround for Mesa behaviour, which will cause black windows
 * when RGBX formats is using. Why exactly? There is an explanation:
 * 1. We create GL_RGBA texture with GL_UNSIGNED_BYTE type, all allowed by ES.
 * 2 .We export these texture to GBM bo with GBM_FORMAT_XRGB8888, and Mesa sets internal
 * format of these textures as GL_RGB8 (mesa/mesa!5034 (merged))
 * 3. We import these BO at some point, and use glTexSubImage on it with GL_RGBA format
 * and with GL_UNSIGNED_BYTE type, as we creates. Mesa checks its internalformat
 * in glTexSubImage2D and fails due to GLES internal format limitation
 * (see https://registry.khronos.org/OpenGL/specs/es/2.0/es_full_spec_2.0.pdf, section 3.7.1).
 */
static uint32_t
gbm_format_for_depth(int depth, int gles)
{
    switch (depth) {
    case 15:
        return GBM_FORMAT_ARGB1555;
    case 16:
        return GBM_FORMAT_RGB565;
    case 24:
        if (gles)
            return GBM_FORMAT_ARGB8888;
        return GBM_FORMAT_XRGB8888;
    case 30:
        return GBM_FORMAT_ARGB2101010;
    default:
        ErrorF("unexpected depth: %d\n", depth);
    case 32:
        return GBM_FORMAT_ARGB8888;
    }
}

static char
is_device_path_render_node (const char *device_path)
{
    char is_render_node;
    int fd;

    fd = open(device_path, O_RDWR | O_CLOEXEC);
    if (fd < 0)
        return 0;

    is_render_node = (drmGetNodeTypeFromFd(fd) == DRM_NODE_RENDER);
    close(fd);

    return is_render_node;
}

static PixmapPtr
xwl_glamor_gbm_create_pixmap_for_bo(ScreenPtr screen, struct gbm_bo *bo,
                                    int depth,
                                    Bool implicit_modifier)
{
    PixmapPtr pixmap;
    struct xwl_pixmap *xwl_pixmap;
    struct xwl_screen *xwl_screen = xwl_screen_get(screen);
#ifdef GBM_BO_FD_FOR_PLANE
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);
    uint64_t modifier = gbm_bo_get_modifier(bo);
    const int num_planes = gbm_bo_get_plane_count(bo);
    int fds[GBM_MAX_PLANES];
    int plane;
    int attr_num = 0;
    EGLint img_attrs[64] = {0};
    enum PlaneAttrs {
        PLANE_FD,
        PLANE_OFFSET,
        PLANE_PITCH,
        PLANE_MODIFIER_LO,
        PLANE_MODIFIER_HI,
        NUM_PLANE_ATTRS
    };
    static const EGLint planeAttrs[][NUM_PLANE_ATTRS] = {
        {
            EGL_DMA_BUF_PLANE0_FD_EXT,
            EGL_DMA_BUF_PLANE0_OFFSET_EXT,
            EGL_DMA_BUF_PLANE0_PITCH_EXT,
            EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT,
            EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT,
        },
        {
            EGL_DMA_BUF_PLANE1_FD_EXT,
            EGL_DMA_BUF_PLANE1_OFFSET_EXT,
            EGL_DMA_BUF_PLANE1_PITCH_EXT,
            EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT,
            EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT,
        },
        {
            EGL_DMA_BUF_PLANE2_FD_EXT,
            EGL_DMA_BUF_PLANE2_OFFSET_EXT,
            EGL_DMA_BUF_PLANE2_PITCH_EXT,
            EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT,
            EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT,
        },
        {
            EGL_DMA_BUF_PLANE3_FD_EXT,
            EGL_DMA_BUF_PLANE3_OFFSET_EXT,
            EGL_DMA_BUF_PLANE3_PITCH_EXT,
            EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT,
            EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT,
        },
    };

    for (plane = 0; plane < num_planes; plane++) fds[plane] = -1;
#endif

    xwl_pixmap = calloc(1, sizeof(*xwl_pixmap));
    if (xwl_pixmap == NULL)
        return NULL;

    pixmap = glamor_create_pixmap(screen,
                                  gbm_bo_get_width(bo),
                                  gbm_bo_get_height(bo),
                                  depth,
                                  GLAMOR_CREATE_PIXMAP_NO_TEXTURE);
    if (!pixmap) {
        free(xwl_pixmap);
        return NULL;
    }

    xwl_glamor_egl_make_current(xwl_screen);
    xwl_pixmap->bo = bo;
    xwl_pixmap->buffer = NULL;
    xwl_pixmap->implicit_modifier = implicit_modifier;
#if defined(XWL_HAS_GLAMOR) && defined(DRI3)
    xwl_pixmap->efd = -1;
#endif /* defined(XWL_HAS_GLAMOR) && defined(DRI3) */

#ifdef GBM_BO_FD_FOR_PLANE
    if (xwl_gbm->dmabuf_capable) {
#define ADD_ATTR(attrs, num, attr)                                      \
        do {                                                            \
            assert(((num) + 1) < (sizeof(attrs) / sizeof((attrs)[0]))); \
            (attrs)[(num)++] = (attr);                                  \
        } while (0)
        ADD_ATTR(img_attrs, attr_num, EGL_WIDTH);
        ADD_ATTR(img_attrs, attr_num, gbm_bo_get_width(bo));
        ADD_ATTR(img_attrs, attr_num, EGL_HEIGHT);
        ADD_ATTR(img_attrs, attr_num, gbm_bo_get_height(bo));
        ADD_ATTR(img_attrs, attr_num, EGL_LINUX_DRM_FOURCC_EXT);
        ADD_ATTR(img_attrs, attr_num, gbm_bo_get_format(bo));

        for (plane = 0; plane < num_planes; plane++) {
            fds[plane] = gbm_bo_get_fd_for_plane(bo, plane);
            ADD_ATTR(img_attrs, attr_num, planeAttrs[plane][PLANE_FD]);
            ADD_ATTR(img_attrs, attr_num, fds[plane]);
            ADD_ATTR(img_attrs, attr_num, planeAttrs[plane][PLANE_OFFSET]);
            ADD_ATTR(img_attrs, attr_num, gbm_bo_get_offset(bo, plane));
            ADD_ATTR(img_attrs, attr_num, planeAttrs[plane][PLANE_PITCH]);
            ADD_ATTR(img_attrs, attr_num, gbm_bo_get_stride_for_plane(bo, plane));
            ADD_ATTR(img_attrs, attr_num, planeAttrs[plane][PLANE_MODIFIER_LO]);
            ADD_ATTR(img_attrs, attr_num, (uint32_t)(modifier & 0xFFFFFFFFULL));
            ADD_ATTR(img_attrs, attr_num, planeAttrs[plane][PLANE_MODIFIER_HI]);
            ADD_ATTR(img_attrs, attr_num, (uint32_t)(modifier >> 32ULL));
        }
        ADD_ATTR(img_attrs, attr_num, EGL_NONE);
#undef ADD_ATTR

        xwl_pixmap->image = eglCreateImageKHR(xwl_screen->egl_display,
                                              EGL_NO_CONTEXT,
                                              EGL_LINUX_DMA_BUF_EXT,
                                              NULL,
                                              img_attrs);

        for (plane = 0; plane < num_planes; plane++) {
            close(fds[plane]);
            fds[plane] = -1;
        }
    }
    else
#endif
    {
        xwl_pixmap->image = eglCreateImageKHR(xwl_screen->egl_display,
                                              EGL_NO_CONTEXT,
                                              EGL_NATIVE_PIXMAP_KHR,
                                              xwl_pixmap->bo, NULL);
    }

    if (xwl_pixmap->image == EGL_NO_IMAGE_KHR)
      goto error;

    glGenTextures(1, &xwl_pixmap->texture);
    glBindTexture(GL_TEXTURE_2D, xwl_pixmap->texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, xwl_pixmap->image);
    if (eglGetError() != EGL_SUCCESS)
      goto error;

    glBindTexture(GL_TEXTURE_2D, 0);

    if (!glamor_set_pixmap_texture(pixmap, xwl_pixmap->texture))
      goto error;

    glamor_set_pixmap_type(pixmap, GLAMOR_TEXTURE_DRM);
    xwl_pixmap_set_private(pixmap, xwl_pixmap);

    return pixmap;

error:
    if (xwl_pixmap->image != EGL_NO_IMAGE_KHR)
      eglDestroyImageKHR(xwl_screen->egl_display, xwl_pixmap->image);
    if (pixmap)
      glamor_destroy_pixmap(pixmap);
    free(xwl_pixmap);

    return NULL;
}

static PixmapPtr
xwl_glamor_gbm_create_pixmap_internal(struct xwl_screen *xwl_screen,
                                      DrawablePtr drawable,
                                      int width, int height, int depth,
                                      unsigned int hint,
                                      Bool implicit_scanout)
{
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);
    struct gbm_bo *bo = NULL;
    PixmapPtr pixmap = NULL;
    uint32_t num_modifiers = 0;
    uint64_t *modifiers = NULL;

    if (width > 0 && height > 0 && depth >= 15 &&
        (hint == CREATE_PIXMAP_USAGE_BACKING_PIXMAP ||
         hint == CREATE_PIXMAP_USAGE_SHARED ||
         (xwl_screen->rootless && hint == 0))) {
        uint32_t format = gbm_format_for_depth(depth, xwl_gbm->glamor_gles);
        Bool implicit = FALSE;

#ifdef GBM_BO_WITH_MODIFIERS
        if (xwl_gbm->dmabuf_capable) {
            Bool supports_scanout = FALSE;

            if (drawable) {
                xwl_glamor_get_drawable_modifiers_and_scanout(drawable,
                                                              format,
                                                              &num_modifiers,
                                                              &modifiers,
                                                              &supports_scanout);
            }

            if (num_modifiers == 0) {
                xwl_glamor_get_modifiers(xwl_screen->screen, format,
                                         &num_modifiers, &modifiers);
            }

            if (num_modifiers > 0) {
#ifdef GBM_BO_WITH_MODIFIERS2
                uint32_t usage = GBM_BO_USE_RENDERING;
                if (supports_scanout)
                    usage |= GBM_BO_USE_SCANOUT;
                bo = gbm_bo_create_with_modifiers2(xwl_gbm->gbm, width, height,
                                                   format, modifiers, num_modifiers,
                                                   usage);
#else
                bo = gbm_bo_create_with_modifiers(xwl_gbm->gbm, width, height,
                                                  format, modifiers, num_modifiers);
#endif
            }
        }
#endif
        if (bo == NULL) {
            uint32_t usage = GBM_BO_USE_RENDERING;
            implicit = TRUE;
            if (implicit_scanout)
                usage |= GBM_BO_USE_SCANOUT;

            if (num_modifiers > 0) {
                Bool has_mod_invalid = FALSE, has_mod_linear = FALSE;
                int i;

                for (i = 0; i < num_modifiers; i++) {
                    if (modifiers[i] == DRM_FORMAT_MOD_INVALID)
                        has_mod_invalid = TRUE;
                    else if (modifiers[i] == DRM_FORMAT_MOD_LINEAR)
                        has_mod_linear = TRUE;
                }

                if (!has_mod_invalid && has_mod_linear)
                    usage |= GBM_BO_USE_LINEAR;
            }

            bo = gbm_bo_create(xwl_gbm->gbm, width, height, format, usage);
        }

        if (bo) {
            pixmap = xwl_glamor_gbm_create_pixmap_for_bo(xwl_screen->screen, bo, depth, implicit);

            if (!pixmap) {
                gbm_bo_destroy(bo);
            }
            else if (xwl_screen->rootless && hint == CREATE_PIXMAP_USAGE_BACKING_PIXMAP) {
                glamor_clear_pixmap(pixmap);
            }
        }
    }

    if (!pixmap)
        pixmap = glamor_create_pixmap(xwl_screen->screen, width, height, depth, hint);

    free(modifiers);
    return pixmap;
}

static PixmapPtr
xwl_glamor_gbm_create_pixmap(ScreenPtr screen,
                             int width, int height, int depth,
                             unsigned int hint)
{
    return xwl_glamor_gbm_create_pixmap_internal(xwl_screen_get(screen), NULL,
                                                 width, height, depth, hint, FALSE);
}

PixmapPtr
xwl_glamor_create_pixmap_for_window(struct xwl_window *xwl_window)
{
    struct xwl_screen *xwl_screen = xwl_window->xwl_screen;
    WindowPtr window = xwl_window->surface_window;
    unsigned border_width = 2 * window->borderWidth;

    if (!xwl_screen->glamor)
        return NullPixmap;

    return xwl_glamor_gbm_create_pixmap_internal(xwl_screen,
                                                 &window->drawable,
                                                 window->drawable.width + border_width,
                                                 window->drawable.height + border_width,
                                                 window->drawable.depth,
                                                 CREATE_PIXMAP_USAGE_BACKING_PIXMAP,
                                                 xwl_window->has_implicit_scanout_support);
}

static Bool
xwl_glamor_gbm_destroy_pixmap(PixmapPtr pixmap)
{
    struct xwl_screen *xwl_screen = xwl_screen_get(pixmap->drawable.pScreen);
    struct xwl_pixmap *xwl_pixmap = xwl_pixmap_get(pixmap);

    if (xwl_pixmap && pixmap->refcnt == 1) {
        xwl_pixmap_del_buffer_release_cb(pixmap);
        if (xwl_pixmap->buffer)
            wl_buffer_destroy(xwl_pixmap->buffer);

        eglDestroyImageKHR(xwl_screen->egl_display, xwl_pixmap->image);
        if (xwl_pixmap->bo)
           gbm_bo_destroy(xwl_pixmap->bo);
        xwl_glamor_gbm_dispose_syncpts(pixmap);
        free(xwl_pixmap);
    }

    return glamor_destroy_pixmap(pixmap);
}

static const struct wl_buffer_listener xwl_glamor_gbm_buffer_listener = {
    xwl_pixmap_buffer_release_cb,
};

#ifdef GBM_BO_WITH_MODIFIERS
static Bool
init_buffer_params_with_modifiers(struct xwl_pixmap *xwl_pixmap,
                                  uint64_t          *modifier,
                                  int               *num_planes,
                                  int               *prime_fds,
                                  uint32_t          *strides,
                                  uint32_t          *offsets)
{
#ifndef GBM_BO_FD_FOR_PLANE
    int32_t first_handle;
#endif
    int i;

    *num_planes = gbm_bo_get_plane_count(xwl_pixmap->bo);
    *modifier = gbm_bo_get_modifier(xwl_pixmap->bo);

    for (i = 0; i < *num_planes; i++) {
#ifdef GBM_BO_FD_FOR_PLANE
        prime_fds[i] = gbm_bo_get_fd_for_plane(xwl_pixmap->bo, i);
#else
        union gbm_bo_handle plane_handle;

        plane_handle = gbm_bo_get_handle_for_plane(xwl_pixmap->bo, i);
        if (i == 0)
            first_handle = plane_handle.s32;

        /* If all planes point to the same object as the first plane, i.e. they
         * all have the same handle, we can fall back to the non-planar
         * gbm_bo_get_fd without losing information. If they point to different
         * objects we are out of luck and need to give up.
         */
        if (first_handle == plane_handle.s32)
            prime_fds[i] = gbm_bo_get_fd(xwl_pixmap->bo);
        else
            prime_fds[i] = -1;
#endif
        if (prime_fds[i] == -1) {
            while (--i >= 0)
                close(prime_fds[i]);
            return FALSE;
        }
        strides[i] = gbm_bo_get_stride_for_plane(xwl_pixmap->bo, i);
        offsets[i] = gbm_bo_get_offset(xwl_pixmap->bo, i);
    }

    return TRUE;
}
#endif

static Bool
init_buffer_params_fallback(struct xwl_pixmap *xwl_pixmap,
                            uint64_t          *modifier,
                            int               *num_planes,
                            int               *prime_fds,
                            uint32_t          *strides,
                            uint32_t          *offsets)
{
    *num_planes = 1;
    *modifier = DRM_FORMAT_MOD_INVALID;
    prime_fds[0] = gbm_bo_get_fd(xwl_pixmap->bo);
    if (prime_fds[0] == -1)
        return FALSE;

    strides[0] = gbm_bo_get_stride(xwl_pixmap->bo);
    offsets[0] = 0;

    return TRUE;
}

struct wl_buffer *
xwl_glamor_pixmap_get_wl_buffer(PixmapPtr pixmap)
{
    struct xwl_screen *xwl_screen = xwl_screen_get(pixmap->drawable.pScreen);
    struct xwl_pixmap *xwl_pixmap = xwl_pixmap_get(pixmap);
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);
    unsigned short width = pixmap->drawable.width;
    unsigned short height = pixmap->drawable.height;
    uint32_t format;
    int num_planes;
    int prime_fds[4];
    uint32_t strides[4];
    uint32_t offsets[4];
    uint64_t modifier;
    int i;

    if (xwl_pixmap == NULL)
       return NULL;

    if (xwl_pixmap->buffer) {
        /* Buffer already exists. */
        return xwl_pixmap->buffer;
    }

    if (!xwl_pixmap->bo)
       return NULL;

    format = wl_drm_format_for_depth(pixmap->drawable.depth);

#ifdef GBM_BO_WITH_MODIFIERS
    if (!xwl_pixmap->implicit_modifier) {
        if (!init_buffer_params_with_modifiers(xwl_pixmap,
                                               &modifier,
                                               &num_planes,
                                               prime_fds,
                                               strides,
                                               offsets))
            return NULL;
    } else
#endif
    {
        if (!init_buffer_params_fallback(xwl_pixmap,
                                         &modifier,
                                         &num_planes,
                                         prime_fds,
                                         strides,
                                         offsets))
            return NULL;
    }

    if (xwl_screen->dmabuf &&
        xwl_glamor_is_modifier_supported(xwl_screen, format, modifier)) {
        struct zwp_linux_buffer_params_v1 *params;

        params = zwp_linux_dmabuf_v1_create_params(xwl_screen->dmabuf);
        for (i = 0; i < num_planes; i++) {
            zwp_linux_buffer_params_v1_add(params, prime_fds[i], i,
                                           offsets[i], strides[i],
                                           modifier >> 32, modifier & 0xffffffff);
        }

        xwl_pixmap->buffer =
           zwp_linux_buffer_params_v1_create_immed(params, width, height,
                                                   format, 0);
        zwp_linux_buffer_params_v1_destroy(params);
    } else if (num_planes == 1 && modifier == DRM_FORMAT_MOD_INVALID &&
               xwl_gbm->drm) {
        xwl_pixmap->buffer =
            wl_drm_create_prime_buffer(xwl_gbm->drm, prime_fds[0], width, height,
                                       format,
                                       0, gbm_bo_get_stride(xwl_pixmap->bo),
                                       0, 0,
                                       0, 0);
    }

    for (i = 0; i < num_planes; i++)
        close(prime_fds[i]);

    /* Add our listener now */
    if (xwl_pixmap->buffer)
        wl_buffer_add_listener(xwl_pixmap->buffer,
                               &xwl_glamor_gbm_buffer_listener, pixmap);

    return xwl_pixmap->buffer;
}

static void
xwl_glamor_gbm_cleanup(struct xwl_screen *xwl_screen)
{
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);

    if (xwl_gbm->device_name)
        free(xwl_gbm->device_name);
    drmFreeDevice(&xwl_gbm->device);
    if (xwl_gbm->drm_fd)
        close(xwl_gbm->drm_fd);
    if (xwl_gbm->drm)
        wl_drm_destroy(xwl_gbm->drm);
    if (xwl_gbm->gbm)
        gbm_device_destroy(xwl_gbm->gbm);
    if (xwl_screen->explicit_sync)
        wp_linux_drm_syncobj_manager_v1_destroy(xwl_screen->explicit_sync);

    free(xwl_gbm);
}

struct xwl_auth_state {
    int fd;
    ClientPtr client;
    struct wl_callback *callback;
};

static void
free_xwl_auth_state(ClientPtr pClient, struct xwl_auth_state *state)
{
    dixSetPrivate(&pClient->devPrivates, &xwl_auth_state_private_key, NULL);
    if (state) {
        wl_callback_destroy(state->callback);
        free(state);
    }
}

static void
xwl_auth_state_client_callback(CallbackListPtr *pcbl, void *unused, void *data)
{
    NewClientInfoRec *clientinfo = (NewClientInfoRec *) data;
    ClientPtr pClient = clientinfo->client;
    struct xwl_auth_state *state;

    switch (pClient->clientState) {
    case ClientStateGone:
    case ClientStateRetained:
        state = dixLookupPrivate(&pClient->devPrivates,
                                 &xwl_auth_state_private_key);
        free_xwl_auth_state(pClient, state);
        break;
    default:
        break;
    }
}

#ifdef DRI3
static void
sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
{
    struct xwl_auth_state *state = data;
    ClientPtr client = state->client;

    /* if the client is gone, the callback is cancelled so it's safe to
     * assume the client is still in ClientStateRunning at this point...
     */
    dri3_send_open_reply(client, state->fd);
    AttendClient(client);
    free_xwl_auth_state(client, state);
}

static const struct wl_callback_listener sync_listener = {
   sync_callback
};

static int
xwl_dri3_open_client(ClientPtr client,
                     ScreenPtr screen,
                     RRProviderPtr provider,
                     int *pfd)
{
    struct xwl_screen *xwl_screen = xwl_screen_get(screen);
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);
    struct xwl_auth_state *state;
    drm_magic_t magic;
    int fd;

    fd = open(xwl_gbm->device_name, O_RDWR | O_CLOEXEC);
    if (fd < 0)
        return BadAlloc;
    if (xwl_gbm->fd_render_node) {
        *pfd = fd;
        return Success;
    }

    state = malloc(sizeof *state);
    if (state == NULL) {
        close(fd);
        return BadAlloc;
    }

    state->client = client;
    state->fd = fd;

    if (drmGetMagic(state->fd, &magic) < 0) {
        close(state->fd);
        free(state);
        return BadMatch;
    }

    wl_drm_authenticate(xwl_gbm->drm, magic);
    state->callback = wl_display_sync(xwl_screen->display);
    wl_callback_add_listener(state->callback, &sync_listener, state);
    dixSetPrivate(&client->devPrivates, &xwl_auth_state_private_key, state);

    IgnoreClient(client);

    return Success;
}
#endif /* DRI3 */

PixmapPtr
glamor_pixmap_from_fds(ScreenPtr screen, CARD8 num_fds, const int *fds,
                       CARD16 width, CARD16 height,
                       const CARD32 *strides, const CARD32 *offsets,
                       CARD8 depth, CARD8 bpp, uint64_t modifier)
{
    struct xwl_screen *xwl_screen = xwl_screen_get(screen);
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);
    struct gbm_bo *bo = NULL;
    PixmapPtr pixmap;
    int i;
    Bool implicit = FALSE;

    if (width == 0 || height == 0 || num_fds == 0 ||
        depth < 15 || bpp != BitsPerPixel(depth) ||
        strides[0] < width * bpp / 8)
       goto error;

    if (xwl_gbm->dmabuf_capable && modifier != DRM_FORMAT_MOD_INVALID) {
#ifdef GBM_BO_WITH_MODIFIERS
       struct gbm_import_fd_modifier_data data;

       data.width = width;
       data.height = height;
       data.num_fds = num_fds;
       data.format = gbm_format_for_depth(depth, xwl_gbm->glamor_gles);
       data.modifier = modifier;
       for (i = 0; i < num_fds; i++) {
          data.fds[i] = fds[i];
          data.strides[i] = strides[i];
          data.offsets[i] = offsets[i];
       }
       bo = gbm_bo_import(xwl_gbm->gbm, GBM_BO_IMPORT_FD_MODIFIER, &data,
                          GBM_BO_USE_RENDERING);
#endif
    } else if (num_fds == 1) {
       struct gbm_import_fd_data data;

       data.fd = fds[0];
       data.width = width;
       data.height = height;
       data.stride = strides[0];
       data.format = gbm_format_for_depth(depth, xwl_gbm->glamor_gles);
       bo = gbm_bo_import(xwl_gbm->gbm, GBM_BO_IMPORT_FD, &data,
                          GBM_BO_USE_RENDERING);
       implicit = TRUE;
    } else {
       goto error;
    }

    if (bo == NULL)
       goto error;

    pixmap = xwl_glamor_gbm_create_pixmap_for_bo(screen, bo, depth, implicit);
    if (pixmap == NULL) {
       gbm_bo_destroy(bo);
       goto error;
    }

    return pixmap;

error:
    return NULL;
}

int
glamor_egl_fds_from_pixmap(ScreenPtr screen, PixmapPtr pixmap, int *fds,
                           uint32_t *strides, uint32_t *offsets,
                           uint64_t *modifier)
{
    struct xwl_pixmap *xwl_pixmap;
#ifdef GBM_BO_WITH_MODIFIERS
#ifndef GBM_BO_FD_FOR_PLANE
    int32_t first_handle;
#endif
    uint32_t num_fds;
    int i;
#endif

    xwl_pixmap = xwl_pixmap_get(pixmap);

    if (xwl_pixmap == NULL)
       return 0;

    if (!xwl_pixmap->bo)
       return 0;

#ifdef GBM_BO_WITH_MODIFIERS
    num_fds = gbm_bo_get_plane_count(xwl_pixmap->bo);
    *modifier = gbm_bo_get_modifier(xwl_pixmap->bo);

    for (i = 0; i < num_fds; i++) {
#ifdef GBM_BO_FD_FOR_PLANE
        fds[i] = gbm_bo_get_fd_for_plane(xwl_pixmap->bo, i);
#else
        union gbm_bo_handle plane_handle;

        plane_handle = gbm_bo_get_handle_for_plane(xwl_pixmap->bo, i);
        if (i == 0)
            first_handle = plane_handle.s32;

        /* If all planes point to the same object as the first plane, i.e. they
         * all have the same handle, we can fall back to the non-planar
         * gbm_bo_get_fd without losing information. If they point to different
         * objects we are out of luck and need to give up.
         */
        if (first_handle == plane_handle.s32)
            fds[i] = gbm_bo_get_fd(xwl_pixmap->bo);
        else
            fds[i] = -1;
#endif
        if (fds[i] == -1) {
            while (--i >= 0)
                close(fds[i]);
            return 0;
        }
        strides[i] = gbm_bo_get_stride_for_plane(xwl_pixmap->bo, i);
        offsets[i] = gbm_bo_get_offset(xwl_pixmap->bo, i);
    }

    return num_fds;
#else
    *modifier = DRM_FORMAT_MOD_INVALID;
    fds[0] = gbm_bo_get_fd(xwl_pixmap->bo);
    if (fds[0] == -1)
        return 0;
    strides[0] = gbm_bo_get_stride(xwl_pixmap->bo);
    offsets[0] = 0;
    return 1;
#endif
}

/* Not actually used, just defined here so there's something for
 * _glamor_egl_fds_from_pixmap() to link against
 */
int
glamor_egl_fd_from_pixmap(ScreenPtr screen, PixmapPtr pixmap,
                          CARD16 *stride, CARD32 *size)
{
    return -1;
}

int
xwl_glamor_dmabuf_export_sync_file(PixmapPtr pixmap)
{
    struct xwl_screen *xwl_screen = xwl_screen_get(pixmap->drawable.pScreen);
    struct xwl_pixmap *xwl_pixmap;
    int num_planes;
    int sync_file = -1;
    int p;
#ifndef GBM_BO_FD_FOR_PLANE
    int32_t first_handle;
#endif

    if (!xwl_screen->glamor)
        return -1;

#ifdef DMA_BUF_IOCTL_EXPORT_SYNC_FILE
    xwl_pixmap = xwl_pixmap_get(pixmap);
    num_planes = gbm_bo_get_plane_count(xwl_pixmap->bo);

    for (p = 0; p < num_planes; ++p) {
        int plane_fd;
#ifdef GBM_BO_FD_FOR_PLANE
        plane_fd = gbm_bo_get_fd_for_plane(xwl_pixmap->bo, p);
#else
        union gbm_bo_handle plane_handle =
            gbm_bo_get_handle_for_plane(xwl_pixmap->bo, p);
        if (p == 0)
            first_handle = plane_handle.s32;

        if (plane_handle.s32 == first_handle)
            plane_fd = gbm_bo_get_fd(xwl_pixmap->bo);
        else
            continue;
#endif /* GBM_BO_FD_FOR_PLANE */
        struct dma_buf_export_sync_file export_args = { 0 };
        export_args.fd = -1;
        export_args.flags = DMA_BUF_SYNC_READ;
        drmIoctl(plane_fd, DMA_BUF_IOCTL_EXPORT_SYNC_FILE, &export_args);
        close(plane_fd);
        if (sync_file == -1) {
            sync_file = export_args.fd;
        } else {
            struct sync_merge_data merge_args = { 0 };
            merge_args.fd2 = export_args.fd;
            ioctl(sync_file, SYNC_IOC_MERGE, &merge_args);
            close(export_args.fd);
            close(sync_file);
            sync_file = merge_args.fence;
        }
    }
#endif /* DMA_BUF_IOCTL_EXPORT_SYNC_FILE */
    return sync_file;
}

void
xwl_glamor_dmabuf_import_sync_file(PixmapPtr pixmap, int sync_file)
{
    struct xwl_screen *xwl_screen = xwl_screen_get(pixmap->drawable.pScreen);
    struct xwl_pixmap *xwl_pixmap;
    int num_planes;
    int p;
#ifndef GBM_BO_FD_FOR_PLANE
    int32_t first_handle;
#endif

    if (!xwl_screen->glamor)
        return;

#ifdef DMA_BUF_IOCTL_IMPORT_SYNC_FILE
    xwl_pixmap = xwl_pixmap_get(pixmap);
    num_planes = gbm_bo_get_plane_count(xwl_pixmap->bo);

    for (p = 0; p < num_planes; ++p) {
        int plane_fd;
#ifdef GBM_BO_FD_FOR_PLANE
        plane_fd = gbm_bo_get_fd_for_plane(xwl_pixmap->bo, p);
#else
        union gbm_bo_handle plane_handle =
            gbm_bo_get_handle_for_plane(xwl_pixmap->bo, p);
        if (p == 0)
            first_handle = plane_handle.s32;

        if (plane_handle.s32 == first_handle)
            plane_fd = gbm_bo_get_fd(xwl_pixmap->bo);
        else
            continue;
#endif /* GBM_BO_FD_FOR_PLANE */
        struct dma_buf_import_sync_file import_args = { 0 };
        import_args.fd = sync_file;
        import_args.flags = DMA_BUF_SYNC_WRITE;
        drmIoctl(plane_fd, DMA_BUF_IOCTL_IMPORT_SYNC_FILE, &import_args);
        close(plane_fd);
    }
#endif /* DMA_BUF_IOCTL_IMPORT_SYNC_FILE */
    close(sync_file);
}

#ifdef DRI3
struct xwl_dri3_syncobj
{
    struct dri3_syncobj base;
    uint32_t handle;
    struct wp_linux_drm_syncobj_timeline_v1 *timeline;
};

void
xwl_glamor_dri3_syncobj_passthrough(struct xwl_window *xwl_window,
                                    struct dri3_syncobj *acquire_syncobj,
                                    struct dri3_syncobj *release_syncobj,
                                    uint64_t acquire_point,
                                    uint64_t release_point)
{
    struct xwl_screen *xwl_screen = xwl_window->xwl_screen;
    struct xwl_dri3_syncobj *xwl_acquire_syncobj = (struct xwl_dri3_syncobj *)acquire_syncobj;
    struct xwl_dri3_syncobj *xwl_release_syncobj = (struct xwl_dri3_syncobj *)release_syncobj;
    uint32_t acquire_hi = acquire_point >> 32;
    uint32_t acquire_lo = acquire_point & 0xffffffff;
    uint32_t release_hi = release_point >> 32;
    uint32_t release_lo = release_point & 0xffffffff;

    if (!xwl_window->surface_sync)
        xwl_window->surface_sync =
            wp_linux_drm_syncobj_manager_v1_get_surface(xwl_screen->explicit_sync,
                                                        xwl_window->surface);

    wp_linux_drm_syncobj_surface_v1_set_acquire_point(xwl_window->surface_sync,
                                                      xwl_acquire_syncobj->timeline,
                                                      acquire_hi, acquire_lo);
    wp_linux_drm_syncobj_surface_v1_set_release_point(xwl_window->surface_sync,
                                                      xwl_release_syncobj->timeline,
                                                      release_hi, release_lo);
}

static Bool
xwl_dri3_check_syncobj(struct dri3_syncobj *syncobj, uint64_t point, Bool check_avail)
{
    struct xwl_dri3_syncobj *xwl_syncobj = (struct xwl_dri3_syncobj *)syncobj;
    struct xwl_screen *xwl_screen = xwl_screen_get(syncobj->screen);
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);

    return !drmSyncobjTimelineWait(xwl_gbm->drm_fd,
                                   &xwl_syncobj->handle, &point, 1,
                                   0 /* timeout */,
                                   check_avail ?
                                   DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE :
                                   DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT,
                                   NULL /* first_signaled */);
}

static Bool
xwl_dri3_syncobj_has_fence(struct dri3_syncobj *syncobj, uint64_t point)
{
    return xwl_dri3_check_syncobj(syncobj, point, TRUE /* check_avail */);
}

static Bool
xwl_dri3_syncobj_is_signaled(struct dri3_syncobj *syncobj, uint64_t point)
{
    return xwl_dri3_check_syncobj(syncobj, point, FALSE /* check_avail */);
}

static int
xwl_dri3_syncobj_export_fence(struct dri3_syncobj *syncobj, uint64_t point)
{
    struct xwl_dri3_syncobj *xwl_syncobj = (struct xwl_dri3_syncobj *)syncobj;
    struct xwl_screen *xwl_screen = xwl_screen_get(syncobj->screen);
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);
    uint32_t temp_syncobj;
    int fd = -1;

    drmSyncobjCreate(xwl_gbm->drm_fd, 0, &temp_syncobj);
    drmSyncobjTransfer(xwl_gbm->drm_fd, temp_syncobj, 0,
                       xwl_syncobj->handle, point, 0);
    drmSyncobjExportSyncFile(xwl_gbm->drm_fd, temp_syncobj, &fd);
    drmSyncobjDestroy(xwl_gbm->drm_fd, temp_syncobj);
    return fd;
}

static void
xwl_dri3_syncobj_import_fence(struct dri3_syncobj *syncobj,
                              uint64_t point, int fd)
{
    struct xwl_dri3_syncobj *xwl_syncobj = (struct xwl_dri3_syncobj *)syncobj;
    struct xwl_screen *xwl_screen = xwl_screen_get(syncobj->screen);
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);
    uint32_t temp_syncobj;

    drmSyncobjCreate(xwl_gbm->drm_fd, 0, &temp_syncobj);
    drmSyncobjImportSyncFile(xwl_gbm->drm_fd, temp_syncobj, fd);
    drmSyncobjTransfer(xwl_gbm->drm_fd, xwl_syncobj->handle, point,
                       temp_syncobj, 0, 0);
    drmSyncobjDestroy(xwl_gbm->drm_fd, temp_syncobj);
    close(fd);
}

static void
xwl_dri3_signal_syncobj(struct dri3_syncobj *syncobj, uint64_t point)
{
    struct xwl_dri3_syncobj *xwl_syncobj = (struct xwl_dri3_syncobj *)syncobj;
    struct xwl_screen *xwl_screen = xwl_screen_get(syncobj->screen);
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);

    drmSyncobjTimelineSignal(xwl_gbm->drm_fd, &xwl_syncobj->handle, &point, 1);
}

static void
xwl_dri3_free_syncobj(struct dri3_syncobj *syncobj)
{
    struct xwl_dri3_syncobj *xwl_syncobj = (struct xwl_dri3_syncobj *)syncobj;
    struct xwl_screen *xwl_screen = xwl_screen_get(syncobj->screen);
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);

    if (xwl_syncobj->timeline)
        wp_linux_drm_syncobj_timeline_v1_destroy(xwl_syncobj->timeline);

    if (xwl_syncobj->handle)
        drmSyncobjDestroy(xwl_gbm->drm_fd, xwl_syncobj->handle);

    free(xwl_syncobj);
}

static void
xwl_dri3_syncobj_eventfd(struct dri3_syncobj *syncobj, uint64_t point,
                         int efd, Bool wait_avail)
{
    struct xwl_dri3_syncobj *xwl_syncobj = (struct xwl_dri3_syncobj *)syncobj;
    struct xwl_screen *xwl_screen = xwl_screen_get(syncobj->screen);
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);

    drmSyncobjEventfd(xwl_gbm->drm_fd, xwl_syncobj->handle, point, efd,
                      wait_avail ? DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE : 0);
}

static void
xwl_dri3_syncobj_submitted_eventfd(struct dri3_syncobj *syncobj,
                                   uint64_t point, int efd)
{
    xwl_dri3_syncobj_eventfd(syncobj, point, efd, TRUE /* wait_avail */);
}

static void
xwl_dri3_syncobj_signaled_eventfd(struct dri3_syncobj *syncobj,
                                  uint64_t point, int efd)
{
    xwl_dri3_syncobj_eventfd(syncobj, point, efd, FALSE /* wait_avail */);
}

static struct dri3_syncobj *
xwl_dri3_create_syncobj(struct xwl_screen *xwl_screen, uint32_t handle)
{
    struct xwl_dri3_syncobj *syncobj = calloc(1, sizeof (*syncobj));
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);
    Bool create = !handle;

    if (!syncobj)
        return NULL;

    if (create && drmSyncobjCreate(xwl_gbm->drm_fd, 0, &handle))
        goto fail;

    if (xwl_screen->explicit_sync) {
        int syncobj_fd = -1;
        if (drmSyncobjHandleToFD(xwl_gbm->drm_fd, handle, &syncobj_fd))
            goto fail;

        syncobj->timeline =
            wp_linux_drm_syncobj_manager_v1_import_timeline(xwl_screen->explicit_sync,
                                                            syncobj_fd);
        close(syncobj_fd);
        if (!syncobj->timeline)
            goto fail;
    }

    syncobj->handle = handle;
    syncobj->base.screen = xwl_screen->screen;
    syncobj->base.refcount = 1;

    syncobj->base.free = xwl_dri3_free_syncobj;
    syncobj->base.has_fence = xwl_dri3_syncobj_has_fence;
    syncobj->base.is_signaled = xwl_dri3_syncobj_is_signaled;
    syncobj->base.export_fence = xwl_dri3_syncobj_export_fence;
    syncobj->base.import_fence = xwl_dri3_syncobj_import_fence;
    syncobj->base.signal = xwl_dri3_signal_syncobj;
    syncobj->base.signaled_eventfd = xwl_dri3_syncobj_signaled_eventfd;
    syncobj->base.submitted_eventfd = xwl_dri3_syncobj_submitted_eventfd;
    return &syncobj->base;

fail:
    if (create && handle)
        drmSyncobjDestroy(xwl_gbm->drm_fd, handle);
    free(syncobj);
    return NULL;
}

struct dri3_syncobj *
xwl_glamor_dri3_syncobj_create(struct xwl_screen *xwl_screen)
{
    return xwl_dri3_create_syncobj(xwl_screen, 0 /* allocate new handle */);
}

static struct dri3_syncobj *
xwl_dri3_import_syncobj(ClientPtr client, ScreenPtr screen, XID id, int fd)
{
    struct xwl_screen *xwl_screen = xwl_screen_get(screen);
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);
    struct xwl_dri3_syncobj *syncobj = NULL;
    uint32_t handle;

    if (drmSyncobjFDToHandle(xwl_gbm->drm_fd, fd, &handle))
        return NULL;

    syncobj = (struct xwl_dri3_syncobj *)xwl_dri3_create_syncobj(xwl_screen, handle);
    if (!syncobj) {
        drmSyncobjDestroy(xwl_gbm->drm_fd, handle);
        return NULL;
    }

    syncobj->base.id = id;

    return &syncobj->base;
}

static Bool
xwl_gbm_supports_syncobjs(struct xwl_screen *xwl_screen)
{
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);
    uint64_t syncobj_cap = 0;

    if (drmGetCap(xwl_gbm->drm_fd, DRM_CAP_SYNCOBJ_TIMELINE,
                  &syncobj_cap) || !syncobj_cap)
        return FALSE;

    /* Check if syncobj eventfd is supported. */
    drmSyncobjEventfd(xwl_gbm->drm_fd, 0, 0, -1, 0);
    if (errno != ENOENT)
        return FALSE;

#if !defined(DMA_BUF_IOCTL_EXPORT_SYNC_FILE) || \
    !defined(DMA_BUF_IOCTL_IMPORT_SYNC_FILE)
    return FALSE;
#else
    return TRUE;
#endif
}

static dri3_screen_info_rec xwl_dri3_info = {
    .version = 2,
    .open = NULL,
    .pixmap_from_fds = glamor_pixmap_from_fds,
    .fds_from_pixmap = glamor_fds_from_pixmap,
    .open_client = xwl_dri3_open_client,
    .get_formats = xwl_glamor_get_formats,
    .get_modifiers = xwl_glamor_get_modifiers,
    .get_drawable_modifiers = xwl_glamor_get_drawable_modifiers,
    .import_syncobj = NULL, /* need to check for kernel support */
};
#endif /* DRI3 */

static const char *
get_render_node_path_for_device(const drmDevicePtr drm_device,
                                const char *device_path)
{
    const char *render_node_path;
    int i;

    if (!(drm_device->available_nodes & (1 << DRM_NODE_RENDER)))
        return NULL;
    render_node_path = drm_device->nodes[DRM_NODE_RENDER];

    for (i = 0; i < DRM_NODE_MAX; i++) {
        if (!(drm_device->available_nodes & (1 << i)))
            continue;
        if (strcmp(device_path, drm_device->nodes[i]) == 0)
            return render_node_path;
    }

    return NULL;
}

static char *
get_render_node_path(const char *device_path)
{
    drmDevicePtr *devices = NULL;
    char *render_node_path = NULL;
    int i, n_devices, max_devices;

    max_devices = drmGetDevices2(0, NULL, 0);
    if (max_devices <= 0)
        goto out;

    devices = calloc(max_devices, sizeof(drmDevicePtr));
    if (!devices)
        goto out;

    n_devices = drmGetDevices2(0, devices, max_devices);
    if (n_devices < 0)
        goto out;

    for (i = 0; i < n_devices; i++) {
       const char *node_path = get_render_node_path_for_device(devices[i],
                                                               device_path);
       if (node_path) {
           render_node_path = strdup(node_path);
           break;
       }
    }

out:
    free(devices);
    return render_node_path;
}

static void
xwl_drm_handle_device(void *data, struct wl_drm *drm, const char *device)
{
   struct xwl_screen *xwl_screen = data;
   struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);
   drm_magic_t magic;
   char *render_node_path = NULL;

   if (!is_device_path_render_node(device))
       render_node_path = get_render_node_path(device);

   if (render_node_path)
       xwl_gbm->device_name = render_node_path;
   else
       xwl_gbm->device_name = strdup(device);

   if (!xwl_gbm->device_name) {
       xwl_glamor_gbm_cleanup(xwl_screen);
       return;
   }

   xwl_gbm->drm_fd = open(xwl_gbm->device_name, O_RDWR | O_CLOEXEC);
   if (xwl_gbm->drm_fd == -1) {
       ErrorF("wayland-egl: could not open %s (%s)\n",
              xwl_gbm->device_name, strerror(errno));
       xwl_glamor_gbm_cleanup(xwl_screen);
       return;
   }

   if (drmGetDevice2(xwl_gbm->drm_fd, 0, &xwl_gbm->device) != 0) {
       ErrorF("wayland-egl: Could not fetch DRM device %s\n",
              xwl_gbm->device_name);
       return;
   }

   if (drmGetNodeTypeFromFd(xwl_gbm->drm_fd) == DRM_NODE_RENDER) {
       xwl_gbm->fd_render_node = TRUE;
       xwl_screen->expecting_event--;
   } else {
       drmGetMagic(xwl_gbm->drm_fd, &magic);
       wl_drm_authenticate(xwl_gbm->drm, magic);
   }
}

static void
xwl_drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
{
}

static void
xwl_drm_handle_authenticated(void *data, struct wl_drm *drm)
{
    struct xwl_screen *xwl_screen = data;
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);

    xwl_gbm->drm_authenticated = TRUE;
    xwl_screen->expecting_event--;
}

static void
xwl_drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
{
    xwl_gbm_get(data)->capabilities = value;
}

static const struct wl_drm_listener xwl_drm_listener = {
    xwl_drm_handle_device,
    xwl_drm_handle_format,
    xwl_drm_handle_authenticated,
    xwl_drm_handle_capabilities
};

Bool
xwl_screen_set_drm_interface(struct xwl_screen *xwl_screen,
                             uint32_t id, uint32_t version)
{
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);

    if (version < 2)
        return FALSE;

    xwl_gbm->drm =
        wl_registry_bind(xwl_screen->registry, id, &wl_drm_interface, 2);
    wl_drm_add_listener(xwl_gbm->drm, &xwl_drm_listener, xwl_screen);
    xwl_screen->expecting_event++;

    return TRUE;
}

Bool
xwl_screen_set_syncobj_interface(struct xwl_screen *xwl_screen,
                                 uint32_t id, uint32_t version)
{
    xwl_screen->explicit_sync =
        wl_registry_bind(xwl_screen->registry, id,
                         &wp_linux_drm_syncobj_manager_v1_interface,
                         version);
    return TRUE;
}

static Bool
xwl_glamor_gbm_has_egl_extension(void)
{
    return (epoxy_has_egl_extension(NULL, "EGL_MESA_platform_gbm") ||
            epoxy_has_egl_extension(NULL, "EGL_KHR_platform_gbm"));
}

#ifdef DRI3
static void
xwl_glamor_gbm_release_fence_avail(int fd, int xevents, void *data)
{
    struct xwl_pixmap *xwl_pixmap = data;
    struct xwl_window_buffer *xwl_window_buffer = xwl_pixmap->xwl_window_buffer;

    SetNotifyFd(fd, NULL, 0, NULL);
    close(fd);
    xwl_pixmap->efd = -1;

    xwl_window_buffer_release(xwl_window_buffer);
}
#endif /* DRI3 */

Bool
xwl_glamor_supports_implicit_sync(struct xwl_screen *xwl_screen)
{
    /* absent glamor, implicit sync is irrelevant so just return TRUE */
    return !xwl_screen->glamor ||
        xwl_gbm_get(xwl_screen)->implicit_sync;
}

Bool
xwl_glamor_supports_syncobjs(struct xwl_screen *xwl_screen)
{
    return xwl_screen->glamor &&
        xwl_gbm_get(xwl_screen)->supports_syncobjs;
}

Bool
xwl_glamor_gbm_set_syncpts(struct xwl_window *xwl_window, PixmapPtr pixmap)
{
#ifdef DRI3
    struct xwl_screen *xwl_screen = xwl_window->xwl_screen;
    struct xwl_pixmap *xwl_pixmap = xwl_pixmap_get(pixmap);
    uint64_t acquire_point;
    uint64_t release_point;
    int fence_fd;

    if (!xwl_screen->glamor)
        return FALSE;

    if (!xwl_pixmap) {
        ErrorF("XWAYLAND: Failed to set synchronization point, no backing xwl_pixmap!\n");
        return FALSE;
    }

    acquire_point = ++xwl_pixmap->timeline_point;
    release_point = ++xwl_pixmap->timeline_point;

    if (!xwl_pixmap->syncobj) {
        struct dri3_syncobj *syncobj = xwl_glamor_dri3_syncobj_create(xwl_screen);
        if (!syncobj)
            goto fail;
        xwl_pixmap->syncobj = syncobj;
    }

    fence_fd = xwl_glamor_get_fence(xwl_screen);
    if (fence_fd >= 0)
        xwl_pixmap->syncobj->import_fence(xwl_pixmap->syncobj, acquire_point, fence_fd);
    else
        goto fail;

    xwl_glamor_dri3_syncobj_passthrough(xwl_window,
                                        xwl_pixmap->syncobj,
                                        xwl_pixmap->syncobj,
                                        acquire_point,
                                        release_point);
    return TRUE;

fail:
    /* can't use explicit sync, we will do a glFinish() before presenting */
    if (xwl_pixmap->syncobj) {
        xwl_pixmap->syncobj->free(xwl_pixmap->syncobj);
        xwl_pixmap->syncobj = NULL;
    }
#endif /* DRI3 */
    return FALSE;
}

void
xwl_glamor_gbm_dispose_syncpts(PixmapPtr pixmap)
{
#ifdef DRI3
    struct xwl_pixmap *xwl_pixmap = xwl_pixmap_get(pixmap);
    struct xwl_screen *xwl_screen = xwl_screen_get(pixmap->drawable.pScreen);

    if (!xwl_screen->glamor || !xwl_pixmap)
        return;

    if (xwl_pixmap->syncobj) {
        xwl_pixmap->syncobj->free(xwl_pixmap->syncobj);
        xwl_pixmap->syncobj = NULL;
    }

    if (xwl_pixmap->efd >= 0) {
        SetNotifyFd(xwl_pixmap->efd, NULL, 0, NULL);
        close(xwl_pixmap->efd);
    }
#endif /* DRI3 */
}

void
xwl_glamor_gbm_wait_syncpts(PixmapPtr pixmap)
{
#ifdef DRI3
    struct xwl_screen *xwl_screen = xwl_screen_get(pixmap->drawable.pScreen);
    struct xwl_pixmap *xwl_pixmap = xwl_pixmap_get(pixmap);

    if (!xwl_screen->glamor || !xwl_pixmap)
        return;

    if (xwl_pixmap->syncobj) {
        int fence_fd = xwl_pixmap->syncobj->export_fence(xwl_pixmap->syncobj,
                                                         xwl_pixmap->timeline_point);

        xwl_glamor_wait_fence(xwl_screen, fence_fd);
    }
#endif /* DRI3 */
}

void
xwl_glamor_gbm_wait_release_fence(struct xwl_window *xwl_window,
                                  PixmapPtr pixmap,
                                  struct xwl_window_buffer *xwl_window_buffer)
{
#ifdef DRI3
    struct xwl_pixmap *xwl_pixmap = xwl_pixmap_get(pixmap);
    struct xwl_screen *xwl_screen = xwl_screen_get(pixmap->drawable.pScreen);

    if (!xwl_screen->glamor || !xwl_pixmap || !xwl_window_buffer)
        return;

    xwl_pixmap->xwl_window_buffer = xwl_window_buffer;
    /* wait until the release fence is available before re-using this buffer */
    xwl_pixmap->efd = eventfd(0, EFD_CLOEXEC);
    SetNotifyFd(xwl_pixmap->efd, xwl_glamor_gbm_release_fence_avail, X_NOTIFY_READ,
                xwl_pixmap);
    xwl_pixmap->syncobj->submitted_eventfd(xwl_pixmap->syncobj,
                                           xwl_pixmap->timeline_point,
                                           xwl_pixmap->efd);
#endif /* DRI3 */
}

static Bool
xwl_glamor_try_to_make_context_current(struct xwl_screen *xwl_screen)
{
    if (xwl_screen->egl_context == EGL_NO_CONTEXT)
        return FALSE;

    return eglMakeCurrent(xwl_screen->egl_display, EGL_NO_SURFACE,
                          EGL_NO_SURFACE, xwl_screen->egl_context);
}

static void
xwl_glamor_maybe_destroy_context(struct xwl_screen *xwl_screen)
{
    if (xwl_screen->egl_context == EGL_NO_CONTEXT)
        return;

   eglMakeCurrent(xwl_screen->egl_display, EGL_NO_SURFACE,
                  EGL_NO_SURFACE, EGL_NO_CONTEXT);
   eglDestroyContext(xwl_screen->egl_display, xwl_screen->egl_context);
   xwl_screen->egl_context = EGL_NO_CONTEXT;
}

static Bool
xwl_glamor_try_big_gl_api(struct xwl_screen *xwl_screen)
{
    static const EGLint config_attribs_core[] = {
        EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR,
        EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR,
        EGL_CONTEXT_MAJOR_VERSION_KHR,
        GLAMOR_GL_CORE_VER_MAJOR,
        EGL_CONTEXT_MINOR_VERSION_KHR,
        GLAMOR_GL_CORE_VER_MINOR,
        EGL_NONE
    };
    int gl_version;

    if (!(xwl_screen->glamor & XWL_GLAMOR_GL))
        return FALSE;

    eglBindAPI(EGL_OPENGL_API);

    xwl_screen->egl_context =
        eglCreateContext(xwl_screen->egl_display, EGL_NO_CONFIG_KHR,
                         EGL_NO_CONTEXT, config_attribs_core);

    if (xwl_screen->egl_context == EGL_NO_CONTEXT)
        xwl_screen->egl_context =
            eglCreateContext(xwl_screen->egl_display, EGL_NO_CONFIG_KHR,
                             EGL_NO_CONTEXT, NULL);

    if (!xwl_glamor_try_to_make_context_current(xwl_screen)) {
        ErrorF("Failed to make EGL context current with GL\n");
        xwl_glamor_maybe_destroy_context(xwl_screen);
        return FALSE;
    }

    /* glamor needs at least GL 2.1, if the GL version is less than 2.1,
     * drop the context we created, it's useless.
     */
    gl_version = epoxy_gl_version();
    if (gl_version < 21) {
        ErrorF("Supported GL version is not sufficient (required 21, found %i)\n",
               gl_version);
        xwl_glamor_maybe_destroy_context(xwl_screen);
        return FALSE;
    }

    return TRUE;
}

static Bool
xwl_glamor_try_gles_api(struct xwl_screen *xwl_screen)
{
    const EGLint gles_attribs[] = {
        EGL_CONTEXT_CLIENT_VERSION,
        2,
        EGL_NONE,
    };

    if (!(xwl_screen->glamor & XWL_GLAMOR_GLES))
        return FALSE;

    eglBindAPI(EGL_OPENGL_ES_API);

    xwl_screen->egl_context = eglCreateContext(xwl_screen->egl_display,
                                               EGL_NO_CONFIG_KHR,
                                               EGL_NO_CONTEXT, gles_attribs);

    if (!xwl_glamor_try_to_make_context_current(xwl_screen)) {
        ErrorF("Failed to make EGL context current with GLES2\n");
        xwl_glamor_maybe_destroy_context(xwl_screen);
        return FALSE;
    }

    return TRUE;
}

static Bool
xwl_glamor_gbm_init_main_dev(struct xwl_screen *xwl_screen)
{
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);
    drmDevice *main_dev;

    while (!xwl_screen->default_feedback.feedback_done) {
        if (wl_display_dispatch(xwl_screen->display) < 0) {
            ErrorF("Failed to dispatch Wayland display\n");
            return FALSE;
        }
    }

    main_dev = xwl_screen->default_feedback.main_dev;
    if (!main_dev) {
        ErrorF("No main linux-dmabuf device advertised by compositor\n");
        return FALSE;
    }

    if (!(main_dev->available_nodes & (1 << DRM_NODE_RENDER))) {
        ErrorF("Main linux-dmabuf device has no render node\n");
        return FALSE;
    }

    xwl_gbm->device_name = strdup(main_dev->nodes[DRM_NODE_RENDER]);
    if (!xwl_gbm->device_name) {
        return FALSE;
    }

    xwl_gbm->drm_fd = open(xwl_gbm->device_name, O_RDWR | O_CLOEXEC);
    if (xwl_gbm->drm_fd < 0) {
        ErrorF("wayland-egl: could not open %s (%s)\n",
               xwl_gbm->device_name, strerror(errno));
        return FALSE;
    }

    if (drmGetDevice2(xwl_gbm->drm_fd, 0, &xwl_gbm->device) != 0) {
        ErrorF("wayland-egl: Could not fetch DRM device %s\n",
               xwl_gbm->device_name);
        return FALSE;
    }

    xwl_gbm->fd_render_node = TRUE;
    return TRUE;
}

Bool
xwl_glamor_gbm_init_egl(struct xwl_screen *xwl_screen)
{
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);
    EGLint major, minor;
    const GLubyte *renderer;
    const char *gbm_backend_name, *egl_vendor;

    if (!xwl_gbm->drm && !xwl_glamor_gbm_init_main_dev(xwl_screen))
        return FALSE;

    if (!xwl_gbm->fd_render_node && !xwl_gbm->drm_authenticated) {
        ErrorF("Failed to get wl_drm, disabling Glamor and DRI3\n");
	return FALSE;
    }

    xwl_gbm->gbm = gbm_create_device(xwl_gbm->drm_fd);
    if (!xwl_gbm->gbm) {
        ErrorF("couldn't create gbm device\n");
        goto error;
    }

    xwl_screen->egl_display = glamor_egl_get_display(EGL_PLATFORM_GBM_MESA,
                                                     xwl_gbm->gbm);
    if (xwl_screen->egl_display == EGL_NO_DISPLAY) {
        ErrorF("glamor_egl_get_display() failed\n");
        goto error;
    }

    if (!eglInitialize(xwl_screen->egl_display, &major, &minor)) {
        ErrorF("eglInitialize() failed\n");
        goto error;
    }

    if (!xwl_glamor_try_big_gl_api(xwl_screen) &&
        !xwl_glamor_try_gles_api(xwl_screen)) {
        ErrorF("Cannot use neither GL nor GLES2\n");
        goto error;
    }

    renderer = glGetString(GL_RENDERER);
    if (!renderer) {
        ErrorF("glGetString() returned NULL, your GL is broken\n");
        goto error;
    }
    if (strstr((const char *)renderer, "softpipe")) {
        ErrorF("Refusing to try glamor on softpipe\n");
        goto error;
    }
    if (!strncmp("llvmpipe", (const char *)renderer, strlen("llvmpipe"))) {
        ErrorF("Refusing to try glamor on llvmpipe\n");
        goto error;
    }

    if (!epoxy_has_gl_extension("GL_OES_EGL_image")) {
        ErrorF("GL_OES_EGL_image not available\n");
        goto error;
    }

    if (epoxy_has_egl_extension(xwl_screen->egl_display,
                                "EXT_image_dma_buf_import") &&
        epoxy_has_egl_extension(xwl_screen->egl_display,
                                "EXT_image_dma_buf_import_modifiers"))
       xwl_gbm->dmabuf_capable = TRUE;

    gbm_backend_name = gbm_device_get_backend_name(xwl_gbm->gbm);
    /* Mesa uses "drm" as backend name, in that case, just do nothing */
    if (gbm_backend_name && strcmp(gbm_backend_name, "drm") != 0)
        xwl_screen->glvnd_vendor = gbm_backend_name;
    xwl_gbm->glamor_gles = !epoxy_is_desktop_gl();

    egl_vendor = eglQueryString(xwl_screen->egl_display, EGL_VENDOR);
    if (!egl_vendor) {
        ErrorF("Could not determine EGL vendor\n");
        goto error;
    }
    /* NVIDIA driver does not support implicit sync */
    xwl_gbm->implicit_sync = !strstr(egl_vendor, "NVIDIA");
#ifdef DRI3
    if (xwl_gbm_supports_syncobjs(xwl_screen) &&
        epoxy_has_egl_extension(xwl_screen->egl_display,
                                "ANDROID_native_fence_sync"))
        xwl_gbm->supports_syncobjs = TRUE;

    if (!xwl_gbm->supports_syncobjs && xwl_screen->explicit_sync) {
        /* explicit sync requires syncobj support */
        wp_linux_drm_syncobj_manager_v1_destroy(xwl_screen->explicit_sync);
        xwl_screen->explicit_sync = NULL;
    }
#endif /* DRI3 */
    return TRUE;
error:
    if (xwl_screen->egl_display != EGL_NO_DISPLAY) {
        xwl_glamor_maybe_destroy_context(xwl_screen);
        eglTerminate(xwl_screen->egl_display);
        xwl_screen->egl_display = EGL_NO_DISPLAY;
    }

    xwl_glamor_gbm_cleanup(xwl_screen);
    return FALSE;
}

Bool
xwl_glamor_gbm_init_screen(struct xwl_screen *xwl_screen)
{
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);
#ifdef DRI3
    if (xwl_gbm->supports_syncobjs) {
        xwl_dri3_info.version = 4;
        xwl_dri3_info.import_syncobj = xwl_dri3_import_syncobj;
    }

    if (!dri3_screen_init(xwl_screen->screen, &xwl_dri3_info)) {
        ErrorF("Failed to initialize dri3\n");
        goto error;
    }
#endif /* DRI3 */
    if (xwl_gbm->fd_render_node)
        goto skip_drm_auth;

    if (!dixRegisterPrivateKey(&xwl_auth_state_private_key, PRIVATE_CLIENT,
                               0)) {
        ErrorF("Failed to register private key\n");
        goto error;
    }

    if (!AddCallback(&ClientStateCallback, xwl_auth_state_client_callback,
                     NULL)) {
        ErrorF("Failed to add client state callback\n");
        goto error;
    }

skip_drm_auth:
    xwl_screen->screen->CreatePixmap = xwl_glamor_gbm_create_pixmap;
    xwl_screen->screen->DestroyPixmap = xwl_glamor_gbm_destroy_pixmap;

    return TRUE;
error:
    xwl_glamor_gbm_cleanup(xwl_screen);
    return FALSE;
}

drmDevice *xwl_gbm_get_main_device(struct xwl_screen *xwl_screen)
{
    struct xwl_gbm_private *xwl_gbm = xwl_gbm_get(xwl_screen);

    return xwl_gbm->device;
}

Bool
xwl_glamor_init_gbm(struct xwl_screen *xwl_screen)
{
    struct xwl_gbm_private *xwl_gbm;

    if (!xwl_glamor_gbm_has_egl_extension())
        return FALSE;

    if (!dixRegisterPrivateKey(&xwl_gbm_private_key, PRIVATE_SCREEN, 0))
        return FALSE;

    xwl_gbm = calloc(1, sizeof(*xwl_gbm));
    if (!xwl_gbm) {
        ErrorF("glamor: Not enough memory to setup GBM, disabling\n");
        return FALSE;
    }

    dixSetPrivate(&xwl_screen->screen->devPrivates, &xwl_gbm_private_key,
                  xwl_gbm);

    return TRUE;
}