summaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/javax/media/opengl/awt/GLJPanel.java
blob: 223badfb699fabb4a007ffa62d5c40be933c7318 (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
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
/*
 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
 * Copyright (c) 2010 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed or intended for use
 * in the design, construction, operation or maintenance of any nuclear
 * facility.
 *
 * Sun gratefully acknowledges that this software was originally authored
 * and developed by Kenneth Bradley Russell and Christopher John Kline.
 */

package javax.media.opengl.awt;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.beans.Beans;
import java.nio.IntBuffer;
import java.util.List;

import javax.media.nativewindow.AbstractGraphicsDevice;
import javax.media.nativewindow.NativeSurface;
import javax.media.nativewindow.ScalableSurface;
import javax.media.nativewindow.SurfaceUpdatedListener;
import javax.media.nativewindow.WindowClosingProtocol;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL2ES3;
import javax.media.opengl.GL2GL3;
import javax.media.opengl.GLAnimatorControl;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLCapabilitiesChooser;
import javax.media.opengl.GLCapabilitiesImmutable;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLDrawable;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLException;
import javax.media.opengl.GLFBODrawable;
import javax.media.opengl.GLProfile;
import javax.media.opengl.GLRunnable;
import javax.media.opengl.GLSharedContextSetter;
import javax.media.opengl.Threading;
import javax.swing.JPanel;

import jogamp.nativewindow.SurfaceScaleUtils;
import jogamp.nativewindow.WrappedSurface;
import jogamp.nativewindow.jawt.JAWTUtil;
import jogamp.opengl.Debug;
import jogamp.opengl.GLContextImpl;
import jogamp.opengl.GLDrawableFactoryImpl;
import jogamp.opengl.GLDrawableHelper;
import jogamp.opengl.GLDrawableImpl;
import jogamp.opengl.awt.AWTTilePainter;
import jogamp.opengl.awt.Java2D;
import jogamp.opengl.util.glsl.GLSLTextureRaster;

import com.jogamp.common.util.awt.AWTEDTExecutor;
import com.jogamp.nativewindow.awt.AWTPrintLifecycle;
import com.jogamp.nativewindow.awt.AWTWindowClosingProtocol;
import com.jogamp.opengl.FBObject;
import com.jogamp.opengl.GLRendererQuirks;
import com.jogamp.opengl.util.GLPixelBuffer.GLPixelAttributes;
import com.jogamp.opengl.util.GLPixelBuffer.SingletonGLPixelBufferProvider;
import com.jogamp.opengl.util.GLDrawableUtil;
import com.jogamp.opengl.util.GLPixelStorageModes;
import com.jogamp.opengl.util.TileRenderer;
import com.jogamp.opengl.util.awt.AWTGLPixelBuffer;
import com.jogamp.opengl.util.awt.AWTGLPixelBuffer.AWTGLPixelBufferProvider;
import com.jogamp.opengl.util.awt.AWTGLPixelBuffer.SingleAWTGLPixelBufferProvider;
import com.jogamp.opengl.util.texture.TextureState;

/** A lightweight Swing component which provides OpenGL rendering
    support. Provided for compatibility with Swing user interfaces
    when adding a heavyweight doesn't work either because of
    Z-ordering or LayoutManager problems.
    <p>
    The GLJPanel can be made transparent by creating it with a
    GLCapabilities object with alpha bits specified and calling {@link
    #setOpaque}(false). Pixels with resulting OpenGL alpha values less
    than 1.0 will be overlaid on any underlying Swing rendering.
    </p>
    <p>
    This component attempts to use hardware-accelerated rendering via FBO or pbuffers and
    falls back on to software rendering if none of the former are available
    using {@link GLDrawableFactory#createOffscreenDrawable(AbstractGraphicsDevice, GLCapabilitiesImmutable, GLCapabilitiesChooser, int, int) GLDrawableFactory.createOffscreenDrawable(..)}.<br/>
    </p>
    <p>
    <a name="verticalFlip">
    In case</a> the drawable {@link #isGLOriented()} and {@link #setSkipGLOrientationVerticalFlip(boolean) vertical flip is not skipped},
    this component performs the required vertical flip to bring the content from OpenGL's orientation into AWT's orientation.
    See details about <a href="#fboGLSLVerticalFlip">FBO and GLSL vertical flipping</a>.
    </p>
    <p>
    The OpenGL path is concluded by copying the rendered pixels an {@link BufferedImage} via {@link GL#glReadPixels(int, int, int, int, int, int, java.nio.Buffer) glReadPixels(..)}
    for later Java2D composition.
    </p>
    <p>
    In case {@link #setSkipGLOrientationVerticalFlip(boolean) vertical-flip is not skipped} and <a href="#fboGLSLVerticalFlip">GLSL based vertical-flip</a> is not performed,
    {@link System#arraycopy(Object, int, Object, int, int) System.arraycopy(..)} is used line by line.
    This step causes more CPU load per frame and is not hardware-accelerated.
    </p>
    <p>
    Finally the Java2D compositioning takes place via via {@link Graphics#drawImage(java.awt.Image, int, int, int, int, java.awt.image.ImageObserver) Graphics.drawImage(...)}
    on the prepared {@link BufferedImage} as described above.
    </p>
    <P>
 *  Please read <a href="GLCanvas.html#java2dgl">Java2D OpenGL Remarks</a>.
 *  </P>
 *
    <a name="fboGLSLVerticalFlip"><h5>FBO / GLSL Vertical Flip</h5></a>
    In case FBO is used and GLSL is available and {@link #setSkipGLOrientationVerticalFlip(boolean) vertical flip is not skipped}, a fragment shader is utilized
    to flip the FBO texture vertically. This hardware-accelerated step can be disabled via system property <code>jogl.gljpanel.noglsl</code>.
    <p>
    The FBO / GLSL code path uses one texture-unit and binds the FBO texture to it's active texture-target,
    see {@link #setTextureUnit(int)} and {@link #getTextureUnit()}.
    </p>
    <p>
    The active and dedicated texture-unit's {@link GL#GL_TEXTURE_2D} state is preserved via {@link TextureState}.
    See also {@link Texture#textureCallOrder Order of Texture Commands}.
    </p>
    <p>
    The current gl-viewport is preserved.
    </p>
    <p>
    <i>Warning (Bug 842)</i>: Certain GL states other than viewport and texture (see above)
    influencing rendering, will also influence the GLSL vertical flip, e.g. {@link GL#glFrontFace(int) glFrontFace}({@link GL#GL_CCW}).
    It is recommended to reset those states to default when leaving the {@link GLEventListener#display(GLAutoDrawable)} method!
    We may change this behavior in the future, i.e. preserve all influencing states.
    </p>
*/

@SuppressWarnings("serial")
public class GLJPanel extends JPanel implements AWTGLAutoDrawable, WindowClosingProtocol, AWTPrintLifecycle, GLSharedContextSetter, ScalableSurface {
  private static final boolean DEBUG;
  private static final boolean DEBUG_VIEWPORT;
  private static final boolean USE_GLSL_TEXTURE_RASTERIZER;
  private static final boolean SKIP_VERTICAL_FLIP_DEFAULT;

  /** Indicates whether the Java 2D OpenGL pipeline is requested by user. */
  private static final boolean java2dOGLEnabledByProp;

  /** Indicates whether the Java 2D OpenGL pipeline is enabled, resource-compatible and requested by user. */
  private static final boolean useJava2DGLPipeline;

  /** Indicates whether the Java 2D OpenGL pipeline's usage is error free. */
  private static boolean java2DGLPipelineOK;

  static {
      Debug.initSingleton();
      DEBUG = Debug.debug("GLJPanel");
      DEBUG_VIEWPORT = Debug.isPropertyDefined("jogl.debug.GLJPanel.Viewport", true);
      USE_GLSL_TEXTURE_RASTERIZER = !Debug.isPropertyDefined("jogl.gljpanel.noglsl", true);
      SKIP_VERTICAL_FLIP_DEFAULT = Debug.isPropertyDefined("jogl.gljpanel.noverticalflip", true);
      boolean enabled = Debug.getBooleanProperty("sun.java2d.opengl", false);
      java2dOGLEnabledByProp = enabled && !Debug.isPropertyDefined("jogl.gljpanel.noogl", true);

      enabled = false;
      if( java2dOGLEnabledByProp ) {
          // Force eager initialization of part of the Java2D class since
          // otherwise it's likely it will try to be initialized while on
          // the Queue Flusher Thread, which is not allowed
          if (Java2D.isOGLPipelineResourceCompatible() && Java2D.isFBOEnabled()) {
              if( null != Java2D.getShareContext(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()) ) {
                  enabled = true;
              }
          }
      }
      useJava2DGLPipeline = enabled;
      java2DGLPipelineOK = enabled;
      if( DEBUG ) {
          System.err.println("GLJPanel: DEBUG_VIEWPORT "+DEBUG_VIEWPORT);
          System.err.println("GLJPanel: USE_GLSL_TEXTURE_RASTERIZER "+USE_GLSL_TEXTURE_RASTERIZER);
          System.err.println("GLJPanel: SKIP_VERTICAL_FLIP_DEFAULT "+SKIP_VERTICAL_FLIP_DEFAULT);
          System.err.println("GLJPanel: java2dOGLEnabledByProp "+java2dOGLEnabledByProp);
          System.err.println("GLJPanel: useJava2DGLPipeline "+useJava2DGLPipeline);
          System.err.println("GLJPanel: java2DGLPipelineOK "+java2DGLPipelineOK);
      }
  }

  private static SingleAWTGLPixelBufferProvider singleAWTGLPixelBufferProvider = null;
  private static synchronized SingleAWTGLPixelBufferProvider getSingleAWTGLPixelBufferProvider() {
      if( null == singleAWTGLPixelBufferProvider ) {
          singleAWTGLPixelBufferProvider = new SingleAWTGLPixelBufferProvider( true /* allowRowStride */ );
      }
      return singleAWTGLPixelBufferProvider;
  }

  private final GLDrawableHelper helper;
  private boolean autoSwapBufferMode;

  private volatile boolean isInitialized;

  //
  // Data used for either pbuffers or pixmap-based offscreen surfaces
  //
  private AWTGLPixelBufferProvider customPixelBufferProvider = null;
  /** Single buffered offscreen caps */
  private GLCapabilitiesImmutable offscreenCaps;
  private final GLProfile             glProfile;
  private final GLDrawableFactoryImpl factory;
  private final GLCapabilitiesChooser chooser;
  private int additionalCtxCreationFlags = 0;

  // Lazy reshape notification: reshapeWidth -> panelWidth -> backend.width
  private boolean handleReshape = false;
  private boolean sendReshape = true;

  private volatile int[] hasPixelScale = new int[] { ScalableSurface.IDENTITY_PIXELSCALE, ScalableSurface.IDENTITY_PIXELSCALE };
  private volatile int[] reqPixelScale = new int[] { ScalableSurface.AUTOMAX_PIXELSCALE, ScalableSurface.AUTOMAX_PIXELSCALE };

  // For handling reshape events lazily: reshapeWidth -> panelWidth -> backend.width
  private int reshapeWidth;
  private int reshapeHeight;

  // Width of the actual GLJPanel: reshapeWidth -> panelWidth -> backend.width
  private int panelWidth   = 0;
  private int panelHeight  = 0;

  // These are always set to (0, 0) except when the Java2D / OpenGL
  // pipeline is active
  private int viewportX;
  private int viewportY;

  private int requestedTextureUnit = 0; // default

  // The backend in use
  private volatile Backend backend;

  private boolean skipGLOrientationVerticalFlip = SKIP_VERTICAL_FLIP_DEFAULT;

  // Used by all backends either directly or indirectly to hook up callbacks
  private final Updater updater = new Updater();

  private boolean oglPipelineUsable() {
      return null == customPixelBufferProvider &&  useJava2DGLPipeline && java2DGLPipelineOK;
  }

  private volatile boolean isShowing;
  private final HierarchyListener hierarchyListener = new HierarchyListener() {
      @Override
      public void hierarchyChanged(HierarchyEvent e) {
          isShowing = GLJPanel.this.isShowing();
      }
  };

  private final AWTWindowClosingProtocol awtWindowClosingProtocol =
          new AWTWindowClosingProtocol(this, new Runnable() {
                @Override
                public void run() {
                    GLJPanel.this.destroy();
                }
            }, null);

  /** Creates a new GLJPanel component with a default set of OpenGL
      capabilities and using the default OpenGL capabilities selection
      mechanism.
   * @throws GLException if no default profile is available for the default desktop device.
   */
  public GLJPanel() throws GLException {
    this(null);
  }

  /** Creates a new GLJPanel component with the requested set of
      OpenGL capabilities, using the default OpenGL capabilities
      selection mechanism.
   * @throws GLException if no GLCapabilities are given and no default profile is available for the default desktop device.
   */
  public GLJPanel(GLCapabilitiesImmutable userCapsRequest) throws GLException {
    this(userCapsRequest, null, null);
  }

  /** Creates a new GLJPanel component. The passed GLCapabilities
      specifies the OpenGL capabilities for the component; if null, a
      default set of capabilities is used. The GLCapabilitiesChooser
      specifies the algorithm for selecting one of the available
      GLCapabilities for the component; a DefaultGLCapabilitesChooser
      is used if null is passed for this argument.
    * @throws GLException if no GLCapabilities are given and no default profile is available for the default desktop device.
  */
  public GLJPanel(GLCapabilitiesImmutable userCapsRequest, GLCapabilitiesChooser chooser)
          throws GLException
  {
      this(userCapsRequest, chooser, null);
  }

  /** Creates a new GLJPanel component. The passed GLCapabilities
      specifies the OpenGL capabilities for the component; if null, a
      default set of capabilities is used. The GLCapabilitiesChooser
      specifies the algorithm for selecting one of the available
      GLCapabilities for the component; a DefaultGLCapabilitesChooser
      is used if null is passed for this argument. The passed
      GLContext specifies an OpenGL context with which to share
      textures, display lists and other OpenGL state, and may be null
      if sharing is not desired. See the note in the overview documentation on
      <a href="../../../spec-overview.html#SHARING">context sharing</a>.
      <P>
      Note: Sharing cannot be enabled using J2D OpenGL FBO sharing,
      since J2D GL Context must be shared and we can only share one context.
    * @throws GLException if no GLCapabilities are given and no default profile is available for the default desktop device.
    * @deprecated Use {@link #GLJPanel(GLCapabilitiesImmutable, GLCapabilitiesChooser)}
    *             and set shared GLContext via {@link #setSharedContext(GLContext)} or {@link #setSharedAutoDrawable(GLAutoDrawable)}.
    */
  public GLJPanel(GLCapabilitiesImmutable userCapsRequest, GLCapabilitiesChooser chooser, GLContext shareWith)
          throws GLException
  {
    super();

    // Works around problems on many vendors' cards; we don't need a
    // back buffer for the offscreen surface anyway
    {
        GLCapabilities caps;
        if (userCapsRequest != null) {
            caps = (GLCapabilities) userCapsRequest.cloneMutable();
        } else {
            caps = new GLCapabilities(GLProfile.getDefault(GLProfile.getDefaultDevice()));
        }
        caps.setDoubleBuffered(false);
        offscreenCaps = caps;
    }
    this.glProfile = offscreenCaps.getGLProfile();
    this.factory = GLDrawableFactoryImpl.getFactoryImpl(glProfile);
    this.chooser = chooser;

    helper = new GLDrawableHelper();
    if( null != shareWith ) {
        helper.setSharedContext(null, shareWith);
    }
    autoSwapBufferMode = helper.getAutoSwapBufferMode();

    this.setFocusable(true); // allow keyboard input!
    this.addHierarchyListener(hierarchyListener);
    this.isShowing = isShowing();
  }

  /**
   * Attempts to initialize the backend, if not initialized yet.
   * <p>
   * If backend is already initialized method returns <code>true</code>.
   * </p>
   * <p>
   * If <code>offthread</code> is <code>true</code>, initialization will kicked off
   * on a <i>short lived</i> arbitrary thread and method returns immediately.<br/>
   * If platform supports such <i>arbitrary thread</i> initialization method returns
   * <code>true</code>, otherwise <code>false</code>.
   * </p>
   * <p>
   * If <code>offthread</code> is <code>false</code>, initialization be performed
   * on the current thread and method returns after initialization.<br/>
   * Method returns <code>true</code> if initialization was successful, otherwise <code>false</code>.
   * <p>
   * @param offthread
   */
  public final boolean initializeBackend(boolean offthread) {
    if( offthread ) {
        new Thread(getThreadName()+"-GLJPanel_Init") {
            public void run() {
              if( !isInitialized ) {
                  initializeBackendImpl();
              }
            } }.start();
        return true;
    } else {
        if( !isInitialized ) {
            return initializeBackendImpl();
        } else {
            return true;
        }
    }
  }

  @Override
  public final void setSharedContext(GLContext sharedContext) throws IllegalStateException {
      helper.setSharedContext(this.getContext(), sharedContext);
  }

  @Override
  public final void setSharedAutoDrawable(GLAutoDrawable sharedAutoDrawable) throws IllegalStateException {
      helper.setSharedAutoDrawable(this, sharedAutoDrawable);
  }

  public AWTGLPixelBufferProvider getCustomPixelBufferProvider() { return customPixelBufferProvider; }

  /**
   * @param custom custom {@link AWTGLPixelBufferProvider}
   * @throws IllegalArgumentException if <code>custom</code> is <code>null</code>
   * @throws IllegalStateException if backend is already realized, i.e. this instanced already painted once.
   */
  public void setPixelBufferProvider(AWTGLPixelBufferProvider custom) throws IllegalArgumentException, IllegalStateException {
      if( null == custom ) {
          throw new IllegalArgumentException("Null PixelBufferProvider");
      }
      if( null != backend ) {
          throw new IllegalStateException("Backend already realized.");
      }
      customPixelBufferProvider = custom;
  }

  @Override
  public final Object getUpstreamWidget() {
    return this;
  }

  @Override
  public void display() {
    if( isShowing || ( printActive && isVisible() ) ) {
        if (EventQueue.isDispatchThread()) {
          // Want display() to be synchronous, so call paintImmediately()
          paintImmediately(0, 0, getWidth(), getHeight());
        } else {
          // Multithreaded redrawing of Swing components is not allowed,
          // so do everything on the event dispatch thread
          try {
            EventQueue.invokeAndWait(paintImmediatelyAction);
          } catch (Exception e) {
            throw new GLException(e);
          }
        }
    }
  }

  protected void dispose() {
    if(DEBUG) {
        System.err.println(getThreadName()+": GLJPanel.dispose() - start");
        // Thread.dumpStack();
    }

    if (backend != null && backend.getContext() != null) {
      boolean animatorPaused = false;
      GLAnimatorControl animator =  getAnimator();
      if(null!=animator) {
        animatorPaused = animator.pause();
      }

      if(backend.getContext().isCreated()) {
          Threading.invoke(true, disposeAction, getTreeLock());
      }
      if(null != backend) {
          // not yet destroyed due to backend.isUsingOwnThreadManagment() == true
          backend.destroy();
          isInitialized = false;
      }

      if(animatorPaused) {
        animator.resume();
      }
    }
    hasPixelScale[0] = ScalableSurface.IDENTITY_PIXELSCALE;
    hasPixelScale[1] = ScalableSurface.IDENTITY_PIXELSCALE;

    if(DEBUG) {
        System.err.println(getThreadName()+": GLJPanel.dispose() - stop");
    }
  }

  /**
   * Just an alias for removeNotify
   */
  @Override
  public void destroy() {
      removeNotify();
  }

  /** Overridden to cause OpenGL rendering to be performed during
      repaint cycles. Subclasses which override this method must call
      super.paintComponent() in their paintComponent() method in order
      to function properly. <P>

      <DL><DD><CODE>paintComponent</CODE> in class <CODE>javax.swing.JComponent</CODE></DD></DL> */
  @Override
  protected void paintComponent(final Graphics g) {
    if (Beans.isDesignTime()) {
      // Make GLJPanel behave better in NetBeans GUI builder
      g.setColor(Color.BLACK);
      g.fillRect(0, 0, getWidth(), getHeight());
      FontMetrics fm = g.getFontMetrics();
      String name = getName();
      if (name == null) {
        name = getClass().getName();
        int idx = name.lastIndexOf('.');
        if (idx >= 0) {
          name = name.substring(idx + 1);
        }
      }
      Rectangle2D bounds = fm.getStringBounds(name, g);
      g.setColor(Color.WHITE);
      g.drawString(name,
                   (int) ((getWidth()  - bounds.getWidth())  / 2),
                   (int) ((getHeight() + bounds.getHeight()) / 2));
      return;
    }

    if( !isInitialized ) {
        initializeBackendImpl();
    }

    if (!isInitialized || printActive) {
      return;
    }

    // NOTE: must do this when the context is not current as it may
    // involve destroying the pbuffer (current context) and
    // re-creating it -- tricky to do properly while the context is
    // current
    if( !printActive ) {
        if (handleReshape) {
          handleReshape = false;
          sendReshape = handleReshape();
        }

        if( isShowing ) {
          updater.setGraphics(g);
          backend.doPaintComponent(g);
        }
    }
  }

  private final void updateWrappedSurfaceScale(final GLDrawable d) {
      final NativeSurface s = d.getNativeSurface();
      if( s instanceof WrappedSurface ) {
          ((WrappedSurface)s).setSurfaceScale(hasPixelScale);
      }
  }

  @Override
  public final void setSurfaceScale(final int[] pixelScale) { // HiDPI support
      SurfaceScaleUtils.validateReqPixelScale(reqPixelScale, pixelScale, DEBUG ? getClass().getSimpleName() : null);
      final Backend b = backend;
      if ( isInitialized && null != b ) {
          final int[] pixelScaleInt;
          {
              final int ps = JAWTUtil.getPixelScale(getGraphicsConfiguration());
              pixelScaleInt = new int[] { ps, ps };
          }
          final int hadPixelScaleX = hasPixelScale[0];
          final int hadPixelScaleY = hasPixelScale[1];
          SurfaceScaleUtils.computePixelScale(hasPixelScale, hasPixelScale, reqPixelScale, pixelScaleInt, DEBUG ? getClass().getSimpleName() : null);
          if( hadPixelScaleX != hasPixelScale[0] || hadPixelScaleY != hasPixelScale[1] ) {
              updateWrappedSurfaceScale(b.getDrawable());
              reshapeImpl(getWidth(), getHeight());
              display();
          }
      }
  }

  @Override
  public final int[] getRequestedSurfaceScale(final int[] result) {
      System.arraycopy(reqPixelScale, 0, result, 0, 2);
      return result;
  }

  @Override
  public final int[] getCurrentSurfaceScale(final int[] result) {
      System.arraycopy(hasPixelScale, 0, result, 0, 2);
      return result;
  }

  /** Overridden to track when this component is added to a container.
      Subclasses which override this method must call
      super.addNotify() in their addNotify() method in order to
      function properly. <P>

      <DL><DD><CODE>addNotify</CODE> in class <CODE>java.awt.Component</CODE></DD></DL> */
  @Override
  public void addNotify() {
    super.addNotify();
    awtWindowClosingProtocol.addClosingListener();

    // HiDPI support
    final int[] pixelScaleInt;
    {
        final int ps = JAWTUtil.getPixelScale(getGraphicsConfiguration());
        pixelScaleInt = new int[] { ps, ps };
    }
    SurfaceScaleUtils.computePixelScale(hasPixelScale, hasPixelScale, reqPixelScale, pixelScaleInt, DEBUG ? getClass().getSimpleName() : null);

    if (DEBUG) {
        System.err.println(getThreadName()+": GLJPanel.addNotify()");
    }
  }

  /** Overridden to track when this component is removed from a
      container. Subclasses which override this method must call
      super.removeNotify() in their removeNotify() method in order to
      function properly. <P>

      <DL><DD><CODE>removeNotify</CODE> in class <CODE>java.awt.Component</CODE></DD></DL> */
  @Override
  public void removeNotify() {
    awtWindowClosingProtocol.removeClosingListener();

    dispose();
    super.removeNotify();
  }

  /** Overridden to cause {@link GLDrawableHelper#reshape} to be
      called on all registered {@link GLEventListener}s. Subclasses
      which override this method must call super.reshape() in
      their reshape() method in order to function properly. <P>
   *
   * {@inheritDoc}
   */
  @SuppressWarnings("deprecation")
  @Override
  public void reshape(int x, int y, int width, int height) {
    super.reshape(x, y, width, height);
    reshapeImpl(width, height);
  }

  private void reshapeImpl(final int width, final int height) {
    final int scaledWidth = width * hasPixelScale[0];
    final int scaledHeight = height * hasPixelScale[1];
    if( DEBUG ) {
        System.err.println(getThreadName()+": GLJPanel.reshape.0 "+this.getName()+" resize"+(printActive?"WithinPrint":"")+
                " [ this "+getWidth()+"x"+getHeight()+", pixelScale "+getPixelScaleStr()+
                ", panel "+panelWidth+"x"+panelHeight +
                ", reshape: " +reshapeWidth+"x"+reshapeHeight +
                "] -> "+(printActive?"[skipped] ":"") + width+"x"+height+" * "+getPixelScaleStr()+" -> "+scaledWidth+"x"+scaledHeight);
    }
    if( !printActive ) {
        reshapeWidth = scaledWidth;
        reshapeHeight = scaledHeight;
        handleReshape = true;
    }
  }

  private volatile boolean printActive = false;
  private GLAnimatorControl printAnimator = null;
  private GLAutoDrawable printGLAD = null;
  private AWTTilePainter printAWTTiles = null;

  @Override
  public void setupPrint(double scaleMatX, double scaleMatY, int numSamples, int tileWidth, int tileHeight) {
      printActive = true;
      final int componentCount = isOpaque() ? 3 : 4;
      final TileRenderer printRenderer = new TileRenderer();
      printAWTTiles = new AWTTilePainter(printRenderer, componentCount, scaleMatX, scaleMatY, numSamples, tileWidth, tileHeight, DEBUG);
      AWTEDTExecutor.singleton.invoke(getTreeLock(), true /* allowOnNonEDT */, true /* wait */, setupPrintOnEDT);
  }
  private final Runnable setupPrintOnEDT = new Runnable() {
      @Override
      public void run() {
          if( !isInitialized ) {
              initializeBackendImpl();
          }
          if (!isInitialized) {
              if(DEBUG) {
                  System.err.println(getThreadName()+": Info: GLJPanel setupPrint - skipped GL render, drawable not valid yet");
              }
              printActive = false;
              return; // not yet available ..
          }
          if( !isVisible() ) {
              if(DEBUG) {
                  System.err.println(getThreadName()+": Info: GLJPanel setupPrint - skipped GL render, panel not visible");
              }
              printActive = false;
              return; // not yet available ..
          }
          sendReshape = false; // clear reshape flag
          handleReshape = false; // ditto
          printAnimator =  helper.getAnimator();
          if( null != printAnimator ) {
              printAnimator.remove(GLJPanel.this);
          }

          printGLAD = GLJPanel.this; // default: re-use
          final GLCapabilities caps = (GLCapabilities)getChosenGLCapabilities().cloneMutable();
          final int printNumSamples = printAWTTiles.getNumSamples(caps);
          GLDrawable printDrawable = printGLAD.getDelegatedDrawable();
          final boolean reqNewGLADSamples = printNumSamples != caps.getNumSamples();
          final boolean reqNewGLADSize = printAWTTiles.customTileWidth != -1 && printAWTTiles.customTileWidth != printDrawable.getSurfaceWidth() ||
                                         printAWTTiles.customTileHeight != -1 && printAWTTiles.customTileHeight != printDrawable.getSurfaceHeight();
          final boolean reqNewGLAD = reqNewGLADSamples || reqNewGLADSize ;
          if( DEBUG ) {
              System.err.println("AWT print.setup: reqNewGLAD "+reqNewGLAD+"[ samples "+reqNewGLADSamples+", size "+reqNewGLADSize+"], "+
                                 ", drawableSize "+printDrawable.getSurfaceWidth()+"x"+printDrawable.getSurfaceHeight()+
                                 ", customTileSize "+printAWTTiles.customTileWidth+"x"+printAWTTiles.customTileHeight+
                                 ", scaleMat "+printAWTTiles.scaleMatX+" x "+printAWTTiles.scaleMatY+
                                 ", numSamples "+printAWTTiles.customNumSamples+" -> "+printNumSamples+", printAnimator "+printAnimator);
          }
          if( reqNewGLAD ) {
              caps.setDoubleBuffered(false);
              caps.setOnscreen(false);
              caps.setSampleBuffers(0 < printNumSamples);
              caps.setNumSamples(printNumSamples);
              final GLDrawableFactory factory = GLDrawableFactory.getFactory(caps.getGLProfile());
              printGLAD = factory.createOffscreenAutoDrawable(null, caps, null,
                      printAWTTiles.customTileWidth != -1 ? printAWTTiles.customTileWidth : DEFAULT_PRINT_TILE_SIZE,
                      printAWTTiles.customTileHeight != -1 ? printAWTTiles.customTileHeight : DEFAULT_PRINT_TILE_SIZE);
              GLDrawableUtil.swapGLContextAndAllGLEventListener(GLJPanel.this, printGLAD);
              printDrawable = printGLAD.getDelegatedDrawable();
          }
          printAWTTiles.setGLOrientation( !GLJPanel.this.skipGLOrientationVerticalFlip && printGLAD.isGLOriented(), printGLAD.isGLOriented() );
          printAWTTiles.renderer.setTileSize(printDrawable.getSurfaceWidth(), printDrawable.getSurfaceHeight(), 0);
          printAWTTiles.renderer.attachAutoDrawable(printGLAD);
          if( DEBUG ) {
              System.err.println("AWT print.setup "+printAWTTiles);
              System.err.println("AWT print.setup AA "+printNumSamples+", "+caps);
              System.err.println("AWT print.setup printGLAD: "+printGLAD.getSurfaceWidth()+"x"+printGLAD.getSurfaceHeight()+", "+printGLAD);
              System.err.println("AWT print.setup printDraw: "+printDrawable.getSurfaceWidth()+"x"+printDrawable.getSurfaceHeight()+", "+printDrawable);
          }
      }
  };

  @Override
  public void releasePrint() {
      if( !printActive ) {
          throw new IllegalStateException("setupPrint() not called");
      }
      sendReshape = false; // clear reshape flag
      handleReshape = false; // ditto
      AWTEDTExecutor.singleton.invoke(getTreeLock(), true /* allowOnNonEDT */, true /* wait */, releasePrintOnEDT);
  }

  private final Runnable releasePrintOnEDT = new Runnable() {
      @Override
      public void run() {
          if( DEBUG ) {
              System.err.println(getThreadName()+": GLJPanel.releasePrintOnEDT.0 "+printAWTTiles);
          }
          printAWTTiles.dispose();
          printAWTTiles= null;
          if( printGLAD != GLJPanel.this ) {
              GLDrawableUtil.swapGLContextAndAllGLEventListener(printGLAD, GLJPanel.this);
              printGLAD.destroy();
          }
          printGLAD = null;
          if( null != printAnimator ) {
              printAnimator.add(GLJPanel.this);
              printAnimator = null;
          }

          // trigger reshape, i.e. gl-viewport and -listener - this component might got resized!
          final int awtWidth = GLJPanel.this.getWidth();
          final int awtHeight= GLJPanel.this.getHeight();
          final int scaledAWTWidth = awtWidth * hasPixelScale[0];
          final int scaledAWTHeight= awtHeight * hasPixelScale[1];
          final GLDrawable drawable = GLJPanel.this.getDelegatedDrawable();
          if( scaledAWTWidth != panelWidth || scaledAWTHeight != panelHeight ||
              drawable.getSurfaceWidth() != panelWidth || drawable.getSurfaceHeight() != panelHeight ) {
              // -> !( awtSize == panelSize == drawableSize )
              if ( DEBUG ) {
                  System.err.println(getThreadName()+": GLJPanel.releasePrintOnEDT.0: resizeWithinPrint panel " +panelWidth+"x"+panelHeight + " @ scale "+getPixelScaleStr()+
                          ", draw "+drawable.getSurfaceWidth()+"x"+drawable.getSurfaceHeight()+
                          " -> " + awtWidth+"x"+awtHeight+" * "+getPixelScaleStr()+" -> "+scaledAWTWidth+"x"+scaledAWTHeight);
              }
              reshapeWidth = scaledAWTWidth;
              reshapeHeight = scaledAWTHeight;
              sendReshape = handleReshape(); // reshapeSize -> panelSize, backend reshape w/ GL reshape
          } else {
              sendReshape = true; // only GL reshape
          }
          printActive = false;
          display();
      }
  };

  @Override
  public void print(Graphics graphics) {
      if( !printActive ) {
          throw new IllegalStateException("setupPrint() not called");
      }
      if(DEBUG && !EventQueue.isDispatchThread()) {
          System.err.println(getThreadName()+": Warning: GLCanvas print - not called from AWT-EDT");
          // we cannot dispatch print on AWT-EDT due to printing internal locking ..
      }
      sendReshape = false; // clear reshape flag
      handleReshape = false; // ditto

      final Graphics2D g2d = (Graphics2D)graphics;
      try {
          printAWTTiles.setupGraphics2DAndClipBounds(g2d, getWidth(), getHeight());
          final TileRenderer tileRenderer = printAWTTiles.renderer;
          if( DEBUG ) {
              System.err.println("AWT print.0: "+tileRenderer);
          }
          if( !tileRenderer.eot() ) {
              try {
                  do {
                      if( printGLAD != GLJPanel.this ) {
                          tileRenderer.display();
                      } else {
                          backend.doPlainPaint();
                      }
                  } while ( !tileRenderer.eot() );
                  if( DEBUG ) {
                      System.err.println("AWT print.1: "+printAWTTiles);
                  }
              } finally {
                  tileRenderer.reset();
                  printAWTTiles.resetGraphics2D();
              }
          }
      } catch (NoninvertibleTransformException nte) {
          System.err.println("Catched: Inversion failed of: "+g2d.getTransform());
          nte.printStackTrace();
      }
      if( DEBUG ) {
          System.err.println("AWT print.X: "+printAWTTiles);
      }
  }
  @Override
  protected void printComponent(Graphics g) {
      if( DEBUG ) {
          System.err.println("AWT printComponent.X: "+printAWTTiles);
      }
      print(g);
  }

  @Override
  public void setOpaque(boolean opaque) {
    if (backend != null) {
      backend.setOpaque(opaque);
    }
    super.setOpaque(opaque);
  }

  @Override
  public void addGLEventListener(GLEventListener listener) {
    helper.addGLEventListener(listener);
  }

  @Override
  public void addGLEventListener(int index, GLEventListener listener) {
    helper.addGLEventListener(index, listener);
  }

  @Override
  public int getGLEventListenerCount() {
      return helper.getGLEventListenerCount();
  }

  @Override
  public GLEventListener getGLEventListener(int index) throws IndexOutOfBoundsException {
      return helper.getGLEventListener(index);
  }

  @Override
  public boolean areAllGLEventListenerInitialized() {
     return helper.areAllGLEventListenerInitialized();
  }

  @Override
  public boolean getGLEventListenerInitState(GLEventListener listener) {
      return helper.getGLEventListenerInitState(listener);
  }

  @Override
  public void setGLEventListenerInitState(GLEventListener listener, boolean initialized) {
      helper.setGLEventListenerInitState(listener, initialized);
  }

  @Override
  public GLEventListener disposeGLEventListener(GLEventListener listener, boolean remove) {
    final DisposeGLEventListenerAction r = new DisposeGLEventListenerAction(listener, remove);
    if (EventQueue.isDispatchThread()) {
      r.run();
    } else {
      // Multithreaded redrawing of Swing components is not allowed,
      // so do everything on the event dispatch thread
      try {
        EventQueue.invokeAndWait(r);
      } catch (Exception e) {
        throw new GLException(e);
      }
    }
    return r.listener;
  }

  @Override
  public GLEventListener removeGLEventListener(GLEventListener listener) {
    return helper.removeGLEventListener(listener);
  }

  @Override
  public void setAnimator(GLAnimatorControl animatorControl) {
    helper.setAnimator(animatorControl);
  }

  @Override
  public GLAnimatorControl getAnimator() {
    return helper.getAnimator();
  }

  @Override
  public final Thread setExclusiveContextThread(Thread t) throws GLException {
      return helper.setExclusiveContextThread(t, getContext());
  }

  @Override
  public final Thread getExclusiveContextThread() {
      return helper.getExclusiveContextThread();
  }

  @Override
  public boolean invoke(boolean wait, GLRunnable glRunnable) {
    return helper.invoke(this, wait, glRunnable);
  }

  @Override
  public boolean invoke(final boolean wait, final List<GLRunnable> glRunnables) {
    return helper.invoke(this, wait, glRunnables);
  }

  @Override
  public GLContext createContext(GLContext shareWith) {
    final Backend b = backend;
    if ( null == b ) {
        return null;
    }
    return b.createContext(shareWith);
  }

  @Override
  public void setRealized(boolean realized) {
  }

  @Override
  public boolean isRealized() {
      return isInitialized;
  }

  @Override
  public GLContext setContext(GLContext newCtx, boolean destroyPrevCtx) {
    final Backend b = backend;
    if ( null == b ) {
        return null;
    }
    final GLContext oldCtx = b.getContext();
    GLDrawableHelper.switchContext(b.getDrawable(), oldCtx, destroyPrevCtx, newCtx, additionalCtxCreationFlags);
    b.setContext(newCtx);
    return oldCtx;
  }


  @Override
  public final GLDrawable getDelegatedDrawable() {
    final Backend b = backend;
    if ( null == b ) {
        return null;
    }
    return b.getDrawable();
  }

  @Override
  public GLContext getContext() {
    final Backend b = backend;
    if ( null == b ) {
        return null;
    }
    return b.getContext();
  }

  @Override
  public GL getGL() {
    if (Beans.isDesignTime()) {
      return null;
    }
    GLContext context = getContext();
    return (context == null) ? null : context.getGL();
  }

  @Override
  public GL setGL(GL gl) {
    GLContext context = getContext();
    if (context != null) {
      context.setGL(gl);
      return gl;
    }
    return null;
  }

  @Override
  public void setAutoSwapBufferMode(boolean enable) {
    this.autoSwapBufferMode = enable;
    boolean backendHandlesSwapBuffer = false;
    if( isInitialized ) {
        final Backend b = backend;
        if ( null != b ) {
            backendHandlesSwapBuffer= b.handlesSwapBuffer();
        }
    }
    if( !backendHandlesSwapBuffer ) {
        helper.setAutoSwapBufferMode(enable);
    }
  }

  @Override
  public boolean getAutoSwapBufferMode() {
    return autoSwapBufferMode;
  }

  @Override
  public void swapBuffers() {
    if( isInitialized ) {
        final Backend b = backend;
        if ( null != b ) {
            b.swapBuffers();
        }
    }
  }

  @Override
  public void setContextCreationFlags(int flags) {
    additionalCtxCreationFlags = flags;
  }

  @Override
  public int getContextCreationFlags() {
    return additionalCtxCreationFlags;
  }

  /** For a translucent GLJPanel (one for which {@link #setOpaque
      setOpaque}(false) has been called), indicates whether the
      application should preserve the OpenGL color buffer
      (GL_COLOR_BUFFER_BIT) for correct rendering of the GLJPanel and
      underlying widgets which may show through portions of the
      GLJPanel with alpha values less than 1.  Most Swing
      implementations currently expect the GLJPanel to be completely
      cleared (e.g., by <code>glClear(GL_COLOR_BUFFER_BIT |
      GL_DEPTH_BUFFER_BIT)</code>), but for certain optimized Swing
      implementations which use OpenGL internally, it may be possible
      to perform OpenGL rendering using the GLJPanel into the same
      OpenGL drawable as the Swing implementation uses. */
  public boolean shouldPreserveColorBufferIfTranslucent() {
    return oglPipelineUsable();
  }

  @Override
  public int getSurfaceWidth() {
      return panelWidth; // FIXME HiDPI: Accurate or: getWidth() * hasPixelScale[0];
  }

  @Override
  public int getSurfaceHeight() {
      return panelHeight; // FIXME HiDPI: Accurate or: getHeight() * hasPixelScale[1];
  }

  /**
   * {@inheritDoc}
   * <p>
   * Method returns a valid value only <i>after</i>
   * the backend has been initialized, either {@link #initializeBackend(boolean) eagerly}
   * or manually via the first display call.<br/>
   * Method always returns a valid value when called from within a {@link GLEventListener}.
   * </p>
   */
  @Override
  public boolean isGLOriented() {
    final Backend b = backend;
    if ( null == b ) {
        return true;
    }
    return b.getDrawable().isGLOriented();
  }

  /**
   * Set skipping {@link #isGLOriented()} based vertical flip,
   * which usually is required by the offscreen backend,
   * see details about <a href="#verticalFlip">vertical flip</a>
   * and <a href="#fboGLSLVerticalFlip">FBO / GLSL vertical flip</a>.
   * <p>
   * If set to <code>true</code>, user needs to flip the OpenGL rendered scene
   * <i>if {@link #isGLOriented()} == true</i>, e.g. via the PMV matrix.<br/>
   * See constraints of {@link #isGLOriented()}.
   * </p>
   */
  public final void setSkipGLOrientationVerticalFlip(boolean v) {
      skipGLOrientationVerticalFlip = v;
  }
  /** See {@link #setSkipGLOrientationVerticalFlip(boolean)}. */
  public final boolean getSkipGLOrientationVerticalFlip() {
      return skipGLOrientationVerticalFlip;
  }

  @Override
  public GLCapabilitiesImmutable getChosenGLCapabilities() {
    final Backend b = backend;
    if ( null == b ) {
        return null;
    }
    return b.getChosenGLCapabilities();
  }

  @Override
  public final GLProfile getGLProfile() {
    return glProfile;
  }

  @Override
  public NativeSurface getNativeSurface() {
    final Backend b = backend;
    if ( null == b ) {
        return null;
    }
    return b.getDrawable().getNativeSurface();
  }

  @Override
  public long getHandle() {
    final Backend b = backend;
    if ( null == b ) {
        return 0;
    }
    return b.getDrawable().getNativeSurface().getSurfaceHandle();
  }

  @Override
  public final GLDrawableFactory getFactory() {
    return factory;
  }

  /**
   * Returns the used texture unit, i.e. a value of [0..n], or -1 if non used.
   * <p>
   * If implementation uses a texture-unit, it will be known only after the first initialization, i.e. display call.
   * </p>
   * <p>
   * See <a href="#fboGLSLVerticalFlip">FBO / GLSL Vertical Flip</a>.
   * </p>
   */
  public final int getTextureUnit() {
    final Backend b = backend;
    if ( null == b ) {
        return -1;
    }
    return b.getTextureUnit();
  }

  /**
   * Allows user to request a texture unit to be used,
   * must be called before the first initialization, i.e. {@link #display()} call.
   * <p>
   * Defaults to <code>0</code>.
   * </p>
   * <p>
   * See <a href="#fboGLSLVerticalFlip">FBO / GLSL Vertical Flip</a>.
   * </p>
   *
   * @param v requested texture unit
   * @see #getTextureUnit()
   */
  public final void setTextureUnit(int v) {
      requestedTextureUnit = v;
  }

  //----------------------------------------------------------------------
  // Internals only below this point
  //

  private final Object initSync = new Object();
  private boolean initializeBackendImpl() {
    synchronized(initSync) {
        if( !isInitialized ) {
            if ( 0 >= panelWidth || 0 >= panelHeight ) {
              // See whether we have a non-zero size yet and can go ahead with
              // initialization
              if (0 >= reshapeWidth || 0 >= reshapeHeight ) {
                  return false;
              }

              if (DEBUG) {
                  System.err.println(getThreadName()+": GLJPanel.createAndInitializeBackend: " +
                          panelWidth+"x"+panelHeight+" @ scale "+getPixelScaleStr() + " -> " +
                          reshapeWidth+"x"+reshapeHeight+" @ scale "+getPixelScaleStr());
              }
              // Pull down reshapeWidth and reshapeHeight into panelWidth and
              // panelHeight eagerly in order to complete initialization, and
              // force a reshape later
              panelWidth = reshapeWidth;
              panelHeight = reshapeHeight;
            }

            if ( null == backend ) {
                if ( oglPipelineUsable() ) {
                    backend = new J2DOGLBackend();
                } else {
                    backend = new OffscreenBackend(glProfile, customPixelBufferProvider);
                }
                isInitialized = false;
            }

            if (!isInitialized) {
                backend.initialize();
            }
            return isInitialized;
        } else {
            return true;
        }
    }
  }

  private final String getPixelScaleStr() { return hasPixelScale[0]+"x"+hasPixelScale[1]; }

  @Override
  public WindowClosingMode getDefaultCloseOperation() {
      return awtWindowClosingProtocol.getDefaultCloseOperation();
  }

  @Override
  public WindowClosingMode setDefaultCloseOperation(WindowClosingMode op) {
      return awtWindowClosingProtocol.setDefaultCloseOperation(op);
  }

  private boolean handleReshape() {
    if (DEBUG) {
      System.err.println(getThreadName()+": GLJPanel.handleReshape: "+
                         panelWidth+"x"+panelHeight+" @ scale "+getPixelScaleStr() + " -> " +
                         reshapeWidth+"x"+reshapeHeight+" @ scale "+getPixelScaleStr());
    }
    panelWidth  = reshapeWidth;
    panelHeight = reshapeHeight;

    return backend.handleReshape();
  }

  // This is used as the GLEventListener for the pbuffer-based backend
  // as well as the callback mechanism for the other backends
  class Updater implements GLEventListener {
    private Graphics g;

    public void setGraphics(Graphics g) {
      this.g = g;
    }

    @Override
    public void init(GLAutoDrawable drawable) {
      if (!backend.preGL(g)) {
        return;
      }
      helper.init(GLJPanel.this, !sendReshape);
      backend.postGL(g, false);
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
      helper.disposeAllGLEventListener(GLJPanel.this, false);
    }

    @Override
    public void display(GLAutoDrawable drawable) {
      if (!backend.preGL(g)) {
        return;
      }
      if (sendReshape) {
        if (DEBUG) {
          System.err.println(getThreadName()+": GLJPanel.display: reshape(" + viewportX + "," + viewportY + " " + panelWidth + "x" + panelHeight + " @ scale "+getPixelScaleStr()+")");
        }
        helper.reshape(GLJPanel.this, viewportX, viewportY, panelWidth, panelHeight);
        sendReshape = false;
      }

      helper.display(GLJPanel.this);
      backend.postGL(g, true);
    }

    public void plainPaint(GLAutoDrawable drawable) {
      helper.display(GLJPanel.this);
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
      // This is handled above and dispatched directly to the appropriate context
    }
  }

  @Override
  public String toString() {
    final GLDrawable d = ( null != backend ) ? backend.getDrawable() : null;
    return "AWT-GLJPanel[ drawableType "+ ( ( null != d ) ? d.getClass().getName() : "null" ) +
           ", chosenCaps " + getChosenGLCapabilities() +
           "]";
  }

  private final Runnable disposeAction = new Runnable() {
    @Override
    public void run() {
        if ( null != backend ) {
            final GLContext _context = backend.getContext();
            final boolean backendDestroy = !backend.isUsingOwnLifecycle();
            if( null != _context && _context.isCreated() ) {
                // Catch dispose GLExceptions by GLEventListener, just 'print' them
                // so we can continue with the destruction.
                try {
                    helper.disposeGL(GLJPanel.this, _context, !backendDestroy);
                } catch (GLException gle) {
                    gle.printStackTrace();
                }
            }
            if ( backendDestroy ) {
                backend.destroy();
                backend = null;
                isInitialized = false;
            }
        }
    }
  };

  private final Runnable updaterInitAction = new Runnable() {
    @Override
    public void run() {
      updater.init(GLJPanel.this);
    }
  };

  private final Runnable updaterDisplayAction = new Runnable() {
    @Override
    public void run() {
      updater.display(GLJPanel.this);
    }
  };

  private final Runnable updaterPlainDisplayAction = new Runnable() {
    @Override
    public void run() {
      updater.plainPaint(GLJPanel.this);
    }
  };

  private final Runnable paintImmediatelyAction = new Runnable() {
    @Override
    public void run() {
      paintImmediately(0, 0, getWidth(), getHeight());
    }
  };

  private class DisposeGLEventListenerAction implements Runnable {
      GLEventListener listener;
      private final boolean remove;
      private DisposeGLEventListenerAction(GLEventListener listener, boolean remove) {
          this.listener = listener;
          this.remove = remove;
      }

      @Override
      public void run() {
          final Backend b = backend;
          if ( null != b ) {
              listener = helper.disposeGLEventListener(GLJPanel.this, b.getDrawable(), b.getContext(), listener, remove);
          }
      }
  };

  private int getGLInteger(GL gl, int which) {
    int[] tmp = new int[1];
    gl.glGetIntegerv(which, tmp, 0);
    return tmp[0];
  }

  protected static String getThreadName() { return Thread.currentThread().getName(); }

  //----------------------------------------------------------------------
  // Implementations of the various backends
  //

  /**
   * Abstraction of the different rendering backends: i.e., pure
   * software / pixmap rendering, pbuffer-based acceleration, Java 2D
   * JOGL bridge
   */
  static interface Backend {
    /** Create, Destroy, .. */
    public boolean isUsingOwnLifecycle();

    /** Called each time the backend needs to initialize itself */
    public void initialize();

    /** Called when the backend should clean up its resources */
    public void destroy();

    /** Called when the opacity of the GLJPanel is changed */
    public void setOpaque(boolean opaque);

    /**
     * Called to manually create an additional OpenGL context against
     * this GLJPanel
     */
    public GLContext createContext(GLContext shareWith);

    /** Called to set the current backend's GLContext */
    public void setContext(GLContext ctx);

    /** Called to get the current backend's GLContext */
    public GLContext getContext();

    /** Called to get the current backend's GLDrawable */
    public GLDrawable getDrawable();

    /** Returns the used texture unit, i.e. a value of [0..n], or -1 if non used. */
    public int getTextureUnit();

    /** Called to fetch the "real" GLCapabilities for the backend */
    public GLCapabilitiesImmutable getChosenGLCapabilities();

    /** Called to fetch the "real" GLProfile for the backend */
    public GLProfile getGLProfile();

    /**
     * Called to handle a reshape event. When this is called, the
     * OpenGL context associated with the backend is not current, to
     * make it easier to destroy and re-create pbuffers if necessary.
     */
    public boolean handleReshape();

    /**
     * Called before the OpenGL work is done in init() and display().
     * If false is returned, this render is aborted.
     */
    public boolean preGL(Graphics g);

    /**
     * Return true if backend handles 'swap buffer' itself
     * and hence the helper's setAutoSwapBuffer(enable) shall not be called.
     * In this case {@link GLJPanel#autoSwapBufferMode} is being used
     * in the backend to determine whether to swap buffers or not.
     */
    public boolean handlesSwapBuffer();

    /**
     * Shall invoke underlying drawable's swapBuffer.
     */
    public void swapBuffers();

    /**
     * Called after the OpenGL work is done in init() and display().
     * The isDisplay argument indicates whether this was called on
     * behalf of a call to display() rather than init().
     */
    public void postGL(Graphics g, boolean isDisplay);

    /** Called from within paintComponent() to initiate the render */
    public void doPaintComponent(Graphics g);

    /** Called from print .. no backend update desired onscreen */
    public void doPlainPaint();
  }

  // Base class used by both the software (pixmap) and pbuffer
  // backends, both of which rely on reading back the OpenGL frame
  // buffer and drawing it with a BufferedImage
  class OffscreenBackend implements Backend {
    private final AWTGLPixelBufferProvider pixelBufferProvider;
    private final boolean useSingletonBuffer;
    private AWTGLPixelBuffer pixelBuffer;
    private BufferedImage alignedImage;

    // One of these is used to store the read back pixels before storing
    // in the BufferedImage
    protected IntBuffer readBackIntsForCPUVFlip;

    // Implementation using software rendering
    private volatile GLDrawableImpl offscreenDrawable; // volatile: avoid locking for read-only access
    private boolean offscreenIsFBO;
    private FBObject fboFlipped;
    private GLSLTextureRaster glslTextureRaster;

    private volatile GLContextImpl offscreenContext; // volatile: avoid locking for read-only access
    private boolean flipVertical;
    private int frameCount = 0;

    // For saving/restoring of OpenGL state during ReadPixels
    private final GLPixelStorageModes psm =  new GLPixelStorageModes();

    OffscreenBackend(GLProfile glp, AWTGLPixelBufferProvider custom) {
        if(null == custom) {
            pixelBufferProvider = getSingleAWTGLPixelBufferProvider();
        } else {
            pixelBufferProvider = custom;
        }
        if( pixelBufferProvider instanceof SingletonGLPixelBufferProvider ) {
            useSingletonBuffer = true;
        } else {
            useSingletonBuffer = false;
        }
    }

    @Override
    public final boolean isUsingOwnLifecycle() { return false; }

    @Override
    public final void initialize() {
      if(DEBUG) {
          System.err.println(getThreadName()+": OffscreenBackend: initialize() - frameCount "+frameCount);
      }
      try {
          final GLContext[] shareWith = { null };
          if( helper.isSharedGLContextPending(shareWith) ) {
              return; // pending ..
          }
          offscreenDrawable = (GLDrawableImpl) factory.createOffscreenDrawable(
                                                    null /* default platform device */,
                                                    offscreenCaps,
                                                    chooser,
                                                    panelWidth, panelHeight);
          updateWrappedSurfaceScale(offscreenDrawable);
          offscreenDrawable.setRealized(true);
          if( DEBUG ) {
              offscreenDrawable.getNativeSurface().addSurfaceUpdatedListener(new SurfaceUpdatedListener() {
                  @Override
                  public final void surfaceUpdated(Object updater, NativeSurface ns, long when) {
                      System.err.println(getThreadName()+": OffscreenBackend.swapBuffers - frameCount "+frameCount);
                  } } );
          }

          offscreenContext = (GLContextImpl) offscreenDrawable.createContext(shareWith[0]);
          offscreenContext.setContextCreationFlags(additionalCtxCreationFlags);
          if( GLContext.CONTEXT_NOT_CURRENT < offscreenContext.makeCurrent() ) {
              isInitialized = true;
              helper.setAutoSwapBufferMode(false); // we handle swap-buffers, see handlesSwapBuffer()

              final GL gl = offscreenContext.getGL();
              flipVertical = !GLJPanel.this.skipGLOrientationVerticalFlip && offscreenDrawable.isGLOriented();
              final GLCapabilitiesImmutable chosenCaps = offscreenDrawable.getChosenGLCapabilities();
              offscreenIsFBO = chosenCaps.isFBO();
              final boolean glslCompliant = !offscreenContext.hasRendererQuirk(GLRendererQuirks.GLSLNonCompliant);
              final boolean useGLSLFlip = flipVertical && offscreenIsFBO && gl.isGL2ES2() && USE_GLSL_TEXTURE_RASTERIZER && glslCompliant;
              if( DEBUG ) {
                  System.err.println(getThreadName()+": OffscreenBackend.initialize: useGLSLFlip "+useGLSLFlip+
                          " [flip "+flipVertical+", isFBO "+offscreenIsFBO+", isGL2ES2 "+gl.isGL2ES2()+
                          ", noglsl "+!USE_GLSL_TEXTURE_RASTERIZER+", glslNonCompliant "+!glslCompliant+
                          ", isGL2ES2 " + gl.isGL2ES2()+"]");
              }
              if( useGLSLFlip ) {
                  final GLFBODrawable fboDrawable = (GLFBODrawable) offscreenDrawable;
                  fboDrawable.setTextureUnit( GLJPanel.this.requestedTextureUnit );
                  try {
                      fboFlipped = new FBObject();
                      fboFlipped.reset(gl, fboDrawable.getSurfaceWidth(), fboDrawable.getSurfaceHeight(), 0, false);
                      fboFlipped.attachTexture2D(gl, 0, chosenCaps.getAlphaBits()>0);
                      // fboFlipped.attachRenderbuffer(gl, Attachment.Type.DEPTH, 24);
                      glslTextureRaster = new GLSLTextureRaster(fboDrawable.getTextureUnit(), true);
                      glslTextureRaster.init(gl.getGL2ES2());
                      glslTextureRaster.reshape(gl.getGL2ES2(), 0, 0, fboDrawable.getSurfaceWidth(), fboDrawable.getSurfaceHeight());
                  } catch (Exception ex) {
                      ex.printStackTrace();
                      if(null != glslTextureRaster) {
                        glslTextureRaster.dispose(gl.getGL2ES2());
                        glslTextureRaster = null;
                      }
                      if(null != fboFlipped) {
                        fboFlipped.destroy(gl);
                        fboFlipped = null;
                      }
                  }
              } else {
                  fboFlipped = null;
                  glslTextureRaster = null;
              }
              offscreenContext.release();
          } else {
              isInitialized = false;
          }
      } finally {
          if( !isInitialized ) {
              if(null != offscreenContext) {
                  offscreenContext.destroy();
                  offscreenContext = null;
              }
              if(null != offscreenDrawable) {
                  offscreenDrawable.setRealized(false);
                  offscreenDrawable = null;
              }
          }
      }
    }

    @Override
    public final void destroy() {
      if(DEBUG) {
          System.err.println(getThreadName()+": OffscreenBackend: destroy() - offscreenContext: "+(null!=offscreenContext)+" - offscreenDrawable: "+(null!=offscreenDrawable)+" - frameCount "+frameCount);
      }
      if ( null != offscreenContext && offscreenContext.isCreated() ) {
        if( GLContext.CONTEXT_NOT_CURRENT < offscreenContext.makeCurrent() ) {
            try {
                final GL gl = offscreenContext.getGL();
                if(null != glslTextureRaster) {
                    glslTextureRaster.dispose(gl.getGL2ES2());
                }
                if(null != fboFlipped) {
                    fboFlipped.destroy(gl);
                }
            } finally {
                offscreenContext.destroy();
            }
        }
      }
      offscreenContext = null;
      glslTextureRaster = null;
      fboFlipped = null;
      offscreenContext = null;

      if (offscreenDrawable != null) {
        final AbstractGraphicsDevice adevice = offscreenDrawable.getNativeSurface().getGraphicsConfiguration().getScreen().getDevice();
        offscreenDrawable.setRealized(false);
        offscreenDrawable = null;
        if(null != adevice) {
            adevice.close();
        }
      }
      offscreenIsFBO = false;

      if( null != readBackIntsForCPUVFlip ) {
          readBackIntsForCPUVFlip.clear();
          readBackIntsForCPUVFlip = null;
      }
      if( null != pixelBuffer ) {
          if( !useSingletonBuffer ) {
              pixelBuffer.dispose();
          }
          pixelBuffer = null;
      }
      alignedImage = null;
    }

    @Override
    public final void setOpaque(boolean opaque) {
      if ( opaque != isOpaque() && !useSingletonBuffer ) {
          pixelBuffer.dispose();
          pixelBuffer = null;
          alignedImage = null;
      }
    }

    @Override
    public final boolean preGL(Graphics g) {
      // Empty in this implementation
      return true;
    }

    @Override
    public final boolean handlesSwapBuffer() {
        return true;
    }

    @Override
    public final void swapBuffers() {
        final GLDrawable d = offscreenDrawable;
        if( null != d ) {
            d.swapBuffers();
        }
    }

    @Override
    public final void postGL(Graphics g, boolean isDisplay) {
      if (isDisplay) {
        if(DEBUG) {
            System.err.println(getThreadName()+": GLJPanel.OffscreenBackend.postGL.0: - frameCount "+frameCount);
        }
        if( autoSwapBufferMode ) {
            // Since we only use a single-buffer non-MSAA or double-buffered MSAA offscreenDrawable,
            // we can always swap!
            offscreenDrawable.swapBuffers();
        }
        final GL gl = offscreenContext.getGL();

        final int componentCount;
        final int alignment;
        if( isOpaque() ) {
            // w/o alpha
            componentCount = 3;
            alignment = 1;
        } else {
            // with alpha
            componentCount = 4;
            alignment = 4;
        }

        final GLPixelAttributes pixelAttribs = pixelBufferProvider.getAttributes(gl, componentCount);

        if( useSingletonBuffer ) { // attempt to fetch the latest AWTGLPixelBuffer
            pixelBuffer = (AWTGLPixelBuffer) ((SingletonGLPixelBufferProvider)pixelBufferProvider).getSingleBuffer(pixelAttribs);
        }
        if( null != pixelBuffer && pixelBuffer.requiresNewBuffer(gl, panelWidth, panelHeight, 0) ) {
            pixelBuffer.dispose();
            pixelBuffer = null;
            alignedImage = null;
        }
        if ( null == pixelBuffer ) {
          if (0 >= panelWidth || 0 >= panelHeight ) {
              return;
          }
          pixelBuffer = pixelBufferProvider.allocate(gl, pixelAttribs, panelWidth, panelHeight, 1, true, 0);
          if(DEBUG) {
              System.err.println(getThreadName()+": GLJPanel.OffscreenBackend.postGL.0: "+GLJPanel.this.getName()+" pixelBufferProvider isSingletonBufferProvider "+useSingletonBuffer+", 0x"+Integer.toHexString(pixelBufferProvider.hashCode())+", "+pixelBufferProvider.getClass().getSimpleName());
              System.err.println(getThreadName()+": GLJPanel.OffscreenBackend.postGL.0: "+GLJPanel.this.getName()+" pixelBuffer 0x"+Integer.toHexString(pixelBuffer.hashCode())+", "+pixelBuffer+", alignment "+alignment);
              System.err.println(getThreadName()+": GLJPanel.OffscreenBackend.postGL.0: "+GLJPanel.this.getName()+" flippedVertical "+flipVertical+", glslTextureRaster "+(null!=glslTextureRaster));
              System.err.println(getThreadName()+": GLJPanel.OffscreenBackend.postGL.0: "+GLJPanel.this.getName()+" panelSize "+panelWidth+"x"+panelHeight+" @ scale "+getPixelScaleStr());
          }
        }
        if( offscreenDrawable.getSurfaceWidth() != panelWidth || offscreenDrawable.getSurfaceHeight() != panelHeight ) {
            throw new InternalError("OffscreenDrawable panelSize mismatch (reshape missed): panelSize "+panelWidth+"x"+panelHeight+" != drawable "+offscreenDrawable.getSurfaceWidth()+"x"+offscreenDrawable.getSurfaceHeight()+", on thread "+getThreadName());
        }
        if( null == alignedImage ||
            panelWidth != alignedImage.getWidth() || panelHeight != alignedImage.getHeight() ||
            !pixelBuffer.isDataBufferSource(alignedImage) ) {
            alignedImage = pixelBuffer.getAlignedImage(panelWidth, panelHeight);
            if(DEBUG) {
                System.err.println(getThreadName()+": GLJPanel.OffscreenBackend.postGL.0: "+GLJPanel.this.getName()+" new alignedImage "+alignedImage.getWidth()+"x"+alignedImage.getHeight()+" @ scale "+getPixelScaleStr()+", "+alignedImage+", pixelBuffer "+pixelBuffer.width+"x"+pixelBuffer.height+", "+pixelBuffer);
            }
        }
        final IntBuffer readBackInts;

        if( !flipVertical || null != glslTextureRaster ) {
           readBackInts = (IntBuffer) pixelBuffer.buffer;
        } else {
           if( null == readBackIntsForCPUVFlip || pixelBuffer.width * pixelBuffer.height > readBackIntsForCPUVFlip.remaining() ) {
               readBackIntsForCPUVFlip = IntBuffer.allocate(pixelBuffer.width * pixelBuffer.height);
           }
           readBackInts = readBackIntsForCPUVFlip;
        }

        final TextureState usrTexState, fboTexState;
        final int fboTexUnit = GL.GL_TEXTURE0 + ( offscreenIsFBO ? ((GLFBODrawable)offscreenDrawable).getTextureUnit() : 0 );

        if( offscreenIsFBO ) {
            usrTexState = new TextureState(gl, GL.GL_TEXTURE_2D);
            if( fboTexUnit != usrTexState.getUnit() ) {
                // glActiveTexture(..) + glBindTexture(..) are implicit performed in GLFBODrawableImpl's
                // swapBuffers/contextMadeCurent -> swapFBOImpl.
                // We need to cache the texture unit's bound texture-id before it's overwritten.
                gl.glActiveTexture(fboTexUnit);
                fboTexState = new TextureState(gl, fboTexUnit, GL.GL_TEXTURE_2D);
            } else {
                fboTexState = usrTexState;
            }
        } else {
            usrTexState = null;
            fboTexState = null;
        }

        // Must now copy pixels from offscreen context into surface
        if(DEBUG) {
            System.err.println(getThreadName()+": GLJPanel.OffscreenBackend.postGL.readPixels: - frameCount "+frameCount);
        }

        // Save PACK modes, reset them to defaults and set alignment
        psm.setPackAlignment(gl, alignment);
        if(gl.isGL2ES3()) {
            final GL2ES3 gl2es3 = gl.getGL2ES3();
            gl2es3.glPixelStorei(GL2ES3.GL_PACK_ROW_LENGTH, panelWidth);
            gl2es3.glReadBuffer(gl2es3.getDefaultReadBuffer());
        }

        if(null != glslTextureRaster) { // implies flippedVertical
            final boolean viewportChange;
            final int[] usrViewport = new int[] { 0, 0, 0, 0 };
            gl.glGetIntegerv(GL.GL_VIEWPORT, usrViewport, 0);
            viewportChange = 0 != usrViewport[0] || 0 != usrViewport[1] ||
                             offscreenDrawable.getSurfaceWidth() != usrViewport[2] || offscreenDrawable.getSurfaceHeight() != usrViewport[3];
            if( DEBUG_VIEWPORT ) {
                System.err.println(getThreadName()+": GLJPanel.OffscreenBackend.postGL: "+GLJPanel.this.getName()+" Viewport: change "+viewportChange+
                         ", "+usrViewport[0]+"/"+usrViewport[1]+" "+usrViewport[2]+"x"+usrViewport[3]+
                         " -> 0/0 "+offscreenDrawable.getSurfaceWidth()+"x"+offscreenDrawable.getSurfaceHeight());
            }
            if( viewportChange ) {
                gl.glViewport(0, 0, offscreenDrawable.getSurfaceWidth(), offscreenDrawable.getSurfaceHeight());
            }

            // perform vert-flipping via OpenGL/FBO
            final GLFBODrawable fboDrawable = (GLFBODrawable)offscreenDrawable;
            final FBObject.TextureAttachment fboTex = fboDrawable.getTextureBuffer(GL.GL_FRONT);

            fboFlipped.bind(gl);

            // gl.glActiveTexture(GL.GL_TEXTURE0 + fboDrawable.getTextureUnit()); // implicit by GLFBODrawableImpl: swapBuffers/contextMadeCurent -> swapFBOImpl
            gl.glBindTexture(GL.GL_TEXTURE_2D, fboTex.getName());
            // gl.glClear(GL.GL_DEPTH_BUFFER_BIT); // fboFlipped runs w/o DEPTH!

            glslTextureRaster.display(gl.getGL2ES2());
            gl.glReadPixels(0, 0, panelWidth, panelHeight, pixelAttribs.format, pixelAttribs.type, readBackInts);

            fboFlipped.unbind(gl);
            if( viewportChange ) {
                gl.glViewport(usrViewport[0], usrViewport[1], usrViewport[2], usrViewport[3]);
            }
            fboTexState.restore(gl);
        } else {
            gl.glReadPixels(0, 0, panelWidth, panelHeight, pixelAttribs.format, pixelAttribs.type, readBackInts);

            if ( flipVertical ) {
                // Copy temporary data into raster of BufferedImage for faster
                // blitting Note that we could avoid this copy in the cases
                // where !offscreenDrawable.isGLOriented(),
                // but that's the software rendering path which is very slow anyway.
                final BufferedImage image = alignedImage;
                final int[] src = readBackInts.array();
                final int[] dest = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
                final int incr = panelWidth;
                int srcPos = 0;
                int destPos = (panelHeight - 1) * panelWidth;
                for (; destPos >= 0; srcPos += incr, destPos -= incr) {
                  System.arraycopy(src, srcPos, dest, destPos, incr);
                }
            }
        }
        if( offscreenIsFBO && fboTexUnit != usrTexState.getUnit() ) {
            usrTexState.restore(gl);
        }

        // Restore saved modes.
        psm.restore(gl);

        // Note: image will be drawn back in paintComponent() for
        // correctness on all platforms
      }
    }

    @Override
    public final int getTextureUnit() {
        if(null != glslTextureRaster && null != offscreenDrawable) { // implies flippedVertical
            return ((GLFBODrawable)offscreenDrawable).getTextureUnit();
        }
        return -1;
    }

    @Override
    public final void doPaintComponent(Graphics g) {
      helper.invokeGL(offscreenDrawable, offscreenContext, updaterDisplayAction, updaterInitAction);

      if ( null != alignedImage ) {
        if( DEBUG ) {
            System.err.println(getThreadName()+": GLJPanel.OffscreenBackend.doPaintComponent.drawImage: - frameCount "+frameCount);
        }
        // Draw resulting image in one shot
        g.drawImage(alignedImage, 0, 0, alignedImage.getWidth()/hasPixelScale[0], alignedImage.getHeight()/hasPixelScale[1], null); // Null ImageObserver since image data is ready.
      }
      frameCount++;
    }

    @Override
    public final void doPlainPaint() {
      helper.invokeGL(offscreenDrawable, offscreenContext, updaterPlainDisplayAction, updaterInitAction);
    }

    @Override
    public final boolean handleReshape() {
        GLDrawableImpl _drawable = offscreenDrawable;
        {
            final GLDrawableImpl _drawableNew = GLDrawableHelper.resizeOffscreenDrawable(_drawable, offscreenContext, panelWidth, panelHeight);
            if(_drawable != _drawableNew) {
                // write back
                _drawable = _drawableNew;
                offscreenDrawable = _drawableNew;
                updateWrappedSurfaceScale(offscreenDrawable);
            }
        }
        if (DEBUG) {
            System.err.println(getThreadName()+": GLJPanel.OffscreenBackend.handleReshape: " +panelWidth+"x"+panelHeight + " @ scale "+getPixelScaleStr() + " -> " + _drawable.getSurfaceWidth()+"x"+_drawable.getSurfaceHeight());
        }
        panelWidth = _drawable.getSurfaceWidth();
        panelHeight = _drawable.getSurfaceHeight();

        if( null != glslTextureRaster ) {
            if( GLContext.CONTEXT_NOT_CURRENT < offscreenContext.makeCurrent() ) {
                try {
                    final GL gl = offscreenContext.getGL();
                    fboFlipped.reset(gl, _drawable.getSurfaceWidth(), _drawable.getSurfaceHeight(), 0, false);
                    glslTextureRaster.reshape(gl.getGL2ES2(), 0, 0, _drawable.getSurfaceWidth(), _drawable.getSurfaceHeight());
                } finally {
                    offscreenContext.release();
                }
            }
        }
        return _drawable.isRealized();
    }

    @Override
    public final GLContext createContext(GLContext shareWith) {
      return (null != offscreenDrawable) ? offscreenDrawable.createContext(shareWith) : null;
    }

    @Override
    public final void setContext(GLContext ctx) {
      offscreenContext=(GLContextImpl)ctx;
    }

    @Override
    public final GLContext getContext() {
      return offscreenContext;
    }

    @Override
    public final GLDrawable getDrawable() {
        return offscreenDrawable;
    }

    @Override
    public final GLCapabilitiesImmutable getChosenGLCapabilities() {
      if (offscreenDrawable == null) {
        return null;
      }
      return offscreenDrawable.getChosenGLCapabilities();
    }

    @Override
    public final GLProfile getGLProfile() {
      if (offscreenDrawable == null) {
        return null;
      }
      return offscreenDrawable.getGLProfile();
    }
  }

  class J2DOGLBackend implements Backend {
    // Opaque Object identifier representing the Java2D surface we are
    // drawing to; used to determine when to destroy and recreate JOGL
    // context
    private Object j2dSurface;
    // Graphics object being used during Java2D update action
    // (absolutely essential to cache this)
    // No-op context representing the Java2D OpenGL context
    private GLContext j2dContext;
    // Context associated with no-op drawable representing the JOGL
    // OpenGL context
    private GLDrawable joglDrawable;
    // The real OpenGL context JOGL uses to render
    private GLContext  joglContext;
    // State captured from Java2D OpenGL context necessary in order to
    // properly render into Java2D back buffer
    private final int[] drawBuffer   = new int[1];
    private final int[] readBuffer   = new int[1];
    // This is required when the FBO option of the Java2D / OpenGL
    // pipeline is active
    private final int[] frameBuffer  = new int[1];
    // Current (as of this writing) NVidia drivers have a couple of bugs
    // relating to the sharing of framebuffer and renderbuffer objects
    // between contexts. It appears we have to (a) reattach the color
    // attachment and (b) actually create new depth buffer storage and
    // attach it in order for the FBO to behave properly in our context.
    private boolean checkedForFBObjectWorkarounds;
    private boolean fbObjectWorkarounds;
    private int[] frameBufferDepthBuffer;
    private int[] frameBufferTexture;
    private boolean createNewDepthBuffer;
    // Current (as of this writing) ATI drivers have problems when the
    // same FBO is bound in two different contexts. Here we check for
    // this case and explicitly release the FBO from Java2D's context
    // before switching to ours. Java2D will re-bind the FBO when it
    // makes its context current the next time. Interestingly, if we run
    // this code path on NVidia hardware, it breaks the rendering
    // results -- no output is generated. This doesn't appear to be an
    // interaction with the abovementioned NVidia-specific workarounds,
    // as even if we disable that code the FBO is still reported as
    // incomplete in our context.
    private boolean checkedGLVendor;
    private boolean vendorIsATI;

    // Holding on to this GraphicsConfiguration is a workaround for a
    // problem in the Java 2D / JOGL bridge when FBOs are enabled; see
    // comment related to Issue 274 below
    private GraphicsConfiguration workaroundConfig;

    @Override
    public final boolean isUsingOwnLifecycle() { return true; }

    @Override
    public final void initialize() {
      if(DEBUG) {
          System.err.println(getThreadName()+": J2DOGL: initialize()");
      }
      // No-op in this implementation; everything is done lazily
      isInitialized = true;
    }

    @Override
    public final void destroy() {
      Java2D.invokeWithOGLContextCurrent(null, new Runnable() {
          @Override
          public void run() {
            if(DEBUG) {
                System.err.println(getThreadName()+": J2DOGL: destroy() - joglContext: "+(null!=joglContext)+" - joglDrawable: "+(null!=joglDrawable));
            }
            if (joglContext != null) {
              joglContext.destroy();
              joglContext = null;
            }
            joglDrawable = null;
            if (j2dContext != null) {
              j2dContext.destroy();
              j2dContext = null;
            }
          }
        });
    }

    @Override
    public final void setOpaque(boolean opaque) {
      // Empty in this implementation
    }

    @Override
    public final GLContext createContext(GLContext shareWith) {
      if(null != shareWith) {
          throw new GLException("J2DOGLBackend cannot create context w/ additional shared context, since it already needs to share the context w/ J2D.");
      }
      return (null != joglDrawable && null != j2dContext) ? joglDrawable.createContext(j2dContext) : null;
    }

    @Override
    public final void setContext(GLContext ctx) {
        joglContext=ctx;
    }

    @Override
    public final GLContext getContext() {
      return joglContext;
    }

    @Override
    public final GLDrawable getDrawable() {
        return joglDrawable;
    }

    @Override
    public final int getTextureUnit() { return -1; }

    @Override
    public final GLCapabilitiesImmutable getChosenGLCapabilities() {
      // FIXME: should do better than this; is it possible to query J2D Caps ?
      return new GLCapabilities(null);
    }

    @Override
    public final GLProfile getGLProfile() {
      // FIXME: should do better than this; is it possible to query J2D's Profile ?
      return GLProfile.getDefault(GLProfile.getDefaultDevice());
    }

    @Override
    public final boolean handleReshape() {
      // Empty in this implementation
      return true;
    }

    @Override
    public final boolean preGL(Graphics g) {
      final GL2 gl = joglContext.getGL().getGL2();
      // Set up needed state in JOGL context from Java2D context
      gl.glEnable(GL2.GL_SCISSOR_TEST);
      Rectangle r = Java2D.getOGLScissorBox(g);

      if (r == null) {
        if (DEBUG) {
          System.err.println(getThreadName()+": Java2D.getOGLScissorBox() returned null");
        }
        return false;
      }
      if (DEBUG) {
        System.err.println(getThreadName()+": GLJPanel: gl.glScissor(" + r.x + ", " + r.y + ", " + r.width + ", " + r.height + ")");
      }

      gl.glScissor(r.x, r.y, r.width, r.height);
      Rectangle oglViewport = Java2D.getOGLViewport(g, panelWidth, panelHeight);
      // If the viewport X or Y changes, in addition to the panel's
      // width or height, we need to send a reshape operation to the
      // client
      if ((viewportX != oglViewport.x) ||
          (viewportY != oglViewport.y)) {
        sendReshape = true;
        if (DEBUG) {
          System.err.println(getThreadName()+": Sending reshape because viewport changed");
          System.err.println("  viewportX (" + viewportX + ") ?= oglViewport.x (" + oglViewport.x + ")");
          System.err.println("  viewportY (" + viewportY + ") ?= oglViewport.y (" + oglViewport.y + ")");
        }
      }
      viewportX = oglViewport.x;
      viewportY = oglViewport.y;

      // If the FBO option is active, bind to the FBO from the Java2D
      // context.
      // Note that all of the plumbing in the context sharing stuff will
      // allow us to bind to this object since it's in our namespace.
      if (Java2D.isFBOEnabled() &&
          Java2D.getOGLSurfaceType(g) == Java2D.FBOBJECT) {

        // The texture target for Java2D's OpenGL pipeline when using FBOs
        // -- either GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE_ARB
        int fboTextureTarget = Java2D.getOGLTextureType(g);

        if (!checkedForFBObjectWorkarounds) {
          checkedForFBObjectWorkarounds = true;
          gl.glBindTexture(fboTextureTarget, 0);
          gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, frameBuffer[0]);
          int status = gl.glCheckFramebufferStatus(GL.GL_FRAMEBUFFER);
          if (status != GL.GL_FRAMEBUFFER_COMPLETE) {
              // Need to do workarounds
              fbObjectWorkarounds = true;
              createNewDepthBuffer = true;
              if (DEBUG) {
                  System.err.println(getThreadName()+": GLJPanel: ERR GL_FRAMEBUFFER_BINDING: Discovered Invalid J2D FBO("+frameBuffer[0]+"): "+FBObject.getStatusString(status) +
                                     ", frame_buffer_object workarounds to be necessary");
              }
          } else {
            // Don't need the frameBufferTexture temporary any more
            frameBufferTexture = null;
            if (DEBUG) {
              System.err.println(getThreadName()+": GLJPanel: OK GL_FRAMEBUFFER_BINDING: "+frameBuffer[0]);
            }
          }
        }

        if (fbObjectWorkarounds && createNewDepthBuffer) {
          if (frameBufferDepthBuffer == null)
            frameBufferDepthBuffer = new int[1];

          // Create our own depth renderbuffer and associated storage
          // If we have an old one, delete it
          if (frameBufferDepthBuffer[0] != 0) {
            gl.glDeleteRenderbuffers(1, frameBufferDepthBuffer, 0);
            frameBufferDepthBuffer[0] = 0;
          }

          gl.glBindTexture(fboTextureTarget, frameBufferTexture[0]);
          int[] width = new int[1];
          int[] height = new int[1];
          gl.glGetTexLevelParameteriv(fboTextureTarget, 0, GL2.GL_TEXTURE_WIDTH, width, 0);
          gl.glGetTexLevelParameteriv(fboTextureTarget, 0, GL2.GL_TEXTURE_HEIGHT, height, 0);

          gl.glGenRenderbuffers(1, frameBufferDepthBuffer, 0);
          if (DEBUG) {
            System.err.println(getThreadName()+": GLJPanel: Generated frameBufferDepthBuffer " + frameBufferDepthBuffer[0] +
                               " with width " + width[0] + ", height " + height[0]);
          }

          gl.glBindRenderbuffer(GL.GL_RENDERBUFFER, frameBufferDepthBuffer[0]);
          // FIXME: may need a loop here like in Java2D
          gl.glRenderbufferStorage(GL.GL_RENDERBUFFER, GL2GL3.GL_DEPTH_COMPONENT24, width[0], height[0]);

          gl.glBindRenderbuffer(GL2.GL_RENDERBUFFER, 0);
          createNewDepthBuffer = false;
        }

        gl.glBindTexture(fboTextureTarget, 0);
        gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, frameBuffer[0]);

        if (fbObjectWorkarounds) {
          // Hook up the color and depth buffer attachment points for this framebuffer
          gl.glFramebufferTexture2D(GL.GL_FRAMEBUFFER,
                                    GL.GL_COLOR_ATTACHMENT0,
                                    fboTextureTarget,
                                    frameBufferTexture[0],
                                    0);
          if (DEBUG) {
            System.err.println(getThreadName()+": GLJPanel: frameBufferDepthBuffer: " + frameBufferDepthBuffer[0]);
          }
          gl.glFramebufferRenderbuffer(GL.GL_FRAMEBUFFER,
                                       GL.GL_DEPTH_ATTACHMENT,
                                       GL.GL_RENDERBUFFER,
                                       frameBufferDepthBuffer[0]);
        }

        if (DEBUG) {
          int status = gl.glCheckFramebufferStatus(GL.GL_FRAMEBUFFER);
          if (status != GL.GL_FRAMEBUFFER_COMPLETE) {
            throw new GLException("Error: framebuffer was incomplete: status = 0x" +
                                  Integer.toHexString(status));
          }
        }
      } else {
        if (DEBUG) {
          System.err.println(getThreadName()+": GLJPanel: Setting up drawBuffer " + drawBuffer[0] +
                             " and readBuffer " + readBuffer[0]);
        }

        gl.glDrawBuffer(drawBuffer[0]);
        gl.glReadBuffer(readBuffer[0]);
      }

      return true;
    }

    @Override
    public final boolean handlesSwapBuffer() {
        return false;
    }

    @Override
    public final void swapBuffers() {
        final GLDrawable d = joglDrawable;
        if( null != d ) {
            d.swapBuffers();
        }
    }

    @Override
    public final void postGL(Graphics g, boolean isDisplay) {
      // Cause OpenGL pipeline to flush its results because
      // otherwise it's possible we will buffer up multiple frames'
      // rendering results, resulting in apparent mouse lag
      GL gl = joglContext.getGL();
      gl.glFinish();

      if (Java2D.isFBOEnabled() &&
          Java2D.getOGLSurfaceType(g) == Java2D.FBOBJECT) {
        // Unbind the framebuffer from our context to work around
        // apparent driver bugs or at least unspecified behavior causing
        // OpenGL to run out of memory with certain cards and drivers
        gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0);
      }
    }

    @Override
    public final void doPaintComponent(final Graphics g) {
      // This is a workaround for an issue in the Java 2D / JOGL
      // bridge (reported by an end user as JOGL Issue 274) where Java
      // 2D can occasionally leave its internal OpenGL context current
      // to the on-screen window rather than its internal "scratch"
      // pbuffer surface to which the FBO is attached. JOGL expects to
      // find a stable OpenGL drawable (on Windows, an HDC) upon which
      // it can create another OpenGL context. It turns out that, on
      // Windows, when Java 2D makes its internal OpenGL context
      // current against the window in order to put pixels on the
      // screen, it gets the device context for the window, makes its
      // context current, and releases the device context. This means
      // that when JOGL's Runnable gets to run below, the HDC is
      // already invalid. The workaround for this is to force Java 2D
      // to make its context current to the scratch surface, which we
      // can do by executing an empty Runnable with the "shared"
      // context current. This will be fixed in a Java SE 6 update
      // release, hopefully 6u2.
      if (Java2D.isFBOEnabled()) {
        if (workaroundConfig == null) {
          workaroundConfig = GraphicsEnvironment.
            getLocalGraphicsEnvironment().
            getDefaultScreenDevice().
            getDefaultConfiguration();
        }
        Java2D.invokeWithOGLSharedContextCurrent(workaroundConfig, new Runnable() { @Override
        public void run() {}});
      }

      Java2D.invokeWithOGLContextCurrent(g, new Runnable() {
          @Override
          public void run() {
            if (DEBUG) {
              System.err.println(getThreadName()+": GLJPanel.invokeWithOGLContextCurrent");
            }

            // Create no-op context representing Java2D context
            if (j2dContext == null) {
              j2dContext = factory.createExternalGLContext();
              if (DEBUG) {
                System.err.println(getThreadName()+": GLJPanel.Created External Context: "+j2dContext);
              }
              if (DEBUG) {
//                j2dContext.setGL(new DebugGL2(j2dContext.getGL().getGL2()));
              }

              // Check to see whether we can support the requested
              // capabilities or need to fall back to a pbuffer
              // FIXME: add more checks?

              j2dContext.makeCurrent();
              GL gl = j2dContext.getGL();
              if ((getGLInteger(gl, GL.GL_RED_BITS)         < offscreenCaps.getRedBits())        ||
                  (getGLInteger(gl, GL.GL_GREEN_BITS)       < offscreenCaps.getGreenBits())      ||
                  (getGLInteger(gl, GL.GL_BLUE_BITS)        < offscreenCaps.getBlueBits())       ||
                  //                  (getGLInteger(gl, GL.GL_ALPHA_BITS)       < offscreenCaps.getAlphaBits())      ||
                  (getGLInteger(gl, GL2.GL_ACCUM_RED_BITS)   < offscreenCaps.getAccumRedBits())   ||
                  (getGLInteger(gl, GL2.GL_ACCUM_GREEN_BITS) < offscreenCaps.getAccumGreenBits()) ||
                  (getGLInteger(gl, GL2.GL_ACCUM_BLUE_BITS)  < offscreenCaps.getAccumBlueBits())  ||
                  (getGLInteger(gl, GL2.GL_ACCUM_ALPHA_BITS) < offscreenCaps.getAccumAlphaBits()) ||
                  //          (getGLInteger(gl, GL2.GL_DEPTH_BITS)       < offscreenCaps.getDepthBits())      ||
                  (getGLInteger(gl, GL.GL_STENCIL_BITS)     < offscreenCaps.getStencilBits())) {
                if (DEBUG) {
                  System.err.println(getThreadName()+": GLJPanel: Falling back to pbuffer-based support because Java2D context insufficient");
                  System.err.println("                    Available              Required");
                  System.err.println("GL_RED_BITS         " + getGLInteger(gl, GL.GL_RED_BITS)         + "              " + offscreenCaps.getRedBits());
                  System.err.println("GL_GREEN_BITS       " + getGLInteger(gl, GL.GL_GREEN_BITS)       + "              " + offscreenCaps.getGreenBits());
                  System.err.println("GL_BLUE_BITS        " + getGLInteger(gl, GL.GL_BLUE_BITS)        + "              " + offscreenCaps.getBlueBits());
                  System.err.println("GL_ALPHA_BITS       " + getGLInteger(gl, GL.GL_ALPHA_BITS)       + "              " + offscreenCaps.getAlphaBits());
                  System.err.println("GL_ACCUM_RED_BITS   " + getGLInteger(gl, GL2.GL_ACCUM_RED_BITS)   + "              " + offscreenCaps.getAccumRedBits());
                  System.err.println("GL_ACCUM_GREEN_BITS " + getGLInteger(gl, GL2.GL_ACCUM_GREEN_BITS) + "              " + offscreenCaps.getAccumGreenBits());
                  System.err.println("GL_ACCUM_BLUE_BITS  " + getGLInteger(gl, GL2.GL_ACCUM_BLUE_BITS)  + "              " + offscreenCaps.getAccumBlueBits());
                  System.err.println("GL_ACCUM_ALPHA_BITS " + getGLInteger(gl, GL2.GL_ACCUM_ALPHA_BITS) + "              " + offscreenCaps.getAccumAlphaBits());
                  System.err.println("GL_DEPTH_BITS       " + getGLInteger(gl, GL.GL_DEPTH_BITS)       + "              " + offscreenCaps.getDepthBits());
                  System.err.println("GL_STENCIL_BITS     " + getGLInteger(gl, GL.GL_STENCIL_BITS)     + "              " + offscreenCaps.getStencilBits());
                }
                isInitialized = false;
                backend = null;
                java2DGLPipelineOK = false;
                handleReshape = true;
                j2dContext.destroy();
                j2dContext = null;
                return;
              }
            } else {
              j2dContext.makeCurrent();
            }
            try {
              captureJ2DState(j2dContext.getGL(), g);
              Object curSurface = Java2D.getOGLSurfaceIdentifier(g);
              if (curSurface != null) {
                if (j2dSurface != curSurface) {
                  if (joglContext != null) {
                    joglContext.destroy();
                    joglContext = null;
                    joglDrawable = null;
                    sendReshape = true;
                    if (DEBUG) {
                      System.err.println(getThreadName()+": Sending reshape because surface changed");
                      System.err.println("New surface = " + curSurface);
                    }
                  }
                  j2dSurface = curSurface;
                  if (DEBUG) {
                      System.err.print(getThreadName()+": Surface type: ");
                      int surfaceType = Java2D.getOGLSurfaceType(g);
                      if (surfaceType == Java2D.UNDEFINED) {
                        System.err.println("UNDEFINED");
                      } else if (surfaceType == Java2D.WINDOW) {
                        System.err.println("WINDOW");
                      } else if (surfaceType == Java2D.PBUFFER) {
                        System.err.println("PBUFFER");
                      } else if (surfaceType == Java2D.TEXTURE) {
                        System.err.println("TEXTURE");
                      } else if (surfaceType == Java2D.FLIP_BACKBUFFER) {
                        System.err.println("FLIP_BACKBUFFER");
                      } else if (surfaceType == Java2D.FBOBJECT) {
                        System.err.println("FBOBJECT");
                      } else {
                        System.err.println("(Unknown surface type " + surfaceType + ")");
                      }
                  }
                }
                if (joglContext == null) {
                  AbstractGraphicsDevice device = j2dContext.getGLDrawable().getNativeSurface().getGraphicsConfiguration().getScreen().getDevice();
                  if (factory.canCreateExternalGLDrawable(device)) {
                    joglDrawable = factory.createExternalGLDrawable();
                    joglContext = joglDrawable.createContext(j2dContext);
                    if (DEBUG) {
                        System.err.println("-- Created External Drawable: "+joglDrawable);
                        System.err.println("-- Created Context: "+joglContext);
                    }
                  }
                  if (Java2D.isFBOEnabled() &&
                      Java2D.getOGLSurfaceType(g) == Java2D.FBOBJECT &&
                      fbObjectWorkarounds) {
                    createNewDepthBuffer = true;
                  }
                }
                helper.invokeGL(joglDrawable, joglContext, updaterDisplayAction, updaterInitAction);
              }
            } finally {
              j2dContext.release();
            }
          }
        });
    }

    @Override
    public final void doPlainPaint() {
      helper.invokeGL(joglDrawable, joglContext, updaterPlainDisplayAction, updaterInitAction);
    }

    private final void captureJ2DState(GL gl, Graphics g) {
      gl.glGetIntegerv(GL2.GL_DRAW_BUFFER, drawBuffer, 0);
      gl.glGetIntegerv(GL2.GL_READ_BUFFER, readBuffer, 0);
      if (Java2D.isFBOEnabled() &&
          Java2D.getOGLSurfaceType(g) == Java2D.FBOBJECT) {
        gl.glGetIntegerv(GL.GL_FRAMEBUFFER_BINDING, frameBuffer, 0);
        if(!gl.glIsFramebuffer(frameBuffer[0])) {
          checkedForFBObjectWorkarounds=true;
          fbObjectWorkarounds = true;
          createNewDepthBuffer = true;
          if (DEBUG) {
              System.err.println(getThreadName()+": GLJPanel: Fetched ERR GL_FRAMEBUFFER_BINDING: "+frameBuffer[0]+" - NOT A FBO"+
                                 ", frame_buffer_object workarounds to be necessary");
          }
        } else if (DEBUG) {
          System.err.println(getThreadName()+": GLJPanel: Fetched OK GL_FRAMEBUFFER_BINDING: "+frameBuffer[0]);
        }

        if(fbObjectWorkarounds || !checkedForFBObjectWorkarounds) {
            // See above for description of what we are doing here
            if (frameBufferTexture == null)
                frameBufferTexture = new int[1];

            // Query the framebuffer for its color buffer so we can hook
            // it back up in our context (should not be necessary)
            gl.glGetFramebufferAttachmentParameteriv(GL.GL_FRAMEBUFFER,
                                                     GL.GL_COLOR_ATTACHMENT0,
                                                     GL.GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
                                                     frameBufferTexture, 0);
            if (DEBUG) {
                System.err.println(getThreadName()+": GLJPanel: FBO COLOR_ATTACHMENT0: " + frameBufferTexture[0]);
            }
        }

        if (!checkedGLVendor) {
          checkedGLVendor = true;
          String vendor = gl.glGetString(GL.GL_VENDOR);

          if ((vendor != null) &&
              vendor.startsWith("ATI")) {
            vendorIsATI = true;
          }
        }

        if (vendorIsATI) {
          // Unbind the FBO from Java2D's context as it appears that
          // driver bugs on ATI's side are causing problems if the FBO is
          // simultaneously bound to more than one context. Java2D will
          // re-bind the FBO during the next validation of its context.
          // Note: this breaks rendering at least on NVidia hardware
          gl.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0);
        }
      }
    }
  }
}