Jogamp
c7a00350b2f4a4ea2baba606f13f604c6f8846d7
[jogl.git] / src / test / com / jogamp / opengl / test / junit / jogl / demos / es2 / newt / TestGearsES2NewtCanvasAWT.java
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  
29 package com.jogamp.opengl.test.junit.jogl.demos.es2.newt;
30
31 import java.awt.BorderLayout;
32 import java.awt.Button;
33 import java.awt.Component;
34 import java.awt.Container;
35 import java.awt.Frame;
36 import java.awt.TextArea;
37 import java.io.IOException;
38 import java.lang.reflect.InvocationTargetException;
39
40 import com.jogamp.common.os.Platform;
41 import com.jogamp.newt.Display;
42 import com.jogamp.newt.NewtFactory;
43 import com.jogamp.newt.Screen;
44 import com.jogamp.newt.awt.NewtCanvasAWT;
45 import com.jogamp.newt.event.WindowEvent;
46 import com.jogamp.newt.event.WindowAdapter;
47 import com.jogamp.newt.opengl.GLWindow;
48 import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
49 import com.jogamp.opengl.test.junit.util.MiscUtils;
50 import com.jogamp.opengl.test.junit.util.UITestCase;
51 import com.jogamp.opengl.test.junit.util.QuitAdapter;
52 import com.jogamp.opengl.util.Animator;
53 import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
54 import com.jogamp.opengl.test.junit.newt.parenting.NewtAWTReparentingKeyAdapter;
55
56 import javax.media.nativewindow.util.Dimension;
57 import javax.media.nativewindow.util.Point;
58 import javax.media.nativewindow.util.PointImmutable;
59 import javax.media.nativewindow.util.DimensionImmutable;
60 import javax.media.opengl.GLCapabilities;
61 import javax.media.opengl.GLCapabilitiesImmutable;
62 import javax.media.opengl.GLProfile;
63 import javax.swing.SwingUtilities;
64
65 import org.junit.Assert;
66 import org.junit.Assume;
67 import org.junit.BeforeClass;
68 import org.junit.AfterClass;
69 import org.junit.Test;
70 import org.junit.FixMethodOrder;
71 import org.junit.runners.MethodSorters;
72
73 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
74 public class TestGearsES2NewtCanvasAWT extends UITestCase {    
75     public enum FrameLayout { None, TextOnBottom, BorderBottom, BorderBottom2, BorderCenter, BorderCenterSurrounded, DoubleBorderCenterSurrounded };
76     public enum ResizeBy { GLWindow, Component, Frame };
77     
78     static int screenIdx = 0;
79     static PointImmutable wpos;
80     static DimensionImmutable wsize, rwsize = null;
81     static FrameLayout frameLayout = FrameLayout.None;
82     static ResizeBy resizeBy = ResizeBy.Component;
83     
84     static long duration = 500; // ms
85     static boolean opaque = true;
86     static int forceAlpha = -1;
87     static boolean fullscreen = false;
88     static boolean pmvUseBackingArray = true;
89     static int swapInterval = 1;
90     static boolean showFPS = false;
91     static int loops = 1;
92     static boolean loop_shutdown = false;
93     static boolean shallUseOffscreenFBOLayer = false;
94     static boolean forceES2 = false;
95     static boolean forceGL3 = false;
96     static boolean mainRun = false;
97     static boolean exclusiveContext = false;
98     static boolean useAnimator = true;
99     
100     @BeforeClass
101     public static void initClass() {
102         if(null == wsize) {
103             wsize = new Dimension(640, 480);
104         }
105     }
106
107     @AfterClass
108     public static void releaseClass() {
109     }
110
111     static void setGLWindowSize(final Frame frame, final GLWindow glw, final DimensionImmutable new_sz) {
112         try {
113             glw.setSize(new_sz.getWidth(), new_sz.getHeight());
114             if( null != frame ) {
115                 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
116                     public void run() {
117                         frame.pack();
118                     } } );
119             }
120         } catch( Throwable throwable ) {
121             throwable.printStackTrace();
122             Assume.assumeNoException( throwable );
123         }       
124     }
125     static void setComponentSize(final Frame frame, final Component comp, final DimensionImmutable new_sz) {
126         try {
127             javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
128                 public void run() {
129                     java.awt.Dimension d = new java.awt.Dimension(new_sz.getWidth(), new_sz.getHeight());
130                     comp.setMinimumSize(d);
131                     comp.setPreferredSize(d);
132                     comp.setSize(d);
133                     if( null != frame ) {
134                         frame.pack();
135                     }
136                 } } );
137         } catch( Throwable throwable ) {
138             throwable.printStackTrace();
139             Assume.assumeNoException( throwable );
140         }       
141     }
142     static void setFrameSize(final Frame frame, final boolean frameLayout, final DimensionImmutable new_sz) {
143         try {
144             javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
145                 public void run() {
146                     java.awt.Dimension d = new java.awt.Dimension(new_sz.getWidth(), new_sz.getHeight());
147                     frame.setSize(d);
148                     if( frameLayout ) {
149                         frame.validate();
150                     }
151                 } } );
152         } catch( Throwable throwable ) {
153             throwable.printStackTrace();
154             Assume.assumeNoException( throwable );
155         }       
156     }
157     
158     static void setSize(final ResizeBy resizeBy, final Frame frame, final boolean frameLayout, final Component comp, final GLWindow glw, final DimensionImmutable new_sz) {
159         switch( resizeBy ) {
160             case GLWindow:
161                 setGLWindowSize(frameLayout ? frame : null, glw, new_sz);
162                 break;
163             case Component:
164                 setComponentSize(frameLayout ? frame : null, comp, new_sz);
165                 break;
166             case Frame:
167                 setFrameSize(frame, frameLayout, new_sz);
168                 break;
169         }        
170     }
171     
172     // public enum ResizeBy { GLWindow, Component, Frame };
173     protected void runTestGL(final GLCapabilitiesImmutable caps, final ResizeBy resizeBy, final FrameLayout frameLayout) throws InterruptedException, InvocationTargetException {
174         System.err.println("requested: vsync "+swapInterval+", "+caps);
175         Display dpy = NewtFactory.createDisplay(null);
176         Screen screen = NewtFactory.createScreen(dpy, screenIdx);
177         final GLWindow glWindow = GLWindow.create(screen, caps);
178         Assert.assertNotNull(glWindow);
179         
180         final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow);
181         if ( shallUseOffscreenFBOLayer ) {
182             newtCanvasAWT.setShallUseOffscreenLayer(true);
183         }
184         
185         final Frame frame = new Frame("AWT Parent Frame");
186         
187         setSize(resizeBy, frame, false, newtCanvasAWT, glWindow, wsize);
188
189         switch( frameLayout) {
190             case None:
191                 frame.add(newtCanvasAWT);
192                 break;
193             case TextOnBottom:
194                 final TextArea ta = new TextArea(2, 20);
195                 ta.append("0123456789");
196                 ta.append(Platform.getNewline());
197                 ta.append("Some Text");
198                 ta.append(Platform.getNewline());
199                 frame.setLayout(new BorderLayout());
200                 frame.add(ta, BorderLayout.SOUTH);
201                 frame.add(newtCanvasAWT, BorderLayout.CENTER);
202                 break;                
203             case BorderBottom:
204                 frame.setLayout(new BorderLayout());
205                 frame.add(newtCanvasAWT, BorderLayout.SOUTH);
206                 break;
207             case BorderBottom2:
208                 frame.setLayout(new BorderLayout());
209                 frame.add(newtCanvasAWT, BorderLayout.SOUTH);
210                 frame.add(new Button("North"), BorderLayout.NORTH);
211                 break;
212             case BorderCenter:
213                 frame.setLayout(new BorderLayout());
214                 frame.add(newtCanvasAWT, BorderLayout.CENTER);
215                 break;
216             case BorderCenterSurrounded:
217                 frame.setLayout(new BorderLayout());
218                 frame.add(new Button("NORTH"), BorderLayout.NORTH);
219                 frame.add(new Button("SOUTH"), BorderLayout.SOUTH);
220                 frame.add(new Button("EAST"), BorderLayout.EAST);
221                 frame.add(new Button("WEST"), BorderLayout.WEST);
222                 frame.add(newtCanvasAWT, BorderLayout.CENTER);
223                 break;
224             case DoubleBorderCenterSurrounded:
225                 Container c = new Container();
226                 c.setLayout(new BorderLayout());
227                 c.add(new Button("north"), BorderLayout.NORTH);
228                 c.add(new Button("south"), BorderLayout.SOUTH);
229                 c.add(new Button("east"), BorderLayout.EAST);
230                 c.add(new Button("west"), BorderLayout.WEST);
231                 c.add(newtCanvasAWT, BorderLayout.CENTER);
232                 
233                 frame.setLayout(new BorderLayout());
234                 frame.add(new Button("NORTH"), BorderLayout.NORTH);
235                 frame.add(new Button("SOUTH"), BorderLayout.SOUTH);
236                 frame.add(new Button("EAST"), BorderLayout.EAST);
237                 frame.add(new Button("WEST"), BorderLayout.WEST);
238                 frame.add(c, BorderLayout.CENTER);
239                 break;
240         }
241         
242         frame.setTitle("Gears NewtCanvasAWT Test (translucent "+!caps.isBackgroundOpaque()+"), swapInterval "+swapInterval+", size "+wsize+", pos "+wpos);
243         
244         final GearsES2 demo = new GearsES2(swapInterval);
245         demo.setPMVUseBackingArray(pmvUseBackingArray);
246         glWindow.addGLEventListener(demo);
247         
248         final Animator animator = useAnimator ? new Animator() : null;
249         if( useAnimator ) {
250             animator.setModeBits(false, Animator.MODE_EXPECT_AWT_RENDERING_THREAD);
251             animator.setExclusiveContext(exclusiveContext);
252         }
253         
254         final QuitAdapter quitAdapter = new QuitAdapter();
255         //glWindow.addKeyListener(new TraceKeyAdapter(quitAdapter));
256         //glWindow.addWindowListener(new TraceWindowAdapter(quitAdapter));
257         glWindow.addKeyListener(quitAdapter);
258         glWindow.addWindowListener(quitAdapter);
259
260         glWindow.addWindowListener(new WindowAdapter() {
261             public void windowResized(WindowEvent e) {
262                 System.err.println("window resized: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight());
263             }
264             public void windowMoved(WindowEvent e) {
265                 System.err.println("window moved:   "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight());
266             }            
267         });
268         
269         glWindow.addKeyListener(new NewtAWTReparentingKeyAdapter(frame, newtCanvasAWT, glWindow, quitAdapter));
270         
271         if( useAnimator ) {
272             animator.add(glWindow);
273             animator.start();
274             Assert.assertTrue(animator.isStarted());
275             Assert.assertTrue(animator.isAnimating());
276             Assert.assertEquals(exclusiveContext ? animator.getThread() : null, glWindow.getExclusiveContextThread());
277         }
278
279         SwingUtilities.invokeAndWait(new Runnable() {
280            public void run() {
281                if( ResizeBy.Frame == resizeBy ) {
282                    frame.validate();
283                } else {
284                    frame.pack();                   
285                }                
286                frame.setVisible(true);               
287            }
288         });        
289         Assert.assertEquals(true,  AWTRobotUtil.waitForVisible(frame, true));
290         Assert.assertEquals(true,  AWTRobotUtil.waitForRealized(glWindow, true)); 
291         
292         if( useAnimator ) {
293             animator.setUpdateFPSFrames(60, showFPS ? System.err : null);
294         }
295         
296         System.err.println("NW chosen: "+glWindow.getDelegatedWindow().getChosenCapabilities());
297         System.err.println("GL chosen: "+glWindow.getChosenCapabilities());
298         System.err.println("window pos/siz: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", "+glWindow.getInsets());
299                 
300         if( null != rwsize ) {
301             Thread.sleep(500); // 500ms delay 
302             setSize(resizeBy, frame, true, newtCanvasAWT, glWindow, rwsize);
303             System.err.println("window resize "+rwsize+" -> pos/siz: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", "+glWindow.getInsets());
304         }
305         
306         final long t0 = System.currentTimeMillis();
307         long t1 = t0;
308         while(!quitAdapter.shouldQuit() && t1-t0<duration) {
309             Thread.sleep(100);
310             t1 = System.currentTimeMillis();
311         }
312
313         if( useAnimator ) {
314             Assert.assertEquals(exclusiveContext ? animator.getThread() : null, glWindow.getExclusiveContextThread());
315             animator.stop();
316             Assert.assertFalse(animator.isAnimating());
317             Assert.assertFalse(animator.isStarted());
318         }
319         Assert.assertEquals(null, glWindow.getExclusiveContextThread());
320         SwingUtilities.invokeAndWait(new Runnable() {
321            public void run() {               
322                frame.dispose();               
323            }
324         });
325         glWindow.destroy();
326         Assert.assertEquals(true,  AWTRobotUtil.waitForRealized(glWindow, false));
327     }
328
329     @Test
330     public void test01GL2ES2() throws InterruptedException, InvocationTargetException {
331         for(int i=1; i<=loops; i++) {
332             System.err.println("Loop "+i+"/"+loops);
333             final GLProfile glp;
334             if(forceGL3) {
335                 glp = GLProfile.get(GLProfile.GL3);
336             } else if(forceES2) {
337                 glp = GLProfile.get(GLProfile.GLES2);
338             } else {
339                 glp = GLProfile.getGL2ES2();
340             }
341             final GLCapabilities caps = new GLCapabilities( glp );
342             caps.setBackgroundOpaque(opaque);
343             if(-1 < forceAlpha) {
344                 caps.setAlphaBits(forceAlpha); 
345             }
346             runTestGL(caps, resizeBy, frameLayout);
347             if(loop_shutdown) {
348                 GLProfile.shutdown();
349             }
350         }
351     }
352
353     @Test
354     public void test02GL3() throws InterruptedException, InvocationTargetException {
355         if(mainRun) return;
356         
357         if( !GLProfile.isAvailable(GLProfile.GL3) ) {
358             System.err.println("GL3 n/a");
359             return;
360         }
361         final GLProfile glp = GLProfile.get(GLProfile.GL3);
362         final GLCapabilities caps = new GLCapabilities( glp );
363         runTestGL(caps, resizeBy, frameLayout);
364     }
365     
366     public static void main(String args[]) throws IOException {
367         mainRun = true;
368         
369         int x=0, y=0, w=640, h=480;
370         int rw=-1, rh=-1;
371         boolean usePos = false;
372         
373         for(int i=0; i<args.length; i++) {
374             if(args[i].equals("-time")) {
375                 i++;
376                 duration = MiscUtils.atol(args[i], duration);
377             } else if(args[i].equals("-rwidth")) {
378                 i++;
379                 rw = MiscUtils.atoi(args[i], rw);
380             } else if(args[i].equals("-rheight")) {
381                 i++;
382                 rh = MiscUtils.atoi(args[i], rh);
383             } else if(args[i].equals("-layout")) {
384                 i++;
385                 frameLayout = FrameLayout.valueOf(args[i]);
386             } else if(args[i].equals("-resizeBy")) {
387                 i++;
388                 resizeBy = ResizeBy.valueOf(args[i]);
389             } else if(args[i].equals("-translucent")) {
390                 opaque = false;
391             } else if(args[i].equals("-forceAlpha")) {
392                 i++;
393                 forceAlpha = MiscUtils.atoi(args[i], 0);
394             } else if(args[i].equals("-fullscreen")) {
395                 fullscreen = true;
396             } else if(args[i].equals("-pmvDirect")) {
397                 pmvUseBackingArray = false;
398             } else if(args[i].equals("-vsync")) {
399                 i++;
400                 swapInterval = MiscUtils.atoi(args[i], swapInterval);
401             } else if(args[i].equals("-layeredFBO")) {
402                 shallUseOffscreenFBOLayer = true;
403             } else if(args[i].equals("-exclctx")) {
404                 exclusiveContext = true;
405             } else if(args[i].equals("-noanim")) {
406                 useAnimator  = false;
407             } else if(args[i].equals("-es2")) {
408                 forceES2 = true;
409             } else if(args[i].equals("-gl3")) {
410                 forceGL3 = true;
411             } else if(args[i].equals("-showFPS")) {
412                 showFPS = true;
413             } else if(args[i].equals("-width")) {
414                 i++;
415                 w = MiscUtils.atoi(args[i], w);
416             } else if(args[i].equals("-height")) {
417                 i++;
418                 h = MiscUtils.atoi(args[i], h);
419             } else if(args[i].equals("-x")) {
420                 i++;
421                 x = MiscUtils.atoi(args[i], x);
422                 usePos = true;
423             } else if(args[i].equals("-y")) {
424                 i++;
425                 y = MiscUtils.atoi(args[i], y);
426                 usePos = true;
427             } else if(args[i].equals("-screen")) {
428                 i++;
429                 screenIdx = MiscUtils.atoi(args[i], 0);
430             } else if(args[i].equals("-loops")) {
431                 i++;
432                 loops = MiscUtils.atoi(args[i], 1);
433             } else if(args[i].equals("-loop-shutdown")) {
434                 loop_shutdown = true;
435             }
436         }
437         wsize = new Dimension(w, h);
438         if( 0 < rw && 0 < rh ) {
439             rwsize = new Dimension(rw, rh);
440         }
441         
442         if(usePos) {
443             wpos = new Point(x, y);
444         }
445         
446         System.err.println("frameLayout "+frameLayout);
447         System.err.println("resizeBy "+resizeBy);
448         System.err.println("position "+wpos);
449         System.err.println("size "+wsize);
450         System.err.println("resize "+rwsize);
451         System.err.println("screen "+screenIdx);
452         System.err.println("translucent "+(!opaque));
453         System.err.println("forceAlpha "+forceAlpha);        
454         System.err.println("fullscreen "+fullscreen);
455         System.err.println("pmvDirect "+(!pmvUseBackingArray));        
456         System.err.println("loops "+loops);
457         System.err.println("loop shutdown "+loop_shutdown);
458         System.err.println("shallUseOffscreenFBOLayer     "+shallUseOffscreenFBOLayer);
459         System.err.println("forceES2 "+forceES2);
460         System.err.println("forceGL3 "+forceGL3);
461         System.err.println("swapInterval "+swapInterval);
462         System.err.println("exclusiveContext "+exclusiveContext);
463         System.err.println("useAnimator "+useAnimator);
464
465         org.junit.runner.JUnitCore.main(TestGearsES2NewtCanvasAWT.class.getName());
466     }
467 }
http://JogAmp.org git info: FAQ, tutorial and man pages.