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
|
/*
* Mesa 3-D graphics library
*
* Copyright 2009, VMware, Inc.
* All Rights Reserved.
* Copyright (C) 2010 LunarG Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Authors:
* Keith Whitwell <keithw@vmware.com> Jakob Bornecrantz
* <wallbraker@gmail.com> Chia-I Wu <olv@lunarg.com>
*/
#include <xf86drm.h>
#include "GL/mesa_glinterop.h"
#include "util/disk_cache.h"
#include "util/u_memory.h"
#include "util/u_inlines.h"
#include "util/format/u_format.h"
#include "util/u_debug.h"
#include "state_tracker/drm_driver.h"
#include "state_tracker/st_cb_bufferobjects.h"
#include "state_tracker/st_cb_fbo.h"
#include "state_tracker/st_cb_texture.h"
#include "state_tracker/st_texture.h"
#include "state_tracker/st_context.h"
#include "pipe-loader/pipe_loader.h"
#include "main/bufferobj.h"
#include "main/texobj.h"
#include "dri_util.h"
#include "dri_helpers.h"
#include "dri_drawable.h"
#include "dri_query_renderer.h"
#include "drm-uapi/drm_fourcc.h"
struct dri2_buffer
{
__DRIbuffer base;
struct pipe_resource *resource;
};
static inline struct dri2_buffer *
dri2_buffer(__DRIbuffer * driBufferPriv)
{
return (struct dri2_buffer *) driBufferPriv;
}
/**
* DRI2 flush extension.
*/
static void
dri2_flush_drawable(__DRIdrawable *dPriv)
{
dri_flush(dPriv->driContextPriv, dPriv, __DRI2_FLUSH_DRAWABLE, -1);
}
static void
dri2_invalidate_drawable(__DRIdrawable *dPriv)
{
struct dri_drawable *drawable = dri_drawable(dPriv);
dri2InvalidateDrawable(dPriv);
drawable->dPriv->lastStamp = drawable->dPriv->dri2.stamp;
drawable->texture_mask = 0;
p_atomic_inc(&drawable->base.stamp);
}
static const __DRI2flushExtension dri2FlushExtension = {
.base = { __DRI2_FLUSH, 4 },
.flush = dri2_flush_drawable,
.invalidate = dri2_invalidate_drawable,
.flush_with_flags = dri_flush,
};
/**
* Retrieve __DRIbuffer from the DRI loader.
*/
static __DRIbuffer *
dri2_drawable_get_buffers(struct dri_drawable *drawable,
const enum st_attachment_type *atts,
unsigned *count)
{
__DRIdrawable *dri_drawable = drawable->dPriv;
const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
boolean with_format;
__DRIbuffer *buffers;
int num_buffers;
unsigned attachments[10];
unsigned num_attachments, i;
assert(loader);
with_format = dri_with_format(drawable->sPriv);
num_attachments = 0;
/* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
if (!with_format)
attachments[num_attachments++] = __DRI_BUFFER_FRONT_LEFT;
for (i = 0; i < *count; i++) {
enum pipe_format format;
unsigned bind;
int att, depth;
dri_drawable_get_format(drawable, atts[i], &format, &bind);
if (format == PIPE_FORMAT_NONE)
continue;
switch (atts[i]) {
case ST_ATTACHMENT_FRONT_LEFT:
/* already added */
if (!with_format)
continue;
att = __DRI_BUFFER_FRONT_LEFT;
break;
case ST_ATTACHMENT_BACK_LEFT:
att = __DRI_BUFFER_BACK_LEFT;
break;
case ST_ATTACHMENT_FRONT_RIGHT:
att = __DRI_BUFFER_FRONT_RIGHT;
break;
case ST_ATTACHMENT_BACK_RIGHT:
att = __DRI_BUFFER_BACK_RIGHT;
break;
default:
continue;
}
/*
* In this switch statement we must support all formats that
* may occur as the stvis->color_format.
*/
switch(format) {
case PIPE_FORMAT_R16G16B16A16_FLOAT:
depth = 64;
break;
case PIPE_FORMAT_R16G16B16X16_FLOAT:
depth = 48;
break;
case PIPE_FORMAT_B10G10R10A2_UNORM:
case PIPE_FORMAT_R10G10B10A2_UNORM:
case PIPE_FORMAT_BGRA8888_UNORM:
case PIPE_FORMAT_RGBA8888_UNORM:
depth = 32;
break;
case PIPE_FORMAT_R10G10B10X2_UNORM:
case PIPE_FORMAT_B10G10R10X2_UNORM:
depth = 30;
break;
case PIPE_FORMAT_BGRX8888_UNORM:
case PIPE_FORMAT_RGBX8888_UNORM:
depth = 24;
break;
case PIPE_FORMAT_B5G6R5_UNORM:
depth = 16;
break;
default:
depth = util_format_get_blocksizebits(format);
assert(!"Unexpected format in dri2_drawable_get_buffers()");
}
attachments[num_attachments++] = att;
if (with_format) {
attachments[num_attachments++] = depth;
}
}
if (with_format) {
num_attachments /= 2;
buffers = loader->getBuffersWithFormat(dri_drawable,
&dri_drawable->w, &dri_drawable->h,
attachments, num_attachments,
&num_buffers, dri_drawable->loaderPrivate);
}
else {
buffers = loader->getBuffers(dri_drawable,
&dri_drawable->w, &dri_drawable->h,
attachments, num_attachments,
&num_buffers, dri_drawable->loaderPrivate);
}
if (buffers)
*count = num_buffers;
return buffers;
}
static bool
dri_image_drawable_get_buffers(struct dri_drawable *drawable,
struct __DRIimageList *images,
const enum st_attachment_type *statts,
unsigned statts_count)
{
__DRIdrawable *dPriv = drawable->dPriv;
__DRIscreen *sPriv = drawable->sPriv;
unsigned int image_format = __DRI_IMAGE_FORMAT_NONE;
enum pipe_format pf;
uint32_t buffer_mask = 0;
unsigned i, bind;
for (i = 0; i < statts_count; i++) {
dri_drawable_get_format(drawable, statts[i], &pf, &bind);
if (pf == PIPE_FORMAT_NONE)
continue;
switch (statts[i]) {
case ST_ATTACHMENT_FRONT_LEFT:
buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
break;
case ST_ATTACHMENT_BACK_LEFT:
buffer_mask |= __DRI_IMAGE_BUFFER_BACK;
break;
default:
continue;
}
switch (pf) {
case PIPE_FORMAT_R16G16B16A16_FLOAT:
image_format = __DRI_IMAGE_FORMAT_ABGR16161616F;
break;
case PIPE_FORMAT_R16G16B16X16_FLOAT:
image_format = __DRI_IMAGE_FORMAT_XBGR16161616F;
break;
case PIPE_FORMAT_B5G5R5A1_UNORM:
image_format = __DRI_IMAGE_FORMAT_ARGB1555;
break;
case PIPE_FORMAT_B5G6R5_UNORM:
image_format = __DRI_IMAGE_FORMAT_RGB565;
break;
case PIPE_FORMAT_BGRX8888_UNORM:
image_format = __DRI_IMAGE_FORMAT_XRGB8888;
break;
case PIPE_FORMAT_BGRA8888_UNORM:
image_format = __DRI_IMAGE_FORMAT_ARGB8888;
break;
case PIPE_FORMAT_RGBX8888_UNORM:
image_format = __DRI_IMAGE_FORMAT_XBGR8888;
break;
case PIPE_FORMAT_RGBA8888_UNORM:
image_format = __DRI_IMAGE_FORMAT_ABGR8888;
break;
case PIPE_FORMAT_B10G10R10X2_UNORM:
image_format = __DRI_IMAGE_FORMAT_XRGB2101010;
break;
case PIPE_FORMAT_B10G10R10A2_UNORM:
image_format = __DRI_IMAGE_FORMAT_ARGB2101010;
break;
case PIPE_FORMAT_R10G10B10X2_UNORM:
image_format = __DRI_IMAGE_FORMAT_XBGR2101010;
break;
case PIPE_FORMAT_R10G10B10A2_UNORM:
image_format = __DRI_IMAGE_FORMAT_ABGR2101010;
break;
default:
image_format = __DRI_IMAGE_FORMAT_NONE;
break;
}
}
return (*sPriv->image.loader->getBuffers) (dPriv, image_format,
(uint32_t *) &drawable->base.stamp,
dPriv->loaderPrivate, buffer_mask,
images);
}
static __DRIbuffer *
dri2_allocate_buffer(__DRIscreen *sPriv,
unsigned attachment, unsigned format,
int width, int height)
{
struct dri_screen *screen = dri_screen(sPriv);
struct dri2_buffer *buffer;
struct pipe_resource templ;
enum pipe_format pf;
unsigned bind = 0;
struct winsys_handle whandle;
switch (attachment) {
case __DRI_BUFFER_FRONT_LEFT:
case __DRI_BUFFER_FAKE_FRONT_LEFT:
bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
break;
case __DRI_BUFFER_BACK_LEFT:
bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
break;
case __DRI_BUFFER_DEPTH:
case __DRI_BUFFER_DEPTH_STENCIL:
case __DRI_BUFFER_STENCIL:
bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
break;
}
/* because we get the handle and stride */
bind |= PIPE_BIND_SHARED;
switch (format) {
case 64:
pf = PIPE_FORMAT_R16G16B16A16_FLOAT;
break;
case 48:
pf = PIPE_FORMAT_R16G16B16X16_FLOAT;
break;
case 32:
pf = PIPE_FORMAT_BGRA8888_UNORM;
break;
case 30:
pf = PIPE_FORMAT_B10G10R10X2_UNORM;
break;
case 24:
pf = PIPE_FORMAT_BGRX8888_UNORM;
break;
case 16:
pf = PIPE_FORMAT_Z16_UNORM;
break;
default:
return NULL;
}
buffer = CALLOC_STRUCT(dri2_buffer);
if (!buffer)
return NULL;
memset(&templ, 0, sizeof(templ));
templ.bind = bind;
templ.format = pf;
templ.target = PIPE_TEXTURE_2D;
templ.last_level = 0;
templ.width0 = width;
templ.height0 = height;
templ.depth0 = 1;
templ.array_size = 1;
buffer->resource =
screen->base.screen->resource_create(screen->base.screen, &templ);
if (!buffer->resource) {
FREE(buffer);
return NULL;
}
memset(&whandle, 0, sizeof(whandle));
if (screen->can_share_buffer)
whandle.type = WINSYS_HANDLE_TYPE_SHARED;
else
whandle.type = WINSYS_HANDLE_TYPE_KMS;
screen->base.screen->resource_get_handle(screen->base.screen, NULL,
buffer->resource, &whandle,
PIPE_HANDLE_USAGE_EXPLICIT_FLUSH);
buffer->base.attachment = attachment;
buffer->base.name = whandle.handle;
buffer->base.cpp = util_format_get_blocksize(pf);
buffer->base.pitch = whandle.stride;
return &buffer->base;
}
static void
dri2_release_buffer(__DRIscreen *sPriv, __DRIbuffer *bPriv)
{
struct dri2_buffer *buffer = dri2_buffer(bPriv);
pipe_resource_reference(&buffer->resource, NULL);
FREE(buffer);
}
/*
* Backend functions for st_framebuffer interface.
*/
static void
dri2_allocate_textures(struct dri_context *ctx,
struct dri_drawable *drawable,
const enum st_attachment_type *statts,
unsigned statts_count)
{
__DRIscreen *sPriv = drawable->sPriv;
__DRIdrawable *dri_drawable = drawable->dPriv;
struct dri_screen *screen = dri_screen(sPriv);
struct pipe_resource templ;
boolean alloc_depthstencil = FALSE;
unsigned i, j, bind;
const __DRIimageLoaderExtension *image = sPriv->image.loader;
/* Image specific variables */
struct __DRIimageList images;
/* Dri2 specific variables */
__DRIbuffer *buffers = NULL;
struct winsys_handle whandle;
unsigned num_buffers = statts_count;
/* First get the buffers from the loader */
if (image) {
if (!dri_image_drawable_get_buffers(drawable, &images,
statts, statts_count))
return;
}
else {
buffers = dri2_drawable_get_buffers(drawable, statts, &num_buffers);
if (!buffers || (drawable->old_num == num_buffers &&
drawable->old_w == dri_drawable->w &&
drawable->old_h == dri_drawable->h &&
memcmp(drawable->old, buffers,
sizeof(__DRIbuffer) * num_buffers) == 0))
return;
}
/* Second clean useless resources*/
/* See if we need a depth-stencil buffer. */
for (i = 0; i < statts_count; i++) {
if (statts[i] == ST_ATTACHMENT_DEPTH_STENCIL) {
alloc_depthstencil = TRUE;
break;
}
}
/* Delete the resources we won't need. */
for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
/* Don't delete the depth-stencil buffer, we can reuse it. */
if (i == ST_ATTACHMENT_DEPTH_STENCIL && alloc_depthstencil)
continue;
/* Flush the texture before unreferencing, so that other clients can
* see what the driver has rendered.
*/
if (i != ST_ATTACHMENT_DEPTH_STENCIL && drawable->textures[i]) {
struct pipe_context *pipe = ctx->st->pipe;
pipe->flush_resource(pipe, drawable->textures[i]);
}
pipe_resource_reference(&drawable->textures[i], NULL);
}
if (drawable->stvis.samples > 1) {
for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
boolean del = TRUE;
/* Don't delete MSAA resources for the attachments which are enabled,
* we can reuse them. */
for (j = 0; j < statts_count; j++) {
if (i == statts[j]) {
del = FALSE;
break;
}
}
if (del) {
pipe_resource_reference(&drawable->msaa_textures[i], NULL);
}
}
}
/* Third use the buffers retrieved to fill the drawable info */
memset(&templ, 0, sizeof(templ));
templ.target = screen->target;
templ.last_level = 0;
templ.depth0 = 1;
templ.array_size = 1;
if (image) {
if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
struct pipe_resource **buf =
&drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
struct pipe_resource *texture = images.front->texture;
dri_drawable->w = texture->width0;
dri_drawable->h = texture->height0;
pipe_resource_reference(buf, texture);
}
if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
struct pipe_resource **buf =
&drawable->textures[ST_ATTACHMENT_BACK_LEFT];
struct pipe_resource *texture = images.back->texture;
dri_drawable->w = texture->width0;
dri_drawable->h = texture->height0;
pipe_resource_reference(buf, texture);
}
/* Note: if there is both a back and a front buffer,
* then they have the same size.
*/
templ.width0 = dri_drawable->w;
templ.height0 = dri_drawable->h;
}
else {
memset(&whandle, 0, sizeof(whandle));
/* Process DRI-provided buffers and get pipe_resources. */
for (i = 0; i < num_buffers; i++) {
__DRIbuffer *buf = &buffers[i];
enum st_attachment_type statt;
enum pipe_format format;
switch (buf->attachment) {
case __DRI_BUFFER_FRONT_LEFT:
if (!screen->auto_fake_front) {
continue; /* invalid attachment */
}
/* fallthrough */
case __DRI_BUFFER_FAKE_FRONT_LEFT:
statt = ST_ATTACHMENT_FRONT_LEFT;
break;
case __DRI_BUFFER_BACK_LEFT:
statt = ST_ATTACHMENT_BACK_LEFT;
break;
default:
continue; /* invalid attachment */
}
dri_drawable_get_format(drawable, statt, &format, &bind);
if (format == PIPE_FORMAT_NONE)
continue;
/* dri2_drawable_get_buffers has already filled dri_drawable->w
* and dri_drawable->h */
templ.width0 = dri_drawable->w;
templ.height0 = dri_drawable->h;
templ.format = format;
templ.bind = bind;
whandle.handle = buf->name;
whandle.stride = buf->pitch;
whandle.offset = 0;
whandle.format = format;
whandle.modifier = DRM_FORMAT_MOD_INVALID;
if (screen->can_share_buffer)
whandle.type = WINSYS_HANDLE_TYPE_SHARED;
else
whandle.type = WINSYS_HANDLE_TYPE_KMS;
drawable->textures[statt] =
screen->base.screen->resource_from_handle(screen->base.screen,
&templ, &whandle,
PIPE_HANDLE_USAGE_EXPLICIT_FLUSH);
assert(drawable->textures[statt]);
}
}
/* Allocate private MSAA colorbuffers. */
if (drawable->stvis.samples > 1) {
for (i = 0; i < statts_count; i++) {
enum st_attachment_type statt = statts[i];
if (statt == ST_ATTACHMENT_DEPTH_STENCIL)
continue;
if (drawable->textures[statt]) {
templ.format = drawable->textures[statt]->format;
templ.bind = drawable->textures[statt]->bind &
~(PIPE_BIND_SCANOUT | PIPE_BIND_SHARED);
templ.nr_samples = drawable->stvis.samples;
templ.nr_storage_samples = drawable->stvis.samples;
/* Try to reuse the resource.
* (the other resource parameters should be constant)
*/
if (!drawable->msaa_textures[statt] ||
drawable->msaa_textures[statt]->width0 != templ.width0 ||
drawable->msaa_textures[statt]->height0 != templ.height0) {
/* Allocate a new one. */
pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
drawable->msaa_textures[statt] =
screen->base.screen->resource_create(screen->base.screen,
&templ);
assert(drawable->msaa_textures[statt]);
/* If there are any MSAA resources, we should initialize them
* such that they contain the same data as the single-sample
* resources we just got from the X server.
*
* The reason for this is that the state tracker (and
* therefore the app) can access the MSAA resources only.
* The single-sample resources are not exposed
* to the state tracker.
*
*/
dri_pipe_blit(ctx->st->pipe,
drawable->msaa_textures[statt],
drawable->textures[statt]);
}
}
else {
pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
}
}
}
/* Allocate a private depth-stencil buffer. */
if (alloc_depthstencil) {
enum st_attachment_type statt = ST_ATTACHMENT_DEPTH_STENCIL;
struct pipe_resource **zsbuf;
enum pipe_format format;
unsigned bind;
dri_drawable_get_format(drawable, statt, &format, &bind);
if (format) {
templ.format = format;
templ.bind = bind & ~PIPE_BIND_SHARED;
if (drawable->stvis.samples > 1) {
templ.nr_samples = drawable->stvis.samples;
templ.nr_storage_samples = drawable->stvis.samples;
zsbuf = &drawable->msaa_textures[statt];
}
else {
templ.nr_samples = 0;
templ.nr_storage_samples = 0;
zsbuf = &drawable->textures[statt];
}
/* Try to reuse the resource.
* (the other resource parameters should be constant)
*/
if (!*zsbuf ||
(*zsbuf)->width0 != templ.width0 ||
(*zsbuf)->height0 != templ.height0) {
/* Allocate a new one. */
pipe_resource_reference(zsbuf, NULL);
*zsbuf = screen->base.screen->resource_create(screen->base.screen,
&templ);
assert(*zsbuf);
}
}
else {
pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
pipe_resource_reference(&drawable->textures[statt], NULL);
}
}
/* For DRI2, we may get the same buffers again from the server.
* To prevent useless imports of gem names, drawable->old* is used
* to bypass the import if we get the same buffers. This doesn't apply
* to DRI3/Wayland, users of image.loader, since the buffer is managed
* by the client (no import), and the back buffer is going to change
* at every redraw.
*/
if (!image) {
drawable->old_num = num_buffers;
drawable->old_w = dri_drawable->w;
drawable->old_h = dri_drawable->h;
memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * num_buffers);
}
}
static void
dri2_flush_frontbuffer(struct dri_context *ctx,
struct dri_drawable *drawable,
enum st_attachment_type statt)
{
__DRIdrawable *dri_drawable = drawable->dPriv;
const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
struct pipe_context *pipe = ctx->st->pipe;
if (statt != ST_ATTACHMENT_FRONT_LEFT)
return;
if (drawable->stvis.samples > 1) {
/* Resolve the front buffer. */
dri_pipe_blit(ctx->st->pipe,
drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
}
if (drawable->textures[ST_ATTACHMENT_FRONT_LEFT]) {
pipe->flush_resource(pipe, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
}
pipe->flush(pipe, NULL, 0);
if (image) {
image->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
}
else if (loader->flushFrontBuffer) {
loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
}
}
/**
* The struct dri_drawable flush_swapbuffers callback
*/
static void
dri2_flush_swapbuffers(struct dri_context *ctx,
struct dri_drawable *drawable)
{
__DRIdrawable *dri_drawable = drawable->dPriv;
const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
if (image && image->base.version >= 3 && image->flushSwapBuffers) {
image->flushSwapBuffers(dri_drawable, dri_drawable->loaderPrivate);
}
}
static void
dri2_update_tex_buffer(struct dri_drawable *drawable,
struct dri_context *ctx,
struct pipe_resource *res)
{
/* no-op */
}
static __DRIimage *
dri2_create_image_from_winsys(__DRIscreen *_screen,
int width, int height, const struct dri2_format_mapping *map,
int num_handles, struct winsys_handle *whandle,
void *loaderPrivate)
{
struct dri_screen *screen = dri_screen(_screen);
struct pipe_screen *pscreen = screen->base.screen;
__DRIimage *img;
struct pipe_resource templ;
unsigned tex_usage = 0;
int i;
bool use_lowered = false;
if (pscreen->is_format_supported(pscreen, map->pipe_format, screen->target, 0, 0,
PIPE_BIND_RENDER_TARGET))
tex_usage |= PIPE_BIND_RENDER_TARGET;
if (pscreen->is_format_supported(pscreen, map->pipe_format, screen->target, 0, 0,
PIPE_BIND_SAMPLER_VIEW))
tex_usage |= PIPE_BIND_SAMPLER_VIEW;
if (!tex_usage && util_format_is_yuv(map->pipe_format)) {
/* YUV format sampling can be emulated by the Mesa state tracker by
* using multiple samplers of varying formats.
* If no tex_usage is set and we detect a YUV format,
* test for support of all planes' sampler formats and
* add sampler view usage.
*/
use_lowered = true;
for (i = 0; i < map->nplanes; i++) {
if (!pscreen->is_format_supported(pscreen,
dri2_get_pipe_format_for_dri_format(map->planes[i].dri_format),
screen->target, 0, 0,
PIPE_BIND_SAMPLER_VIEW))
break;
}
if (i == map->nplanes)
tex_usage |= PIPE_BIND_SAMPLER_VIEW;
}
if (!tex_usage)
return NULL;
img = CALLOC_STRUCT(__DRIimageRec);
if (!img)
return NULL;
memset(&templ, 0, sizeof(templ));
templ.bind = tex_usage;
templ.target = screen->target;
templ.last_level = 0;
templ.depth0 = 1;
templ.array_size = 1;
for (i = (use_lowered ? map->nplanes : num_handles) - 1; i >= 0; i--) {
struct pipe_resource *tex;
templ.width0 = width >> map->planes[i].width_shift;
templ.height0 = height >> map->planes[i].height_shift;
if (use_lowered)
templ.format = dri2_get_pipe_format_for_dri_format(map->planes[i].dri_format);
else
templ.format = map->pipe_format;
assert(templ.format != PIPE_FORMAT_NONE);
tex = pscreen->resource_from_handle(pscreen,
&templ, &whandle[use_lowered ? map->planes[i].buffer_index : i],
PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
if (!tex) {
pipe_resource_reference(&img->texture, NULL);
FREE(img);
return NULL;
}
tex->next = img->texture;
img->texture = tex;
}
img->level = 0;
img->layer = 0;
img->use = 0;
img->loader_private = loaderPrivate;
return img;
}
static __DRIimage *
dri2_create_image_from_name(__DRIscreen *_screen,
int width, int height, int format,
int name, int pitch, void *loaderPrivate)
{
const struct dri2_format_mapping *map = dri2_get_mapping_by_format(format);
struct winsys_handle whandle;
__DRIimage *img;
if (!map)
return NULL;
memset(&whandle, 0, sizeof(whandle));
whandle.type = WINSYS_HANDLE_TYPE_SHARED;
whandle.handle = name;
whandle.format = map->pipe_format;
whandle.modifier = DRM_FORMAT_MOD_INVALID;
whandle.stride = pitch * util_format_get_blocksize(map->pipe_format);
img = dri2_create_image_from_winsys(_screen, width, height, map,
1, &whandle, loaderPrivate);
if (!img)
return NULL;
img->dri_components = map->dri_components;
img->dri_fourcc = map->dri_fourcc;
img->dri_format = map->dri_format;
return img;
}
static unsigned
dri2_get_modifier_num_planes(uint64_t modifier, int fourcc)
{
const struct dri2_format_mapping *map = dri2_get_mapping_by_fourcc(fourcc);
if (!map)
return 0;
switch (modifier) {
case I915_FORMAT_MOD_Y_TILED_CCS:
return 2;
case DRM_FORMAT_MOD_BROADCOM_UIF:
case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
case DRM_FORMAT_MOD_LINEAR:
/* DRM_FORMAT_MOD_NONE is the same as LINEAR */
case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_EIGHT_GOB:
case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_FOUR_GOB:
case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_ONE_GOB:
case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_SIXTEEN_GOB:
case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_THIRTYTWO_GOB:
case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_TWO_GOB:
case DRM_FORMAT_MOD_QCOM_COMPRESSED:
case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
case DRM_FORMAT_MOD_VIVANTE_TILED:
/* FD_FORMAT_MOD_QCOM_TILED is not in drm_fourcc.h */
case I915_FORMAT_MOD_X_TILED:
case I915_FORMAT_MOD_Y_TILED:
case DRM_FORMAT_MOD_INVALID:
return map->nplanes;
default:
return 0;
}
}
static __DRIimage *
dri2_create_image_from_fd(__DRIscreen *_screen,
int width, int height, int fourcc,
uint64_t modifier, int *fds, int num_fds,
int *strides, int *offsets, unsigned *error,
void *loaderPrivate)
{
struct winsys_handle whandles[3];
const struct dri2_format_mapping *map = dri2_get_mapping_by_fourcc(fourcc);
__DRIimage *img = NULL;
unsigned err = __DRI_IMAGE_ERROR_SUCCESS;
int i, expected_num_fds;
int num_handles = dri2_get_modifier_num_planes(modifier, fourcc);
if (!map || num_handles == 0) {
err = __DRI_IMAGE_ERROR_BAD_MATCH;
goto exit;
}
switch (fourcc) {
case DRM_FORMAT_YUYV:
case DRM_FORMAT_UYVY:
expected_num_fds = 1;
break;
default:
expected_num_fds = num_handles;
break;
}
if (num_fds != expected_num_fds) {
err = __DRI_IMAGE_ERROR_BAD_MATCH;
goto exit;
}
memset(whandles, 0, sizeof(whandles));
for (i = 0; i < num_fds; i++) {
if (fds[i] < 0) {
err = __DRI_IMAGE_ERROR_BAD_ALLOC;
goto exit;
}
whandles[i].type = WINSYS_HANDLE_TYPE_FD;
whandles[i].handle = (unsigned)fds[i];
whandles[i].stride = (unsigned)strides[i];
whandles[i].offset = (unsigned)offsets[i];
whandles[i].format = map->pipe_format;
whandles[i].modifier = modifier;
whandles[i].plane = i;
}
img = dri2_create_image_from_winsys(_screen, width, height, map,
num_fds, whandles, loaderPrivate);
if(img == NULL) {
err = __DRI_IMAGE_ERROR_BAD_ALLOC;
goto exit;
}
img->dri_components = map->dri_components;
img->dri_fourcc = fourcc;
img->dri_format = map->dri_format;
img->imported_dmabuf = TRUE;
exit:
if (error)
*error = err;
return img;
}
static __DRIimage *
dri2_create_image_common(__DRIscreen *_screen,
int width, int height,
int format, unsigned int use,
const uint64_t *modifiers,
const unsigned count,
void *loaderPrivate)
{
const struct dri2_format_mapping *map = dri2_get_mapping_by_format(format);
struct dri_screen *screen = dri_screen(_screen);
__DRIimage *img;
struct pipe_resource templ;
unsigned tex_usage;
if (!map)
return NULL;
tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
if (use & __DRI_IMAGE_USE_SCANOUT)
tex_usage |= PIPE_BIND_SCANOUT;
if (use & __DRI_IMAGE_USE_SHARE)
tex_usage |= PIPE_BIND_SHARED;
if (use & __DRI_IMAGE_USE_LINEAR)
tex_usage |= PIPE_BIND_LINEAR;
if (use & __DRI_IMAGE_USE_CURSOR) {
if (width != 64 || height != 64)
return NULL;
tex_usage |= PIPE_BIND_CURSOR;
}
img = CALLOC_STRUCT(__DRIimageRec);
if (!img)
return NULL;
memset(&templ, 0, sizeof(templ));
templ.bind = tex_usage;
templ.format = map->pipe_format;
templ.target = PIPE_TEXTURE_2D;
templ.last_level = 0;
templ.width0 = width;
templ.height0 = height;
templ.depth0 = 1;
templ.array_size = 1;
if (modifiers)
img->texture =
screen->base.screen
->resource_create_with_modifiers(screen->base.screen,
&templ,
modifiers,
count);
else
img->texture =
screen->base.screen->resource_create(screen->base.screen, &templ);
if (!img->texture) {
FREE(img);
return NULL;
}
img->level = 0;
img->layer = 0;
img->dri_format = format;
img->dri_fourcc = map->dri_fourcc;
img->dri_components = 0;
img->use = use;
img->loader_private = loaderPrivate;
return img;
}
static __DRIimage *
dri2_create_image(__DRIscreen *_screen,
int width, int height, int format,
unsigned int use, void *loaderPrivate)
{
return dri2_create_image_common(_screen, width, height, format, use,
NULL /* modifiers */, 0 /* count */,
loaderPrivate);
}
static __DRIimage *
dri2_create_image_with_modifiers(__DRIscreen *dri_screen,
int width, int height, int format,
const uint64_t *modifiers,
const unsigned count,
void *loaderPrivate)
{
return dri2_create_image_common(dri_screen, width, height, format,
__DRI_IMAGE_USE_SHARE, modifiers, count,
loaderPrivate);
}
static bool
dri2_query_image_common(__DRIimage *image, int attrib, int *value)
{
switch (attrib) {
case __DRI_IMAGE_ATTRIB_FORMAT:
*value = image->dri_format;
return true;
case __DRI_IMAGE_ATTRIB_WIDTH:
*value = image->texture->width0;
return true;
case __DRI_IMAGE_ATTRIB_HEIGHT:
*value = image->texture->height0;
return true;
case __DRI_IMAGE_ATTRIB_COMPONENTS:
if (image->dri_components == 0)
return false;
*value = image->dri_components;
return true;
case __DRI_IMAGE_ATTRIB_FOURCC:
if (image->dri_fourcc) {
*value = image->dri_fourcc;
} else {
const struct dri2_format_mapping *map;
map = dri2_get_mapping_by_format(image->dri_format);
if (!map)
return false;
*value = map->dri_fourcc;
}
return true;
default:
return false;
}
}
static bool
dri2_query_image_by_resource_handle(__DRIimage *image, int attrib, int *value)
{
struct pipe_screen *pscreen = image->texture->screen;
struct winsys_handle whandle;
unsigned usage;
memset(&whandle, 0, sizeof(whandle));
whandle.plane = image->plane;
switch (attrib) {
case __DRI_IMAGE_ATTRIB_STRIDE:
case __DRI_IMAGE_ATTRIB_OFFSET:
case __DRI_IMAGE_ATTRIB_HANDLE:
whandle.type = WINSYS_HANDLE_TYPE_KMS;
break;
case __DRI_IMAGE_ATTRIB_NAME:
whandle.type = WINSYS_HANDLE_TYPE_SHARED;
break;
case __DRI_IMAGE_ATTRIB_FD:
whandle.type = WINSYS_HANDLE_TYPE_FD;
break;
case __DRI_IMAGE_ATTRIB_NUM_PLANES:
*value = 1;
return true;
case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
whandle.type = WINSYS_HANDLE_TYPE_KMS;
whandle.modifier = DRM_FORMAT_MOD_INVALID;
break;
default:
return false;
}
usage = PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
if (image->use & __DRI_IMAGE_USE_BACKBUFFER)
usage |= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
if (!pscreen->resource_get_handle(pscreen, NULL, image->texture,
&whandle, usage))
return false;
switch (attrib) {
case __DRI_IMAGE_ATTRIB_STRIDE:
*value = whandle.stride;
return true;
case __DRI_IMAGE_ATTRIB_OFFSET:
*value = whandle.offset;
return true;
case __DRI_IMAGE_ATTRIB_HANDLE:
case __DRI_IMAGE_ATTRIB_NAME:
case __DRI_IMAGE_ATTRIB_FD:
*value = whandle.handle;
return true;
case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
if (whandle.modifier == DRM_FORMAT_MOD_INVALID)
return false;
*value = (whandle.modifier >> 32) & 0xffffffff;
return true;
case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
if (whandle.modifier == DRM_FORMAT_MOD_INVALID)
return false;
*value = whandle.modifier & 0xffffffff;
return true;
default:
return false;
}
}
static bool
dri2_resource_get_param(__DRIimage *image, enum pipe_resource_param param,
unsigned handle_usage, uint64_t *value)
{
struct pipe_screen *pscreen = image->texture->screen;
if (!pscreen->resource_get_param)
return false;
return pscreen->resource_get_param(pscreen, NULL, image->texture,
image->plane, 0, param, handle_usage,
value);
}
static bool
dri2_query_image_by_resource_param(__DRIimage *image, int attrib, int *value)
{
enum pipe_resource_param param;
uint64_t res_param;
unsigned handle_usage;
if (!image->texture->screen->resource_get_param)
return false;
switch (attrib) {
case __DRI_IMAGE_ATTRIB_STRIDE:
param = PIPE_RESOURCE_PARAM_STRIDE;
break;
case __DRI_IMAGE_ATTRIB_OFFSET:
param = PIPE_RESOURCE_PARAM_OFFSET;
break;
case __DRI_IMAGE_ATTRIB_NUM_PLANES:
param = PIPE_RESOURCE_PARAM_NPLANES;
break;
case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
param = PIPE_RESOURCE_PARAM_MODIFIER;
break;
case __DRI_IMAGE_ATTRIB_HANDLE:
param = PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS;
break;
case __DRI_IMAGE_ATTRIB_NAME:
param = PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED;
break;
case __DRI_IMAGE_ATTRIB_FD:
param = PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD;
break;
default:
return false;
}
handle_usage = PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
if (image->use & __DRI_IMAGE_USE_BACKBUFFER)
handle_usage |= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
if (!dri2_resource_get_param(image, param, handle_usage, &res_param))
return false;
switch (attrib) {
case __DRI_IMAGE_ATTRIB_STRIDE:
case __DRI_IMAGE_ATTRIB_OFFSET:
case __DRI_IMAGE_ATTRIB_NUM_PLANES:
if (res_param > INT_MAX)
return false;
*value = (int)res_param;
return true;
case __DRI_IMAGE_ATTRIB_HANDLE:
case __DRI_IMAGE_ATTRIB_NAME:
case __DRI_IMAGE_ATTRIB_FD:
if (res_param > UINT_MAX)
return false;
*value = (int)res_param;
return true;
case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
if (res_param == DRM_FORMAT_MOD_INVALID)
return false;
*value = (res_param >> 32) & 0xffffffff;
return true;
case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
if (res_param == DRM_FORMAT_MOD_INVALID)
return false;
*value = res_param & 0xffffffff;
return true;
default:
return false;
}
}
static GLboolean
dri2_query_image(__DRIimage *image, int attrib, int *value)
{
if (dri2_query_image_common(image, attrib, value))
return GL_TRUE;
else if (dri2_query_image_by_resource_param(image, attrib, value))
return GL_TRUE;
else if (dri2_query_image_by_resource_handle(image, attrib, value))
return GL_TRUE;
else
return GL_FALSE;
}
static __DRIimage *
dri2_dup_image(__DRIimage *image, void *loaderPrivate)
{
__DRIimage *img;
img = CALLOC_STRUCT(__DRIimageRec);
if (!img)
return NULL;
img->texture = NULL;
pipe_resource_reference(&img->texture, image->texture);
img->level = image->level;
img->layer = image->layer;
img->dri_format = image->dri_format;
/* This should be 0 for sub images, but dup is also used for base images. */
img->dri_components = image->dri_components;
img->loader_private = loaderPrivate;
return img;
}
static GLboolean
dri2_validate_usage(__DRIimage *image, unsigned int use)
{
if (!image || !image->texture)
return false;
struct pipe_screen *screen = image->texture->screen;
if (!screen->check_resource_capability)
return true;
/* We don't want to check these:
* __DRI_IMAGE_USE_SHARE (all images are shareable)
* __DRI_IMAGE_USE_BACKBUFFER (all images support this)
*/
unsigned bind = 0;
if (use & __DRI_IMAGE_USE_SCANOUT)
bind |= PIPE_BIND_SCANOUT;
if (use & __DRI_IMAGE_USE_LINEAR)
bind |= PIPE_BIND_LINEAR;
if (use & __DRI_IMAGE_USE_CURSOR)
bind |= PIPE_BIND_CURSOR;
if (!bind)
return true;
return screen->check_resource_capability(screen, image->texture, bind);
}
static __DRIimage *
dri2_from_names(__DRIscreen *screen, int width, int height, int format,
int *names, int num_names, int *strides, int *offsets,
void *loaderPrivate)
{
const struct dri2_format_mapping *map = dri2_get_mapping_by_format(format);
__DRIimage *img;
struct winsys_handle whandle;
if (!map)
return NULL;
if (num_names != 1)
return NULL;
memset(&whandle, 0, sizeof(whandle));
whandle.type = WINSYS_HANDLE_TYPE_SHARED;
whandle.handle = names[0];
whandle.stride = strides[0];
whandle.offset = offsets[0];
whandle.format = map->pipe_format;
whandle.modifier = DRM_FORMAT_MOD_INVALID;
img = dri2_create_image_from_winsys(screen, width, height, map,
1, &whandle, loaderPrivate);
if (img == NULL)
return NULL;
img->dri_components = map->dri_components;
img->dri_fourcc = map->dri_fourcc;
img->dri_format = map->pipe_format;
return img;
}
static __DRIimage *
dri2_from_planar(__DRIimage *image, int plane, void *loaderPrivate)
{
__DRIimage *img;
if (plane < 0) {
return NULL;
} else if (plane > 0) {
uint64_t planes;
if (!dri2_resource_get_param(image, PIPE_RESOURCE_PARAM_NPLANES, 0,
&planes) ||
plane >= planes) {
return NULL;
}
}
if (image->dri_components == 0) {
uint64_t modifier;
if (!dri2_resource_get_param(image, PIPE_RESOURCE_PARAM_MODIFIER, 0,
&modifier) ||
modifier == DRM_FORMAT_MOD_INVALID) {
return NULL;
}
}
img = dri2_dup_image(image, loaderPrivate);
if (img == NULL)
return NULL;
if (img->texture->screen->resource_changed)
img->texture->screen->resource_changed(img->texture->screen,
img->texture);
/* set this to 0 for sub images. */
img->dri_components = 0;
img->plane = plane;
return img;
}
static __DRIimage *
dri2_from_fds(__DRIscreen *screen, int width, int height, int fourcc,
int *fds, int num_fds, int *strides, int *offsets,
void *loaderPrivate)
{
return dri2_create_image_from_fd(screen, width, height, fourcc,
DRM_FORMAT_MOD_INVALID, fds, num_fds,
strides, offsets, NULL, loaderPrivate);
}
static boolean
dri2_query_dma_buf_modifiers(__DRIscreen *_screen, int fourcc, int max,
uint64_t *modifiers, unsigned int *external_only,
int *count)
{
struct dri_screen *screen = dri_screen(_screen);
struct pipe_screen *pscreen = screen->base.screen;
const struct dri2_format_mapping *map = dri2_get_mapping_by_fourcc(fourcc);
enum pipe_format format;
if (!map)
return false;
format = map->pipe_format;
if (pscreen->query_dmabuf_modifiers != NULL &&
(pscreen->is_format_supported(pscreen, format, screen->target, 0, 0,
PIPE_BIND_RENDER_TARGET) ||
pscreen->is_format_supported(pscreen, format, screen->target, 0, 0,
PIPE_BIND_SAMPLER_VIEW))) {
pscreen->query_dmabuf_modifiers(pscreen, format, max, modifiers,
external_only, count);
return true;
}
return false;
}
static boolean
dri2_query_dma_buf_format_modifier_attribs(__DRIscreen *_screen,
uint32_t fourcc, uint64_t modifier,
int attrib, uint64_t *value)
{
switch (attrib) {
case __DRI_IMAGE_FORMAT_MODIFIER_ATTRIB_PLANE_COUNT: {
uint64_t mod_planes = dri2_get_modifier_num_planes(modifier, fourcc);
if (mod_planes > 0)
*value = mod_planes;
return mod_planes > 0;
}
default:
return false;
}
}
static __DRIimage *
dri2_from_dma_bufs(__DRIscreen *screen,
int width, int height, int fourcc,
int *fds, int num_fds,
int *strides, int *offsets,
enum __DRIYUVColorSpace yuv_color_space,
enum __DRISampleRange sample_range,
enum __DRIChromaSiting horizontal_siting,
enum __DRIChromaSiting vertical_siting,
unsigned *error,
void *loaderPrivate)
{
__DRIimage *img;
img = dri2_create_image_from_fd(screen, width, height, fourcc,
DRM_FORMAT_MOD_INVALID, fds, num_fds,
strides, offsets, error, loaderPrivate);
if (img == NULL)
return NULL;
img->yuv_color_space = yuv_color_space;
img->sample_range = sample_range;
img->horizontal_siting = horizontal_siting;
img->vertical_siting = vertical_siting;
*error = __DRI_IMAGE_ERROR_SUCCESS;
return img;
}
static __DRIimage *
dri2_from_dma_bufs2(__DRIscreen *screen,
int width, int height, int fourcc,
uint64_t modifier, int *fds, int num_fds,
int *strides, int *offsets,
enum __DRIYUVColorSpace yuv_color_space,
enum __DRISampleRange sample_range,
enum __DRIChromaSiting horizontal_siting,
enum __DRIChromaSiting vertical_siting,
unsigned *error,
void *loaderPrivate)
{
__DRIimage *img;
img = dri2_create_image_from_fd(screen, width, height, fourcc,
modifier, fds, num_fds, strides, offsets,
error, loaderPrivate);
if (img == NULL)
return NULL;
img->yuv_color_space = yuv_color_space;
img->sample_range = sample_range;
img->horizontal_siting = horizontal_siting;
img->vertical_siting = vertical_siting;
*error = __DRI_IMAGE_ERROR_SUCCESS;
return img;
}
static void
dri2_blit_image(__DRIcontext *context, __DRIimage *dst, __DRIimage *src,
int dstx0, int dsty0, int dstwidth, int dstheight,
int srcx0, int srcy0, int srcwidth, int srcheight,
int flush_flag)
{
struct dri_context *ctx = dri_context(context);
struct pipe_context *pipe = ctx->st->pipe;
struct pipe_screen *screen;
struct pipe_fence_handle *fence;
struct pipe_blit_info blit;
if (!dst || !src)
return;
memset(&blit, 0, sizeof(blit));
blit.dst.resource = dst->texture;
blit.dst.box.x = dstx0;
blit.dst.box.y = dsty0;
blit.dst.box.width = dstwidth;
blit.dst.box.height = dstheight;
blit.dst.box.depth = 1;
blit.dst.format = dst->texture->format;
blit.src.resource = src->texture;
blit.src.box.x = srcx0;
blit.src.box.y = srcy0;
blit.src.box.width = srcwidth;
blit.src.box.height = srcheight;
blit.src.box.depth = 1;
blit.src.format = src->texture->format;
blit.mask = PIPE_MASK_RGBA;
blit.filter = PIPE_TEX_FILTER_NEAREST;
pipe->blit(pipe, &blit);
if (flush_flag == __BLIT_FLAG_FLUSH) {
pipe->flush_resource(pipe, dst->texture);
ctx->st->flush(ctx->st, 0, NULL, NULL, NULL);
} else if (flush_flag == __BLIT_FLAG_FINISH) {
screen = dri_screen(ctx->sPriv)->base.screen;
pipe->flush_resource(pipe, dst->texture);
ctx->st->flush(ctx->st, 0, &fence, NULL, NULL);
(void) screen->fence_finish(screen, NULL, fence, PIPE_TIMEOUT_INFINITE);
screen->fence_reference(screen, &fence, NULL);
}
}
static void *
dri2_map_image(__DRIcontext *context, __DRIimage *image,
int x0, int y0, int width, int height,
unsigned int flags, int *stride, void **data)
{
struct dri_context *ctx = dri_context(context);
struct pipe_context *pipe = ctx->st->pipe;
enum pipe_transfer_usage pipe_access = 0;
struct pipe_transfer *trans;
void *map;
if (!image || !data || *data)
return NULL;
if (flags & __DRI_IMAGE_TRANSFER_READ)
pipe_access |= PIPE_TRANSFER_READ;
if (flags & __DRI_IMAGE_TRANSFER_WRITE)
pipe_access |= PIPE_TRANSFER_WRITE;
map = pipe_transfer_map(pipe, image->texture,
0, 0, pipe_access, x0, y0, width, height,
&trans);
if (map) {
*data = trans;
*stride = trans->stride;
}
return map;
}
static void
dri2_unmap_image(__DRIcontext *context, __DRIimage *image, void *data)
{
struct dri_context *ctx = dri_context(context);
struct pipe_context *pipe = ctx->st->pipe;
pipe_transfer_unmap(pipe, (struct pipe_transfer *)data);
}
static int
dri2_get_capabilities(__DRIscreen *_screen)
{
struct dri_screen *screen = dri_screen(_screen);
return (screen->can_share_buffer ? __DRI_IMAGE_CAP_GLOBAL_NAMES : 0);
}
/* The extension is modified during runtime if DRI_PRIME is detected */
static __DRIimageExtension dri2ImageExtension = {
.base = { __DRI_IMAGE, 17 },
.createImageFromName = dri2_create_image_from_name,
.createImageFromRenderbuffer = dri2_create_image_from_renderbuffer,
.destroyImage = dri2_destroy_image,
.createImage = dri2_create_image,
.queryImage = dri2_query_image,
.dupImage = dri2_dup_image,
.validateUsage = dri2_validate_usage,
.createImageFromNames = dri2_from_names,
.fromPlanar = dri2_from_planar,
.createImageFromTexture = dri2_create_from_texture,
.createImageFromFds = NULL,
.createImageFromDmaBufs = NULL,
.blitImage = dri2_blit_image,
.getCapabilities = dri2_get_capabilities,
.mapImage = dri2_map_image,
.unmapImage = dri2_unmap_image,
.createImageWithModifiers = NULL,
.createImageFromDmaBufs2 = NULL,
.queryDmaBufFormats = NULL,
.queryDmaBufModifiers = NULL,
.queryDmaBufFormatModifierAttribs = NULL,
.createImageFromRenderbuffer2 = dri2_create_image_from_renderbuffer2,
};
static const __DRIrobustnessExtension dri2Robustness = {
.base = { __DRI2_ROBUSTNESS, 1 }
};
static int
dri2_interop_query_device_info(__DRIcontext *_ctx,
struct mesa_glinterop_device_info *out)
{
struct pipe_screen *screen = dri_context(_ctx)->st->pipe->screen;
/* There is no version 0, thus we do not support it */
if (out->version == 0)
return MESA_GLINTEROP_INVALID_VERSION;
out->pci_segment_group = screen->get_param(screen, PIPE_CAP_PCI_GROUP);
out->pci_bus = screen->get_param(screen, PIPE_CAP_PCI_BUS);
out->pci_device = screen->get_param(screen, PIPE_CAP_PCI_DEVICE);
out->pci_function = screen->get_param(screen, PIPE_CAP_PCI_FUNCTION);
out->vendor_id = screen->get_param(screen, PIPE_CAP_VENDOR_ID);
out->device_id = screen->get_param(screen, PIPE_CAP_DEVICE_ID);
/* Instruct the caller that we support up-to version one of the interface */
out->version = 1;
return MESA_GLINTEROP_SUCCESS;
}
static int
dri2_interop_export_object(__DRIcontext *_ctx,
struct mesa_glinterop_export_in *in,
struct mesa_glinterop_export_out *out)
{
struct st_context_iface *st = dri_context(_ctx)->st;
struct pipe_screen *screen = st->pipe->screen;
struct gl_context *ctx = ((struct st_context *)st)->ctx;
struct pipe_resource *res = NULL;
struct winsys_handle whandle;
unsigned target, usage;
boolean success;
/* There is no version 0, thus we do not support it */
if (in->version == 0 || out->version == 0)
return MESA_GLINTEROP_INVALID_VERSION;
/* Validate the target. */
switch (in->target) {
case GL_TEXTURE_BUFFER:
case GL_TEXTURE_1D:
case GL_TEXTURE_2D:
case GL_TEXTURE_3D:
case GL_TEXTURE_RECTANGLE:
case GL_TEXTURE_1D_ARRAY:
case GL_TEXTURE_2D_ARRAY:
case GL_TEXTURE_CUBE_MAP_ARRAY:
case GL_TEXTURE_CUBE_MAP:
case GL_TEXTURE_2D_MULTISAMPLE:
case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
case GL_TEXTURE_EXTERNAL_OES:
case GL_RENDERBUFFER:
case GL_ARRAY_BUFFER:
target = in->target;
break;
case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
target = GL_TEXTURE_CUBE_MAP;
break;
default:
return MESA_GLINTEROP_INVALID_TARGET;
}
/* Validate the simple case of miplevel. */
if ((target == GL_RENDERBUFFER || target == GL_ARRAY_BUFFER) &&
in->miplevel != 0)
return MESA_GLINTEROP_INVALID_MIP_LEVEL;
/* Validate the OpenGL object and get pipe_resource. */
simple_mtx_lock(&ctx->Shared->Mutex);
if (target == GL_ARRAY_BUFFER) {
/* Buffer objects.
*
* The error checking is based on the documentation of
* clCreateFromGLBuffer from OpenCL 2.0 SDK.
*/
struct gl_buffer_object *buf = _mesa_lookup_bufferobj(ctx, in->obj);
/* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
* "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
* a GL buffer object but does not have an existing data store or
* the size of the buffer is 0."
*/
if (!buf || buf->Size == 0) {
simple_mtx_unlock(&ctx->Shared->Mutex);
return MESA_GLINTEROP_INVALID_OBJECT;
}
res = st_buffer_object(buf)->buffer;
if (!res) {
/* this shouldn't happen */
simple_mtx_unlock(&ctx->Shared->Mutex);
return MESA_GLINTEROP_INVALID_OBJECT;
}
out->buf_offset = 0;
out->buf_size = buf->Size;
buf->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
} else if (target == GL_RENDERBUFFER) {
/* Renderbuffers.
*
* The error checking is based on the documentation of
* clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
*/
struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, in->obj);
/* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
* "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
* object or if the width or height of renderbuffer is zero."
*/
if (!rb || rb->Width == 0 || rb->Height == 0) {
simple_mtx_unlock(&ctx->Shared->Mutex);
return MESA_GLINTEROP_INVALID_OBJECT;
}
/* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
* "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
* renderbuffer object."
*/
if (rb->NumSamples > 1) {
simple_mtx_unlock(&ctx->Shared->Mutex);
return MESA_GLINTEROP_INVALID_OPERATION;
}
/* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
* "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
* required by the OpenCL implementation on the device."
*/
res = st_renderbuffer(rb)->texture;
if (!res) {
simple_mtx_unlock(&ctx->Shared->Mutex);
return MESA_GLINTEROP_OUT_OF_RESOURCES;
}
out->internal_format = rb->InternalFormat;
out->view_minlevel = 0;
out->view_numlevels = 1;
out->view_minlayer = 0;
out->view_numlayers = 1;
} else {
/* Texture objects.
*
* The error checking is based on the documentation of
* clCreateFromGLTexture from OpenCL 2.0 SDK.
*/
struct gl_texture_object *obj = _mesa_lookup_texture(ctx, in->obj);
if (obj)
_mesa_test_texobj_completeness(ctx, obj);
/* From OpenCL 2.0 SDK, clCreateFromGLTexture:
* "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
* type matches texture_target, if the specified miplevel of texture
* is not defined, or if the width or height of the specified
* miplevel is zero or if the GL texture object is incomplete."
*/
if (!obj ||
obj->Target != target ||
!obj->_BaseComplete ||
(in->miplevel > 0 && !obj->_MipmapComplete)) {
simple_mtx_unlock(&ctx->Shared->Mutex);
return MESA_GLINTEROP_INVALID_OBJECT;
}
if (target == GL_TEXTURE_BUFFER) {
struct st_buffer_object *stBuf =
st_buffer_object(obj->BufferObject);
if (!stBuf || !stBuf->buffer) {
/* this shouldn't happen */
simple_mtx_unlock(&ctx->Shared->Mutex);
return MESA_GLINTEROP_INVALID_OBJECT;
}
res = stBuf->buffer;
out->internal_format = obj->BufferObjectFormat;
out->buf_offset = obj->BufferOffset;
out->buf_size = obj->BufferSize == -1 ? obj->BufferObject->Size :
obj->BufferSize;
obj->BufferObject->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
} else {
/* From OpenCL 2.0 SDK, clCreateFromGLTexture:
* "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
* levelbase (for OpenGL implementations) or zero (for OpenGL ES
* implementations); or greater than the value of q (for both OpenGL
* and OpenGL ES). levelbase and q are defined for the texture in
* section 3.8.10 (Texture Completeness) of the OpenGL 2.1
* specification and section 3.7.10 of the OpenGL ES 2.0."
*/
if (in->miplevel < obj->BaseLevel || in->miplevel > obj->_MaxLevel) {
simple_mtx_unlock(&ctx->Shared->Mutex);
return MESA_GLINTEROP_INVALID_MIP_LEVEL;
}
if (!st_finalize_texture(ctx, st->pipe, obj, 0)) {
simple_mtx_unlock(&ctx->Shared->Mutex);
return MESA_GLINTEROP_OUT_OF_RESOURCES;
}
res = st_get_texobj_resource(obj);
if (!res) {
/* Incomplete texture buffer object? This shouldn't really occur. */
simple_mtx_unlock(&ctx->Shared->Mutex);
return MESA_GLINTEROP_INVALID_OBJECT;
}
out->internal_format = obj->Image[0][0]->InternalFormat;
out->view_minlevel = obj->MinLevel;
out->view_numlevels = obj->NumLevels;
out->view_minlayer = obj->MinLayer;
out->view_numlayers = obj->NumLayers;
}
}
/* Get the handle. */
switch (in->access) {
case MESA_GLINTEROP_ACCESS_READ_ONLY:
usage = 0;
break;
case MESA_GLINTEROP_ACCESS_READ_WRITE:
case MESA_GLINTEROP_ACCESS_WRITE_ONLY:
usage = PIPE_HANDLE_USAGE_SHADER_WRITE;
break;
default:
usage = 0;
}
memset(&whandle, 0, sizeof(whandle));
whandle.type = WINSYS_HANDLE_TYPE_FD;
success = screen->resource_get_handle(screen, st->pipe, res, &whandle,
usage);
simple_mtx_unlock(&ctx->Shared->Mutex);
if (!success)
return MESA_GLINTEROP_OUT_OF_HOST_MEMORY;
out->dmabuf_fd = whandle.handle;
out->out_driver_data_written = 0;
if (res->target == PIPE_BUFFER)
out->buf_offset += whandle.offset;
/* Instruct the caller that we support up-to version one of the interface */
in->version = 1;
out->version = 1;
return MESA_GLINTEROP_SUCCESS;
}
static const __DRI2interopExtension dri2InteropExtension = {
.base = { __DRI2_INTEROP, 1 },
.query_device_info = dri2_interop_query_device_info,
.export_object = dri2_interop_export_object
};
/**
* \brief the DRI2bufferDamageExtension set_damage_region method
*/
static void
dri2_set_damage_region(__DRIdrawable *dPriv, unsigned int nrects, int *rects)
{
struct dri_drawable *drawable = dri_drawable(dPriv);
struct pipe_box *boxes = NULL;
if (nrects) {
boxes = CALLOC(nrects, sizeof(*boxes));
assert(boxes);
for (unsigned int i = 0; i < nrects; i++) {
int *rect = &rects[i * 4];
u_box_2d(rect[0], rect[1], rect[2], rect[3], &boxes[i]);
}
}
FREE(drawable->damage_rects);
drawable->damage_rects = boxes;
drawable->num_damage_rects = nrects;
/* Only apply the damage region if the BACK_LEFT texture is up-to-date. */
if (drawable->texture_stamp == drawable->dPriv->lastStamp &&
(drawable->texture_mask & (1 << ST_ATTACHMENT_BACK_LEFT))) {
struct pipe_screen *screen = drawable->screen->base.screen;
struct pipe_resource *resource;
if (drawable->stvis.samples > 1)
resource = drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT];
else
resource = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
screen->set_damage_region(screen, resource,
drawable->num_damage_rects,
drawable->damage_rects);
}
}
static __DRI2bufferDamageExtension dri2BufferDamageExtension = {
.base = { __DRI2_BUFFER_DAMAGE, 1 },
};
/**
* \brief the DRI2ConfigQueryExtension configQueryb method
*/
static int
dri2GalliumConfigQueryb(__DRIscreen *sPriv, const char *var,
unsigned char *val)
{
struct dri_screen *screen = dri_screen(sPriv);
if (!driCheckOption(&screen->dev->option_cache, var, DRI_BOOL))
return dri2ConfigQueryExtension.configQueryb(sPriv, var, val);
*val = driQueryOptionb(&screen->dev->option_cache, var);
return 0;
}
/**
* \brief the DRI2ConfigQueryExtension configQueryi method
*/
static int
dri2GalliumConfigQueryi(__DRIscreen *sPriv, const char *var, int *val)
{
struct dri_screen *screen = dri_screen(sPriv);
if (!driCheckOption(&screen->dev->option_cache, var, DRI_INT) &&
!driCheckOption(&screen->dev->option_cache, var, DRI_ENUM))
return dri2ConfigQueryExtension.configQueryi(sPriv, var, val);
*val = driQueryOptioni(&screen->dev->option_cache, var);
return 0;
}
/**
* \brief the DRI2ConfigQueryExtension configQueryf method
*/
static int
dri2GalliumConfigQueryf(__DRIscreen *sPriv, const char *var, float *val)
{
struct dri_screen *screen = dri_screen(sPriv);
if (!driCheckOption(&screen->dev->option_cache, var, DRI_FLOAT))
return dri2ConfigQueryExtension.configQueryf(sPriv, var, val);
*val = driQueryOptionf(&screen->dev->option_cache, var);
return 0;
}
/**
* \brief the DRI2ConfigQueryExtension struct.
*
* We first query the driver option cache. Then the dri2 option cache.
*/
static const __DRI2configQueryExtension dri2GalliumConfigQueryExtension = {
.base = { __DRI2_CONFIG_QUERY, 1 },
.configQueryb = dri2GalliumConfigQueryb,
.configQueryi = dri2GalliumConfigQueryi,
.configQueryf = dri2GalliumConfigQueryf,
};
/**
* \brief the DRI2blobExtension set_cache_funcs method
*/
static void
set_blob_cache_funcs(__DRIscreen *sPriv, __DRIblobCacheSet set,
__DRIblobCacheGet get)
{
struct dri_screen *screen = dri_screen(sPriv);
struct pipe_screen *pscreen = screen->base.screen;
if (!pscreen->get_disk_shader_cache)
return;
struct disk_cache *cache = pscreen->get_disk_shader_cache(pscreen);
if (!cache)
return;
disk_cache_set_callbacks(cache, set, get);
}
static const __DRI2blobExtension driBlobExtension = {
.base = { __DRI2_BLOB, 1 },
.set_cache_funcs = set_blob_cache_funcs
};
/*
* Backend function init_screen.
*/
static const __DRIextension *dri_screen_extensions[] = {
&driTexBufferExtension.base,
&dri2FlushExtension.base,
&dri2ImageExtension.base,
&dri2RendererQueryExtension.base,
&dri2GalliumConfigQueryExtension.base,
&dri2ThrottleExtension.base,
&dri2FenceExtension.base,
&dri2BufferDamageExtension.base,
&dri2InteropExtension.base,
&dri2NoErrorExtension.base,
&driBlobExtension.base,
NULL
};
static const __DRIextension *dri_robust_screen_extensions[] = {
&driTexBufferExtension.base,
&dri2FlushExtension.base,
&dri2ImageExtension.base,
&dri2RendererQueryExtension.base,
&dri2GalliumConfigQueryExtension.base,
&dri2ThrottleExtension.base,
&dri2FenceExtension.base,
&dri2InteropExtension.base,
&dri2BufferDamageExtension.base,
&dri2Robustness.base,
&dri2NoErrorExtension.base,
&driBlobExtension.base,
NULL
};
/**
* This is the driver specific part of the createNewScreen entry point.
*
* Returns the struct gl_config supported by this driver.
*/
static const __DRIconfig **
dri2_init_screen(__DRIscreen * sPriv)
{
const __DRIconfig **configs;
struct dri_screen *screen;
struct pipe_screen *pscreen = NULL;
screen = CALLOC_STRUCT(dri_screen);
if (!screen)
return NULL;
screen->sPriv = sPriv;
screen->fd = sPriv->fd;
(void) mtx_init(&screen->opencl_func_mutex, mtx_plain);
sPriv->driverPrivate = (void *)screen;
if (pipe_loader_drm_probe_fd(&screen->dev, screen->fd)) {
dri_init_options(screen);
pscreen = pipe_loader_create_screen(screen->dev);
}
if (!pscreen)
goto release_pipe;
screen->throttle = pscreen->get_param(pscreen, PIPE_CAP_THROTTLE);
if (pscreen->resource_create_with_modifiers)
dri2ImageExtension.createImageWithModifiers =
dri2_create_image_with_modifiers;
if (pscreen->get_param(pscreen, PIPE_CAP_DMABUF)) {
uint64_t cap;
if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
(cap & DRM_PRIME_CAP_IMPORT)) {
dri2ImageExtension.createImageFromFds = dri2_from_fds;
dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
dri2ImageExtension.createImageFromDmaBufs2 = dri2_from_dma_bufs2;
if (pscreen->query_dmabuf_modifiers) {
dri2ImageExtension.queryDmaBufFormats = dri2_query_dma_buf_formats;
dri2ImageExtension.queryDmaBufModifiers =
dri2_query_dma_buf_modifiers;
dri2ImageExtension.queryDmaBufFormatModifierAttribs =
dri2_query_dma_buf_format_modifier_attribs;
}
}
}
if (pscreen->set_damage_region)
dri2BufferDamageExtension.set_damage_region = dri2_set_damage_region;
if (pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY)) {
sPriv->extensions = dri_robust_screen_extensions;
screen->has_reset_status_query = true;
}
else
sPriv->extensions = dri_screen_extensions;
configs = dri_init_screen_helper(screen, pscreen);
if (!configs)
goto destroy_screen;
screen->can_share_buffer = true;
screen->auto_fake_front = dri_with_format(sPriv);
screen->broken_invalidate = !sPriv->dri2.useInvalidate;
screen->lookup_egl_image = dri2_lookup_egl_image;
return configs;
destroy_screen:
dri_destroy_screen_helper(screen);
release_pipe:
if (screen->dev)
pipe_loader_release(&screen->dev, 1);
FREE(screen);
return NULL;
}
/**
* This is the driver specific part of the createNewScreen entry point.
*
* Returns the struct gl_config supported by this driver.
*/
static const __DRIconfig **
dri_kms_init_screen(__DRIscreen * sPriv)
{
#if defined(GALLIUM_SOFTPIPE)
const __DRIconfig **configs;
struct dri_screen *screen;
struct pipe_screen *pscreen = NULL;
uint64_t cap;
screen = CALLOC_STRUCT(dri_screen);
if (!screen)
return NULL;
screen->sPriv = sPriv;
screen->fd = sPriv->fd;
sPriv->driverPrivate = (void *)screen;
if (pipe_loader_sw_probe_kms(&screen->dev, screen->fd)) {
dri_init_options(screen);
pscreen = pipe_loader_create_screen(screen->dev);
}
if (!pscreen)
goto release_pipe;
if (pscreen->resource_create_with_modifiers)
dri2ImageExtension.createImageWithModifiers =
dri2_create_image_with_modifiers;
if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
(cap & DRM_PRIME_CAP_IMPORT)) {
dri2ImageExtension.createImageFromFds = dri2_from_fds;
dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
dri2ImageExtension.createImageFromDmaBufs2 = dri2_from_dma_bufs2;
if (pscreen->query_dmabuf_modifiers) {
dri2ImageExtension.queryDmaBufFormats = dri2_query_dma_buf_formats;
dri2ImageExtension.queryDmaBufModifiers = dri2_query_dma_buf_modifiers;
}
}
sPriv->extensions = dri_screen_extensions;
configs = dri_init_screen_helper(screen, pscreen);
if (!configs)
goto destroy_screen;
screen->can_share_buffer = false;
screen->auto_fake_front = dri_with_format(sPriv);
screen->broken_invalidate = !sPriv->dri2.useInvalidate;
screen->lookup_egl_image = dri2_lookup_egl_image;
return configs;
destroy_screen:
dri_destroy_screen_helper(screen);
release_pipe:
if (screen->dev)
pipe_loader_release(&screen->dev, 1);
FREE(screen);
#endif // GALLIUM_SOFTPIPE
return NULL;
}
static boolean
dri2_create_buffer(__DRIscreen * sPriv,
__DRIdrawable * dPriv,
const struct gl_config * visual, boolean isPixmap)
{
struct dri_drawable *drawable = NULL;
if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
return FALSE;
drawable = dPriv->driverPrivate;
drawable->allocate_textures = dri2_allocate_textures;
drawable->flush_frontbuffer = dri2_flush_frontbuffer;
drawable->update_tex_buffer = dri2_update_tex_buffer;
drawable->flush_swapbuffers = dri2_flush_swapbuffers;
return TRUE;
}
/**
* DRI driver virtual function table.
*
* DRI versions differ in their implementation of init_screen and swap_buffers.
*/
const struct __DriverAPIRec galliumdrm_driver_api = {
.InitScreen = dri2_init_screen,
.DestroyScreen = dri_destroy_screen,
.CreateContext = dri_create_context,
.DestroyContext = dri_destroy_context,
.CreateBuffer = dri2_create_buffer,
.DestroyBuffer = dri_destroy_buffer,
.MakeCurrent = dri_make_current,
.UnbindContext = dri_unbind_context,
.AllocateBuffer = dri2_allocate_buffer,
.ReleaseBuffer = dri2_release_buffer,
};
/**
* DRI driver virtual function table.
*
* KMS/DRM version of the DriverAPI above sporting a different InitScreen
* hook. The latter is used to explicitly initialise the kms_swrast driver
* rather than selecting the approapriate driver as suggested by the loader.
*/
const struct __DriverAPIRec dri_kms_driver_api = {
.InitScreen = dri_kms_init_screen,
.DestroyScreen = dri_destroy_screen,
.CreateContext = dri_create_context,
.DestroyContext = dri_destroy_context,
.CreateBuffer = dri2_create_buffer,
.DestroyBuffer = dri_destroy_buffer,
.MakeCurrent = dri_make_current,
.UnbindContext = dri_unbind_context,
.AllocateBuffer = dri2_allocate_buffer,
.ReleaseBuffer = dri2_release_buffer,
};
/* This is the table of extensions that the loader will dlsym() for. */
const __DRIextension *galliumdrm_driver_extensions[] = {
&driCoreExtension.base,
&driImageDriverExtension.base,
&driDRI2Extension.base,
&gallium_config_options.base,
NULL
};
/* vim: set sw=3 ts=8 sts=3 expandtab: */
|