29package com.jogamp.opengl.test.junit.jogl.swt;
31import java.lang.reflect.InvocationTargetException;
33import org.eclipse.swt.SWT ;
35import org.eclipse.swt.layout.FillLayout ;
37import org.eclipse.swt.widgets.Composite ;
38import org.eclipse.swt.widgets.Display ;
39import org.eclipse.swt.widgets.Shell ;
41import org.junit.Assert;
42import org.junit.Assume;
44import org.junit.FixMethodOrder;
45import org.junit.runners.MethodSorters;
47import com.jogamp.opengl.GLAutoDrawable;
49import com.jogamp.opengl.GLProfile;
51import jogamp.newt.swt.SWTEDTUtil;
52import jogamp.newt.swt.event.SWTNewtEventFactory;
54import com.jogamp.common.util.InterruptSource;
55import com.jogamp.nativewindow.swt.SWTAccessor;
56import com.jogamp.newt.NewtFactory;
59import com.jogamp.opengl.swt.GLCanvas;
60import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
61import com.jogamp.opengl.test.junit.util.MiscUtils;
62import com.jogamp.opengl.test.junit.util.NewtTestUtil;
63import com.jogamp.opengl.test.junit.util.SWTTestUtil;
64import com.jogamp.opengl.test.junit.util.TestUtil;
65import com.jogamp.opengl.test.junit.util.UITestCase;
66import com.jogamp.opengl.util.Animator;
71@FixMethodOrder(MethodSorters.NAME_ASCENDING)
74 static int duration = 500;
75 static boolean useAnimator =
false;
79 static void resetSWTAndNEWTEDTCounter() {
80 synchronized(swtCountSync) {
83 synchronized(edtCountSync) {
87 static int incrSWTCount() {
88 synchronized(swtCountSync) {
93 static int getSWTCount() {
94 synchronized(swtCountSync) {
98 static int incrNEWTCount() {
99 synchronized(edtCountSync) {
104 static int getNEWTCount() {
105 synchronized(edtCountSync) {
109 static Object swtCountSync =
new Object();
110 static int swtCount = 0;
111 static Object edtCountSync =
new Object();
112 static int edtCount = 0;
116 static class AsyncExecEDTFeederThread
extends InterruptSource.Thread {
117 volatile boolean shallStop =
false;
118 private final Display swtDisplay ;
119 private final jogamp.newt.DisplayImpl newtDisplay;
120 private int swtN, newtN ;
122 public AsyncExecEDTFeederThread(
final Display swtDisplay,
final com.jogamp.newt.Display newtDisplay )
124 this.swtDisplay = swtDisplay ;
125 this.newtDisplay = (jogamp.newt.DisplayImpl)newtDisplay;
128 final Runnable swtAsyncAction =
new Runnable() {
131 ++swtN ; incrSWTCount();
132 System.err.println(
"[SWT A-i shallStop "+shallStop+
"]: Counter[loc "+swtN+
", glob: "+getSWTCount()+
"]");
135 final Runnable newtAsyncAction =
new Runnable() {
138 ++newtN ; incrNEWTCount();
139 System.err.println(
"[NEWT A-i shallStop "+shallStop+
"]: Counter[loc "+newtN+
", glob: "+getNEWTCount()+
"]");
144 System.err.println(
"[A-0 shallStop "+shallStop+
"]");
146 while( !shallStop && !swtDisplay.isDisposed() )
150 if( !swtDisplay.isDisposed() ) {
151 swtDisplay.asyncExec( swtAsyncAction );
153 if(
null != newtDisplay && newtDisplay.isNativeValid() && newtDisplay.getEDTUtil().isRunning()) {
155 newtDisplay.runOnEDTIfAvail(
false, newtAsyncAction);
157 java.lang.Thread.sleep( 50L ) ;
158 }
catch(
final InterruptedException e ) {
162 System.err.println(
"*R-Exit* shallStop "+shallStop);
168 private volatile boolean shallStop =
false;
170 static class SWT_DSC {
178 display =
new Display();
179 Assert.assertNotNull( display );
182 display.syncExec(
new Runnable() {
184 shell =
new Shell( display );
185 Assert.assertNotNull( shell );
186 shell.setLayout(
new FillLayout() );
187 composite =
new Composite( shell, SWT.NO_BACKGROUND );
188 composite.setLayout(
new FillLayout() );
189 Assert.assertNotNull( composite );
193 public void dispose() {
194 Assert.assertNotNull( display );
195 Assert.assertNotNull( shell );
196 Assert.assertNotNull( composite );
198 display.syncExec(
new Runnable() {
208 catch(
final Throwable throwable ) {
209 throwable.printStackTrace();
210 Assume.assumeNoException( throwable );
218 private void testImpl(
final boolean useJOGLGLCanvas,
final boolean useNewtCanvasSWT,
final boolean glWindowPreVisible)
throws InterruptedException, InvocationTargetException {
219 resetSWTAndNEWTEDTCounter();
221 final SWT_DSC dsc =
new SWT_DSC();
230 if( useJOGLGLCanvas ) {
233 final SWTNewtEventFactory swtNewtEventFactory =
new SWTNewtEventFactory();
234 dsc.display.syncExec(
new Runnable() {
242 }
else if( useNewtCanvasSWT ) {
247 if( glWindowPreVisible ) {
248 newtDisplay.
setEDTUtil(
new SWTEDTUtil(newtDisplay, dsc.display));
256 throw new InternalError(
"XXX");
264 System.err.println(
"**** Pre Shell Open");
265 dsc.display.syncExec(
new Runnable() {
267 dsc.shell.setText(
"NewtCanvasSWT Resize Bug Demo" ) ;
268 dsc.shell.setSize( 400, 450 ) ;
271 System.err.println(
"**** Post Shell Open");
275 final int[] counterBeforeExit =
new int[] { 0 , 0 };
277 final AsyncExecEDTFeederThread asyncExecFeeder;
279 asyncExecFeeder =
new AsyncExecEDTFeederThread(dsc.display, newtDisplay) ;
280 asyncExecFeeder.start() ;
284 final Thread t =
new InterruptSource.Thread(
null,
new Runnable() {
288 Thread.sleep(duration);
289 }
catch (
final InterruptedException e) {}
291 counterBeforeExit[0] = getSWTCount();
292 counterBeforeExit[1] = getNEWTCount();
293 asyncExecFeeder.shallStop =
true;
296 asyncExecFeeder.join();
297 }
catch(
final InterruptedException e ) { }
308 final Display d = dsc.display;
309 while( !shallStop && !d.isDisposed() ) {
310 generalWaitAction.run();
312 }
catch (
final Exception e0) {
313 e0.printStackTrace();
314 Assert.assertTrue(
"Deadlock @ dispatch: "+e0,
false);
321 System.err.println(
"EDT Counter before exit: SWT " + counterBeforeExit[0] +
", NEWT "+counterBeforeExit[1]);
322 Assert.assertTrue(
"SWT EDT Counter not greater zero before dispose!", 0 < counterBeforeExit[0]);
323 if(
null != newtDisplay ) {
324 Assert.assertTrue(
"NEWT EDT Counter not greater zero before dispose!", 0 < counterBeforeExit[1]);
330 testImpl(
true ,
false ,
false );
335 testImpl(
false ,
true ,
false );
340 testImpl(
false ,
true ,
true );
343 public static void main(
final String[] args ) {
344 for(
int i=0; i<args.length; i++) {
345 if(args[i].equals(
"-time")) {
347 }
else if(args[i].equals(
"-anim")) {
351 System.out.println(
"durationPerTest: "+duration);
static void invokeOnOSTKThread(final boolean blocking, final Runnable runnable)
Runs the specified action in an SWT compatible OS toolkit thread, which is:
abstract EDTUtil setEDTUtil(EDTUtil usrEDTUtil)
Sets a new EDTUtil and returns the previous one.
static Display createDisplay(final String name)
Create a Display entity.
static Screen createScreen(final Display display, final int index)
Create a Screen entity.
A screen may span multiple MonitorDevices representing their combined virtual size.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
SWT Canvas containing a NEWT Window using native parenting.
static NewtCanvasSWT create(final Composite parent, final int style, final Window child)
Creates an instance using NewtCanvasSWT(Composite, int, Window) on the SWT thread.
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
static final String GL2
The desktop OpenGL profile 1.x up to 3.0.
A heavyweight AWT component which provides OpenGL rendering support.
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
void test01JOGLGLCanvas()
void test02NewtCanvasSWTSimple()
void test02NewtCanvasSWTPreVisible()
static void main(final String[] args)
static int atoi(final String str, final int def)
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
final synchronized boolean start()
Starts this animator, if not running.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.