JOGL v2.6.0-rc-20250822
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 SWTAccessor.printInfo(System.err, display);
110 }});
111 display.syncExec(new Runnable() {
112 @Override
113 public void run() {
114 shell = new Shell( display );
115 Assert.assertNotNull( shell );
116 shell.setLayout( new FillLayout() );
117 composite = new Composite( shell, SWT.NONE );
118 composite.setLayout( new FillLayout() );
119 Assert.assertNotNull( composite );
120 }});
121 }
122
123 @After
124 public void release() {
125 Assert.assertNotNull( display );
126 Assert.assertNotNull( shell );
127 Assert.assertNotNull( composite );
128 try {
129 display.syncExec(new Runnable() {
130 @Override
131 public void run() {
132 composite.dispose();
133 shell.dispose();
134 }});
135 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
136 @Override
137 public void run() {
138 display.dispose();
139 }});
140 }
141 catch( final Throwable throwable ) {
142 throwable.printStackTrace();
143 Assume.assumeNoException( throwable );
144 }
145 display = null;
146 shell = null;
147 composite = null;
148 }
149
150 protected void runTestGL(final GLCapabilitiesImmutable caps) throws InterruptedException, InvocationTargetException {
151 System.err.println("requested: vsync "+swapInterval+", "+caps);
152
153 final GLCanvas canvas = GLCanvas.create( composite, 0, caps, null);
154 Assert.assertNotNull( canvas );
155
156 final GearsES2 demo = new GearsES2(swapInterval);
157 canvas.addGLEventListener(demo);
158
159 final Animator animator = new Animator(0 /* w/o AWT */);
160 animator.setExclusiveContext(exclusiveContext);
161
162 animator.add(canvas);
163 animator.start();
164 Assert.assertTrue(animator.isStarted());
165 Assert.assertTrue(animator.isAnimating());
166 Assert.assertEquals(exclusiveContext ? animator.getThread() : null, canvas.getExclusiveContextThread());
167
168 display.syncExec( new Runnable() {
169 @Override
170 public void run() {
171 shell.setText( getSimpleTestName(".") );
172 shell.setSize( wsize.getWidth(), wsize.getHeight() );
173 if( null != wpos ) {
174 shell.setLocation( wpos.getX(), wpos.getY() );
175 }
176 shell.open();
177 }
178 });
179
180 animator.setUpdateFPSFrames(60, showFPS ? System.err : null);
181
182 final SWTTestUtil.WaitAction waitAction = new SWTTestUtil.WaitAction(display, true, 16);
183
184 System.err.println("TestGearsES2SWT.test: 2.0: Exception "+(null != waitAction.getException(true)));
185 Assert.assertEquals(true, GLTestUtil.waitForRealized(canvas, true, waitAction));
186 System.err.println("TestGearsES2SWT.test: 2.1: Exception "+(null != waitAction.getException(true)));
187
188 while(animator.isAnimating() && !canvas.isRealized() && animator.getTotalFPSDuration()<duration) {
189 waitAction.run();
190 }
191 System.err.println("TestGearsES2SWT.test: 3.0: Exception "+(null != waitAction.getException(true)));
192 System.err.println("NW chosen: "+canvas.getDelegatedDrawable().getChosenGLCapabilities());
193 System.err.println("GL chosen: "+canvas.getChosenGLCapabilities());
194 display.syncExec(new Runnable() {
195 @Override
196 public void run() {
197 System.err.println("window pos/siz: "+canvas.getLocation()+" "+canvas.getSurfaceWidth()+"x"+canvas.getSurfaceHeight());
198 } } );
199
200 if( null != rwsize ) {
201 for(int i=0; i<50; i++) { // 500 ms dispatched delay
202 waitAction.run();
203 }
204 System.err.println("TestGearsES2SWT.test: 4.0: Exception "+(null != waitAction.getException(true)));
205 display.syncExec( new Runnable() {
206 @Override
207 public void run() {
208 shell.setSize( rwsize.getWidth(), rwsize.getHeight() );
209 }
210 });
211 System.err.println("window resize pos/siz: "+canvas.getLocation()+" "+canvas.getSurfaceWidth()+"x"+canvas.getSurfaceHeight());
212 }
213
214 while(animator.isAnimating() && animator.getTotalFPSDuration()<duration) {
215 waitAction.run();
216 }
217 System.err.println("TestGearsES2SWT.test: 5.0: Exception "+(null != waitAction.getException(true)));
218
219 Assert.assertEquals(exclusiveContext ? animator.getThread() : null, canvas.getExclusiveContextThread());
220 animator.stop();
221 Assert.assertFalse(animator.isAnimating());
222 Assert.assertFalse(animator.isStarted());
223 Assert.assertEquals(null, canvas.getExclusiveContextThread());
224
225 display.syncExec(new Runnable() {
226 @Override
227 public void run() {
228 canvas.dispose();
229 } } );
230 }
231
232 @Test
233 public void test01GL2ES2() throws InterruptedException, InvocationTargetException {
234 for(int i=1; i<=loops; i++) {
235 System.err.println("Loop "+i+"/"+loops);
236 final GLProfile glp;
237 if(forceGL3) {
238 glp = GLProfile.get(GLProfile.GL3);
239 } else if(forceES2) {
241 } else {
242 glp = GLProfile.getGL2ES2();
243 }
244 final GLCapabilities caps = new GLCapabilities( glp );
245 caps.setBackgroundOpaque(opaque);
246 if(-1 < forceAlpha) {
247 caps.setAlphaBits(forceAlpha);
248 }
249 runTestGL(caps);
250 if(loop_shutdown) {
252 }
253 }
254 }
255
256 @Test
257 public void test02GL3() throws InterruptedException, InvocationTargetException {
258 if(mainRun) return;
259
261 System.err.println("GL3 n/a");
262 return;
263 }
264 final GLProfile glp = GLProfile.get(GLProfile.GL3);
265 final GLCapabilities caps = new GLCapabilities( glp );
266 runTestGL(caps);
267 }
268
269 public static void main(final String args[]) throws IOException {
270 mainRun = true;
271
272 int x=0, y=0, w=640, h=480, rw=-1, rh=-1;
273 boolean usePos = false;
274
275 for(int i=0; i<args.length; i++) {
276 if(args[i].equals("-time")) {
277 i++;
278 duration = MiscUtils.atol(args[i], duration);
279 } else if(args[i].equals("-translucent")) {
280 opaque = false;
281 } else if(args[i].equals("-forceAlpha")) {
282 i++;
283 forceAlpha = MiscUtils.atoi(args[i], 0);
284 } else if(args[i].equals("-fullscreen")) {
285 fullscreen = true;
286 } else if(args[i].equals("-vsync")) {
287 i++;
288 swapInterval = MiscUtils.atoi(args[i], swapInterval);
289 } else if(args[i].equals("-exclctx")) {
290 exclusiveContext = true;
291 } else if(args[i].equals("-es2")) {
292 forceES2 = true;
293 } else if(args[i].equals("-gl3")) {
294 forceGL3 = true;
295 } else if(args[i].equals("-showFPS")) {
296 showFPS = true;
297 } else if(args[i].equals("-width")) {
298 i++;
299 w = MiscUtils.atoi(args[i], w);
300 } else if(args[i].equals("-height")) {
301 i++;
302 h = MiscUtils.atoi(args[i], h);
303 } else if(args[i].equals("-x")) {
304 i++;
305 x = MiscUtils.atoi(args[i], x);
306 usePos = true;
307 } else if(args[i].equals("-y")) {
308 i++;
309 y = MiscUtils.atoi(args[i], y);
310 usePos = true;
311 } else if(args[i].equals("-rwidth")) {
312 i++;
313 rw = MiscUtils.atoi(args[i], rw);
314 } else if(args[i].equals("-rheight")) {
315 i++;
316 rh = MiscUtils.atoi(args[i], rh);
317 } else if(args[i].equals("-screen")) {
318 i++;
319 screenIdx = MiscUtils.atoi(args[i], 0);
320 } else if(args[i].equals("-loops")) {
321 i++;
322 loops = MiscUtils.atoi(args[i], 1);
323 } else if(args[i].equals("-loop-shutdown")) {
324 loop_shutdown = true;
325 }
326 }
327 wsize = new Dimension(w, h);
328 if( 0 < rw && 0 < rh ) {
329 rwsize = new Dimension(rw, rh);
330 }
331
332 if(usePos) {
333 wpos = new Point(x, y);
334 }
335 System.err.println("position "+wpos);
336 System.err.println("size "+wsize);
337 System.err.println("resize "+rwsize);
338 System.err.println("screen "+screenIdx);
339 System.err.println("translucent "+(!opaque));
340 System.err.println("forceAlpha "+forceAlpha);
341 System.err.println("fullscreen "+fullscreen);
342 System.err.println("loops "+loops);
343 System.err.println("loop shutdown "+loop_shutdown);
344 System.err.println("forceES2 "+forceES2);
345 System.err.println("forceGL3 "+forceGL3);
346 System.err.println("swapInterval "+swapInterval);
347 System.err.println("exclusiveContext "+exclusiveContext);
348
349 org.junit.runner.JUnitCore.main(TestGearsES2SWT.class.getName());
350 }
351}
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:
static void printInfo(final PrintStream out, final Display d)
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:797
boolean isRealized()
Returns true if this drawable is realized, otherwise false.
Definition: GLCanvas.java:927
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLCanvas.java:701
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:787
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
Definition: GLCanvas.java:892
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Definition: GLCanvas.java:706
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:717
void dispose()
@Override public boolean forceFocus() { final boolean r = super.forceFocus(); if(r && 0 !...
Definition: GLCanvas.java:668
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...