Update of /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/opengl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10507/src/java/org/lwjgl/opengl Modified Files: AWTGLCanvas.java Display.java DisplayImplementation.java LinuxDisplay.java MacOSXDisplay.java Pbuffer.java Win32Display.java Added Files: AWTCanvasImplementation.java AWTSurfaceLock.java Context.java ContextImplementation.java Drawable.java LinuxAWTGLCanvasPeerInfo.java LinuxCanvasImplementation.java LinuxContextImplementation.java LinuxDisplayPeerInfo.java LinuxPeerInfo.java PeerInfo.java Log Message: Linux support for AWTGLCanvas. Big refactor of context specific code into Context.java that is now shared between Display, Pbuffer and AWTGLCanvas. (Win32 and Mac OS X is now broken while I implement the same changes on those platforms) Index: Win32Display.java =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/opengl/Win32Display.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- Win32Display.java 18 Jan 2005 20:22:46 -0000 1.10 +++ Win32Display.java 20 Feb 2005 11:24:17 -0000 1.11 @@ -58,25 +58,29 @@ public native void setGammaRamp(FloatBuffer gammaRamp) throws LWJGLException; public native String getAdapter(); public native String getVersion(); - public native DisplayMode init(); + public native DisplayMode init() throws LWJGLException; public native void setTitle(String title); public native boolean isCloseRequested(); public native boolean isVisible(); public native boolean isActive(); public native boolean isDirty(); - public native void swapBuffers(); - public native void makeCurrent() throws LWJGLException; - public native void createContext(PixelFormat pixel_format) throws LWJGLException; - public native void destroyContext(); +// public native void swapBuffers(); +// public native void makeCurrent() throws LWJGLException; + public PeerInfo createPeerInfo(PixelFormat pixel_format) throws LWJGLException { + throw new RuntimeException("Not supported yet"); + } +// public native void createContext(PixelFormat pixel_format) throws LWJGLException; +// public native void destroyContext(); + public native void destroyPeerInfo(); public native void update(); public native void setVSyncEnabled(boolean sync); public native void reshape(int x, int y, int width, int height); - public native DisplayMode[] getAvailableDisplayModes(); + public native DisplayMode[] getAvailableDisplayModes() throws LWJGLException; /* Mouse */ public native boolean hasWheel(); public native int getButtonCount(); - public native void createMouse(); + public native void createMouse() throws LWJGLException; public native void destroyMouse(); public native void pollMouse(IntBuffer coord_buffer, ByteBuffer buttons); public native int readMouse(IntBuffer buffer, int buffer_position); @@ -106,10 +110,17 @@ public native void destroyCursor(Object cursorHandle); public native int getPbufferCapabilities(); - public native boolean isBufferLost(ByteBuffer handle); - public native void makePbufferCurrent(ByteBuffer handle) throws LWJGLException; + public native boolean isBufferLost(PeerInfo handle); +// public native boolean isBufferLost(ByteBuffer handle); +// public native void makePbufferCurrent(ByteBuffer handle) throws LWJGLException; - public ByteBuffer createPbuffer(int width, int height, PixelFormat pixel_format, + public PeerInfo createPbuffer(int width, int height, PixelFormat pixel_format, + IntBuffer pixelFormatCaps, + IntBuffer pBufferAttribs) throws LWJGLException { + throw new RuntimeException("Not yet supported"); + } + +/* public ByteBuffer createPbuffer(int width, int height, PixelFormat pixel_format, IntBuffer pixelFormatCaps, IntBuffer pBufferAttribs, ByteBuffer shared_pbuffer_handle) throws LWJGLException { ByteBuffer handle = BufferUtils.createByteBuffer(PBUFFER_HANDLE_SIZE); @@ -120,10 +131,14 @@ private native void nCreatePbuffer(ByteBuffer handle, int width, int height, PixelFormat pixel_format, IntBuffer pixelFormatCaps, IntBuffer pBufferAttribs, ByteBuffer shared_pbuffer_handle) throws LWJGLException; +*/ + public native void destroyPbuffer(PeerInfo handle); +// public native void destroyPbuffer(ByteBuffer handle); - public native void destroyPbuffer(ByteBuffer handle); - - public native void setPbufferAttrib(ByteBuffer handle, int attrib, int value); + public native void setPbufferAttrib(PeerInfo handle, int attrib, int value); + public native void bindTexImageToPbuffer(PeerInfo handle, int buffer); + public native void releaseTexImageFromPbuffer(PeerInfo handle, int buffer); +/* public native void setPbufferAttrib(ByteBuffer handle, int attrib, int value); public native void bindTexImageToPbuffer(ByteBuffer handle, int buffer); - public native void releaseTexImageFromPbuffer(ByteBuffer handle, int buffer); + public native void releaseTexImageFromPbuffer(ByteBuffer handle, int buffer);*/ } --- NEW FILE: LinuxCanvasImplementation.java --- /* * Copyright (c) 2002-2004 LWJGL Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions 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 'LWJGL' nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.lwjgl.opengl; import java.nio.ByteBuffer; import org.lwjgl.LWJGLException; import org.lwjgl.BufferUtils; import java.awt.GraphicsDevice; import java.awt.GraphicsConfiguration; /* * Import the needed SUN specific classes. They * are needed since there is no official way to access * the screen and visual from GraphicsDevice and * GraphicsConfiguration respectively. */ import sun.awt.X11GraphicsDevice; import sun.awt.X11GraphicsConfig; /** * $Id: LinuxCanvasImplementation.java,v 1.1 2005/02/20 11:24:17 elias_naur Exp $ * * @author elias_naur <eli...@us...> * @version $Revision: 1.1 $ */ final class LinuxCanvasImplementation implements AWTCanvasImplementation { static int getScreenFromDevice(GraphicsDevice device) { X11GraphicsDevice x11_device = (X11GraphicsDevice)device; return x11_device.getScreen(); } private static int getVisualIDFromConfiguration(GraphicsConfiguration configuration) { X11GraphicsConfig x11_config = (X11GraphicsConfig)configuration; return x11_config.getVisual(); } public PeerInfo createPeerInfo(AWTGLCanvas canvas) throws LWJGLException { return new LinuxAWTGLCanvasPeerInfo(canvas); } /** * Find a proper GraphicsConfiguration from the given GraphicsDevice and PixelFormat. * * @return The GraphicsConfiguration corresponding to a visual that matches the pixel format. */ public GraphicsConfiguration findConfiguration(GraphicsDevice device, PixelFormat pixel_format) throws LWJGLException { X11GraphicsDevice x11_device = (X11GraphicsDevice)device; int screen = getScreenFromDevice(device); int visual_id_matching_format = findVisualIDFromFormat(screen, pixel_format); GraphicsConfiguration[] configurations = device.getConfigurations(); for (int i = 0; i < configurations.length; i++) { int visual_id = getVisualIDFromConfiguration(configurations[i]); if (visual_id == visual_id_matching_format) return configurations[i]; } throw new LWJGLException("Could not find the matching GraphicsConfiguration to visual id"); } private static int findVisualIDFromFormat(int screen, PixelFormat pixel_format) throws LWJGLException { try { LinuxDisplay.lockAWT(); try { GLContext.loadOpenGLLibrary(); try { LinuxDisplay.incDisplay(); return nFindVisualIDFromFormat(screen, pixel_format); } finally { LinuxDisplay.decDisplay(); } } finally { GLContext.unloadOpenGLLibrary(); } } finally { LinuxDisplay.unlockAWT(); } } private static native int nFindVisualIDFromFormat(int screen, PixelFormat pixel_format) throws LWJGLException; } --- NEW FILE: PeerInfo.java --- /* * Copyright (c) 2002-2004 LWJGL Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions 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 'LWJGL' nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.lwjgl.opengl; import java.nio.ByteBuffer; import org.lwjgl.LWJGLException; import org.lwjgl.Sys; /** * $Id: PeerInfo.java,v 1.1 2005/02/20 11:24:17 elias_naur Exp $ * * @author elias_naur <eli...@us...> * @version $Revision: 1.1 $ */ abstract class PeerInfo { private final ByteBuffer handle; protected PeerInfo(ByteBuffer handle) { this.handle = handle; } private final void lockAndInitHandle() throws LWJGLException { doLockAndInitHandle(); } public synchronized final void unlock() throws LWJGLException { doUnlock(); } protected abstract void doLockAndInitHandle() throws LWJGLException; protected abstract void doUnlock() throws LWJGLException; public synchronized final ByteBuffer lockAndGetHandle() throws LWJGLException { lockAndInitHandle(); return getHandle(); } protected final ByteBuffer getHandle() { return handle; } } --- NEW FILE: AWTCanvasImplementation.java --- /* * Copyright (c) 2002-2004 LWJGL Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions 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 'LWJGL' nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.lwjgl.opengl; import java.nio.ByteBuffer; import org.lwjgl.LWJGLException; import java.awt.GraphicsDevice; import java.awt.GraphicsConfiguration; /** * $Id: AWTCanvasImplementation.java,v 1.1 2005/02/20 11:24:17 elias_naur Exp $ * * @author elias_naur <eli...@us...> * @version $Revision: 1.1 $ */ interface AWTCanvasImplementation { /** * Return an opaque handle to the canvas peer information required to create a context from it. */ public PeerInfo createPeerInfo(AWTGLCanvas canvas) throws LWJGLException; /** * Find a proper GraphicsConfiguration from the given GraphicsDevice and PixelFormat. * * @return A GraphicsConfiguration matching the given GraphicsConfiguration and PixelFormat. * @throws LWJGLException if no suitable configuration could be found. */ public GraphicsConfiguration findConfiguration(GraphicsDevice device, PixelFormat pixel_format) throws LWJGLException; } --- NEW FILE: Drawable.java --- /* * Copyright (c) 2002-2004 LWJGL Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions 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 'LWJGL' nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.lwjgl.opengl; /** * The Drawable interface describes an OpenGL drawable with an associated * Context. * * @author elias_naur */ public interface Drawable { Context getContext(); } --- NEW FILE: LinuxAWTGLCanvasPeerInfo.java --- /* * Copyright (c) 2002-2004 LWJGL Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions 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 'LWJGL' nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.lwjgl.opengl; import java.nio.ByteBuffer; import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException; import org.lwjgl.Sys; /** * $Id: LinuxAWTGLCanvasPeerInfo.java,v 1.1 2005/02/20 11:24:17 elias_naur Exp $ * * @author elias_naur <eli...@us...> * @version $Revision: 1.1 $ */ final class LinuxAWTGLCanvasPeerInfo extends LinuxPeerInfo { private final static int LOCK_HANDLE_SIZE = 64; private final ByteBuffer lock_buffer = BufferUtils.createByteBuffer(LOCK_HANDLE_SIZE); private final AWTGLCanvas canvas; private final AWTSurfaceLock awt_surface = new AWTSurfaceLock(); public LinuxAWTGLCanvasPeerInfo(AWTGLCanvas canvas) { this.canvas = canvas; } protected void doLockAndInitHandle() throws LWJGLException { int screen = LinuxCanvasImplementation.getScreenFromDevice(canvas.getGraphicsConfiguration().getDevice()); nInitHandle(screen, awt_surface.lockAndGetHandle(canvas), getHandle()); } private static native void nInitHandle(int screen, ByteBuffer surface_buffer, ByteBuffer peer_info_handle) throws LWJGLException; protected void doUnlock() throws LWJGLException { awt_surface.unlock(); } } Index: AWTGLCanvas.java =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/opengl/AWTGLCanvas.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- AWTGLCanvas.java 16 Feb 2005 13:47:21 -0000 1.2 +++ AWTGLCanvas.java 20 Feb 2005 11:24:17 -0000 1.3 @@ -33,6 +33,8 @@ import java.awt.Canvas; import java.awt.Graphics; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; import org.lwjgl.LWJGLException; import org.lwjgl.Sys; @@ -45,37 +47,88 @@ * @version $Revision$ * @author $Author$ */ -public class AWTGLCanvas extends Canvas { +public class AWTGLCanvas extends Canvas implements Drawable { + private final static AWTCanvasImplementation implementation; static { System.loadLibrary("jawt"); Sys.initialize(); + String class_name; + String OS_NAME = System.getProperty("os.name"); + if (OS_NAME.startsWith("Linux")) { + class_name = "org.lwjgl.opengl.LinuxCanvasImplementation"; + } else if (OS_NAME.startsWith("Windows")) { + class_name = "org.lwjgl.opengl.DefaultCanvasImplementation"; + } else if (OS_NAME.startsWith("Mac")) { + class_name = "org.lwjgl.opengl.DefaultCanvasImplementation"; + } else + throw new IllegalStateException("The platform " + OS_NAME + " is not supported"); + try { + Class impl_class = Class.forName(class_name); + implementation = (AWTCanvasImplementation)impl_class.newInstance(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } } /** The requested pixel format */ - private PixelFormat pixelFormat; + private final PeerInfo peer_info; + + /** The drawable to share context with */ + private final Drawable drawable; /** Context handle */ - private long context; + private Context context; + + /** + * This method should only be called internally. + */ + public Context getContext() { + return context; + } /** * Constructor using the default PixelFormat. */ - public AWTGLCanvas() { + public AWTGLCanvas() throws LWJGLException { this(new PixelFormat()); } /** - * Create an AWTGLCanvas with the requested PixelFormat. Construction is always - * successful, however, when the time comes to actually realise the component on the - * screen + * Create an AWTGLCanvas with the requested PixelFormat on the default GraphicsDevice. + * * @param pixelFormat The desired pixel format. May not be null + * @param device the device to create the canvas on. */ - public AWTGLCanvas(PixelFormat pixelFormat) { - if (pixelFormat == null) { - throw new IllegalArgumentException("Pixel format may not be null"); - } - this.pixelFormat = pixelFormat; + public AWTGLCanvas(PixelFormat pixel_format) throws LWJGLException { + this(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(), pixel_format); + } + + /** + * Create an AWTGLCanvas with the requested PixelFormat on the default GraphicsDevice. + * + * @param pixelFormat The desired pixel format. May not be null + * @param device the device to create the canvas on. + */ + public AWTGLCanvas(GraphicsDevice device, PixelFormat pixel_format) throws LWJGLException { + this(device, pixel_format, null); + } + + /** + * Create an AWTGLCanvas with the requested PixelFormat on the specified GraphicsDevice. + * + * @param device the device to create the canvas on. + * @param pixelFormat The desired pixel format. May not be null + * @param shared_drawable The Drawable to share context with + */ + public AWTGLCanvas(GraphicsDevice device, PixelFormat pixel_format, Drawable drawable) throws LWJGLException { + super(implementation.findConfiguration(device, pixel_format)); + this.peer_info = implementation.createPeerInfo(this); + this.drawable = drawable; } /* (non-Javadoc) @@ -94,64 +147,74 @@ * @see java.awt.Component#removeNotify() */ public void removeNotify() { - super.removeNotify(); try { destroyContext(); } catch (LWJGLException e) { throw new RuntimeException(e); } + super.removeNotify(); } /** - * Create the OpenGL context. This occurs when the component becomes displayable - * @throws LWJGLException + * Enable vsync */ - private synchronized void createContext() throws LWJGLException { - nCreateContext(); + public synchronized void setVSyncEnabled(boolean enabled) throws LWJGLException { + if (context == null) + throw new IllegalStateException("Canvas not yet displayable"); + context.setVSync(enabled); } - private native void nCreateContext() throws LWJGLException; /** - * Destroy the OpenGL context. This occurs when the component is no longer displayable. + * Swap the canvas' buffer */ - private synchronized void destroyContext() throws LWJGLException { - nDestroyContext(); + public synchronized void swapBuffers() throws LWJGLException { + if (context == null) + throw new IllegalStateException("Canvas not yet displayable"); + context.swapBuffers(); } - private native void nDestroyContext() throws LWJGLException; - /* (non-Javadoc) - * @see java.awt.Canvas#paint(java.awt.Graphics) - */ - public synchronized final void paint(Graphics g) { - try { - nPaint(); - } catch (Exception e) { - throw new RuntimeException(e); - } + public synchronized void releaseContext() throws LWJGLException { + if (context == null) + throw new IllegalStateException("Canvas not yet displayable"); + if (context.isCurrent()) + Context.releaseCurrentContext(); } - private native void nPaint() throws Exception; - /** - * Paint callback from native code + * Make the canvas' context current. It is highly recommended that the context + * is only made current inside the AWT thread (for example in an overridden paint()). */ - private final void cPaint() { - try { - GLContext.useContext(this); - } catch (LWJGLException e) { - throw new RuntimeException(e); - } - doPaint(); + public synchronized void makeCurrent() throws LWJGLException { + if (context == null) + throw new IllegalStateException("Canvas not yet displayable"); + context.makeCurrent(); } /** - * Do painting. Override this method to call GL commands. + * Create the OpenGL context. This occurs when the component becomes displayable + * @throws LWJGLException */ - protected void doPaint() { + private synchronized void createContext() throws LWJGLException { + if (context == null) + context = new Context(peer_info, drawable != null ? drawable.getContext() : null); } - /* (non-Javadoc) - * @see java.awt.Canvas#update(java.awt.Graphics) + /** + * Destroy the OpenGL context. This happens when the component becomes undisplayable + */ + private synchronized void destroyContext() throws LWJGLException { + context.forceDestroy(); + context = null; + } + + /** + * Empty paint to avoid clearing + */ + public void paint(Graphics g) { + } + + /** + * override update to avoid clearing */ public void update(Graphics g) { paint(g); Index: MacOSXDisplay.java =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/opengl/MacOSXDisplay.java,v retrieving revision 1.21 retrieving revision 1.22 diff -u -d -r1.21 -r1.22 --- MacOSXDisplay.java 22 Jan 2005 00:34:49 -0000 1.21 +++ MacOSXDisplay.java 20 Feb 2005 11:24:17 -0000 1.22 @@ -157,11 +157,11 @@ return new DisplayMode(awt_mode.getWidth(), awt_mode.getHeight(), bit_depth, refresh_rate); } - public DisplayMode init() { + public DisplayMode init() throws LWJGLException { return createLWJGLDisplayMode(MacOSXFrame.getDevice().getDisplayMode()); } - public DisplayMode[] getAvailableDisplayModes() { + public DisplayMode[] getAvailableDisplayModes() throws LWJGLException { java.awt.DisplayMode[] awt_modes = MacOSXFrame.getDevice().getDisplayModes(); List modes = new ArrayList(); for (int i = 0; i < awt_modes.length; i++) @@ -199,13 +199,17 @@ public native void setView(MacOSXGLCanvas canvas); - public native void swapBuffers(); +// public native void swapBuffers(); - public native void makeCurrent() throws LWJGLException; +// public native void makeCurrent() throws LWJGLException; - public native void createContext(PixelFormat pixel_format) throws LWJGLException; + public PeerInfo createPeerInfo(PixelFormat pixel_format) throws LWJGLException { + throw new RuntimeException("Not supported yet"); + } +// public native void createContext(PixelFormat pixel_format) throws LWJGLException; - public native void destroyContext(); + public native void destroyPeerInfo(); +// public native void destroyContext(); public void update() { if (frame.syncShouldUpdateContext()) { @@ -251,7 +255,7 @@ return MouseEventQueue.NUM_BUTTONS; } - public void createMouse() { + public void createMouse() throws LWJGLException { MacOSXGLCanvas canvas = frame.getCanvas(); this.mouse_queue = new MouseEventQueue(canvas.getWidth(), canvas.getHeight()); canvas.addMouseListener(mouse_queue); @@ -409,13 +413,19 @@ } } - public boolean isBufferLost(ByteBuffer handle) { + public boolean isBufferLost(PeerInfo handle) { return false; } - public native void makePbufferCurrent(ByteBuffer handle) throws LWJGLException; +// public native void makePbufferCurrent(ByteBuffer handle) throws LWJGLException; - public ByteBuffer createPbuffer(int width, int height, PixelFormat pixel_format, + public PeerInfo createPbuffer(int width, int height, PixelFormat pixel_format, + IntBuffer pixelFormatCaps, + IntBuffer pBufferAttribs) throws LWJGLException { + throw new RuntimeException("Not yet supported"); + } + +/* public ByteBuffer createPbuffer(int width, int height, PixelFormat pixel_format, IntBuffer pixelFormatCaps, IntBuffer pBufferAttribs, ByteBuffer shared_pbuffer_handle) throws LWJGLException { ByteBuffer handle = BufferUtils.createByteBuffer(PBUFFER_HANDLE_SIZE); @@ -426,18 +436,19 @@ private native void nCreatePbuffer(ByteBuffer handle, int width, int height, PixelFormat pixel_format, IntBuffer pixelFormatCaps, IntBuffer pBufferAttribs, ByteBuffer shared_pbuffer_handle) throws LWJGLException; +*/ +// public native void destroyPbuffer(ByteBuffer handle); + public native void destroyPbuffer(PeerInfo handle); - public native void destroyPbuffer(ByteBuffer handle); - - public void setPbufferAttrib(ByteBuffer handle, int attrib, int value) { + public void setPbufferAttrib(PeerInfo handle, int attrib, int value) { throw new UnsupportedOperationException(); } - public void bindTexImageToPbuffer(ByteBuffer handle, int buffer) { + public void bindTexImageToPbuffer(PeerInfo handle, int buffer) { throw new UnsupportedOperationException(); } - public void releaseTexImageFromPbuffer(ByteBuffer handle, int buffer) { + public void releaseTexImageFromPbuffer(PeerInfo handle, int buffer) { throw new UnsupportedOperationException(); } } Index: Display.java =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/opengl/Display.java,v retrieving revision 1.43 retrieving revision 1.44 diff -u -d -r1.43 -r1.44 --- Display.java 4 Feb 2005 12:00:24 -0000 1.43 +++ Display.java 20 Feb 2005 11:24:17 -0000 1.44 @@ -86,20 +86,41 @@ private static boolean vsync; /** A unique context object, so we can track different contexts between creates() and destroys() */ - private static Display context; + private static Context context; private static boolean window_created = false; static { Sys.initialize(); display_impl = createDisplayImplementation(); - current_mode = initial_mode = display_impl.init(); - Sys.log("Initial mode: " + initial_mode); - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - reset(); - } - }); + try { + current_mode = initial_mode = display_impl.init(); + Sys.log("Initial mode: " + initial_mode); + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + reset(); + } + }); + } catch (LWJGLException e) { + throw new RuntimeException(e); + } + } + + /** + * Fetch the Drawable from the Display. + * + * @return the Drawable corresponding to the Display context, or null it display is + * not created. + */ + public static Drawable getDrawable() { + if (context != null) { + return new Drawable() { + public Context getContext() { + return context; + } + }; + } else + return null; } private static DisplayImplementation createDisplayImplementation() { @@ -141,7 +162,7 @@ * * @return an array of all display modes the system reckons it can handle. */ - public static DisplayMode[] getAvailableDisplayModes() { + public static DisplayMode[] getAvailableDisplayModes() throws LWJGLException { DisplayMode[] unfilteredModes = display_impl.getAvailableDisplayModes(); if (unfilteredModes == null) { @@ -192,7 +213,7 @@ switchDisplayMode(); createWindow(); } catch (LWJGLException e) { - display_impl.destroyContext(); + destroyContext(); display_impl.resetDisplayMode(); throw e; } @@ -385,7 +406,7 @@ } createWindow(); } catch (LWJGLException e) { - display_impl.destroyContext(); + destroyContext(); display_impl.resetDisplayMode(); throw e; } @@ -483,7 +504,11 @@ // We paint only when the window is visible or dirty if (isVisible() || isDirty()) { Util.checkGLError(); - display_impl.swapBuffers(); + try { + context.swapBuffers(); + } catch (LWJGLException e) { + throw new RuntimeException(e); + } } processMessages(); @@ -505,8 +530,7 @@ public static void makeCurrent() throws LWJGLException { if (!isCreated()) throw new IllegalStateException("No window created to make current"); - display_impl.makeCurrent(); - GLContext.useContext(context); + context.makeCurrent(); } /** @@ -537,25 +561,35 @@ * @throws LWJGLException */ public static void create(PixelFormat pixel_format) throws LWJGLException { + create(pixel_format, null); + } + + /** + * Create the OpenGL context with the given minimum parameters. If isFullscreen() is true or if windowed + * context are not supported on the platform, the display mode will be switched to the mode returned by + * getDisplayMode(), and a fullscreen context will be created. If isFullscreen() is false, a windowed context + * will be created with the dimensions given in the mode returned by getDisplayMode(). If a context can't be + * created with the given parameters, a LWJGLException will be thrown. + * + * <p>The window created will be set up in orthographic 2D projection, with 1:1 pixel ratio with GL coordinates. + * + * @param pixel_format Describes the minimum specifications the context must fulfill. + * @param shared_drawable The Drawable to share context with. + * @throws LWJGLException + */ + public static void create(PixelFormat pixel_format, Drawable shared_drawable) throws LWJGLException { if (isCreated()) throw new IllegalStateException("Only one LWJGL context may be instantiated at any one time."); if (fullscreen) switchDisplayMode(); try { - GLContext.loadOpenGLLibrary(); + PeerInfo peer_info = display_impl.createPeerInfo(pixel_format); + context = new Context(peer_info, shared_drawable != null ? shared_drawable.getContext() : null); try { - display_impl.createContext(pixel_format); - try { - context = new Display(); - createWindow(); - initContext(); - } catch (LWJGLException e) { - display_impl.destroyContext(); - context = null; - throw e; - } + createWindow(); + initContext(); } catch (LWJGLException e) { - GLContext.unloadOpenGLLibrary(); + destroyContext(); throw e; } } catch (LWJGLException e) { @@ -617,16 +651,20 @@ } destroyWindow(); - display_impl.destroyContext(); - GLContext.unloadOpenGLLibrary(); - context = null; - x = y = -1; + destroyContext(); + x = y = -1; + reset(); + } + + private static void destroyContext() { try { - GLContext.useContext(null); + context.forceDestroy(); } catch (LWJGLException e) { - Sys.log("Failed to reset GLContext due to: " + e); + throw new RuntimeException(e); + } finally { + context = null; + display_impl.destroyPeerInfo(); } - reset(); } /* @@ -641,7 +679,7 @@ /** * @return the unique Display context (or null, if the Display has not been created) */ - public static Object getContext() { + public static Context getContext() { return context; } @@ -660,7 +698,7 @@ public static void setVSyncEnabled(boolean sync) { vsync = sync; if (isCreated()) - display_impl.setVSyncEnabled(vsync); + context.setVSync(vsync); } /** Index: Pbuffer.java =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/opengl/Pbuffer.java,v retrieving revision 1.37 retrieving revision 1.38 diff -u -d -r1.37 -r1.38 --- Pbuffer.java 22 Jan 2005 00:34:50 -0000 1.37 +++ Pbuffer.java 20 Feb 2005 11:24:17 -0000 1.38 @@ -41,14 +41,13 @@ * <p/> * Pbuffer encapsulates an OpenGL pbuffer. * <p/> - * Each instance of GL is only valid in the thread that creates it. In addition, only one instance of an OpenGL window or - * Pbuffer may be the current GL context in any one thread. To make a GL instance the current context, use makeCurrent(). + * + * This class is thread-safe. * * @author elias_naur <eli...@us...> * @version $Revision$ */ -public final class Pbuffer { - +public final class Pbuffer implements Drawable { /** * Indicates that Pbuffers can be created. */ @@ -137,7 +136,7 @@ /** * Handle to the native GL rendering context */ - private final ByteBuffer handle; + private final PeerInfo peer_info; /** * Width @@ -149,6 +148,10 @@ */ private final int height; + private final Context context; + + private boolean destroyed; + static { Sys.initialize(); } @@ -169,29 +172,40 @@ * @param height Pbuffer height * @param pixel_format Minimum Pbuffer context properties * @param renderTexture - * @param shared_context If non-null the Pbuffer will share display lists and textures with it. Otherwise, the Pbuffer will share + * @param shared_drawable If non-null the Pbuffer will share display lists and textures with it. Otherwise, the Pbuffer will share * with the Display context (if created). */ - public Pbuffer(int width, int height, PixelFormat pixel_format, RenderTexture renderTexture, Pbuffer shared_context) throws LWJGLException { + public Pbuffer(int width, int height, PixelFormat pixel_format, RenderTexture renderTexture, Drawable shared_drawable) throws LWJGLException { this.width = width; this.height = height; - this.handle = createPbuffer(width, height, pixel_format, renderTexture, shared_context != null ? shared_context.handle : null); + this.peer_info = createPbuffer(width, height, pixel_format, renderTexture); + Context shared_context = null; + if (shared_drawable != null) { + shared_context = shared_drawable.getContext(); + } else { + Drawable display_drawable = Display.getDrawable(); + if (display_drawable != null) + shared_context = display_drawable.getContext(); + } + this.context = new Context(peer_info, shared_context); } - private static ByteBuffer createPbuffer(int width, int height, PixelFormat pixel_format, RenderTexture renderTexture, ByteBuffer shared_context_handle) throws LWJGLException { - GLContext.loadOpenGLLibrary(); - try { - if ( renderTexture == null ) - return Display.getImplementation().createPbuffer(width, height, pixel_format, null, null, shared_context_handle); - else - return Display.getImplementation().createPbuffer(width, height, pixel_format, - renderTexture.pixelFormatCaps, - renderTexture.pBufferAttribs, - shared_context_handle); - } catch (LWJGLException e) { - GLContext.unloadOpenGLLibrary(); - throw e; - } + private static PeerInfo createPbuffer(int width, int height, PixelFormat pixel_format, RenderTexture renderTexture) throws LWJGLException { + if ( renderTexture == null ) + return Display.getImplementation().createPbuffer(width, height, pixel_format, null, null); + else + return Display.getImplementation().createPbuffer(width, height, pixel_format, + renderTexture.pixelFormatCaps, + renderTexture.pBufferAttribs); + } + + public Context getContext() { + return context; + } + + private void checkDestroyed() { + if (destroyed) + throw new IllegalStateException("Pbuffer is destroyed"); } /** @@ -201,17 +215,18 @@ * * @return true if the buffer is lost and destroyed, false if the buffer is valid. */ - public boolean isBufferLost() { - return Display.getImplementation().isBufferLost(handle); + public synchronized boolean isBufferLost() { + checkDestroyed(); + return Display.getImplementation().isBufferLost(peer_info); } /** * Method to make the Pbuffer context current. All subsequent OpenGL calls will go to this buffer. * @throws LWJGLException if the context could not be made current */ - public void makeCurrent() throws LWJGLException { - Display.getImplementation().makePbufferCurrent(handle); - GLContext.useContext(this); + public synchronized void makeCurrent() throws LWJGLException { + checkDestroyed(); + context.makeCurrent(); } /** @@ -227,17 +242,15 @@ * Destroys the Pbuffer. After this call, there will be no valid GL rendering context - regardless of whether this Pbuffer was * the current rendering context or not. */ - public void destroy() { + public synchronized void destroy() { + if (destroyed) + return; try { - makeCurrent(); - int error = GL11.glGetError(); - Display.getImplementation().destroyPbuffer(handle); - GLContext.useContext(null); - GLContext.unloadOpenGLLibrary(); - if (error != GL11.GL_NO_ERROR) - throw new OpenGLException(error); + context.forceDestroy(); + Display.getImplementation().destroyPbuffer(peer_info); + destroyed = true; } catch (LWJGLException e) { - // ignore exception + Sys.log("Exception occurred while destroying pbuffer: " + e); } } @@ -257,8 +270,9 @@ * @param attrib * @param value */ - public void setAttrib(int attrib, int value) { - Display.getImplementation().setPbufferAttrib(handle, attrib, value); + public synchronized void setAttrib(int attrib, int value) { + checkDestroyed(); + Display.getImplementation().setPbufferAttrib(peer_info, attrib, value); } /** @@ -268,8 +282,9 @@ * * @param buffer */ - public void bindTexImage(int buffer) { - Display.getImplementation().bindTexImageToPbuffer(handle, buffer); + public synchronized void bindTexImage(int buffer) { + checkDestroyed(); + Display.getImplementation().bindTexImageToPbuffer(peer_info, buffer); } /** @@ -277,21 +292,24 @@ * * @param buffer */ - public void releaseTexImage(int buffer) { - Display.getImplementation().releaseTexImageFromPbuffer(handle, buffer); + public synchronized void releaseTexImage(int buffer) { + checkDestroyed(); + Display.getImplementation().releaseTexImageFromPbuffer(peer_info, buffer); } /** * @return Returns the height. */ - public int getHeight() { + public synchronized int getHeight() { + checkDestroyed(); return height; } /** * @return Returns the width. */ - public int getWidth() { + public synchronized int getWidth() { + checkDestroyed(); return width; } } --- NEW FILE: Context.java --- /* * Copyright (c) 2002-2004 LWJGL Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions 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 'LWJGL' nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.lwjgl.opengl; import java.nio.ByteBuffer; import org.lwjgl.LWJGLException; import org.lwjgl.Sys; /** * $Id: Context.java,v 1.1 2005/02/20 11:24:17 elias_naur Exp $ * <p/> * Context encapsulates an OpenGL context. * <p/> * * This class is thread-safe. * * @author elias_naur <eli...@us...> * @version $Revision: 1.1 $ */ final class Context { /** * The platform specific implementation of context methods */ private final static ContextImplementation implementation; /** The current Context */ private final static ThreadLocal current_context_local = new ThreadLocal(); /** * Handle to the native GL rendering context */ private final ByteBuffer handle; private final PeerInfo peer_info; /** Whether the context has been destroyed */ private boolean destroyed; private boolean destroy_requested; /** The thread that has this context current, or null. */ private Thread thread; static { Sys.initialize(); String class_name; String OS_NAME = System.getProperty("os.name"); if (OS_NAME.startsWith("Linux")) { class_name = "org.lwjgl.opengl.LinuxContextImplementation"; } else if (OS_NAME.startsWith("Windows")) { class_name = "org.lwjgl.opengl.Win32ContextImplementation"; } else if (OS_NAME.startsWith("Mac")) { class_name = "org.lwjgl.opengl.MacOSXContextImplementation"; } else throw new IllegalStateException("The platform " + OS_NAME + " is not supported"); try { Class impl_class = Class.forName(class_name); implementation = (ContextImplementation)impl_class.newInstance(); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } } PeerInfo getPeerInfo() { return peer_info; } static Context getCurrentContext() { return (Context)current_context_local.get(); } /** * Create a context with the specified peer info and shared context */ public Context(PeerInfo peer_info, Context shared_context) throws LWJGLException { Context context_lock = shared_context != null ? shared_context : this; // If shared_context is not null, synchronize on it to make sure it is not deleted // while this context is created. Otherwise, simply synchronize on ourself to avoid NPE synchronized (context_lock) { if (shared_context != null && shared_context.destroyed) throw new IllegalArgumentException("Shared context is destroyed"); GLContext.loadOpenGLLibrary(); try { this.peer_info = peer_info; this.handle = implementation.create(peer_info, shared_context != null ? shared_context.handle : null); } catch (LWJGLException e) { GLContext.unloadOpenGLLibrary(); throw e; } } } /** * Release the current context (if any). After this call, no context is current. */ public static void releaseCurrentContext() throws LWJGLException { Context current_context = getCurrentContext(); if (current_context != null) { implementation.releaseCurrentContext(); GLContext.useContext(null); current_context_local.set(null); synchronized (current_context) { current_context.thread = null; current_context.checkDestroy(); } } } /** * Swap the buffers on the current context. Only valid for double-buffered contexts */ public static void swapBuffers() throws LWJGLException { implementation.swapBuffers(); } private boolean canAccess() { return thread == null || Thread.currentThread() == thread; } private void checkAccess() { if (!canAccess()) throw new IllegalStateException("From thread " + Thread.currentThread() + ": " +thread + " already has the context current"); } /** * Make the context current */ public synchronized void makeCurrent() throws LWJGLException { checkAccess(); if (destroyed) throw new IllegalStateException("Context is destroyed"); thread = Thread.currentThread(); current_context_local.set(this); implementation.makeCurrent(peer_info, handle); GLContext.useContext(this); } /** * Query whether the context is current */ public synchronized boolean isCurrent() throws LWJGLException { if (destroyed) throw new IllegalStateException("Context is destroyed"); return implementation.isCurrent(handle); } private void checkDestroy() { if (!destroyed && destroy_requested) { try { implementation.destroy(peer_info, handle); destroyed = true; thread = null; GLContext.unloadOpenGLLibrary(); } catch (LWJGLException e) { Sys.log("Exception occurred while destroying context: " + e); } } } /** * Enable or disable vertical monitor synchronization. This call is a best-attempt at changing * the monitor vertical refresh synchronization of the context, and is not guaranteed to be successful. * @param sync true to synchronize; false to ignore synchronization */ public synchronized void setVSync(boolean enable) { implementation.setVSync(enable); } /** * Destroy the context. This method behaves the same as destroy() with the extra * requirement that the context must be either current to the current thread or not * current at all. */ public synchronized void forceDestroy() throws LWJGLException { checkAccess(); destroy(); } /** * Request destruction of the Context. If the context is current, no context will be current after this call. * The context is destroyed when no thread has it current. */ public synchronized void destroy() throws LWJGLException { if (destroyed) return; destroy_requested = true; boolean was_current = isCurrent(); int error = GL11.GL_NO_ERROR; if (was_current) { error = GL11.glGetError(); releaseCurrentContext(); } checkDestroy(); if (was_current && error != GL11.GL_NO_ERROR) throw new OpenGLException(error); } } --- NEW FILE: LinuxContextImplementation.java --- /* * Copyright (c) 2002-2004 LWJGL Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions 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 'LWJGL' nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.lwjgl.opengl; import java.nio.ByteBuffer; import org.lwjgl.LWJGLException; import org.lwjgl.BufferUtils; /** * $Id: LinuxContextImplementation.java,v 1.1 2005/02/20 11:24:17 elias_naur Exp $ * * @author elias_naur <eli...@us...> * @version $Revision: 1.1 $ */ final class LinuxContextImplementation implements ContextImplementation { private final static int HANDLE_SIZE = 64; private static PeerInfo getCurrentPeerInfo() { return Context.getCurrentContext().getPeerInfo(); } public ByteBuffer create(PeerInfo peer_info, ByteBuffer shared_context_handle) throws LWJGLException { ByteBuffer handle = BufferUtils.createByteBuffer(HANDLE_SIZE); LinuxDisplay.lockAWT(); try { ByteBuffer peer_handle = peer_info.lockAndGetHandle(); try { nCreate(peer_handle, handle, shared_context_handle); return handle; } finally { peer_info.unlock(); } } finally { LinuxDisplay.unlockAWT(); } } private static native void nCreate(ByteBuffer peer_handle, ByteBuffer context_handle, ByteBuffer shared_context_handle) throws LWJGLException; public void swapBuffers() throws LWJGLException { PeerInfo current_peer_info = getCurrentPeerInfo(); if (current_peer_info == null) throw new IllegalStateException("No context is current"); LinuxDisplay.lockAWT(); try { ByteBuffer peer_handle = current_peer_info.lockAndGetHandle(); try { nSwapBuffers(peer_handle); } finally { current_peer_info.unlock(); } } finally { LinuxDisplay.unlockAWT(); } } private static native void nSwapBuffers(ByteBuffer peer_info_handle) throws LWJGLException; public void releaseCurrentContext() throws LWJGLException { PeerInfo current_peer_info = getCurrentPeerInfo(); if (current_peer_info == null) return; // No context is current LinuxDisplay.lockAWT(); try { ByteBuffer peer_handle = current_peer_info.lockAndGetHandle(); try { nReleaseCurrentContext(peer_handle); } finally { current_peer_info.unlock(); } } finally { LinuxDisplay.unlockAWT(); } } private static native void nReleaseCurrentContext(ByteBuffer peer_info_handle) throws LWJGLException; public void makeCurrent(PeerInfo peer_info, ByteBuffer handle) throws LWJGLException { LinuxDisplay.lockAWT(); try { ByteBuffer peer_handle = peer_info.lockAndGetHandle(); try { nMakeCurrent(peer_handle, handle); } finally { peer_info.unlock(); } } finally { LinuxDisplay.unlockAWT(); } } private static native void nMakeCurrent(ByteBuffer peer_handle, ByteBuffer context_handle) throws LWJGLException; public boolean isCurrent(ByteBuffer handle) throws LWJGLException { LinuxDisplay.lockAWT(); try { boolean result = nIsCurrent(handle); return result; } finally { LinuxDisplay.unlockAWT(); } } private static native boolean nIsCurrent(ByteBuffer context_handle) throws LWJGLException; public void setVSync(boolean enabled) { LinuxDisplay.lockAWT(); nSetVSync(enabled); LinuxDisplay.unlockAWT(); } private static native void nSetVSync(boolean enabled); public void destroy(PeerInfo peer_info, ByteBuffer handle) throws LWJGLException { LinuxDisplay.lockAWT(); try { ByteBuffer peer_handle = peer_info.lockAndGetHandle(); try { nDestroy(peer_handle, handle); } finally { peer_info.unlock(); } } finally { LinuxDisplay.unlockAWT(); } } private static native void nDestroy(ByteBuffer peer_handle, ByteBuffer context_handle) throws LWJGLException; } Index: LinuxDisplay.java =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/opengl/LinuxDisplay.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -d -r1.14 -r1.15 --- LinuxDisplay.java 20 Jan 2005 22:51:26 -0000 1.14 +++ LinuxDisplay.java 20 Feb 2005 11:24:17 -0000 1.15 @@ -42,6 +42,7 @@ import java.nio.FloatBuffer; import java.nio.IntBuffer; +import org.lwjgl.Sys; import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException; import org.lwjgl.input.Keyboard; @@ -51,41 +52,62 @@ private static final int PBUFFER_HANDLE_SIZE = 24; private static final int NUM_BUTTONS = 3; + private static PeerInfo peer_info; + /* Since Xlib is not guaranteed to be thread safe, we need a way to synchronize LWJGL - * Xlib calls with AWT Xlib calls. Fortunately, JAWT implements LockAWT and UnlockAWT(), to + * Xlib calls with AWT Xlib calls. Fortunately, JAWT implements LockAWT and UnlockAWT() to * do just that. */ - private native void lockAWT(); - private native void unlockAWT(); - + static native void lockAWT(); + static native void unlockAWT(); + + /** + * increment and decrement display usage. + */ + static native void incDisplay() throws LWJGLException; + static native void decDisplay(); + public void createWindow(DisplayMode mode, boolean fullscreen, int x, int y) throws LWJGLException { lockAWT(); - nCreateWindow(mode, fullscreen, x, y); - unlockAWT(); + try { + ByteBuffer handle = peer_info.lockAndGetHandle(); + try { + nCreateWindow(handle, mode, fullscreen, x, y); + } finally { + peer_info.unlock(); + } + } finally { + unlockAWT(); + } } - public native void nCreateWindow(DisplayMode mode, boolean fullscreen, int x, int y) throws LWJGLException; - + private static native void nCreateWindow(ByteBuffer peer_info_handle, DisplayMode mode, boolean fullscreen, int x, int y) throws LWJGLException; public void destroyWindow() { lockAWT(); nDestroyWindow(); unlockAWT(); } - public native void nDestroyWindow(); + private static native void nDestroyWindow(); public void switchDisplayMode(DisplayMode mode) throws LWJGLException { lockAWT(); - nSwitchDisplayMode(mode); - unlockAWT(); + try { + nSwitchDisplayMode(mode); + } finally { + unlockAWT(); + } } - public native void nSwitchDisplayMode(DisplayMode mode) throws LWJGLException; - + private static native void nSwitchDisplayMode(DisplayMode mode) throws LWJGLException; + public void resetDisplayMode() { lockAWT(); - nResetDisplayMode(); - unlockAWT(); + try { + nResetDisplayMode(); + } finally { + unlockAWT(); + } } - public native void nResetDisplayMode(); + private static native void nResetDisplayMode(); public int getGammaRampLength() { lockAWT(); @@ -93,126 +115,130 @@ unlockAWT(); return length; } - public native int nGetGammaRampLength(); - + private static native int nGetGammaRampLength(); + public void setGammaRamp(FloatBuffer gammaRamp) throws LWJGLException { lockAWT(); - nSetGammaRamp(gammaRamp); - unlockAWT(); + try { + nSetGammaRamp(gammaRamp); + } finally { + unlockAWT(); + } } - public native void nSetGammaRamp(FloatBuffer gammaRamp) throws LWJGLException; + private static native void nSetGammaRamp(FloatBuffer gammaRamp) throws LWJGLException; public String getAdapter() { return null; } - + public String getVersion() { return null; } - - public DisplayMode init() { + + public DisplayMode init() throws LWJGLException { lockAWT(); - DisplayMode mode = nInit(); - unlockAWT(); - return mode; + try { + DisplayMode mode = nInit(); + return mode; + } finally { + unlockAWT(); + } } - public native DisplayMode nInit(); + private static native DisplayMode nInit() throws LWJGLException; public void setTitle(String title) { lockAWT(); nSetTitle(title); unlockAWT(); } - public native void nSetTitle(String title); - + private static native void nSetTitle(String title); + public boolean isCloseRequested() { lockAWT(); boolean result = nIsCloseRequested(); unlockAWT(); return result; } - public native boolean nIsCloseRequested(); - + private static native boolean nIsCloseRequested(); + public boolean isVisible() { lockAWT(); boolean result = nIsVisible(); unlockAWT(); return result; } - public native boolean nIsVisible(); - + private static native boolean nIsVisible(); + public boolean isActive() { lockAWT(); boolean result = nIsActive(); unlockAWT(); return result; } - public native boolean nIsActive(); - + private static native boolean nIsActive(); + public boolean isDirty() { lockAWT(); boolean result = nIsDirty(); unlockAWT(); return result; } - public native boolean nIsDirty(); - - public void swapBuffers() { - lockAWT(); - nSwapBuffers(); - unlockAWT(); - } - public native void nSwapBuffers(); - - public void makeCurrent() throws LWJGLException { - lockAWT(); - nMakeCurrent(); - unlockAWT(); - } - public native void nMakeCurrent() throws LWJGLException; + private static native boolean nIsDirty(); - public void createContext(PixelFormat pixel_format) throws LWJGLException { + public PeerInfo createPeerInfo(PixelFormat pixel_format) throws LWJGLExcepti... [truncated message content] |