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