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