diff options
author | Sven Gothel <[email protected]> | 2013-11-25 04:25:04 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2013-11-25 04:25:04 +0100 |
commit | d5c25aa5584e98416208afef57610c85ac6c254c (patch) | |
tree | 6f56777890402c2f80218be16a6c6abbc1102890 /src | |
parent | 09b5d1ed25f9b5de13d2b0980c95121d0d5d7c0e (diff) |
Bug 672 (NewtCanvasSWT): Reuse SWTAccessor.isOS_TYPE ; Impl NW.getLocationOnScreen(..) for X11 and Windows ; Allow unit test to run on all platforms.
- Reuse SWTAccessor.isOS_TYPE (public now)
- Impl NW.getLocationOnScreen(..) for X11 and Windows reusing existing native code
- Allow unit test to run on all platforms.
Note: NewtCanvasSWT unit tests require a 'wait for realized' while SWT dispatching.
Otherwise the 'sash unit test' will fail since realiziation happens later, at least on X11.
Hence extended AWTRobotUtil.waitForRealized(..) to use a 'waitAction'
which is used here w/ special SWT dispatch Runnable.
AWTRobotUtil.waitForRealized(..) operates on time-delta instead of iteration-counter,
allowing above 'waitAction' Runnable.
AWTRobotUtil.waitForRealized(..) removed 2nd 'glad.isRealized()' loop ..
Diffstat (limited to 'src')
7 files changed, 312 insertions, 343 deletions
diff --git a/src/nativewindow/classes/com/jogamp/nativewindow/swt/SWTAccessor.java b/src/nativewindow/classes/com/jogamp/nativewindow/swt/SWTAccessor.java index 361d61c65..36bf646d5 100644 --- a/src/nativewindow/classes/com/jogamp/nativewindow/swt/SWTAccessor.java +++ b/src/nativewindow/classes/com/jogamp/nativewindow/swt/SWTAccessor.java @@ -63,10 +63,10 @@ public class SWTAccessor { private static Field swt_osx_view_id = null; private static final String nwt; - private static final boolean isOSX; - private static final boolean isWindows; - private static final boolean isX11; - private static final boolean isX11GTK; + public static final boolean isOSX; + public static final boolean isWindows; + public static final boolean isX11; + public static final boolean isX11GTK; // X11/GTK, Windows/GDI, .. private static final String str_handle = "handle"; diff --git a/src/newt/classes/com/jogamp/newt/swt/NewtCanvasSWT.java b/src/newt/classes/com/jogamp/newt/swt/NewtCanvasSWT.java index 029cee3da..5ed8d9e63 100644 --- a/src/newt/classes/com/jogamp/newt/swt/NewtCanvasSWT.java +++ b/src/newt/classes/com/jogamp/newt/swt/NewtCanvasSWT.java @@ -46,6 +46,8 @@ import javax.media.nativewindow.util.Point; import javax.media.opengl.GLCapabilities; import jogamp.nativewindow.macosx.OSXUtil; +import jogamp.nativewindow.windows.GDIUtil; +import jogamp.nativewindow.x11.X11Lib; import jogamp.newt.Debug; import jogamp.newt.swt.SWTEDTUtil; @@ -70,7 +72,6 @@ import com.jogamp.newt.util.EDTUtil; */ 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); private final AbstractGraphicsScreen screen; @@ -145,7 +146,9 @@ public class NewtCanvasSWT extends Canvas implements WindowClosingProtocol { newtChild.setSize(clientArea.width, clientArea.height); postSetSize = false; } - if( isOSX ) newtChild.setPosition(parent.getLocation().x,parent.getLocation().y); + if( SWTAccessor.isOSX ) { + newtChild.setPosition(parent.getLocation().x,parent.getLocation().y); + } newtChild.windowRepaint(0, 0, clientArea.width, clientArea.height); } } @@ -398,7 +401,7 @@ public class NewtCanvasSWT extends Canvas implements WindowClosingProtocol { public SWTNativeWindow(AbstractGraphicsConfiguration config, long nativeWindowHandle) { this.config = config; this.nativeWindowHandle = nativeWindowHandle; - if(isOSX) { + if( SWTAccessor.isOSX ) { this.insets = OSXUtil.GetInsets(nativeWindowHandle); } else { this.insets = new Insets(0, 0, 0, 0); @@ -501,22 +504,25 @@ public class NewtCanvasSWT extends Canvas implements WindowClosingProtocol { @Override public Point getLocationOnScreen(Point point) { - if( isOSX ) { - final Point los = OSXUtil.GetLocationOnScreen(nativeWindowHandle, false, 0, 0); - // top-level position -> client window position - final Rectangle swtCanvasPosition = getSWTCanvasPosition(); - los.set(swtCanvasPosition.x + los.getX() + insets.getLeftWidth(), swtCanvasPosition.y + los.getY() + insets.getTopHeight()); - if(null!=point) { - return point.translate(los); - } else { - return los; - } + final Point los; // client window location on screen + if( SWTAccessor.isOSX ) { + los = OSXUtil.GetLocationOnScreen(nativeWindowHandle, false, 0, 0); + // top-level position -> client window position: OSX needs to add SWT parent position incl. insets + final Rectangle swtCanvasPosition = getSWTCanvasPosition(); + los.translate(swtCanvasPosition.x + insets.getLeftWidth(), swtCanvasPosition.y + insets.getTopHeight()); + } else if (SWTAccessor.isX11) { + final AbstractGraphicsScreen s = config.getScreen(); + los = X11Lib.GetRelativeLocation(s.getDevice().getHandle(), s.getIndex(), nativeWindowHandle, 0 /*root win*/, 0, 0); + } else if (SWTAccessor.isWindows) { + los = GDIUtil.GetRelativeLocation( nativeWindowHandle, 0 /*root win*/, 0, 0); } else { - // client position on 'normal' windowing systems is 0/0 - if(null == point) { - point = new Point(0, 0); - } - return point; + // fall-back to 0/0 + los = new Point(0, 0); + } + if(null!=point) { + return point.translate(los); + } else { + return los; } } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestBug672NewtCanvasSWTSashForm.java b/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestBug672NewtCanvasSWTSashForm.java index f6f8918a3..4b7537655 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestBug672NewtCanvasSWTSashForm.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestBug672NewtCanvasSWTSashForm.java @@ -3,14 +3,14 @@ * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. - * + * * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR @@ -20,12 +20,12 @@ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * + * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of JogAmp Community. */ - + package com.jogamp.opengl.test.junit.jogl.swt; import java.io.IOException; @@ -43,17 +43,13 @@ import com.jogamp.opengl.test.junit.util.AWTRobotUtil; import com.jogamp.opengl.test.junit.util.MiscUtils; import com.jogamp.opengl.test.junit.util.UITestCase; import com.jogamp.opengl.test.junit.util.QuitAdapter; - import com.jogamp.opengl.util.Animator; - import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2; -import javax.media.nativewindow.NativeWindowFactory; import javax.media.nativewindow.util.Dimension; import javax.media.nativewindow.util.Point; import javax.media.nativewindow.util.PointImmutable; import javax.media.nativewindow.util.DimensionImmutable; - import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLCapabilitiesImmutable; import javax.media.opengl.GLProfile; @@ -75,28 +71,15 @@ import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; @FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class TestBug672NewtCanvasSWTSashForm extends UITestCase { +public class TestBug672NewtCanvasSWTSashForm extends UITestCase { static int screenIdx = 0; static PointImmutable wpos; static DimensionImmutable wsize, rwsize = null; static long duration = 500; // ms - static boolean opaque = true; - static int forceAlpha = -1; - static boolean fullscreen = false; - static boolean pmvUseBackingArray = true; - static int swapInterval = 1; - static boolean showFPS = false; - static int loops = 1; - static boolean loop_shutdown = false; - static boolean forceES2 = false; - static boolean forceGL3 = false; - static boolean mainRun = false; - static boolean exclusiveContext = false; - + @BeforeClass public static void initClass() { - setTestSupported(NativeWindowFactory.TYPE_MACOSX == NativeWindowFactory.getNativeWindowType(false)); if(null == wsize) { wsize = new Dimension(640, 480); } @@ -109,23 +92,28 @@ public class TestBug672NewtCanvasSWTSashForm extends UITestCase { Display display = null; Shell shell = null; Composite composite = null; + SashForm sash = null; com.jogamp.newt.Display swtNewtDisplay = null; - + @Before public void init() { SWTAccessor.invoke(true, new Runnable() { - public void run() { + public void run() { display = new Display(); Assert.assertNotNull( display ); }}); display.syncExec(new Runnable() { - public void run() { + public void run() { shell = new Shell( display ); Assert.assertNotNull( shell ); shell.setLayout( new FillLayout() ); composite = new Composite( shell, SWT.NONE ); composite.setLayout( new FillLayout() ); Assert.assertNotNull( composite ); + sash = new SashForm(composite, SWT.NONE); + Assert.assertNotNull( sash ); + final org.eclipse.swt.widgets.Label c = new org.eclipse.swt.widgets.Label(sash, SWT.NONE); + c.setText("Left cell"); }}); swtNewtDisplay = NewtFactory.createDisplay(null, false); // no-reuse } @@ -135,9 +123,11 @@ public class TestBug672NewtCanvasSWTSashForm extends UITestCase { Assert.assertNotNull( display ); Assert.assertNotNull( shell ); Assert.assertNotNull( composite ); + Assert.assertNotNull( sash ); try { display.syncExec(new Runnable() { public void run() { + sash.dispose(); composite.dispose(); shell.dispose(); }}); @@ -154,22 +144,38 @@ public class TestBug672NewtCanvasSWTSashForm extends UITestCase { display = null; shell = null; composite = null; + sash = null; + } + + class WaitAction implements Runnable { + private final long sleepMS; + + WaitAction(long sleepMS) { + this.sleepMS = sleepMS; + } + public void run() { + if( !display.readAndDispatch() ) { + // blocks on linux .. display.sleep(); + try { + Thread.sleep(sleepMS); + } catch (InterruptedException e) { } + } + } } - + final WaitAction awtRobotWaitAction = new WaitAction(AWTRobotUtil.TIME_SLICE); + final WaitAction generalWaitAction = new WaitAction(10); + protected void runTestGL(GLCapabilitiesImmutable caps) throws InterruptedException, InvocationTargetException { - System.err.println("requested: vsync "+swapInterval+", "+caps); com.jogamp.newt.Screen screen = NewtFactory.createScreen(swtNewtDisplay, screenIdx); final GLWindow glWindow = GLWindow.create(screen, caps); Assert.assertNotNull(glWindow); - - final GearsES2 demo = new GearsES2(swapInterval); - demo.setPMVUseBackingArray(pmvUseBackingArray); + + final GearsES2 demo = new GearsES2(1); glWindow.addGLEventListener(demo); - + Animator animator = new Animator(); animator.setModeBits(false, Animator.MODE_EXPECT_AWT_RENDERING_THREAD); - animator.setExclusiveContext(exclusiveContext); - + QuitAdapter quitAdapter = new QuitAdapter(); //glWindow.addKeyListener(new TraceKeyAdapter(quitAdapter)); //glWindow.addWindowListener(new TraceWindowAdapter(quitAdapter)); @@ -182,14 +188,14 @@ public class TestBug672NewtCanvasSWTSashForm extends UITestCase { } public void windowMoved(WindowEvent e) { System.err.println("window moved: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight()); - } + } }); - + glWindow.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) { if( !e.isPrintableKey() || e.isAutoRepeat() ) { return; - } + } if(e.getKeyChar()=='f') { new Thread() { public void run() { @@ -207,10 +213,7 @@ public class TestBug672NewtCanvasSWTSashForm extends UITestCase { animator.start(); Assert.assertTrue(animator.isStarted()); Assert.assertTrue(animator.isAnimating()); - Assert.assertEquals(exclusiveContext ? animator.getThread() : null, glWindow.getExclusiveContextThread()); - final SashForm sash = new SashForm(composite, SWT.NONE); - org.eclipse.swt.widgets.Label c = new org.eclipse.swt.widgets.Label(sash, SWT.NONE); - c.setText("Left cell"); + animator.setUpdateFPSFrames(60, null); final NewtCanvasSWT canvas1 = NewtCanvasSWT.create( sash, 0, glWindow ); Assert.assertNotNull( canvas1 ); @@ -224,116 +227,68 @@ public class TestBug672NewtCanvasSWTSashForm extends UITestCase { shell.open(); } }); - - animator.setUpdateFPSFrames(60, showFPS ? System.err : null); - + Assert.assertTrue("GLWindow didn't become visible natively!", AWTRobotUtil.waitForRealized(glWindow, awtRobotWaitAction, true)); + Assert.assertNotNull( canvas1.getNativeWindow() ); + System.err.println("NW chosen: "+glWindow.getDelegatedWindow().getChosenCapabilities()); System.err.println("GL chosen: "+glWindow.getChosenCapabilities()); - System.err.println("window pos/siz: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight()+", "+glWindow.getInsets()); - + System.err.println("window pos/siz.0: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight()+", "+glWindow.getInsets()); + System.err.println("GLWindow LOS.0: "+glWindow.getLocationOnScreen(null)); + System.err.println("NewtCanvasSWT LOS.0: "+canvas1.getNativeWindow().getLocationOnScreen(null)); + if( null != rwsize ) { for(int i=0; i<50; i++) { // 500 ms dispatched delay - if( !display.readAndDispatch() ) { - // blocks on linux .. display.sleep(); - Thread.sleep(10); - } + generalWaitAction.run(); } display.syncExec( new Runnable() { public void run() { shell.setSize( rwsize.getWidth(), rwsize.getHeight() ); } }); - System.err.println("window resize pos/siz: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight()+", "+glWindow.getInsets()); + System.err.println("window resize pos/siz.1: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight()+", "+glWindow.getInsets()); + System.err.println("GLWindow LOS.1: "+glWindow.getLocationOnScreen(null)); + System.err.println("NewtCanvasSWT LOS.1: "+canvas1.getNativeWindow().getLocationOnScreen(null)); } - - Assert.assertNotNull( canvas1.getNativeWindow() ); - Assert.assertNotEquals( canvas1.getNativeWindow().getLocationOnScreen(null), 0 ); - while(!quitAdapter.shouldQuit() && animator.isAnimating() && animator.getTotalFPSDuration()<duration) { - if( !display.readAndDispatch() ) { - // blocks on linux .. display.sleep(); - Thread.sleep(10); - } + final PointImmutable pSashRightClient = new Point(wsize.getWidth(), 0); + final PointImmutable pNatWinLOS = canvas1.getNativeWindow().getLocationOnScreen(null); + final PointImmutable pGLWinLOS = glWindow.getLocationOnScreen(null); + + System.err.println("GLWindow LOS: "+pGLWinLOS); + System.err.println("NewtCanvasSWT LOS: "+pNatWinLOS); + + Assert.assertTrue( "NewtCanvasAWT LOS "+pNatWinLOS+" not >= sash-right "+pSashRightClient, pNatWinLOS.compareTo(pSashRightClient) >= 0 ); + Assert.assertTrue( "GLWindow LOS "+pGLWinLOS+" not >= sash-right "+pSashRightClient, pGLWinLOS.compareTo(pSashRightClient) >= 0 ); + + while( !quitAdapter.shouldQuit() && animator.isAnimating() && animator.getTotalFPSDuration()<duration ) { + generalWaitAction.run(); } - Assert.assertEquals(exclusiveContext ? animator.getThread() : null, glWindow.getExclusiveContextThread()); animator.stop(); Assert.assertFalse(animator.isAnimating()); Assert.assertFalse(animator.isStarted()); Assert.assertEquals(null, glWindow.getExclusiveContextThread()); - + canvas1.dispose(); glWindow.destroy(); Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glWindow, false)); } @Test - public void test01GL2ES2() throws InterruptedException, InvocationTargetException { - for(int i=1; i<=loops; i++) { - System.err.println("Loop "+i+"/"+loops); - final GLProfile glp; - if(forceGL3) { - glp = GLProfile.get(GLProfile.GL3); - } else if(forceES2) { - glp = GLProfile.get(GLProfile.GLES2); - } else { - glp = GLProfile.getGL2ES2(); - } - final GLCapabilities caps = new GLCapabilities( glp ); - caps.setBackgroundOpaque(opaque); - if(-1 < forceAlpha) { - caps.setAlphaBits(forceAlpha); - } - runTestGL(caps); - if(loop_shutdown) { - GLProfile.shutdown(); - } - } - } - - @Test - public void test02GL3() throws InterruptedException, InvocationTargetException { - if(mainRun) return; - - if( !GLProfile.isAvailable(GLProfile.GL3) ) { - System.err.println("GL3 n/a"); - return; - } - final GLProfile glp = GLProfile.get(GLProfile.GL3); + public void test01() throws InterruptedException, InvocationTargetException { + final GLProfile glp = GLProfile.getGL2ES2(); final GLCapabilities caps = new GLCapabilities( glp ); runTestGL(caps); } - + public static void main(String args[]) throws IOException { - mainRun = true; - int x=0, y=0, w=640, h=480, rw=-1, rh=-1; boolean usePos = false; - + for(int i=0; i<args.length; i++) { if(args[i].equals("-time")) { i++; duration = MiscUtils.atol(args[i], duration); - } else if(args[i].equals("-translucent")) { - opaque = false; - } else if(args[i].equals("-forceAlpha")) { - i++; - forceAlpha = MiscUtils.atoi(args[i], 0); - } else if(args[i].equals("-fullscreen")) { - fullscreen = true; - } else if(args[i].equals("-pmvDirect")) { - pmvUseBackingArray = false; - } else if(args[i].equals("-vsync")) { - i++; - swapInterval = MiscUtils.atoi(args[i], swapInterval); - } else if(args[i].equals("-exclctx")) { - exclusiveContext = true; - } else if(args[i].equals("-es2")) { - forceES2 = true; - } else if(args[i].equals("-gl3")) { - forceGL3 = true; - } else if(args[i].equals("-showFPS")) { - showFPS = true; } else if(args[i].equals("-width")) { i++; w = MiscUtils.atoi(args[i], w); @@ -357,35 +312,20 @@ public class TestBug672NewtCanvasSWTSashForm extends UITestCase { } else if(args[i].equals("-screen")) { i++; screenIdx = MiscUtils.atoi(args[i], 0); - } else if(args[i].equals("-loops")) { - i++; - loops = MiscUtils.atoi(args[i], 1); - } else if(args[i].equals("-loop-shutdown")) { - loop_shutdown = true; } } wsize = new Dimension(w, h); if( 0 < rw && 0 < rh ) { rwsize = new Dimension(rw, rh); } - + if(usePos) { wpos = new Point(x, y); } System.err.println("position "+wpos); System.err.println("size "+wsize); - System.err.println("resize "+rwsize); + System.err.println("resize "+rwsize); System.err.println("screen "+screenIdx); - System.err.println("translucent "+(!opaque)); - System.err.println("forceAlpha "+forceAlpha); - System.err.println("fullscreen "+fullscreen); - System.err.println("pmvDirect "+(!pmvUseBackingArray)); - System.err.println("loops "+loops); - System.err.println("loop shutdown "+loop_shutdown); - System.err.println("forceES2 "+forceES2); - System.err.println("forceGL3 "+forceGL3); - System.err.println("swapInterval "+swapInterval); - System.err.println("exclusiveContext "+exclusiveContext); org.junit.runner.JUnitCore.main(TestBug672NewtCanvasSWTSashForm.class.getName()); } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestBug672NewtCanvasSWTSashFormComposite.java b/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestBug672NewtCanvasSWTSashFormComposite.java index c2d58534b..876eafe86 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestBug672NewtCanvasSWTSashFormComposite.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestBug672NewtCanvasSWTSashFormComposite.java @@ -3,14 +3,14 @@ * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. - * + * * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR @@ -20,12 +20,12 @@ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * + * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of JogAmp Community. */ - + package com.jogamp.opengl.test.junit.jogl.swt; import java.io.IOException; @@ -43,17 +43,13 @@ import com.jogamp.opengl.test.junit.util.AWTRobotUtil; import com.jogamp.opengl.test.junit.util.MiscUtils; import com.jogamp.opengl.test.junit.util.UITestCase; import com.jogamp.opengl.test.junit.util.QuitAdapter; - import com.jogamp.opengl.util.Animator; - import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2; -import javax.media.nativewindow.NativeWindowFactory; import javax.media.nativewindow.util.Dimension; import javax.media.nativewindow.util.Point; import javax.media.nativewindow.util.PointImmutable; import javax.media.nativewindow.util.DimensionImmutable; - import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLCapabilitiesImmutable; import javax.media.opengl.GLProfile; @@ -75,28 +71,15 @@ import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; @FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class TestBug672NewtCanvasSWTSashFormComposite extends UITestCase { +public class TestBug672NewtCanvasSWTSashFormComposite extends UITestCase { static int screenIdx = 0; static PointImmutable wpos; static DimensionImmutable wsize, rwsize = null; static long duration = 500; // ms - static boolean opaque = true; - static int forceAlpha = -1; - static boolean fullscreen = false; - static boolean pmvUseBackingArray = true; - static int swapInterval = 1; - static boolean showFPS = false; - static int loops = 1; - static boolean loop_shutdown = false; - static boolean forceES2 = false; - static boolean forceGL3 = false; - static boolean mainRun = false; - static boolean exclusiveContext = false; - + @BeforeClass public static void initClass() { - setTestSupported(NativeWindowFactory.TYPE_MACOSX == NativeWindowFactory.getNativeWindowType(false)); if(null == wsize) { wsize = new Dimension(640, 480); } @@ -109,23 +92,32 @@ public class TestBug672NewtCanvasSWTSashFormComposite extends UITestCase { Display display = null; Shell shell = null; Composite composite = null; + SashForm sash = null; + Composite innerComposite = null; com.jogamp.newt.Display swtNewtDisplay = null; - + @Before public void init() { SWTAccessor.invoke(true, new Runnable() { - public void run() { + public void run() { display = new Display(); Assert.assertNotNull( display ); }}); display.syncExec(new Runnable() { - public void run() { + public void run() { shell = new Shell( display ); Assert.assertNotNull( shell ); shell.setLayout( new FillLayout() ); composite = new Composite( shell, SWT.NONE ); composite.setLayout( new FillLayout() ); Assert.assertNotNull( composite ); + sash = new SashForm(composite, SWT.NONE); + Assert.assertNotNull( sash ); + final org.eclipse.swt.widgets.Label c = new org.eclipse.swt.widgets.Label(sash, SWT.NONE); + c.setText("Left cell"); + innerComposite = new Composite(sash, SWT.NONE); + Assert.assertNotNull( innerComposite ); + innerComposite.setLayout( new FillLayout() ); }}); swtNewtDisplay = NewtFactory.createDisplay(null, false); // no-reuse } @@ -135,9 +127,13 @@ public class TestBug672NewtCanvasSWTSashFormComposite extends UITestCase { Assert.assertNotNull( display ); Assert.assertNotNull( shell ); Assert.assertNotNull( composite ); + Assert.assertNotNull( sash ); + Assert.assertNotNull( innerComposite ); try { display.syncExec(new Runnable() { public void run() { + innerComposite.dispose(); + sash.dispose(); composite.dispose(); shell.dispose(); }}); @@ -154,22 +150,39 @@ public class TestBug672NewtCanvasSWTSashFormComposite extends UITestCase { display = null; shell = null; composite = null; + sash = null; + innerComposite = null; + } + + class WaitAction implements Runnable { + private final long sleepMS; + + WaitAction(long sleepMS) { + this.sleepMS = sleepMS; + } + public void run() { + if( !display.readAndDispatch() ) { + // blocks on linux .. display.sleep(); + try { + Thread.sleep(sleepMS); + } catch (InterruptedException e) { } + } + } } - + final WaitAction awtRobotWaitAction = new WaitAction(AWTRobotUtil.TIME_SLICE); + final WaitAction generalWaitAction = new WaitAction(10); + protected void runTestGL(GLCapabilitiesImmutable caps) throws InterruptedException, InvocationTargetException { - System.err.println("requested: vsync "+swapInterval+", "+caps); com.jogamp.newt.Screen screen = NewtFactory.createScreen(swtNewtDisplay, screenIdx); final GLWindow glWindow = GLWindow.create(screen, caps); Assert.assertNotNull(glWindow); - - final GearsES2 demo = new GearsES2(swapInterval); - demo.setPMVUseBackingArray(pmvUseBackingArray); + + final GearsES2 demo = new GearsES2(1); glWindow.addGLEventListener(demo); - + Animator animator = new Animator(); animator.setModeBits(false, Animator.MODE_EXPECT_AWT_RENDERING_THREAD); - animator.setExclusiveContext(exclusiveContext); - + QuitAdapter quitAdapter = new QuitAdapter(); //glWindow.addKeyListener(new TraceKeyAdapter(quitAdapter)); //glWindow.addWindowListener(new TraceWindowAdapter(quitAdapter)); @@ -182,14 +195,14 @@ public class TestBug672NewtCanvasSWTSashFormComposite extends UITestCase { } public void windowMoved(WindowEvent e) { System.err.println("window moved: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight()); - } + } }); - + glWindow.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) { if( !e.isPrintableKey() || e.isAutoRepeat() ) { return; - } + } if(e.getKeyChar()=='f') { new Thread() { public void run() { @@ -207,12 +220,7 @@ public class TestBug672NewtCanvasSWTSashFormComposite extends UITestCase { animator.start(); Assert.assertTrue(animator.isStarted()); Assert.assertTrue(animator.isAnimating()); - Assert.assertEquals(exclusiveContext ? animator.getThread() : null, glWindow.getExclusiveContextThread()); - final SashForm sash = new SashForm(composite, SWT.NONE); - org.eclipse.swt.widgets.Label c = new org.eclipse.swt.widgets.Label(sash, SWT.NONE); - c.setText("Left cell"); - Composite innerComposite = new Composite(sash, SWT.NONE); - innerComposite.setLayout( new FillLayout() ); + animator.setUpdateFPSFrames(60, null); final NewtCanvasSWT canvas1 = NewtCanvasSWT.create( innerComposite, 0, glWindow ); Assert.assertNotNull( canvas1 ); @@ -226,116 +234,68 @@ public class TestBug672NewtCanvasSWTSashFormComposite extends UITestCase { shell.open(); } }); - - animator.setUpdateFPSFrames(60, showFPS ? System.err : null); - + Assert.assertTrue("GLWindow didn't become visible natively!", AWTRobotUtil.waitForRealized(glWindow, awtRobotWaitAction, true)); + Assert.assertNotNull( canvas1.getNativeWindow() ); + System.err.println("NW chosen: "+glWindow.getDelegatedWindow().getChosenCapabilities()); System.err.println("GL chosen: "+glWindow.getChosenCapabilities()); - System.err.println("window pos/siz: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight()+", "+glWindow.getInsets()); - + System.err.println("window pos/siz.0: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight()+", "+glWindow.getInsets()); + System.err.println("GLWindow LOS.0: "+glWindow.getLocationOnScreen(null)); + System.err.println("NewtCanvasSWT LOS.0: "+canvas1.getNativeWindow().getLocationOnScreen(null)); + if( null != rwsize ) { for(int i=0; i<50; i++) { // 500 ms dispatched delay - if( !display.readAndDispatch() ) { - // blocks on linux .. display.sleep(); - Thread.sleep(10); - } + generalWaitAction.run(); } display.syncExec( new Runnable() { public void run() { shell.setSize( rwsize.getWidth(), rwsize.getHeight() ); } }); - System.err.println("window resize pos/siz: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight()+", "+glWindow.getInsets()); + System.err.println("window resize pos/siz.1: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight()+", "+glWindow.getInsets()); + System.err.println("GLWindow LOS.1: "+glWindow.getLocationOnScreen(null)); + System.err.println("NewtCanvasSWT LOS.1: "+canvas1.getNativeWindow().getLocationOnScreen(null)); } - - Assert.assertNotNull( canvas1.getNativeWindow() ); - Assert.assertTrue( canvas1.getNEWTChild().getX() - canvas1.getNEWTChild().getLocationOnScreen(null).getX() > 0 ); - while(!quitAdapter.shouldQuit() && animator.isAnimating() && animator.getTotalFPSDuration()<duration) { - if( !display.readAndDispatch() ) { - // blocks on linux .. display.sleep(); - Thread.sleep(10); - } + final PointImmutable pSashRightClient = new Point(wsize.getWidth(), 0); + final PointImmutable pNatWinLOS = canvas1.getNativeWindow().getLocationOnScreen(null); + final PointImmutable pGLWinLOS = glWindow.getLocationOnScreen(null); + + System.err.println("GLWindow LOS: "+pGLWinLOS); + System.err.println("NewtCanvasSWT LOS: "+pNatWinLOS); + + Assert.assertTrue( "NewtCanvasAWT LOS "+pNatWinLOS+" not >= sash-right "+pSashRightClient, pNatWinLOS.compareTo(pSashRightClient) >= 0 ); + Assert.assertTrue( "GLWindow LOS "+pGLWinLOS+" not >= sash-right "+pSashRightClient, pGLWinLOS.compareTo(pSashRightClient) >= 0 ); + + while( !quitAdapter.shouldQuit() && animator.isAnimating() && animator.getTotalFPSDuration()<duration ) { + generalWaitAction.run(); } - Assert.assertEquals(exclusiveContext ? animator.getThread() : null, glWindow.getExclusiveContextThread()); animator.stop(); Assert.assertFalse(animator.isAnimating()); Assert.assertFalse(animator.isStarted()); Assert.assertEquals(null, glWindow.getExclusiveContextThread()); - + canvas1.dispose(); glWindow.destroy(); Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glWindow, false)); } @Test - public void test01GL2ES2() throws InterruptedException, InvocationTargetException { - for(int i=1; i<=loops; i++) { - System.err.println("Loop "+i+"/"+loops); - final GLProfile glp; - if(forceGL3) { - glp = GLProfile.get(GLProfile.GL3); - } else if(forceES2) { - glp = GLProfile.get(GLProfile.GLES2); - } else { - glp = GLProfile.getGL2ES2(); - } - final GLCapabilities caps = new GLCapabilities( glp ); - caps.setBackgroundOpaque(opaque); - if(-1 < forceAlpha) { - caps.setAlphaBits(forceAlpha); - } - runTestGL(caps); - if(loop_shutdown) { - GLProfile.shutdown(); - } - } - } - - @Test - public void test02GL3() throws InterruptedException, InvocationTargetException { - if(mainRun) return; - - if( !GLProfile.isAvailable(GLProfile.GL3) ) { - System.err.println("GL3 n/a"); - return; - } - final GLProfile glp = GLProfile.get(GLProfile.GL3); + public void test01() throws InterruptedException, InvocationTargetException { + final GLProfile glp = GLProfile.getGL2ES2(); final GLCapabilities caps = new GLCapabilities( glp ); runTestGL(caps); } - + public static void main(String args[]) throws IOException { - mainRun = true; - int x=0, y=0, w=640, h=480, rw=-1, rh=-1; boolean usePos = false; - + for(int i=0; i<args.length; i++) { if(args[i].equals("-time")) { i++; duration = MiscUtils.atol(args[i], duration); - } else if(args[i].equals("-translucent")) { - opaque = false; - } else if(args[i].equals("-forceAlpha")) { - i++; - forceAlpha = MiscUtils.atoi(args[i], 0); - } else if(args[i].equals("-fullscreen")) { - fullscreen = true; - } else if(args[i].equals("-pmvDirect")) { - pmvUseBackingArray = false; - } else if(args[i].equals("-vsync")) { - i++; - swapInterval = MiscUtils.atoi(args[i], swapInterval); - } else if(args[i].equals("-exclctx")) { - exclusiveContext = true; - } else if(args[i].equals("-es2")) { - forceES2 = true; - } else if(args[i].equals("-gl3")) { - forceGL3 = true; - } else if(args[i].equals("-showFPS")) { - showFPS = true; } else if(args[i].equals("-width")) { i++; w = MiscUtils.atoi(args[i], w); @@ -359,35 +319,20 @@ public class TestBug672NewtCanvasSWTSashFormComposite extends UITestCase { } else if(args[i].equals("-screen")) { i++; screenIdx = MiscUtils.atoi(args[i], 0); - } else if(args[i].equals("-loops")) { - i++; - loops = MiscUtils.atoi(args[i], 1); - } else if(args[i].equals("-loop-shutdown")) { - loop_shutdown = true; } } wsize = new Dimension(w, h); if( 0 < rw && 0 < rh ) { rwsize = new Dimension(rw, rh); } - + if(usePos) { wpos = new Point(x, y); } System.err.println("position "+wpos); System.err.println("size "+wsize); - System.err.println("resize "+rwsize); + System.err.println("resize "+rwsize); System.err.println("screen "+screenIdx); - System.err.println("translucent "+(!opaque)); - System.err.println("forceAlpha "+forceAlpha); - System.err.println("fullscreen "+fullscreen); - System.err.println("pmvDirect "+(!pmvUseBackingArray)); - System.err.println("loops "+loops); - System.err.println("loop shutdown "+loop_shutdown); - System.err.println("forceES2 "+forceES2); - System.err.println("forceGL3 "+forceGL3); - System.err.println("swapInterval "+swapInterval); - System.err.println("exclusiveContext "+exclusiveContext); org.junit.runner.JUnitCore.main(TestBug672NewtCanvasSWTSashFormComposite.class.getName()); } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestNewtCanvasSWTBug628ResizeDeadlockAWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestNewtCanvasSWTBug628ResizeDeadlockAWT.java index 87d4dafd6..be3a11bfc 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestNewtCanvasSWTBug628ResizeDeadlockAWT.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestNewtCanvasSWTBug628ResizeDeadlockAWT.java @@ -33,13 +33,10 @@ import java.awt.Robot; import java.lang.reflect.InvocationTargetException; import org.eclipse.swt.SWT ; - import org.eclipse.swt.layout.FillLayout ; - import org.eclipse.swt.widgets.Composite ; import org.eclipse.swt.widgets.Display ; import org.eclipse.swt.widgets.Shell ; - import org.junit.Assert; import org.junit.Assume; import org.junit.Test; @@ -299,6 +296,22 @@ public class TestNewtCanvasSWTBug628ResizeDeadlockAWT extends UITestCase { shell = null; composite = null; } + class WaitAction implements Runnable { + private final long sleepMS; + + WaitAction(long sleepMS) { + this.sleepMS = sleepMS; + } + public void run() { + if( !display.readAndDispatch() ) { + // blocks on linux .. display.sleep(); + try { + Thread.sleep(sleepMS); + } catch (InterruptedException e) { } + } + } + } + final WaitAction awtRobotWaitAction = new WaitAction(AWTRobotUtil.TIME_SLICE); } @Test @@ -334,6 +347,7 @@ public class TestNewtCanvasSWTBug628ResizeDeadlockAWT extends UITestCase { dsc.shell.setSize( 400, 450 ) ; dsc.shell.open() ; } } ); + Assert.assertTrue("GLWindow didn't become visible natively!", AWTRobotUtil.waitForRealized(glWindow, dsc.awtRobotWaitAction, true)); AWTRobotUtil.requestFocus(robot, glWindow, false); AWTRobotUtil.setMouseToClientLocation(robot, glWindow, 50, 50); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestNewtCanvasSWTGLn.java b/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestNewtCanvasSWTGLn.java index bcb462953..5426ab7af 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestNewtCanvasSWTGLn.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestNewtCanvasSWTGLn.java @@ -3,14 +3,14 @@ * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. - * + * * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR @@ -20,12 +20,12 @@ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * + * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of JogAmp Community. */ - + package com.jogamp.opengl.test.junit.jogl.swt; import javax.media.opengl.GLAutoDrawable; @@ -39,7 +39,6 @@ import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; - import org.junit.Assert; import org.junit.Assume; import org.junit.Before; @@ -56,6 +55,7 @@ import com.jogamp.newt.opengl.GLWindow; import com.jogamp.newt.swt.NewtCanvasSWT; import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2; import com.jogamp.opengl.test.junit.jogl.demos.es2.MultisampleDemoES2; +import com.jogamp.opengl.test.junit.util.AWTRobotUtil; import com.jogamp.opengl.test.junit.util.MiscUtils; import com.jogamp.opengl.test.junit.util.UITestCase; import com.jogamp.opengl.util.Animator; @@ -64,14 +64,14 @@ import com.jogamp.opengl.util.texture.TextureIO; /** * Tests that a basic SWT app can open without crashing under different GL profiles - * _and_ custom GLCapabilities. - * <p> + * _and_ custom GLCapabilities. + * <p> * Uses JOGL's NewtCanvasSWT, which allows to be a native container of a NEWT Window.<br/> * This method allows utilizing custom GLCapability settings, * independent from the already instantiated SWT visual. * </p> * <p> - * Note that {@link SWTAccessor#invoke(boolean, Runnable)} is still used to comply w/ + * Note that {@link SWTAccessor#invoke(boolean, Runnable)} is still used to comply w/ * SWT running on Mac OSX, i.e. to enforce UI action on the main thread. * </p> */ @@ -87,7 +87,7 @@ public class TestNewtCanvasSWTGLn extends UITestCase { Shell shell = null; Composite composite = null; com.jogamp.newt.Display swtNewtDisplay = null; - + @BeforeClass public static void startup() { System.out.println( "GLProfile " + GLProfile.glAvailabilityToString() ); @@ -96,12 +96,12 @@ public class TestNewtCanvasSWTGLn extends UITestCase { @Before public void init() { SWTAccessor.invoke(true, new Runnable() { - public void run() { + public void run() { display = new Display(); Assert.assertNotNull( display ); }}); display.syncExec(new Runnable() { - public void run() { + public void run() { shell = new Shell( display ); Assert.assertNotNull( shell ); shell.setLayout( new FillLayout() ); @@ -138,10 +138,28 @@ public class TestNewtCanvasSWTGLn extends UITestCase { composite = null; } - protected void runTestAGL( GLCapabilitiesImmutable caps, GLEventListener demo, + class WaitAction implements Runnable { + private final long sleepMS; + + WaitAction(long sleepMS) { + this.sleepMS = sleepMS; + } + public void run() { + if( !display.readAndDispatch() ) { + // blocks on linux .. display.sleep(); + try { + Thread.sleep(sleepMS); + } catch (InterruptedException e) { } + } + } + } + final WaitAction awtRobotWaitAction = new WaitAction(AWTRobotUtil.TIME_SLICE); + final WaitAction generalWaitAction = new WaitAction(10); + + protected void runTestAGL( GLCapabilitiesImmutable caps, GLEventListener demo, boolean postAttach, boolean useAnimator ) throws InterruptedException { final GLReadBufferUtil screenshot = new GLReadBufferUtil(false, false); - + final Screen screen = NewtFactory.createScreen(swtNewtDisplay, 0); final GLWindow glWindow1 = GLWindow.create(screen, caps); Assert.assertNotNull(glWindow1); @@ -151,7 +169,7 @@ public class TestNewtCanvasSWTGLn extends UITestCase { glWindow1.addGLEventListener(demo); glWindow1.addGLEventListener(new GLEventListener() { int displayCount = 0; - public void init(final GLAutoDrawable drawable) { } + public void init(final GLAutoDrawable drawable) { } public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { } public void display(final GLAutoDrawable drawable) { if(displayCount < 3) { @@ -159,8 +177,8 @@ public class TestNewtCanvasSWTGLn extends UITestCase { } } public void dispose(final GLAutoDrawable drawable) { } - }); - + }); + final NewtCanvasSWT canvas1 = NewtCanvasSWT.create( composite, 0, postAttach ? null : glWindow1 ); Assert.assertNotNull( canvas1 ); @@ -171,13 +189,18 @@ public class TestNewtCanvasSWTGLn extends UITestCase { shell.open(); } }); - + if(postAttach) { canvas1.setNEWTChild(glWindow1); } - + + Assert.assertTrue("GLWindow didn't become visible natively!", AWTRobotUtil.waitForRealized(glWindow1, awtRobotWaitAction, true)); + + System.err.println("GLWindow LOS.0: "+glWindow1.getLocationOnScreen(null)); + System.err.println("NewtCanvasSWT LOS.0: "+canvas1.getNativeWindow().getLocationOnScreen(null)); + // canvas1.update(); - + Animator anim; if(useAnimator) { anim = new Animator(glWindow1); @@ -185,15 +208,12 @@ public class TestNewtCanvasSWTGLn extends UITestCase { } else { anim = null; } - + long lStartTime = System.currentTimeMillis(); long lEndTime = lStartTime + duration; try { while( (System.currentTimeMillis() < lEndTime) && !canvas1.isDisposed() ) { - if( !display.readAndDispatch() ) { - // blocks on linux .. display.sleep(); - Thread.sleep(10); - } + generalWaitAction.run(); } } catch( Throwable throwable ) { throwable.printStackTrace(); @@ -202,7 +222,7 @@ public class TestNewtCanvasSWTGLn extends UITestCase { if(null != anim) { anim.stop(); } - + canvas1.dispose(); } @@ -220,7 +240,7 @@ public class TestNewtCanvasSWTGLn extends UITestCase { public void postAttach_WithAnimator() throws InterruptedException { runTestAGL( new GLCapabilities(GLProfile.getGL2ES2()), new GearsES2(), true /* postAttach */, true /* animator */); } - + @Test public void test_MultisampleAndAlpha() throws InterruptedException { GLCapabilities caps = new GLCapabilities(GLProfile.getGL2ES2()); diff --git a/src/test/com/jogamp/opengl/test/junit/util/AWTRobotUtil.java b/src/test/com/jogamp/opengl/test/junit/util/AWTRobotUtil.java index cd14835bb..657936adc 100644 --- a/src/test/com/jogamp/opengl/test/junit/util/AWTRobotUtil.java +++ b/src/test/com/jogamp/opengl/test/junit/util/AWTRobotUtil.java @@ -682,53 +682,97 @@ public class AWTRobotUtil { } /** + * @param obj the component to wait for + * @param realized true if waiting for component to become realized, otherwise false * @return True if the Component becomes realized (not displayable, native invalid) within TIME_OUT + * @throws InterruptedException */ public static boolean waitForRealized(Object obj, boolean realized) throws InterruptedException { - int wait; + return waitForRealized(obj, null, realized); + } + + /** + * @param obj the component to wait for + * @param waitAction if not null, Runnable shall wait {@link #TIME_SLICE} ms, if appropriate + * @param realized true if waiting for component to become realized, otherwise false + * @return True if the Component becomes realized (not displayable, native invalid) within TIME_OUT + * @throws InterruptedException + */ + public static boolean waitForRealized(Object obj, Runnable waitAction, boolean realized) throws InterruptedException { + long t0 = System.currentTimeMillis(); + long t1 = t0; if(obj instanceof com.jogamp.newt.Screen) { com.jogamp.newt.Screen screen = (com.jogamp.newt.Screen) obj; - for (wait=0; wait<POLL_DIVIDER && realized != screen.isNativeValid(); wait++) { - Thread.sleep(TIME_SLICE); + while( (t1-t0) < TIME_OUT && realized != screen.isNativeValid() ) { + if( null != waitAction ) { + waitAction.run(); + } else { + Thread.sleep(TIME_SLICE); + } + t1 = System.currentTimeMillis(); } } else if(obj instanceof com.jogamp.newt.Window) { com.jogamp.newt.Window win = (com.jogamp.newt.Window) obj; - for (wait=0; wait<POLL_DIVIDER && realized != win.isNativeValid(); wait++) { - Thread.sleep(TIME_SLICE); + while( (t1-t0) < TIME_OUT && realized != win.isNativeValid() ) { + if( null != waitAction ) { + waitAction.run(); + } else { + Thread.sleep(TIME_SLICE); + } + t1 = System.currentTimeMillis(); } } else if (NativeWindowFactory.isAWTAvailable() && obj instanceof java.awt.Component) { java.awt.Component comp = (java.awt.Component) obj; - for (wait=0; wait<POLL_DIVIDER && realized != comp.isDisplayable(); wait++) { - Thread.sleep(TIME_SLICE); + while( (t1-t0) < TIME_OUT && realized != comp.isDisplayable() ) { + if( null != waitAction ) { + waitAction.run(); + } else { + Thread.sleep(TIME_SLICE); + } + t1 = System.currentTimeMillis(); } // if GLCanvas, ensure it got also painted -> drawable.setRealized(true); - if(wait<POLL_DIVIDER && comp instanceof GLAutoDrawable) { + if( (t1-t0) < TIME_OUT && comp instanceof GLAutoDrawable) { GLAutoDrawable glad = (GLAutoDrawable) comp; - for (wait=0; wait<POLL_DIVIDER && realized != glad.isRealized(); wait++) { - Thread.sleep(TIME_SLICE); + t0 = System.currentTimeMillis(); + while( (t1-t0) < TIME_OUT && realized != glad.isRealized() ) { + if( null != waitAction ) { + waitAction.run(); + } else { + Thread.sleep(TIME_SLICE); + } + t1 = System.currentTimeMillis(); } - if(wait>=POLL_DIVIDER) { + if( (t1-t0) >= TIME_OUT ) { // for some reason GLCanvas hasn't been painted yet, force it! System.err.println("XXX: FORCE REPAINT PRE - glad: "+glad); comp.repaint(); - for (wait=0; wait<POLL_DIVIDER && realized != glad.isRealized(); wait++) { - Thread.sleep(TIME_SLICE); + t0 = System.currentTimeMillis(); + while( (t1-t0) < TIME_OUT && realized != glad.isRealized() ) { + if( null != waitAction ) { + waitAction.run(); + } else { + Thread.sleep(TIME_SLICE); + } + t1 = System.currentTimeMillis(); } System.err.println("XXX: FORCE REPAINT POST - glad: "+glad); } - for (wait=0; wait<POLL_DIVIDER && realized != glad.isRealized(); wait++) { - Thread.sleep(TIME_SLICE); - } } } else if(obj instanceof GLAutoDrawable) { GLAutoDrawable glad = (GLAutoDrawable) obj; - for (wait=0; wait<POLL_DIVIDER && realized != glad.isRealized(); wait++) { - Thread.sleep(TIME_SLICE); + while( (t1-t0) < TIME_OUT && realized != glad.isRealized() ) { + if( null != waitAction ) { + waitAction.run(); + } else { + Thread.sleep(TIME_SLICE); + } + t1 = System.currentTimeMillis(); } } else { throw new RuntimeException("Neither AWT nor NEWT nor GLAutoDrawable: "+obj); } - return wait<POLL_DIVIDER; + return (t1-t0) < TIME_OUT; } /** |