JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestScreenMode01bNEWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 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
29package com.jogamp.opengl.test.junit.newt.mm;
30
31import java.io.IOException;
32
33import com.jogamp.opengl.GLCapabilities;
34import com.jogamp.opengl.GLProfile;
35
36import org.junit.AfterClass;
37import org.junit.Assert;
38import org.junit.BeforeClass;
39import org.junit.Test;
40import org.junit.FixMethodOrder;
41import org.junit.runners.MethodSorters;
42
43import com.jogamp.newt.Display;
44import com.jogamp.newt.MonitorDevice;
45import com.jogamp.newt.NewtFactory;
46import com.jogamp.newt.Screen;
47import com.jogamp.newt.Window;
48import com.jogamp.newt.MonitorMode;
49import com.jogamp.newt.opengl.GLWindow;
50import com.jogamp.newt.util.MonitorModeUtil;
51import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
52import com.jogamp.opengl.test.junit.util.MiscUtils;
53import com.jogamp.opengl.test.junit.util.NewtTestUtil;
54import com.jogamp.opengl.test.junit.util.UITestCase;
55import com.jogamp.opengl.util.Animator;
56
57import java.util.List;
58
59import com.jogamp.nativewindow.util.Dimension;
60import com.jogamp.nativewindow.util.DimensionImmutable;
61import com.jogamp.nativewindow.util.Rectangle;
62import com.jogamp.nativewindow.util.RectangleImmutable;
63
64/**
65 * Mode change on separate monitors ..
66 */
67@FixMethodOrder(MethodSorters.NAME_ASCENDING)
68public class TestScreenMode01bNEWT extends UITestCase {
69 static GLProfile glp;
70 static int width, height;
71
72 static long waitTimeShort = 2000;
73 static long duration = 6000;
74
75 @BeforeClass
76 public static void initClass() {
77 setResetXRandRIfX11AfterClass();
78 width = 200;
79 height = 200;
80 glp = GLProfile.getDefault();
81 }
82
83 @AfterClass
84 public static void releaseClass() throws InterruptedException {
85 Thread.sleep(waitTimeShort);
86 }
87
88 static GLWindow createWindow(final Screen screen, final GLCapabilities caps, final String name, final int screenXPos, final int screenYPos, final int width, final int height) throws InterruptedException {
89 Assert.assertNotNull(caps);
90
91 final GLWindow window = GLWindow.create(screen, caps);
92 // Window window = NewtFactory.createWindow(screen, caps);
93 final int[] winPos = window.convertToWindowUnits(new int[] { screenXPos, screenYPos });
94 window.setTitle(name);
95 window.setPosition(winPos[0], winPos[1]);
96 window.setSize(width, height);
97 window.addGLEventListener(new GearsES2());
98 Assert.assertNotNull(window);
99 final long t0 = System.currentTimeMillis();
100 window.setVisible(true);
101 System.err.println("Time for visible/pos: "+(System.currentTimeMillis()-t0)+" ms");
102 return window;
103 }
104
105 static void destroyWindow(final Window window) throws InterruptedException {
106 if(null!=window) {
107 window.destroy();
108 Assert.assertTrue(NewtTestUtil.waitForRealized(window, false, null));
109 }
110 }
111
112 @Test
113 public void testScreenModeChangeSingleQ1() throws InterruptedException {
114 final Display display = NewtFactory.createDisplay(null); // local display
115 Assert.assertNotNull(display);
116 final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
117 Assert.assertNotNull(screen);
118 screen.addReference(); // trigger creation
119 try {
120 final RectangleImmutable monitorVp = screen.getMonitorDevices().get(0).getViewport();
121 testScreenModeChangeImpl(screen, monitorVp.getX(), monitorVp.getY());
122 } finally {
123 screen.removeReference();
124 Assert.assertTrue(NewtTestUtil.waitForRealized(screen, false, null));
125 }
126 }
127
128 @Test
129 public void testScreenModeChangeSingleQ2() throws InterruptedException {
130 final Display display = NewtFactory.createDisplay(null); // local display
131 Assert.assertNotNull(display);
132 final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
133 Assert.assertNotNull(screen);
134 screen.addReference(); // trigger creation
135 try {
136 if( 2 > screen.getMonitorDevices().size() ) {
137 System.err.println("Test Disabled (1): Monitor count < 2: "+screen);
138 return;
139 }
140 final RectangleImmutable monitorVp = screen.getMonitorDevices().get(1).getViewport();
141 testScreenModeChangeImpl(screen, monitorVp.getX(), monitorVp.getY());
142 } finally {
143 screen.removeReference();
144 Assert.assertTrue(NewtTestUtil.waitForRealized(screen, false, null));
145 }
146 }
147
148 void testScreenModeChangeImpl(final Screen screen, final int screenXPos, final int screenYPos) throws InterruptedException {
149 Thread.sleep(waitTimeShort);
150
151 final GLCapabilities caps = new GLCapabilities(glp);
152 Assert.assertNotNull(caps);
153 final Display display = screen.getDisplay();
154 System.err.println("Test.0: Window screen: "+screen);
155
156 System.err.println("Test.0: Window bounds (pre): screenPos "+screenXPos+"/"+screenYPos+" [pixels], windowSize "+width+"x"+height+" [wu] within "+screen.getViewport()+" [pixels]");
157
158 final GLWindow window0 = createWindow(screen, caps, "win0", screenXPos, screenYPos, width, height);
159 Assert.assertNotNull(window0);
160 Rectangle window0WindowBounds = window0.getBounds();
161 DimensionImmutable window0SurfaceSize = new Dimension(window0.getSurfaceWidth(), window0.getSurfaceHeight());
162 System.err.println("Test.0: Window bounds : "+window0WindowBounds+" [wu] within "+screen.getViewportInWindowUnits()+" [wu]");
163 System.err.println("Test.0: Window size : "+window0SurfaceSize+" [pixels]");
164
165 final Animator anim = new Animator(window0);
166 anim.start();
167
168 final List<MonitorMode> allMonitorModes = screen.getMonitorModes();
169 Assert.assertTrue(allMonitorModes.size()>0);
170 if(allMonitorModes.size()==1) {
171 // no support ..
172 System.err.println("Your platform has no MonitorMode change support (all), sorry");
173 destroyWindow(window0);
174 return;
175 }
176
177 final MonitorDevice monitor = window0.getMainMonitor();
178 System.err.println("Test.0: Window monitor: "+monitor);
179
180 List<MonitorMode> monitorModes = monitor.getSupportedModes();
181 Assert.assertTrue(monitorModes.size()>0);
182 if(monitorModes.size()==1) {
183 // no support ..
184 System.err.println("Your platform has no MonitorMode change support (monitor), sorry");
185 destroyWindow(window0);
186 return;
187 }
188 Assert.assertTrue(allMonitorModes.containsAll(monitorModes));
189
190 MonitorMode mmCurrent = monitor.getCurrentMode();
191 Assert.assertNotNull(mmCurrent);
192 final MonitorMode mmOrig = monitor.getOriginalMode();
193 Assert.assertNotNull(mmOrig);
194 System.err.println("[0] orig : "+mmOrig);
195 System.err.println("[0] current: "+mmCurrent);
196 Assert.assertEquals(mmCurrent, mmOrig);
197
198 monitorModes = MonitorModeUtil.filterByFlags(monitorModes, 0); // no interlace, double-scan etc
199 Assert.assertNotNull(monitorModes);
200 Assert.assertTrue(monitorModes.size()>0);
201 monitorModes = MonitorModeUtil.filterByRotation(monitorModes, 0);
202 Assert.assertNotNull(monitorModes);
203 Assert.assertTrue(monitorModes.size()>0);
204 monitorModes = MonitorModeUtil.filterByResolution(monitorModes, new Dimension(801, 601));
205 Assert.assertNotNull(monitorModes);
206 Assert.assertTrue(monitorModes.size()>0);
207 monitorModes = MonitorModeUtil.filterByRate(monitorModes, mmOrig.getRefreshRate());
208 Assert.assertNotNull(monitorModes);
209 Assert.assertTrue(monitorModes.size()>0);
210
211 monitorModes = MonitorModeUtil.getHighestAvailableBpp(monitorModes);
212 Assert.assertNotNull(monitorModes);
213 Assert.assertTrue(monitorModes.size()>0);
214
215 // set mode
216 {
217 final MonitorMode mm = monitorModes.get(0);
218 System.err.println("[0] set current: "+mm);
219 final boolean smOk = monitor.setCurrentMode(mm);
220 mmCurrent = monitor.getCurrentMode();
221 System.err.println("[0] has current: "+mmCurrent+", changeOK "+smOk);
222 Assert.assertTrue(monitor.isModeChangedByUs());
223 Assert.assertEquals(mm, mmCurrent);
224 Assert.assertNotSame(mmOrig, mmCurrent);
225 Assert.assertEquals(mmCurrent, monitor.queryCurrentMode());
226 Assert.assertTrue(smOk);
227 }
228
229 window0WindowBounds = window0.getBounds();
230 window0SurfaceSize = new Dimension(window0.getSurfaceWidth(), window0.getSurfaceHeight());
231 System.err.println("Test.1: Screen : "+screen);
232 System.err.println("Test.1: Window bounds : "+window0WindowBounds+" [wu] within "+screen.getViewportInWindowUnits()+" [wu]");
233 System.err.println("Test.1: Window size : "+window0SurfaceSize+" [pixels]");
234 System.err.println("Test.1: Screen viewport : "+screen.getViewport()+" [pixels]");
235 System.err.println("Test.1: Monitor viewport : "+monitor.getViewport()+" [pixels], "+monitor.getViewportInWindowUnits()+" [wu]");
236 System.err.println("Test.1: Window main-mon : "+window0.getMainMonitor());
237
238 Thread.sleep(duration);
239
240 Assert.assertEquals(true,display.isNativeValid());
241 Assert.assertEquals(true,screen.isNativeValid());
242 Assert.assertEquals(true,window0.isNativeValid());
243 Assert.assertEquals(true,window0.isVisible());
244
245 // manual restore!
246 {
247 System.err.println("[1] set orig: "+mmOrig);
248 final boolean smOk = monitor.setCurrentMode(mmOrig);
249 mmCurrent = monitor.getCurrentMode();
250 System.err.println("[1] has orig?: "+mmCurrent+", changeOK "+smOk);
251 Assert.assertFalse(monitor.isModeChangedByUs());
252 Assert.assertEquals(mmOrig, mmCurrent);
253 Assert.assertTrue(smOk);
254 }
255
256 window0WindowBounds = window0.getBounds();
257 window0SurfaceSize = new Dimension(window0.getSurfaceWidth(), window0.getSurfaceHeight());
258 System.err.println("Test.2: Screen : "+screen);
259 System.err.println("Test.2: Window bounds : "+window0WindowBounds+" [wu] within "+screen.getViewportInWindowUnits()+" [wu]");
260 System.err.println("Test.2: Window size : "+window0SurfaceSize+" [pixels]");
261 System.err.println("Test.2: Screen viewport : "+screen.getViewport()+" [pixels]");
262 System.err.println("Test.2: Monitor viewport : "+monitor.getViewport()+" [pixels], "+monitor.getViewportInWindowUnits()+" [wu]");
263 System.err.println("Test.2: Window main-mon : "+window0.getMainMonitor());
264
265 Thread.sleep(duration);
266 anim.stop();
267 destroyWindow(window0);
268
269 Assert.assertEquals(false,window0.isVisible());
270 Assert.assertEquals(false,window0.isNativeValid());
271 Assert.assertEquals(true,display.isNativeValid());
272 Assert.assertEquals(true,screen.isNativeValid());
273 }
274
275 public static void main(final String args[]) throws IOException {
276 for(int i=0; i<args.length; i++) {
277 if(args[i].equals("-time")) {
278 i++;
279 duration = MiscUtils.atol(args[i], duration);
280 }
281 }
282 final String tstname = TestScreenMode01bNEWT.class.getName();
283 org.junit.runner.JUnitCore.main(tstname);
284 }
285}
abstract boolean isNativeValid()
final List< MonitorMode > getSupportedModes()
Returns a list of immutable MonitorModes supported by this monitor.
static Display createDisplay(final String name)
Create a Display entity.
static Screen createScreen(final Display display, final int index)
Create a Screen entity.
A screen may span multiple MonitorDevices representing their combined virtual size.
Definition: Screen.java:58
abstract int addReference()
See Display#addReference().
abstract int removeReference()
See Display#removeReference().
abstract List< MonitorDevice > getMonitorDevices()
Return a list of available MonitorDevices.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:466
final void setPosition(final int x, final int y)
Sets the location of the window's client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:525
final void setTitle(final String title)
Definition: GLWindow.java:297
final int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:461
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
Definition: GLWindow.java:625
final int[] convertToWindowUnits(final int[] pixelUnitsAndResult)
Converts the given pixel units into window units in place.
Definition: GLWindow.java:476
final MonitorDevice getMainMonitor()
Returns the MonitorDevice with the highest viewport coverage of this window.
Definition: GLWindow.java:292
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
final Rectangle getBounds()
Returns a newly created Rectangle containing window origin, getX() & getY(), and size,...
Definition: GLWindow.java:456
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Definition: GLWindow.java:169
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
Definition: GLProfile.java:739
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
Immutable Dimension Interface, consisting of it's read only components:
Immutable Rectangle interface, with its position on the top-left.
int getX()
x-position, left of rectangle.
int getY()
y-position, top of rectangle.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.