private static X11GLXGraphicsConfiguration chooseGraphicsConfigurationFBConfig(
      GLCapabilitiesImmutable capsChosen,
      GLCapabilitiesImmutable capsReq,
      GLCapabilitiesChooser chooser,
      X11GraphicsScreen x11Screen) {
    int recommendedIndex = -1;
    PointerBuffer fbcfgsL = null;
    GLProfile glProfile = capsChosen.getGLProfile();
    boolean onscreen = capsChosen.isOnscreen();
    boolean usePBuffer = capsChosen.isPBuffer();

    // Utilizing FBConfig
    //
    AbstractGraphicsDevice absDevice = x11Screen.getDevice();
    long display = absDevice.getHandle();
    int screen = x11Screen.getIndex();

    final X11GLXDrawableFactory factory =
        (X11GLXDrawableFactory) GLDrawableFactory.getDesktopFactory();
    final boolean isMultisampleAvailable = factory.isGLXMultisampleAvailable(absDevice);
    int[] attribs =
        X11GLXGraphicsConfiguration.GLCapabilities2AttribList(
            capsChosen, true, isMultisampleAvailable, display, screen);
    int[] count = {-1};
    ArrayList /*<X11GLCapabilities>*/ availableCaps = new ArrayList();
    final int winattrmask = GLGraphicsConfigurationUtil.getWinAttributeBits(onscreen, usePBuffer);

    // 1st choice: get GLCapabilities based on users GLCapabilities setting recommendedIndex as
    // preferred choice
    fbcfgsL = GLX.glXChooseFBConfig(display, screen, attribs, 0, count, 0);
    if (fbcfgsL != null && fbcfgsL.limit() > 0) {
      for (int i = 0; i < fbcfgsL.limit(); i++) {
        if (!X11GLXGraphicsConfiguration.GLXFBConfig2GLCapabilities(
            availableCaps,
            glProfile,
            display,
            fbcfgsL.get(i),
            winattrmask,
            isMultisampleAvailable)) {
          if (DEBUG) {
            System.err.println(
                "X11GLXGraphicsConfiguration.chooseGraphicsConfigurationFBConfig: FBConfig invalid (1): ("
                    + x11Screen
                    + ","
                    + capsChosen
                    + "): fbcfg: "
                    + toHexString(fbcfgsL.get(i)));
          }
        }
      }
      if (availableCaps.size() > 0) {
        recommendedIndex =
            capsChosen.isBackgroundOpaque() ? 0 : -1; // only use recommended idx if not translucent
        if (DEBUG) {
          System.err.println(
              "glXChooseFBConfig recommended fbcfg "
                  + toHexString(fbcfgsL.get(0))
                  + ", idx "
                  + recommendedIndex);
          System.err.println("user  caps " + capsChosen);
          System.err.println("fbcfg caps " + availableCaps.get(0));
        }
      } else if (DEBUG) {
        System.err.println(
            "glXChooseFBConfig no caps for recommended fbcfg " + toHexString(fbcfgsL.get(0)));
        System.err.println("user  caps " + capsChosen);
      }
    }

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

      fbcfgsL = GLX.glXChooseFBConfig(display, screen, null, 0, count, 0);
      if (fbcfgsL == null || fbcfgsL.limit() <= 0) {
        if (DEBUG) {
          System.err.println(
              "X11GLXGraphicsConfiguration.chooseGraphicsConfigurationFBConfig: Failed glXChooseFBConfig ("
                  + x11Screen
                  + ","
                  + capsChosen
                  + "): "
                  + fbcfgsL
                  + ", "
                  + count[0]);
        }
        return null;
      }

      for (int i = 0; i < fbcfgsL.limit(); i++) {
        if (!X11GLXGraphicsConfiguration.GLXFBConfig2GLCapabilities(
            availableCaps,
            glProfile,
            display,
            fbcfgsL.get(i),
            winattrmask,
            isMultisampleAvailable)) {
          if (DEBUG) {
            System.err.println(
                "X11GLXGraphicsConfiguration.chooseGraphicsConfigurationFBConfig: FBConfig invalid (2): ("
                    + x11Screen
                    + "): fbcfg: "
                    + toHexString(fbcfgsL.get(i)));
          }
        }
      }
    }
    int chosenIndex = chooseCapabilities(chooser, capsChosen, availableCaps, recommendedIndex);
    if (0 > chosenIndex) {
      if (DEBUG) {
        System.err.println(
            "X11GLXGraphicsConfiguration.chooseGraphicsConfigurationFBConfig: failed, return null");
        Thread.dumpStack();
      }
      return null;
    }
    X11GLCapabilities chosenCaps = (X11GLCapabilities) availableCaps.get(chosenIndex);

    return new X11GLXGraphicsConfiguration(x11Screen, chosenCaps, capsReq, chooser);
  }
  private static X11GLXGraphicsConfiguration chooseGraphicsConfigurationXVisual(
      GLCapabilitiesImmutable capsChosen,
      GLCapabilitiesImmutable capsReq,
      GLCapabilitiesChooser chooser,
      X11GraphicsScreen x11Screen) {
    if (chooser == null) {
      chooser = new DefaultGLCapabilitiesChooser();
    }

    GLProfile glProfile = capsChosen.getGLProfile();
    final int winattrmask =
        GLGraphicsConfigurationUtil.getWinAttributeBits(
            capsChosen.isOnscreen(), false /* pbuffer */);
    ArrayList availableCaps = new ArrayList();
    int recommendedIndex = -1;

    AbstractGraphicsDevice absDevice = x11Screen.getDevice();
    long display = absDevice.getHandle();
    int screen = x11Screen.getIndex();

    final X11GLXDrawableFactory factory =
        (X11GLXDrawableFactory) GLDrawableFactory.getDesktopFactory();
    final boolean isMultisampleAvailable = factory.isGLXMultisampleAvailable(absDevice);
    int[] attribs =
        X11GLXGraphicsConfiguration.GLCapabilities2AttribList(
            capsChosen, false, isMultisampleAvailable, display, screen);

    // 1st choice: get GLCapabilities based on users GLCapabilities setting recommendedIndex as
    // preferred choice
    XVisualInfo recommendedVis = GLX.glXChooseVisual(display, screen, attribs, 0);
    if (DEBUG) {
      System.err.print("glXChooseVisual recommended ");
      if (recommendedVis == null) {
        System.err.println("null visual");
      } else {
        System.err.println("visual id " + toHexString(recommendedVis.getVisualid()));
      }
    }

    // 2nd choice: get all GLCapabilities available, preferred recommendedIndex might be available
    // if 1st choice was successful
    int[] count = new int[1];
    XVisualInfo template = XVisualInfo.create();
    template.setScreen(screen);
    XVisualInfo[] infos =
        X11Lib.XGetVisualInfo(display, X11Lib.VisualScreenMask, template, count, 0);
    if (infos == null || infos.length < 1) {
      throw new GLException("Error while enumerating available XVisualInfos");
    }

    for (int i = 0; i < infos.length; i++) {
      if (!X11GLXGraphicsConfiguration.XVisualInfo2GLCapabilities(
          availableCaps, glProfile, display, infos[i], winattrmask, isMultisampleAvailable)) {
        if (DEBUG) {
          System.err.println(
              "X11GLXGraphicsConfiguration.chooseGraphicsConfigurationXVisual: XVisual invalid: ("
                  + x11Screen
                  + "): fbcfg: "
                  + toHexString(infos[i].getVisualid()));
        }
      } else {
        // Attempt to find the visual chosenIndex by glXChooseVisual, if not translucent
        if (capsChosen.isBackgroundOpaque()
            && recommendedVis != null
            && recommendedVis.getVisualid() == infos[i].getVisualid()) {
          recommendedIndex = availableCaps.size() - 1;
        }
      }
    }

    int chosenIndex = chooseCapabilities(chooser, capsChosen, availableCaps, recommendedIndex);
    if (0 > chosenIndex) {
      if (DEBUG) {
        System.err.println(
            "X11GLXGraphicsConfiguration.chooseGraphicsConfigurationXVisual: failed, return null");
        Thread.dumpStack();
      }
      return null;
    }
    X11GLCapabilities chosenCaps = (X11GLCapabilities) availableCaps.get(chosenIndex);

    return new X11GLXGraphicsConfiguration(x11Screen, chosenCaps, capsReq, chooser);
  }
  public static IntBuffer GLCapabilities2AttribList(GLCapabilitiesImmutable caps) {
    final IntBuffer attrs = Buffers.newDirectIntBuffer(32);
    int idx = 0;

    attrs.put(idx++, EGL.EGL_SURFACE_TYPE);
    final int surfaceType;
    if (caps.isOnscreen()) {
      surfaceType = EGL.EGL_WINDOW_BIT;
    } else if (caps.isFBO()) {
      surfaceType = EGL.EGL_PBUFFER_BIT; // native replacement!
    } else if (caps.isPBuffer()) {
      surfaceType = EGL.EGL_PBUFFER_BIT;
    } else if (caps.isBitmap()) {
      surfaceType = EGL.EGL_PIXMAP_BIT;
    } else {
      throw new GLException("no surface type set in caps: " + caps);
    }
    attrs.put(idx++, surfaceType);

    attrs.put(idx++, EGL.EGL_RED_SIZE);
    attrs.put(idx++, caps.getRedBits());

    attrs.put(idx++, EGL.EGL_GREEN_SIZE);
    attrs.put(idx++, caps.getGreenBits());

    attrs.put(idx++, EGL.EGL_BLUE_SIZE);
    attrs.put(idx++, caps.getBlueBits());

    if (caps.getAlphaBits() > 0) {
      attrs.put(idx++, EGL.EGL_ALPHA_SIZE);
      attrs.put(idx++, caps.getAlphaBits());
    }

    if (caps.getStencilBits() > 0) {
      attrs.put(idx++, EGL.EGL_STENCIL_SIZE);
      attrs.put(idx++, caps.getStencilBits());
    }

    attrs.put(idx++, EGL.EGL_DEPTH_SIZE);
    attrs.put(idx++, caps.getDepthBits());

    if (caps.getSampleBuffers()) {
      if (caps.getSampleExtension().equals(GLGraphicsConfigurationUtil.NV_coverage_sample)) {
        attrs.put(idx++, EGLExt.EGL_COVERAGE_BUFFERS_NV);
        attrs.put(idx++, 1);
        attrs.put(idx++, EGLExt.EGL_COVERAGE_SAMPLES_NV);
        attrs.put(idx++, caps.getNumSamples());
      } else {
        // try default ..
        attrs.put(idx++, EGL.EGL_SAMPLE_BUFFERS);
        attrs.put(idx++, 1);
        attrs.put(idx++, EGL.EGL_SAMPLES);
        attrs.put(idx++, caps.getNumSamples());
      }
    }

    attrs.put(idx++, EGL.EGL_TRANSPARENT_TYPE);
    attrs.put(idx++, caps.isBackgroundOpaque() ? EGL.EGL_NONE : EGL.EGL_TRANSPARENT_TYPE);

    // 22

    if (!caps.isBackgroundOpaque()) {
      attrs.put(idx++, EGL.EGL_TRANSPARENT_RED_VALUE);
      attrs.put(
          idx++,
          caps.getTransparentRedValue() >= 0 ? caps.getTransparentRedValue() : EGL.EGL_DONT_CARE);

      attrs.put(idx++, EGL.EGL_TRANSPARENT_GREEN_VALUE);
      attrs.put(
          idx++,
          caps.getTransparentGreenValue() >= 0
              ? caps.getTransparentGreenValue()
              : EGL.EGL_DONT_CARE);

      attrs.put(idx++, EGL.EGL_TRANSPARENT_BLUE_VALUE);
      attrs.put(
          idx++,
          caps.getTransparentBlueValue() >= 0 ? caps.getTransparentBlueValue() : EGL.EGL_DONT_CARE);

      /**
       * Not define in EGL attrs.put(idx++, EGL.EGL_TRANSPARENT_ALPHA_VALUE; attrs.put(idx++,
       * caps.getTransparentAlphaValue()>=0?caps.getTransparentAlphaValue():EGL.EGL_DONT_CARE;
       */
    }

    // 28
    attrs.put(idx++, EGL.EGL_RENDERABLE_TYPE);
    if (caps.getGLProfile().usesNativeGLES1()) {
      attrs.put(idx++, EGL.EGL_OPENGL_ES_BIT);
    } else if (caps.getGLProfile().usesNativeGLES2()) {
      attrs.put(idx++, EGL.EGL_OPENGL_ES2_BIT);
    } else if (caps.getGLProfile().usesNativeGLES3()) {
      if (GLRendererQuirks.existStickyDeviceQuirk(
          GLDrawableFactory.getEGLFactory().getDefaultDevice(),
          GLRendererQuirks.GLES3ViaEGLES2Config)) {
        attrs.put(idx++, EGL.EGL_OPENGL_ES2_BIT);
      } else {
        attrs.put(idx++, EGLExt.EGL_OPENGL_ES3_BIT_KHR);
      }
    } else {
      attrs.put(idx++, EGL.EGL_OPENGL_BIT);
    }

    // 30

    attrs.put(idx++, EGL.EGL_NONE);

    return attrs;
  }