Jogamp
8172eb3355e71dc42979928544d40681991b8468
[jogl.git] / src / test / com / jogamp / opengl / test / junit / jogl / demos / es2 / awt / TestGearsES2GLJPanelAWT.java
1 /**
2  * Copyright 2010 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
29 package com.jogamp.opengl.test.junit.jogl.demos.es2.awt;
30
31 import java.awt.AWTException;
32 import java.awt.BorderLayout;
33 import java.awt.Dimension;
34 import java.awt.event.ComponentEvent;
35 import java.awt.event.ComponentListener;
36 import java.lang.reflect.InvocationTargetException;
37
38 import javax.media.opengl.GLCapabilities;
39 import javax.media.opengl.GLCapabilitiesImmutable;
40 import javax.media.opengl.GLProfile;
41 import javax.media.opengl.awt.GLJPanel;
42 import javax.swing.JFrame;
43 import javax.swing.SwingUtilities;
44
45 import org.junit.AfterClass;
46 import org.junit.Assert;
47 import org.junit.Assume;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 import org.junit.FixMethodOrder;
51 import org.junit.runners.MethodSorters;
52
53 import com.jogamp.newt.event.TraceKeyAdapter;
54 import com.jogamp.newt.event.TraceWindowAdapter;
55 import com.jogamp.newt.event.awt.AWTKeyAdapter;
56 import com.jogamp.newt.event.awt.AWTWindowAdapter;
57 import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
58 import com.jogamp.opengl.test.junit.jogl.demos.gl2.Gears;
59 import com.jogamp.opengl.test.junit.util.MiscUtils;
60 import com.jogamp.opengl.test.junit.util.QuitAdapter;
61 import com.jogamp.opengl.test.junit.util.UITestCase;
62 import com.jogamp.opengl.util.FPSAnimator;
63
64 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
65 public class TestGearsES2GLJPanelAWT extends UITestCase {
66     static Dimension wsize, rwsize=null;
67     static boolean forceES2 = false;
68     static boolean forceGL3 = false;
69     static boolean forceGLFFP = false;
70     static boolean shallUsePBuffer = false;
71     static boolean shallUseBitmap = false;
72     static boolean useMSAA = false;
73     static int swapInterval = 0;
74     static boolean useAnimator = true;
75     static boolean manualTest = false;
76     static boolean skipGLOrientationVerticalFlip = false;
77     static int xpos = 10, ypos = 10;
78
79     @BeforeClass
80     public static void initClass() {
81         if(null == wsize) {
82             wsize = new Dimension(640, 480);
83         }
84     }
85
86     @AfterClass
87     public static void releaseClass() {
88     }
89
90     static void setFrameSize(final JFrame frame, final boolean frameLayout, final java.awt.Dimension new_sz) {
91         try {
92             javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
93                 public void run() {
94                     frame.setSize(new_sz);
95                     if( frameLayout ) {
96                         frame.validate();
97                     }
98                 } } );
99         } catch( Throwable throwable ) {
100             throwable.printStackTrace();
101             Assume.assumeNoException( throwable );
102         }
103     }
104
105     private void setTitle(final JFrame frame, final GLJPanel glc, final GLCapabilitiesImmutable caps) {
106         final String capsA = caps.isBackgroundOpaque() ? "opaque" : "transl";
107         final java.awt.Rectangle b = glc.getBounds();
108         frame.setTitle("GLJPanel["+capsA+"], swapI "+swapInterval+", win: ["+b.x+"/"+b.y+" "+b.width+"x"+b.height+"], pix: "+glc.getSurfaceWidth()+"x"+glc.getSurfaceHeight());
109     }
110
111     protected void runTestGL(final GLCapabilities caps)
112             throws AWTException, InterruptedException, InvocationTargetException
113     {
114         final JFrame frame = new JFrame("Swing GLJPanel");
115         Assert.assertNotNull(frame);
116
117         final GLJPanel glJPanel = new GLJPanel(caps);
118         Assert.assertNotNull(glJPanel);
119         glJPanel.setSkipGLOrientationVerticalFlip(skipGLOrientationVerticalFlip);
120         glJPanel.setMinimumSize(wsize);
121         glJPanel.setPreferredSize(wsize);
122         glJPanel.setSize(wsize);
123         if( caps.isBitmap() || caps.getGLProfile().isGL2() ) {
124             final Gears gears = new Gears(swapInterval);
125             gears.setFlipVerticalInGLOrientation(skipGLOrientationVerticalFlip);
126             glJPanel.addGLEventListener(gears);
127         } else {
128             final GearsES2 gears = new GearsES2(swapInterval);
129             gears.setFlipVerticalInGLOrientation(skipGLOrientationVerticalFlip);
130             glJPanel.addGLEventListener(gears);
131         }
132         final SnapshotGLEventListener snap = new SnapshotGLEventListener();
133         glJPanel.addGLEventListener(snap);
134         setTitle(frame, glJPanel, caps);
135         frame.setLocation(xpos, ypos);
136
137         frame.addComponentListener(new ComponentListener() {
138             @Override
139             public void componentResized(ComponentEvent e) {
140                 setTitle(frame, glJPanel, caps);
141             }
142
143             @Override
144             public void componentMoved(ComponentEvent e) {
145                 setTitle(frame, glJPanel, caps);
146             }
147
148             @Override
149             public void componentShown(ComponentEvent e) { }
150
151             @Override
152             public void componentHidden(ComponentEvent e) { }
153         });
154
155         final FPSAnimator animator = useAnimator ? new FPSAnimator(glJPanel, 60) : null;
156
157         SwingUtilities.invokeAndWait(new Runnable() {
158                 public void run() {
159                     frame.getContentPane().add(glJPanel, BorderLayout.CENTER);
160                     frame.getContentPane().validate();
161                     frame.pack();
162                     frame.setVisible(true);
163                 } } ) ;
164
165         if( useAnimator ) {
166             animator.setUpdateFPSFrames(60, System.err);
167             animator.start();
168             Assert.assertEquals(true, animator.isAnimating());
169         }
170
171         final QuitAdapter quitAdapter = new QuitAdapter();
172         new AWTKeyAdapter(new TraceKeyAdapter(quitAdapter), glJPanel).addTo(glJPanel);
173         new AWTWindowAdapter(new TraceWindowAdapter(quitAdapter), glJPanel).addTo(frame);
174
175         snap.setMakeSnapshot();
176
177         if( null != rwsize ) {
178             Thread.sleep(500); // 500ms delay
179             setFrameSize(frame, true, rwsize);
180             System.err.println("window resize pos/siz: "+glJPanel.getX()+"/"+glJPanel.getY()+" "+glJPanel.getSurfaceWidth()+"x"+glJPanel.getSurfaceHeight());
181         }
182
183         snap.setMakeSnapshot();
184
185         final long t0 = System.currentTimeMillis();
186         long t1 = t0;
187         boolean triggerSnap = false;
188         while(!quitAdapter.shouldQuit() && t1 - t0 < duration) {
189             Thread.sleep(100);
190             t1 = System.currentTimeMillis();
191             snap.getDisplayCount();
192             if( !triggerSnap && snap.getDisplayCount() > 1 ) {
193                 // Snapshot only after one frame has been rendered to suite FBO MSAA!
194                 snap.setMakeSnapshot();
195                 triggerSnap = true;
196             }
197         }
198
199         Assert.assertNotNull(frame);
200         Assert.assertNotNull(glJPanel);
201         Assert.assertNotNull(animator);
202
203         if( useAnimator ) {
204             animator.stop();
205             Assert.assertEquals(false, animator.isAnimating());
206         }
207         SwingUtilities.invokeAndWait(new Runnable() {
208                 public void run() {
209                     frame.setVisible(false);
210                     frame.getContentPane().remove(glJPanel);
211                     frame.remove(glJPanel);
212                     glJPanel.destroy();
213                     frame.dispose();
214                 } } );
215     }
216
217     @Test
218     public void test01_DefaultNorm()
219             throws AWTException, InterruptedException, InvocationTargetException
220     {
221         final GLProfile glp;
222         if(forceGL3) {
223             glp = GLProfile.get(GLProfile.GL3);
224         } else if(forceES2) {
225             glp = GLProfile.get(GLProfile.GLES2);
226         } else if(forceGLFFP) {
227             glp = GLProfile.getMaxFixedFunc(true);
228         } else {
229             glp = GLProfile.getGL2ES2();
230         }
231         GLCapabilities caps = new GLCapabilities( glp );
232         if(useMSAA) {
233             caps.setNumSamples(4);
234             caps.setSampleBuffers(true);
235         }
236         if(shallUsePBuffer) {
237             caps.setPBuffer(true);
238         }
239         if(shallUseBitmap) {
240             caps.setBitmap(true);
241         }
242         runTestGL(caps);
243     }
244
245     @Test
246     public void test02_DefaultMsaa()
247             throws AWTException, InterruptedException, InvocationTargetException
248     {
249         if( manualTest ) {
250             return;
251         }
252         GLCapabilities caps = new GLCapabilities(GLProfile.getDefault());
253         caps.setNumSamples(4);
254         caps.setSampleBuffers(true);
255         runTestGL(caps);
256     }
257
258     @Test
259     public void test03_PbufferNorm()
260             throws AWTException, InterruptedException, InvocationTargetException
261     {
262         if( manualTest ) {
263             return;
264         }
265         GLCapabilities caps = new GLCapabilities(GLProfile.getDefault());
266         caps.setPBuffer(true);
267         runTestGL(caps);
268     }
269
270     @Test
271     public void test04_PbufferMsaa()
272             throws AWTException, InterruptedException, InvocationTargetException
273     {
274         if( manualTest ) {
275             return;
276         }
277         GLCapabilities caps = new GLCapabilities(GLProfile.getDefault());
278         caps.setNumSamples(4);
279         caps.setSampleBuffers(true);
280         caps.setPBuffer(true);
281         runTestGL(caps);
282     }
283
284     @Test
285     public void test05_BitmapNorm()
286             throws AWTException, InterruptedException, InvocationTargetException
287     {
288         if( manualTest ) {
289             return;
290         }
291         GLCapabilities caps = new GLCapabilities(GLProfile.getDefault());
292         caps.setBitmap(true);
293         runTestGL(caps);
294     }
295
296     @Test
297     public void test06_BitmapMsaa()
298             throws AWTException, InterruptedException, InvocationTargetException
299     {
300         if( manualTest ) {
301             return;
302         }
303         GLCapabilities caps = new GLCapabilities(GLProfile.getDefault());
304         caps.setNumSamples(4);
305         caps.setSampleBuffers(true);
306         caps.setBitmap(true);
307         runTestGL(caps);
308     }
309
310     @Test
311     public void test20_GLES2()
312             throws AWTException, InterruptedException, InvocationTargetException
313     {
314         if( manualTest ) {
315             return;
316         }
317
318         if( !GLProfile.isAvailable(GLProfile.GLES2) ) {
319             System.err.println("GLES2 n/a");
320             return;
321         }
322         final GLProfile glp = GLProfile.get(GLProfile.GLES2);
323         final GLCapabilities caps = new GLCapabilities( glp );
324         runTestGL(caps);
325     }
326
327     @Test
328     public void test30_GL3()
329             throws AWTException, InterruptedException, InvocationTargetException
330     {
331         if( manualTest ) {
332             return;
333         }
334
335         if( !GLProfile.isAvailable(GLProfile.GL3) ) {
336             System.err.println("GL3 n/a");
337             return;
338         }
339         final GLProfile glp = GLProfile.get(GLProfile.GL3);
340         final GLCapabilities caps = new GLCapabilities( glp );
341         runTestGL(caps);
342     }
343
344     static long duration = 500; // ms
345
346     public static void main(String args[]) {
347         int w=640, h=480, rw=-1, rh=-1;
348
349         for(int i=0; i<args.length; i++) {
350             if(args[i].equals("-time")) {
351                 i++;
352                 duration = MiscUtils.atol(args[i], duration);
353             } else if(args[i].equals("-es2")) {
354                 forceES2 = true;
355             } else if(args[i].equals("-gl3")) {
356                 forceGL3 = true;
357             } else if(args[i].equals("-glFFP")) {
358                 forceGLFFP = true;
359             } else if(args[i].equals("-width")) {
360                 i++;
361                 w = MiscUtils.atoi(args[i], w);
362             } else if(args[i].equals("-height")) {
363                 i++;
364                 h = MiscUtils.atoi(args[i], h);
365             } else if(args[i].equals("-x")) {
366                 i++;
367                 xpos = MiscUtils.atoi(args[i], xpos);
368             } else if(args[i].equals("-y")) {
369                 i++;
370                 ypos = MiscUtils.atoi(args[i], ypos);
371             } else if(args[i].equals("-rwidth")) {
372                 i++;
373                 rw = MiscUtils.atoi(args[i], rw);
374             } else if(args[i].equals("-rheight")) {
375                 i++;
376                 rh = MiscUtils.atoi(args[i], rh);
377             } else if(args[i].equals("-userVFlip")) {
378                 skipGLOrientationVerticalFlip = true;
379             } else if(args[i].equals("-vsync")) {
380                 i++;
381                 swapInterval = MiscUtils.atoi(args[i], swapInterval);
382             } else if(args[i].equals("-msaa")) {
383                 useMSAA = true;
384             } else if(args[i].equals("-noanim")) {
385                 useAnimator  = false;
386             } else if(args[i].equals("-pbuffer")) {
387                 shallUsePBuffer = true;
388             } else if(args[i].equals("-bitmap")) {
389                 shallUseBitmap = true;
390             } else if(args[i].equals("-manual")) {
391                 manualTest = true;
392             }
393         }
394         wsize = new Dimension(w, h);
395         if( 0 < rw && 0 < rh ) {
396             rwsize = new Dimension(rw, rh);
397         }
398
399         System.err.println("size "+wsize);
400         System.err.println("resize "+rwsize);
401         System.err.println("userVFlip "+skipGLOrientationVerticalFlip);
402         System.err.println("swapInterval "+swapInterval);
403         System.err.println("forceES2 "+forceES2);
404         System.err.println("forceGL3 "+forceGL3);
405         System.err.println("forceGLFFP "+forceGLFFP);
406         System.err.println("useMSAA "+useMSAA);
407         System.err.println("useAnimator "+useAnimator);
408         System.err.println("shallUsePBuffer "+shallUsePBuffer);
409         System.err.println("shallUseBitmap "+shallUseBitmap);
410         System.err.println("manualTest "+manualTest);
411
412         org.junit.runner.JUnitCore.main(TestGearsES2GLJPanelAWT.class.getName());
413     }
414 }
http://JogAmp.org git info: FAQ, tutorial and man pages.