Commit f60f8d5d84eeac4e115617070606a83f71abc61d

Manuel Alfayate Corchete 2020-12-18T22:53:51

[Video/KMSDRM]: Add Vulkan support to the KMSDRM backend.

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
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
diff --git a/src/video/kmsdrm/SDL_kmsdrmmouse.c b/src/video/kmsdrm/SDL_kmsdrmmouse.c
index a841d94..5c7b6ec 100644
--- a/src/video/kmsdrm/SDL_kmsdrmmouse.c
+++ b/src/video/kmsdrm/SDL_kmsdrmmouse.c
@@ -19,6 +19,7 @@
      misrepresented as being the original software.
   3. This notice may not be removed or altered from any source distribution.
 */
+
 #include "../../SDL_internal.h"
 
 #if SDL_VIDEO_DRIVER_KMSDRM
@@ -26,6 +27,7 @@
 #include "SDL_kmsdrmvideo.h"
 #include "SDL_kmsdrmmouse.h"
 #include "SDL_kmsdrmdyn.h"
+#include "SDL_assert.h"
 
 #include "../../events/SDL_mouse_c.h"
 #include "../../events/default_cursor.h"
@@ -54,8 +56,10 @@ drm_atomic_movecursor(KMSDRM_CursorData *curdata, uint16_t x, uint16_t y)
     if (!dispdata->atomic_req)
         dispdata->atomic_req = KMSDRM_drmModeAtomicAlloc();
     
-    add_plane_property(dispdata->atomic_req, dispdata->cursor_plane, "CRTC_X", x - curdata->hot_x);
-    add_plane_property(dispdata->atomic_req, dispdata->cursor_plane, "CRTC_Y", y - curdata->hot_y);
+    add_plane_property(dispdata->atomic_req,
+            dispdata->cursor_plane, "CRTC_X", x - curdata->hot_x);
+    add_plane_property(dispdata->atomic_req,
+            dispdata->cursor_plane, "CRTC_Y", y - curdata->hot_y);
 
     return 0;
 }
@@ -66,8 +70,9 @@ drm_atomic_movecursor(KMSDRM_CursorData *curdata, uint16_t x, uint16_t y)
 
 /* Converts a pixel from straight-alpha [AA, RR, GG, BB], which the SDL cursor surface has,
    to premultiplied-alpha [AA. AA*RR, AA*GG, AA*BB].
-   These multiplications have to be done with floats instead of uint32_t's, and the resulting values have 
-   to be converted to be relative to the 0-255 interval, where 255 is 1.00 and anything between 0 and 255 is 0.xx. */
+   These multiplications have to be done with floats instead of uint32_t's,
+   and the resulting values have to be converted to be relative to the 0-255 interval,
+   where 255 is 1.00 and anything between 0 and 255 is 0.xx. */
 void alpha_premultiply_ARGB8888 (uint32_t *pixel) {
 
     uint32_t A, R, G, B;
@@ -93,35 +98,28 @@ KMSDRM_CreateDefaultCursor(void)
     return SDL_CreateCursor(default_cdata, default_cmask, DEFAULT_CWIDTH, DEFAULT_CHEIGHT, DEFAULT_CHOTX, DEFAULT_CHOTY);
 }
 
