aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/games/jogl/impl/macosx
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/java/games/jogl/impl/macosx')
-rw-r--r--src/net/java/games/jogl/impl/macosx/MacOSXDummyGLContext.java32
-rw-r--r--src/net/java/games/jogl/impl/macosx/MacOSXGLContext.java119
-rw-r--r--src/net/java/games/jogl/impl/macosx/MacOSXGLContextFactory.java27
-rw-r--r--src/net/java/games/jogl/impl/macosx/MacOSXGLDrawable.java78
-rw-r--r--src/net/java/games/jogl/impl/macosx/MacOSXOffscreenGLContext.java45
-rw-r--r--src/net/java/games/jogl/impl/macosx/MacOSXOffscreenGLDrawable.java63
-rw-r--r--src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLContext.java169
-rw-r--r--src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLDrawable.java236
-rw-r--r--src/net/java/games/jogl/impl/macosx/MacOSXPbufferGLContext.java132
-rw-r--r--src/net/java/games/jogl/impl/macosx/MacOSXPbufferGLDrawable.java152
10 files changed, 646 insertions, 407 deletions
diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXDummyGLContext.java b/src/net/java/games/jogl/impl/macosx/MacOSXDummyGLContext.java
index 29a3ef63b..1c48e1171 100644
--- a/src/net/java/games/jogl/impl/macosx/MacOSXDummyGLContext.java
+++ b/src/net/java/games/jogl/impl/macosx/MacOSXDummyGLContext.java
@@ -54,7 +54,7 @@ class MacOSXDummyGLContext extends MacOSXGLContext
private MacOSXGLImpl gl;
MacOSXDummyGLContext(MacOSXGLImpl gl) {
- super(null, null, null, null);
+ super(null, null);
this.gl = gl;
}
@@ -62,10 +62,6 @@ class MacOSXDummyGLContext extends MacOSXGLContext
return gl;
}
- protected boolean isOffscreen() {
- return false;
- }
-
public int getOffscreenContextReadBuffer() {
throw new GLException("Should not call this");
}
@@ -78,10 +74,12 @@ class MacOSXDummyGLContext extends MacOSXGLContext
throw new GLException("Should not call this");
}
- public synchronized GLContext createPbufferContext(GLCapabilities capabilities, int initialWidth, int initialHeight) {
+ public GLDrawableImpl createPbufferDrawable(GLCapabilities capabilities,
+ int initialWidth,
+ int initialHeight) {
throw new GLException("Should not call this");
}
-
+
public void bindPbufferToTexture() {
throw new GLException("Should not call this");
}
@@ -90,26 +88,6 @@ class MacOSXDummyGLContext extends MacOSXGLContext
throw new GLException("Should not call this");
}
- protected synchronized boolean makeCurrent(Runnable initAction) throws GLException {
- throw new GLException("Should not call this");
- }
-
- public 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 boolean create() {
- throw new GLException("Should not call this");
- }
-
- public 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 5471fcda4..56fba2900 100644
--- a/src/net/java/games/jogl/impl/macosx/MacOSXGLContext.java
+++ b/src/net/java/games/jogl/impl/macosx/MacOSXGLContext.java
@@ -47,20 +47,17 @@ import net.java.games.jogl.impl.*;
public abstract class MacOSXGLContext extends GLContextImpl
{
- private static JAWT jawt;
+ protected MacOSXGLDrawable drawable;
protected long nsContext; // NSOpenGLContext
- protected long nsView; // NSView
- protected long updater; // ContextUpdater
// Table that holds the addresses of the native C-language entry points for
// OpenGL functions.
private GLProcAddressTable glProcAddressTable;
- public MacOSXGLContext(Component component,
- GLCapabilities capabilities,
- GLCapabilitiesChooser chooser,
+ public MacOSXGLContext(MacOSXGLDrawable drawable,
GLContext shareWith)
{
- super(component, capabilities, chooser, shareWith);
+ super(shareWith);
+ this.drawable = drawable;
}
protected GL createGL()
@@ -78,34 +75,11 @@ public abstract class MacOSXGLContext extends GLContextImpl
return glExtensionName;
}
- protected boolean isFunctionAvailable(String glFunctionName)
- {
- return super.isFunctionAvailable(glFunctionName);
- }
-
- public boolean isExtensionAvailable(String glExtensionName) {
- if (glExtensionName.equals("GL_ARB_pbuffer") ||
- glExtensionName.equals("GL_ARB_pixel_format")) {
- return true;
- }
- return super.isExtensionAvailable(glExtensionName);
- }
-
- protected abstract boolean isOffscreen();
-
- public int getOffscreenContextReadBuffer() {
+ public int getOffscreenContextPixelDataType() {
throw new GLException("Should not call this");
}
- public int getOffscreenContextWidth() {
- throw new GLException("Should not call this");
- }
-
- public int getOffscreenContextHeight() {
- throw new GLException("Should not call this");
- }
-
- public int getOffscreenContextPixelDataType() {
+ public int getOffscreenContextReadBuffer() {
throw new GLException("Should not call this");
}
@@ -131,8 +105,9 @@ public abstract class MacOSXGLContext extends GLContextImpl
}
}
int[] viewNotReady = new int[1];
+ GLCapabilities capabilities = drawable.getCapabilities();
nsContext = CGL.createContext(share,
- nsView,
+ drawable.getView(),
capabilities.getDoubleBuffered() ? 1 : 0,
capabilities.getStereo() ? 1 : 0,
capabilities.getRedBits(),
@@ -160,38 +135,37 @@ public abstract class MacOSXGLContext extends GLContextImpl
}
throw new GLException("Error creating nsContext");
}
- //updater = CGL.updateContextRegister(nsContext, nsView); // gznote: not thread safe yet!
GLContextShareSet.contextCreated(this);
return true;
}
protected int makeCurrentImpl() throws GLException {
- boolean created = false;
- if (nsContext == 0) {
- if (!create()) {
- return CONTEXT_NOT_CURRENT;
- }
- if (DEBUG) {
- System.err.println("!!! Created GL nsContext for " + getClass().getName());
- }
- created = true;
+ boolean created = false;
+ if (nsContext == 0) {
+ if (!create()) {
+ return CONTEXT_NOT_CURRENT;
}
-
- if (!CGL.makeCurrentContext(nsContext, nsView)) {
- throw new GLException("Error making nsContext current");
+ if (DEBUG) {
+ System.err.println("!!! Created GL nsContext for " + getClass().getName());
}
+ created = true;
+ }
- if (created) {
- resetGLFunctionAvailability();
- return CONTEXT_CURRENT_NEW;
- }
- return CONTEXT_CURRENT;
+ if (!CGL.makeCurrentContext(nsContext, drawable.getView())) {
+ throw new GLException("Error making nsContext current");
+ }
+
+ if (created) {
+ resetGLFunctionAvailability();
+ return CONTEXT_CURRENT_NEW;
+ }
+ return CONTEXT_CURRENT;
}
protected void releaseImpl() throws GLException {
- if (!CGL.clearCurrentContext(nsContext, nsView)) {
- throw new GLException("Error freeing OpenGL nsContext");
- }
+ if (!CGL.clearCurrentContext(nsContext, drawable.getView())) {
+ throw new GLException("Error freeing OpenGL nsContext");
+ }
}
protected void destroyImpl() throws GLException {
@@ -199,15 +173,14 @@ public abstract class MacOSXGLContext extends GLContextImpl
if (!CGL.deleteContext(nsContext, 0)) {
throw new GLException("Unable to delete OpenGL context");
}
+ nsContext = 0;
+ GLContextShareSet.contextDestroyed(this);
if (DEBUG) {
System.err.println("!!! Destroyed OpenGL context " + nsContext);
}
- nsContext = 0;
}
}
- public abstract void swapBuffers() throws GLException;
-
protected long dynamicLookupFunction(String glFuncName) {
return CGL.getProcAddress(glFuncName);
}
@@ -247,6 +220,19 @@ public abstract class MacOSXGLContext extends GLContextImpl
CGL.setSwapInterval(nsContext, interval);
}
+ protected boolean isFunctionAvailable(String glFunctionName)
+ {
+ return super.isFunctionAvailable(glFunctionName);
+ }
+
+ public boolean isExtensionAvailable(String glExtensionName) {
+ if (glExtensionName.equals("GL_ARB_pbuffer") ||
+ glExtensionName.equals("GL_ARB_pixel_format")) {
+ return true;
+ }
+ return super.isExtensionAvailable(glExtensionName);
+ }
+
//----------------------------------------------------------------------
// Internals only below this point
//
@@ -254,23 +240,4 @@ public abstract class MacOSXGLContext extends GLContextImpl
protected long getNSContext() {
return nsContext;
}
-
- protected long getNSView() {
- return nsView;
- }
-
- protected JAWT getJAWT()
- {
- 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
index 25f15b3f2..a3c502b7c 100644
--- a/src/net/java/games/jogl/impl/macosx/MacOSXGLContextFactory.java
+++ b/src/net/java/games/jogl/impl/macosx/MacOSXGLContextFactory.java
@@ -46,20 +46,31 @@ import net.java.games.jogl.*;
import net.java.games.jogl.impl.*;
public class MacOSXGLContextFactory extends GLContextFactory {
+ static {
+ NativeLibLoader.load();
+ }
+
public GraphicsConfiguration chooseGraphicsConfiguration(GLCapabilities capabilities,
GLCapabilitiesChooser chooser,
GraphicsDevice device) {
return null;
}
- public GLContext createGLContext(Component component,
- GLCapabilities capabilities,
- GLCapabilitiesChooser chooser,
- GLContext shareWith) {
- if (component != null) {
- return new MacOSXOnscreenGLContext(component, capabilities, chooser, shareWith);
- } else {
- return new MacOSXOffscreenGLContext(capabilities, chooser, shareWith);
+ public GLDrawable getGLDrawable(Object target,
+ GLCapabilities capabilities,
+ GLCapabilitiesChooser chooser) {
+ if (target == null) {
+ throw new IllegalArgumentException("Null target");
+ }
+ if (!(target instanceof Component)) {
+ throw new IllegalArgumentException("GLDrawables not supported for objects of type " +
+ target.getClass().getName() + " (only Components are supported in this implementation)");
}
+ return new MacOSXOnscreenGLDrawable((Component) target, capabilities, chooser);
+ }
+
+ public GLDrawableImpl createOffscreenDrawable(GLCapabilities capabilities,
+ GLCapabilitiesChooser chooser) {
+ return new MacOSXOffscreenGLDrawable(capabilities);
}
}
diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXGLDrawable.java b/src/net/java/games/jogl/impl/macosx/MacOSXGLDrawable.java
new file mode 100644
index 000000000..b139678db
--- /dev/null
+++ b/src/net/java/games/jogl/impl/macosx/MacOSXGLDrawable.java
@@ -0,0 +1,78 @@
+/*
+ * 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
+ * MICROSYSTEMS, 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 abstract class MacOSXGLDrawable extends GLDrawableImpl {
+ protected static final boolean DEBUG = Debug.debug("MacOSXGLDrawable");
+
+ protected long nsView; // NSView
+ protected GLCapabilities capabilities;
+ protected GLCapabilitiesChooser chooser;
+
+ public MacOSXGLDrawable(GLCapabilities capabilities,
+ GLCapabilitiesChooser chooser) {
+ this.capabilities = (GLCapabilities) capabilities.clone();
+ this.chooser = chooser;
+ }
+
+ public void setRealized(boolean val) {
+ throw new GLException("Should not call this (should only be called for onscreen GLDrawables)");
+ }
+
+ public void destroy() {
+ throw new GLException("Should not call this (should only be called for offscreen GLDrawables)");
+ }
+
+ public void swapBuffers() throws GLException {
+ }
+
+ public GLCapabilities getCapabilities() {
+ return capabilities;
+ }
+
+ public long getView() {
+ return nsView;
+ }
+} \ No newline at end of file
diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXOffscreenGLContext.java b/src/net/java/games/jogl/impl/macosx/MacOSXOffscreenGLContext.java
index 39aab5652..c6ac894b1 100644
--- a/src/net/java/games/jogl/impl/macosx/MacOSXOffscreenGLContext.java
+++ b/src/net/java/games/jogl/impl/macosx/MacOSXOffscreenGLContext.java
@@ -45,34 +45,21 @@ import net.java.games.jogl.impl.*;
public class MacOSXOffscreenGLContext extends MacOSXPbufferGLContext
{
- public MacOSXOffscreenGLContext(GLCapabilities capabilities,
- GLCapabilitiesChooser chooser,
+ public MacOSXOffscreenGLContext(MacOSXPbufferGLDrawable drawable,
GLContext shareWith) {
- super(capabilities, -1, -1);
+ super(drawable, shareWith);
}
- protected boolean isOffscreen() {
- return true;
- }
-
- public boolean offscreenImageNeedsVerticalFlip() {
- return true;
- }
-
- public int getOffscreenContextWidth() {
- return initWidth;
- }
-
- public int getOffscreenContextHeight() {
- return initWidth;
- }
-
public int getOffscreenContextPixelDataType() {
- return GL.GL_UNSIGNED_INT_8_8_8_8_REV;
+ return GL.GL_UNSIGNED_INT_8_8_8_8_REV;
}
public int getOffscreenContextReadBuffer() {
- return GL.GL_BACK;
+ return GL.GL_FRONT;
+ }
+
+ public boolean offscreenImageNeedsVerticalFlip() {
+ return true;
}
public void bindPbufferToTexture() {
@@ -82,20 +69,4 @@ public class MacOSXOffscreenGLContext extends MacOSXPbufferGLContext
public void releasePbufferFromTexture() {
throw new GLException("Should not call this");
}
-
- protected int makeCurrentImpl() throws GLException {
- if (pendingOffscreenResize && (nsContext != 0)) {
- if (pendingOffscreenWidth != width || pendingOffscreenHeight != height) {
- destroyPBuffer();
- initWidth = pendingOffscreenWidth;
- initHeight = pendingOffscreenHeight;
- createPbuffer(0, 0);
- pendingOffscreenResize = false;
- }
- }
- return super.makeCurrentImpl();
- }
-
- public void swapBuffers() throws GLException {
- }
}
diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXOffscreenGLDrawable.java b/src/net/java/games/jogl/impl/macosx/MacOSXOffscreenGLDrawable.java
new file mode 100644
index 000000000..d7da03ec0
--- /dev/null
+++ b/src/net/java/games/jogl/impl/macosx/MacOSXOffscreenGLDrawable.java
@@ -0,0 +1,63 @@
+/*
+ * 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
+ * MICROSYSTEMS, 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.*;
+
+public class MacOSXOffscreenGLDrawable extends MacOSXPbufferGLDrawable {
+
+ public MacOSXOffscreenGLDrawable(GLCapabilities capabilities) {
+ super(capabilities, 0, 0);
+ }
+
+ public GLContext createContext(GLContext shareWith) {
+ return new MacOSXOffscreenGLContext(this, shareWith);
+ }
+
+ public void setSize(int width, int height) {
+ destroy();
+ initWidth = width;
+ initHeight = height;
+ // Floating-point frame buffers are never used with offscreen
+ // drawables (in GLJPanel) so don't need a GL object here
+ createPbuffer(null);
+ }
+}
diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLContext.java b/src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLContext.java
index 70da97f32..a8ffcf8ec 100644
--- a/src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLContext.java
+++ b/src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLContext.java
@@ -39,49 +39,22 @@
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.*;
-import java.security.*;
-
public class MacOSXOnscreenGLContext extends MacOSXGLContext {
- // Variables for lockSurface/unlockSurface
- private JAWT_DrawingSurface ds;
- private JAWT_DrawingSurfaceInfo dsi;
- private JAWT_MacOSXDrawingSurfaceInfo macosxdsi;
-
- // Indicates whether the component (if an onscreen context) has been
- // realized. Plausibly, before the component is realized the JAWT
- // should return an error or NULL object from some of its
- // operations; this appears to be the case on Win32 but is not true
- // at least with Sun's current X11 implementation (1.4.x), which
- // crashes with no other error reported if the DrawingSurfaceInfo is
- // fetched from a locked DrawingSurface during the validation as a
- // result of calling show() on the main thread. To work around this
- // we prevent any JAWT or OpenGL operations from being done until
- // addNotify() is called on the component.
- protected boolean realized;
-
+ protected MacOSXOnscreenGLDrawable drawable;
// Variables for pbuffer support
List pbuffersToInstantiate = new ArrayList();
- // Workaround for instance of 4796548
- private boolean firstLock = true;
-
- public MacOSXOnscreenGLContext(Component component,
- GLCapabilities capabilities,
- GLCapabilitiesChooser chooser,
+ public MacOSXOnscreenGLContext(MacOSXOnscreenGLDrawable drawable,
GLContext shareWith) {
- super(component, capabilities, chooser, shareWith);
+ super(drawable, shareWith);
+ this.drawable = drawable;
}
- protected boolean isOffscreen() {
- return false;
- }
-
public int getOffscreenContextReadBuffer() {
throw new GLException("Should not call this");
}
@@ -94,12 +67,12 @@ public class MacOSXOnscreenGLContext extends MacOSXGLContext {
return true;
}
- public GLContext createPbufferContext(GLCapabilities capabilities, int initialWidth, int initialHeight) {
- MacOSXPbufferGLContext ctx = new MacOSXPbufferGLContext(capabilities, initialWidth, initialHeight);
- ctx.setSynchronized(true);
- GLContextShareSet.registerSharing(this, ctx);
- pbuffersToInstantiate.add(ctx);
- return ctx;
+ public GLDrawableImpl createPbufferDrawable(GLCapabilities capabilities,
+ int initialWidth,
+ int initialHeight) {
+ MacOSXPbufferGLDrawable buf = new MacOSXPbufferGLDrawable(capabilities, initialWidth, initialHeight);
+ pbuffersToInstantiate.add(buf);
+ return buf;
}
public void bindPbufferToTexture() {
@@ -110,17 +83,14 @@ public class MacOSXOnscreenGLContext extends MacOSXGLContext {
throw new GLException("Should not call this");
}
- public void setRealized() {
- realized = true;
- }
-
protected int makeCurrentImpl() throws GLException {
try {
- if (!realized) {
+ int lockRes = drawable.lockSurface();
+ if (lockRes == MacOSXOnscreenGLDrawable.LOCK_SURFACE_NOT_READY) {
return CONTEXT_NOT_CURRENT;
}
- if (!lockSurface()) {
- return CONTEXT_NOT_CURRENT;
+ if (lockRes == MacOSXOnscreenGLDrawable.LOCK_SURFACE_CHANGED) {
+ super.destroy();
}
int ret = super.makeCurrentImpl();
if ((ret == CONTEXT_CURRENT) ||
@@ -132,21 +102,21 @@ public class MacOSXOnscreenGLContext extends MacOSXGLContext {
// do this updating only upon reshape of this component or reshape or movement
// of an ancestor, but this also wasn't sufficient and left garbage on the
// screen in some situations.
- CGL.updateContext(nsContext, nsView);
+ CGL.updateContext(nsContext, drawable.getView());
// Instantiate any pending pbuffers
while (!pbuffersToInstantiate.isEmpty()) {
- MacOSXPbufferGLContext ctx =
- (MacOSXPbufferGLContext) pbuffersToInstantiate.remove(pbuffersToInstantiate.size() - 1);
- ctx.createPbuffer(nsView, nsContext);
+ MacOSXPbufferGLDrawable buf =
+ (MacOSXPbufferGLDrawable) pbuffersToInstantiate.remove(pbuffersToInstantiate.size() - 1);
+ buf.createPbuffer(getGL());
}
} else {
// View might not have been ready
- unlockSurface();
+ drawable.unlockSurface();
}
return ret;
} catch (RuntimeException e) {
try {
- unlockSurface();
+ drawable.unlockSurface();
} catch (Exception e2) {
// do nothing if unlockSurface throws
}
@@ -158,108 +128,13 @@ public class MacOSXOnscreenGLContext extends MacOSXGLContext {
try {
super.releaseImpl();
} finally {
- unlockSurface();
+ drawable.unlockSurface();
}
}
- protected void destroyImpl() throws GLException {
- realized = false;
- super.destroyImpl();
- }
-
public void swapBuffers() throws GLException {
- if (!CGL.flushBuffer(nsContext, nsView)) {
+ if (!CGL.flushBuffer(nsContext, drawable.getView())) {
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) {
- //CGL.updateContextUnregister(nsContext, nsView, updater); // gznote: not thread safe yet!
- if (!CGL.deleteContext(nsContext, nsView)) {
- throw new GLException("Unable to delete old GL nsContext after surface changed");
- }
- }
- }
-
- if (firstLock) {
- AccessController.doPrivileged(new PrivilegedAction() {
- public Object run() {
- dsi = ds.GetDrawingSurfaceInfo();
- return null;
- }
- });
- } else {
- dsi = ds.GetDrawingSurfaceInfo();
- }
- if (dsi == null) {
- ds.Unlock();
- getJAWT().FreeDrawingSurface(ds);
- ds = null;
-
- // Widget not yet realized
- return false;
- }
-
- firstLock = 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;
- }
}
diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLDrawable.java b/src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLDrawable.java
new file mode 100644
index 000000000..808a47472
--- /dev/null
+++ b/src/net/java/games/jogl/impl/macosx/MacOSXOnscreenGLDrawable.java
@@ -0,0 +1,236 @@
+/*
+ * 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
+ * MICROSYSTEMS, 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.lang.ref.WeakReference;
+import java.security.*;
+import java.util.*;
+
+import net.java.games.jogl.*;
+import net.java.games.jogl.impl.*;
+
+public class MacOSXOnscreenGLDrawable extends MacOSXGLDrawable {
+ public static final int LOCK_SURFACE_NOT_READY = 1;
+ public static final int LOCK_SURFACE_CHANGED = 2;
+ public static final int LOCK_SUCCESS = 3;
+
+ private static JAWT jawt;
+ protected Component component;
+
+ private List/*<WeakReference<GLContext>>*/ createdContexts =
+ new ArrayList();
+
+ // Variables for lockSurface/unlockSurface
+ private JAWT_DrawingSurface ds;
+ private JAWT_DrawingSurfaceInfo dsi;
+ private JAWT_MacOSXDrawingSurfaceInfo macosxdsi;
+
+ // Indicates whether the component (if an onscreen context) has been
+ // realized. Plausibly, before the component is realized the JAWT
+ // should return an error or NULL object from some of its
+ // operations; this appears to be the case on Win32 but is not true
+ // at least with Sun's current X11 implementation (1.4.x), which
+ // crashes with no other error reported if the DrawingSurfaceInfo is
+ // fetched from a locked DrawingSurface during the validation as a
+ // result of calling show() on the main thread. To work around this
+ // we prevent any JAWT or OpenGL operations from being done until
+ // addNotify() is called on the component.
+ protected boolean realized;
+
+ // Workaround for instance of 4796548
+ private boolean firstLock = true;
+
+ public MacOSXOnscreenGLDrawable(Component component,
+ GLCapabilities capabilities,
+ GLCapabilitiesChooser chooser) {
+ super(capabilities, chooser);
+ this.component = component;
+ }
+
+ public GLContext createContext(GLContext shareWith) {
+ MacOSXOnscreenGLContext context =
+ new MacOSXOnscreenGLContext(this, shareWith);
+ // NOTE: we need to keep track of the created contexts in order to
+ // implement swapBuffers() because of how Mac OS X implements its
+ // OpenGL window interface
+ synchronized (this) {
+ List newContexts = new ArrayList();
+ newContexts.addAll(createdContexts);
+ newContexts.add(new WeakReference(context));
+ createdContexts = newContexts;
+ }
+ return context;
+ }
+
+ public void setRealized(boolean realized) {
+ this.realized = realized;
+ }
+
+ public void setSize(int width, int height) {
+ component.setSize(width, height);
+ }
+
+ public int getWidth() {
+ return component.getWidth();
+ }
+
+ public int getHeight() {
+ return component.getHeight();
+ }
+
+ public void swapBuffers() throws GLException {
+ for (Iterator iter = createdContexts.iterator(); iter.hasNext(); ) {
+ WeakReference ref = (WeakReference) iter.next();
+ MacOSXOnscreenGLContext ctx = (MacOSXOnscreenGLContext) ref.get();
+ // FIXME: clear out unreachable contexts
+ if (ctx != null) {
+ ctx.swapBuffers();
+ // FIXME: it isn't clear how to produce the same functionality
+ // as swapping an HDC's or GLXDrawable's buffers using the
+ // NSOpenGLContext API flushBuffer. Other platforms would
+ // associate the swapBuffer / flushBuffer API with the NSView
+ // rather than the NSOpenGLContext. For now, assume that
+ // calling flushBuffer on one context is the same as calling a
+ // "swapBuffers" function for the NSView.
+ return;
+ }
+ }
+ }
+
+ public int lockSurface() throws GLException {
+ if (!realized) {
+ return LOCK_SURFACE_NOT_READY;
+ }
+ if (nsView != 0) {
+ throw new GLException("Surface already locked");
+ }
+ ds = getJAWT().GetDrawingSurface(component);
+ if (ds == null) {
+ // Widget not yet realized
+ return LOCK_SURFACE_NOT_READY;
+ }
+ 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 context so it will be recreated (NOTE: removeNotify
+ // should handle this case, but it may be possible that race
+ // conditions can cause this code to be triggered -- should test
+ // more)
+ int ret = LOCK_SUCCESS;
+ if ((res & JAWTFactory.JAWT_LOCK_SURFACE_CHANGED) != 0) {
+ ret = LOCK_SURFACE_CHANGED;
+ }
+ if (firstLock) {
+ AccessController.doPrivileged(new PrivilegedAction() {
+ public Object run() {
+ dsi = ds.GetDrawingSurfaceInfo();
+ return null;
+ }
+ });
+ } else {
+ dsi = ds.GetDrawingSurfaceInfo();
+ }
+ if (dsi == null) {
+ // Widget not yet realized
+ ds.Unlock();
+ getJAWT().FreeDrawingSurface(ds);
+ ds = null;
+ return LOCK_SURFACE_NOT_READY;
+ }
+ firstLock = false;
+ macosxdsi = (JAWT_MacOSXDrawingSurfaceInfo) dsi.platformInfo();
+ if (macosxdsi == null) {
+ // Widget not yet realized
+ ds.FreeDrawingSurfaceInfo(dsi);
+ ds.Unlock();
+ getJAWT().FreeDrawingSurface(ds);
+ ds = null;
+ dsi = null;
+ return LOCK_SURFACE_NOT_READY;
+ }
+ nsView = macosxdsi.cocoaViewRef();
+ if (nsView == 0) {
+ // Widget not yet realized
+ ds.FreeDrawingSurfaceInfo(dsi);
+ ds.Unlock();
+ getJAWT().FreeDrawingSurface(ds);
+ ds = null;
+ dsi = null;
+ macosxdsi = null;
+ return LOCK_SURFACE_NOT_READY;
+ }
+ return ret;
+ }
+
+ public 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;
+ }
+
+ //----------------------------------------------------------------------
+ // Internals only below this point
+ //
+
+ private JAWT getJAWT()
+ {
+ 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;
+ }
+} \ No newline at end of file
diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXPbufferGLContext.java b/src/net/java/games/jogl/impl/macosx/MacOSXPbufferGLContext.java
index b163bb9ce..02e5c1ead 100644
--- a/src/net/java/games/jogl/impl/macosx/MacOSXPbufferGLContext.java
+++ b/src/net/java/games/jogl/impl/macosx/MacOSXPbufferGLContext.java
@@ -7,7 +7,12 @@ import net.java.games.jogl.*;
import net.java.games.jogl.impl.*;
public class MacOSXPbufferGLContext extends MacOSXGLContext {
- private static final boolean DEBUG = Debug.debug("MacOSXPbufferGLContext");
+ protected MacOSXPbufferGLDrawable drawable;
+
+ // State for render-to-texture and render-to-texture-rectangle support
+ private int textureTarget; // e.g. GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_NV
+ private int texture; // actual texture object
+
private static boolean isTigerOrLater;
static {
@@ -22,33 +27,20 @@ public class MacOSXPbufferGLContext extends MacOSXGLContext {
int minor = Integer.parseInt(tok.nextToken());
isTigerOrLater = ((major > 10) || (minor > 3));
}
-
- protected int initWidth;
- protected int initHeight;
-
- private long pBuffer;
-
- protected int width;
- protected int height;
-
- // State for render-to-texture and render-to-texture-rectangle support
- private boolean created;
- private int textureTarget; // e.g. GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_NV
- private int texture; // actual texture object
- public MacOSXPbufferGLContext(GLCapabilities capabilities, int initialWidth, int initialHeight) {
- super(null, capabilities, null, null);
- this.initWidth = initialWidth;
- this.initHeight = initialHeight;
+ public MacOSXPbufferGLContext(MacOSXPbufferGLDrawable drawable,
+ GLContext shareWith) {
+ super(drawable, shareWith);
+ this.drawable = drawable;
}
public boolean canCreatePbufferContext() {
return false;
}
- public GLContext createPbufferContext(GLCapabilities capabilities,
- int initialWidth,
- int initialHeight) {
+ public GLDrawableImpl createPbufferDrawable(GLCapabilities capabilities,
+ int initialWidth,
+ int initialHeight) {
throw new GLException("Not supported");
}
@@ -58,56 +50,14 @@ public class MacOSXPbufferGLContext extends MacOSXGLContext {
// FIXME: not clear whether this is really necessary, but since
// the API docs seem to imply it is and since it doesn't seem to
// impact performance, leaving it in
- CGL.setContextTextureImageToPBuffer(nsContext, pBuffer, GL.GL_FRONT);
+ CGL.setContextTextureImageToPBuffer(nsContext, drawable.getPbuffer(), GL.GL_FRONT);
}
public void releasePbufferFromTexture() {
}
- public void createPbuffer(long parentView, long parentContext) {
- GL gl = getGL();
- // Must initally grab OpenGL function pointers while parent's
- // context is current because otherwise we don't have the cgl
- // extensions available to us
- resetGLFunctionAvailability();
-
- int renderTarget;
- if (capabilities.getOffscreenRenderToTextureRectangle()) {
- width = initWidth;
- height = initHeight;
- renderTarget = GL.GL_TEXTURE_RECTANGLE_EXT;
- } else {
- width = getNextPowerOf2(initWidth);
- height = getNextPowerOf2(initHeight);
- renderTarget = GL.GL_TEXTURE_2D;
- }
-
- int internalFormat = GL.GL_RGBA;
- if (capabilities.getOffscreenFloatingPointBuffers()) {
- if (!gl.isExtensionAvailable("GL_APPLE_float_pixels")) {
- throw new GLException("Floating-point support (GL_APPLE_float_pixels) not available");
- }
- switch (capabilities.getRedBits()) {
- case 16: internalFormat = GL.GL_RGBA_FLOAT16_APPLE; break;
- case 32: internalFormat = GL.GL_RGBA_FLOAT32_APPLE; break;
- default: throw new GLException("Invalid floating-point bit depth (only 16 and 32 supported)");
- }
- }
-
- pBuffer = CGL.createPBuffer(renderTarget, internalFormat, width, height);
- if (pBuffer == 0) {
- throw new GLException("pbuffer creation error: CGL.createPBuffer() failed");
- }
-
- if (DEBUG) {
- System.err.println("Created pbuffer 0x" + Long.toHexString(pBuffer) + ", " + width + " x " + height + " for " + this);
- }
- }
-
protected int makeCurrentImpl() throws GLException {
- created = false;
-
- if (pBuffer == 0) {
+ if (drawable.getPbuffer() == 0) {
if (DEBUG) {
System.err.println("Pbuffer not instantiated yet for " + this);
}
@@ -116,9 +66,9 @@ public class MacOSXPbufferGLContext extends MacOSXGLContext {
}
int res = super.makeCurrentImpl();
- if (created) {
+ if (res == CONTEXT_CURRENT_NEW) {
// Initialize render-to-texture support if requested
- boolean rect = capabilities.getOffscreenRenderToTextureRectangle();
+ boolean rect = drawable.getCapabilities().getOffscreenRenderToTextureRectangle();
GL gl = getGL();
if (rect) {
if (!gl.isExtensionAvailable("GL_EXT_texture_rectangle")) {
@@ -136,58 +86,17 @@ public class MacOSXPbufferGLContext extends MacOSXGLContext {
gl.glTexParameteri(textureTarget, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
gl.glTexParameteri(textureTarget, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
gl.glTexParameteri(textureTarget, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);
- gl.glCopyTexImage2D(textureTarget, 0, GL.GL_RGB, 0, 0, width, height, 0);
+ gl.glCopyTexImage2D(textureTarget, 0, GL.GL_RGB, 0, 0, drawable.getWidth(), drawable.getHeight(), 0);
}
return res;
}
- public void destroyPBuffer() {
- if (this.pBuffer != 0) {
- CGL.destroyPBuffer(nsContext, pBuffer);
- }
- this.pBuffer = 0;
-
- if (DEBUG) {
- System.err.println("Destroyed pbuffer " + width + " x " + height);
- }
- }
-
- public void handleModeSwitch(long parentView, long parentContext) {
- throw new GLException("Not yet implemented");
- }
-
- protected boolean isOffscreen() {
- // FIXME: currently the only caller of this won't cause proper
- // resizing of the pbuffer anyway.
- return false;
- }
-
- protected void destroyImpl() throws GLException {
- destroyPBuffer();
- }
-
- public void swapBuffers() throws GLException {
- // FIXME: do we need to do anything if the pbuffer is double-buffered?
- }
-
public int getFloatingPointMode() {
return GLPbuffer.APPLE_FLOAT;
}
- private int getNextPowerOf2(int number) {
- if (((number-1) & number) == 0) {
- //ex: 8 -> 0b1000; 8-1=7 -> 0b0111; 0b1000&0b0111 == 0
- return number;
- }
- int power = 0;
- while (number > 0) {
- number = number>>1;
- power++;
- }
- return (1<<power);
- }
-
protected boolean create() {
+ GLCapabilities capabilities = drawable.getCapabilities();
if (capabilities.getOffscreenFloatingPointBuffers() &&
!isTigerOrLater) {
throw new GLException("Floating-point pbuffers supported only on OS X 10.4 or later");
@@ -195,9 +104,8 @@ public class MacOSXPbufferGLContext extends MacOSXGLContext {
if (!super.create(true, capabilities.getOffscreenFloatingPointBuffers())) {
return false;
}
- created = true;
// Must now associate the pbuffer with our newly-created context
- CGL.setContextPBuffer(nsContext, pBuffer);
+ CGL.setContextPBuffer(nsContext, drawable.getPbuffer());
return true;
}
}
diff --git a/src/net/java/games/jogl/impl/macosx/MacOSXPbufferGLDrawable.java b/src/net/java/games/jogl/impl/macosx/MacOSXPbufferGLDrawable.java
new file mode 100644
index 000000000..fc5f3c22e
--- /dev/null
+++ b/src/net/java/games/jogl/impl/macosx/MacOSXPbufferGLDrawable.java
@@ -0,0 +1,152 @@
+/*
+ * 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
+ * MICROSYSTEMS, 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.*;
+
+public class MacOSXPbufferGLDrawable extends MacOSXGLDrawable {
+ private static final boolean DEBUG = Debug.debug("MacOSXPbufferGLContext");
+
+ protected int initWidth;
+ protected int initHeight;
+
+ protected long pBuffer;
+
+ protected int width;
+ protected int height;
+
+ // State for render-to-texture and render-to-texture-rectangle support
+ private int textureTarget; // e.g. GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_NV
+ private int texture; // actual texture object
+
+ public MacOSXPbufferGLDrawable(GLCapabilities capabilities, int initialWidth, int initialHeight) {
+ super(capabilities, null);
+ this.initWidth = initialWidth;
+ this.initHeight = initialHeight;
+ }
+
+ public GLContext createContext(GLContext shareWith) {
+ return new MacOSXPbufferGLContext(this, shareWith);
+ }
+
+ public void destroy() {
+ if (this.pBuffer != 0) {
+ CGL.destroyPBuffer(0, pBuffer);
+ this.pBuffer = 0;
+
+ if (DEBUG) {
+ System.err.println("Destroyed pbuffer " + width + " x " + height);
+ }
+ }
+ }
+
+ public void setSize(int width, int height) {
+ // FIXME
+ throw new GLException("Not yet implemented");
+ }
+
+ public int getWidth() {
+ return width;
+ }
+
+ public int getHeight() {
+ return height;
+ }
+
+ public void createPbuffer(GL gl) {
+ int renderTarget;
+ if (capabilities.getOffscreenRenderToTextureRectangle()) {
+ width = initWidth;
+ height = initHeight;
+ renderTarget = GL.GL_TEXTURE_RECTANGLE_EXT;
+ } else {
+ width = getNextPowerOf2(initWidth);
+ height = getNextPowerOf2(initHeight);
+ renderTarget = GL.GL_TEXTURE_2D;
+ }
+
+ int internalFormat = GL.GL_RGBA;
+ if (capabilities.getOffscreenFloatingPointBuffers()) {
+ if (!gl.isExtensionAvailable("GL_APPLE_float_pixels")) {
+ throw new GLException("Floating-point support (GL_APPLE_float_pixels) not available");
+ }
+ switch (capabilities.getRedBits()) {
+ case 16: internalFormat = GL.GL_RGBA_FLOAT16_APPLE; break;
+ case 32: internalFormat = GL.GL_RGBA_FLOAT32_APPLE; break;
+ default: throw new GLException("Invalid floating-point bit depth (only 16 and 32 supported)");
+ }
+ }
+
+ pBuffer = CGL.createPBuffer(renderTarget, internalFormat, width, height);
+ if (pBuffer == 0) {
+ throw new GLException("pbuffer creation error: CGL.createPBuffer() failed");
+ }
+
+ if (DEBUG) {
+ System.err.println("Created pbuffer 0x" + toHexString(pBuffer) + ", " + width + " x " + height + " for " + this);
+ }
+ }
+
+ public GLCapabilities getCapabilities() {
+ return capabilities;
+ }
+
+ public long getPbuffer() {
+ return pBuffer;
+ }
+
+ public void swapBuffers() throws GLException {
+ // FIXME: do we need to do anything if the pbuffer is double-buffered?
+ }
+
+ private int getNextPowerOf2(int number) {
+ if (((number-1) & number) == 0) {
+ //ex: 8 -> 0b1000; 8-1=7 -> 0b0111; 0b1000&0b0111 == 0
+ return number;
+ }
+ int power = 0;
+ while (number > 0) {
+ number = number>>1;
+ power++;
+ }
+ return (1<<power);
+ }
+}