aboutsummaryrefslogtreecommitdiffstats
path: root/meta-emenlow/recipes-graphics/libva/libva-0.31.0/322_libva_glx.patch
blob: ccd9ae090eddb01adba152e038baf77e914b5d1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
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
commit 8e76000abce070da5f1c902a6290f4ccaa3eccc8
Author: Gwenole Beauchesne <gbeauchesne@splitted-desktop.com>
Date:   Fri Sep 18 15:51:08 2009 +0000

    Add C++ guards.

commit bf1ae22ef324fbb347f5369e1ba307e847553fe8
Author: Gwenole Beauchesne <gbeauchesne@splitted-desktop.com>
Date:   Fri Sep 18 15:49:55 2009 +0000

    Fix check for GL extensions.

commit df0953a951d8a2e5e4b0a28a95ae0f1ac735726e
Author: Gwenole Beauchesne <gbeauchesne@splitted-desktop.com>
Date:   Tue Sep 8 12:25:14 2009 +0000

    Add generic VA/GLX implementation with TFP and FBO.

commit f640b1cf9eab4e5d478239b608ed0d8b68f6c5f6
Author: Gwenole Beauchesne <gbeauchesne@splitted-desktop.com>
Date:   Tue Sep 8 12:15:35 2009 +0000

    Move GLX VTable to a new file.

commit 70d9cb6d1aa2fc2dde6646f3b692433e0d93d431
Author: Gwenole Beauchesne <gbeauchesne@splitted-desktop.com>
Date:   Fri Aug 28 11:15:51 2009 +0000

    Add OpenGL extensions (v3).