-/* Create a GBM cursor from a surface, which means creating a hardware cursor.
-   Most programs use software cursors, but protracker-clone for example uses
-   an optional hardware cursor. */
+/* This simply copies over the cursor bitmap from the SDLSurface we receive to the
+   cursor GBM BO. Setting up the cursor plane, creating the cursor FB BO, etc.. is
+   done in KMSDRM_InitMouse(): when we get here. everything must be ready. */
 static SDL_Cursor *
 KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
 {
-    SDL_VideoDevice *dev = SDL_GetVideoDevice();
-    SDL_VideoData *viddata = ((SDL_VideoData *)dev->driverdata);
+    SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
     KMSDRM_CursorData *curdata;
     SDL_Cursor *cursor;
-    uint64_t usable_cursor_w, usable_cursor_h;
+
     uint32_t bo_stride, pixel;
     uint32_t *buffer = NULL;
     size_t bufsize;
     unsigned int i, j;
 
-    /* All code below assumes ARGB8888 format for the cursor surface, like other backends do.
-       Also, the GBM BO pixels have to be alpha-premultiplied, but the SDL surface we receive has
+    /* All code below assumes ARGB8888 format for the cursor surface,
+       like other backends do. Also, the GBM BO pixels have to be
+       alpha-premultiplied, but the SDL surface we receive has
        straight-alpha pixels, so we always have to convert. */ 
     SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
     SDL_assert(surface->pitch == surface->w * 4);
 
-    if (!KMSDRM_gbm_device_is_format_supported(viddata->gbm_dev, GBM_FORMAT_ARGB8888,
-                                               GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE))
-    {
-        SDL_SetError("Unsupported pixel format for cursor");
-        return NULL;
-    }
-
     cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
     if (!cursor) {
         SDL_OutOfMemory();
@@ -134,34 +132,13 @@ KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
         return NULL;
     }
 
-    /* Find out what GBM cursor size is recommended by the driver. */
-    if (KMSDRM_drmGetCap(viddata->drm_fd, DRM_CAP_CURSOR_WIDTH,  &usable_cursor_w) ||
-        KMSDRM_drmGetCap(viddata->drm_fd, DRM_CAP_CURSOR_HEIGHT, &usable_cursor_h))
-    {
-        SDL_SetError("Could not get the recommended GBM cursor size");
-        goto cleanup;
-    }
-
-    if (usable_cursor_w == 0 || usable_cursor_h == 0) {
-        SDL_SetError("Could not get an usable GBM cursor size");
-        goto cleanup;
-    }
-
     /* hox_x and hot_y are the coordinates of the "tip of the cursor" from it's base. */
     curdata->hot_x = hot_x;
     curdata->hot_y = hot_y;
-    curdata->w = usable_cursor_w;
-    curdata->h = usable_cursor_h;
+    curdata->w = dispdata->cursor_w;
+    curdata->h = dispdata->cursor_h;
 
-    curdata->bo = KMSDRM_gbm_bo_create(viddata->gbm_dev, usable_cursor_w, usable_cursor_h,
-                                       GBM_FORMAT_ARGB8888, GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
-
-    if (!curdata->bo) {
-        SDL_SetError("Could not create GBM cursor BO");
-        goto cleanup;
-    }
-
-    bo_stride = KMSDRM_gbm_bo_get_stride(curdata->bo);
+    bo_stride = KMSDRM_gbm_bo_get_stride(dispdata->cursor_bo);
     bufsize = bo_stride * curdata->h;
 
     /* Always use a temp buffer: it serves the purpose of storing the
@@ -197,7 +174,7 @@ KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
         SDL_UnlockSurface(surface);
     }
 
-    if (KMSDRM_gbm_bo_write(curdata->bo, buffer, bufsize)) {
+    if (KMSDRM_gbm_bo_write(dispdata->cursor_bo, buffer, bufsize)) {
         SDL_SetError("Could not write to GBM cursor BO");
         goto cleanup;
     }
@@ -218,11 +195,9 @@ cleanup:
         SDL_free(cursor);
     }
     if (curdata) {
-        if (curdata->bo) {
-            KMSDRM_gbm_bo_destroy(curdata->bo);
-        }
         SDL_free(curdata);
     }
+
     return NULL;
 }
 
@@ -263,7 +238,8 @@ KMSDRM_ShowCursor(SDL_Cursor * cursor)
            and SDL is stored in mouse->cur_cursor. */
         if (mouse->cur_cursor && mouse->cur_cursor->driverdata) {
             if (dispdata && dispdata->cursor_plane) {
-                info.plane = dispdata->cursor_plane; /* The rest of the members are zeroed. */
+                info.plane = dispdata->cursor_plane;
+                /* The rest of the members are zeroed. */
                 drm_atomic_set_plane_props(&info);
                 if (drm_atomic_commit(display->device, SDL_TRUE))
                     return SDL_SetError("Failed atomic commit in KMSDRM_ShowCursor.");
@@ -288,18 +264,14 @@ KMSDRM_ShowCursor(SDL_Cursor * cursor)
     
     curdata = (KMSDRM_CursorData *) cursor->driverdata;
 
-    if (!curdata || !curdata->bo) {
+    if (!curdata || !dispdata->cursor_bo) {
         return SDL_SetError("Cursor not initialized properly.");
     }
 
-    curdata->crtc_id  = dispdata->crtc->crtc->crtc_id;
-    curdata->plane    = dispdata->cursor_plane;
-    curdata->video    = video_device;
-
-    fb = KMSDRM_FBFromBO(curdata->video, curdata->bo);
+    fb = KMSDRM_FBFromBO(video_device, dispdata->cursor_bo);
 
     info.plane = dispdata->cursor_plane;
-    info.crtc_id = curdata->crtc_id;
+    info.crtc_id = dispdata->crtc->crtc->crtc_id;
     info.fb_id = fb->fb_id; 
     info.src_w = curdata->w;
     info.src_h = curdata->h;
@@ -313,39 +285,17 @@ KMSDRM_ShowCursor(SDL_Cursor * cursor)
     if (drm_atomic_commit(display->device, SDL_TRUE)) {
         return SDL_SetError("Failed atomic commit in KMSDRM_ShowCursor.");
     }
+
     return 0;
 }
 
-/* Unset the cursor from the cursor plane, and ONLY WHEN THAT'S DONE,
-   DONE FOR REAL, and not only requested, destroy it by destroying the curso BO.
-   Destroying the cursor BO is an special an delicate situation,
-   because drm_atomic_set_plane_props() returns immediately, and we DON'T 
-   want to get to gbm_bo_destroy() before the prop changes requested
-   in drm_atomic_set_plane_props() have effectively been done. So we
-   issue a BLOCKING atomic_commit here to avoid that situation.
-   REMEMBER you yan issue an atomic_commit whenever you want, and
-   the changes requested until that moment (for any planes, crtcs, etc.)
-   will be done. */
+/* We have destroyed the cursor by now, in KMSDRM_DestroyCursor.
+   This is only for freeing the SDL_cursor.*/
 static void
 KMSDRM_FreeCursor(SDL_Cursor * cursor)
 {
-    KMSDRM_CursorData *curdata = NULL;
-    SDL_VideoDevice *video_device = SDL_GetVideoDevice();
-    KMSDRM_PlaneInfo info = {0};
+    /* Even if the cursor is not ours, free it. */
     if (cursor) {
-        curdata = (KMSDRM_CursorData *) cursor->driverdata;
-        if (video_device && curdata->bo && curdata->plane) {
-            info.plane = curdata->plane; /* The other members are zeroed. */
-            drm_atomic_set_plane_props(&info);
-            /* Wait until the cursor is unset from the cursor plane before destroying it's BO. */
-            if (drm_atomic_commit(video_device, SDL_TRUE)) {
-                SDL_SetError("Failed atomic commit in KMSDRM_FreeCursor.");
-            }
-            KMSDRM_gbm_bo_destroy(curdata->bo);
-            curdata->bo = NULL;
-        }
-
-        /* Even if the cursor is not ours, free it. */
         SDL_free(cursor->driverdata);
         SDL_free(cursor);
     }
@@ -365,6 +315,7 @@ KMSDRM_WarpMouseGlobal(int x, int y)
 {
     KMSDRM_CursorData *curdata;
     SDL_Mouse *mouse = SDL_GetMouse();
+    SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
 
     if (mouse && mouse->cur_cursor && mouse->cur_cursor->driverdata) {
         /* Update internal mouse position. */
@@ -372,7 +323,7 @@ KMSDRM_WarpMouseGlobal(int x, int y)
 
         /* And now update the cursor graphic position on screen. */
         curdata = (KMSDRM_CursorData *) mouse->cur_cursor->driverdata;
-        if (curdata->bo) {
+        if (dispdata->cursor_bo) {
             if (drm_atomic_movecursor(curdata, x, y)) {
                 return SDL_SetError("drm_atomic_movecursor() failed.");
             }
@@ -382,7 +333,8 @@ KMSDRM_WarpMouseGlobal(int x, int y)
     } else {
         return SDL_SetError("No mouse or current cursor.");
     }
-return 0;
+
+    return 0;
 }
 
 void
@@ -392,8 +344,11 @@ KMSDRM_InitMouse(_THIS)
      * but there's no point in doing so as there's no multimice support...yet!
      */
 
+    SDL_VideoDevice *dev = SDL_GetVideoDevice();
+    SDL_VideoData *viddata = ((SDL_VideoData *)dev->driverdata);
     SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
     SDL_Mouse *mouse = SDL_GetMouse();
+    uint64_t usable_cursor_w, usable_cursor_h;
 
     mouse->CreateCursor = KMSDRM_CreateCursor;
     mouse->ShowCursor = KMSDRM_ShowCursor;
@@ -402,20 +357,96 @@ KMSDRM_InitMouse(_THIS)
     mouse->WarpMouse = KMSDRM_WarpMouse;
     mouse->WarpMouseGlobal = KMSDRM_WarpMouseGlobal;
 
-    /* Init cursor plane, if we haven't yet. */
+    /***************************************************************************/
+    /* REMEMBER TO BE SURE OF UNDOING ALL THESE STEPS PROPERLY BEFORE CALLING  */
+    /* gbm_device_destroy, OR YOU WON'T BE ABLE TO CREATE A NEW ONE (ERROR -13 */
+    /* ON gbm_create_device).                                                  */
+    /***************************************************************************/
+
+    /* 1- Init cursor plane, if we haven't yet. */
     if (!dispdata->cursor_plane) {
         setup_plane(_this, &(dispdata->cursor_plane), DRM_PLANE_TYPE_CURSOR);
     }
 
+    /* 2- Create the cursor GBM BO, if we haven't yet. */
+    if (!dispdata->cursor_bo) {
+
+        if (!KMSDRM_gbm_device_is_format_supported(viddata->gbm_dev, GBM_FORMAT_ARGB8888,
+                                               GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE))
+        {
+            SDL_SetError("Unsupported pixel format for cursor");
+            return;
+        }
+
+	if (KMSDRM_drmGetCap(viddata->drm_fd, DRM_CAP_CURSOR_WIDTH,  &usable_cursor_w) ||
+	    KMSDRM_drmGetCap(viddata->drm_fd, DRM_CAP_CURSOR_HEIGHT, &usable_cursor_h))
+	{
+	    SDL_SetError("Could not get the recommended GBM cursor size");
+	    goto cleanup;
+	}
+
+	if (usable_cursor_w == 0 || usable_cursor_h == 0) {
+	    SDL_SetError("Could not get an usable GBM cursor size");
+	    goto cleanup;
+	}
+
+        dispdata->cursor_w = usable_cursor_w;
+        dispdata->cursor_h = usable_cursor_h;
+
+	dispdata->cursor_bo = KMSDRM_gbm_bo_create(viddata->gbm_dev,
+	    usable_cursor_w, usable_cursor_h,
+	    GBM_FORMAT_ARGB8888, GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
+
+	if (!dispdata->cursor_bo) {
+	    SDL_SetError("Could not create GBM cursor BO");
+	    goto cleanup;
+	}
+    }
+
+    /* SDL expects to set the default cursor on screen when we init the mouse. */
     SDL_SetDefaultCursor(KMSDRM_CreateDefaultCursor());
+
+    return;
+
+cleanup:
+    if (dispdata->cursor_bo) {
+	KMSDRM_gbm_bo_destroy(dispdata->cursor_bo);
+	dispdata->cursor_bo = NULL;
+    }
 }
 
 void
-KMSDRM_QuitMouse(_THIS)
+KMSDRM_DeinitMouse(_THIS)
 {
-    /* Free the plane on which the cursor was being shown. */
+    SDL_VideoDevice *video_device = SDL_GetVideoDevice();
     SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
-    free_plane(&dispdata->cursor_plane);
+    KMSDRM_PlaneInfo info = {0};
+ 
+    /*******************************************/
+    /* UNDO WHAT WE DID IN KMSDRM_InitMouse(). */
+    /*******************************************/
+    
+    /* 1- Destroy the curso GBM BO. */
+    if (video_device && dispdata->cursor_bo) {
+	/* Unsethe the cursor BO from the cursor plane.
+	   (The other members of the plane info are zeroed). */
+	info.plane = dispdata->cursor_plane;
+	drm_atomic_set_plane_props(&info);
+	/* Wait until the cursor is unset from the cursor plane
+	   before destroying it's BO. */
+	if (drm_atomic_commit(video_device, SDL_TRUE)) {
+	    SDL_SetError("Failed atomic commit in KMSDRM_DenitMouse.");
+	}
+	/* ..and finally destroy the cursor DRM BO! */
+	KMSDRM_gbm_bo_destroy(dispdata->cursor_bo);
+	dispdata->cursor_bo = NULL;
+    }
+
+    /* 2- Free the cursor plane, on which the cursor was being shown. */
+    if (dispdata->cursor_plane) {
+        free_plane(&dispdata->cursor_plane);
+    }
+
 }
 
 /* This is called when a mouse motion event occurs */
@@ -429,11 +460,13 @@ KMSDRM_MoveCursor(SDL_Cursor * cursor)
        That's why we move the cursor graphic ONLY. */
     if (mouse && mouse->cur_cursor && mouse->cur_cursor->driverdata) {
         curdata = (KMSDRM_CursorData *) mouse->cur_cursor->driverdata;
+
         /* Some programs expect cursor movement even while they don't do SwapWindow() calls,
            and since we ride on the atomic_commit() in SwapWindow() for cursor movement,
-           cursor won't move in these situations. We could do an atomic_commit() for each
-           cursor movement request, but it cripples the movement to 30FPS, so a future solution
-           is needed. SDLPoP "QUIT?" menu is an example of this situation. */
+           cursor won't move in these situations. We could do an atomic_commit() here
+           for each cursor movement request, but it cripples the movement to 30FPS,
+           so a future solution is needed. SDLPoP "QUIT?" menu is an example of this
+           situation. */
 
         if (drm_atomic_movecursor(curdata, mouse->x, mouse->y)) {
             SDL_SetError("drm_atomic_movecursor() failed.");
diff --git a/src/video/kmsdrm/SDL_kmsdrmmouse.h b/src/video/kmsdrm/SDL_kmsdrmmouse.h
index f0a337d..8f63506 100644
--- a/src/video/kmsdrm/SDL_kmsdrmmouse.h
+++ b/src/video/kmsdrm/SDL_kmsdrmmouse.h
@@ -33,19 +33,13 @@
 /* Driverdata with driver-side info about the cursor. */
 typedef struct _KMSDRM_CursorData
 {
-    struct gbm_bo *bo;
-    struct plane *plane;
-    uint32_t       crtc_id;
     uint16_t       hot_x, hot_y;
     uint16_t       w, h;
-    /* The video devide implemented on SDL_kmsdrmvideo.c 
-     * to be used as _THIS pointer in SDL_kmsdrmvideo.c 
-     * functions that need it. */
-    SDL_VideoDevice *video;
+
 } KMSDRM_CursorData;
 
 extern void KMSDRM_InitMouse(_THIS);
-extern void KMSDRM_QuitMouse(_THIS);
+extern void KMSDRM_DeinitMouse(_THIS);
 
 #endif /* SDL_KMSDRM_mouse_h_ */
 
diff --git a/src/video/kmsdrm/SDL_kmsdrmopengles.c b/src/video/kmsdrm/SDL_kmsdrmopengles.c
index a7f9745..c9ec554 100644
--- a/src/video/kmsdrm/SDL_kmsdrmopengles.c
+++ b/src/video/kmsdrm/SDL_kmsdrmopengles.c
@@ -59,11 +59,24 @@ KMSDRM_GLES_DefaultProfileConfig(_THIS, int *mask, int *major, int *minor)
 #endif
 }
 
-
 int
 KMSDRM_GLES_LoadLibrary(_THIS, const char *path) {
+    /* Just pretend you do this here, but don't do it until KMSDRM_CreateWindow(),
+       where we do the same library load we would normally do here.
+       because this gets called by SDL_CreateWindow() before KMSDR_CreateWindow(),
+       so gbm dev isn't yet created when this is called, AND we can't alter the
+       call order in SDL_CreateWindow(). */
+#if 0
     NativeDisplayType display = (NativeDisplayType)((SDL_VideoData *)_this->driverdata)->gbm_dev;
     return SDL_EGL_LoadLibrary(_this, path, display, EGL_PLATFORM_GBM_MESA);
+#endif
+    return 0;
+}
+
+void
+KMSDRM_GLES_UnloadLibrary(_THIS) {
+    /* As with KMSDRM_GLES_LoadLibrary(), we define our own unloading function so
+       we manually unload the library whenever we want. */
 }
 
 SDL_EGL_CreateContext_impl(KMSDRM)
@@ -94,6 +107,7 @@ static EGLSyncKHR create_fence(int fd, _THIS)
         EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fd,
         EGL_NONE,
     };
+
     EGLSyncKHR fence = _this->egl_data->eglCreateSyncKHR
         (_this->egl_data->egl_display, EGL_SYNC_NATIVE_FENCE_ANDROID, attrib_list);
 
@@ -342,16 +356,6 @@ KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window)
 {
     SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata);
 
-     /* Get the EGL context, now that SDL_CreateRenderer() has already been called,
-       and call eglMakeCurrent() on it and the EGL surface. */
-#if SDL_VIDEO_OPENGL_EGL
-    if (windata->egl_context_pending) {
-        EGLContext egl_context = (EGLContext)SDL_GL_GetCurrentContext();
-        SDL_EGL_MakeCurrent(_this, windata->egl_surface, egl_context);
-        windata->egl_context_pending = SDL_FALSE;
-    }
-#endif
-
     if (windata->swap_window == NULL) {
         /* We want the fenced version by default, but it needs extensions. */
         if ( (SDL_GetHintBoolean(SDL_HINT_VIDEO_DOUBLE_BUFFER, SDL_FALSE)) ||
diff --git a/src/video/kmsdrm/SDL_kmsdrmopengles.h b/src/video/kmsdrm/SDL_kmsdrmopengles.h
index 2718f96..f81b0c3 100644
--- a/src/video/kmsdrm/SDL_kmsdrmopengles.h
+++ b/src/video/kmsdrm/SDL_kmsdrmopengles.h
@@ -32,7 +32,6 @@
 /* OpenGLES functions */
 #define KMSDRM_GLES_GetAttribute SDL_EGL_GetAttribute
 #define KMSDRM_GLES_GetProcAddress SDL_EGL_GetProcAddress
-#define KMSDRM_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
 #define KMSDRM_GLES_DeleteContext SDL_EGL_DeleteContext
 #define KMSDRM_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
 
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c
index 5411d2e..19e2496 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.c
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c
@@ -41,6 +41,7 @@
 #include "SDL_kmsdrmopengles.h"
 #include "SDL_kmsdrmmouse.h"
 #include "SDL_kmsdrmdyn.h"
+#include "SDL_kmsdrmvulkan.h"
 #include <sys/stat.h>
 #include <dirent.h>
 #include <errno.h>
@@ -249,19 +250,20 @@ err:
 }
 
 static void
-KMSDRM_DestroyDumbBuffer(_THIS, dumb_buffer *buffer)
+KMSDRM_DestroyDumbBuffer(_THIS, dumb_buffer **buffer)
 {
     SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
 
     struct drm_mode_destroy_dumb destroy = {
-        .handle = buffer->gem_handles[0],
+        .handle = (*buffer)->gem_handles[0],
     };
 
-    KMSDRM_drmModeRmFB(viddata->drm_fd, buffer->fb_id);
+    KMSDRM_drmModeRmFB(viddata->drm_fd, (*buffer)->fb_id);
 
-    munmap(buffer->dumb.mem, buffer->dumb.size);
+    munmap((*buffer)->dumb.mem, (*buffer)->dumb.size);
     KMSDRM_drmIoctl(viddata->drm_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy);
-    free(buffer);
+    free(*buffer);
+    *buffer = NULL;
 }
 
 /* Using the CPU mapping, fill the dumb buffer with black pixels. */
@@ -304,7 +306,7 @@ static dumb_buffer *KMSDRM_CreateBuffer(_THIS)
     return ret;
 
 err:
-    KMSDRM_DestroyDumbBuffer(_this, ret);
+    KMSDRM_DestroyDumbBuffer(_this, &ret);
     return NULL;
 }
 
@@ -389,17 +391,19 @@ void print_plane_info(_THIS, drmModePlanePtr plane)
     drmModeRes *resources;
     uint32_t type = 0;
     SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
-    int i;
 
     drmModeObjectPropertiesPtr props = KMSDRM_drmModeObjectGetProperties(viddata->drm_fd,
         plane->plane_id, DRM_MODE_OBJECT_PLANE);
 
     /* Search the plane props for the plane type. */
-    for (i = 0; i < props->count_props; i++) {
-        drmModePropertyPtr p = KMSDRM_drmModeGetProperty(viddata->drm_fd, props->props[i]);
+    for (int j = 0; j < props->count_props; j++) {
+
+        drmModePropertyPtr p = KMSDRM_drmModeGetProperty(viddata->drm_fd, props->props[j]);
+
         if ((strcmp(p->name, "type") == 0)) {
-            type = props->prop_values[i];
+            type = props->prop_values[j];
         }
+
         KMSDRM_drmModeFreeProperty(p);
     }
 
@@ -428,7 +432,7 @@ void print_plane_info(_THIS, drmModePlanePtr plane)
         return;
 
     printf("--PLANE ID: %d\nPLANE TYPE: %s\nCRTC READING THIS PLANE: %d\nCRTCS SUPPORTED BY THIS PLANE: ",  plane->plane_id, plane_type, plane->crtc_id);
-    for (i = 0; i < resources->count_crtcs; i++) {
+    for (int i = 0; i < resources->count_crtcs; i++) {
         if (plane->possible_crtcs & (1 << i)) {
             uint32_t crtc_id = resources->crtcs[i];
             printf ("%d", crtc_id);
@@ -557,16 +561,19 @@ setup_plane(_THIS, struct plane **plane, uint32_t plane_type)
 {
     uint32_t plane_id;
     SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
+    int ret = 0;
 
     *plane = SDL_calloc(1, sizeof(**plane));
     if (!(*plane)) {
-        return SDL_OutOfMemory();
+        ret = SDL_OutOfMemory();
+        goto cleanup;
     }
 
     /* Get plane ID. */
     plane_id = get_plane_id(_this, plane_type);
 
     if (!plane_id) {
+        ret = SDL_SetError("Invalid Plane ID");
         goto cleanup;
     }
 
@@ -582,7 +589,7 @@ setup_plane(_THIS, struct plane **plane, uint32_t plane_type)
             sizeof(*(*plane)->props_info));
 
         if ( !((*plane)->props_info) ) {
-            SDL_OutOfMemory();
+            ret = SDL_OutOfMemory();
             goto cleanup;
         }
 
@@ -592,12 +599,15 @@ setup_plane(_THIS, struct plane **plane, uint32_t plane_type)
         }
     }   
 
-    return 0;
-
 cleanup:
-    SDL_free(*plane);
-    *plane = NULL;
-    return -1;
+
+    if (ret != 0) {
+        if (*plane) {
+            SDL_free(*plane);
+            *plane = NULL;
+        }
+    }
+    return ret;
 }
 
 /* Free a plane and it's props. */
@@ -673,7 +683,7 @@ int drm_atomic_commit(_THIS, SDL_bool blocking)
     if (ret) {
         SDL_SetError("Atomic commit failed, returned %d.", ret);
         /* Uncomment this for fast-debugging */
-        // printf("ATOMIC COMMIT FAILED: %d.\n", ret);
+        printf("ATOMIC COMMIT FAILED: %s.\n", strerror(errno));
         goto out;
     }
 
@@ -769,8 +779,6 @@ KMSDRM_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hd
     return 0;
 }
 
-
-
 static SDL_VideoDevice *
 KMSDRM_CreateDevice(int devindex)
 {
@@ -846,7 +854,13 @@ KMSDRM_CreateDevice(int devindex)
 #endif
     device->PumpEvents = KMSDRM_PumpEvents;
     device->free = KMSDRM_DeleteDevice;
-
+#if SDL_VIDEO_VULKAN
+    device->Vulkan_LoadLibrary = KMSDRM_Vulkan_LoadLibrary;
+    device->Vulkan_UnloadLibrary = KMSDRM_Vulkan_UnloadLibrary;
+    device->Vulkan_GetInstanceExtensions = KMSDRM_Vulkan_GetInstanceExtensions;
+    device->Vulkan_CreateSurface = KMSDRM_Vulkan_CreateSurface;
+    device->Vulkan_GetDrawableSize = KMSDRM_Vulkan_GetDrawableSize;
+#endif
     return device;
 
 cleanup:
@@ -915,8 +929,8 @@ KMSDRM_FBFromBO(_THIS, struct gbm_bo *bo)
     }
 
     /* Create framebuffer object for the buffer.
-       It's VERY important to note that fb_id is what we ise to set the FB_ID prop of a plane
-       when using the ATOMIC interface, and we get fb_id it here. */
+       It's VERY important to note that fb_id is what we use to set the FB_ID prop
+       of a plane when using the ATOMIC interface, and we get the fb_id here. */
     ret = KMSDRM_drmModeAddFB2(viddata->drm_fd, width, height, format,
             handles, strides, offsets, &fb_info->fb_id, 0);
 
@@ -938,409 +952,774 @@ KMSDRM_FBFromBO(_THIS, struct gbm_bo *bo)
 /* _this is a SDL_VideoDevice *                                              */
 /*****************************************************************************/
 
-/* Destroy the window surfaces and buffers. Have the PRIMARY PLANE
-   disconnected from these buffers before doing so, or have the PRIMARY PLANE
-   reading the original FB where the TTY lives, before doing this, or CRTC will
-   be disconnected by the kernel. */
-void
-KMSDRM_DestroySurfaces(_THIS, SDL_Window *window)
-{
-    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
-    SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
-    KMSDRM_PlaneInfo plane_info = {0};
+/* Deinitializes the dispdata members needed for KMSDRM operation that are
+   inoffeensive for VK compatibility. */
+void KMSDRM_DisplayDataDeinit (_THIS, SDL_DisplayData *dispdata) {
+    /* Free connector */
+    if (dispdata && dispdata->connector) {
+        if (dispdata->connector->connector) {
+            KMSDRM_drmModeFreeConnector(dispdata->connector->connector);
+            dispdata->connector->connector = NULL;
+        }
+        if (dispdata->connector->props_info) {
+            SDL_free(dispdata->connector->props_info); 
+            dispdata->connector->props_info = NULL;
+        }
+        SDL_free(dispdata->connector); 
+        dispdata->connector = NULL;
+    }
 
-#if SDL_VIDEO_OPENGL_EGL
-    EGLContext egl_context;
-#endif
+    /* Free CRTC */
+    if (dispdata && dispdata->crtc) {
+        if (dispdata->crtc->crtc) {
+            KMSDRM_drmModeFreeCrtc(dispdata->crtc->crtc);
+            dispdata->crtc->crtc = NULL;
+        }
+        if (dispdata->crtc->props_info) {
+            SDL_free(dispdata->crtc->props_info); 
+            dispdata->crtc->props_info = NULL;
+        }
+        SDL_free(dispdata->crtc); 
+        dispdata->crtc = NULL;
+    }
+}
 
-    /********************************************************************/
-    /* BLOCK 1: protect the PRIMARY PLANE before destroying the buffers */
-    /* it's using.                                                      */
-    /********************************************************************/
+/* Initializes the dispdata members needed for KMSDRM operation that are
+   inoffeensive for VK compatibility, except we must leave the drm_fd
+   closed when we get to the end of this function.
+   This is to be called early, in VideoInit(), because it gets us
+   the videomode information, which SDL needs immediately after VideoInit(). */
+int KMSDRM_DisplayDataInit (_THIS, SDL_DisplayData *dispdata) {
+    SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
 
-    plane_info.plane = dispdata->display_plane;
-    plane_info.crtc_id = dispdata->crtc->crtc->crtc_id;
-    plane_info.fb_id = dispdata->dumb_buffer->fb_id;
-    plane_info.src_w = dispdata->mode.hdisplay;
-    plane_info.src_h = dispdata->mode.vdisplay;
-    plane_info.crtc_w = dispdata->mode.hdisplay;
-    plane_info.crtc_h = dispdata->mode.vdisplay;
+    drmModeRes *resources = NULL;
+    drmModeEncoder *encoder = NULL;
+    drmModeConnector *connector = NULL;
+    drmModeCrtc *crtc = NULL;
 
-    drm_atomic_set_plane_props(&plane_info);
+    char devname[32];
+    int ret = 0;
+    unsigned i,j;
 
-    /* Issue blocking atomic commit. */    
-    if (drm_atomic_commit(_this, SDL_TRUE)) {
-        SDL_SetError("Failed to issue atomic commit on window destruction.");
+    dispdata->atomic_flags = 0;
+    dispdata->atomic_req = NULL;
+    dispdata->kms_fence = NULL;
+    dispdata->gpu_fence = NULL;
+    dispdata->kms_out_fence_fd = -1;
+    dispdata->dumb_buffer = NULL;
+    dispdata->modeset_pending = SDL_FALSE;
+    dispdata->gbm_init = SDL_FALSE;
+
+    dispdata->display_plane = NULL;
+    dispdata->cursor_plane = NULL;
+
+    dispdata->cursor_bo = NULL;
+
+    /* Open /dev/dri/cardNN */
+    SDL_snprintf(viddata->devpath, sizeof(viddata->devpath), "/dev/dri/card%d", viddata->devindex);
+
+    SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Opening device %s", devname);
+    viddata->drm_fd = open(viddata->devpath, O_RDWR | O_CLOEXEC);
+
+    if (viddata->drm_fd < 0) {
+        ret = SDL_SetError("Could not open %s", devname);
+        goto cleanup;
     }
 
-    /****************************************************************************/
-    /* BLOCK 2: We can finally destroy the window GBM and EGL surfaces, and     */
-    /* GBM buffers now that the buffers are not being used by the PRIMARY PLANE */
-    /* anymore.                                                                 */
-    /****************************************************************************/
+    SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Opened DRM FD (%d)", viddata->drm_fd);
 
-    /* Destroy the GBM surface and buffers. */
-    if (windata->bo) {
-        KMSDRM_gbm_surface_release_buffer(windata->gs, windata->bo);
-        windata->bo = NULL;
+    /* Try ATOMIC compatibility */
+    ret = check_atomic_modesetting(viddata->drm_fd);
+    if (ret) {
+        ret = SDL_SetError("not compatible with atomic modesetting");
+        goto cleanup;
     }
 
-    if (windata->next_bo) {
-        KMSDRM_gbm_surface_release_buffer(windata->gs, windata->next_bo);
-        windata->next_bo = NULL;
+    /********************************************/
+    /* Block for enabling ATOMIC compatibility. */
+    /********************************************/
+
+    /* Set ATOMIC compatibility */
+    /* TODO: We have just tried ATOMIC compatibility, haven't we? */
+    ret = KMSDRM_drmSetClientCap(viddata->drm_fd, DRM_CLIENT_CAP_ATOMIC, 1);
+    if (ret) {
+        ret = SDL_SetError("no atomic modesetting support.");
+        goto cleanup;
     }
 
-    /* Destroy the EGL surface. */
-#if SDL_VIDEO_OPENGL_EGL
-    /***************************************************************************/
-    /* In this eglMakeCurrent() call, we disable the current EGL surface       */
-    /* because we're going to destroy it, but DON'T disable the EGL context,   */
-    /* because it won't be enabled again until the programs ask for a pageflip */
-    /* so we get to SwapWindow().                                              */
-    /* If we disable the context until then and a program tries to retrieve    */
-    /* the context version info before calling for a pageflip, the program     */
-    /* will get wrong info and we will be in trouble.                          */
-    /***************************************************************************/
-    egl_context = (EGLContext)SDL_GL_GetCurrentContext();
-    SDL_EGL_MakeCurrent(_this, EGL_NO_SURFACE, egl_context);
+    /* Set UNIVERSAL PLANES compatibility */
+    ret = KMSDRM_drmSetClientCap(viddata->drm_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
+    if (ret) {
+        ret = SDL_SetError("no universal planes support.");
+        goto cleanup;
+    }
 
-    if (windata->egl_surface != EGL_NO_SURFACE) {
-        SDL_EGL_DestroySurface(_this, windata->egl_surface);
-        windata->egl_surface = EGL_NO_SURFACE;
-    }   
-#endif
+    /*******************************************/
+    /* Block for getting the ATOMIC resources. */
+    /*******************************************/
 
-    if (windata->gs) {
-        KMSDRM_gbm_surface_destroy(windata->gs);
-        windata->gs = NULL;
+    /* Get all of the available connectors / devices / crtcs */
+    resources = KMSDRM_drmModeGetResources(viddata->drm_fd);
+    if (!resources) {
+        ret = SDL_SetError("drmModeGetResources(%d) failed", viddata->drm_fd);
+        goto cleanup;
     }
-}
 
-int
-KMSDRM_CreateSurfaces(_THIS, SDL_Window * window)
-{
-    SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
-    SDL_WindowData *windata = (SDL_WindowData *)window->driverdata;
-    SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
-    uint32_t surface_fmt = GBM_FORMAT_ARGB8888;
-    uint32_t surface_flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING;
-    uint32_t width, height;
+    /* Iterate on the available connectors to find a connected connector. */
+    for (i = 0; i < resources->count_connectors; i++) {
+        drmModeConnector *conn = KMSDRM_drmModeGetConnector(viddata->drm_fd,
+            resources->connectors[i]);
 
-    /* Destroy the surfaces and buffers before creating the new ones. */
-    KMSDRM_DestroySurfaces(_this, window);
+        if (!conn) {
+            continue;
+        }
 
-    if (((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) ||
-       ((window->flags & SDL_WINDOW_FULLSCREEN) == SDL_WINDOW_FULLSCREEN)) {
+        if (conn->connection == DRM_MODE_CONNECTED && conn->count_modes) {
+            SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found connector %d with %d modes.",
+                         conn->connector_id, conn->count_modes);
+            connector = conn;
 
-        width = dispdata->mode.hdisplay;
-        height = dispdata->mode.vdisplay;
-    } else {
-        width = window->w;
-        height = window->h;
+            break;
+        }
+
+        KMSDRM_drmModeFreeConnector(conn);
     }
 
-    if (!KMSDRM_gbm_device_is_format_supported(viddata->gbm_dev, surface_fmt, surface_flags)) {
-        SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "GBM surface format not supported. Trying anyway.");
+    if (!connector) {
+        ret = SDL_SetError("No currently active connector found.");
+        goto cleanup;
     }
 
-    windata->gs = KMSDRM_gbm_surface_create(viddata->gbm_dev, width, height, surface_fmt, surface_flags);
+    /* Try to find the connector's current encoder */
+    for (i = 0; i < resources->count_encoders; i++) {
+        encoder = KMSDRM_drmModeGetEncoder(viddata->drm_fd, resources->encoders[i]);
 
-    if (!windata->gs) {
-        return SDL_SetError("Could not create GBM surface");
-    }
+        if (!encoder) {
+          continue;
+        }
 
-#if SDL_VIDEO_OPENGL_EGL
-    /* We can't get the EGL context yet because SDL_CreateRenderer has not been called,
-       but we need an EGL surface NOW, or GL won't be able to render into any surface
-       and we won't see the first frame. */ 
-    SDL_EGL_SetRequiredVisualId(_this, surface_fmt);
-    windata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType)windata->gs);
+        if (encoder->encoder_id == connector->encoder_id) {
+            SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found encoder %d.", encoder->encoder_id);
+            break;
+        }
 
-    if (windata->egl_surface == EGL_NO_SURFACE) {
-        return SDL_SetError("Could not create EGL window surface");
+        KMSDRM_drmModeFreeEncoder(encoder);
+        encoder = NULL;
     }
 
-    /* Take note that we're still missing the EGL contex,
-       so we can get it in SwapWindow, when SDL_CreateRenderer()
-       has already been called. */
-    windata->egl_context_pending = SDL_TRUE;
-#endif
+    if (!encoder) {
+        /* No encoder was connected, find the first supported one */
+        for (i = 0; i < resources->count_encoders; i++) {
+            encoder = KMSDRM_drmModeGetEncoder(viddata->drm_fd, resources->encoders[i]);
 
-    return 0;
-}
+            if (!encoder) {
+              continue;
+            }
 
-void
-KMSDRM_DestroyWindow(_THIS, SDL_Window *window)
-{
-    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
-    SDL_VideoData *viddata;
-    unsigned int i, j;
+            for (j = 0; j < connector->count_encoders; j++) {
+                if (connector->encoders[j] == encoder->encoder_id) {
+                    break;
+                }
+            }
 
-    if (!windata) {
-        return;
+            if (j != connector->count_encoders) {
+              break;
+            }
+
+            KMSDRM_drmModeFreeEncoder(encoder);
+            encoder = NULL;
+        }
     }
 
-    KMSDRM_DestroySurfaces(_this, window);
+    if (!encoder) {
+        ret = SDL_SetError("No connected encoder found.");
+        goto cleanup;
+    }
 
-    /********************************************/
-    /* Remove from the internal SDL window list */
-    /********************************************/
-    viddata = windata->viddata;
+    SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found encoder %d.", encoder->encoder_id);
 
-    for (i = 0; i < viddata->num_windows; i++) {
-        if (viddata->windows[i] == window) {
-            viddata->num_windows--;
+    /* Try to find a CRTC connected to this encoder */
+    crtc = KMSDRM_drmModeGetCrtc(viddata->drm_fd, encoder->crtc_id);
 
-            for (j = i; j < viddata->num_windows; j++) {
-                viddata->windows[j] = viddata->windows[j + 1];
+    /* If no CRTC was connected to the encoder, find the first CRTC
+       that is supported by the encoder, and use that. */
+    if (!crtc) {
+        for (i = 0; i < resources->count_crtcs; i++) {
+            if (encoder->possible_crtcs & (1 << i)) {
+                encoder->crtc_id = resources->crtcs[i];
+                crtc = KMSDRM_drmModeGetCrtc(viddata->drm_fd, encoder->crtc_id);
+                break;
             }
-
-            break;
         }
     }
 
-    /*********************************************************************/
-    /* Free the window driverdata. Bye bye, surface and buffer pointers! */
-    /*********************************************************************/
-    window->driverdata = NULL;
-    SDL_free(windata);
-}
+    if (!crtc) {
+        ret = SDL_SetError("No CRTC found.");
+        goto cleanup;
+    }
 
-/*****************************************************************************/
-/* Reconfigure the window scaling parameters and re-construct it's surfaces, */
-/* without destroying the window itself.                                     */
-/* To be used by SetWindowSize() and SetWindowFullscreen().                  */
-/*****************************************************************************/
-static int
-KMSDRM_ReconfigureWindow( _THIS, SDL_Window * window) {
-    SDL_WindowData *windata = window->driverdata;
-    SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
-    float ratio;  
+    /* Figure out the default mode to be set. */
+    dispdata->mode = crtc->mode;
 
-    if (((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) ||
-       ((window->flags & SDL_WINDOW_FULLSCREEN) == SDL_WINDOW_FULLSCREEN)) {
+    /* Find the connector's preferred mode, to be used in case the current mode
+       is not valid, or if restoring the current mode fails.
+       We can always count on the preferred mode! */
+    for (i = 0; i < connector->count_modes; i++) {
+	if (connector->modes[i].type & DRM_MODE_TYPE_PREFERRED) {
+	    dispdata->preferred_mode = connector->modes[i];
+	}
+    }
 
-        windata->src_w = dispdata->mode.hdisplay;
-        windata->src_h = dispdata->mode.vdisplay;
-        windata->output_w = dispdata->mode.hdisplay;
-        windata->output_h = dispdata->mode.vdisplay;
-        windata->output_x = 0;
+    /* If the current CRTC's mode isn't valid, select the preferred
+       mode of the connector. */
+    if (crtc->mode_valid == 0) {
+        dispdata->mode = dispdata->preferred_mode;
+    }
+
+    if (dispdata->mode.hdisplay == 0 || dispdata->mode.vdisplay == 0 ) {
+        ret = SDL_SetError("Couldn't get a valid connector videomode.");
+        goto cleanup;
+    }
+
+    /* Get CRTC properties */
+    dispdata->crtc->props = KMSDRM_drmModeObjectGetProperties(viddata->drm_fd,
+        crtc->crtc_id, DRM_MODE_OBJECT_CRTC);	
+
+    dispdata->crtc->props_info = SDL_calloc(dispdata->crtc->props->count_props,
+        sizeof(*dispdata->crtc->props_info));
     
-    } else {
+    if (!dispdata->crtc->props_info) {
+        ret = SDL_OutOfMemory();
+        goto cleanup;
+    }
 
-        /* Normal non-fullscreen windows are scaled using the CRTC,
-           so get output (CRTC) size and position, for AR correction. */
-        ratio = (float)window->w / (float)window->h;
-        windata->src_w = window->w;
-        windata->src_h = window->h;
-        windata->output_w = dispdata->mode.vdisplay * ratio;
-        windata->output_h = dispdata->mode.vdisplay;
-        windata->output_x = (dispdata->mode.hdisplay - windata->output_w) / 2;
+    for (i = 0; i < dispdata->crtc->props->count_props; i++) {
+        dispdata->crtc->props_info[i] = KMSDRM_drmModeGetProperty(viddata->drm_fd,
+        dispdata->crtc->props->props[i]);
+    }
 
+    /* Get connector properties */
+    dispdata->connector->props = KMSDRM_drmModeObjectGetProperties(viddata->drm_fd,
+        connector->connector_id, DRM_MODE_OBJECT_CONNECTOR);	
+
+    dispdata->connector->props_info = SDL_calloc(dispdata->connector->props->count_props,
+        sizeof(*dispdata->connector->props_info));
+
+    if (!dispdata->connector->props_info) {
+        ret = SDL_OutOfMemory();
+        goto cleanup;
     }
 
-    if (KMSDRM_CreateSurfaces(_this, window)) {
-        return -1; 
-    }  
+    for (i = 0; i < dispdata->connector->props->count_props; i++) {
+        dispdata->connector->props_info[i] = KMSDRM_drmModeGetProperty(viddata->drm_fd,
+        dispdata->connector->props->props[i]);
+    }
+    
+    /* Store the connector and crtc for future use. This is all we keep from this function,
+       and these are just structs, inoffensive to VK. */
+    dispdata->connector->connector = connector;
+    dispdata->crtc->crtc = crtc;
+
+    /***********************************/
+    /* Block fpr Vulkan compatibility. */
+    /***********************************/
+
+    /* THIS IS FOR VULKAN! Leave the FD closed, so VK can work. 
+       Will reopen this in CreateWindow, but only if requested a non-VK window. */
+    KMSDRM_drmSetClientCap(viddata->drm_fd, DRM_CLIENT_CAP_ATOMIC, 0);
+    KMSDRM_drmSetClientCap(viddata->drm_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 0);
+    close (viddata->drm_fd);
+    viddata->drm_fd = -1;
 
-    return 0;
+cleanup:
+    // TODO Use it as a list to see if everything we use here is freed.
+    if (encoder)
+        KMSDRM_drmModeFreeEncoder(encoder);
+    if (resources)
+        KMSDRM_drmModeFreeResources(resources);
+    if (ret != 0) {
+        /* Error (complete) cleanup */
+        if (dispdata->connector->connector) {
+            KMSDRM_drmModeFreeConnector(dispdata->connector->connector);
+            dispdata->connector->connector = NULL;
+        }
+        if (dispdata->crtc->crtc) {
+            KMSDRM_drmModeFreeCrtc(dispdata->crtc->crtc);
+            dispdata->crtc->crtc = NULL;
+        }
+        if (viddata->drm_fd >= 0) {
+            close(viddata->drm_fd);
+            viddata->drm_fd = -1;
+        }
+        SDL_free(dispdata);
+    }
+
+    return ret;
 }
 
+/* Init the Vulkan-INCOMPATIBLE stuff:
+   Reopen FD, create gbm dev, create dumb buffer and setup display plane.
+   This is to be called late, in WindowCreate(), and ONLY if this is not
+   a Vulkan window.
+   We are doing this so late to allow Vulkan to work if we build a VK window.
+   These things are incompatible with Vulkan, which accesses the same resources
+   internally so they must be free when trying to build a Vulkan surface.
+*/
 int
-KMSDRM_VideoInit(_THIS)
+KMSDRM_GBMInit (_THIS, SDL_DisplayData *dispdata)
 {
-    unsigned int i, j;
+    SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata;
     int ret = 0;
+
+    /* Reopen the FD! */
+    viddata->drm_fd = open(viddata->devpath, O_RDWR | O_CLOEXEC);
+    KMSDRM_drmSetClientCap(viddata->drm_fd, DRM_CLIENT_CAP_ATOMIC, 1);
+    KMSDRM_drmSetClientCap(viddata->drm_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
+
+    /* Create aux dumb buffer. It's only useful to keep the PRIMARY PLANE occupied
+       when we destroy the GBM surface and it's KMS buffers, so not being able to
+       create it is not fatal. */
+    dispdata->dumb_buffer = KMSDRM_CreateBuffer(_this);
+    if (!dispdata->dumb_buffer) {
+        ret = SDL_SetError("can't create dumb buffer.");
+    } else {
+        /* Fill the dumb buffer with black pixels. */
+        KMSDRM_FillDumbBuffer(dispdata->dumb_buffer);
+    }
+
+    /* Create the GBM device. */
+    viddata->gbm_dev = KMSDRM_gbm_create_device(viddata->drm_fd);
+    if (!viddata->gbm_dev) {
+        ret = SDL_SetError("Couldn't create gbm device.");
+    }
+
+    /* Setup the display plane. ONLY do this after dispdata has the right
+       crtc and connector, because these are used in this function. */
+    ret = setup_plane(_this, &(dispdata->display_plane), DRM_PLANE_TYPE_PRIMARY);
+    if (ret) {
+        ret = SDL_SetError("can't find suitable display plane.");
+    }
+
+    dispdata->gbm_init = SDL_TRUE;
+
+    return ret;
+}
+
+/* Deinit the Vulkan-incompatible KMSDRM stuff. */
+void
+KMSDRM_GBMDeinit (_THIS, SDL_DisplayData *dispdata)
+{
+    KMSDRM_PlaneInfo plane_info = {0};
     SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
-    SDL_DisplayData *dispdata = NULL;
-    drmModeRes *resources = NULL;
-    drmModeEncoder *encoder = NULL;
-    char devname[32];
-    SDL_VideoDisplay display = {0};
+    drmModeModeInfo mode;
 
-    viddata->video_init = SDL_FALSE;
+    uint32_t blob_id;
 
-    dispdata = (SDL_DisplayData *) SDL_calloc(1, sizeof(SDL_DisplayData));
-    if (!dispdata) {
-        return SDL_OutOfMemory();
+    /*****************************************************************/
+    /*                                                               */
+    /* BLOCK to safely destroy the DUMB BUFFER.                      */
+    /*                                                               */
+    /* We come from DestroyWindow(), where both DestroySurfaces()    */
+    /* and this GBMDeinit() are called, one after another.           */
+    /* So the GBM/EGL surfaces and buffers of the windows have       */
+    /* already been destroyed already and, because of that, the      */
+    /* PRIMARY PLANE is using the DUMB BUFFER. BUT the DUMB BUFFER   */
+    /* we use to keep the PRIMARY PLANE occupied when we do          */
+    /* DestroySurfaces calls is going to be destroyed one way or     */
+    /* another when the program quits so, to prevent the the kernel  */
+    /* from disabling the CRTC when it detects the deletion of a     */
+    /* buffer that IS IN USE BY THE PRIMARY PLANE, we do one of      */
+    /* these:                                                        */
+    /*                                                               */
+    /* -In AMDGPU, where manually disabling the CRTC and             */
+    /*  disconnecting the CONNECTOR from the CRTC is an              */
+    /*  unrecoverable situation, we point the PRIMARY PLANE to       */ 
+    /*  the original TTY buffer (not guaranteed to be there for us!) */
+    /*  and then destroy the DUMB BUFFER).                           */
+    /*                                                               */
+    /* -In other drivers, we disconnect the CONNECTOR from the CRTC  */
+    /*  (remember: several connectors can read a CRTC), deactivate   */
+    /*  the CRTC, and set the PRIMARY PLANE props CRTC_ID and FB_ID  */
+    /*  to 0. Then we destroy the DUMB BUFFER.                       */
+    /*  We can leave all like this if we are exiting the program:    */
+    /*  FBCON or whatever will reconfigure things as it needs.       */
+    /*                                                               */
+    /*****************************************************************/
+
+    /* dispdata->crtc->crtc->mode is the original video mode that was
+       configured on the connector when we lauched the program,
+       dispdata->mode is the current video mode, which could be different,
+       and dispdata->preferred_mode is the native display mode. */
+    mode = dispdata->crtc->crtc->mode;
+
+#if AMDGPU_COMPAT
+    /* This won't work if the console wasn't correctly restored when a prevous
+       program exited, because in that case the TTY buffer won't be in
+       crtc->crtc->buffer_id, so the following atomic commit will fail. */
+    plane_info.plane = dispdata->display_plane;
+    plane_info.crtc_id = dispdata->crtc->crtc->crtc_id;
+    plane_info.fb_id = dispdata->crtc->crtc->buffer_id;
+    plane_info.src_w = mode.hdisplay;
+    plane_info.src_h = mode.vdisplay;
+    plane_info.crtc_w = mode.hdisplay;
+    plane_info.crtc_h = mode.vdisplay;
+
+    drm_atomic_set_plane_props(&plane_info);
+#else
+    add_connector_property(dispdata->atomic_req, dispdata->connector , "CRTC_ID", 0);
+    add_crtc_property(dispdata->atomic_req, dispdata->crtc , "ACTIVE", 0);
+
+    /* Since we initialize plane_info to all zeros,
+       ALL PRIMARY PLANE props are set to 0 with this,
+       including FB_ID and CRTC_ID.
+       Not all drivers like FB_ID and CRTC_ID to 0 yet. */
+    plane_info.plane = dispdata->display_plane;
+    drm_atomic_set_plane_props(&plane_info);
+#endif
+
+    /* Set the props that restore the original video mode. */
+    dispdata->atomic_flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
+    add_connector_property(dispdata->atomic_req, dispdata->connector, "CRTC_ID", dispdata->crtc->crtc->crtc_id);
+    KMSDRM_drmModeCreatePropertyBlob(viddata->drm_fd, &mode, sizeof(mode), &blob_id);
+    add_crtc_property(dispdata->atomic_req, dispdata->crtc, "MODE_ID", blob_id);
+    add_crtc_property(dispdata->atomic_req, dispdata->crtc, "ACTIVE", 1);
+
+    /* Issue blocking atomic commit. */    
+    if (drm_atomic_commit(_this, SDL_TRUE)) {
+        SDL_SetError("Failed to issue atomic commit on video quitting.");
     }
 
-    dispdata->display_plane = SDL_calloc(1, sizeof(*dispdata->display_plane));
-    dispdata->crtc = SDL_calloc(1, sizeof(*dispdata->crtc));
-    dispdata->connector = SDL_calloc(1, sizeof(*dispdata->connector));
-    if (!(dispdata->display_plane) || !(dispdata->crtc) || !(dispdata->connector)) {
-        return SDL_OutOfMemory();
+    /* Destroy the DUMB buffer if it exists, now that it's not being
+       used anymore by the PRIMARY PLANE. */
+    if (dispdata->dumb_buffer) {
+        KMSDRM_DestroyDumbBuffer(_this, &dispdata->dumb_buffer);
     }
 
-    dispdata->atomic_flags = 0;
-    dispdata->atomic_req = NULL;
-    dispdata->kms_fence = NULL;
-    dispdata->gpu_fence = NULL;
-    dispdata->kms_out_fence_fd = -1;
-    dispdata->dumb_buffer = NULL;
-    dispdata->modeset_pending = SDL_FALSE;
+    /*************************************************/
+    /* BLOCK to safely destroy the dumb buffer ENDS. */
+    /*************************************************/
+  
+    /* Free display plane */
+    free_plane(&dispdata->display_plane);
 
-    SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "KMSDRM_VideoInit()");
+    /* Free cursor plane (if still not freed) */
+    free_plane(&dispdata->cursor_plane);
 
-    /* Open /dev/dri/cardNN */
-    SDL_snprintf(devname, sizeof(devname), "/dev/dri/card%d", viddata->devindex);
+    /* Destroy GBM device. GBM surface is destroyed by DestroySurfaces(),
+       already called when we get here. */
+    if (viddata->gbm_dev) {
+        KMSDRM_gbm_device_destroy(viddata->gbm_dev);
+        viddata->gbm_dev = NULL;
+    }
 
-    SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Opening device %s", devname);
-    viddata->drm_fd = open(devname, O_RDWR | O_CLOEXEC);
+    /* Finally close DRM FD. May be reopen on next non-vulkan window creation. */
+    if (viddata->drm_fd >= 0) {
+        close(viddata->drm_fd);
+        viddata->drm_fd = -1;
+    }
 
-    if (viddata->drm_fd < 0) {
-        ret = SDL_SetError("Could not open %s", devname);
-        goto cleanup;
+    dispdata->gbm_init = SDL_FALSE;
+}
+
+/* Destroy the window surfaces and buffers. Have the PRIMARY PLANE
+   disconnected from these buffers before doing so, or have the PRIMARY PLANE
+   reading the original FB where the TTY lives, before doing this, or CRTC will
+   be disconnected by the kernel. */
+void
+KMSDRM_DestroySurfaces(_THIS, SDL_Window *window)
+{
+    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
+    SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
+    KMSDRM_PlaneInfo plane_info = {0};
+
+#if SDL_VIDEO_OPENGL_EGL
+    EGLContext egl_context;
+#endif
+
+    /********************************************************************/
+    /* BLOCK 1: protect the PRIMARY PLANE before destroying the buffers */
+    /* it's using, by making it point to the dumb buffer.               */
+    /********************************************************************/
+
+    plane_info.plane = dispdata->display_plane;
+    plane_info.crtc_id = dispdata->crtc->crtc->crtc_id;
+    plane_info.fb_id = dispdata->dumb_buffer->fb_id;
+    plane_info.src_w = dispdata->mode.hdisplay;
+    plane_info.src_h = dispdata->mode.vdisplay;
+    plane_info.crtc_w = dispdata->mode.hdisplay;
+    plane_info.crtc_h = dispdata->mode.vdisplay;
+
+    drm_atomic_set_plane_props(&plane_info);
+
+    /* Issue blocking atomic commit. */    
+    if (drm_atomic_commit(_this, SDL_TRUE)) {
+        SDL_SetError("Failed to issue atomic commit on surfaces destruction.");
     }
 
-    SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Opened DRM FD (%d)", viddata->drm_fd);
+    /****************************************************************************/
+    /* BLOCK 2: We can finally destroy the window GBM and EGL surfaces, and     */
+    /* GBM buffers now that the buffers are not being used by the PRIMARY PLANE */
+    /* anymore.                                                                 */
+    /****************************************************************************/
 
-    /* Try ATOMIC compatibility */
+    /* Destroy the GBM surface and buffers. */
+    if (windata->bo) {
+        KMSDRM_gbm_surface_release_buffer(windata->gs, windata->bo);
+        windata->bo = NULL;
+    }
 
-    ret = check_atomic_modesetting(viddata->drm_fd);
-    if (ret) {
-        goto cleanup;
+    if (windata->next_bo) {
+        KMSDRM_gbm_surface_release_buffer(windata->gs, windata->next_bo);
+        windata->next_bo = NULL;
     }
 
-    ret = KMSDRM_drmSetClientCap(viddata->drm_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
-    if (ret) {
-        ret = SDL_SetError("no universal planes support.");
-        goto cleanup;
+
+    /***************************************************************************/
+    /* Destroy the EGL surface.                                                */
+    /* In this eglMakeCurrent() call, we disable the current EGL surface       */
+    /* because we're going to destroy it, but DON'T disable the EGL context,   */
+    /* because it won't be enabled again until the programs ask for a pageflip */
+    /* so we get to SwapWindow().                                              */
+    /* If we disable the context until then and a program tries to retrieve    */
+    /* the context version info before calling for a pageflip, the program     */
+    /* will get wrong info and we will be in trouble.                          */
+    /***************************************************************************/
+
+#if SDL_VIDEO_OPENGL_EGL
+    egl_context = (EGLContext)SDL_GL_GetCurrentContext();
+    SDL_EGL_MakeCurrent(_this, EGL_NO_SURFACE, egl_context);
+
+    if (windata->egl_surface != EGL_NO_SURFACE) {
+        SDL_EGL_DestroySurface(_this, windata->egl_surface);
+        windata->egl_surface = EGL_NO_SURFACE;
+    }  
+#endif
+
+    if (windata->gs) {
+        KMSDRM_gbm_surface_destroy(windata->gs);
+        windata->gs = NULL;
     }
 
-    /* Create the GBM device */
+}
 
-    viddata->gbm_dev = KMSDRM_gbm_create_device(viddata->drm_fd);
-    if (!viddata->gbm_dev) {
-        ret = SDL_SetError("Couldn't create gbm device.");
-        goto cleanup;
+int
+KMSDRM_CreateSurfaces(_THIS, SDL_Window * window)
+{
+    SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
+    SDL_WindowData *windata = (SDL_WindowData *)window->driverdata;
+    SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
+    uint32_t surface_fmt = GBM_FORMAT_ARGB8888;
+    uint32_t surface_flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING;
+    uint32_t width, height;
+
+    EGLContext egl_context; 
+
+    /* Destroy the surfaces and buffers before creating the new ones. */
+    // TODO REENABLE THIS IF CGENIUS FAILS BECAUSE IT CREATES A NEW WINDOW
+    // W/O DESTRYING THE PREVIOUS ONE.
+    //KMSDRM_DestroySurfaces(_this, window);
+
+    if (((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) ||
+       ((window->flags & SDL_WINDOW_FULLSCREEN) == SDL_WINDOW_FULLSCREEN)) {
+
+        width = dispdata->mode.hdisplay;
+        height = dispdata->mode.vdisplay;
+    } else {
+        width = window->w;
+        height = window->h;
     }
 
-    /* Get all of the available connectors / devices / crtcs */
-    resources = KMSDRM_drmModeGetResources(viddata->drm_fd);
-    if (!resources) {
-        ret = SDL_SetError("drmModeGetResources(%d) failed", viddata->drm_fd);
-        goto cleanup;
+    if (!KMSDRM_gbm_device_is_format_supported(viddata->gbm_dev, surface_fmt, surface_flags)) {
+        SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "GBM surface format not supported. Trying anyway.");
     }
 
-    /* Iterate on the available connectors to find a connected connector. */
-    for (i = 0; i < resources->count_connectors; i++) {
-        drmModeConnector *conn = KMSDRM_drmModeGetConnector(viddata->drm_fd, resources->connectors[i]);
+    windata->gs = KMSDRM_gbm_surface_create(viddata->gbm_dev, width, height, surface_fmt, surface_flags);
 
-        if (!conn) {
-            continue;
-        }
+    if (!windata->gs) {
+        return SDL_SetError("Could not create GBM surface");
+    }
 
-        if (conn->connection == DRM_MODE_CONNECTED && conn->count_modes) {
-            SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found connector %d with %d modes.",
-                         conn->connector_id, conn->count_modes);
-            dispdata->connector->connector = conn;
-            break;
-        }
+#if SDL_VIDEO_OPENGL_EGL //TODO Restore this lo LibraryLoad and Unload calls.
+    /* We can't get the EGL context yet because SDL_CreateRenderer has not been called,
+       but we need an EGL surface NOW, or GL won't be able to render into any surface
+       and we won't see the first frame. */ 
+    SDL_EGL_SetRequiredVisualId(_this, surface_fmt);
+    windata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType)windata->gs);
 
-        KMSDRM_drmModeFreeConnector(conn);
+    if (windata->egl_surface == EGL_NO_SURFACE) {
+        return SDL_SetError("Could not create EGL window surface");
     }
 
-    if (!dispdata->connector->connector) {
-        ret = SDL_SetError("No currently active connector found.");
-        goto cleanup;
+    /* Current context passing to EGL is now done here. If something fails,
+       go back to delayed SDL_EGL_MakeCurrent() call in SwapWindow. */
+    /* TODO Errorcheck on this (may lead to delayed call again...) */
+    egl_context = (EGLContext)SDL_GL_GetCurrentContext();
+    SDL_EGL_MakeCurrent(_this, windata->egl_surface, egl_context);
+
+#endif
+
+    return 0;
+}
+
+void
+KMSDRM_DestroyWindow(_THIS, SDL_Window *window)
+{
+    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
+    SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
+    SDL_VideoData *viddata = windata->viddata;
+    SDL_bool is_vulkan = window->flags & SDL_WINDOW_VULKAN; /* Is this a VK window? */
+    unsigned int i, j;
+
+    if (!windata) {
+        return;
     }
 
-    /* Try to find the connector's current encoder */
-    for (i = 0; i < resources->count_encoders; i++) {
-        encoder = KMSDRM_drmModeGetEncoder(viddata->drm_fd, resources->encoders[i]);
+    if (!is_vulkan) {
 
-        if (!encoder) {
-          continue;
-        }
+        KMSDRM_DestroySurfaces(_this, window);
 
-        if (encoder->encoder_id == dispdata->connector->connector->encoder_id) {
-            SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found encoder %d.", encoder->encoder_id);
-            break;
+        KMSDRM_DeinitMouse(_this);
+ 
+        if (_this->egl_data) {
+	    SDL_EGL_UnloadLibrary(_this);
         }
 
-        KMSDRM_drmModeFreeEncoder(encoder);
-        encoder = NULL;
+        if (dispdata->gbm_init) {
+            KMSDRM_GBMDeinit(_this, dispdata);
+        } 
     }
 
-    if (!encoder) {
-        /* No encoder was connected, find the first supported one */
-        for (i = 0; i < resources->count_encoders; i++) {
-            encoder = KMSDRM_drmModeGetEncoder(viddata->drm_fd, resources->encoders[i]);
+#if AMDGPU_COMPAT
+    /* Since vkDestroySurfaceKHR does not destroy the native surface (only the Vulkan one),
+       runnin Vulkan programs leave the last buffer connected to the primary plane,
+       at least on AMDGPU, and the TTY buffer isn't restored.
+       That's not a big problem, but running a GLES program after that will cause the
+       atomic commit we do for restoring the TTY buffer to fail, because the TTY buffer
+       isn't there when we launch the GLES program.
+       So what we do here is "hack" Vulkan and restore the TTY buffer here, as if
+       we were running a GLES program. We get here after the program's vkDestroySurfaceKHR
+       has been called, which allows us to think as if we were in the beginig
+       of a GLES program.
+       THIS IS DONE IN A SEPARATE BLOCK FOR POSSIBLE EASY FUTURE REMOVAL. DON'T OPTIMIZE. */
+
+    if (is_vulkan) {
+         KMSDRM_GBMInit(_this, dispdata);
+         KMSDRM_GBMDeinit(_this, dispdata);
+    }
+#endif
 
-            if (!encoder) {
-              continue;
-            }
+    /********************************************/
+    /* Remove from the internal SDL window list */
+    /********************************************/
 
-            for (j = 0; j < dispdata->connector->connector->count_encoders; j++) {
-                if (dispdata->connector->connector->encoders[j] == encoder->encoder_id) {
-                    break;
-                }
-            }
+    for (i = 0; i < viddata->num_windows; i++) {
+        if (viddata->windows[i] == window) {
+            viddata->num_windows--;
 
-            if (j != dispdata->connector->connector->count_encoders) {
-              break;
+            for (j = i; j < viddata->num_windows; j++) {
+                viddata->windows[j] = viddata->windows[j + 1];
             }
 
-            KMSDRM_drmModeFreeEncoder(encoder);
-            encoder = NULL;
-        }
+            break;
+        }
+    }
+
+    /*********************************************************************/
+    /* Free the window driverdata. Bye bye, surface and buffer pointers! */
+    /*********************************************************************/
+    window->driverdata = NULL;
+    SDL_free(windata);
+}
+
+/*****************************************************************************/
+/* Reconfigure the window scaling parameters and re-construct it's surfaces, */
+/* without destroying the window itself.                                     */
+/* To be used by SetWindowSize() and SetWindowFullscreen().                  */
+/*****************************************************************************/
+static int
+KMSDRM_ReconfigureWindow( _THIS, SDL_Window * window) {
+    SDL_WindowData *windata = window->driverdata;
+    SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
+    SDL_bool is_vulkan = window->flags & SDL_WINDOW_VULKAN; /* Is this a VK window? */
+    float ratio;  
+
+    if (((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) ||
+       ((window->flags & SDL_WINDOW_FULLSCREEN) == SDL_WINDOW_FULLSCREEN)) {
+
+        windata->src_w = dispdata->mode.hdisplay;
+        windata->src_h = dispdata->mode.vdisplay;
+        windata->output_w = dispdata->mode.hdisplay;
+        windata->output_h = dispdata->mode.vdisplay;
+        windata->output_x = 0;
+    
+    } else {
+
+        /* Normal non-fullscreen windows are scaled using the CRTC,
+           so get output (CRTC) size and position, for AR correction. */
+        ratio = (float)window->w / (float)window->h;
+        windata->src_w = window->w;
+        windata->src_h = window->h;
+        windata->output_w = dispdata->mode.vdisplay * ratio;
+        windata->output_h = dispdata->mode.vdisplay;
+        windata->output_x = (dispdata->mode.hdisplay - windata->output_w) / 2;
+
     }
 
-    if (!encoder) {
-        ret = SDL_SetError("No connected encoder found.");
-        goto cleanup;
+    if (!is_vulkan) {
+        if (KMSDRM_CreateSurfaces(_this, window)) {
+            return -1; 
+        }  
     }
+    return 0;
+}
 
-    SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found encoder %d.", encoder->encoder_id);
+int
+KMSDRM_VideoInit(_THIS)
+{
+    int ret = 0;
 
-    /* Try to find a CRTC connected to this encoder */
-    dispdata->crtc->crtc = KMSDRM_drmModeGetCrtc(viddata->drm_fd, encoder->crtc_id);
+    SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
+    SDL_DisplayData *dispdata = NULL;
+    SDL_VideoDisplay display = {0};
 
-    /* If no CRTC was connected to the encoder, find the first CRTC that is supported by the encoder, and use that. */
-    if (!dispdata->crtc->crtc) {
-        for (i = 0; i < resources->count_crtcs; i++) {
-            if (encoder->possible_crtcs & (1 << i)) {
-                encoder->crtc_id = resources->crtcs[i];
-                dispdata->crtc->crtc = KMSDRM_drmModeGetCrtc(viddata->drm_fd, encoder->crtc_id);
-                break;
-            }
-        }
+    SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "KMSDRM_VideoInit()");
+
+    viddata->video_init = SDL_FALSE;
+
+    dispdata = (SDL_DisplayData *) SDL_calloc(1, sizeof(SDL_DisplayData));
+    if (!dispdata) {
+        return SDL_OutOfMemory();
     }
 
-    if (!dispdata->crtc->crtc) {
-        ret = SDL_SetError("No CRTC found.");
+    /* Alloc memory for these. */
+    dispdata->display_plane = SDL_calloc(1, sizeof(*dispdata->display_plane));
+    dispdata->crtc = SDL_calloc(1, sizeof(*dispdata->crtc));
+    dispdata->connector = SDL_calloc(1, sizeof(*dispdata->connector));
+    if (!(dispdata->display_plane) || !(dispdata->crtc) || !(dispdata->connector)) {
+        ret = SDL_OutOfMemory();
         goto cleanup;
     }
 
-    /* Figure out the default mode to be set. If the current CRTC's mode isn't
-       valid, select the first mode supported by the connector
-
-       FIXME find first mode that specifies DRM_MODE_TYPE_PREFERRED */
-    dispdata->mode = dispdata->crtc->crtc->mode;
+    /* Setup the single display that's available.
+       There's no problem with it being still incomplete. */
+    display.driverdata = dispdata;
 
-    if (dispdata->crtc->crtc->mode_valid == 0) {
-        SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO,
-            "Current mode is invalid, selecting connector's mode #0.");
-        dispdata->mode = dispdata->connector->connector->modes[0];
+    /* Get KMSDRM resources info and store what we need. Getting and storing
+       this info isn't a problem for VK compatibility.
+       For VK-incompatible initializations we have KMSDRM_GBMInit(), which is
+       called on window creation, and only when we know it's not a VK window. */
+    if (KMSDRM_DisplayDataInit(_this, dispdata)) {
+        ret = SDL_SetError("error getting KMS/DRM information");
+        goto cleanup;
     }
 
-    /* Setup the single display that's available */
-
     display.desktop_mode.w = dispdata->mode.hdisplay;
     display.desktop_mode.h = dispdata->mode.vdisplay;
     display.desktop_mode.refresh_rate = dispdata->mode.vrefresh;
-#if 1
     display.desktop_mode.format = SDL_PIXELFORMAT_ARGB8888;
-#else
-    /* FIXME */
-    drmModeFB *fb = drmModeGetFB(viddata->drm_fd, dispdata->crtc->buffer_id);
-    display.desktop_mode.format = drmToSDLPixelFormat(fb->bpp, fb->depth);
-    drmModeFreeFB(fb);
-#endif
-
     display.current_mode = display.desktop_mode;
-    display.driverdata = dispdata;
+
+    /* Add the display only when it's ready, */
     SDL_AddVideoDisplay(&display, SDL_FALSE);
 
     /* Use this if you ever need to see info on all available planes. */
@@ -1348,263 +1727,48 @@ KMSDRM_VideoInit(_THIS)
     get_planes_info(_this);
 #endif
 
-    /* Setup display plane */
-    ret = setup_plane(_this, &(dispdata->display_plane), DRM_PLANE_TYPE_PRIMARY);
-    if (ret) {
-        ret = SDL_SetError("can't find suitable display plane.");
-        goto cleanup;
-    }
-
-    /* Get CRTC properties */
-    dispdata->crtc->props = KMSDRM_drmModeObjectGetProperties(viddata->drm_fd,
-        dispdata->crtc->crtc->crtc_id, DRM_MODE_OBJECT_CRTC);	
-
-    dispdata->crtc->props_info = SDL_calloc(dispdata->crtc->props->count_props,
-        sizeof(*dispdata->crtc->props_info));
-    
-    if (!dispdata->crtc->props_info) {
-        ret = SDL_OutOfMemory();
-        goto cleanup;
-    }
-
-    for (i = 0; i < dispdata->crtc->props->count_props; i++) {
-        dispdata->crtc->props_info[i] = KMSDRM_drmModeGetProperty(viddata->drm_fd,
-        dispdata->crtc->props->props[i]);
-    }
-
-    /* Get connector properties */
-    dispdata->connector->props = KMSDRM_drmModeObjectGetProperties(viddata->drm_fd,
-        dispdata->connector->connector->connector_id, DRM_MODE_OBJECT_CONNECTOR);	
-
-    dispdata->connector->props_info = SDL_calloc(dispdata->connector->props->count_props,
-        sizeof(*dispdata->connector->props_info));
-
-    if (!dispdata->connector->props_info) {
-        ret = SDL_OutOfMemory();
-        goto cleanup;
-    }
-
-    for (i = 0; i < dispdata->connector->props->count_props; i++) {
-        dispdata->connector->props_info[i] = KMSDRM_drmModeGetProperty(viddata->drm_fd,
-        dispdata->connector->props->props[i]);
-    }
-
-    /* Create aux dumb buffer. It's only useful to keep the PRIMARY PLANE occupied
-       when we destroy the GBM surface and it's KMS buffers, so not being able to
-       create it is not fatal. */
-    dispdata->dumb_buffer = KMSDRM_CreateBuffer(_this);
-    if (!dispdata->dumb_buffer) {
-        ret = SDL_SetError("can't create dumb buffer.");
-    } else {
-        /* Fill the dumb buffer with black pixels. */
-        KMSDRM_FillDumbBuffer(dispdata->dumb_buffer);
-    }
-
-    /*********************/
-    /* Atomic block ends */
-    /*********************/
-
 #ifdef SDL_INPUT_LINUXEV
     SDL_EVDEV_Init();
 #endif
 
-    KMSDRM_InitMouse(_this);
-
     viddata->video_init = SDL_TRUE;
 
 cleanup:
 
-    if (encoder)
-        KMSDRM_drmModeFreeEncoder(encoder);
-    if (resources)
-        KMSDRM_drmModeFreeResources(resources);
-    if (ret != 0) {
-        /* Error (complete) cleanup */
-        if (dispdata->connector->connector) {
-            KMSDRM_drmModeFreeConnector(dispdata->connector->connector);
-            dispdata->connector = NULL;
-        }
-        if (dispdata->crtc->crtc) {
-            KMSDRM_drmModeFreeCrtc(dispdata->crtc->crtc);
-            dispdata->crtc = NULL;
-        }
-        if (viddata->gbm_dev) {
-            KMSDRM_gbm_device_destroy(viddata->gbm_dev);
-            viddata->gbm_dev = NULL;
-        }
-        if (viddata->drm_fd >= 0) {
-            close(viddata->drm_fd);
-            viddata->drm_fd = -1;
-        }
-        SDL_free(dispdata);
+    if (ret) {
+	if (dispdata->display_plane) {
+	    SDL_free(dispdata->display_plane);
+	}
+	if (dispdata->crtc) {
+	    SDL_free(dispdata->crtc);
+	}
+	if (dispdata->connector) {
+	    SDL_free(dispdata->connector);
+	}
     }
 
     return ret;
 }
 
-/* The driverdata pointers, like dispdata, viddata, etc...
+/* The driverdata pointers, like dispdata, viddata, windata, etc...
    are freed by SDL internals, so not our job. */
 void
 KMSDRM_VideoQuit(_THIS)
 {
     SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
-    SDL_DisplayData *dispdata;
-
-    KMSDRM_PlaneInfo plane_info = {0};
-    drmModeModeInfo mode;
-
-    uint32_t blob_id;
-
-    /* Video was not initialized properly, hence SDL internals
-       called VideoQuit(). We will segault somewhere if we go further. */
-    if (!viddata->video_init) {
-        return;
-    }
-
-    /* Don't call any SDL_GetDisplay* function until we get sure that
-       VideoInit() succeeded, because the SDL_GetDisplay* call would
-       throw it's own SDL_SetError(), overwritting the error set in
-       the failed VideoInit() call. */
-    dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
-
-    /*****************************************************************/
-    /*                                                               */
-    /* BLOCK to safely destroy the DUMB BUFFER.                      */
-    /*                                                               */
-    /* Since the program should already have called DestroyWindow()  */
-    /* on all the windows by now, there's no need to destroy the     */
-    /* GBM/EGL surfaces and buffers of the windows here: they have   */
-    /* already been destroyed, and the PRIMARY PLANE is using the    */
-    /* DUMB BUFFER. BUT the DUMB BUFFER we use to keep the PRIMARY   */
-    /* PLANE occupied when we do DestroySurfaces calls is going to   */
-    /* be destroyed one way or another when the program quits, so    */
-    /* to avoid the kernel disabling the CRTC when it detects the    */
-    /* deletion of a buffer that IS IN USE BY THE PRIMARY PLANE,     */
-    /* we do one of these:                                           */
-    /*                                                               */
-    /* -In AMDGPU, where manually disabling the CRTC and             */
-    /*  disconnecting the CONNECTOR from the CRTC is an              */
-    /*  unrecoverable situation, so we point the PRIMARY PLANE to    */ 
-    /*  the original TTY buffer (not guaranteed to be there for us!) */
-    /*  and then destroy the DUMB BUFFER).                           */
-    /*                                                               */
-    /* -In other drivers, we disconnect the CONNECTOR from the CRTC  */
-    /*  (remember: several connectors can read a CRTC), deactivate   */
-    /*  the CRTC, and set the PRIMARY PLANE props CRTC_ID and FB_ID  */
-    /*  to 0. Then we destroy the DUMB BUFFER.                       */
-    /*  We can leave all like this if we are exiting the program:    */
-    /*  FBCON or whatever will reconfigure things as it needs.       */
-    /*                                                               */
-    /*****************************************************************/
-
-    mode = dispdata->crtc->crtc->mode;
-
-#if AMDGPU_COMPAT
-    plane_info.plane = dispdata->display_plane;
-    plane_info.crtc_id = dispdata->crtc->crtc->crtc_id;
-    plane_info.fb_id = dispdata->crtc->crtc->buffer_id;
-    plane_info.src_w = mode.hdisplay;
-    plane_info.src_h = mode.vdisplay;
-    plane_info.crtc_w = mode.hdisplay;
-    plane_info.crtc_h = mode.vdisplay;
-
-    drm_atomic_set_plane_props(&plane_info);
-
-#else
-
-    add_connector_property(dispdata->atomic_req, dispdata->connector , "CRTC_ID", 0);
-    add_crtc_property(dispdata->atomic_req, dispdata->crtc , "ACTIVE", 0);
-
-    /* Since we initialize plane_info to all zeros,  ALL PRIMARY PLANE props are set to 0 with this,
-       including FB_ID and CRTC_ID. Not all drivers like FB_ID and CRTC_ID to 0 yet. */
-    plane_info.plane = dispdata->display_plane;
+    SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
 
-    drm_atomic_set_plane_props(&plane_info);
+    KMSDRM_DisplayDataDeinit(_this, dispdata);
 
+#ifdef SDL_INPUT_LINUXEV
+    SDL_EVDEV_Quit();
 #endif
 
-    /* Set props that restore the original video mode. */
-    dispdata->atomic_flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
-    add_connector_property(dispdata->atomic_req, dispdata->connector, "CRTC_ID", dispdata->crtc->crtc->crtc_id);
-    KMSDRM_drmModeCreatePropertyBlob(viddata->drm_fd, &mode, sizeof(mode), &blob_id);
-    add_crtc_property(dispdata->atomic_req, dispdata->crtc, "MODE_ID", blob_id);
-    add_crtc_property(dispdata->atomic_req, dispdata->crtc, "ACTIVE", 1);
-
-    /* Issue blocking atomic commit. */    
-    if (drm_atomic_commit(_this, SDL_TRUE)) {
-        SDL_SetError("Failed to issue atomic commit on video quitting.");
-    }
-
-    /* Destroy the DUMB buffer if it exists, now that it's not being
-       used anymore by the PRIMARY PLANE. */
-    if (dispdata->dumb_buffer) {
-        KMSDRM_DestroyDumbBuffer(_this, dispdata->dumb_buffer);
-    }
-
-    /***************/
-    /* BLOCK ENDS. */
-    /***************/
-
     /* Clear out the window list */
     SDL_free(viddata->windows);
     viddata->windows = NULL;
     viddata->max_windows = 0;
     viddata->num_windows = 0;
-
-#if SDL_VIDEO_OPENGL_EGL
-    if (_this->gl_config.driver_loaded) {
-        SDL_GL_UnloadLibrary();
-    }
-#endif
-
-    /* Free connector */
-    if (dispdata && dispdata->connector) {
-        if (dispdata->connector->connector) {
-            KMSDRM_drmModeFreeConnector(dispdata->connector->connector);
-            dispdata->connector->connector = NULL;
-        }
-        if (dispdata->connector->props_info) {
-            SDL_free(dispdata->connector->props_info); 
-            dispdata->connector->props_info = NULL;
-        }
-        SDL_free(dispdata->connector); 
-        dispdata->connector = NULL;
-    }
-
-    /* Free CRTC */
-    if (dispdata && dispdata->crtc) {
-        if (dispdata->crtc->crtc) {
-            KMSDRM_drmModeFreeCrtc(dispdata->crtc->crtc);
-            dispdata->crtc->crtc = NULL;
-        }
-        if (dispdata->crtc->props_info) {
-            SDL_free(dispdata->crtc->props_info); 
-            dispdata->crtc->props_info = NULL;
-        }
-        SDL_free(dispdata->crtc); 
-        dispdata->crtc = NULL;
-    }
-
-    /* Free display plane */
-    free_plane(&dispdata->display_plane);
-
-    /* Free cursor plane (if still not freed) */
-    free_plane(&dispdata->cursor_plane);
-
-    /* Destroy GBM device. GBM surface is destroyed by DestroySurfaces(),
-       already called by DestroyWindow() when we get here. */
-    if (viddata->gbm_dev) {
-        KMSDRM_gbm_device_destroy(viddata->gbm_dev);
-        viddata->gbm_dev = NULL;
-    }
-    if (viddata->drm_fd >= 0) {
-        close(viddata->drm_fd);
-        SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Closed DRM FD %d", viddata->drm_fd);
-        viddata->drm_fd = -1;
-    }
-#ifdef SDL_INPUT_LINUXEV
-    SDL_EVDEV_Quit();
-#endif
     viddata->video_init = SDL_FALSE;
 }
 
@@ -1690,19 +1854,36 @@ KMSDRM_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
 int
 KMSDRM_CreateWindow(_THIS, SDL_Window * window)
 {
-    SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata;
-    SDL_VideoDisplay *display = NULL;
-    SDL_DisplayData *dispdata = NULL;
     SDL_WindowData *windata = NULL;
+    SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata;
+    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
+    SDL_DisplayData *dispdata = display->driverdata;
+    SDL_bool is_vulkan = window->flags & SDL_WINDOW_VULKAN; /* Is this a VK window? */
+    NativeDisplayType egl_display;
+
     float ratio;
 
-#if SDL_VIDEO_OPENGL_EGL
-    if (!_this->egl_data) {
-        if (SDL_GL_LoadLibrary(NULL) < 0) {
-            goto error;
-        }
+    if ( !(dispdata->gbm_init) && (!is_vulkan)) {
+         /* Reopen FD, create gbm dev, setup display plane, etc,.
+            but only when we come here for the first time,
+            and only if it's not a VK window. */
+         KMSDRM_GBMInit(_this, dispdata);
+
+         /* Manually load the EGL library. KMSDRM_EGL_LoadLibrary() has already
+            been called by SDL_CreateWindow() but we don't do anything there,
+            precisely to be able to load it here.
+            If we let SDL_CreateWindow() load the lib, it will be loaded
+            before we call KMSDRM_GBMInit(), causing GLES programs to fail. */
+         // TODO errorcheck the return value of this.
+         if (!_this->egl_data) {
+             egl_display = (NativeDisplayType)((SDL_VideoData *)_this->driverdata)->gbm_dev;
+             SDL_EGL_LoadLibrary(_this, NULL, egl_display, EGL_PLATFORM_GBM_MESA);
+         }
+
+         /* Can't init mouse sooner because planes are not ready. */
+         // TODO Add a mouse_init bool and use it to avoid double intializations.
+         KMSDRM_InitMouse(_this);
     }
-#endif
 
     /* Allocate window internal data */
     windata = (SDL_WindowData *)SDL_calloc(1, sizeof(SDL_WindowData));
@@ -1711,9 +1892,6 @@ KMSDRM_CreateWindow(_THIS, SDL_Window * window)
         goto error;
     }
 
-    display = SDL_GetDisplayForWindow(window);
-    dispdata = display->driverdata;
-
     if (((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) ||
        ((window->flags & SDL_WINDOW_FULLSCREEN) == SDL_WINDOW_FULLSCREEN))
     {
@@ -1744,8 +1922,10 @@ KMSDRM_CreateWindow(_THIS, SDL_Window * window)
     windata->viddata = viddata;
     window->driverdata = windata;
 
-    if (KMSDRM_CreateSurfaces(_this, window)) {
-        goto error;
+    if (!is_vulkan) {
+        if (KMSDRM_CreateSurfaces(_this, window)) {
+            goto error;
+        }
     }
 
     /* Add window to the internal list of tracked windows. Note, while it may
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.h b/src/video/kmsdrm/SDL_kmsdrmvideo.h
index 6bec4e3..25570b0 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.h
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.h
@@ -85,6 +85,8 @@ typedef struct SDL_VideoData
 {
     int devindex;               /* device index that was passed on creation */
     int drm_fd;                 /* DRM file desc */
+    char devpath[32];           /* DRM dev path. */
+
     struct gbm_device *gbm_dev;
 
     SDL_Window **windows;
@@ -117,6 +119,7 @@ typedef struct connector {
 typedef struct SDL_DisplayData
 {
     drmModeModeInfo mode;
+    drmModeModeInfo preferred_mode;
     uint32_t atomic_flags;
 
     plane *display_plane;
@@ -141,6 +144,14 @@ typedef struct SDL_DisplayData
     dumb_buffer *dumb_buffer;
 
     SDL_bool modeset_pending;
+    SDL_bool gbm_init;
+
+    /* DRM & GBM cursor stuff lives here, not in an SDL_Cursor's driverdata struct,
+       because setting/unsetting up these is done on window creation/destruction,
+       where we may not have an SDL_Cursor at all (so no SDL_Cursor driverdata).
+       There's only one cursor GBM BO because we only support one cursor. */
+    struct gbm_bo *cursor_bo;
+    uint64_t cursor_w, cursor_h;
 
 } SDL_DisplayData;
 
@@ -167,12 +178,9 @@ typedef struct SDL_WindowData
     int32_t output_h;
     int32_t output_x;
 
-    /* This is for deferred eglMakeCurrent() call: we can't call it until
-       the EGL context is available, but we need the EGL surface sooner. */
-    SDL_bool egl_context_pending;
-
     /* This dictates what approach we'll use for SwapBuffers. */
     int (*swap_window)(_THIS, SDL_Window * window);
+
 } SDL_WindowData;
 
 typedef struct SDL_DisplayModeData
diff --git a/src/video/kmsdrm/SDL_kmsdrmvulkan.c b/src/video/kmsdrm/SDL_kmsdrmvulkan.c
new file mode 100644
index 0000000..49d4cf7
--- /dev/null
+++ b/src/video/kmsdrm/SDL_kmsdrmvulkan.c
@@ -0,0 +1,395 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+
+/*
+ * @author Manuel Alfayate Corchere <redwindwanderer@gmail.com>.
+ * Based on Jacob Lifshay's SDL_x11vulkan.c.
+ */
+
+#include "../../SDL_internal.h"
+
+#if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_KMSDRM
+
+#include "SDL_kmsdrmvideo.h"
+#include "SDL_kmsdrmdyn.h"
+#include "SDL_assert.h"
+
+#include "SDL_loadso.h"
+#include "SDL_kmsdrmvulkan.h"
+#include "SDL_syswm.h"
+#include "sys/ioctl.h"
+
+#if defined(__OpenBSD__)
+#define DEFAULT_VULKAN  "libvulkan.so"
+#else
+#define DEFAULT_VULKAN  "libvulkan.so.1"
+#endif
+
+int KMSDRM_Vulkan_LoadLibrary(_THIS, const char *path)
+{
+    VkExtensionProperties *extensions = NULL;
+    Uint32 i, extensionCount = 0;
+    SDL_bool hasSurfaceExtension = SDL_FALSE;
+    SDL_bool hasDisplayExtension = SDL_FALSE;
+    PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
+
+    if(_this->vulkan_config.loader_handle)
+        return SDL_SetError("Vulkan already loaded");
+
+    /* Load the Vulkan library */
+    if(!path)
+        path = SDL_getenv("SDL_VULKAN_LIBRARY");
+    if(!path)
+        path = DEFAULT_VULKAN;
+
+    _this->vulkan_config.loader_handle = SDL_LoadObject(path);
+
+    if(!_this->vulkan_config.loader_handle)
+        return -1;
+
+    SDL_strlcpy(_this->vulkan_config.loader_path, path,
+                SDL_arraysize(_this->vulkan_config.loader_path));
+
+    vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction(
+        _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr");
+
+    if(!vkGetInstanceProcAddr)
+        goto fail;
+
+    _this->vulkan_config.vkGetInstanceProcAddr = (void *)vkGetInstanceProcAddr;
+    _this->vulkan_config.vkEnumerateInstanceExtensionProperties =
+        (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)(
+            VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
+
+    if(!_this->vulkan_config.vkEnumerateInstanceExtensionProperties)
+        goto fail;
+
+    extensions = SDL_Vulkan_CreateInstanceExtensionsList(
+        (PFN_vkEnumerateInstanceExtensionProperties)
+            _this->vulkan_config.vkEnumerateInstanceExtensionProperties,
+        &extensionCount);
+
+    if(!extensions)
+        goto fail;
+
+    for(i = 0; i < extensionCount; i++)
+    {
+        if(SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0)
+            hasSurfaceExtension = SDL_TRUE;
+        else if(SDL_strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, extensions[i].extensionName) == 0)
+            hasDisplayExtension = SDL_TRUE;
+    }
+
+    SDL_free(extensions);
+
+    if(!hasSurfaceExtension)
+    {
+        SDL_SetError("Installed Vulkan doesn't implement the "
+                     VK_KHR_SURFACE_EXTENSION_NAME " extension");
+        goto fail;
+    }
+    else if(!hasDisplayExtension)
+    {
+        SDL_SetError("Installed Vulkan doesn't implement the "
+                     VK_KHR_DISPLAY_EXTENSION_NAME "extension");
+        goto fail;
+    }
+
+    return 0;
+
+fail:
+    SDL_UnloadObject(_this->vulkan_config.loader_handle);
+    _this->vulkan_config.loader_handle = NULL;
+    return -1;
+}
+
+void KMSDRM_Vulkan_UnloadLibrary(_THIS)
+{
+    if(_this->vulkan_config.loader_handle)
+    {
+        SDL_UnloadObject(_this->vulkan_config.loader_handle);
+        _this->vulkan_config.loader_handle = NULL;
+    }
+}
+
+/*********************************************************************/
+/* Here we can put whatever Vulkan extensions we want to be enabled  */
+/* at instance creation, which is done in the programs, not in SDL.  */
+/* So: programs call SDL_Vulkan_GetInstanceExtensions() and here     */
+/* we put the extensions specific to this backend so the programs    */
+/* get a list with the extension we want, so they can include that   */
+/* list in the ppEnabledExtensionNames and EnabledExtensionCount     */
+/* members of the VkInstanceCreateInfo struct passed to              */
+/* vkCreateInstance().                                               */
+/*********************************************************************/
+SDL_bool KMSDRM_Vulkan_GetInstanceExtensions(_THIS,
+                                          SDL_Window *window,
+                                          unsigned *count,
+                                          const char **names)
+{
+    static const char *const extensionsForKMSDRM[] = {
+        VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_DISPLAY_EXTENSION_NAME
+    };
+    if(!_this->vulkan_config.loader_handle)
+    {
+        SDL_SetError("Vulkan is not loaded");
+        return SDL_FALSE;
+    }
+    return SDL_Vulkan_GetInstanceExtensions_Helper(
+            count, names, SDL_arraysize(extensionsForKMSDRM),
+            extensionsForKMSDRM);
+}
+
+void KMSDRM_Vulkan_GetDrawableSize(_THIS, SDL_Window *window, int *w, int *h)
+{
+    if (w) {
+        *w = window->w;
+    }
+
+    if (h) {
+        *h = window->h;
+    }
+}
+
+/***********************************************************************/
+/* First thing to know is that we don't call vkCreateInstance() here.  */
+/* Instead, programs using SDL and Vulkan create their Vulkan instance */
+/* and we get it here, ready to use.                                   */
+/* Extensions specific for this platform are activated in              */
+/* KMSDRM_Vulkan_GetInstanceExtensions(), like we do with              */
+/* VK_KHR_DISPLAY_EXTENSION_NAME, which is what we need for x-less VK. */                
+/***********************************************************************/
+SDL_bool KMSDRM_Vulkan_CreateSurface(_THIS,
+                                  SDL_Window *window,
+                                  VkInstance instance,
+                                  VkSurfaceKHR *surface)
+{
+    VkPhysicalDevice gpu;
+    uint32_t gpu_count;
+    uint32_t display_count;
+    uint32_t mode_count;
+    uint32_t plane_count;
+
+    VkPhysicalDevice *physical_devices = NULL;
+    VkDisplayPropertiesKHR *displays_props = NULL;
+    VkDisplayModePropertiesKHR *modes_props = NULL;
+    VkDisplayPlanePropertiesKHR *planes_props = NULL;
+
+    VkDisplayModeCreateInfoKHR display_mode_create_info;
+    VkDisplaySurfaceCreateInfoKHR display_plane_surface_create_info;
+
+    VkExtent2D image_size;
+    VkDisplayModeKHR display_mode;
+    VkDisplayModePropertiesKHR display_mode_props = {0};
+
+    VkResult result;
+    SDL_bool ret = SDL_FALSE;
+
+    /* Get the function pointers for the functions we will use. */
+    PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr =
+        (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr;
+
+    PFN_vkCreateDisplayPlaneSurfaceKHR vkCreateDisplayPlaneSurfaceKHR =
+        (PFN_vkCreateDisplayPlaneSurfaceKHR)vkGetInstanceProcAddr(
+            instance, "vkCreateDisplayPlaneSurfaceKHR");
+
+    PFN_vkEnumeratePhysicalDevices vkEnumeratePhysicalDevices =
+        (PFN_vkEnumeratePhysicalDevices)vkGetInstanceProcAddr(
+            instance, "vkEnumeratePhysicalDevices");
+
+    PFN_vkGetPhysicalDeviceDisplayPropertiesKHR vkGetPhysicalDeviceDisplayPropertiesKHR =
+        (PFN_vkGetPhysicalDeviceDisplayPropertiesKHR)vkGetInstanceProcAddr(
+            instance, "vkGetPhysicalDeviceDisplayPropertiesKHR");
+
+    PFN_vkGetDisplayModePropertiesKHR vkGetDisplayModePropertiesKHR =
+        (PFN_vkGetDisplayModePropertiesKHR)vkGetInstanceProcAddr(
+            instance, "vkGetDisplayModePropertiesKHR");
+
+    PFN_vkGetPhysicalDeviceDisplayPlanePropertiesKHR vkGetPhysicalDeviceDisplayPlanePropertiesKHR =
+        (PFN_vkGetPhysicalDeviceDisplayPlanePropertiesKHR)vkGetInstanceProcAddr(
+            instance, "vkGetPhysicalDeviceDisplayPlanePropertiesKHR");
+
+    /*PFN_vkGetDisplayPlaneSupportedDisplaysKHR vkGetDisplayPlaneSupportedDisplaysKHR =
+        (PFN_vkGetDisplayPlaneSupportedDisplaysKHR)vkGetInstanceProcAddr(
+            instance, "vkGetDisplayPlaneSupportedDisplaysKHR");
+    
+    PFN_vkGetDisplayPlaneCapabilitiesKHR vkGetDisplayPlaneCapabilitiesKHR =
+        (PFN_vkGetDisplayPlaneCapabilitiesKHR)vkGetInstanceProcAddr(
+            instance, "vkGetDisplayPlaneCapabilitiesKHR");
+    */
+
+    PFN_vkCreateDisplayModeKHR vkCreateDisplayModeKHR =
+        (PFN_vkCreateDisplayModeKHR)vkGetInstanceProcAddr(
+            instance, "vkCreateDisplayModeKHR");
+
+    if(!_this->vulkan_config.loader_handle)
+    {
+        SDL_SetError("Vulkan is not loaded");
+        goto clean;
+    }
+
+    /*************************************/
+    /* Block for vulkan surface creation */
+    /*************************************/
+
+    /****************************************************************/
+    /* If we got vkCreateDisplayPlaneSurfaceKHR() pointer, it means */
+    /* that the VK_KHR_Display extension is active on the instance. */
+    /* That's the central extension we need for x-less VK!          */
+    /****************************************************************/
+    if(!vkCreateDisplayPlaneSurfaceKHR)
+    {
+        SDL_SetError(VK_KHR_DISPLAY_EXTENSION_NAME
+                     " extension is not enabled in the Vulkan instance.");
+        goto clean;
+    }
+
+    /* Get the physical device count. */
+    vkEnumeratePhysicalDevices(instance, &gpu_count, NULL);
+
+    if (gpu_count == 0) {
+        SDL_SetError("Vulkan can't find physical devices (gpus).");
+        goto clean;
+    }
+
+    /* Get the physical devices. */
+    physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
+    vkEnumeratePhysicalDevices(instance, &gpu_count, physical_devices);
+
+    /* For now, just grab the first physical device (gpu = physical_device in vkcube example).*/
+    /* TODO What happens on video systems with multiple display ports like the Rpi4 ? */
+    gpu = physical_devices[0]; 
+
+    /* Get the display count of the phsysical device. */
+    vkGetPhysicalDeviceDisplayPropertiesKHR(gpu, &display_count, NULL);
+    if (display_count == 0) {
+        SDL_SetError("Vulkan can't find any displays.");
+        goto clean;
+    }
+
+    /* Get the props of the displays of the physical device. */
+    displays_props = (VkDisplayPropertiesKHR *) malloc(display_count * sizeof(*displays_props));
+    vkGetPhysicalDeviceDisplayPropertiesKHR(gpu,
+                                           &display_count,
+                                           displays_props);
+
+    /* Get the videomode count for the first display. */
+    /* TODO What happens on video systems with multiple display ports like the Rpi4 ? */
+    vkGetDisplayModePropertiesKHR(gpu,
+                                 displays_props[0].display,
+                                 &mode_count, NULL);
+
+    if (mode_count == 0) {
+        SDL_SetError("Vulkan can't find any video modes for display %i (%s)\n", 0,
+                               displays_props[0].displayName);
+        goto clean;
+    }
+
+    /* Get the props of the videomodes for the first display. */
+    modes_props = (VkDisplayModePropertiesKHR *) malloc(mode_count * sizeof(*modes_props));
+    vkGetDisplayModePropertiesKHR(gpu,
+                                 displays_props[0].display,
+                                 &mode_count, modes_props);
+
+    /* Get the planes count of the physical device. */
+    /* TODO: find out if we need other planes. */
+    vkGetPhysicalDeviceDisplayPlanePropertiesKHR(gpu, &plane_count, NULL);
+    if (plane_count == 0) {
+        SDL_SetError("Vulkan can't find any planes.");
+        goto clean;
+    }
+
+    /* Get the props of the planes for the physical device. */
+    planes_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
+    vkGetPhysicalDeviceDisplayPlanePropertiesKHR(gpu, &plane_count, planes_props);
+
+    /* Get a video mode matching the window size.
+       REMEMBER: We have to get a small enough videomode for the window size,
+       because videomode determines how big the scanout region is and we can't
+       scanout a region bigger than the window (we would be reading past the
+       buffer, and Vulkan would give us a confusing VK_ERROR_SURFACE_LOST_KHR). */
+    for (int i = 0; i < mode_count; i++) {
+        if (modes_props[i].parameters.visibleRegion.width <= window->w &&
+            modes_props[i].parameters.visibleRegion.height <= window->h)
+        {
+            display_mode_props = modes_props[i];
+            break;
+        }
+    } 
+
+    if (display_mode_props.parameters.visibleRegion.width == 0
+        || display_mode_props.parameters.visibleRegion.height == 0)
+    {
+        SDL_SetError("Vulkan can't find a proper display mode for the window size.");
+        goto clean;
+    }
+
+    /* We have the props of the display mode, but we need an actual display mode. */
+    display_mode_create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_MODE_CREATE_INFO_KHR;
+    display_mode_create_info.parameters = display_mode_props.parameters;
+    result = vkCreateDisplayModeKHR(gpu,
+			      displays_props[0].display,
+			      &display_mode_create_info,
+			      NULL, &display_mode);
+    if (result != VK_SUCCESS) {
+        SDL_SetError("Vulkan can't create the display mode.");
+        goto clean;
+    }
+
+    /* Let's finally create the Vulkan surface! */
+
+    image_size.width = window->w;
+    image_size.height = window->h;
+    
+    display_plane_surface_create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
+    display_plane_surface_create_info.displayMode = display_mode;
+    display_plane_surface_create_info.planeIndex = 0;  /* For now, simply use the first plane. */
+    display_plane_surface_create_info.imageExtent = image_size;
+
+    result = vkCreateDisplayPlaneSurfaceKHR(instance,
+                                     &display_plane_surface_create_info,
+                                     NULL,
+                                     surface);
+
+    if(result != VK_SUCCESS)
+    {
+        SDL_SetError("vkCreateKMSDRMSurfaceKHR failed: %s", SDL_Vulkan_GetResultString(result));
+        goto clean;
+    }
+
+    ret = SDL_TRUE;
+
+clean:
+    if (physical_devices)
+        free (physical_devices);
+    if (displays_props)
+        free (displays_props);
+    if (planes_props)
+        free (planes_props);
+    if (modes_props)
+        free (modes_props);
+
+    return ret;
+}
+
+#endif
+
+/* vim: set ts=4 sw=4 expandtab: */
diff --git a/src/video/kmsdrm/SDL_kmsdrmvulkan.h b/src/video/kmsdrm/SDL_kmsdrmvulkan.h
new file mode 100644
index 0000000..2ee04bb
--- /dev/null
+++ b/src/video/kmsdrm/SDL_kmsdrmvulkan.h
@@ -0,0 +1,53 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+
+/*
+ * @author Manuel Alfayate Corchere <redwindwanderer@gmail.com>.
+ * Based on Jacob Lifshay's SDL_x11vulkan.c.
+ */
+
+#include "../../SDL_internal.h"
+
+#ifndef SDL_kmsdrmvulkan_h_
+#define SDL_kmsdrmvulkan_h_
+
+#include "../SDL_vulkan_internal.h"
+#include "../SDL_sysvideo.h"
+
+#if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_KMSDRM
+
+int KMSDRM_Vulkan_LoadLibrary(_THIS, const char *path);
+void KMSDRM_Vulkan_UnloadLibrary(_THIS);
+SDL_bool KMSDRM_Vulkan_GetInstanceExtensions(_THIS,
+                                          SDL_Window *window,
+                                          unsigned *count,
+                                          const char **names);
+void KMSDRM_Vulkan_GetDrawableSize(_THIS, SDL_Window *window, int *w, int *h);
+SDL_bool KMSDRM_Vulkan_CreateSurface(_THIS,
+                                  SDL_Window *window,
+                                  VkInstance instance,
+                                  VkSurfaceKHR *surface);
+
+#endif
+
+#endif /* SDL_kmsdrmvulkan_h_ */
+
+/* vi: set ts=4 sw=4 expandtab: */