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