capabilities = new GLCapabilities();
}
if (chooser == null) {
chooser = new DefaultGLCapabilitiesChooser();
}
GraphicsDevice device = null;
if (absDevice != null &&
!(absDevice instanceof AWTGraphicsDevice)) {
throw new IllegalArgumentException("This GLDrawableFactory accepts only AWTGraphicsDevice objects");
}
if ((absDevice == null) ||
(((AWTGraphicsDevice) absDevice).getGraphicsDevice() == null)) {
device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
} else {
device = ((AWTGraphicsDevice) absDevice).getGraphicsDevice();
}
int screen;
if (isXineramaEnabled()) {
screen = 0;
} else {
screen = X11SunJDKReflection.graphicsDeviceGetScreen(device);
}
// Until we have a rock-solid visual selection algorithm written
// in pure Java, we're going to provide the underlying window
// system's selection to the chooser as a hint
int[] attribs = glCapabilities2AttribList(capabilities, isMultisampleAvailable(), false, 0, 0);
XVisualInfo[] infos = null;
GLCapabilities[] caps = null;
int recommendedIndex = -1;
lockToolkit();
try {
long display = getDisplayConnection();
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 0x" + Long.toHexString(recommendedVis.visualid()));
}
}
int[] count = new int[1];
XVisualInfo template = XVisualInfo.create();
template.screen(screen);
infos = GLX.XGetVisualInfo(display, GLX.VisualScreenMask, template, count, 0);
if (infos == null) {
throw new GLException("Error while enumerating available XVisualInfos");
}
caps = new GLCapabilities[infos.length];
for (int i = 0; i < infos.length; i++) {
caps[i] = xvi2GLCapabilities(display, infos[i]);
// Attempt to find the visual chosen by glXChooseVisual
if (recommendedVis != null && recommendedVis.visualid() == infos[i].visualid()) {
recommendedIndex = i;
}
}
} finally {
unlockToolkit();
}
// Store these away for later
for (int i = 0; i < infos.length; i++) {
if (caps[i] != null) {
visualToGLCapsMap.put(new ScreenAndVisualIDKey(screen, infos[i].visualid()),
caps[i].clone());
}
}
int chosen = chooser.chooseCapabilities(capabilities, caps, recommendedIndex);
if (chosen < 0 || chosen >= caps.length) {
throw new GLException("GLCapabilitiesChooser specified invalid index (expected 0.." + (caps.length - 1) + ")");
}
XVisualInfo vis = infos[chosen];
if (vis == null) {
throw new GLException("GLCapabilitiesChooser chose an invalid visual");
}
// FIXME: need to look at glue code and see type of this field
long visualID = vis.visualid();
// FIXME: the storage for the infos array, as well as that for the
// recommended visual, is leaked; should free them here with XFree()
// Now figure out which GraphicsConfiguration corresponds to this
// visual by matching the visual ID
GraphicsConfiguration[] configs = device.getConfigurations();
for (int i = 0; i < configs.length; i++) {
GraphicsConfiguration config = configs[i];
if (config != null) {
if (X11SunJDKReflection.graphicsConfigurationGetVisualID(config) == visualID) {
return new AWTGraphicsConfiguration(config);