001: /*
002: * $RCSfile: X11NativeConfigTemplate3D.java,v $
003: *
004: * Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.2 $
028: * $Date: 2008/02/28 20:17:57 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import java.awt.GraphicsDevice;
035: import java.awt.GraphicsConfiguration;
036: import java.awt.GraphicsConfigTemplate;
037: import java.awt.Rectangle;
038: import sun.awt.X11GraphicsDevice;
039: import sun.awt.X11GraphicsConfig;
040:
041: /**
042: * Native config template class. A singleton instance of this class is
043: * created by a factory method in the base class using reflection.
044: */
045: class X11NativeConfigTemplate3D extends NativeConfigTemplate3D {
046: private final static boolean debug = false;
047:
048: X11NativeConfigTemplate3D() {
049: }
050:
051: // Native method to get an OpenGL visual id and a pointer to the
052: // GLXFBConfig structure list itself.
053: native int chooseOglVisual(long display, int screen,
054: int[] attrList, long[] fbConfig);
055:
056: // Native method to free an GLXFBConfig struct. This is static since it
057: // may need to be called to clean up the Canvas3D graphicsConfigTable
058: // after the X11NativeConfigTemplate3D has been disposed of.
059: static native void freeFBConfig(long fbConfig);
060:
061: // Native methods to return whether a particular attribute is available
062: native boolean isStereoAvailable(long display, int screen, int vid);
063:
064: native boolean isDoubleBufferAvailable(long display, int screen,
065: int vid);
066:
067: native boolean isSceneAntialiasingAccumAvailable(long display,
068: int screen, int vid);
069:
070: native boolean isSceneAntialiasingMultisampleAvailable(
071: long display, int screen, int vid);
072:
073: native int getStencilSize(long display, int screen, int vid);
074:
075: /*
076: * Chooses the best FBConfig for Java 3D apps.
077: */
078: @Override
079: GraphicsConfiguration getBestConfiguration(
080: GraphicsConfigTemplate3D template,
081: GraphicsConfiguration[] gc) {
082:
083: X11GraphicsDevice gd = (X11GraphicsDevice) ((X11GraphicsConfig) gc[0])
084: .getDevice();
085:
086: if (!X11NativeScreenInfo.isGLX13()) {
087: return null;
088: }
089:
090: long display = NativeScreenInfo.getNativeScreenInfo()
091: .getDisplay();
092: int screen = NativeScreenInfo.getNativeScreenInfo().getScreen(
093: gd);
094:
095: if (debug) {
096: System.out
097: .println(" X11NativeConfigTemplate3D: using device "
098: + gd);
099: System.out.println(" display " + display + " screen "
100: + screen);
101: System.out.println(" configuration count: " + gc.length);
102: for (int i = 0; i < gc.length; i++) {
103: System.out.println(" visual id at index " + i
104: + ": "
105: + ((X11GraphicsConfig) gc[i]).getVisual());
106: }
107: }
108:
109: Rectangle bounds = gc[0].getBounds();
110: if ((bounds.x != 0 || bounds.y != 0)
111: && (!VirtualUniverse.mc.xineramaDisabled)) {
112: // Xinerama is being used. The screen needs to be set to 0 since
113: // glxChooseFBConfig will not return a valid visual otherwise.
114: screen = 0;
115: if (debug) {
116: System.out.println(" Non-primary Xinerama screen:");
117: System.out.println(" bounds = " + bounds);
118: System.out.println(" using screen 0 visual");
119: }
120: }
121:
122: int[] attrList; // holds the list of attributes to be translated
123: // for glxChooseFBConfig call
124:
125: attrList = new int[NUM_ITEMS];
126:
127: // assign template values to array
128: attrList[RED_SIZE] = template.getRedSize();
129: attrList[GREEN_SIZE] = template.getGreenSize();
130: attrList[BLUE_SIZE] = template.getBlueSize();
131:
132: attrList[DEPTH_SIZE] = template.getDepthSize();
133: attrList[DOUBLEBUFFER] = template.getDoubleBuffer();
134: attrList[STEREO] = template.getStereo();
135: attrList[ANTIALIASING] = template.getSceneAntialiasing();
136: attrList[STENCIL_SIZE] = template.getStencilSize();
137: // System.out.println("X11NativeConfigTemplate3D : getStencilSize " +
138: // attrList[STENCIL_SIZE]);
139:
140: long[] fbConfig = new long[1];
141: int visID = chooseOglVisual(display, screen, attrList, fbConfig);
142: if (debug) {
143: System.out.println(" chooseOglVisual() returns " + visID);
144: System.out.println(" pointer to GLXFBConfig is "
145: + fbConfig[0]);
146: System.out.println();
147: }
148:
149: if (visID == 0 || fbConfig[0] == 0) {
150: return null; // no valid visual was found
151: }
152:
153: // search list of graphics configurations for config
154: // with matching visualId
155: X11GraphicsConfig gc0 = null;
156: for (int i = 0; i < gc.length; i++) {
157: if (((X11GraphicsConfig) gc[i]).getVisual() == visID) {
158: gc0 = (X11GraphicsConfig) gc[i];
159: break;
160: }
161: }
162:
163: // Just return if we didn't find a match
164: if (gc0 == null) {
165: return null;
166: }
167:
168: // Create a new GraphicsConfig object based on the one we found
169: X11GraphicsConfig gc1 = X11GraphicsConfig.getConfig(gd, gc0
170: .getVisual(), gc0.getDepth(), gc0.getColormap(), false);
171:
172: // We need to cache the GraphicsTemplate3D and the private
173: // fbconfig info.
174: synchronized (Canvas3D.graphicsConfigTable) {
175: if (Canvas3D.graphicsConfigTable.get(gc1) == null) {
176: GraphicsConfigInfo gcInfo = new GraphicsConfigInfo(
177: template);
178: gcInfo.setPrivateData(new Long(fbConfig[0]));
179: Canvas3D.graphicsConfigTable.put(gc1, gcInfo);
180: } else {
181: freeFBConfig(fbConfig[0]);
182: }
183: }
184: return gc1;
185: }
186:
187: /*
188: * Determine if a given GraphicsConfiguration object can be used
189: * by Java 3D.
190: */
191: @Override
192: boolean isGraphicsConfigSupported(
193: GraphicsConfigTemplate3D template, GraphicsConfiguration gc) {
194:
195: X11GraphicsDevice gd = (X11GraphicsDevice) ((X11GraphicsConfig) gc)
196: .getDevice();
197:
198: if (!X11NativeScreenInfo.isGLX13()) {
199: return false;
200: }
201:
202: long display = NativeScreenInfo.getNativeScreenInfo()
203: .getDisplay();
204: int screen = NativeScreenInfo.getNativeScreenInfo().getScreen(
205: gd);
206:
207: int[] attrList; // holds the list of attributes to be tramslated
208: // for glxChooseVisual call
209:
210: attrList = new int[NUM_ITEMS];
211:
212: // assign template values to array
213: attrList[RED_SIZE] = template.getRedSize();
214: attrList[GREEN_SIZE] = template.getGreenSize();
215: attrList[BLUE_SIZE] = template.getBlueSize();
216:
217: attrList[DEPTH_SIZE] = template.getDepthSize();
218: attrList[DOUBLEBUFFER] = template.getDoubleBuffer();
219: attrList[STEREO] = template.getStereo();
220: attrList[ANTIALIASING] = template.getSceneAntialiasing();
221: attrList[STENCIL_SIZE] = template.getStencilSize();
222: // System.out.println("X11NativeConfigTemplate3D : getStencilSize " +
223: // attrList[STENCIL_SIZE]);
224:
225: long[] fbConfig = new long[1];
226: int visID = chooseOglVisual(display, screen, attrList, fbConfig);
227:
228: if (visID == 0 || fbConfig[0] == 0)
229: return false; // no valid visual was found
230: else
231: return true;
232: }
233:
234: // Return whether stereo is available.
235: @Override
236: boolean hasStereo(Canvas3D c3d) {
237: GraphicsConfiguration gc = c3d.graphicsConfiguration;
238:
239: X11GraphicsDevice gd = (X11GraphicsDevice) ((X11GraphicsConfig) gc)
240: .getDevice();
241:
242: long display = NativeScreenInfo.getNativeScreenInfo()
243: .getDisplay();
244: int screen = NativeScreenInfo.getNativeScreenInfo().getScreen(
245: gd);
246: int vid = ((X11GraphicsConfig) gc).getVisual();
247:
248: return isStereoAvailable(display, screen, vid);
249: }
250:
251: // Return the stencil of this canvas.
252: @Override
253: int getStencilSize(Canvas3D c3d) {
254: GraphicsConfiguration gc = c3d.graphicsConfiguration;
255:
256: X11GraphicsDevice gd = (X11GraphicsDevice) ((X11GraphicsConfig) gc)
257: .getDevice();
258:
259: long display = NativeScreenInfo.getNativeScreenInfo()
260: .getDisplay();
261: int screen = NativeScreenInfo.getNativeScreenInfo().getScreen(
262: gd);
263: int vid = ((X11GraphicsConfig) gc).getVisual();
264:
265: return getStencilSize(display, screen, vid);
266: }
267:
268: // Return whether a double buffer is available.
269: @Override
270: boolean hasDoubleBuffer(Canvas3D c3d) {
271: GraphicsConfiguration gc = c3d.graphicsConfiguration;
272:
273: X11GraphicsDevice gd = (X11GraphicsDevice) ((X11GraphicsConfig) gc)
274: .getDevice();
275:
276: long display = NativeScreenInfo.getNativeScreenInfo()
277: .getDisplay();
278: int screen = NativeScreenInfo.getNativeScreenInfo().getScreen(
279: gd);
280: int vid = ((X11GraphicsConfig) gc).getVisual();
281:
282: return isDoubleBufferAvailable(display, screen, vid);
283: }
284:
285: // Return whether scene antialiasing is available.
286: @Override
287: boolean hasSceneAntialiasingAccum(Canvas3D c3d) {
288: GraphicsConfiguration gc = c3d.graphicsConfiguration;
289:
290: X11GraphicsDevice gd = (X11GraphicsDevice) ((X11GraphicsConfig) gc)
291: .getDevice();
292:
293: long display = NativeScreenInfo.getNativeScreenInfo()
294: .getDisplay();
295: int screen = NativeScreenInfo.getNativeScreenInfo().getScreen(
296: gd);
297: int vid = ((X11GraphicsConfig) gc).getVisual();
298:
299: return isSceneAntialiasingAccumAvailable(display, screen, vid);
300: }
301:
302: // Return whether scene antialiasing is available.
303: @Override
304: boolean hasSceneAntialiasingMultisample(Canvas3D c3d) {
305: GraphicsConfiguration gc = c3d.graphicsConfiguration;
306:
307: X11GraphicsDevice gd = (X11GraphicsDevice) ((X11GraphicsConfig) gc)
308: .getDevice();
309:
310: long display = NativeScreenInfo.getNativeScreenInfo()
311: .getDisplay();
312: int screen = NativeScreenInfo.getNativeScreenInfo().getScreen(
313: gd);
314: int vid = ((X11GraphicsConfig) gc).getVisual();
315:
316: return isSceneAntialiasingMultisampleAvailable(display, screen,
317: vid);
318: }
319:
320: // Ensure that the native libraries are loaded
321: static {
322: VirtualUniverse.loadLibraries();
323: }
324: }
|