From 0a6e191eaebcc8edc2611dbedab6fd04a615fc2f Mon Sep 17 00:00:00 2001 From: Kenneth Russel Date: Thu, 26 Jun 2003 13:21:12 +0000 Subject: Initial Mac OS X port of Jogl by Gerard Ziemski and Kenneth Russell, subsuming the previous prototype implementation (no GLCanvas support) done by Marc Downie. Added user's guide (HTML format) under doc/userguide/index.html. git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/trunk@13 232f8b59-042b-4e1e-8c03-345bb8c30851 --- .../jogl/impl/macosx/MacOSXDummyGLContext.java | 120 ++++++++++ .../games/jogl/impl/macosx/MacOSXGLContext.java | 152 +++++++++--- .../jogl/impl/macosx/MacOSXGLContextFactory.java | 54 +++++ .../jogl/impl/macosx/MacOSXOffscreenGLContext.java | 119 ++++++++++ .../jogl/impl/macosx/MacOSXOnscreenGLContext.java | 262 +++++++++++++++++++++ 5 files changed, 672 insertions(+), 35 deletions(-) create mode 100644 src/net/java/games/jogl/impl/macosx/MacOSXDummyGLContext.java create mode 100644 src/net/java/games/jogl/impl/macosx/MacOSXGLContextFactory.java create mode 100644 src/net/java/games/jogl/impl/macosx/MacOSXOffscreenGLContext.java create mode 100644 src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLContext.java (limited to 'src/net/java/games/jogl/impl/macosx') diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXDummyGLContext.java b/src/net/java/games/jogl/impl/macosx/MacOSXDummyGLContext.java new file mode 100644 index 000000000..05ed7d9dc --- /dev/null +++ b/src/net/java/games/jogl/impl/macosx/MacOSXDummyGLContext.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. 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 + * MIDROSYSTEMS, 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. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package net.java.games.jogl.impl.macosx; + +import net.java.games.jogl.*; +import net.java.games.jogl.impl.*; + +/** This MacOSXGLContext implementation provides interoperability with + the NSOpenGLView Cocoa widget. The MacOSXGLImpl can be + instantiated without a GLContext, in which case it expects that + the end user will handle all OpenGL context management. Dynamic + function lookup is supported in this configuration by having this + object provide the FunctionAvailabilityTable. */ + +class MacOSXDummyGLContext extends MacOSXGLContext +{ + private MacOSXGLImpl gl; + + MacOSXDummyGLContext(MacOSXGLImpl gl) { + super(null, null, null); + this.gl = gl; + } + + protected GL createGL() { + return gl; + } + + protected boolean isOffscreen() { + return false; + } + + public int getOffscreenContextBufferedImageType() { + throw new GLException("Should not call this"); + } + + public int getOffscreenContextReadBuffer() { + throw new GLException("Should not call this"); + } + + public boolean offscreenImageNeedsVerticalFlip() { + throw new GLException("Should not call this"); + } + + public boolean canCreatePbufferContext() { + throw new GLException("Should not call this"); + } + + public synchronized GLContext createPbufferContext(GLCapabilities capabilities, int initialWidth, int initialHeight) { + throw new GLException("Should not call this"); + } + + public void bindPbufferToTexture() { + throw new GLException("Should not call this"); + } + + public void releasePbufferFromTexture() { + throw new GLException("Should not call this"); + } + + protected synchronized boolean makeCurrent(Runnable initAction) throws GLException { + throw new GLException("Should not call this"); + } + + protected synchronized void swapBuffers() throws GLException { + throw new GLException("Should not call this"); + } + + protected synchronized void free() throws GLException { + throw new GLException("Should not call this"); + } + + protected void create() { + throw new GLException("Should not call this"); + } + + private void destroy() { + throw new GLException("Should not call this"); + } + + public void resetGLFunctionAvailability() { + super.resetGLFunctionAvailability(); + } +} diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXGLContext.java b/src/net/java/games/jogl/impl/macosx/MacOSXGLContext.java index bcb33e602..23367c045 100644 --- a/src/net/java/games/jogl/impl/macosx/MacOSXGLContext.java +++ b/src/net/java/games/jogl/impl/macosx/MacOSXGLContext.java @@ -45,23 +45,33 @@ import net.java.games.gluegen.opengl.*; // for PROCADDRESS_VAR_PREFIX import net.java.games.jogl.*; import net.java.games.jogl.impl.*; -public abstract class MacOSXGLContext extends GLContext { - - public MacOSXGLContext(Component component, GLCapabilities capabilities, GLCapabilitiesChooser chooser) { +public abstract class MacOSXGLContext extends GLContext +{ + private static JAWT jawt; + protected long nsContext; // NSOpenGLContext + + public MacOSXGLContext(Component component, GLCapabilities capabilities, GLCapabilitiesChooser chooser) + { super(component, capabilities, chooser); } - - protected GL createGL() + + protected String mapToRealGLFunctionName(String glFunctionName) { - return new MacOSXGLImpl(this); + return glFunctionName; } - - protected String mapToRealGLFunctionName(String glFunctionName) { + + protected String mapToRealGLExtensionName(String glFunctionName) + { return glFunctionName; } - - protected abstract boolean isOffscreen(); + + protected boolean isFunctionAvailable(String glFunctionName) + { + return super.isFunctionAvailable(glFunctionName); + } + protected abstract boolean isOffscreen(); + public abstract int getOffscreenContextBufferedImageType(); public abstract int getOffscreenContextReadBuffer(); @@ -69,42 +79,114 @@ public abstract class MacOSXGLContext extends GLContext { public abstract boolean offscreenImageNeedsVerticalFlip(); /** - * Creates and initializes an appropriate OpenGl context. Should only be + * Creates and initializes an appropriate OpenGl nsContext. Should only be * called by {@link makeCurrent(Runnable)}. */ protected abstract void create(); - - protected synchronized boolean makeCurrent(Runnable initAction) throws GLException + + protected abstract boolean makeCurrent(Runnable initAction) throws GLException; + + protected abstract void free() throws GLException; + + protected abstract void swapBuffers() throws GLException; + + + protected void resetGLFunctionAvailability() { - throw new RuntimeException(" FIXME: not implemented "); + super.resetGLFunctionAvailability(); + resetGLProcAddressTable(); } + + protected void resetGLProcAddressTable() + { + if (DEBUG) { + System.err.println("!!! Initializing OpenGL extension address table"); + } - protected synchronized void free() throws GLException { - throw new RuntimeException(" FIXME: not implemented "); - } - - protected abstract void swapBuffers() throws GLException; + net.java.games.jogl.impl.ProcAddressTable table = getGLProcAddressTable(); + + // if GL is no longer an interface, we'll have to re-implement the code + // below so it only iterates through gl methods (a non-interface might + // have constructors, custom methods, etc). For now we assume all methods + // will be gl methods. + GL gl = getGL(); + Class tableClass = table.getClass(); + + java.lang.reflect.Field[] fields = tableClass.getDeclaredFields(); + + for (int i = 0; i < fields.length; ++i) { + String addressFieldName = fields[i].getName(); + if (!addressFieldName.startsWith(GLEmitter.PROCADDRESS_VAR_PREFIX)) + { + // not a proc address variable + continue; + } + int startOfMethodName = GLEmitter.PROCADDRESS_VAR_PREFIX.length(); + String glFuncName = addressFieldName.substring(startOfMethodName); + try + { + java.lang.reflect.Field addressField = tableClass.getDeclaredField(addressFieldName); + assert(addressField.getType() == Long.TYPE); + // get the current value of the proc address variable in the table object + long oldProcAddress = addressField.getLong(table); + long newProcAddress = CGL.getProcAddress(glFuncName); + /* + System.err.println( + "!!! Address=" + (newProcAddress == 0 + ? " " + : ("0x" + + Long.toHexString(newProcAddress))) + + "\tGL func: " + glFuncName); + */ + // set the current value of the proc address variable in the table object + addressField.setLong(gl, newProcAddress); + } catch (Exception e) { + throw new GLException( + "Cannot get GL proc address for method \"" + + glFuncName + "\": Couldn't get value of field \"" + addressFieldName + + "\" in class " + tableClass.getName(), e); + } + } - protected void resetGLFunctionAvailability() { - throw new RuntimeException(" FIXME: not implemented "); } - - protected void resetGLProcAddressTable() { - throw new RuntimeException(" FIXME: not implemented "); - } - - public net.java.games.jogl.impl.ProcAddressTable getGLProcAddressTable() { - throw new RuntimeException(" FIXME: not implemented "); + + public net.java.games.jogl.impl.ProcAddressTable getGLProcAddressTable() + { + if (glProcAddressTable == null) { + // FIXME: cache ProcAddressTables by capability bits so we can + // share them among contexts with the same capabilities + glProcAddressTable = + new net.java.games.jogl.impl.ProcAddressTable(); + } + return glProcAddressTable; } - - public String getPlatformExtensionsString() { - throw new RuntimeException(" FIXME: not implemented "); + + public String getPlatformExtensionsString() + { + return ""; } - - protected boolean isFunctionAvailable(String glFunctionName) + + //---------------------------------------------------------------------- + // Internals only below this point + // + + // Table that holds the addresses of the native C-language entry points for + // OpenGL functions. + private net.java.games.jogl.impl.ProcAddressTable glProcAddressTable; + + protected JAWT getJAWT() { - throw new RuntimeException(" FIXME: not implemented "); + if (jawt == null) + { + JAWT j = new JAWT(); + j.version(JAWTFactory.JAWT_VERSION_1_4); + if (!JAWTFactory.JAWT_GetAWT(j)) + { + throw new RuntimeException("Unable to initialize JAWT"); + } + jawt = j; + } + return jawt; } - } diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXGLContextFactory.java b/src/net/java/games/jogl/impl/macosx/MacOSXGLContextFactory.java new file mode 100644 index 000000000..323f75480 --- /dev/null +++ b/src/net/java/games/jogl/impl/macosx/MacOSXGLContextFactory.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. 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 + * MIDROSYSTEMS, 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. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package net.java.games.jogl.impl.macosx; + +import java.awt.Component; +import net.java.games.jogl.*; +import net.java.games.jogl.impl.*; + +public class MacOSXGLContextFactory extends GLContextFactory { + public GLContext createGLContext(Component component, GLCapabilities capabilities, GLCapabilitiesChooser chooser) { + if (component != null) { + return new MacOSXOnscreenGLContext(component, capabilities, chooser); + } else { + return new MacOSXOffscreenGLContext(capabilities, chooser); + } + } +} diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXOffscreenGLContext.java b/src/net/java/games/jogl/impl/macosx/MacOSXOffscreenGLContext.java new file mode 100644 index 000000000..46d754462 --- /dev/null +++ b/src/net/java/games/jogl/impl/macosx/MacOSXOffscreenGLContext.java @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. 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 + * MIDROSYSTEMS, 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. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package net.java.games.jogl.impl.macosx; + +import java.awt.image.BufferedImage; +import net.java.games.jogl.*; +import net.java.games.jogl.impl.*; + +public class MacOSXOffscreenGLContext extends MacOSXGLContext +{ + // Width and height of the underlying bitmap + private int width; + private int height; + + public MacOSXOffscreenGLContext(GLCapabilities capabilities, GLCapabilitiesChooser chooser) { + super(null, capabilities, chooser); + System.err.println("MacOSXOffscreenGLContext not implemented yet"); + } + + protected GL createGL() { + return new MacOSXGLImpl(this); + } + + protected boolean isOffscreen() { + return true; + } + + public int getOffscreenContextBufferedImageType() { + if (capabilities.getAlphaBits() > 0) { + return BufferedImage.TYPE_INT_ARGB; + } else { + return BufferedImage.TYPE_INT_RGB; + } + } + + public int getOffscreenContextReadBuffer() { + // On Windows these nsContexts are always single-buffered + return GL.GL_FRONT; + } + + public boolean offscreenImageNeedsVerticalFlip() { + // We can take care of this in the DIB creation (see below) + return false; + } + + public boolean canCreatePbufferContext() { + // For now say no + return false; + } + + public synchronized GLContext createPbufferContext(GLCapabilities capabilities, int initialWidth, int initialHeight) { + throw new GLException("Not supported"); + } + + public void bindPbufferToTexture() { + throw new GLException("Should not call this"); + } + + public void releasePbufferFromTexture() { + throw new GLException("Should not call this"); + } + + protected synchronized boolean makeCurrent(Runnable initAction) throws GLException { + return false; + } + + protected synchronized void swapBuffers() throws GLException { + throw new GLException("Not yet implemented"); + } + + protected synchronized void free() throws GLException { + throw new GLException("Not yet implemented"); + } + + protected void create() { + throw new GLException("Not yet implemented"); + } + + private void destroy() { + throw new GLException("Not yet implemented"); + } +} diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLContext.java b/src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLContext.java new file mode 100644 index 000000000..5b0df9052 --- /dev/null +++ b/src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLContext.java @@ -0,0 +1,262 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. 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 + * MIDROSYSTEMS, 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. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package net.java.games.jogl.impl.macosx; + +import java.awt.Component; +import java.util.*; + +import net.java.games.jogl.*; +import net.java.games.jogl.impl.*; + +public class MacOSXOnscreenGLContext extends MacOSXGLContext { + // Variables for lockSurface/unlockSurface + private JAWT_DrawingSurface ds; + private JAWT_DrawingSurfaceInfo dsi; + private JAWT_MacOSXDrawingSurfaceInfo macosxdsi; + private long nsView; // NSView + private Runnable myDeferredReshapeAction; + + public MacOSXOnscreenGLContext(Component component, GLCapabilities capabilities, GLCapabilitiesChooser chooser) { + super(component, capabilities, chooser); + } + + public synchronized void invokeGL(final Runnable runnable, boolean isReshape, Runnable initAction) throws GLException { + if (isReshape) { + myDeferredReshapeAction = new Runnable() { + public void run() { + CGL.updateContext(nsView, nsContext); + runnable.run(); + } + }; + } else { + if (myDeferredReshapeAction != null) { + super.invokeGL(myDeferredReshapeAction, true, initAction); + myDeferredReshapeAction = null; + } + super.invokeGL(runnable, isReshape, initAction); + } + } + + protected GL createGL() { + return new MacOSXGLImpl(this); + } + + protected boolean isOffscreen() { + return false; + } + + public int getOffscreenContextBufferedImageType() { + throw new GLException("Should not call this"); + } + + public int getOffscreenContextReadBuffer() { + throw new GLException("Should not call this"); + } + + public boolean offscreenImageNeedsVerticalFlip() { + throw new GLException("Should not call this"); + } + + public boolean canCreatePbufferContext() { + // For now say no + return false; + } + + public synchronized GLContext createPbufferContext(GLCapabilities capabilities, int initialWidth, int initialHeight) { + throw new GLException("Not supported"); + } + + public void bindPbufferToTexture() { + throw new GLException("Should not call this"); + } + + public void releasePbufferFromTexture() { + throw new GLException("Should not call this"); + } + + public synchronized void setRenderingThread(Thread currentThreadOrNull, Runnable initAction) { + this.willSetRenderingThread = false; + // FIXME: the JAWT in the Panther developer release + // requires all JAWT operations to be done on the AWT + // thread. This means that setRenderingThread won't work + // yet on this platform. This method can be deleted once + // the update for that release ships. + } + + protected void create() { + nsContext = CGL.createContext(nsView); + if (nsContext == 0) { + throw new GLException("Error creating nsContext"); + } + // FIXME + //choosePixelFormatAndCreateContext(true); + } + + protected synchronized boolean makeCurrent(Runnable initAction) throws GLException { + try { + if (!lockSurface()) { + return false; + } + + boolean created = false; + if (nsContext == 0) { + create(); + if (DEBUG) { + System.err.println("!!! Created GL nsContext for " + getClass().getName()); + } + created = true; + } + + if (!CGL.makeCurrentContext(nsView, nsContext)) { + throw new GLException("Error making nsContext current"); + } + + if (created) { + resetGLFunctionAvailability(); + if (initAction != null) { + initAction.run(); + } + } + return true; + } catch (RuntimeException e) { + try { + unlockSurface(); + } catch (Exception e2) { + // do nothing if unlockSurface throws + } + + throw(e); + } + } + + protected synchronized void free() throws GLException { + try { + if (!CGL.clearCurrentContext(nsView, nsContext)) { + throw new GLException("Error freeing OpenGL nsContext"); + } + } finally { + unlockSurface(); + } + } + + protected synchronized void swapBuffers() throws GLException { + if (!CGL.flushBuffer(nsView, nsContext)) { + throw new GLException("Error swapping buffers"); + } + } + + private boolean lockSurface() throws GLException { + if (nsView != 0) { + throw new GLException("Surface already locked"); + } + + ds = getJAWT().GetDrawingSurface(component); + if (ds == null) { + // Widget not yet realized + return false; + } + + int res = ds.Lock(); + if ((res & JAWTFactory.JAWT_LOCK_ERROR) != 0) { + throw new GLException("Unable to lock surface"); + } + + // See whether the surface changed and if so destroy the old + // OpenGL nsContext so it will be recreated + if ((res & JAWTFactory.JAWT_LOCK_SURFACE_CHANGED) != 0) { + if (nsContext != 0) { + if (!CGL.deleteContext(nsView, nsContext)) { + throw new GLException("Unable to delete old GL nsContext after surface changed"); + } + } + } + + dsi = ds.GetDrawingSurfaceInfo(); + if (dsi == null) { + ds.Unlock(); + getJAWT().FreeDrawingSurface(ds); + ds = null; + + // Widget not yet realized + return false; + } + + macosxdsi = (JAWT_MacOSXDrawingSurfaceInfo) dsi.platformInfo(); + if (macosxdsi == null) { + ds.FreeDrawingSurfaceInfo(dsi); + ds.Unlock(); + getJAWT().FreeDrawingSurface(ds); + ds = null; + dsi = null; + + // Widget not yet realized + return false; + } + + nsView = macosxdsi.cocoaViewRef(); + if (nsView == 0) { + ds.FreeDrawingSurfaceInfo(dsi); + ds.Unlock(); + getJAWT().FreeDrawingSurface(ds); + ds = null; + dsi = null; + macosxdsi = null; + + // Widget not yet realized + return false; + } + + return true; + } + + private void unlockSurface() throws GLException { + if (nsView == 0) { + throw new GLException("Surface already unlocked"); + } + + ds.FreeDrawingSurfaceInfo(dsi); + ds.Unlock(); + getJAWT().FreeDrawingSurface(ds); + ds = null; + dsi = null; + macosxdsi = null; + nsView = 0; + } +} -- cgit v1.2.3