JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGearsES2NewtCanvasSWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2011 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.opengl.test.junit.jogl.demos.es2.newt;
30
31import java.io.IOException;
32import java.lang.reflect.InvocationTargetException;
33
34import com.jogamp.nativewindow.NativeWindowFactory;
35import com.jogamp.nativewindow.swt.SWTAccessor;
36import com.jogamp.newt.NewtFactory;
37import com.jogamp.newt.event.KeyAdapter;
38import com.jogamp.newt.event.KeyEvent;
39import com.jogamp.newt.event.WindowEvent;
40import com.jogamp.newt.event.WindowAdapter;
41import com.jogamp.newt.opengl.GLWindow;
42import com.jogamp.newt.opengl.util.NEWTDemoListener;
43import com.jogamp.newt.swt.NewtCanvasSWT;
44import com.jogamp.opengl.test.junit.util.GLTestUtil;
45import com.jogamp.opengl.test.junit.util.MiscUtils;
46import com.jogamp.opengl.test.junit.util.NewtTestUtil;
47import com.jogamp.opengl.test.junit.util.UITestCase;
48import com.jogamp.opengl.test.junit.util.QuitAdapter;
49import com.jogamp.opengl.test.junit.util.SWTTestUtil;
50import com.jogamp.opengl.util.Animator;
51import com.jogamp.opengl.util.AnimatorBase;
52
53import jogamp.newt.DisplayImpl;
54
55import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
56
57import com.jogamp.nativewindow.util.Dimension;
58import com.jogamp.nativewindow.util.Point;
59import com.jogamp.nativewindow.util.PointImmutable;
60import com.jogamp.nativewindow.util.DimensionImmutable;
61import com.jogamp.opengl.GLCapabilities;
62import com.jogamp.opengl.GLCapabilitiesImmutable;
63import com.jogamp.opengl.GLProfile;
64
65import org.eclipse.swt.SWT;
66import org.eclipse.swt.layout.FillLayout;
67import org.eclipse.swt.widgets.Composite;
68import org.eclipse.swt.widgets.Display;
69import org.eclipse.swt.widgets.Shell;
70import org.junit.After;
71import org.junit.Assert;
72import org.junit.Assume;
73import org.junit.Before;
74import org.junit.BeforeClass;
75import org.junit.AfterClass;
76import org.junit.Test;
77import org.junit.FixMethodOrder;
78import org.junit.runners.MethodSorters;
79
80@FixMethodOrder(MethodSorters.NAME_ASCENDING)
82 static int screenIdx = 0;
83 static PointImmutable wpos;
84 static DimensionImmutable wsize, rwsize = null;
85
86 static long duration = 500; // ms
87 static boolean opaque = true;
88 static int forceAlpha = -1;
89 static boolean fullscreen = false;
90 static int swapInterval = 1;
91 static boolean showFPS = false;
92 static int loops = 1;
93 static boolean loop_shutdown = false;
94 static boolean forceES2 = false;
95 static boolean forceGL3 = false;
96 static boolean mainRun = false;
97 static boolean exclusiveContext = false;
98
99 @BeforeClass
100 public static void initClass() {
101 if(null == wsize) {
102 wsize = new Dimension(640, 480);
103 }
104 }
105
106 @AfterClass
107 public static void releaseClass() {
108 }
109
110 Display display = null;
111 Shell shell = null;
112 Composite composite = null;
113 com.jogamp.newt.Display swtNewtDisplay = null;
114
115 @Before
116 public void init() {
117 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
118 @Override
119 public void run() {
120 display = new Display();
121 Assert.assertNotNull( display );
122 }});
123 display.syncExec(new Runnable() {
124 @Override
125 public void run() {
126 shell = new Shell( display );
127 Assert.assertNotNull( shell );
128 shell.setLayout( new FillLayout() );
129 composite = new Composite( shell, SWT.NONE );
130 composite.setLayout( new FillLayout() );
131 Assert.assertNotNull( composite );
132 }});
133 swtNewtDisplay = NewtFactory.createDisplay(null, false); // no-reuse
134 }
135
136 @After
137 public void release() {
138 Assert.assertNotNull( display );
139 Assert.assertNotNull( shell );
140 Assert.assertNotNull( composite );
141 try {
142 display.syncExec(new Runnable() {
143 @Override
144 public void run() {
145 composite.dispose();
146 shell.dispose();
147 }});
148 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
149 @Override
150 public void run() {
151 display.dispose();
152 }});
153 }
154 catch( final Throwable throwable ) {
155 throwable.printStackTrace();
156 Assume.assumeNoException( throwable );
157 }
158 swtNewtDisplay = null;
159 display = null;
160 shell = null;
161 composite = null;
162 }
163
164 private String isOSXMainThread() {
166 return ", isOSX-Main-Thread: " + jogamp.nativewindow.macosx.OSXUtil.IsMainThread();
167 } else {
168 return "";
169 }
170 }
171 protected void runTestGL(final GLCapabilitiesImmutable caps) throws InterruptedException, InvocationTargetException {
172 System.err.println("CCC00: Run Thread: "+Thread.currentThread()+isOSXMainThread());
173 display.syncExec( new Runnable() {
174 @Override
175 public void run() {
176 System.err.println("CCC01: SWT Thread: "+Thread.currentThread()+isOSXMainThread());
177 } } );
178 {
179 final DisplayImpl d = (DisplayImpl)NewtFactory.createDisplay(null);
180 d.runOnEDTIfAvail(true, new Runnable() {
181 @Override
182 public void run() {
183 System.err.println("CCC02: NEWT EDT Thread: "+Thread.currentThread()+isOSXMainThread());
184 }
185 });
186 }
187
188 System.err.println("requested: vsync "+swapInterval+", "+caps);
189 final com.jogamp.newt.Screen screen = NewtFactory.createScreen(swtNewtDisplay, screenIdx);
190 final GLWindow glWindow = GLWindow.create(screen, caps);
191 Assert.assertNotNull(glWindow);
192
193 final GearsES2 demo = new GearsES2(swapInterval);
194 glWindow.addGLEventListener(demo);
195
196 final Animator animator = new Animator(0 /* w/o AWT */);
197 animator.setExclusiveContext(exclusiveContext);
198
199 final QuitAdapter quitAdapter = new QuitAdapter();
200 //glWindow.addKeyListener(new TraceKeyAdapter(quitAdapter));
201 //glWindow.addWindowListener(new TraceWindowAdapter(quitAdapter));
202 glWindow.addKeyListener(quitAdapter);
203 glWindow.addWindowListener(quitAdapter);
204
205 glWindow.addWindowListener(new WindowAdapter() {
206 @Override
207 public void windowResized(final WindowEvent e) {
208 System.err.println("window resized: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight());
209 }
210 @Override
211 public void windowMoved(final WindowEvent e) {
212 System.err.println("window moved: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight());
213 }
214 });
215
216 glWindow.addKeyListener(new KeyAdapter() {
217 @Override
218 public void keyReleased(final KeyEvent e) {
219 if( !e.isPrintableKey() || e.isAutoRepeat() ) {
220 return;
221 }
222 if(e.getKeyChar()=='f') {
223 glWindow.invokeOnNewThread(null, false, new Runnable() {
224 @Override
225 public void run() {
226 System.err.println("[set fullscreen pre]: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", f "+glWindow.isFullscreen()+", a "+glWindow.isAlwaysOnTop()+", "+glWindow.getInsets());
227 glWindow.setFullscreen(!glWindow.isFullscreen());
228 System.err.println("[set fullscreen post]: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", f "+glWindow.isFullscreen()+", a "+glWindow.isAlwaysOnTop()+", "+glWindow.getInsets());
229 } } );
230 }
231 }
232 });
233 final NEWTDemoListener newtDemoListener = new NEWTDemoListener(glWindow);
234 newtDemoListener.quitAdapterEnable(false);
235 glWindow.addKeyListener(newtDemoListener);
236 glWindow.addMouseListener(newtDemoListener);
237 glWindow.addWindowListener(newtDemoListener);
238
239 animator.add(glWindow);
240 animator.start();
241 Assert.assertTrue(animator.isStarted());
242 Assert.assertTrue(animator.isAnimating());
243 Assert.assertEquals(exclusiveContext ? animator.getThread() : null, glWindow.getExclusiveContextThread());
244
245 final NewtCanvasSWT canvas1 = NewtCanvasSWT.create( composite, 0, glWindow );
246 Assert.assertNotNull( canvas1 );
247
248 display.syncExec( new Runnable() {
249 @Override
250 public void run() {
251 shell.setText( getSimpleTestName(".") );
252 shell.setSize( wsize.getWidth(), wsize.getHeight() );
253 if( null != wpos ) {
254 shell.setLocation( wpos.getX(), wpos.getY() );
255 }
256 shell.open();
257 } } );
258
259 animator.setUpdateFPSFrames(60, showFPS ? System.err : null);
260
261 final SWTTestUtil.WaitAction waitAction = new SWTTestUtil.WaitAction(display, true, 10);
262 Assert.assertEquals(true, NewtTestUtil.waitForVisible(glWindow, true, waitAction));
263 Assert.assertEquals(true, GLTestUtil.waitForRealized(glWindow, true, waitAction));
264
265 System.err.println("NW chosen: "+glWindow.getDelegatedWindow().getChosenCapabilities());
266 System.err.println("GL chosen: "+glWindow.getChosenCapabilities());
267 System.err.println("window pos/siz: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", "+glWindow.getInsets());
268
269 if( null != rwsize ) {
270 for(int i=0; i<50; i++) { // 500 ms dispatched delay
271 waitAction.run();
272 }
273 display.syncExec( new Runnable() {
274 @Override
275 public void run() {
276 shell.setSize( rwsize.getWidth(), rwsize.getHeight() );
277 } } );
278 System.err.println("window resize pos/siz: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", "+glWindow.getInsets());
279 }
280
281 while(!quitAdapter.shouldQuit() && animator.isAnimating() && animator.getTotalFPSDuration()<duration) {
282 waitAction.run();
283 }
284
285 Assert.assertEquals(exclusiveContext ? animator.getThread() : null, glWindow.getExclusiveContextThread());
286 animator.stop();
287 Assert.assertFalse(animator.isAnimating());
288 Assert.assertFalse(animator.isStarted());
289 Assert.assertEquals(null, glWindow.getExclusiveContextThread());
290
291 display.syncExec( new Runnable() {
292 @Override
293 public void run() {
294 canvas1.dispose();
295 }
296 });
297 glWindow.destroy();
298 Assert.assertEquals(true, NewtTestUtil.waitForRealized(glWindow, false, null));
299 }
300
301 @Test
302 public void test01GL2ES2() throws InterruptedException, InvocationTargetException {
303 for(int i=1; i<=loops; i++) {
304 System.err.println("Loop "+i+"/"+loops);
305 final GLProfile glp;
306 if(forceGL3) {
307 glp = GLProfile.get(GLProfile.GL3);
308 } else if(forceES2) {
310 } else {
311 glp = GLProfile.getGL2ES2();
312 }
313 final GLCapabilities caps = new GLCapabilities( glp );
314 caps.setBackgroundOpaque(opaque);
315 if(-1 < forceAlpha) {
316 caps.setAlphaBits(forceAlpha);
317 }
318 runTestGL(caps);
319 if(loop_shutdown) {
321 }
322 }
323 }
324
325 @Test
326 public void test02GL3() throws InterruptedException, InvocationTargetException {
327 if(mainRun) return;
328
330 System.err.println("GL3 n/a");
331 return;
332 }
333 final GLProfile glp = GLProfile.get(GLProfile.GL3);
334 final GLCapabilities caps = new GLCapabilities( glp );
335 runTestGL(caps);
336 }
337
338 public static void main(final String args[]) throws IOException {
339 mainRun = true;
340
341 int x=0, y=0, w=640, h=480, rw=-1, rh=-1;
342 boolean usePos = false;
343
344 for(int i=0; i<args.length; i++) {
345 if(args[i].equals("-time")) {
346 i++;
347 duration = MiscUtils.atol(args[i], duration);
348 } else if(args[i].equals("-translucent")) {
349 opaque = false;
350 } else if(args[i].equals("-forceAlpha")) {
351 i++;
352 forceAlpha = MiscUtils.atoi(args[i], 0);
353 } else if(args[i].equals("-fullscreen")) {
354 fullscreen = true;
355 } else if(args[i].equals("-vsync")) {
356 i++;
357 swapInterval = MiscUtils.atoi(args[i], swapInterval);
358 } else if(args[i].equals("-exclctx")) {
359 exclusiveContext = true;
360 } else if(args[i].equals("-es2")) {
361 forceES2 = true;
362 } else if(args[i].equals("-gl3")) {
363 forceGL3 = true;
364 } else if(args[i].equals("-showFPS")) {
365 showFPS = true;
366 } else if(args[i].equals("-width")) {
367 i++;
368 w = MiscUtils.atoi(args[i], w);
369 } else if(args[i].equals("-height")) {
370 i++;
371 h = MiscUtils.atoi(args[i], h);
372 } else if(args[i].equals("-x")) {
373 i++;
374 x = MiscUtils.atoi(args[i], x);
375 usePos = true;
376 } else if(args[i].equals("-y")) {
377 i++;
378 y = MiscUtils.atoi(args[i], y);
379 usePos = true;
380 } else if(args[i].equals("-rwidth")) {
381 i++;
382 rw = MiscUtils.atoi(args[i], rw);
383 } else if(args[i].equals("-rheight")) {
384 i++;
385 rh = MiscUtils.atoi(args[i], rh);
386 } else if(args[i].equals("-screen")) {
387 i++;
388 screenIdx = MiscUtils.atoi(args[i], 0);
389 } else if(args[i].equals("-loops")) {
390 i++;
391 loops = MiscUtils.atoi(args[i], 1);
392 } else if(args[i].equals("-loop-shutdown")) {
393 loop_shutdown = true;
394 }
395 }
396 wsize = new Dimension(w, h);
397 if( 0 < rw && 0 < rh ) {
398 rwsize = new Dimension(rw, rh);
399 }
400
401 if(usePos) {
402 wpos = new Point(x, y);
403 }
404 System.err.println("position "+wpos);
405 System.err.println("size "+wsize);
406 System.err.println("resize "+rwsize);
407 System.err.println("screen "+screenIdx);
408 System.err.println("translucent "+(!opaque));
409 System.err.println("forceAlpha "+forceAlpha);
410 System.err.println("fullscreen "+fullscreen);
411 System.err.println("loops "+loops);
412 System.err.println("loop shutdown "+loop_shutdown);
413 System.err.println("forceES2 "+forceES2);
414 System.err.println("forceGL3 "+forceGL3);
415 System.err.println("swapInterval "+swapInterval);
416 System.err.println("exclusiveContext "+exclusiveContext);
417
418 org.junit.runner.JUnitCore.main(TestGearsES2NewtCanvasSWT.class.getName());
419 }
420}
void setBackgroundOpaque(final boolean opaque)
Sets whether the surface shall be opaque or translucent.
void setAlphaBits(final int alphaBits)
Sets the number of bits requested for the color buffer's alpha component.
Provides a pluggable mechanism for arbitrary window toolkits to adapt their components to the NativeW...
static final String TYPE_MACOSX
Mac OS X type, as retrieved with getNativeWindowType(boolean).
static String getNativeWindowType(final boolean useCustom)
static void invokeOnOSTKThread(final boolean blocking, final Runnable runnable)
Runs the specified action in an SWT compatible OS toolkit thread, which is:
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.
Definition: Screen.java:58
final boolean isAutoRepeat()
getModifiers() contains AUTOREPEAT_MASK.
final char getKeyChar()
Returns the UTF-16 character reflecting the key symbol incl.
Definition: KeyEvent.java:161
static boolean isPrintableKey(final short uniChar, final boolean isKeyChar)
Returns true if given uniChar represents a printable character, i.e.
Definition: KeyEvent.java:316
NEWT Window events are provided for notification purposes ONLY.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:466
final void addMouseListener(final MouseListener l)
Appends the given MouseListener to the end of the list.
Definition: GLWindow.java:927
final int getX()
Returns the current x position of this window, relative to it's parent.
Definition: GLWindow.java:436
final void addKeyListener(final KeyListener l)
Appends the given com.jogamp.newt.event.KeyListener to the end of the list.
Definition: GLWindow.java:902
final int getY()
Returns the current y position of the top-left corner of the client area relative to it's parent in w...
Definition: GLWindow.java:441
final int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:461
final boolean setFullscreen(final boolean fullscreen)
Enable or disable fullscreen mode for this window.
Definition: GLWindow.java:534
final void addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
Definition: GLWindow.java:882
final CapabilitiesImmutable getChosenCapabilities()
Gets an immutable set of chosen capabilities.
Definition: GLWindow.java:266
final InsetsImmutable getInsets()
Returns the insets defined as the width and height of the window decoration on the left,...
Definition: GLWindow.java:431
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
final Window getDelegatedWindow()
If the implementation uses delegation, return the delegated Window instance, otherwise return this in...
Definition: GLWindow.java:277
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Definition: GLWindow.java:169
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.
void dispose()
Destroys this resource:
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static boolean isAvailable(final AbstractGraphicsDevice device, final String profile)
Returns the availability of a profile on a device.
Definition: GLProfile.java:305
static final String GL3
The desktop OpenGL core profile 3.x, with x >= 1.
Definition: GLProfile.java:576
static void shutdown()
Manual shutdown method, may be called after your last JOGL use within the running JVM.
Definition: GLProfile.java:277
static final String GLES2
The embedded OpenGL profile ES 2.x, with x >= 0.
Definition: GLProfile.java:585
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
static GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
static boolean waitForRealized(final GLAutoDrawable glad, final boolean realized, final Runnable waitAction)
Definition: GLTestUtil.java:91
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
static boolean waitForVisible(final Window win, final boolean visible, final Runnable waitAction)
final synchronized Thread getThread()
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
final synchronized Thread setExclusiveContext(final Thread t)
Dedicate all GLAutoDrawable's context to the given exclusive context thread.
final void setUpdateFPSFrames(final int frames, final PrintStream out)
synchronized boolean isStarted()
Indicates whether this animator has been started.
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
Immutable Dimension Interface, consisting of it's read only components:
CapabilitiesImmutable getChosenCapabilities()
Gets an immutable set of chosen capabilities.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Specifies an immutable set of OpenGL capabilities.