diff --git a/src/glx/Makefile.am b/src/glx/Makefile.am
new file mode 100644
index 0000000..7783d8c
--- /dev/null
+++ b/src/glx/Makefile.am
@@ -0,0 +1,41 @@
+# Copyright (C) 2009 Splitted-Desktop Systems. All Rights Reserved.
+#
+# 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, sub license, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+# 
+# The above copyright notice and this permission notice (including the
+# next paragraph) shall be included in all copies or substantial portions
+# of the Software.
+# 
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+# IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.
+
+AM_CFLAGS = -DLINUX -DIN_LIBVA -I$(top_srcdir)/src -I$(top_srcdir)/src/x11
+
+source_c = \
+	va_glx.c		\
+	va_glx_impl.c
+
+source_h = \
+	va_glx.h		\
+	va_backend_glx.h
+
+source_h_priv = \
+	va_glx_impl.h		\
+	va_glx_private.h
+
+noinst_LTLIBRARIES	 = libva_glx.la
+libva_glxincludedir	 = ${includedir}/va
+libva_glxinclude_HEADERS = $(source_h)
+libva_glx_la_SOURCES	 = $(source_c)
+noinst_HEADERS		 = $(source_h_priv)
diff --git a/src/glx/va_backend_glx.h b/src/glx/va_backend_glx.h
new file mode 100644
index 0000000..3885d30
--- /dev/null
+++ b/src/glx/va_backend_glx.h
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2009 Splitted-Desktop Systems. All Rights Reserved.
+ *
+ * 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, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.
+ */
+
+#ifndef VA_BACKEND_GLX_H
+#define VA_BACKEND_GLX_H
+
+struct VADriverContext;
+
+struct VADriverVTableGLX {
+    /* Optional: create a surface used for display to OpenGL */
+    VAStatus (*vaCreateSurfaceGLX)(
+        struct VADriverContext *ctx,
+        unsigned int            gl_target,
+        unsigned int            gl_texture,
+        void                  **gl_surface
+    );
+
+    /* Optional: destroy a VA/GLX surface */
+    VAStatus (*vaDestroySurfaceGLX)(
+        struct VADriverContext *ctx,
+        void                   *gl_surface
+    );
+
+    /* Optional: associate a VA surface to a VA/GLX surface */
+    VAStatus (*vaAssociateSurfaceGLX)(
+        struct VADriverContext *ctx,
+        void                   *gl_surface,
+        VASurfaceID             surface,
+        unsigned int            flags
+    );
+
+    /* Optional: deassociate a VA surface from a VA/GLX surface */
+    VAStatus (*vaDeassociateSurfaceGLX)(
+        struct VADriverContext *ctx,
+        void                   *gl_surface
+    );
+
+    /* Optional: synchronize a VA/GLX surface */
+    VAStatus (*vaSyncSurfaceGLX)(
+        struct VADriverContext *ctx,
+        void                   *gl_surface
+    );
+
+    /* Optional: prepare VA/GLX surface for rendering */
+    VAStatus (*vaBeginRenderSurfaceGLX)(
+        struct VADriverContext *ctx,
+        void                   *gl_surface
+    );
+
+    /* Optional: notify the server that the VA/GLX surface is no
+       longer used for rendering */
+    VAStatus (*vaEndRenderSurfaceGLX)(
+        struct VADriverContext *ctx,
+        void                   *gl_surface
+    );
+
+    /* Optional: copy a VA surface to a VA/GLX surface */
+    VAStatus (*vaCopySurfaceGLX)(
+        struct VADriverContext *ctx,
+        void                   *gl_surface,
+        VASurfaceID             surface,
+        unsigned int            flags
+    );
+};
+
+#endif /* VA_BACKEND_GLX_H */
diff --git a/src/glx/va_glx.c b/src/glx/va_glx.c
new file mode 100644
index 0000000..f6ec2c3
--- /dev/null
+++ b/src/glx/va_glx.c
@@ -0,0 +1,295 @@
+/*
+ * Copyright (C) 2009 Splitted-Desktop Systems. All Rights Reserved.
+ *
+ * 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, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "va_glx_private.h"
+#include "va_glx_impl.h"
+
+#define INIT_CONTEXT(ctx, dpy) do {                             \
+        if (!vaDisplayIsValid(dpy))                             \
+            return VA_STATUS_ERROR_INVALID_DISPLAY;             \
+                                                                \
+        ctx = ((VADisplayContextP)(dpy))->pDriverContext;       \
+        if (!(ctx))                                             \
+            return VA_STATUS_ERROR_INVALID_DISPLAY;             \
+                                                                \
+        VAStatus status = va_glx_init_context(ctx);             \
+        if (status != VA_STATUS_SUCCESS)                        \
+            return status;                                      \
+    } while (0)
+
+#define INIT_SURFACE(surface, surface_arg) do {                 \
+        surface = (VASurfaceGLXP)(surface_arg);                 \
+        if (!vaSurfaceIsValid(surface))                         \
+            return VA_STATUS_ERROR_INVALID_SURFACE;             \
+    } while (0)
+
+#define INVOKE(ctx, func, args) do {                            \
+        VADriverVTableGLXP vtable;                              \
+        vtable = &VA_DRIVER_CONTEXT_GLX(ctx)->vtable;           \
+        if (!vtable->va##func##GLX)                             \
+            return VA_STATUS_ERROR_UNIMPLEMENTED;               \
+        status = vtable->va##func##GLX args;                    \
+    } while (0)
+
+// Check VADisplay is valid
+static inline int vaDisplayIsValid(VADisplay dpy)
+{
+    VADisplayContextP pDisplayContext = (VADisplayContextP)dpy;
+
+    return (pDisplayContext &&
+            pDisplayContext->vaIsValid &&
+            pDisplayContext->vaIsValid(pDisplayContext));
+}
+
+// Check VASurfaceGLX is valid
+static inline int vaSurfaceIsValid(VASurfaceGLXP pSurfaceGLX)
+{
+    return pSurfaceGLX && pSurfaceGLX->magic == VA_SURFACE_GLX_MAGIC;
+}
+
+// Destroy VA/GLX display context
+static void va_DisplayContextDestroy(VADisplayContextP pDisplayContext)
+{
+    VADisplayContextGLXP pDisplayContextGLX;
+    VADriverContextP     pDriverContext;
+    VADriverContextGLXP  pDriverContextGLX;
+
+    if (!pDisplayContext)
+        return;
+
+    pDriverContext     = pDisplayContext->pDriverContext;
+    pDriverContextGLX  = pDriverContext->glx;
+    if (pDriverContextGLX) {
+        free(pDriverContextGLX);
+        pDriverContext->glx = NULL;
+    }
+
+    pDisplayContextGLX = pDisplayContext->opaque;
+    if (pDisplayContextGLX) {
+        if (pDisplayContextGLX->vaDestroy)
+            pDisplayContextGLX->vaDestroy(pDisplayContext);
+        free(pDisplayContextGLX);
+        pDisplayContext->opaque = NULL;
+    }
+}
+
+// Return a suitable VADisplay for VA API
+VADisplay vaGetDisplayGLX(Display *native_dpy)
+{
+    VADisplay            dpy                = NULL;
+    VADisplayContextP    pDisplayContext    = NULL;
+    VADisplayContextGLXP pDisplayContextGLX = NULL;
+    VADriverContextP     pDriverContext;
+    VADriverContextGLXP  pDriverContextGLX  = NULL;
+
+    dpy = vaGetDisplay(native_dpy);
+    if (!dpy)
+        return NULL;
+    pDisplayContext = (VADisplayContextP)dpy;
+    pDriverContext  = pDisplayContext->pDriverContext;
+
+    pDisplayContextGLX = calloc(1, sizeof(*pDisplayContextGLX));
+    if (!pDisplayContextGLX)
+        goto error;
+
+    pDriverContextGLX = calloc(1, sizeof(*pDriverContextGLX));
+    if (!pDriverContextGLX)
+        goto error;
+
+    pDisplayContextGLX->vaDestroy = pDisplayContext->vaDestroy;
+    pDisplayContext->vaDestroy    = va_DisplayContextDestroy;
+    pDisplayContext->opaque       = pDisplayContextGLX;
+    pDriverContext->glx           = pDriverContextGLX;
+    return dpy;
+
+error:
+    free(pDriverContextGLX);
+    free(pDisplayContextGLX);
+    pDisplayContext->vaDestroy(pDisplayContext);
+    return NULL;
+}
+
+// Create a surface used for display to OpenGL
+VAStatus vaCreateSurfaceGLX(
+    VADisplay dpy,
+    GLenum    target,
+    GLuint    texture,
+    void    **gl_surface
+)
+{
+    VADriverContextP ctx;
+    VASurfaceGLXP pSurfaceGLX;
+    VAStatus status;
+
+    /* Make sure it is a valid GL texture object */
+    if (!glIsTexture(texture))
+        return VA_STATUS_ERROR_INVALID_PARAMETER;
+
+    INIT_CONTEXT(ctx, dpy);
+
+    pSurfaceGLX = va_glx_create_surface(ctx, target, texture);
+    if (!pSurfaceGLX)
+        return VA_STATUS_ERROR_ALLOCATION_FAILED;
+
+    INVOKE(ctx, CreateSurface, (ctx, target, texture, &pSurfaceGLX->priv));
+
+    if (status != VA_STATUS_SUCCESS)
+        va_glx_destroy_surface(ctx, &pSurfaceGLX);
+
+    *gl_surface = pSurfaceGLX;
+    return status;
+}
+
+// Destroy a VA/GLX surface
+VAStatus vaDestroySurfaceGLX(
+    VADisplay dpy,
+    void     *gl_surface
+)
+{
+    VADriverContextP ctx;
+    VASurfaceGLXP pSurfaceGLX;
+    VAStatus status;
+
+    INIT_CONTEXT(ctx, dpy);
+    INIT_SURFACE(pSurfaceGLX, gl_surface);
+
+    INVOKE(ctx, DestroySurface, (ctx, pSurfaceGLX));
+
+    free(pSurfaceGLX);
+    return status;
+}
+
+// Associate a VA surface to a VA/GLX surface
+VAStatus vaAssociateSurfaceGLX(
+    VADisplay    dpy,
+    void        *gl_surface,
+    VASurfaceID  surface,
+    unsigned int flags
+)
+{
+    VADriverContextP ctx;
+    VASurfaceGLXP pSurfaceGLX;
+    VAStatus status;
+
+    INIT_CONTEXT(ctx, dpy);
+    INIT_SURFACE(pSurfaceGLX, gl_surface);
+
+    INVOKE(ctx, AssociateSurface, (ctx, pSurfaceGLX, surface, flags));
+
+    if (status == VA_STATUS_SUCCESS)
+        pSurfaceGLX->surface = surface;
+
+    return status;
+}
+
+// Deassociate a VA surface from a VA/GLX surface
+VAStatus vaDeassociateSurfaceGLX(
+    VADisplay dpy,
+    void     *gl_surface
+)
+{
+    VADriverContextP ctx;
+    VASurfaceGLXP pSurfaceGLX;
+    VAStatus status;
+
+    INIT_CONTEXT(ctx, dpy);
+    INIT_SURFACE(pSurfaceGLX, gl_surface);
+
+    INVOKE(ctx, DeassociateSurface, (ctx, pSurfaceGLX));
+
+    if (status == VA_STATUS_SUCCESS)
+        pSurfaceGLX->surface = VA_INVALID_SURFACE;
+
+    return status;
+}
+
+// Synchronize a VA/GLX surface
+VAStatus vaSyncSurfaceGLX(
+    VADisplay dpy,
+    void     *gl_surface
+)
+{
+    VADriverContextP ctx;
+    VASurfaceGLXP pSurfaceGLX;
+    VAStatus status;
+
+    INIT_CONTEXT(ctx, dpy);
+    INIT_SURFACE(pSurfaceGLX, gl_surface);
+
+    INVOKE(ctx, SyncSurface, (ctx, pSurfaceGLX));
+    return status;
+}
+
+// Prepare VA/GLX surface for rendering
+VAStatus vaBeginRenderSurfaceGLX(
+    VADisplay dpy,
+    void     *gl_surface
+)
+{
+    VADriverContextP ctx;
+    VASurfaceGLXP pSurfaceGLX;
+    VAStatus status;
+
+    INIT_CONTEXT(ctx, dpy);
+    INIT_SURFACE(pSurfaceGLX, gl_surface);
+
+    INVOKE(ctx, BeginRenderSurface, (ctx, pSurfaceGLX));
+    return status;
+}
+
+// Notify the server that the VA/GLX surface is no longer used for rendering
+VAStatus vaEndRenderSurfaceGLX(
+    VADisplay dpy,
+    void     *gl_surface
+)
+{
+    VADriverContextP ctx;
+    VASurfaceGLXP pSurfaceGLX;
+    VAStatus status;
+
+    INIT_CONTEXT(ctx, dpy);
+    INIT_SURFACE(pSurfaceGLX, gl_surface);
+
+    INVOKE(ctx, EndRenderSurface, (ctx, pSurfaceGLX));
+    return status;
+}
+
+// Copy a VA surface to a VA/GLX surface
+VAStatus vaCopySurfaceGLX(
+    VADisplay    dpy,
+    void        *gl_surface,
+    VASurfaceID  surface,
+    unsigned int flags
+)
+{
+    VADriverContextP ctx;
+    VASurfaceGLXP pSurfaceGLX;
+    VAStatus status;
+
+    INIT_CONTEXT(ctx, dpy);
+    INIT_SURFACE(pSurfaceGLX, gl_surface);
+
+    INVOKE(ctx, CopySurface, (ctx, pSurfaceGLX, surface, flags));
+    return status;
+}
diff --git a/src/glx/va_glx.h b/src/glx/va_glx.h
new file mode 100644
index 0000000..183ef3a
--- /dev/null
+++ b/src/glx/va_glx.h
@@ -0,0 +1,217 @@
+/*
+ * Copyright (C) 2009 Splitted-Desktop Systems. All Rights Reserved.
+ *
+ * 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, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.
+ */
+
+#ifndef VA_GLX_H
+#define VA_GLX_H
+
+#ifdef IN_LIBVA
+#include "va.h"
+#else
+#include <va/va.h>
+#endif
+#include <GL/glx.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Return a suitable VADisplay for VA API
+ *
+ * @param[in] dpy the X11 display
+ * @return a VADisplay
+ */
+VADisplay vaGetDisplayGLX(
+    Display *dpy
+);
+
+/**
+ * Create a surface used for display to OpenGL
+ *
+ * The application shall maintain the live GLX context itself.
+ * Implementations are free to use glXGetCurrentContext() and
+ * glXGetCurrentDrawable() functions for internal purposes.
+ *
+ * @param[in]  dpy        the VA display
+ * @param[in]  target     the GL target to which the texture needs to be bound
+ * @param[in]  texture    the GL texture
+ * @param[out] gl_surface the VA/GLX surface
+ * @return VA_STATUS_SUCCESS if successful
+ */
+VAStatus vaCreateSurfaceGLX(
+    VADisplay dpy,
+    GLenum    target,
+    GLuint    texture,
+    void    **gl_surface
+);
+
+/**
+ * Destroy a VA/GLX surface
+ *
+ * The application shall maintain the live GLX context itself.
+ * Implementations are free to use glXGetCurrentContext() and
+ * glXGetCurrentDrawable() functions for internal purposes.
+ *
+ * @param[in]  dpy        the VA display
+ * @param[in]  gl_surface the VA surface
+ * @return VA_STATUS_SUCCESS if successful
+ */
+VAStatus vaDestroySurfaceGLX(
+    VADisplay dpy,
+    void     *gl_surface
+);
+
+/**
+ * Associate a VA surface to a VA/GLX surface
+ *
+ * The association is live until vaDeassociateSurfaceGLX(),
+ * vaCopySurfaceGLX() or the next call to vaBeginPicture() with the
+ * specificed VA surface.
+ * 
+ * The application shall maintain the live GLX context itself.
+ * Implementations are free to use glXGetCurrentContext() and
+ * glXGetCurrentDrawable() functions for internal purposes.
+ *
+ * @param[in]  dpy        the VA display
+ * @param[in]  gl_surface the VA/GLX surface
+ * @param[in]  surface    the VA surface
+ * @param[in]  flags      the PutSurface flags
+ * @return VA_STATUS_SUCCESS if successful
+ */
+VAStatus vaAssociateSurfaceGLX(
+    VADisplay    dpy,
+    void        *gl_surface,
+    VASurfaceID  surface,
+    unsigned int flags
+);
+
+/**
+ * Deassociate a VA surface from a VA/GLX surface
+ *
+ * The application shall maintain the live GLX context itself.
+ * Implementations are free to use glXGetCurrentContext() and
+ * glXGetCurrentDrawable() functions for internal purposes.
+ *
+ * @param[in]  dpy        the VA display
+ * @param[in]  gl_surface the VA surface
+ * @return VA_STATUS_SUCCESS if successful
+ */
+VAStatus vaDeassociateSurfaceGLX(
+    VADisplay dpy,
+    void     *gl_surface
+);
+
+/**
+ * Synchronize a VA/GLX surface
+ *
+ * This function blocks until all pending operations on the VA/GLX
+ * surface have been completed.
+ *
+ * The application shall maintain the live GLX context itself.
+ * Implementations are free to use glXGetCurrentContext() and
+ * glXGetCurrentDrawable() functions for internal purposes.
+ *
+ * @param[in]  dpy        the VA display
+ * @param[in]  gl_surface the VA surface
+ * @return VA_STATUS_SUCCESS if successful
+ */
+VAStatus vaSyncSurfaceGLX(
+    VADisplay dpy,
+    void     *gl_surface
+);
+
+/**
+ * Prepare VA/GLX surface for rendering
+ *
+ * This function performs an implicit vaSyncSurfaceGLX().
+ *
+ * Implementations using the GLX texture-from-pixmap extension will
+ * generally call glXBindTexImage() here.
+ *
+ * The application shall maintain the live GLX context itself.
+ * Implementations are free to use glXGetCurrentContext() and
+ * glXGetCurrentDrawable() functions for internal purposes.
+ *
+ * @param[in]  dpy        the VA display
+ * @param[in]  gl_surface the VA surface
+ * @return VA_STATUS_SUCCESS if successful
+ */
+VAStatus vaBeginRenderSurfaceGLX(
+    VADisplay dpy,
+    void     *gl_surface
+);
+
+/**
+ * Notify the server that the VA/GLX surface is no longer used for
+ * rendering
+ *
+ * Implementations using the GLX texture-from-pixmap extension will
+ * generally call glXReleaseTexImage() here.
+ *
+ * The application shall maintain the live GLX context itself.
+ * Implementations are free to use glXGetCurrentContext() and
+ * glXGetCurrentDrawable() functions for internal purposes.
+ *
+ * @param[in]  dpy        the VA display
+ * @param[in]  gl_surface the VA surface
+ * @return VA_STATUS_SUCCESS if successful
+ */
+VAStatus vaEndRenderSurfaceGLX(
+    VADisplay dpy,
+    void     *gl_surface
+);
+
+/**
+ * Copy a VA surface to a VA/GLX surface
+ *
+ * This function kills any association that was previously made with
+ * vaAssociateSurfaceGLX() and will not return until the copy is
+ * completed.
+ *
+ * Upon successful return, the underlying GL texture will contain the
+ * complete pixels and no call to vaBeginRenderSurfaceGLX() or
+ * vaEndRenderSurfaceGLX() is required.
+ *
+ * The application shall maintain the live GLX context itself.
+ * Implementations are free to use glXGetCurrentContext() and
+ * glXGetCurrentDrawable() functions for internal purposes.
+ *
+ * @param[in]  dpy        the VA display
+ * @param[in]  gl_surface the VA/GLX destination surface
+ * @param[in]  surface    the VA source surface
+ * @param[in]  flags      the PutSurface flags
+ * @return VA_STATUS_SUCCESS if successful
+ */
+VAStatus vaCopySurfaceGLX(
+    VADisplay    dpy,
+    void        *gl_surface,
+    VASurfaceID  surface,
+    unsigned int flags
+);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* VA_GLX_H */
diff --git a/src/glx/va_glx_impl.c b/src/glx/va_glx_impl.c
new file mode 100644
index 0000000..d4f9c1d
--- /dev/null
+++ b/src/glx/va_glx_impl.c
@@ -0,0 +1,1168 @@
+/*
+ * Copyright (C) 2009 Splitted-Desktop Systems. All Rights Reserved.
+ *
+ * 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, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.
+ */
+
+#define _GNU_SOURCE 1
+#include "va_glx_private.h"
+#include "va_glx_impl.h"
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#include <assert.h>
+#include <dlfcn.h>
+
+static void va_glx_error_message(const char *format, ...)
+{
+    va_list args;
+    va_start(args, format);
+    fprintf(stderr, "[%s] ", PACKAGE_NAME);
+    vfprintf(stderr, format, args);
+    va_end(args);
+}
+
+// X error trap
+static int x11_error_code = 0;
+static int (*old_error_handler)(Display *, XErrorEvent *);
+
+static int error_handler(Display *dpy, XErrorEvent *error)
+{
+    x11_error_code = error->error_code;
+    return 0;
+}
+
+static void x11_trap_errors(void)
+{
+    x11_error_code    = 0;
+    old_error_handler = XSetErrorHandler(error_handler);
+}
+
+static int x11_untrap_errors(void)
+{
+    XSetErrorHandler(old_error_handler);
+    return x11_error_code;
+}
+
+// Returns a string representation of an OpenGL error
+static const char *gl_get_error_string(GLenum error)
+{
+    static const struct {
+        GLenum val;
+        const char *str;
+    }
+    gl_errors[] = {
+        { GL_NO_ERROR,          "no error" },
+        { GL_INVALID_ENUM,      "invalid enumerant" },
+        { GL_INVALID_VALUE,     "invalid value" },
+        { GL_INVALID_OPERATION, "invalid operation" },
+        { GL_STACK_OVERFLOW,    "stack overflow" },
+        { GL_STACK_UNDERFLOW,   "stack underflow" },
+        { GL_OUT_OF_MEMORY,     "out of memory" },
+#ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
+        { GL_INVALID_FRAMEBUFFER_OPERATION_EXT, "invalid framebuffer operation" },
+#endif
+        { ~0, NULL }
+    };
+
+    int i;
+    for (i = 0; gl_errors[i].str; i++) {
+        if (gl_errors[i].val == error)
+            return gl_errors[i].str;
+    }
+    return "unknown";
+}
+
+static inline int gl_do_check_error(int report)
+{
+    GLenum error;
+    int is_error = 0;
+    while ((error = glGetError()) != GL_NO_ERROR) {
+        if (report)
+            va_glx_error_message("glError: %s caught\n",
+                                 gl_get_error_string(error));
+        is_error = 1;
+    }
+    return is_error;
+}
+
+static inline void gl_purge_errors(void)
+{
+    gl_do_check_error(0);
+}
+
+static inline int gl_check_error(void)
+{
+    return gl_do_check_error(1);
+}
+
+// glGetFloatv() wrapper
+static int gl_get_current_color(float color[4])
+{
+    gl_purge_errors();
+    glGetFloatv(GL_CURRENT_COLOR, color);
+    if (gl_check_error())
+        return -1;
+    return 0;
+}
+
+// glGetIntegerv() wrapper
+static int gl_get_param(GLenum param, unsigned int *pval)
+{
+    GLint val;
+
+    gl_purge_errors();
+    glGetIntegerv(param, &val);
+    if (gl_check_error())
+        return -1;
+    if (pval)
+        *pval = val;
+    return 0;
+}
+
+// glGetTexLevelParameteriv() wrapper
+static int gl_get_texture_param(GLenum param, unsigned int *pval)
+{
+    GLint val;
+
+    gl_purge_errors();
+    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, param, &val);
+    if (gl_check_error())
+        return -1;
+    if (pval)
+        *pval = val;
+    return 0;
+}
+
+// Returns the OpenGL VTable
+static inline VAOpenGLVTableP gl_get_vtable(VADriverContextP ctx)
+{
+    return &VA_DRIVER_CONTEXT_GLX(ctx)->gl_vtable;
+}
+
+// Lookup for a GLX function
+typedef void (*GLFuncPtr)(void);
+typedef GLFuncPtr (*GLXGetProcAddressProc)(const char *);
+
+static GLFuncPtr get_proc_address_default(const char *name)
+{
+    return NULL;
+}
+
+static GLXGetProcAddressProc get_proc_address_func(void)
+{
+    GLXGetProcAddressProc get_proc_func;
+
+    dlerror();
+    get_proc_func = (GLXGetProcAddressProc)
+        dlsym(RTLD_DEFAULT, "glXGetProcAddress");
+    if (dlerror() == NULL)
+        return get_proc_func;
+
+    get_proc_func = (GLXGetProcAddressProc)
+        dlsym(RTLD_DEFAULT, "glXGetProcAddressARB");
+    if (dlerror() == NULL)
+        return get_proc_func;
+
+    return get_proc_address_default;
+}
+
+static inline GLFuncPtr get_proc_address(const char *name)
+{
+    static GLXGetProcAddressProc get_proc_func = NULL;
+    if (get_proc_func == NULL)
+        get_proc_func = get_proc_address_func();
+    return get_proc_func(name);
+}
+
+// Check for GLX extensions (TFP, FBO)
+static int check_extension(const char *name, const char *ext)
+{
+    const char *end;
+    int name_len, n;
+
+    if (name == NULL || ext == NULL)
+        return 0;
+
+    end = ext + strlen(ext);
+    name_len = strlen(name);
+    while (ext < end) {
+        n = strcspn(ext, " ");
+        if (n == name_len && strncmp(name, ext, n) == 0)
+            return 1;
+        ext += (n + 1);
+    }
+    return 0;
+}
+
+static int check_tfp_extensions(VADriverContextP ctx)
+{
+    const char *gl_extensions;
+    const char *glx_extensions;
+
+    gl_extensions = (const char *)glGetString(GL_EXTENSIONS);
+    if (!check_extension("GL_ARB_texture_non_power_of_two", gl_extensions))
+        return 0;
+
+    glx_extensions = glXQueryExtensionsString(ctx->x11_dpy, ctx->x11_screen);
+    if (!check_extension("GLX_EXT_texture_from_pixmap", glx_extensions))
+        return 0;
+    return 1;
+}
+
+static int check_fbo_extensions(VADriverContextP ctx)
+{
+    const char *gl_extensions;
+
+    gl_extensions = (const char *)glGetString(GL_EXTENSIONS);
+    if (!check_extension("GL_ARB_framebuffer_object", gl_extensions))
+        return 0;
+    if (!check_extension("GL_EXT_framebuffer_object", gl_extensions))
+        return 0;
+    return 1;
+}
+
+// Load GLX extensions
+static int load_tfp_extensions(VADriverContextP ctx)
+{
+    VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
+
+    pOpenGLVTable->glx_bind_tex_image = (PFNGLXBINDTEXIMAGEEXTPROC)
+        get_proc_address("glXBindTexImageEXT");
+    if (pOpenGLVTable->glx_bind_tex_image == NULL)
+        return 0;
+    pOpenGLVTable->glx_release_tex_image = (PFNGLXRELEASETEXIMAGEEXTPROC)
+        get_proc_address("glXReleaseTexImageEXT");
+    if (pOpenGLVTable->glx_release_tex_image == NULL)
+        return 0;
+    return 1;
+}
+
+static int load_fbo_extensions(VADriverContextP ctx)
+{
+    VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
+
+    pOpenGLVTable->gl_gen_framebuffers = (PFNGLGENFRAMEBUFFERSEXTPROC)
+        get_proc_address("glGenFramebuffersEXT");
+    if (pOpenGLVTable->gl_gen_framebuffers == NULL)
+        return 0;
+    pOpenGLVTable->gl_delete_framebuffers = (PFNGLDELETEFRAMEBUFFERSEXTPROC)
+        get_proc_address("glDeleteFramebuffersEXT");
+    if (pOpenGLVTable->gl_delete_framebuffers == NULL)
+        return 0;
+    pOpenGLVTable->gl_bind_framebuffer = (PFNGLBINDFRAMEBUFFEREXTPROC)
+        get_proc_address("glBindFramebufferEXT");
+    if (pOpenGLVTable->gl_bind_framebuffer == NULL)
+        return 0;
+    pOpenGLVTable->gl_gen_renderbuffers = (PFNGLGENRENDERBUFFERSEXTPROC)
+        get_proc_address("glGenRenderbuffersEXT");
+    if (pOpenGLVTable->gl_gen_renderbuffers == NULL)
+        return 0;
+    pOpenGLVTable->gl_delete_renderbuffers = (PFNGLDELETERENDERBUFFERSEXTPROC)
+        get_proc_address("glDeleteRenderbuffersEXT");
+    if (pOpenGLVTable->gl_delete_renderbuffers == NULL)
+        return 0;
+    pOpenGLVTable->gl_bind_renderbuffer = (PFNGLBINDRENDERBUFFEREXTPROC)
+        get_proc_address("glBindRenderbufferEXT");
+    if (pOpenGLVTable->gl_bind_renderbuffer == NULL)
+        return 0;
+    pOpenGLVTable->gl_renderbuffer_storage = (PFNGLRENDERBUFFERSTORAGEEXTPROC)
+        get_proc_address("glRenderbufferStorageEXT");
+    if (pOpenGLVTable->gl_renderbuffer_storage == NULL)
+        return 0;
+    pOpenGLVTable->gl_framebuffer_renderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC)
+        get_proc_address("glFramebufferRenderbufferEXT");
+    if (pOpenGLVTable->gl_framebuffer_renderbuffer == NULL)
+        return 0;
+    pOpenGLVTable->gl_framebuffer_texture_2d = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)
+        get_proc_address("glFramebufferTexture2DEXT");
+    if (pOpenGLVTable->gl_framebuffer_texture_2d == NULL)
+        return 0;
+    pOpenGLVTable->gl_check_framebuffer_status = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
+        get_proc_address("glCheckFramebufferStatusEXT");
+    if (pOpenGLVTable->gl_check_framebuffer_status == NULL)
+        return 0;
+    return 1;
+}
+
+
+/* ========================================================================= */
+/* === VA/GLX helpers                                                    === */
+/* ========================================================================= */
+
+// OpenGL texture state
+typedef struct OpenGLTextureState *OpenGLTextureStateP;
+
+struct OpenGLTextureState {
+    int    was_enabled;
+    int    was_bound;
+    GLenum target;
+    GLuint old_texture;
+};
+
+// Bind texture, preserve previous texture state
+static int bind_texture(OpenGLTextureStateP ts, GLenum target, GLuint texture)
+{
+    ts->target      = target;
+    ts->old_texture = 0;
+    ts->was_bound   = 0;
+    ts->was_enabled = glIsEnabled(target);
+    if (!ts->was_enabled)
+        glEnable(target);
+
+    GLenum texture_binding;
+    switch (target) {
+    case GL_TEXTURE_1D:
+        texture_binding = GL_TEXTURE_BINDING_1D;
+        break;
+    case GL_TEXTURE_2D:
+        texture_binding = GL_TEXTURE_BINDING_2D;
+        break;
+    case GL_TEXTURE_3D:
+        texture_binding = GL_TEXTURE_BINDING_3D;
+        break;
+    case GL_TEXTURE_RECTANGLE_ARB:
+        texture_binding = GL_TEXTURE_BINDING_RECTANGLE_ARB;
+        break;
+    default:
+        assert(!target);
+        return -1;
+    }
+
+    if (ts->was_enabled && gl_get_param(texture_binding, &ts->old_texture) < 0)
+        return -1;
+
+    ts->was_bound = texture == ts->old_texture;
+    if (!ts->was_bound) {
+        gl_purge_errors();
+        glBindTexture(target, texture);
+        if (gl_check_error())
+            return -1;
+    }
+    return 0;
+}
+
+// Unbind texture, restore previous texture state
+static void unbind_texture(OpenGLTextureStateP ts)
+{
+    if (!ts->was_bound && ts->old_texture)
+        glBindTexture(ts->target, ts->old_texture);
+    if (!ts->was_enabled)
+        glDisable(ts->target);
+}
+
+// Create Pixmaps for GLX texture-from-pixmap extension
+static int create_tfp_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
+{
+    const unsigned int     width      = pSurfaceGLX->width;
+    const unsigned int     height     = pSurfaceGLX->height;
+    Pixmap                 pixmap     = None;
+    GLXFBConfig           *fbconfig   = NULL;
+    GLXPixmap              glx_pixmap = None;
+    Window                 root_window;
+    XWindowAttributes      wattr;
+    int                   *attrib;
+    int                    n_fbconfig_attribs, x, y, status;
+    unsigned int           border_width, depth, dummy;
+
+    root_window = RootWindow(ctx->x11_dpy, ctx->x11_screen);
+    XGetWindowAttributes(ctx->x11_dpy, root_window, &wattr);
+    pixmap = XCreatePixmap(ctx->x11_dpy, root_window,
+                           width, height, wattr.depth);
+    if (!pixmap)
+        return -1;
+    pSurfaceGLX->pixmap = pixmap;
+
+    x11_trap_errors();
+    status = XGetGeometry(ctx->x11_dpy,
+                          (Drawable)pixmap,
+                          &root_window,
+                          &x,
+                          &y,
+                          &dummy,
+                          &dummy,
+                          &border_width,
+                          &depth);
+    if (x11_untrap_errors() != 0 || status == 0)
+        return -1;
+    if (depth != 24 && depth != 32)
+        return -1;
+
+    int fbconfig_attribs[32] = {
+        GLX_DRAWABLE_TYPE,      GLX_PIXMAP_BIT,
+        GLX_DOUBLEBUFFER,       GL_TRUE,
+        GLX_RENDER_TYPE,        GLX_RGBA_BIT,
+        GLX_X_RENDERABLE,       GL_TRUE,
+        GLX_Y_INVERTED_EXT,     GL_TRUE,
+        GLX_RED_SIZE,           8,
+        GLX_GREEN_SIZE,         8,
+        GLX_BLUE_SIZE,          8,
+        GL_NONE,
+    };
+    for (attrib = fbconfig_attribs; *attrib != GL_NONE; attrib += 2)
+        ;
+    *attrib++ = GLX_DEPTH_SIZE;                         *attrib++ = depth;
+    if (depth == 32) {
+        *attrib++ = GLX_ALPHA_SIZE;                     *attrib++ = 8;
+        *attrib++ = GLX_BIND_TO_TEXTURE_RGBA_EXT;       *attrib++ = GL_TRUE;
+    }
+    else {
+        *attrib++ = GLX_BIND_TO_TEXTURE_RGB_EXT;        *attrib++ = GL_TRUE;
+    }
+    *attrib++ = GL_NONE;
+
+    fbconfig = glXChooseFBConfig(ctx->x11_dpy, ctx->x11_screen, fbconfig_attribs, &n_fbconfig_attribs);
+    if (fbconfig == NULL)
+        return -1;
+
+    int pixmap_attribs[10] = {
+        GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
+        GLX_MIPMAP_TEXTURE_EXT, GL_FALSE,
+        GL_NONE,
+    };
+    for (attrib = pixmap_attribs; *attrib != GL_NONE; attrib += 2)
+        ;
+    *attrib++ = GLX_TEXTURE_FORMAT_EXT;
+    if (depth == 32)
+        *attrib++ = GLX_TEXTURE_FORMAT_RGBA_EXT;
+    else
+        *attrib++ = GLX_TEXTURE_FORMAT_RGB_EXT;
+    *attrib++ = GL_NONE;
+
+    x11_trap_errors();
+    glx_pixmap = glXCreatePixmap(ctx->x11_dpy,
+                                 fbconfig[0],
+                                 pixmap,
+                                 pixmap_attribs);
+    free(fbconfig);
+    if (x11_untrap_errors() != 0)
+        return -1;
+    pSurfaceGLX->glx_pixmap = glx_pixmap;
+    return 0;
+}
+
+// Destroy Pixmaps used for TFP
+static void destroy_tfp_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
+{
+    if (pSurfaceGLX->glx_pixmap) {
+        glXDestroyPixmap(ctx->x11_dpy, pSurfaceGLX->glx_pixmap);
+        pSurfaceGLX->glx_pixmap = None;
+    }
+
+    if (pSurfaceGLX->pixmap) {
+        XFreePixmap(ctx->x11_dpy, pSurfaceGLX->pixmap);
+        pSurfaceGLX->pixmap = None;
+    }
+}
+
+// Bind GLX Pixmap to texture
+static int bind_pixmap(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
+{
+    VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
+
+    if (pSurfaceGLX->is_bound)
+        return 0;
+
+    x11_trap_errors();
+    pOpenGLVTable->glx_bind_tex_image(ctx->x11_dpy, pSurfaceGLX->glx_pixmap,
+                                      GLX_FRONT_LEFT_EXT, NULL);
+    XSync(ctx->x11_dpy, False);
+    if (x11_untrap_errors() != 0) {
+        va_glx_error_message("failed to bind pixmap\n");
+        return -1;
+    }
+
+    pSurfaceGLX->is_bound = 1;
+    return 0;
+}
+
+// Release GLX Pixmap from texture
+static int unbind_pixmap(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
+{
+    VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
+
+    if (!pSurfaceGLX->is_bound)
+        return 0;
+
+    x11_trap_errors();
+    pOpenGLVTable->glx_release_tex_image(ctx->x11_dpy, pSurfaceGLX->glx_pixmap,
+                                         GLX_FRONT_LEFT_EXT);
+    XSync(ctx->x11_dpy, False);
+    if (x11_untrap_errors() != 0) {
+        va_glx_error_message("failed to release pixmap\n");
+        return -1;
+    }
+
+    pSurfaceGLX->is_bound = 0;
+    return 0;
+}
+
+// Render GLX Pixmap to texture
+static void render_pixmap(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
+{
+    const unsigned int w = pSurfaceGLX->width;
+    const unsigned int h = pSurfaceGLX->height;
+    float old_color[4];
+
+    gl_get_current_color(old_color);
+    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
+    glBegin(GL_QUADS);
+    {
+        glTexCoord2f(0.0f, 0.0f); glVertex2i(0, 0);
+        glTexCoord2f(0.0f, 1.0f); glVertex2i(0, h);
+        glTexCoord2f(1.0f, 1.0f); glVertex2i(w, h);
+        glTexCoord2f(1.0f, 0.0f); glVertex2i(w, 0);
+    }
+    glEnd();
+    glColor4fv(old_color);
+}
+
+// Create offscreen surface
+static int create_fbo_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
+{
+    VAOpenGLVTableP       pOpenGLVTable  = gl_get_vtable(ctx);
+    const GLenum          texture        = pSurfaceGLX->texture;
+    const unsigned int    texture_width  = pSurfaceGLX->width;
+    const unsigned int    texture_height = pSurfaceGLX->height;
+    GLuint fbo, fbo_buffer, fbo_texture;
+    GLenum status;
+
+    glGenTextures(1, &fbo_texture);
+    glBindTexture(GL_TEXTURE_2D, fbo_texture);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width, texture_height, 0,
+                 GL_BGRA, GL_UNSIGNED_BYTE, NULL);
+
+    pOpenGLVTable->gl_gen_framebuffers(1, &fbo);
+    pOpenGLVTable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, fbo);
+    pOpenGLVTable->gl_gen_renderbuffers(1, &fbo_buffer);
+    pOpenGLVTable->gl_bind_renderbuffer(GL_RENDERBUFFER_EXT, fbo_buffer);
+
+    glBindTexture(GL_TEXTURE_2D, texture);
+    pOpenGLVTable->gl_framebuffer_texture_2d(GL_FRAMEBUFFER_EXT,
+                                             GL_COLOR_ATTACHMENT0_EXT,
+                                             GL_TEXTURE_2D, texture, 0);
+
+    status = pOpenGLVTable->gl_check_framebuffer_status(GL_DRAW_FRAMEBUFFER_EXT);
+    pOpenGLVTable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, 0);
+    if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
+        return -1;
+
+    pSurfaceGLX->fbo         = fbo;
+    pSurfaceGLX->fbo_buffer  = fbo_buffer;
+    pSurfaceGLX->fbo_texture = fbo_texture;
+    return 0;
+}
+
+// Destroy offscreen surface
+static void destroy_fbo_surface(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
+{
+    VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
+
+    if (pSurfaceGLX->fbo_texture) {
+        glDeleteTextures(1, &pSurfaceGLX->fbo_texture);
+        pSurfaceGLX->fbo_texture = 0;
+    }
+
+    if (pSurfaceGLX->fbo_buffer) {
+        pOpenGLVTable->gl_delete_renderbuffers(1, &pSurfaceGLX->fbo_buffer);
+        pSurfaceGLX->fbo_buffer = 0;
+    }
+
+    if (pSurfaceGLX->fbo) {
+        pOpenGLVTable->gl_delete_framebuffers(1, &pSurfaceGLX->fbo);
+        pSurfaceGLX->fbo = 0;
+    }
+}
+
+// Setup matrices to match the FBO texture dimensions
+static void fbo_enter(VADriverContextP ctx, VASurfaceGLXP pSurfaceGLX)
+{
+    VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
+    const unsigned int width  = pSurfaceGLX->width;
+    const unsigned int height = pSurfaceGLX->height;
+
+    pOpenGLVTable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, pSurfaceGLX->fbo);
+    glPushAttrib(GL_VIEWPORT_BIT);
+    glMatrixMode(GL_PROJECTION);
+    glPushMatrix();
+    glLoadIdentity();
+    glMatrixMode(GL_MODELVIEW);
+    glPushMatrix();
+    glLoadIdentity();
+    glViewport(0, 0, width, height);
+    glTranslatef(-1.0f, -1.0f, 0.0f);
+    glScalef(2.0f / width, 2.0f / height, 1.0f);
+
+    glBindTexture(GL_TEXTURE_2D, pSurfaceGLX->fbo_texture);
+}
+
+// Restore original OpenGL matrices
+static void fbo_leave(VADriverContextP ctx)
+{
+    VAOpenGLVTableP pOpenGLVTable = gl_get_vtable(ctx);
+
+    glPopAttrib();
+    glMatrixMode(GL_PROJECTION);
+    glPopMatrix();
+    glMatrixMode(GL_MODELVIEW);
+    glPopMatrix();
+    pOpenGLVTable->gl_bind_framebuffer(GL_FRAMEBUFFER_EXT, 0);
+}
+
+// Create VA/GLX surface
+VASurfaceGLXP
+va_glx_create_surface(VADriverContextP ctx, GLenum target, GLuint texture)
+{
+    VADriverContextGLXP pDriverContextGLX = VA_DRIVER_CONTEXT_GLX(ctx);
+    VASurfaceGLXP pSurfaceGLX;
+    unsigned int internal_format, border_width, width, height;
+    int is_error = 1;
+
+    /* Make sure binding succeeds, if texture was not already bound */
+    struct OpenGLTextureState ts;
+    if (bind_texture(&ts, target, texture) < 0)
+        goto end;
+
+    pSurfaceGLX = malloc(sizeof(*pSurfaceGLX));
+    if (!pSurfaceGLX)
+        goto end;
+
+    pSurfaceGLX->magic          = VA_SURFACE_GLX_MAGIC;
+    pSurfaceGLX->target         = target;
+    pSurfaceGLX->texture        = texture;
+    pSurfaceGLX->surface        = VA_INVALID_SURFACE;
+    pSurfaceGLX->is_bound       = 0;
+    pSurfaceGLX->pixmap         = None;
+    pSurfaceGLX->glx_pixmap     = None;
+    pSurfaceGLX->fbo            = 0;
+    pSurfaceGLX->fbo_buffer     = 0;
+    pSurfaceGLX->fbo_texture    = 0;
+    pSurfaceGLX->priv           = NULL;
+
+    /* XXX: we don't support other textures than RGBA */
+    if (gl_get_texture_param(GL_TEXTURE_INTERNAL_FORMAT, &internal_format) < 0)
+        goto end;
+    if (internal_format != GL_RGBA)
+        goto end;
+
+    /* Check texture dimensions */
+    if (gl_get_texture_param(GL_TEXTURE_BORDER, &border_width) < 0)
+        goto end;
+    if (gl_get_texture_param(GL_TEXTURE_WIDTH, &width) < 0)
+        goto end;
+    if (gl_get_texture_param(GL_TEXTURE_HEIGHT, &height) < 0)
+        goto end;
+
+    width  -= 2 * border_width;
+    height -= 2 * border_width;
+    if (width == 0 || height == 0)
+        goto end;
+
+    pSurfaceGLX->width  = width;
+    pSurfaceGLX->height = height;
+
+    /* Create Pixmaps for TFP */
+    if (pDriverContextGLX->use_tfp) {
+        if (create_tfp_surface(ctx, pSurfaceGLX) < 0)
+            goto end;
+    }
+
+    /* Create Pixmaps for FBO */
+    if (pDriverContextGLX->use_fbo) {
+        if (create_fbo_surface(ctx, pSurfaceGLX) < 0)
+            goto end;
+    }
+
+    is_error = 0;
+end:
+    if (is_error && pSurfaceGLX)
+        va_glx_destroy_surface(ctx, &pSurfaceGLX);
+
+    unbind_texture(&ts);
+    return pSurfaceGLX;
+}
+
+// Destroy VA/GLX surface
+void va_glx_destroy_surface(VADriverContextP ctx, VASurfaceGLXP *ppSurfaceGLX)
+{
+    VASurfaceGLXP pSurfaceGLX = *ppSurfaceGLX;
+
+    unbind_pixmap(ctx, pSurfaceGLX);
+    destroy_fbo_surface(ctx, pSurfaceGLX);
+    destroy_tfp_surface(ctx, pSurfaceGLX);
+
+    free(pSurfaceGLX);
+    *ppSurfaceGLX = NULL;
+}
+
+
+/* ========================================================================= */
+/* === VA/GLX implementation from the driver (fordward calls)            === */
+/* ========================================================================= */
+
+#define INVOKE(ctx, func, args) do {                    \
+        VADriverVTableGLXP vtable = &(ctx)->vtable.glx; \
+        if (!vtable->va##func##GLX)                     \
+            return VA_STATUS_ERROR_UNIMPLEMENTED;       \
+                                                        \
+        VAStatus status = vtable->va##func##GLX args;   \
+        if (status != VA_STATUS_SUCCESS)                \
+            return status;                              \
+    } while (0)
+
+static VAStatus
+vaCreateSurfaceGLX_impl_driver(
+    VADriverContextP    ctx,
+    GLenum              target,
+    GLuint              texture,
+    void              **gl_surface
+)
+{
+    INVOKE(ctx, CreateSurface, (ctx, target, texture, gl_surface));
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaDestroySurfaceGLX_impl_driver(VADriverContextP ctx, void *gl_surface)
+{
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+
+    INVOKE(ctx, DestroySurface, (ctx, pSurfaceGLX->priv));
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaAssociateSurfaceGLX_impl_driver(
+    VADriverContextP    ctx,
+    void               *gl_surface,
+    VASurfaceID         surface,
+    unsigned int        flags
+)
+{
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+
+    INVOKE(ctx, AssociateSurface, (ctx, pSurfaceGLX->priv, surface, flags));
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaDeassociateSurfaceGLX_impl_driver(VADriverContextP ctx, void *gl_surface)
+{
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+
+    INVOKE(ctx, DeassociateSurface, (ctx, pSurfaceGLX->priv));
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaSyncSurfaceGLX_impl_driver(VADriverContextP ctx, void *gl_surface)
+{
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+
+    INVOKE(ctx, SyncSurface, (ctx, pSurfaceGLX->priv));
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaBeginRenderSurfaceGLX_impl_driver(VADriverContextP ctx, void *gl_surface)
+{
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+
+    INVOKE(ctx, BeginRenderSurface, (ctx, pSurfaceGLX->priv));
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaEndRenderSurfaceGLX_impl_driver(VADriverContextP ctx, void *gl_surface)
+{
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+
+    INVOKE(ctx, EndRenderSurface, (ctx, pSurfaceGLX->priv));
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaCopySurfaceGLX_impl_driver(
+    VADriverContextP    ctx,
+    void               *gl_surface,
+    VASurfaceID         surface,
+    unsigned int        flags
+)
+{
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+
+    INVOKE(ctx, CopySurface, (ctx, pSurfaceGLX->priv, surface, flags));
+    return VA_STATUS_SUCCESS;
+}
+
+#undef INVOKE
+
+
+/* ========================================================================= */
+/* === VA/GLX implementation from libVA (generic and suboptimal path)    === */
+/* ========================================================================= */
+
+static VAStatus
+vaCreateSurfaceGLX_impl_libva(
+    VADriverContextP    ctx,
+    GLenum              target,
+    GLuint              texture,
+    void              **gl_surface
+)
+{
+    VADriverContextGLXP pDriverContextGLX = VA_DRIVER_CONTEXT_GLX(ctx);
+
+    if (!pDriverContextGLX->use_tfp)
+        return VA_STATUS_ERROR_UNIMPLEMENTED;
+
+    *gl_surface = NULL;
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaDestroySurfaceGLX_impl_libva(VADriverContextP ctx, void *gl_surface)
+{
+    VADriverContextGLXP pDriverContextGLX = VA_DRIVER_CONTEXT_GLX(ctx);
+
+    if (!pDriverContextGLX->use_tfp)
+        return VA_STATUS_ERROR_UNIMPLEMENTED;
+
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaAssociateSurfaceGLX_impl_libva(
+    VADriverContextP    ctx,
+    void               *gl_surface,
+    VASurfaceID         surface,
+    unsigned int        flags
+)
+{
+    VADriverContextGLXP pDriverContextGLX = VA_DRIVER_CONTEXT_GLX(ctx);
+    VADriverVTableGLXP vtable = &pDriverContextGLX->vtable;
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+    VAStatus status;
+
+    if (!pDriverContextGLX->use_tfp)
+        return VA_STATUS_ERROR_UNIMPLEMENTED;
+
+    /* XXX: only support VA_FRAME_PICTURE */
+    if (flags != VA_FRAME_PICTURE)
+        return VA_STATUS_ERROR_FLAG_NOT_SUPPORTED;
+
+    /* XXX: optimise case where we are associating the same VA surface
+       as before an no changed occurred to it */
+    status = vtable->vaDeassociateSurfaceGLX(ctx, gl_surface);
+    if (status != VA_STATUS_SUCCESS)
+        return status;
+
+    x11_trap_errors();
+    status = ctx->vtable.vaPutSurface(ctx,
+                                      surface,
+                                      pSurfaceGLX->pixmap,
+                                      0, 0,
+                                      pSurfaceGLX->width,
+                                      pSurfaceGLX->height,
+                                      0, 0,
+                                      pSurfaceGLX->width,
+                                      pSurfaceGLX->height,
+                                      NULL, 0,
+                                      flags);
+    XSync(ctx->x11_dpy, False);
+    if (x11_untrap_errors() != 0)
+        return VA_STATUS_ERROR_OPERATION_FAILED;
+    if (status != VA_STATUS_SUCCESS)
+        return status;
+
+    pSurfaceGLX->surface = surface;
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaDeassociateSurfaceGLX_impl_libva(VADriverContextP ctx, void *gl_surface)
+{
+    VADriverContextGLXP pDriverContextGLX = VA_DRIVER_CONTEXT_GLX(ctx);
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+
+    if (!pDriverContextGLX->use_tfp)
+        return VA_STATUS_ERROR_UNIMPLEMENTED;
+
+    if (unbind_pixmap(ctx, pSurfaceGLX) < 0)
+        return VA_STATUS_ERROR_OPERATION_FAILED;
+
+    pSurfaceGLX->surface = VA_INVALID_SURFACE;
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaSyncSurfaceGLX_impl_libva(VADriverContextP ctx, void *gl_surface)
+{
+    VADriverContextGLXP pDriverContextGLX = VA_DRIVER_CONTEXT_GLX(ctx);
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+
+    if (!pDriverContextGLX->use_tfp)
+        return VA_STATUS_ERROR_UNIMPLEMENTED;
+
+    if (pSurfaceGLX->surface == VA_INVALID_SURFACE)
+        return VA_STATUS_ERROR_INVALID_SURFACE;
+
+    return ctx->vtable.vaSyncSurface(ctx, pSurfaceGLX->surface);
+}
+
+static VAStatus
+vaBeginRenderSurfaceGLX_impl_libva(VADriverContextP ctx, void *gl_surface)
+{
+    VADriverContextGLXP pDriverContextGLX = VA_DRIVER_CONTEXT_GLX(ctx);
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+    VAStatus status;
+
+    if (!pDriverContextGLX->use_tfp)
+        return VA_STATUS_ERROR_UNIMPLEMENTED;
+
+    status = vaSyncSurfaceGLX_impl_libva(ctx, gl_surface);
+    if (status != VA_STATUS_SUCCESS)
+        return status;
+
+    if (bind_pixmap(ctx, pSurfaceGLX) < 0)
+        return VA_STATUS_ERROR_OPERATION_FAILED;
+
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaEndRenderSurfaceGLX_impl_libva(VADriverContextP ctx, void *gl_surface)
+{
+    VADriverContextGLXP pDriverContextGLX = VA_DRIVER_CONTEXT_GLX(ctx);
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+    VAStatus status;
+
+    if (!pDriverContextGLX->use_tfp)
+        return VA_STATUS_ERROR_UNIMPLEMENTED;
+
+    if (unbind_pixmap(ctx, pSurfaceGLX) < 0)
+        return VA_STATUS_ERROR_OPERATION_FAILED;
+
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaCopySurfaceGLX_impl_libva(
+    VADriverContextP    ctx,
+    void               *gl_surface,
+    VASurfaceID         surface,
+    unsigned int        flags
+)
+{
+    VADriverContextGLXP pDriverContextGLX = VA_DRIVER_CONTEXT_GLX(ctx);
+    VADriverVTableGLXP vtable = &pDriverContextGLX->vtable;
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+    VAStatus status;
+
+    if (!pDriverContextGLX->use_fbo)
+        return VA_STATUS_ERROR_UNIMPLEMENTED;
+
+    /* XXX: only support VA_FRAME_PICTURE */
+    if (flags != VA_FRAME_PICTURE)
+        return VA_STATUS_ERROR_FLAG_NOT_SUPPORTED;
+
+    /* Associate VA surface */
+    status = vtable->vaAssociateSurfaceGLX(ctx, gl_surface, surface, flags);
+    if (status != VA_STATUS_SUCCESS)
+        return status;
+
+    /* Make sure binding succeeds, if texture was not already bound */
+    struct OpenGLTextureState ts;
+    if (bind_texture(&ts, pSurfaceGLX->target, pSurfaceGLX->texture) < 0)
+        return VA_STATUS_ERROR_OPERATION_FAILED;
+
+    /* Render to FBO */
+    fbo_enter(ctx, pSurfaceGLX);
+    status = vtable->vaBeginRenderSurfaceGLX(ctx, gl_surface);
+    if (status == VA_STATUS_SUCCESS) {
+        render_pixmap(ctx, pSurfaceGLX);
+        status = vtable->vaEndRenderSurfaceGLX(ctx, gl_surface);
+    }
+    fbo_leave(ctx);
+    unbind_texture(&ts);
+    if (status != VA_STATUS_SUCCESS)
+        return status;
+
+    return vtable->vaDeassociateSurfaceGLX(ctx, gl_surface);
+}
+
+
+/* ========================================================================= */
+/* === VA/GLX bind functions implementation with vaCopySurfaceGLX()      === */
+/* ========================================================================= */
+
+static VAStatus
+vaAssociateSurfaceGLX_impl_bind(
+    VADriverContextP    ctx,
+    void               *gl_surface,
+    VASurfaceID         surface,
+    unsigned int        flags
+)
+{
+    VADriverContextGLXP pDriverContextGLX = VA_DRIVER_CONTEXT_GLX(ctx);
+    VADriverVTableGLXP vtable = &pDriverContextGLX->vtable;
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+    VAStatus status;
+
+    if (!pDriverContextGLX->has_copy)
+        return VA_STATUS_ERROR_UNIMPLEMENTED;
+
+    status = vtable->vaCopySurfaceGLX(ctx, gl_surface, surface, flags);
+    if (status != VA_STATUS_SUCCESS)
+        return status;
+
+    pSurfaceGLX->surface = surface;
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaDeassociateSurfaceGLX_impl_bind(VADriverContextP ctx, void *gl_surface)
+{
+    VADriverContextGLXP pDriverContextGLX = VA_DRIVER_CONTEXT_GLX(ctx);
+    VASurfaceGLXP pSurfaceGLX = gl_surface;
+
+    if (!pDriverContextGLX->has_copy)
+        return VA_STATUS_ERROR_UNIMPLEMENTED;
+
+    pSurfaceGLX->surface = VA_INVALID_SURFACE;
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaBeginRenderSurfaceGLX_impl_bind(VADriverContextP ctx, void *gl_surface)
+{
+    /* Surface is already copied into the texture, in vaAssociateSurfaceGLX() */
+    return VA_STATUS_SUCCESS;
+}
+
+static VAStatus
+vaEndRenderSurfaceGLX_impl_bind(VADriverContextP ctx, void *gl_surface)
+{
+    return VA_STATUS_SUCCESS;
+}
+
+
+/* ========================================================================= */
+/* === Private VA/GLX vtable initialization                              === */
+/* ========================================================================= */
+
+// Initialize GLX driver context
+VAStatus va_glx_init_context(VADriverContextP ctx)
+{
+    VADriverContextGLXP glx_ctx = VA_DRIVER_CONTEXT_GLX(ctx);
+    VADriverVTableGLXP  vtable  = &glx_ctx->vtable;
+    int needs_tfp = 0, needs_fbo = 0;
+
+    if (glx_ctx->is_initialized)
+        return VA_STATUS_SUCCESS;
+
+    glx_ctx->has_copy = ctx->vtable.glx.vaCopySurfaceGLX != NULL;
+    glx_ctx->has_bind = (ctx->vtable.glx.vaAssociateSurfaceGLX   != NULL &&
+                         ctx->vtable.glx.vaBeginRenderSurfaceGLX != NULL &&
+                         ctx->vtable.glx.vaEndRenderSurfaceGLX   != NULL &&
+                         ctx->vtable.glx.vaDeassociateSurfaceGLX != NULL);
+
+    switch ((((unsigned int)glx_ctx->has_bind) << 1) | glx_ctx->has_copy) {
+    case 0:
+        /* Full implementation in libVA */
+        needs_tfp = 1;
+        needs_fbo = 1;
+        vtable->vaCreateSurfaceGLX      = vaCreateSurfaceGLX_impl_libva;
+        vtable->vaDestroySurfaceGLX     = vaDestroySurfaceGLX_impl_libva;
+        vtable->vaAssociateSurfaceGLX   = vaAssociateSurfaceGLX_impl_libva;
+        vtable->vaDeassociateSurfaceGLX = vaDeassociateSurfaceGLX_impl_libva;
+        vtable->vaBeginRenderSurfaceGLX = vaBeginRenderSurfaceGLX_impl_libva;
+        vtable->vaEndRenderSurfaceGLX   = vaEndRenderSurfaceGLX_impl_libva;
+        vtable->vaSyncSurfaceGLX        = vaSyncSurfaceGLX_impl_libva;
+        vtable->vaCopySurfaceGLX        = vaCopySurfaceGLX_impl_libva;
+        break;
+    case 1:
+        /* Add bind functions based on vaCopySurfaceGLX() */
+        /* XXX: override vaSyncSurfaceGLX()? */
+        vtable->vaCreateSurfaceGLX      = vaCreateSurfaceGLX_impl_driver;
+        vtable->vaDestroySurfaceGLX     = vaDestroySurfaceGLX_impl_driver;
+        vtable->vaAssociateSurfaceGLX   = vaAssociateSurfaceGLX_impl_bind;
+        vtable->vaDeassociateSurfaceGLX = vaDeassociateSurfaceGLX_impl_bind;
+        vtable->vaBeginRenderSurfaceGLX = vaBeginRenderSurfaceGLX_impl_bind;
+        vtable->vaEndRenderSurfaceGLX   = vaEndRenderSurfaceGLX_impl_bind;
+        vtable->vaSyncSurfaceGLX        = vaSyncSurfaceGLX_impl_driver;
+        vtable->vaCopySurfaceGLX        = vaCopySurfaceGLX_impl_driver;
+        break;
+    case 2:
+        /* Add copy function based on vaBeginRenderSurfaceGLX() et al. */
+        needs_fbo = 1;
+        vtable->vaCreateSurfaceGLX      = vaCreateSurfaceGLX_impl_driver;
+        vtable->vaDestroySurfaceGLX     = vaDestroySurfaceGLX_impl_driver;
+        vtable->vaAssociateSurfaceGLX   = vaAssociateSurfaceGLX_impl_driver;
+        vtable->vaDeassociateSurfaceGLX = vaDeassociateSurfaceGLX_impl_driver;
+        vtable->vaBeginRenderSurfaceGLX = vaBeginRenderSurfaceGLX_impl_driver;
+        vtable->vaEndRenderSurfaceGLX   = vaEndRenderSurfaceGLX_impl_driver;
+        vtable->vaSyncSurfaceGLX        = vaSyncSurfaceGLX_impl_driver;
+        vtable->vaCopySurfaceGLX        = vaCopySurfaceGLX_impl_libva;
+        break;
+    case 3:
+        /* Keep driver bind & copy functions */
+        vtable->vaCreateSurfaceGLX      = vaCreateSurfaceGLX_impl_driver;
+        vtable->vaDestroySurfaceGLX     = vaDestroySurfaceGLX_impl_driver;
+        vtable->vaAssociateSurfaceGLX   = vaAssociateSurfaceGLX_impl_driver;
+        vtable->vaDeassociateSurfaceGLX = vaDeassociateSurfaceGLX_impl_driver;
+        vtable->vaBeginRenderSurfaceGLX = vaBeginRenderSurfaceGLX_impl_driver;
+        vtable->vaEndRenderSurfaceGLX   = vaEndRenderSurfaceGLX_impl_driver;
+        vtable->vaSyncSurfaceGLX        = vaSyncSurfaceGLX_impl_driver;
+        vtable->vaCopySurfaceGLX        = vaCopySurfaceGLX_impl_driver;
+        break;
+    default:
+        /* Fatal error: this cannot happen */
+        assert(0);
+        return VA_STATUS_ERROR_UNKNOWN;
+    }
+
+    glx_ctx->has_tfp = 0;
+    glx_ctx->use_tfp = 0;
+    glx_ctx->has_fbo = 0;
+    glx_ctx->use_fbo = 0;
+
+    if (needs_tfp) {
+        glx_ctx->has_tfp = check_tfp_extensions(ctx);
+        if (!glx_ctx->has_tfp || !load_tfp_extensions(ctx))
+            return VA_STATUS_ERROR_UNIMPLEMENTED;
+        glx_ctx->use_tfp = 1;
+    }
+
+    if (needs_fbo) {
+        glx_ctx->has_fbo = check_fbo_extensions(ctx);
+        if (!glx_ctx->has_fbo || !load_fbo_extensions(ctx))
+            return VA_STATUS_ERROR_UNIMPLEMENTED;
+        glx_ctx->use_fbo = 1;
+    }
+
+    glx_ctx->is_initialized = 1;
+    return VA_STATUS_SUCCESS;
+}
diff --git a/src/glx/va_glx_impl.h b/src/glx/va_glx_impl.h
new file mode 100644
index 0000000..977bfcc
--- /dev/null
+++ b/src/glx/va_glx_impl.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2009 Splitted-Desktop Systems. All Rights Reserved.
+ *
+ * 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, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.
+ */
+
+#ifndef VA_GLX_IMPL_H
+#define VA_GLX_IMPL_H
+
+/**
+ * Initialize GLX driver context
+ *
+ * @param[in]  ctx        the VA driver context
+ * @return VA_STATUS_SUCCESS if successful
+ */
+VAStatus va_glx_init_context(VADriverContextP ctx)
+    ATTRIBUTE_HIDDEN;
+
+/** Create VA/GLX surface */
+VASurfaceGLXP
+va_glx_create_surface(VADriverContextP ctx, GLenum target, GLuint texture)
+    ATTRIBUTE_HIDDEN;
+
+/** Destroy VA/GLX surface */
+void va_glx_destroy_surface(VADriverContextP ctx, VASurfaceGLXP *pSurfaceGLX)
+    ATTRIBUTE_HIDDEN;
+
+#endif /* VA_GLX_IMPL_H */
diff --git a/src/glx/va_glx_private.h b/src/glx/va_glx_private.h
new file mode 100644
index 0000000..8658ad3
--- /dev/null
+++ b/src/glx/va_glx_private.h
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2009 Splitted-Desktop Systems. All Rights Reserved.
+ *
+ * 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, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.
+ */
+
+#ifndef VA_GLX_PRIVATE_H
+#define VA_GLX_PRIVATE_H
+
+#include "config.h"
+#include "va.h"
+#include "va_backend.h"
+#include "va_x11.h"
+#include "va_glx.h"
+
+#if GLX_GLXEXT_VERSION < 18
+typedef void (*PFNGLXBINDTEXIMAGEEXTPROC)(Display *, GLXDrawable, int, const int *);
+typedef void (*PFNGLXRELEASETEXIMAGEEXTPROC)(Display *, GLXDrawable, int);
+#endif
+
+typedef struct VAOpenGLVTable *VAOpenGLVTableP;
+
+struct VAOpenGLVTable {
+    PFNGLXBINDTEXIMAGEEXTPROC           glx_bind_tex_image;
+    PFNGLXRELEASETEXIMAGEEXTPROC        glx_release_tex_image;
+    PFNGLGENFRAMEBUFFERSEXTPROC         gl_gen_framebuffers;
+    PFNGLDELETEFRAMEBUFFERSEXTPROC      gl_delete_framebuffers;
+    PFNGLBINDFRAMEBUFFEREXTPROC         gl_bind_framebuffer;
+    PFNGLGENRENDERBUFFERSEXTPROC        gl_gen_renderbuffers;
+    PFNGLDELETERENDERBUFFERSEXTPROC     gl_delete_renderbuffers;
+    PFNGLBINDRENDERBUFFEREXTPROC        gl_bind_renderbuffer;
+    PFNGLRENDERBUFFERSTORAGEEXTPROC     gl_renderbuffer_storage;
+    PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC gl_framebuffer_renderbuffer;
+    PFNGLFRAMEBUFFERTEXTURE2DEXTPROC    gl_framebuffer_texture_2d;
+    PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC  gl_check_framebuffer_status;
+};
+
+typedef struct VADisplayContextGLX *VADisplayContextGLXP;
+typedef struct VADriverContextGLX  *VADriverContextGLXP;
+typedef struct VASurfaceGLX        *VASurfaceGLXP;
+typedef struct VADriverVTableGLX   *VADriverVTableGLXP;
+
+struct VADisplayContextGLX {
+    void (*vaDestroy)(VADisplayContextP ctx);
+};
+
+#define VA_DRIVER_CONTEXT_GLX(ctx) ((VADriverContextGLXP)((ctx)->glx))
+
+struct VADriverContextGLX {
+    struct VADriverVTableGLX    vtable;
+    struct VAOpenGLVTable       gl_vtable;
+    unsigned int                is_initialized  : 1;
+    unsigned int                has_copy        : 1;
+    unsigned int                has_bind        : 1;
+    unsigned int                has_tfp         : 1;
+    unsigned int                has_fbo         : 1;
+    unsigned int                use_tfp         : 1;
+    unsigned int                use_fbo         : 1;
+};
+
+/** Unique VASurfaceGLX identifier */
+#define VA_SURFACE_GLX_MAGIC VA_FOURCC('V','A','G','L')
+
+struct VASurfaceGLX {
+    uint32_t     magic;         ///< Magic number identifying a VASurfaceGLX
+    GLenum       target;        ///< GL target to which the texture is bound
+    GLuint       texture;       ///< GL texture
+    VASurfaceID  surface;       ///< Associated VA surface
+    unsigned int width;
+    unsigned int height;
+    int          is_bound;
+    Pixmap       pixmap;
+    GLXPixmap    glx_pixmap;
+    GLuint       fbo;
+    GLuint       fbo_buffer;
+    GLuint       fbo_texture;
+    void        *priv;          ///< Private VA/GLX surface data from driver
+};
+
+#endif /* VA_GLX_PRIVATE_H */