diff options
Diffstat (limited to 'src/newt/classes/com')
6 files changed, 284 insertions, 48 deletions
diff --git a/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java b/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java index 6f0028a77..89a749c51 100644 --- a/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java +++ b/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java @@ -68,6 +68,15 @@ import com.jogamp.newt.event.awt.AWTAdapter; import com.jogamp.newt.event.awt.AWTKeyAdapter; import com.jogamp.newt.event.awt.AWTMouseAdapter; +/** + * AWT {@link java.awt.Canvas Canvas} containing a NEWT {@link Window} using native parenting. + * + * <h5><A NAME="java2dgl">Offscreen Layer Remarks</A></h5> + * + * {@link OffscreenLayerOption#setShallUseOffscreenLayer(boolean) setShallUseOffscreenLayer(true)} + * maybe called to use an offscreen drawable (FBO or PBuffer) allowing + * the underlying JAWT mechanism to composite the image, if supported. + */ @SuppressWarnings("serial") public class NewtCanvasAWT extends java.awt.Canvas implements WindowClosingProtocol, OffscreenLayerOption { public static final boolean DEBUG = Debug.debug("Window"); diff --git a/src/newt/classes/com/jogamp/newt/event/awt/AWTWindowAdapter.java b/src/newt/classes/com/jogamp/newt/event/awt/AWTWindowAdapter.java index 69b0d0482..2d63ca455 100644 --- a/src/newt/classes/com/jogamp/newt/event/awt/AWTWindowAdapter.java +++ b/src/newt/classes/com/jogamp/newt/event/awt/AWTWindowAdapter.java @@ -28,6 +28,8 @@ package com.jogamp.newt.event.awt; +import java.awt.Dimension; + import jogamp.newt.awt.event.AWTNewtEventFactory; public class AWTWindowAdapter @@ -61,9 +63,9 @@ public class AWTWindowAdapter if(awtComponent instanceof java.awt.Window) { ((java.awt.Window)awtComponent).addWindowListener(this); } - return this; + return this; } - + public AWTAdapter removeFrom(java.awt.Component awtComponent) { awtComponent.removeFocusListener(this); awtComponent.removeComponentListener(this); @@ -89,6 +91,9 @@ public class AWTWindowAdapter public void focusGained(java.awt.event.FocusEvent e) { com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); + if(DEBUG_IMPLEMENTATION) { + System.err.println("AWT: focusGained: "+e+" -> "+event); + } if(null!=newtListener) { ((com.jogamp.newt.event.WindowListener)newtListener).windowGainedFocus(event); } else { @@ -98,6 +103,9 @@ public class AWTWindowAdapter public void focusLost(java.awt.event.FocusEvent e) { com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); + if(DEBUG_IMPLEMENTATION) { + System.err.println("AWT: focusLost: "+e+" -> "+event); + } if(null!=newtListener) { ((com.jogamp.newt.event.WindowListener)newtListener).windowLostFocus(event); } else { @@ -108,7 +116,19 @@ public class AWTWindowAdapter public void componentResized(java.awt.event.ComponentEvent e) { com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); if(DEBUG_IMPLEMENTATION) { - System.err.println("AWT: componentResized: "+event); + final java.awt.Component c = e.getComponent(); + final java.awt.Dimension sz = c.getSize(); + final java.awt.Insets insets; + final java.awt.Dimension sz2; + if(c instanceof java.awt.Container) { + insets = ((java.awt.Container)c).getInsets(); + sz2 = new Dimension(sz.width - insets.left - insets.right, + sz.height - insets.top - insets.bottom); + } else { + insets = null; + sz2 = sz; + } + System.err.println("AWT: componentResized: "+sz+" ( "+insets+", "+sz2+" ), "+e+" -> "+event); } if(null!=newtListener) { ((com.jogamp.newt.event.WindowListener)newtListener).windowResized(event); @@ -120,7 +140,7 @@ public class AWTWindowAdapter public void componentMoved(java.awt.event.ComponentEvent e) { com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); if(DEBUG_IMPLEMENTATION) { - System.err.println("AWT: componentMoved: "+event); + System.err.println("AWT: componentMoved: "+e+" -> "+event); } if(null!=newtListener) { ((com.jogamp.newt.event.WindowListener)newtListener).windowMoved(event); diff --git a/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java b/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java index 0fc1b4e89..a89ccaedb 100644 --- a/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java +++ b/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java @@ -51,6 +51,7 @@ import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLCapabilitiesImmutable; import javax.media.opengl.GLContext; +import javax.media.opengl.GLDrawable; import javax.media.opengl.GLDrawableFactory; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLException; @@ -97,7 +98,7 @@ public class GLWindow extends GLAutoDrawableBase implements GLAutoDrawable, Wind * Constructor. Do not call this directly -- use {@link #create()} instead. */ protected GLWindow(Window window) { - super(null, null, false); + super(null, null, false /* always handle device lifecycle ourselves */); this.window = (WindowImpl) window; this.window.setHandleDestroyNotify(false); window.addWindowListener(new WindowAdapter() { @@ -107,8 +108,8 @@ public class GLWindow extends GLAutoDrawableBase implements GLAutoDrawable, Wind } @Override - public void windowResized(WindowEvent e) { - defaultWindowResizedOp(); + public void windowResized(WindowEvent e) { + defaultWindowResizedOp(getWidth(), getHeight()); } @Override @@ -201,11 +202,8 @@ public class GLWindow extends GLAutoDrawableBase implements GLAutoDrawable, Wind @Override public final CapabilitiesImmutable getChosenCapabilities() { - if (drawable == null) { - return window.getChosenCapabilities(); - } - - return drawable.getChosenGLCapabilities(); + final GLDrawable _drawable = drawable; + return null != _drawable ? _drawable.getChosenGLCapabilities() : window.getChosenCapabilities(); } @Override @@ -536,19 +534,24 @@ public class GLWindow extends GLAutoDrawableBase implements GLAutoDrawable, Wind return; } + final boolean done; final RecursiveLock lock = window.getLock(); lock.lock(); // sync: context/drawable could have been recreated/destroyed while animating try { if( null != context ) { // surface is locked/unlocked implicit by context's makeCurrent/release helper.invokeGL(drawable, context, defaultDisplayAction, defaultInitAction); - } else if( 0<getWidth()*getHeight() ) { - // retry drawable and context creation, will itself issue resize -> display - setVisible(true); + done = true; + } else { + done = false; } } finally { lock.unlock(); } + if( !done && 0<getWidth()*getHeight() ) { + // retry drawable and context creation, will itself issue resize -> display + setVisible(true); + } } //---------------------------------------------------------------------- diff --git a/src/newt/classes/com/jogamp/newt/swt/NewtCanvasSWT.java b/src/newt/classes/com/jogamp/newt/swt/NewtCanvasSWT.java index f45b864fa..36bc3f28f 100644 --- a/src/newt/classes/com/jogamp/newt/swt/NewtCanvasSWT.java +++ b/src/newt/classes/com/jogamp/newt/swt/NewtCanvasSWT.java @@ -63,13 +63,13 @@ import com.jogamp.newt.Window; import com.jogamp.newt.event.WindowEvent; import com.jogamp.newt.util.EDTUtil; +/** + * SWT {@link Canvas} containing a NEWT {@link Window} using native parenting. + */ public class NewtCanvasSWT extends Canvas implements WindowClosingProtocol { private static final boolean DEBUG = Debug.debug("Window"); private static final boolean isOSX = NativeWindowFactory.TYPE_MACOSX == NativeWindowFactory.getNativeWindowType(false); - /** SWT EDTUtil associated w/ parent's SWT Display */ - private final EDTUtil swtEDTUtil; - private final AbstractGraphicsScreen screen; private WindowClosingMode newtChildCloseOp = WindowClosingMode.DISPOSE_ON_CLOSE; @@ -117,8 +117,6 @@ public class NewtCanvasSWT extends Canvas implements WindowClosingProtocol { public NewtCanvasSWT(final Composite parent, final int style, Window child) { super(parent, style | SWT.NO_BACKGROUND); - swtEDTUtil = new SWTEDTUtil(parent.getDisplay()); - SWTAccessor.setRealized(this, true); clientArea = getClientArea(); @@ -326,8 +324,9 @@ public class NewtCanvasSWT extends Canvas implements WindowClosingProtocol { updateSizeCheck(); final int w = clientArea.width; final int h = clientArea.height; - - newtChild.getScreen().getDisplay().setEDTUtil(swtEDTUtil); + + final Display newtDisplay = newtChild.getScreen().getDisplay(); + newtDisplay.setEDTUtil(new SWTEDTUtil(newtDisplay, getDisplay())); newtChild.setSize(w, h); newtChild.reparentWindow(nativeWindow); diff --git a/src/newt/classes/com/jogamp/newt/swt/SWTEDTUtil.java b/src/newt/classes/com/jogamp/newt/swt/SWTEDTUtil.java index 3538caad2..d4b83d891 100644 --- a/src/newt/classes/com/jogamp/newt/swt/SWTEDTUtil.java +++ b/src/newt/classes/com/jogamp/newt/swt/SWTEDTUtil.java @@ -27,7 +27,9 @@ */ package com.jogamp.newt.swt; -import org.eclipse.swt.widgets.Display; +import java.awt.EventQueue; + +import jogamp.newt.Debug; import com.jogamp.newt.util.EDTUtil; @@ -36,64 +38,243 @@ import com.jogamp.newt.util.EDTUtil; * of the given {@link Display}. */ public class SWTEDTUtil implements EDTUtil { - private final Display swtDisplay; + public static final boolean DEBUG = Debug.debug("EDT"); + + private final Object edtLock = new Object(); // locking the EDT start/stop state + private final ThreadGroup threadGroup; + private final String name; + private final Runnable dispatchMessages; + private final org.eclipse.swt.widgets.Display swtDisplay; + private NewtEventDispatchThread nedt = null; + private int start_iter=0; + private static long pollPeriod = EDTUtil.defaultEDTPollPeriod; - public SWTEDTUtil(Display swtDisplay) { + public SWTEDTUtil(final com.jogamp.newt.Display newtDisplay, org.eclipse.swt.widgets.Display swtDisplay) { + this.threadGroup = Thread.currentThread().getThreadGroup(); + this.name=Thread.currentThread().getName()+"-SWTDisplay-"+newtDisplay.getFQName()+"-EDT-"; + this.dispatchMessages = new Runnable() { + public void run() { + ((jogamp.newt.DisplayImpl) newtDisplay).dispatchMessages(); + } }; this.swtDisplay = swtDisplay; + this.nedt = new NewtEventDispatchThread(threadGroup, name); + this.nedt.setDaemon(true); // don't stop JVM from shutdown .. } - public final Display getDisplay() { + public final org.eclipse.swt.widgets.Display getDisplay() { return swtDisplay; } @Override public long getPollPeriod() { - return 0; + return pollPeriod; } @Override public void setPollPeriod(long ms) { + pollPeriod = ms; } @Override public void reset() { + synchronized(edtLock) { + waitUntilStopped(); + if(DEBUG) { + System.err.println(Thread.currentThread()+": EDT reset - edt: "+nedt); + } + this.nedt = new NewtEventDispatchThread(threadGroup, name); + this.nedt.setDaemon(true); // don't stop JVM from shutdown .. + } } - @Override - public void start() { + private final void startImpl() { + if(nedt.isAlive()) { + throw new RuntimeException("EDT Thread.isAlive(): true, isRunning: "+nedt.isRunning()+", edt: "+nedt); + } + start_iter++; + nedt.setName(name+start_iter); + nedt.shouldStop = false; + if(DEBUG) { + System.err.println(Thread.currentThread()+": EDT START - edt: "+nedt); + // Thread.dumpStack(); + } + nedt.start(); } - + @Override public boolean isCurrentThreadEDT() { return swtDisplay.getThread() == Thread.currentThread(); } @Override + public final boolean isCurrentThreadNEDT() { + return nedt == Thread.currentThread(); + } + + @Override + public final boolean isCurrentThreadEDTorNEDT() { + final Thread ct = Thread.currentThread(); + return ct == swtDisplay.getThread() || ct == nedt ; + } + + @Override public boolean isRunning() { - return true; + return nedt.isRunning() ; // SWT is always running } - + @Override - public void invokeStop(Runnable finalTask) { - swtDisplay.syncExec(finalTask); + public final void invokeStop(Runnable task) { + invokeImpl(true, task, true); } @Override - public void invoke(boolean wait, Runnable task) { + public final void invoke(boolean wait, Runnable task) { + invokeImpl(wait, task, false); + } + + private void invokeImpl(boolean wait, Runnable task, boolean stop) { + if(task == null) { + throw new RuntimeException("Null Runnable"); + } + synchronized(edtLock) { // lock the EDT status + if( nedt.shouldStop ) { + // drop task .. + if(DEBUG) { + System.err.println("Warning: EDT about (1) to stop, won't enqueue new task: "+nedt); + Thread.dumpStack(); + } + return; + } + // System.err.println(Thread.currentThread()+" XXX stop: "+stop+", tasks: "+edt.tasks.size()+", task: "+task); + // Thread.dumpStack(); + if(stop) { + nedt.shouldStop = true; + if(DEBUG) { + System.err.println(Thread.currentThread()+": EDT signal STOP (on edt: "+isCurrentThreadEDT()+") - "+nedt); + // Thread.dumpStack(); + } + } + + // start if should not stop && not started yet + if( !stop && !nedt.isRunning() ) { + startImpl(); + } + } if(wait) { swtDisplay.syncExec(task); } else { swtDisplay.asyncExec(task); } - } + } @Override - public void waitUntilIdle() { - // all sync .. + final public void waitUntilIdle() { + final NewtEventDispatchThread _edt; + synchronized(edtLock) { + _edt = nedt; + } + if(!_edt.isRunning() || EventQueue.isDispatchThread() || _edt == Thread.currentThread()) { + return; + } + try { + swtDisplay.syncExec(new Runnable() { + public void run() { } + }); + } catch (Exception e) { } } @Override - public void waitUntilStopped() { - // all sync .. + final public void waitUntilStopped() { + synchronized(edtLock) { + if(nedt.isRunning() && nedt != Thread.currentThread() ) { + while(nedt.isRunning()) { + try { + edtLock.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } } + + class NewtEventDispatchThread extends Thread { + volatile boolean shouldStop = false; + volatile boolean isRunning = false; + Object sync = new Object(); + + public NewtEventDispatchThread(ThreadGroup tg, String name) { + super(tg, name); + } + + final public boolean isRunning() { + return isRunning; + } + + @Override + final public void start() throws IllegalThreadStateException { + isRunning = true; + super.start(); + } + + /** + * Utilizing locking only on tasks and its execution, + * not for event dispatching. + */ + @Override + final public void run() { + if(DEBUG) { + System.err.println(getName()+": EDT run() START "+ getName()); + } + RuntimeException error = null; + try { + do { + // event dispatch + if(!shouldStop) { + // FIXME: Determine whether we require to run the + // delivery of events (dispatch) on AWT-EDT. + // Since the WindowDriver itself delivers all Window related events, + // this shall not be required. + // AWTEDTExecutor.singleton.invoke(true, dispatchMessages); + dispatchMessages.run(); + } + // wait + synchronized(sync) { + if(!shouldStop) { + try { + sync.wait(pollPeriod); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } while(!shouldStop) ; + } catch (Throwable t) { + // handle errors .. + shouldStop = true; + if(t instanceof RuntimeException) { + error = (RuntimeException) t; + } else { + error = new RuntimeException("Within EDT", t); + } + } finally { + if(DEBUG) { + System.err.println(getName()+": EDT run() END "+ getName()+", "+error); + } + synchronized(edtLock) { + isRunning = !shouldStop; + if(!isRunning) { + edtLock.notifyAll(); + } + } + if(DEBUG) { + System.err.println(getName()+": EDT run() EXIT "+ getName()+", exception: "+error); + } + if(null!=error) { + throw error; + } + } // finally + } // run() + } // EventDispatchThread + } diff --git a/src/newt/classes/com/jogamp/newt/util/EDTUtil.java b/src/newt/classes/com/jogamp/newt/util/EDTUtil.java index 4493e2781..7e19d9de5 100644 --- a/src/newt/classes/com/jogamp/newt/util/EDTUtil.java +++ b/src/newt/classes/com/jogamp/newt/util/EDTUtil.java @@ -28,6 +28,9 @@ package com.jogamp.newt.util; +import jogamp.newt.DisplayImpl; +import com.jogamp.newt.event.NEWTEvent; + /** * EDT stands for Event Dispatch Thread. * <p> @@ -63,25 +66,46 @@ public interface EDTUtil { /** * Create a new EDT. One should invoke <code>reset()</code><br> - * after <code>invokeStop(..)</code> in case another <code>start()</code> or <code>invoke(..)</code> + * after <code>invokeStop(..)</code> in case another start via <code>invoke(..)</code> * is expected. * - * @see #start() * @see #invoke(boolean, java.lang.Runnable) * @see #invokeStop(java.lang.Runnable) */ public void reset(); /** - * Start the EDT + * Returns true if the current thread is the event dispatch thread (EDT). + * <p> + * The EDT is the platform specific thread dispatching toolkit-events + * and executing toolkit-tasks enqueued via {@link #invoke(boolean, Runnable)}. + * </p> + * <p> + * Usually it is the same thread as used to dequeue informal {@link NEWTEvent}s (NEDT), see {@link #isCurrentThreadNEDT()}, + * however, this may differ, e.g. SWT and AWT implementation. + * </p> */ - public void start(); + public boolean isCurrentThreadEDT(); /** - * @return True if the current thread is the EDT thread + * Returns true if the current thread is the internal NEWT event dequeue thread (NEDT). + * <p> + * The NEDT is the NEWT thread used to dequeue informal {@link NEWTEvent}s enqueued internally + * via {@link DisplayImpl#enqueueEvent(boolean, NEWTEvent)}. + * </p> + * <p> + * Usually it is the same thread as the EDT, see {@link #isCurrentThreadEDT()}, + * however, this may differ, e.g. SWT and AWT implementation. + * </p> */ - public boolean isCurrentThreadEDT(); - + public boolean isCurrentThreadNEDT(); + + /** + * Returns <code>true</code> if either {@link #isCurrentThreadEDT()} or {@link #isCurrentThreadNEDT()} is <code>true</code>, + * otherwise <code>false</code>. + */ + public boolean isCurrentThreadEDTorNEDT(); + /** * @return True if EDT is running */ @@ -101,9 +125,9 @@ public interface EDTUtil { public void invokeStop(Runnable finalTask); /** + * Shall start the thread if not running.<br> * Append task to the EDT task queue.<br> * Wait until execution is finished if <code>wait == true</code>.<br> - * Shall start the thread if not running.<br> * Can be issued from within EDT, ie from within an enqueued task.<br> * * @throws RuntimeException in case EDT is stopped and not {@link #reset()} |