aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/jogamp/opengl/egl/EGLGraphicsConfigurationFactory.java
blob: 1d9a9732d52cbfa3ca7a1729954088a79443bf77 (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
/*
 * Copyright (c) 2008 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.
 */

package jogamp.opengl.egl;

import java.io.PrintStream;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.jogamp.nativewindow.AbstractGraphicsConfiguration;
import com.jogamp.nativewindow.AbstractGraphicsDevice;
import com.jogamp.nativewindow.AbstractGraphicsScreen;
import com.jogamp.nativewindow.CapabilitiesChooser;
import com.jogamp.nativewindow.CapabilitiesFilter;
import com.jogamp.nativewindow.CapabilitiesImmutable;
import com.jogamp.nativewindow.GraphicsConfigurationFactory;
import com.jogamp.nativewindow.NativeWindowFactory;
import com.jogamp.nativewindow.VisualIDHolder;
import com.jogamp.nativewindow.CapabilitiesFilter.Test;
import com.jogamp.nativewindow.VisualIDHolder.VIDType;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLCapabilitiesChooser;
import com.jogamp.opengl.GLCapabilitiesFilter;
import com.jogamp.opengl.GLCapabilitiesImmutable;
import com.jogamp.opengl.GLContext;
import com.jogamp.opengl.GLDrawableFactory;
import com.jogamp.opengl.GLException;
import com.jogamp.opengl.GLProfile;

import jogamp.opengl.GLGraphicsConfigurationFactory;
import jogamp.opengl.GLGraphicsConfigurationUtil;

import com.jogamp.common.nio.Buffers;
import com.jogamp.common.nio.PointerBuffer;
import com.jogamp.nativewindow.egl.EGLGraphicsDevice;
import com.jogamp.opengl.GLRendererQuirks;
import com.jogamp.opengl.egl.EGL;


/** Subclass of GraphicsConfigurationFactory used when non-AWT tookits
    are used on X11 platforms. Toolkits will likely need to delegate
    to this one to change the accepted and returned types of the
    GraphicsDevice and GraphicsConfiguration abstractions. */

public class EGLGraphicsConfigurationFactory extends GLGraphicsConfigurationFactory {
    static VisualIDHolder.VIDComparator EglCfgIDComparator = new VisualIDHolder.VIDComparator(VisualIDHolder.VIDType.EGL_CONFIG);
    static GraphicsConfigurationFactory nativeGraphicsConfigurationFactory = null;
    static GraphicsConfigurationFactory kdeglGraphicsConfigurationFactory = null;
    static GraphicsConfigurationFactory fallbackGraphicsConfigurationFactory = null;

    static void registerFactory() {
        final GraphicsConfigurationFactory eglFactory = new EGLGraphicsConfigurationFactory();

        // become the pre-selector for X11/.. to match the native visual id w/ EGL, if native ES is selected
        final String nwType = NativeWindowFactory.getNativeWindowType(false);
        if(NativeWindowFactory.TYPE_X11 == nwType) {
            nativeGraphicsConfigurationFactory = GraphicsConfigurationFactory.registerFactory(com.jogamp.nativewindow.x11.X11GraphicsDevice.class, GLCapabilitiesImmutable.class, eglFactory);
            if(null != nativeGraphicsConfigurationFactory) {
                fallbackGraphicsConfigurationFactory = nativeGraphicsConfigurationFactory;
            } else {
                fallbackGraphicsConfigurationFactory = GraphicsConfigurationFactory.getFactory(com.jogamp.nativewindow.x11.X11GraphicsDevice.class, CapabilitiesImmutable.class);
            }
        } /* else if(NativeWindowFactory.TYPE_WINDOWS == NativeWindowFactory.getNativeWindowType(false)) {
            nativeGraphicsConfigurationFactory = GraphicsConfigurationFactory.registerFactory(com.jogamp.nativewindow.windows.WindowsGraphicsDevice.class, eglFactory);
        } else if(NativeWindowFactory.TYPE_MACOSX == NativeWindowFactory.getNativeWindowType(false)) {
        } */

        // become the selector for KD/EGL ..
        kdeglGraphicsConfigurationFactory = GraphicsConfigurationFactory.registerFactory(com.jogamp.nativewindow.egl.EGLGraphicsDevice.class, GLCapabilitiesImmutable.class, eglFactory);
    }

    static void unregisterFactory() {
        final String nwType = NativeWindowFactory.getNativeWindowType(false);
        if(NativeWindowFactory.TYPE_X11 == nwType) {
            GraphicsConfigurationFactory.registerFactory(com.jogamp.nativewindow.x11.X11GraphicsDevice.class, GLCapabilitiesImmutable.class, nativeGraphicsConfigurationFactory);
        } /* else if(NativeWindowFactory.TYPE_WINDOWS == NativeWindowFactory.getNativeWindowType(false)) {
            GraphicsConfigurationFactory.registerFactory(com.jogamp.nativewindow.windows.WindowsGraphicsDevice.class, nativeGraphicsConfigurationFactory);
        } else if(NativeWindowFactory.TYPE_MACOSX == NativeWindowFactory.getNativeWindowType(false)) {
        } */
        nativeGraphicsConfigurationFactory = null;
        fallbackGraphicsConfigurationFactory = null;

        GraphicsConfigurationFactory.registerFactory(com.jogamp.nativewindow.egl.EGLGraphicsDevice.class, GLCapabilitiesImmutable.class, kdeglGraphicsConfigurationFactory);
        kdeglGraphicsConfigurationFactory = null;
    }

    private EGLGraphicsConfigurationFactory() {
    }

    @Override
    protected AbstractGraphicsConfiguration chooseGraphicsConfigurationImpl (
            final CapabilitiesImmutable capsChosen, final CapabilitiesImmutable capsRequested,
            final CapabilitiesChooser chooser, final AbstractGraphicsScreen absScreen, final int nativeVisualID) {
        if (absScreen == null) {
            throw new IllegalArgumentException("This NativeWindowFactory accepts only AbstractGraphicsDevice objects");
        }

        if (! (capsChosen instanceof GLCapabilitiesImmutable) ) {
            throw new IllegalArgumentException("This NativeWindowFactory accepts only GLCapabilities objects - chosen");
        }
        final GLCapabilitiesImmutable glCapsChosen = (GLCapabilitiesImmutable) capsChosen;

        if (! (capsRequested instanceof GLCapabilitiesImmutable) ) {
            throw new IllegalArgumentException("This NativeWindowFactory accepts only GLCapabilities objects - requested");
        }

        if (chooser != null &&
            !(chooser instanceof GLCapabilitiesChooser)) {
            throw new IllegalArgumentException("This NativeWindowFactory accepts only GLCapabilitiesChooser objects");
        }

        final AbstractGraphicsDevice absDevice = absScreen.getDevice();
        if(null==absDevice) {
            throw new GLException("Null AbstractGraphicsDevice");
        }

        AbstractGraphicsConfiguration cfg = null;

        if( absDevice instanceof EGLGraphicsDevice ) {
            cfg = chooseGraphicsConfigurationStatic((GLCapabilitiesImmutable) capsChosen,
                                                    (GLCapabilitiesImmutable) capsRequested,
                                                    (GLCapabilitiesChooser) chooser,
                                                    absScreen, nativeVisualID, false);
        } else {
            // handle non native cases (X11, ..)
            if(null == fallbackGraphicsConfigurationFactory) {
                throw new InternalError("Native fallback GraphicsConfigurationFactory is null, but call issued for device: "+absDevice+" of type "+absDevice.getClass().getSimpleName());
            }

            if(glCapsChosen.getGLProfile().usesNativeGLES()) {
                if(DEBUG) {
                    System.err.println("EGLGraphicsConfigurationFactory.choose..: Handle native device "+absDevice.getClass().getSimpleName());
                }
                cfg = chooseGraphicsConfigurationStatic((GLCapabilitiesImmutable) capsChosen,
                                                        (GLCapabilitiesImmutable) capsRequested,
                                                        (GLCapabilitiesChooser) chooser,
                                                        absScreen, nativeVisualID, false);
                if(null == cfg || VisualIDHolder.VID_UNDEFINED == cfg.getVisualID(VIDType.NATIVE)) {
                    cfg = null;
                    if(DEBUG) {
                        System.err.println("EGLGraphicsConfigurationFactory.choose..: No native visual ID, fallback ..");
                    }
                }
            }
            if(null == cfg) {
                // fwd to native config factory (only X11 for now)
                if(DEBUG) {
                    System.err.println("EGLGraphicsConfigurationFactory.choose..: Delegate to "+fallbackGraphicsConfigurationFactory.getClass().getSimpleName());
                }
                cfg = fallbackGraphicsConfigurationFactory.chooseGraphicsConfiguration(capsChosen, capsRequested, chooser, absScreen, nativeVisualID);
            }
        }
        return cfg;
    }

    protected static List<GLCapabilitiesImmutable> getAvailableCapabilities(final EGLDrawableFactory factory, final AbstractGraphicsDevice device) {
        final EGLDrawableFactory.SharedResource sharedResource = factory.getOrCreateSharedResourceImpl(device);
        if(null == sharedResource) {
            throw new GLException("Shared resource for device n/a: "+device);
        }
        final EGLGraphicsDevice eglDevice = sharedResource.getDevice();
        final long eglDisplay = eglDevice.getHandle();
        if(0 == eglDisplay) {
            throw new GLException("null eglDisplay");
        }
        List<GLCapabilitiesImmutable> availableCaps = null;
        final IntBuffer numConfigs = Buffers.newDirectIntBuffer(1);

        if(!EGL.eglGetConfigs(eglDisplay, null, 0, numConfigs)) {
            throw new GLException("Graphics configuration get maxConfigs (eglGetConfigs) call failed, error "+toHexString(EGL.eglGetError()));
        }
        if(0 == numConfigs.get(0)) {
            throw new GLException("Graphics configuration get maxConfigs (eglGetConfigs) no configs");
        }

        final PointerBuffer configs = PointerBuffer.allocateDirect(numConfigs.get(0));

        if(!EGL.eglGetConfigs(eglDisplay, configs, configs.capacity(), numConfigs)) {
            throw new GLException("Graphics configuration get all configs (eglGetConfigs) call failed, error "+toHexString(EGL.eglGetError()));
        }
        if (numConfigs.get(0) > 0) {
            availableCaps = eglConfigs2GLCaps(eglDevice, null, configs, numConfigs.get(0), GLGraphicsConfigurationUtil.ALL_BITS, false /* forceTransparentFlag */, false /* onlyFirstValid */);
            if( null != availableCaps && availableCaps.size() > 1) {
                Collections.sort(availableCaps, EglCfgIDComparator);
            }
        }
        return availableCaps;
    }

    /**
     * Called mainly by {@link #chooseGraphicsConfigurationImpl(CapabilitiesImmutable, CapabilitiesImmutable, CapabilitiesChooser, AbstractGraphicsScreen, int)}
     * @param capsChosen     the intermediate chosen capabilities to be refined by this implementation, may be equal to capsRequested
     * @param capsReq        the original requested capabilities
     * @param chooser        the choosing implementation
     * @param absScreen      the referring Screen
     * @param nativeVisualID if not {@link VisualIDHolder#VID_UNDEFINED} it reflects a pre-chosen visualID of the native platform's windowing system.
     * @param forceTransparentFlag
     * @return               the complete GraphicsConfiguration
     * @see #chooseGraphicsConfigurationImpl(CapabilitiesImmutable, CapabilitiesImmutable, CapabilitiesChooser, AbstractGraphicsScreen, int)
     * @see GraphicsConfigurationFactory#chooseGraphicsConfiguration(CapabilitiesImmutable, CapabilitiesImmutable, CapabilitiesChooser, AbstractGraphicsScreen, int)
     */
    public static EGLGraphicsConfiguration chooseGraphicsConfigurationStatic(GLCapabilitiesImmutable capsChosen,
                                                                             final GLCapabilitiesImmutable capsReq,
                                                                             final GLCapabilitiesChooser chooser,
                                                                             final AbstractGraphicsScreen absScreen, final int nativeVisualID,
                                                                             final boolean forceTransparentFlag) {
        if (capsChosen == null) {
            capsChosen = new GLCapabilities(null);
        }

        if(null==absScreen) {
            throw new GLException("Null AbstractGraphicsScreen");
        }
        final AbstractGraphicsDevice absDevice = absScreen.getDevice();
        if(null==absDevice) {
            throw new GLException("Null AbstractGraphicsDevice");
        }

        final EGLGraphicsDevice eglDevice;
        final boolean ownEGLDisplay;
        if( absDevice instanceof EGLGraphicsDevice ) {
            eglDevice = (EGLGraphicsDevice) absDevice;
            if (eglDevice.getHandle() == EGL.EGL_NO_DISPLAY) {
                throw new GLException("Invalid EGL display: "+eglDevice);
            }
            ownEGLDisplay = false;
        } else {
            eglDevice = EGLDisplayUtil.eglCreateEGLGraphicsDevice(absDevice);
            eglDevice.open();
            ownEGLDisplay = true;
        }

        final GLProfile glp = capsChosen.getGLProfile();
        capsChosen = GLGraphicsConfigurationUtil.fixGLCapabilities( capsChosen, GLDrawableFactory.getEGLFactory(), absDevice);
        EGLGraphicsConfiguration res = eglChooseConfig(eglDevice, capsChosen, capsReq, chooser, absScreen, nativeVisualID, forceTransparentFlag);
        if(null==res) {
            if(DEBUG) {
                System.err.println("eglChooseConfig failed with given capabilities "+capsChosen);
            }

            // Last try .. add a fixed embedded profile [ATI, Nokia, Intel, ..]
            //
            // rgb888 - d16, s4
            final GLCapabilities fixedCaps = new GLCapabilities(glp);
            fixedCaps.setSampleBuffers(true);
            fixedCaps.setNumSamples(4);
            fixedCaps.setRedBits(8);
            fixedCaps.setGreenBits(8);
            fixedCaps.setBlueBits(8);
            fixedCaps.setDepthBits(16);
            if( !capsChosen.isOnscreen() ) {
                fixedCaps.setOnscreen(false);
                fixedCaps.setPBuffer(capsChosen.isPBuffer());
                fixedCaps.setFBO(capsChosen.isFBO());
            }
            if(DEBUG) {
                System.err.println("trying fixed caps (1): "+fixedCaps);
            }
            res = eglChooseConfig(eglDevice, fixedCaps, capsReq, chooser, absScreen, nativeVisualID, false);
        }
        if(null==res) {
            //
            // rgb565 - d16, s0
            final GLCapabilities fixedCaps = new GLCapabilities(glp);
            fixedCaps.setRedBits(5);
            fixedCaps.setGreenBits(6);
            fixedCaps.setBlueBits(5);
            fixedCaps.setDepthBits(16);
            if( !capsChosen.isOnscreen() ) {
                fixedCaps.setOnscreen(false);
                fixedCaps.setPBuffer(capsChosen.isPBuffer());
                fixedCaps.setFBO(capsChosen.isFBO());
            }
            if(DEBUG) {
                System.err.println("trying fixed caps (2): "+fixedCaps);
            }
            res = eglChooseConfig(eglDevice, fixedCaps, capsReq, chooser, absScreen, nativeVisualID, false);
        }
        if(null==res) {
            //
            // rgb565 - d16, s4
            final GLCapabilities fixedCaps = new GLCapabilities(glp);
            fixedCaps.setSampleBuffers(true);
            fixedCaps.setNumSamples(4);
            fixedCaps.setRedBits(5);
            fixedCaps.setGreenBits(6);
            fixedCaps.setBlueBits(5);
            fixedCaps.setDepthBits(16);
            if( !capsChosen.isOnscreen() ) {
                fixedCaps.setOnscreen(false);
                fixedCaps.setPBuffer(capsChosen.isPBuffer());
                fixedCaps.setFBO(capsChosen.isFBO());
            }
            if(DEBUG) {
                System.err.println("trying fixed caps (3): "+fixedCaps);
            }
            res = eglChooseConfig(eglDevice, fixedCaps, capsReq, chooser, absScreen, nativeVisualID, false);
        }
        if(null==res) {
            throw new GLException("Graphics configuration failed [direct caps, eglGetConfig/chooser and fixed-caps(1-3)]");
        }
        if(ownEGLDisplay) {
            ((EGLGLCapabilities) res.getChosenCapabilities()).setEGLConfig(0); // eglDisplay: EOL
            eglDevice.close();
        }
        return res;
    }


    static EGLGraphicsConfiguration eglChooseConfig(final EGLGraphicsDevice device,
                                                    final GLCapabilitiesImmutable capsChosen, final GLCapabilitiesImmutable capsRequested,
                                                    final GLCapabilitiesChooser chooser,
                                                    final AbstractGraphicsScreen absScreen,
                                                    final int nativeVisualID, final boolean forceTransparentFlag) {
        final long eglDisplay = device.getHandle();
        final GLProfile glp = capsChosen.getGLProfile();
        final GLRendererQuirks glrq = GLDrawableFactory.getEGLFactory().getRendererQuirks(device, glp);
        final int winattrmask = GLGraphicsConfigurationUtil.getExclusiveWinAttributeBits(capsChosen);
        ArrayList<GLCapabilitiesImmutable> availableCaps = null;
        int recommendedIndex = -1;
        final IntBuffer numConfigs = Buffers.newDirectIntBuffer(1);

        if(!EGL.eglGetConfigs(eglDisplay, null, 0, numConfigs)) {
            throw new GLException("EGLGraphicsConfiguration.eglChooseConfig: Get maxConfigs (eglGetConfigs) call failed, error "+toHexString(EGL.eglGetError()));
        }
        if(0 == numConfigs.get(0)) {
            throw new GLException("EGLGraphicsConfiguration.eglChooseConfig: Get maxConfigs (eglGetConfigs) no configs");
        }
        final int numEGLConfigs = numConfigs.get(0);
        if (DEBUG) {
            System.err.println("EGLGraphicsConfiguration.eglChooseConfig: eglChooseConfig eglDisplay "+toHexString(eglDisplay)+
                               ", nativeVisualID "+toHexString(nativeVisualID)+
                               ", capsChosen "+capsChosen+", winbits "+GLGraphicsConfigurationUtil.winAttributeBits2String(null, winattrmask).toString()+
                               ", fboAvail "+GLContext.isFBOAvailable(device, glp)+
                               ", device "+device+", "+device.getUniqueID()+
                               ", numEGLConfigs "+numEGLConfigs);
        }

        final IntBuffer attrs = EGLGraphicsConfiguration.GLCapabilities2AttribList(capsChosen);
        final PointerBuffer configs = PointerBuffer.allocateDirect(numConfigs.get(0));

        // 1st choice: get GLCapabilities based on users GLCapabilities
        //             setting recommendedIndex as preferred choice
        // skipped if nativeVisualID is given
        final boolean hasEGLChosenCaps;
        if( VisualIDHolder.VID_UNDEFINED == nativeVisualID ) {
            if( !EGL.eglChooseConfig(eglDisplay, attrs, configs, configs.capacity(), numConfigs) ) {
                if(DEBUG) {
                    System.err.println("EGLGraphicsConfiguration.eglChooseConfig: #1 eglChooseConfig: false");
                }
                numConfigs.put(0, 0);
                hasEGLChosenCaps = false;
            } else {
                hasEGLChosenCaps = numConfigs.get(0)>0;
            }
        } else {
            if(DEBUG) {
                System.err.println("EGLGraphicsConfiguration.eglChooseConfig: Skipped due to given visualID: "+toHexString(nativeVisualID));
            }
            hasEGLChosenCaps = false;
        }
        final boolean isPBufferOrBitmap = capsChosen.isBitmap() || capsChosen.isPBuffer();
        final boolean dontChooseFBConfigBestMatch = GLRendererQuirks.exist(glrq, GLRendererQuirks.DontChooseFBConfigBestMatch) ||
                                                    ( isPBufferOrBitmap && GLRendererQuirks.exist(glrq, GLRendererQuirks.No10BitColorCompOffscreen) );
        final boolean useRecommendedIndex = !dontChooseFBConfigBestMatch &&
                                            hasEGLChosenCaps && !forceTransparentFlag && capsChosen.isBackgroundOpaque();
        final boolean shallSkipCapsChooser = null == chooser && useRecommendedIndex;
        if( hasEGLChosenCaps ) {
            availableCaps = eglConfigs2GLCaps(device, glp, configs, numConfigs.get(0), winattrmask, forceTransparentFlag, shallSkipCapsChooser /* onlyFirsValid */);
            if(availableCaps.size() > 0) {
                if( useRecommendedIndex ) {
                    recommendedIndex = 0;
                }
                if (DEBUG) {
                    final long recommendedEGLConfig =  configs.get(0);
                    System.err.println("EGLGraphicsConfiguration.eglChooseConfig: #1 eglChooseConfig: recommended fbcfg " + toHexString(recommendedEGLConfig) + ", idx " + recommendedIndex);
                    System.err.println("EGLGraphicsConfiguration.eglChooseConfig: #1 useRecommendedIndex "+useRecommendedIndex+", shallSkipCapsChooser "+shallSkipCapsChooser);
                    if( 0 <= recommendedIndex ) {
                        System.err.println("EGLGraphicsConfiguration.eglChooseConfig: #1 fbcfg recommended caps " + availableCaps.get(recommendedIndex));
                    }
                }
            } else if (DEBUG) {
                System.err.println("EGLGraphicsConfiguration.eglChooseConfig: #1 eglChooseConfig: no caps for recommended fbcfg " + toHexString(configs.get(0)));
                System.err.println("EGLGraphicsConfiguration.eglChooseConfig: #1 useRecommendedIndex "+useRecommendedIndex+", shallSkipCapsChooser "+shallSkipCapsChooser);
            }
        } else if (DEBUG) {
            System.err.println("EGLGraphicsConfiguration.eglChooseConfig: #1 eglChooseConfig: no configs");
            System.err.println("EGLGraphicsConfiguration.eglChooseConfig: #1 useRecommendedIndex "+useRecommendedIndex+", shallSkipCapsChooser "+shallSkipCapsChooser);
        }

        // 2nd choice: get all GLCapabilities available, no preferred recommendedIndex available
        if( null == availableCaps || 0 == availableCaps.size() ) {
            // reset ..
            recommendedIndex = -1;

            if(!EGL.eglGetConfigs(eglDisplay, configs, configs.capacity(), numConfigs)) {
                throw new GLException("EGLGraphicsConfiguration.eglChooseConfig: #2 Get all configs (eglGetConfigs) call failed, error "+toHexString(EGL.eglGetError()));
            }
            if (numConfigs.get(0) > 0) {
                availableCaps = eglConfigs2GLCaps(device, glp, configs, numConfigs.get(0), winattrmask, forceTransparentFlag, false /* onlyFirsValid */);
            }
        }

        if( null == availableCaps || 0 == availableCaps.size() ) {
            if(DEBUG) {
                // FIXME: this happens on a ATI PC Emulation ..
                System.err.println("EGLGraphicsConfiguration.eglChooseConfig: #2 Graphics configuration 1st choice and 2nd choice failed - no configs");
                availableCaps = eglConfigs2GLCaps(device, glp, configs, numConfigs.get(0), GLGraphicsConfigurationUtil.ALL_BITS, forceTransparentFlag, false /* onlyFirsValid */);
                printCaps("AllCaps", availableCaps, System.err);
            }
            return null;
        }

        final boolean skipCapsChooser = shallSkipCapsChooser && 0 <= recommendedIndex;
        if(DEBUG) {
            System.err.println("EGLGraphicsConfiguration.eglChooseConfig: got configs: "+availableCaps.size());
            for(int i=0; i<availableCaps.size(); i++) {
                System.err.println(i+": "+availableCaps.get(i));
            }
        }
        // Filter availableCaps
        {
            final ArrayList<Test<GLCapabilitiesImmutable>> criteria = new ArrayList<Test<GLCapabilitiesImmutable>>();
            if( !skipCapsChooser && isPBufferOrBitmap && GLRendererQuirks.exist(glrq, GLRendererQuirks.No10BitColorCompOffscreen) ) {
                criteria.add(new CapabilitiesFilter.TestMoreColorCompBits<GLCapabilitiesImmutable>(8));
            }
            if( VisualIDHolder.VID_UNDEFINED != nativeVisualID) {
                criteria.add(new CapabilitiesFilter.TestUnmatchedNativeVisualID<GLCapabilitiesImmutable>(nativeVisualID));
            }
            if( 0 < capsChosen.getDepthBits() ) {
                // Hack for HiSilicon/Vivante/Immersion.16 Renderer ..
                criteria.add(new GLCapabilitiesFilter.TestLessDepthBits<GLCapabilitiesImmutable>(1));
            }
            if( criteria.size() > 0 ) {
                final ArrayList<GLCapabilitiesImmutable> removedCaps = CapabilitiesFilter.removeMatching(availableCaps, criteria);
                if( removedCaps.size() > 0 ) {
                    if(DEBUG) {
                        System.err.println("EGLGraphicsConfiguration.eglChooseConfig: filtered configs: "+availableCaps.size());
                        for(int i=0; i<availableCaps.size(); i++) {
                            System.err.println(i+": "+availableCaps.get(i));
                        }
                    }
                }
                if(0==availableCaps.size()) {
                    availableCaps = removedCaps;
                    if(DEBUG) {
                        System.err.println("EGLGraphicsConfiguration.eglChooseConfig: post filter visualID "+toHexString(nativeVisualID )+" no config found, revert to all");
                    }
                }
            }
        }

        final int chosenIndex;
        if( skipCapsChooser ) {
            chosenIndex = recommendedIndex;
        } else {
            chosenIndex = chooseCapabilities(chooser, capsChosen, availableCaps, recommendedIndex);
        }
        if ( 0 > chosenIndex ) {
            if (DEBUG) {
                System.err.println("EGLGraphicsConfiguration.eglChooseConfig: #2 chooseCapabilities failed");
            }
            return null;
        }
        final EGLGLCapabilities chosenCaps = (EGLGLCapabilities) availableCaps.get(chosenIndex);
        final EGLGraphicsConfiguration res = new EGLGraphicsConfiguration(absScreen, chosenCaps, capsRequested, chooser);
        if (DEBUG) {
            System.err.println("EGLGraphicsConfiguration.eglChooseConfig: X chosen :"+chosenIndex+", eglConfig: "+toHexString(chosenCaps.getEGLConfig())+": "+res);
        }
        return res;
    }

    static ArrayList<GLCapabilitiesImmutable> eglConfigs2GLCaps(final EGLGraphicsDevice device, final GLProfile glp, final PointerBuffer configs, final int num, final int winattrmask, final boolean forceTransparentFlag, final boolean onlyFirstValid) {
        final GLRendererQuirks defaultQuirks = GLRendererQuirks.getStickyDeviceQuirks( GLDrawableFactory.getEGLFactory().getDefaultDevice() );
        final ArrayList<GLCapabilitiesImmutable> bucket = new ArrayList<GLCapabilitiesImmutable>(num);
        for(int i=0; i<num; i++) {
            final GLCapabilitiesImmutable caps = EGLGraphicsConfiguration.EGLConfig2Capabilities(defaultQuirks, device, glp, configs.get(i), winattrmask, forceTransparentFlag);
            if(null != caps) {
                bucket.add(caps);
                if(onlyFirstValid) {
                    break;
                }
            }
        }
        return bucket;
    }

    static void printCaps(final String prefix, final List<GLCapabilitiesImmutable> caps, final PrintStream out) {
        for(int i=0; i<caps.size(); i++) {
            out.println(prefix+"["+i+"] "+caps.get(i));
        }
    }
}