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 / newt / TestGearsES2NEWT.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.io.IOException;
32 import java.lang.reflect.InvocationTargetException;
33 import java.net.URLConnection;
34
35 import com.jogamp.common.util.IOUtil;
36 import com.jogamp.newt.Display;
37 import com.jogamp.newt.Display.PointerIcon;
38 import com.jogamp.newt.NewtFactory;
39 import com.jogamp.newt.Screen;
40 import com.jogamp.newt.Window;
41 import com.jogamp.newt.event.KeyAdapter;
42 import com.jogamp.newt.event.KeyEvent;
43 import com.jogamp.newt.event.MouseAdapter;
44 import com.jogamp.newt.event.MouseEvent;
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.newt.util.EDTUtil;
49 import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
50 import com.jogamp.opengl.test.junit.util.MiscUtils;
51 import com.jogamp.opengl.test.junit.util.UITestCase;
52 import com.jogamp.opengl.test.junit.util.QuitAdapter;
53 import com.jogamp.opengl.util.Animator;
54 import com.jogamp.opengl.util.PNGPixelRect;
55 import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
56
57 import javax.media.nativewindow.NativeWindowFactory;
58 import javax.media.nativewindow.ScalableSurface;
59 import javax.media.nativewindow.util.Dimension;
60 import javax.media.nativewindow.util.Point;
61 import javax.media.nativewindow.util.PointImmutable;
62 import javax.media.nativewindow.util.DimensionImmutable;
63 import javax.media.opengl.GLAnimatorControl;
64 import javax.media.opengl.GLAutoDrawable;
65 import javax.media.opengl.GLCapabilities;
66 import javax.media.opengl.GLCapabilitiesImmutable;
67 import javax.media.opengl.GLEventListener;
68 import javax.media.opengl.GLProfile;
69
70 import jogamp.newt.DefaultEDTUtil;
71
72 import org.junit.Assert;
73 import org.junit.BeforeClass;
74 import org.junit.AfterClass;
75 import org.junit.Test;
76 import org.junit.FixMethodOrder;
77 import org.junit.runners.MethodSorters;
78
79 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
80 public class TestGearsES2NEWT extends UITestCase {
81     static int screenIdx = 0;
82     static PointImmutable wpos;
83     static DimensionImmutable wsize, rwsize=null;
84     static int[] reqSurfacePixelScale = new int[] { ScalableSurface.AUTOMAX_PIXELSCALE, ScalableSurface.AUTOMAX_PIXELSCALE };
85
86     static long duration = 500; // ms
87     static boolean opaque = true;
88     static int forceAlpha = -1;
89     static boolean undecorated = false;
90     static boolean alwaysOnTop = false;
91     static boolean fullscreen = false;
92     static boolean pmvUseBackingArray = true;
93     static int swapInterval = 1;
94     static boolean waitForKey = false;
95     static boolean mouseVisible = true;
96     static boolean mouseConfined = false;
97     static boolean setPointerIcon = false;
98     static boolean showFPS = false;
99     static int loops = 1;
100     static boolean loop_shutdown = false;
101     static boolean forceES2 = false;
102     static boolean forceES3 = false;
103     static boolean forceGL3 = false;
104     static boolean forceGL2 = false;
105     static boolean manualTest = false;
106     static boolean exclusiveContext = false;
107     static boolean useAnimator = true;
108     static boolean useMappedBuffers = false;
109     static enum SysExit { none, testExit, testError, testEDTError, displayExit, displayError, displayEDTError };
110     static SysExit sysExit = SysExit.none;
111
112     @BeforeClass
113     public static void initClass() {
114         if(null == wsize) {
115             wsize = new Dimension(640, 480);
116         }
117     }
118
119     @AfterClass
120     public static void releaseClass() {
121     }
122
123     private void setTitle(final Window win, final GLCapabilitiesImmutable caps) {
124         final String capsA = caps.isBackgroundOpaque() ? "opaque" : "transl";
125         win.setTitle("GLWindow["+capsA+"], swapI "+swapInterval+", win: "+win.getBounds()+", pix: "+win.getSurfaceWidth()+"x"+win.getSurfaceHeight());
126     }
127     protected void runTestGL(final GLCapabilitiesImmutable caps, boolean undecorated) throws InterruptedException {
128         System.err.println("requested: vsync "+swapInterval+", "+caps);
129         Display dpy = NewtFactory.createDisplay(null);
130         Screen screen = NewtFactory.createScreen(dpy, screenIdx);
131         final GLWindow glWindow = GLWindow.create(screen, caps);
132         Assert.assertNotNull(glWindow);
133         glWindow.setSurfaceScale(reqSurfacePixelScale);
134         final int[] valReqSurfacePixelScale = glWindow.getRequestedSurfaceScale(new int[2]);
135         glWindow.setSize(wsize.getWidth(), wsize.getHeight());
136         if(null != wpos) {
137             glWindow.setPosition(wpos.getX(), wpos.getY());
138         }
139         glWindow.setUndecorated(undecorated);
140         glWindow.setAlwaysOnTop(alwaysOnTop);
141         glWindow.setFullscreen(fullscreen);
142         glWindow.setPointerVisible(mouseVisible);
143         glWindow.confinePointer(mouseConfined);
144         setTitle(glWindow, caps);
145
146         final GearsES2 demo = new GearsES2(swapInterval);
147         demo.setPMVUseBackingArray(pmvUseBackingArray);
148         demo.setUseMappedBuffers(useMappedBuffers);
149         demo.setValidateBuffers(true);
150         glWindow.addGLEventListener(demo);
151
152         final SnapshotGLEventListener snap = new SnapshotGLEventListener();
153         glWindow.addGLEventListener(snap);
154         if(waitForKey) {
155             glWindow.addGLEventListener(new GLEventListener() {
156                 public void init(GLAutoDrawable drawable) { }
157                 public void dispose(GLAutoDrawable drawable) { }
158                 public void display(GLAutoDrawable drawable) {
159                     GLAnimatorControl  actrl = drawable.getAnimator();
160                     if(waitForKey && actrl.getTotalFPSFrames() == 60*3) {
161                         UITestCase.waitForKey("3s mark");
162                         actrl.resetFPSCounter();
163                         waitForKey = false;
164                     }
165                 }
166                 public void reshape(GLAutoDrawable drawable, int x, int y,
167                         int width, int height) { }
168             });
169         }
170
171         final Animator animator = useAnimator ? new Animator() : null;
172         if( useAnimator ) {
173             animator.setModeBits(false, Animator.MODE_EXPECT_AWT_RENDERING_THREAD);
174             animator.setExclusiveContext(exclusiveContext);
175         }
176
177         final QuitAdapter quitAdapter = new QuitAdapter();
178         //glWindow.addKeyListener(new TraceKeyAdapter(quitAdapter));
179         //glWindow.addWindowListener(new TraceWindowAdapter(quitAdapter));
180         glWindow.addKeyListener(quitAdapter);
181         glWindow.addWindowListener(quitAdapter);
182
183         glWindow.addWindowListener(new WindowAdapter() {
184             public void windowResized(WindowEvent e) {
185                 System.err.println("window resized: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight());
186                 setTitle(glWindow, caps);
187             }
188             public void windowMoved(WindowEvent e) {
189                 System.err.println("window moved:   "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight());
190                 setTitle(glWindow, caps);
191             }
192         });
193
194         final PointerIcon[] pointerIcons = { null, null, null };
195         {
196             final Display disp = glWindow.getScreen().getDisplay();
197             disp.createNative();
198             {
199                 PointerIcon _pointerIcon = null;
200                 final IOUtil.ClassResources res = new IOUtil.ClassResources(glWindow.getClass(), new String[] { "newt/data/cross-grey-alpha-16x16.png" } );
201                 try {
202                     _pointerIcon = disp.createPointerIcon(res, 8, 8);
203                     System.err.println("Create PointerIcon #01: "+_pointerIcon);
204                 } catch (Exception e) {
205                     e.printStackTrace();
206                 }
207                 pointerIcons[0] = _pointerIcon;
208             }
209             {
210                 PointerIcon _pointerIcon = null;
211                 final IOUtil.ClassResources res = new IOUtil.ClassResources(glWindow.getClass(), new String[] { "newt/data/pointer-grey-alpha-16x24.png" } );
212                 try {
213                     _pointerIcon = disp.createPointerIcon(res, 0, 0);
214                     System.err.println("Create PointerIcon #02: "+_pointerIcon);
215                 } catch (Exception e) {
216                     e.printStackTrace();
217                 }
218                 pointerIcons[1] = _pointerIcon;
219             }
220             {
221                 PointerIcon _pointerIcon = null;
222                 final IOUtil.ClassResources res = new IOUtil.ClassResources(glWindow.getClass(), new String[] { "jogamp-pointer-64x64.png" } );
223                 try {
224                     final URLConnection urlConn = res.resolve(0);
225                     final PNGPixelRect image = PNGPixelRect.read(urlConn.getInputStream(), null, false /* directBuffer */, 0 /* destMinStrideInBytes */, false /* destIsGLOriented */);
226                     System.err.println("Create PointerIcon #03: "+image);
227                     _pointerIcon = disp.createPointerIcon(image, 32, 0);
228                     System.err.println("Create PointerIcon #03: "+_pointerIcon);
229                 } catch (Exception e) {
230                     e.printStackTrace();
231                 }
232                 pointerIcons[2] = _pointerIcon;
233             }
234         }
235         if( setPointerIcon ) {
236             glWindow.setPointerIcon(pointerIcons[0]);
237             System.err.println("Set PointerIcon: "+glWindow.getPointerIcon());
238         }
239
240         glWindow.addKeyListener(new KeyAdapter() {
241             int pointerIconIdx = 0;
242
243             @Override
244             public void keyPressed(final KeyEvent e) {
245                 if( e.isAutoRepeat() ) {
246                     return;
247                 }
248                 if(e.getKeyChar()=='f') {
249                     new Thread() {
250                         public void run() {
251                             final Thread t = glWindow.setExclusiveContextThread(null);
252                             System.err.println("[set fullscreen  pre]: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", f "+glWindow.isFullscreen()+", a "+glWindow.isAlwaysOnTop()+", "+glWindow.getInsets());
253                             if( glWindow.isFullscreen() ) {
254                                 glWindow.setFullscreen( false );
255                             } else {
256                                 if( e.isAltDown() ) {
257                                     glWindow.setFullscreen( null );
258                                 } else {
259                                     glWindow.setFullscreen( true );
260                                 }
261                             }
262                             System.err.println("[set fullscreen post]: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", f "+glWindow.isFullscreen()+", a "+glWindow.isAlwaysOnTop()+", "+glWindow.getInsets());
263                             glWindow.setExclusiveContextThread(t);
264                     } }.start();
265                 } else if(e.getKeyChar()=='a') {
266                     new Thread() {
267                         public void run() {
268                             final Thread t = glWindow.setExclusiveContextThread(null);
269                             System.err.println("[set alwaysontop pre]: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", f "+glWindow.isFullscreen()+", a "+glWindow.isAlwaysOnTop()+", "+glWindow.getInsets());
270                             glWindow.setAlwaysOnTop(!glWindow.isAlwaysOnTop());
271                             System.err.println("[set alwaysontop post]: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", f "+glWindow.isFullscreen()+", a "+glWindow.isAlwaysOnTop()+", "+glWindow.getInsets());
272                             glWindow.setExclusiveContextThread(t);
273                     } }.start();
274                 } else if(e.getKeyChar()=='d') {
275                     new Thread() {
276                         public void run() {
277                             final Thread t = glWindow.setExclusiveContextThread(null);
278                             // while( null != glWindow.getExclusiveContextThread() ) ;
279                             System.err.println("[set undecorated  pre]: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", d "+glWindow.isUndecorated()+", "+glWindow.getInsets());
280                             glWindow.setUndecorated(!glWindow.isUndecorated());
281                             System.err.println("[set undecorated post]: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", d "+glWindow.isUndecorated()+", "+glWindow.getInsets());
282                             glWindow.setExclusiveContextThread(t);
283                     } }.start();
284                 } else if(e.getKeyChar()=='s') {
285                     new Thread() {
286                         public void run() {
287                             final Thread t = glWindow.setExclusiveContextThread(null);
288                             System.err.println("[set position  pre]: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", "+glWindow.getInsets());
289                             glWindow.setPosition(100, 100);
290                             System.err.println("[set position post]: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", "+glWindow.getInsets());
291                             glWindow.setExclusiveContextThread(t);
292                     } }.start();
293                 } else if(e.getKeyChar()=='c') {
294                     new Thread() {
295                         public void run() {
296                             final Thread t = glWindow.setExclusiveContextThread(null);
297                             System.err.println("[set pointer-icon pre]");
298                             final PointerIcon currentPI = glWindow.getPointerIcon();
299                             final PointerIcon newPI;
300                             if( pointerIconIdx >= pointerIcons.length ) {
301                                 newPI=null;
302                                 pointerIconIdx=0;
303                             } else {
304                                 newPI=pointerIcons[pointerIconIdx++];
305                             }
306                             glWindow.setPointerIcon( newPI );
307                             System.err.println("[set pointer-icon post] "+currentPI+" -> "+glWindow.getPointerIcon());
308                             glWindow.setExclusiveContextThread(t);
309                     } }.start();
310                 } else if(e.getKeyChar()=='i') {
311                     new Thread() {
312                         public void run() {
313                             final Thread t = glWindow.setExclusiveContextThread(null);
314                             System.err.println("[set mouse visible pre]: "+glWindow.isPointerVisible());
315                             glWindow.setPointerVisible(!glWindow.isPointerVisible());
316                             System.err.println("[set mouse visible post]: "+glWindow.isPointerVisible());
317                             glWindow.setExclusiveContextThread(t);
318                     } }.start();
319                 } else if(e.getKeyChar()=='j') {
320                     new Thread() {
321                         public void run() {
322                             final Thread t = glWindow.setExclusiveContextThread(null);
323                             System.err.println("[set mouse confined pre]: "+glWindow.isPointerConfined());
324                             glWindow.confinePointer(!glWindow.isPointerConfined());
325                             System.err.println("[set mouse confined post]: "+glWindow.isPointerConfined());
326                             if(!glWindow.isPointerConfined()) {
327                                 demo.setConfinedFixedCenter(false);
328                             }
329                             glWindow.setExclusiveContextThread(t);
330                     } }.start();
331                 } else if(e.getKeyChar()=='J') {
332                     new Thread() {
333                         public void run() {
334                             final Thread t = glWindow.setExclusiveContextThread(null);
335                             System.err.println("[set mouse confined pre]: "+glWindow.isPointerConfined());
336                             glWindow.confinePointer(!glWindow.isPointerConfined());
337                             System.err.println("[set mouse confined post]: "+glWindow.isPointerConfined());
338                             demo.setConfinedFixedCenter(glWindow.isPointerConfined());
339                             glWindow.setExclusiveContextThread(t);
340                     } }.start();
341                 } else if(e.getKeyChar()=='w') {
342                     new Thread() {
343                         public void run() {
344                             final Thread t = glWindow.setExclusiveContextThread(null);
345                             System.err.println("[set mouse pos pre]");
346                             glWindow.warpPointer(glWindow.getSurfaceWidth()/2, glWindow.getSurfaceHeight()/2);
347                             System.err.println("[set mouse pos post]");
348                             glWindow.setExclusiveContextThread(t);
349                     } }.start();
350                 } else if(e.getKeyChar()=='x') {
351                     final int[] hadSurfacePixelScale = glWindow.getCurrentSurfaceScale(new int[2]);
352                     final int[] reqSurfacePixelScale;
353                     if( hadSurfacePixelScale[0] == ScalableSurface.IDENTITY_PIXELSCALE ) {
354                         reqSurfacePixelScale = new int[] { ScalableSurface.AUTOMAX_PIXELSCALE, ScalableSurface.AUTOMAX_PIXELSCALE };
355                     } else {
356                         reqSurfacePixelScale = new int[] { ScalableSurface.IDENTITY_PIXELSCALE, ScalableSurface.IDENTITY_PIXELSCALE };
357                     }
358                     System.err.println("[set PixelScale pre]: had "+hadSurfacePixelScale[0]+"x"+hadSurfacePixelScale[1]+" -> req "+reqSurfacePixelScale[0]+"x"+reqSurfacePixelScale[1]);
359                     glWindow.setSurfaceScale(reqSurfacePixelScale);
360                     final int[] valReqSurfacePixelScale = glWindow.getRequestedSurfaceScale(new int[2]);
361                     final int[] hasSurfacePixelScale0 = glWindow.convertToPixelUnits(new int[] { 1, 1 });
362                     final int[] hasSurfacePixelScale1 = glWindow.getCurrentSurfaceScale(new int[2]);
363                     System.err.println("[set PixelScale post]: "+hadSurfacePixelScale[0]+"x"+hadSurfacePixelScale[1]+" (had) -> "+
364                                        reqSurfacePixelScale[0]+"x"+reqSurfacePixelScale[1]+" (req) -> "+
365                                        valReqSurfacePixelScale[0]+"x"+valReqSurfacePixelScale[1]+" (val) -> "+
366                                        hasSurfacePixelScale1[0]+"x"+hasSurfacePixelScale1[1]+" (has)");
367                     setTitle(glWindow, caps);
368                     Assert.assertArrayEquals(hasSurfacePixelScale0, hasSurfacePixelScale1);
369                 }
370             }
371         });
372         glWindow.addMouseListener(new MouseAdapter() {
373             public void mouseClicked(MouseEvent e) {
374                 if(e.getClickCount() == 2 && e.getPointerCount() == 1) {
375                     glWindow.setFullscreen(!glWindow.isFullscreen());
376                     System.err.println("setFullscreen: "+glWindow.isFullscreen());
377                 }
378             }
379          });
380
381         if( useAnimator ) {
382             animator.add(glWindow);
383             animator.start();
384             Assert.assertTrue(animator.isStarted());
385             Assert.assertTrue(animator.isAnimating());
386             Assert.assertEquals(exclusiveContext ? animator.getThread() : null, glWindow.getExclusiveContextThread());
387         }
388
389         if( SysExit.displayError == sysExit || SysExit.displayExit == sysExit || SysExit.displayEDTError == sysExit ) {
390             glWindow.addGLEventListener(new GLEventListener() {
391                 @Override
392                 public void init(GLAutoDrawable drawable) {}
393                 @Override
394                 public void dispose(GLAutoDrawable drawable) { }
395                 @Override
396                 public void display(GLAutoDrawable drawable) {
397                     final GLAnimatorControl anim = drawable.getAnimator();
398                     if( null != anim && anim.isAnimating() ) {
399                         final long ms = anim.getTotalFPSDuration();
400                         if( ms >= duration/2 || ms >= 3000 ) { // max 3s wait until provoking error
401                             if( SysExit.displayError == sysExit ) {
402                                 throw new Error("test error send from GLEventListener.display - "+Thread.currentThread());
403                             } else if ( SysExit.displayExit == sysExit ) {
404                                 System.err.println("exit(0) send from GLEventListener");
405                                 System.exit(0);
406                             } else if ( SysExit.displayEDTError == sysExit ) {
407                                 final Object upstream = drawable.getUpstreamWidget();
408                                 System.err.println("EDT invokeAndWaitError: upstream type "+upstream.getClass().getName());
409                                 if( upstream instanceof Window ) {
410                                     final EDTUtil edt = ((Window)upstream).getScreen().getDisplay().getEDTUtil();
411                                     System.err.println("EDT invokeAndWaitError: edt type "+edt.getClass().getName());
412                                     if( edt instanceof DefaultEDTUtil ) {
413                                         quitAdapter.doQuit();
414                                         ((DefaultEDTUtil)edt).invokeAndWaitError(new Runnable() {
415                                             public void run() {
416                                                 throw new RuntimeException("XXX Should never ever be seen! - "+Thread.currentThread());
417                                             }
418                                         });
419                                     }
420                                 }
421                             }
422                         }
423                     } else {
424                         System.exit(0);
425                     }
426                 }
427                 @Override
428                 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
429             });
430         }
431
432         glWindow.setVisible(true);
433         if( useAnimator ) {
434             animator.setUpdateFPSFrames(60, showFPS ? System.err : null);
435         }
436
437         System.err.println("NW chosen: "+glWindow.getDelegatedWindow().getChosenCapabilities());
438         System.err.println("GL chosen: "+glWindow.getChosenCapabilities());
439         System.err.println("window pos/siz: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", "+glWindow.getInsets());
440
441         final int[] hasSurfacePixelScale0 = glWindow.convertToPixelUnits(new int[] { 1, 1 });
442         final int[] hasSurfacePixelScale1 = glWindow.getCurrentSurfaceScale(new int[2]);
443         System.err.println("HiDPI PixelScale: "+reqSurfacePixelScale[0]+"x"+reqSurfacePixelScale[1]+" (req) -> "+
444                            valReqSurfacePixelScale[0]+"x"+valReqSurfacePixelScale[1]+" (val) -> "+
445                            hasSurfacePixelScale1[0]+"x"+hasSurfacePixelScale1[1]+" (has)");
446         setTitle(glWindow, caps);
447         Assert.assertArrayEquals(hasSurfacePixelScale0, hasSurfacePixelScale1);
448
449         snap.setMakeSnapshot();
450
451         if( null != rwsize ) {
452             Thread.sleep(500); // 500ms delay
453             glWindow.setSize(rwsize.getWidth(), rwsize.getHeight());
454             System.err.println("window resize pos/siz: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", "+glWindow.getInsets());
455         }
456
457         snap.setMakeSnapshot();
458
459         final long t0 = System.currentTimeMillis();
460         long t1 = t0;
461         while(!quitAdapter.shouldQuit() && t1-t0<duration) {
462             Thread.sleep(100);
463             t1 = System.currentTimeMillis();
464             if( SysExit.testError == sysExit || SysExit.testExit == sysExit || SysExit.testEDTError == sysExit) {
465                 final long ms = t1-t0;
466                 if( ms >= duration/2 || ms >= 3000 ) { // max 3s wait until provoking error
467                     if( SysExit.testError == sysExit ) {
468                         throw new Error("test error send from test thread");
469                     } else if ( SysExit.testExit == sysExit ) {
470                         System.err.println("exit(0) send from test thread");
471                         System.exit(0);
472                     } else if ( SysExit.testEDTError == sysExit ) {
473                         final EDTUtil edt = glWindow.getScreen().getDisplay().getEDTUtil();
474                         System.err.println("EDT invokeAndWaitError: edt type "+edt.getClass().getName());
475                         if( edt instanceof DefaultEDTUtil ) {
476                             quitAdapter.doQuit();
477                             ((DefaultEDTUtil)edt).invokeAndWaitError(new Runnable() {
478                                 public void run() {
479                                     throw new RuntimeException("XXX Should never ever be seen!");
480                                 }
481                             });
482                         }
483                     }
484                 }
485             }
486         }
487
488         if( useAnimator ) {
489             Assert.assertEquals(exclusiveContext ? animator.getThread() : null, glWindow.getExclusiveContextThread());
490             animator.stop();
491             Assert.assertFalse(animator.isAnimating());
492             Assert.assertFalse(animator.isStarted());
493         }
494         Assert.assertEquals(null, glWindow.getExclusiveContextThread());
495         glWindow.destroy();
496         if( NativeWindowFactory.isAWTAvailable() ) {
497             Assert.assertEquals(true,  AWTRobotUtil.waitForRealized(glWindow, false));
498         }
499     }
500
501     @Test
502     public void test01_GL2ES2() throws InterruptedException {
503         for(int i=1; i<=loops; i++) {
504             System.err.println("Loop "+i+"/"+loops);
505             final GLProfile glp;
506             if(forceGL3) {
507                 glp = GLProfile.get(GLProfile.GL3);
508             } else if(forceES3) {
509                 glp = GLProfile.get(GLProfile.GLES3);
510             } else if(forceES2) {
511                 glp = GLProfile.get(GLProfile.GLES2);
512             } else if(forceGL2) {
513                 glp = GLProfile.get(GLProfile.GL2);
514             } else {
515                 glp = GLProfile.getGL2ES2();
516             }
517             final GLCapabilities caps = new GLCapabilities( glp );
518             caps.setBackgroundOpaque(opaque);
519             if(-1 < forceAlpha) {
520                 caps.setAlphaBits(forceAlpha);
521             }
522             runTestGL(caps, undecorated);
523             if(loop_shutdown) {
524                 GLProfile.shutdown();
525             }
526         }
527     }
528
529     @Test
530     public void test02_GLES2() throws InterruptedException {
531         if(manualTest) return;
532
533         if( !GLProfile.isAvailable(GLProfile.GLES2) ) {
534             System.err.println("GLES2 n/a");
535             return;
536         }
537         final GLProfile glp = GLProfile.get(GLProfile.GLES2);
538         final GLCapabilities caps = new GLCapabilities( glp );
539         runTestGL(caps, undecorated);
540     }
541
542     @Test
543     public void test03_GL3() throws InterruptedException {
544         if(manualTest) return;
545
546         if( !GLProfile.isAvailable(GLProfile.GL3) ) {
547             System.err.println("GL3 n/a");
548             return;
549         }
550         final GLProfile glp = GLProfile.get(GLProfile.GL3);
551         final GLCapabilities caps = new GLCapabilities( glp );
552         runTestGL(caps, undecorated);
553     }
554
555     @Test
556     public void test99_PixelScale1_DefaultNorm() throws InterruptedException, InvocationTargetException {
557         if( manualTest ) return;
558
559         reqSurfacePixelScale[0] = ScalableSurface.IDENTITY_PIXELSCALE;
560         reqSurfacePixelScale[1] = ScalableSurface.IDENTITY_PIXELSCALE;
561
562         GLCapabilities caps = new GLCapabilities(GLProfile.getGL2ES2());
563         runTestGL(caps, undecorated);
564     }
565
566     public static void main(String args[]) throws IOException {
567         int x=0, y=0, w=640, h=480, rw=-1, rh=-1;
568         boolean usePos = false;
569
570         for(int i=0; i<args.length; i++) {
571             if(args[i].equals("-time")) {
572                 i++;
573                 duration = MiscUtils.atol(args[i], duration);
574             } else if(args[i].equals("-translucent")) {
575                 opaque = false;
576             } else if(args[i].equals("-forceAlpha")) {
577                 i++;
578                 forceAlpha = MiscUtils.atoi(args[i], 0);
579             } else if(args[i].equals("-undecorated")) {
580                 undecorated = true;
581             } else if(args[i].equals("-atop")) {
582                 alwaysOnTop = true;
583             } else if(args[i].equals("-fullscreen")) {
584                 fullscreen = true;
585             } else if(args[i].equals("-pmvDirect")) {
586                 pmvUseBackingArray = false;
587             } else if(args[i].equals("-vsync")) {
588                 i++;
589                 swapInterval = MiscUtils.atoi(args[i], swapInterval);
590             } else if(args[i].equals("-exclctx")) {
591                 exclusiveContext = true;
592             } else if(args[i].equals("-noanim")) {
593                 useAnimator  = false;
594             } else if(args[i].equals("-es2")) {
595                 forceES2 = true;
596             } else if(args[i].equals("-es3")) {
597                 forceES3 = true;
598             } else if(args[i].equals("-gl3")) {
599                 forceGL3 = true;
600             } else if(args[i].equals("-gl2")) {
601                 forceGL2 = true;
602             } else if(args[i].equals("-mappedBuffers")) {
603                 useMappedBuffers = true;
604             } else if(args[i].equals("-wait")) {
605                 waitForKey = true;
606             } else if(args[i].equals("-mouseInvisible")) {
607                 mouseVisible = false;
608             } else if(args[i].equals("-mouseConfine")) {
609                 mouseConfined = true;
610             } else if(args[i].equals("-pointerIcon")) {
611                 setPointerIcon = true;
612             } else if(args[i].equals("-showFPS")) {
613                 showFPS = true;
614             } else if(args[i].equals("-width")) {
615                 i++;
616                 w = MiscUtils.atoi(args[i], w);
617             } else if(args[i].equals("-height")) {
618                 i++;
619                 h = MiscUtils.atoi(args[i], h);
620             } else if(args[i].equals("-x")) {
621                 i++;
622                 x = MiscUtils.atoi(args[i], x);
623                 usePos = true;
624             } else if(args[i].equals("-y")) {
625                 i++;
626                 y = MiscUtils.atoi(args[i], y);
627                 usePos = true;
628             } else if(args[i].equals("-pixelScale")) {
629                 i++;
630                 final int pS = MiscUtils.atoi(args[i], reqSurfacePixelScale[0]);
631                 reqSurfacePixelScale[0] = pS;
632                 reqSurfacePixelScale[1] = pS;
633             } else if(args[i].equals("-rwidth")) {
634                 i++;
635                 rw = MiscUtils.atoi(args[i], rw);
636             } else if(args[i].equals("-rheight")) {
637                 i++;
638                 rh = MiscUtils.atoi(args[i], rh);
639             } else if(args[i].equals("-screen")) {
640                 i++;
641                 screenIdx = MiscUtils.atoi(args[i], 0);
642             } else if(args[i].equals("-loops")) {
643                 i++;
644                 loops = MiscUtils.atoi(args[i], 1);
645             } else if(args[i].equals("-loop-shutdown")) {
646                 loop_shutdown = true;
647             } else if(args[i].equals("-sysExit")) {
648                 i++;
649                 sysExit = SysExit.valueOf(args[i]);
650             } else if(args[i].equals("-manual")) {
651                 manualTest = true;
652             }
653         }
654         wsize = new Dimension(w, h);
655         if( 0 < rw && 0 < rh ) {
656             rwsize = new Dimension(rw, rh);
657         }
658
659         if(usePos) {
660             wpos = new Point(x, y);
661         }
662         System.err.println("position "+wpos);
663         System.err.println("size "+wsize);
664         System.err.println("resize "+rwsize);
665         System.err.println("screen "+screenIdx);
666         System.err.println("translucent "+(!opaque));
667         System.err.println("forceAlpha "+forceAlpha);
668         System.err.println("undecorated "+undecorated);
669         System.err.println("atop "+alwaysOnTop);
670         System.err.println("fullscreen "+fullscreen);
671         System.err.println("pmvDirect "+(!pmvUseBackingArray));
672         System.err.println("mouseVisible "+mouseVisible);
673         System.err.println("mouseConfined "+mouseConfined);
674         System.err.println("pointerIcon "+setPointerIcon);
675         System.err.println("loops "+loops);
676         System.err.println("loop shutdown "+loop_shutdown);
677         System.err.println("forceES2 "+forceES2);
678         System.err.println("forceES3 "+forceES3);
679         System.err.println("forceGL3 "+forceGL3);
680         System.err.println("forceGL2 "+forceGL2);
681         System.err.println("swapInterval "+swapInterval);
682         System.err.println("exclusiveContext "+exclusiveContext);
683         System.err.println("useAnimator "+useAnimator);
684         System.err.println("sysExitWithin "+sysExit);
685         System.err.println("mappedBuffers "+useMappedBuffers);
686
687         if(waitForKey) {
688             UITestCase.waitForKey("Start");
689         }
690         org.junit.runner.JUnitCore.main(TestGearsES2NEWT.class.getName());
691     }
692 }
http://JogAmp.org git info: FAQ, tutorial and man pages.