Let otherwise generic AWT utilities classes take a Component instead of the more specific AWTGLCanvas

This commit is contained in:
Elias Naur 2008-04-06 14:39:32 +00:00
parent bc33b3c4d5
commit 350c3c2661
10 changed files with 42 additions and 33 deletions

View File

@ -33,6 +33,7 @@ package org.lwjgl.opengl;
import java.awt.GraphicsConfiguration; import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice; import java.awt.GraphicsDevice;
import java.awt.Component;
import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLException;
@ -46,7 +47,7 @@ interface AWTCanvasImplementation {
/** /**
* Return an opaque handle to the canvas peer information required to create a context from it. * Return an opaque handle to the canvas peer information required to create a context from it.
*/ */
PeerInfo createPeerInfo(AWTGLCanvas canvas, PixelFormat pixel_format) throws LWJGLException; PeerInfo createPeerInfo(Component component, PixelFormat pixel_format) throws LWJGLException;
/** /**
* Find a proper GraphicsConfiguration from the given GraphicsDevice and PixelFormat. * Find a proper GraphicsConfiguration from the given GraphicsDevice and PixelFormat.

View File

@ -89,7 +89,7 @@ public class AWTGLCanvas extends Canvas implements Drawable, ComponentListener,
implementation = createImplementation(); implementation = createImplementation();
} }
private static AWTCanvasImplementation createImplementation() { static AWTCanvasImplementation createImplementation() {
switch (LWJGLUtil.getPlatform()) { switch (LWJGLUtil.getPlatform()) {
case LWJGLUtil.PLATFORM_LINUX: case LWJGLUtil.PLATFORM_LINUX:
return new LinuxCanvasImplementation(); return new LinuxCanvasImplementation();

View File

@ -31,7 +31,7 @@
*/ */
package org.lwjgl.opengl; package org.lwjgl.opengl;
import java.awt.Canvas; import java.awt.Component;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedActionException; import java.security.PrivilegedActionException;
@ -60,8 +60,8 @@ final class AWTSurfaceLock {
private static native ByteBuffer createHandle(); private static native ByteBuffer createHandle();
public ByteBuffer lockAndGetHandle(Canvas canvas) throws LWJGLException { public ByteBuffer lockAndGetHandle(Component component) throws LWJGLException {
while (!privilegedLockAndInitHandle(canvas)) { while (!privilegedLockAndInitHandle(component)) {
LWJGLUtil.log("Could not get drawing surface info, retrying..."); LWJGLUtil.log("Could not get drawing surface info, retrying...");
try { try {
Thread.sleep(WAIT_DELAY_MILLIS); Thread.sleep(WAIT_DELAY_MILLIS);
@ -73,20 +73,20 @@ final class AWTSurfaceLock {
return lock_buffer; return lock_buffer;
} }
private boolean privilegedLockAndInitHandle(final Canvas canvas) throws LWJGLException { private boolean privilegedLockAndInitHandle(final Component component) throws LWJGLException {
// Workaround for Sun JDK bug 4796548 which still exists in java for OS X // Workaround for Sun JDK bug 4796548 which still exists in java for OS X
// We need to elevate privileges because of an AWT bug. Please see // We need to elevate privileges because of an AWT bug. Please see
// http://192.18.37.44/forums/index.php?topic=10572 for a discussion. // http://192.18.37.44/forums/index.php?topic=10572 for a discussion.
// It is only needed on first call, so we avoid it on all subsequent calls // It is only needed on first call, so we avoid it on all subsequent calls
// due to performance. // due to performance.
if (firstLockSucceeded) if (firstLockSucceeded)
return lockAndInitHandle(lock_buffer, canvas); return lockAndInitHandle(lock_buffer, component);
else else
try { try {
final Object result = AccessController.doPrivileged(new PrivilegedExceptionAction() { final Object result = AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws LWJGLException { public Object run() throws LWJGLException {
return Boolean.valueOf(lockAndInitHandle(lock_buffer, canvas)); return Boolean.valueOf(lockAndInitHandle(lock_buffer, component));
} }
}); });
firstLockSucceeded = ((Boolean) result).booleanValue(); firstLockSucceeded = ((Boolean) result).booleanValue();
@ -96,7 +96,7 @@ final class AWTSurfaceLock {
} }
} }
private static native boolean lockAndInitHandle(ByteBuffer lock_buffer, Canvas canvas) throws LWJGLException; private static native boolean lockAndInitHandle(ByteBuffer lock_buffer, Component component) throws LWJGLException;
protected void unlock() throws LWJGLException { protected void unlock() throws LWJGLException {
nUnlock(lock_buffer); nUnlock(lock_buffer);

View File

@ -35,6 +35,7 @@ import java.nio.ByteBuffer;
import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil; import org.lwjgl.LWJGLUtil;
import java.awt.Component;
/** /**
* *
@ -43,16 +44,16 @@ import org.lwjgl.LWJGLUtil;
* $Id$ * $Id$
*/ */
final class LinuxAWTGLCanvasPeerInfo extends LinuxPeerInfo { final class LinuxAWTGLCanvasPeerInfo extends LinuxPeerInfo {
private final AWTGLCanvas canvas; private final Component component;
private final AWTSurfaceLock awt_surface = new AWTSurfaceLock(); private final AWTSurfaceLock awt_surface = new AWTSurfaceLock();
private int screen = -1; private int screen = -1;
public LinuxAWTGLCanvasPeerInfo(AWTGLCanvas canvas) { public LinuxAWTGLCanvasPeerInfo(Component component) {
this.canvas = canvas; this.component = component;
} }
protected void doLockAndInitHandle() throws LWJGLException { protected void doLockAndInitHandle() throws LWJGLException {
ByteBuffer surface_handle = awt_surface.lockAndGetHandle(canvas); ByteBuffer surface_handle = awt_surface.lockAndGetHandle(component);
if (screen == -1) { if (screen == -1) {
try { try {
screen = getScreenFromSurfaceInfo(surface_handle); screen = getScreenFromSurfaceInfo(surface_handle);

View File

@ -33,6 +33,7 @@ package org.lwjgl.opengl;
import java.awt.GraphicsConfiguration; import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice; import java.awt.GraphicsDevice;
import java.awt.Component;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedExceptionAction; import java.security.PrivilegedExceptionAction;
@ -75,8 +76,8 @@ final class LinuxCanvasImplementation implements AWTCanvasImplementation {
} }
} }
public PeerInfo createPeerInfo(AWTGLCanvas canvas, PixelFormat pixel_format) throws LWJGLException { public PeerInfo createPeerInfo(Component component, PixelFormat pixel_format) throws LWJGLException {
return new LinuxAWTGLCanvasPeerInfo(canvas); return new LinuxAWTGLCanvasPeerInfo(component);
} }
/** /**

View File

@ -33,6 +33,8 @@ package org.lwjgl.opengl;
import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLException;
import java.awt.Component;
/** /**
* *
* @author elias_naur <elias_naur@users.sourceforge.net> * @author elias_naur <elias_naur@users.sourceforge.net>
@ -40,14 +42,14 @@ import org.lwjgl.LWJGLException;
* $Id$ * $Id$
*/ */
final class MacOSXAWTGLCanvasPeerInfo extends MacOSXCanvasPeerInfo { final class MacOSXAWTGLCanvasPeerInfo extends MacOSXCanvasPeerInfo {
private final AWTGLCanvas canvas; private final Component component;
public MacOSXAWTGLCanvasPeerInfo(AWTGLCanvas canvas, PixelFormat pixel_format, boolean support_pbuffer) throws LWJGLException { public MacOSXAWTGLCanvasPeerInfo(Component component, PixelFormat pixel_format, boolean support_pbuffer) throws LWJGLException {
super(pixel_format, support_pbuffer); super(pixel_format, support_pbuffer);
this.canvas = canvas; this.component = component;
} }
protected void doLockAndInitHandle() throws LWJGLException { protected void doLockAndInitHandle() throws LWJGLException {
initHandle(canvas); initHandle(component);
} }
} }

View File

@ -33,6 +33,7 @@ package org.lwjgl.opengl;
import java.awt.GraphicsConfiguration; import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice; import java.awt.GraphicsDevice;
import java.awt.Component;
import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLException;
@ -43,11 +44,11 @@ import org.lwjgl.LWJGLException;
* $Id$ * $Id$
*/ */
final class MacOSXCanvasImplementation implements AWTCanvasImplementation { final class MacOSXCanvasImplementation implements AWTCanvasImplementation {
public PeerInfo createPeerInfo(AWTGLCanvas canvas, PixelFormat pixel_format) throws LWJGLException { public PeerInfo createPeerInfo(Component component, PixelFormat pixel_format) throws LWJGLException {
try { try {
return new MacOSXAWTGLCanvasPeerInfo(canvas, pixel_format, true); return new MacOSXAWTGLCanvasPeerInfo(component, pixel_format, true);
} catch (LWJGLException e) { } catch (LWJGLException e) {
return new MacOSXAWTGLCanvasPeerInfo(canvas, pixel_format, false); return new MacOSXAWTGLCanvasPeerInfo(component, pixel_format, false);
} }
} }

View File

@ -31,7 +31,7 @@
*/ */
package org.lwjgl.opengl; package org.lwjgl.opengl;
import java.awt.Canvas; import java.awt.Component;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLException;
@ -49,8 +49,8 @@ abstract class MacOSXCanvasPeerInfo extends MacOSXPeerInfo {
super(pixel_format, true, true, support_pbuffer, true); super(pixel_format, true, true, support_pbuffer, true);
} }
protected void initHandle(Canvas canvas) throws LWJGLException { protected void initHandle(Component component) throws LWJGLException {
nInitHandle(awt_surface.lockAndGetHandle(canvas), getHandle()); nInitHandle(awt_surface.lockAndGetHandle(component), getHandle());
} }
private static native void nInitHandle(ByteBuffer surface_buffer, ByteBuffer peer_info_handle) throws LWJGLException; private static native void nInitHandle(ByteBuffer surface_buffer, ByteBuffer peer_info_handle) throws LWJGLException;

View File

@ -35,6 +35,8 @@ import java.nio.ByteBuffer;
import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLException;
import java.awt.Component;
/** /**
* *
* @author elias_naur <elias_naur@users.sourceforge.net> * @author elias_naur <elias_naur@users.sourceforge.net>
@ -42,21 +44,21 @@ import org.lwjgl.LWJGLException;
* $Id$ * $Id$
*/ */
final class WindowsAWTGLCanvasPeerInfo extends WindowsPeerInfo { final class WindowsAWTGLCanvasPeerInfo extends WindowsPeerInfo {
private final AWTGLCanvas canvas; private final Component component;
private final AWTSurfaceLock awt_surface = new AWTSurfaceLock(); private final AWTSurfaceLock awt_surface = new AWTSurfaceLock();
private final PixelFormat pixel_format; private final PixelFormat pixel_format;
private boolean has_pixel_format= false; private boolean has_pixel_format= false;
public WindowsAWTGLCanvasPeerInfo(AWTGLCanvas canvas, PixelFormat pixel_format) { public WindowsAWTGLCanvasPeerInfo(Component component, PixelFormat pixel_format) {
this.canvas = canvas; this.component = component;
this.pixel_format = pixel_format; this.pixel_format = pixel_format;
} }
protected void doLockAndInitHandle() throws LWJGLException { protected void doLockAndInitHandle() throws LWJGLException {
nInitHandle(awt_surface.lockAndGetHandle(canvas), getHandle()); nInitHandle(awt_surface.lockAndGetHandle(component), getHandle());
if (!has_pixel_format) { if (!has_pixel_format && pixel_format != null) {
// If we haven't applied a pixel format yet, do it now // If we haven't applied a pixel format yet, do it now
choosePixelFormat(canvas.getX(), canvas.getY(), pixel_format, null, true, true, false, true); choosePixelFormat(component.getX(), component.getY(), pixel_format, null, true, true, false, true);
has_pixel_format = true; has_pixel_format = true;
} }
} }

View File

@ -34,6 +34,7 @@ package org.lwjgl.opengl;
import java.awt.GraphicsConfiguration; import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice; import java.awt.GraphicsDevice;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.Component;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.security.AccessController; import java.security.AccessController;
@ -69,8 +70,8 @@ final class WindowsCanvasImplementation implements AWTCanvasImplementation {
return new WindowsAWTInput(canvas); return new WindowsAWTInput(canvas);
} }
public PeerInfo createPeerInfo(AWTGLCanvas canvas, PixelFormat pixel_format) throws LWJGLException { public PeerInfo createPeerInfo(Component component, PixelFormat pixel_format) throws LWJGLException {
return new WindowsAWTGLCanvasPeerInfo(canvas, pixel_format); return new WindowsAWTGLCanvasPeerInfo(component, pixel_format);
} }
/** /**