JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGearsES2SWT.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.swt;
30
31import java.io.IOException;
32import java.lang.reflect.InvocationTargetException;
33
34import com.jogamp.nativewindow.swt.SWTAccessor;
35import com.jogamp.opengl.swt.GLCanvas;
36import com.jogamp.opengl.test.junit.util.GLTestUtil;
37import com.jogamp.opengl.test.junit.util.MiscUtils;
38import com.jogamp.opengl.test.junit.util.NewtTestUtil;
39import com.jogamp.opengl.test.junit.util.SWTTestUtil;
40import com.jogamp.opengl.test.junit.util.UITestCase;
41import com.jogamp.opengl.util.Animator;
42import com.jogamp.opengl.util.AnimatorBase;
43import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
44
45import com.jogamp.nativewindow.util.Dimension;
46import com.jogamp.nativewindow.util.Point;
47import com.jogamp.nativewindow.util.PointImmutable;
48import com.jogamp.nativewindow.util.DimensionImmutable;
49import com.jogamp.opengl.GLCapabilities;
50import com.jogamp.opengl.GLCapabilitiesImmutable;
51import com.jogamp.opengl.GLProfile;
52
53import org.eclipse.swt.SWT;
54import org.eclipse.swt.layout.FillLayout;
55import org.eclipse.swt.widgets.Composite;
56import org.eclipse.swt.widgets.Display;
57import org.eclipse.swt.widgets.Shell;
58import org.junit.After;
59import org.junit.Assert;
60import org.junit.Assume;
61import org.junit.Before;
62import org.junit.BeforeClass;
63import org.junit.AfterClass;
64import org.junit.Test;
65import org.junit.FixMethodOrder;
66import org.junit.runners.MethodSorters;
67
68@FixMethodOrder(MethodSorters.NAME_ASCENDING)
69public class TestGearsES2SWT extends UITestCase {
70 static int screenIdx = 0;
71 static PointImmutable wpos;
72 static DimensionImmutable wsize, rwsize=null;
73
74 static long duration = 500; // ms
75 static boolean opaque = true;
76 static int forceAlpha = -1;
77 static boolean fullscreen = false;
78 static int swapInterval = 1;
79 static boolean showFPS = false;
80 static int loops = 1;
81 static boolean loop_shutdown = false;
82 static boolean forceES2 = false;
83 static boolean forceGL3 = false;
84 static boolean mainRun = false;
85 static boolean exclusiveContext = false;
86
87 @BeforeClass
88 public static void initClass() {
89 if(null == wsize) {
90 wsize = new Dimension(640, 480);
91 }
92 }
93
94 @AfterClass
95 public static void releaseClass() {
96 }
97
98 Display display = null;
99 Shell shell = null;
100 Composite composite = null;
101
102 @Before
103 public void init() {
104 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
105 @Override
106 public void run() {
107 display = new Display();
108 Assert.assertNotNull( display );
109 }});
110 display.syncExec(new Runnable() {
111 @Override
112 public void run() {
113 shell = new Shell( display );
114 Assert.assertNotNull( shell );
115 shell.setLayout( new FillLayout() );
116 composite = new Composite( shell, SWT.NONE );
117 composite.setLayout( new FillLayout() );
118 Assert.assertNotNull( composite );
119 }});
120 }
121
122 @After
123 public void release() {
124 Assert.assertNotNull( display );
125 Assert.assertNotNull( shell );
126 Assert.assertNotNull( composite );
127 try {
128 display.syncExec(new Runnable() {
129 @Override
130 public void run() {
131 composite.dispose();
132 shell.dispose();
133 }});
134 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
135 @Override
136 public void run() {
137 display.dispose();
138 }});
139 }
140 catch( final Throwable throwable ) {
141 throwable.printStackTrace();
142 Assume.assumeNoException( throwable );
143 }
144 display = null;
145 shell = null;
146 composite = null;
147 }
148
149 protected void runTestGL(final GLCapabilitiesImmutable caps) throws InterruptedException, InvocationTargetException {
150 System.err.println("requested: vsync "+swapInterval+", "+caps);
151
152 final GLCanvas canvas = GLCanvas.create( composite, 0, caps, null);
153 Assert.assertNotNull( canvas );
154
155 final GearsES2 demo = new GearsES2(swapInterval);
156 canvas.addGLEventListener(demo);
157
158 final Animator animator = new Animator(0 /* w/o AWT */);
159 animator.setExclusiveContext(exclusiveContext);
160
161 animator.add(canvas);
162 animator.start();
163 Assert.assertTrue(animator.isStarted());
164 Assert.assertTrue(animator.isAnimating());
165 Assert.assertEquals(exclusiveContext ? animator.getThread() : null, canvas.getExclusiveContextThread());
166
167 display.syncExec( new Runnable() {
168 @Override
169 public void run() {
170 shell.setText( getSimpleTestName(".") );
171 shell.setSize( wsize.getWidth(), wsize.getHeight() );
172 if( null != wpos ) {
173 shell.setLocation( wpos.getX(), wpos.getY() );
174 }
175 shell.open();
176 }
177 });
178
179 animator.setUpdateFPSFrames(60, showFPS ? System.err : null);
180
181 final SWTTestUtil.WaitAction waitAction = new SWTTestUtil.WaitAction(display, true, 16);
182
183 System.err.println("TestGearsES2SWT.test: 2.0: Exception "+(null != waitAction.getException(true)));
184 Assert.assertEquals(true, GLTestUtil.waitForRealized(canvas, true, waitAction));
185 System.err.println("TestGearsES2SWT.test: 2.1: Exception "+(null != waitAction.getException(true)));
186
187 while(animator.isAnimating() && !canvas.isRealized() && animator.getTotalFPSDuration()<duration) {
188 waitAction.run();
189 }
190 System.err.println("TestGearsES2SWT.test: 3.0: Exception "+(null != waitAction.getException(true)));
191 System.err.println("NW chosen: "+canvas.getDelegatedDrawable().getChosenGLCapabilities());
192 System.err.println("GL chosen: "+canvas.getChosenGLCapabilities());
193 display.syncExec(new Runnable() {
194 @Override
195 public void run() {
196 System.err.println("window pos/siz: "+canvas.getLocation()+" "+canvas.getSurfaceWidth()+"x"+canvas.getSurfaceHeight());
197 } } );
198
199 if( null != rwsize ) {
200 for(int i=0; i<50; i++) { // 500 ms dispatched delay
201 waitAction.run();
202 }
203 System.err.println("TestGearsES2SWT.test: 4.0: Exception "+(null != waitAction.getException(true)));
204 display.syncExec( new Runnable() {
205 @Override
206 public void run() {
207 shell.setSize( rwsize.getWidth(), rwsize.getHeight() );
208 }
209 });
210 System.err.println("window resize pos/siz: "+canvas.getLocation()+" "+canvas.getSurfaceWidth()+"x"+canvas.getSurfaceHeight());
211 }
212
213 while(animator.isAnimating() && animator.getTotalFPSDuration()<duration) {
214 waitAction.run();
215 }
216 System.err.println("TestGearsES2SWT.test: 5.0: Exception "+(null != waitAction.getException(true)));
217
218 Assert.assertEquals(exclusiveContext ? animator.getThread() : null, canvas.getExclusiveContextThread());
219 animator.stop();
220 Assert.assertFalse(animator.isAnimating());
221 Assert.assertFalse(animator.isStarted());
222 Assert.assertEquals(null, canvas.getExclusiveContextThread());
223
224 display.syncExec(new Runnable() {
225 @Override
226 public void run() {
227 canvas.dispose();
228 } } );
229 }
230
231 @Test
232 public void test01GL2ES2() throws InterruptedException, InvocationTargetException {
233 for(int i=1; i<=loops; i++) {
234 System.err.println("Loop "+i+"/"+loops);
235 final GLProfile glp;
236 if(forceGL3) {
237 glp = GLProfile.get(GLProfile.GL3);
238 } else if(forceES2) {
240 } else {
241 glp = GLProfile.getGL2ES2();
242 }
243 final GLCapabilities caps = new GLCapabilities( glp );
244 caps.setBackgroundOpaque(opaque);
245 if(-1 < forceAlpha) {
246 caps.setAlphaBits(forceAlpha);
247 }
248 runTestGL(caps);
249 if(loop_shutdown) {
251 }
252 }
253 }
254
255 @Test
256 public void test02GL3() throws InterruptedException, InvocationTargetException {
257 if(mainRun) return;
258
260 System.err.println("GL3 n/a");
261 return;
262 }
263 final GLProfile glp = GLProfile.get(GLProfile.GL3);
264 final GLCapabilities caps = new GLCapabilities( glp );
265 runTestGL(caps);
266 }
267
268 public static void main(final String args[]) throws IOException {
269 mainRun = true;
270
271 int x=0, y=0, w=640, h=480, rw=-1, rh=-1;
272 boolean usePos = false;
273
274 for(int i=0; i<args.length; i++) {
275 if(args[i].equals("-time")) {
276 i++;
277 duration = MiscUtils.atol(args[i], duration);
278 } else if(args[i].equals("-translucent")) {
279 opaque = false;
280 } else if(args[i].equals("-forceAlpha")) {
281 i++;
282 forceAlpha = MiscUtils.atoi(args[i], 0);
283 } else if(args[i].equals("-fullscreen")) {
284 fullscreen = true;
285 } else if(args[i].equals("-vsync")) {
286 i++;
287 swapInterval = MiscUtils.atoi(args[i], swapInterval);
288 } else if(args[i].equals("-exclctx")) {
289 exclusiveContext = true;
290 } else if(args[i].equals("-es2")) {
291 forceES2 = true;
292 } else if(args[i].equals("-gl3")) {
293 forceGL3 = true;
294 } else if(args[i].equals("-showFPS")) {
295 showFPS = true;
296 } else if(args[i].equals("-width")) {
297 i++;
298 w = MiscUtils.atoi(args[i], w);
299 } else if(args[i].equals("-height")) {
300 i++;
301 h = MiscUtils.atoi(args[i], h);
302 } else if(args[i].equals("-x")) {
303 i++;
304 x = MiscUtils.atoi(args[i], x);
305 usePos = true;
306 } else if(args[i].equals("-y")) {
307 i++;
308 y = MiscUtils.atoi(args[i], y);
309 usePos = true;
310 } else if(args[i].equals("-rwidth")) {
311 i++;
312 rw = MiscUtils.atoi(args[i], rw);
313 } else if(args[i].equals("-rheight")) {
314 i++;
315 rh = MiscUtils.atoi(args[i], rh);
316 } else if(args[i].equals("-screen")) {
317 i++;
318 screenIdx = MiscUtils.atoi(args[i], 0);
319 } else if(args[i].equals("-loops")) {
320 i++;
321 loops = MiscUtils.atoi(args[i], 1);
322 } else if(args[i].equals("-loop-shutdown")) {
323 loop_shutdown = true;
324 }
325 }
326 wsize = new Dimension(w, h);
327 if( 0 < rw && 0 < rh ) {
328 rwsize = new Dimension(rw, rh);
329 }
330
331 if(usePos) {
332 wpos = new Point(x, y);
333 }
334 System.err.println("position "+wpos);
335 System.err.println("size "+wsize);
336 System.err.println("resize "+rwsize);
337 System.err.println("screen "+screenIdx);
338 System.err.println("translucent "+(!opaque));
339 System.err.println("forceAlpha "+forceAlpha);
340 System.err.println("fullscreen "+fullscreen);
341 System.err.println("loops "+loops);
342 System.err.println("loop shutdown "+loop_shutdown);
343 System.err.println("forceES2 "+forceES2);
344 System.err.println("forceGL3 "+forceGL3);
345 System.err.println("swapInterval "+swapInterval);
346 System.err.println("exclusiveContext "+exclusiveContext);
347
348 org.junit.runner.JUnitCore.main(TestGearsES2SWT.class.getName());
349 }
350}
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.
static void invokeOnOSTKThread(final boolean blocking, final Runnable runnable)
Runs the specified action in an SWT compatible OS toolkit thread, which is:
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
Native SWT Canvas implementing GLAutoDrawable.
Definition: GLCanvas.java:98
final GLDrawable getDelegatedDrawable()
If the implementation uses delegation, return the delegated GLDrawable instance, otherwise return thi...
Definition: GLCanvas.java:786
boolean isRealized()
Returns true if this drawable is realized, otherwise false.
Definition: GLCanvas.java:916
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLCanvas.java:690
static GLCanvas create(final Composite parent, final int style, final GLCapabilitiesImmutable caps, final GLCapabilitiesChooser chooser)
Creates an instance using GLCanvas(Composite, int, GLCapabilitiesImmutable, GLCapabilitiesChooser) on...
Definition: GLCanvas.java:313
final Thread getExclusiveContextThread()
Definition: GLCanvas.java:776
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
Definition: GLCanvas.java:881
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Definition: GLCanvas.java:695
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:706
void dispose()
@Override public boolean forceFocus() { final boolean r = super.forceFocus(); if(r && 0 !...
Definition: GLCanvas.java:657
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
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:
Specifies an immutable set of OpenGL capabilities.
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...