JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestScreenMode00cNEWT.java
Go to the documentation of this file.
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
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;
55
56import java.util.List;
57
58import com.jogamp.nativewindow.NativeWindowFactory;
59import com.jogamp.nativewindow.util.Dimension;
60import com.jogamp.nativewindow.util.DimensionImmutable;
61import com.jogamp.nativewindow.util.Rectangle;
62
63/**
64 * Tests X11 XRandR MonitorMode reset via {@link UITestCase#resetXRandRIfX11()}.
65 */
66@FixMethodOrder(MethodSorters.NAME_ASCENDING)
67public class TestScreenMode00cNEWT extends UITestCase {
68 static boolean manualTest = false;
69 static GLProfile glp;
70 static int width, height;
71
72 static final int waitTimeShort = 2000;
73 static long duration = 4000;
74
75 static int mm_width = 800;
76 static int mm_height = 600;
77
78 @BeforeClass
79 public static void initClass() {
80 setResetXRandRIfX11AfterClass();
83 setTestSupported(false);
84 return;
85 }
86 width = 100;
87 height = 100;
88 glp = GLProfile.getDefault();
89 }
90
91 @AfterClass
92 public static void releaseClass() throws InterruptedException {
93 Thread.sleep(waitTimeShort);
94 }
95
96 static Window createWindow(final Screen screen, final GLCapabilities caps, final String name, final int x, final int y, final int width, final int height) {
97 Assert.assertNotNull(caps);
98
99 final GLWindow window = GLWindow.create(screen, caps);
100 // Window window = NewtFactory.createWindow(screen, caps);
101 window.setTitle(name);
102 window.setPosition(x, y);
103 window.setSize(width, height);
104 window.addGLEventListener(new GearsES2());
105 Assert.assertNotNull(window);
106 window.setVisible(true);
107 return window;
108 }
109
110 static void destroyWindow(final Window window) throws InterruptedException {
111 if(null!=window) {
112 window.destroy();
113 Assert.assertTrue(NewtTestUtil.waitForRealized(window, false, null));
114 }
115 }
116
117 @Test
118 public void testScreenModeChange01() throws InterruptedException {
119 Thread.sleep(waitTimeShort);
120
121 final GLCapabilities caps = new GLCapabilities(glp);
122 Assert.assertNotNull(caps);
123 final Display display = NewtFactory.createDisplay(null); // local display
124 Assert.assertNotNull(display);
125 final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
126 Assert.assertNotNull(screen);
127 final Window window0 = createWindow(screen, caps, "win0", 0, 0, width, height);
128 Assert.assertNotNull(window0);
129
130 Rectangle window0WindowBounds = window0.getBounds();
131 DimensionImmutable window0SurfaceSize = new Dimension(window0.getSurfaceWidth(), window0.getSurfaceHeight());
132 System.err.println("Test.0: Window bounds : "+window0WindowBounds+" [wu] within "+screen.getViewportInWindowUnits()+" [wu]");
133 System.err.println("Test.0: Window size : "+window0SurfaceSize+" [pixels]");
134 System.err.println("Test.0: Screen viewport : "+screen.getViewport()+" [pixels]");
135
136 final List<MonitorMode> allMonitorModes = screen.getMonitorModes();
137 Assert.assertTrue(allMonitorModes.size()>0);
138 if(allMonitorModes.size()==1) {
139 // no support ..
140 System.err.println("Your platform has no MonitorMode change support (all), sorry");
141 destroyWindow(window0);
142 return;
143 }
144
145 final MonitorDevice monitor = screen.getMonitorDevices().get(0);
146 List<MonitorMode> monitorModes = monitor.getSupportedModes();
147 Assert.assertTrue(monitorModes.size()>0);
148 if(monitorModes.size()==1) {
149 // no support ..
150 System.err.println("Your platform has no MonitorMode change support (monitor), sorry");
151 destroyWindow(window0);
152 return;
153 }
154 Assert.assertTrue(allMonitorModes.containsAll(monitorModes));
155
156 final MonitorMode mmSet0 = monitor.queryCurrentMode();
157 Assert.assertNotNull(mmSet0);
158 final MonitorMode mmOrig = monitor.getOriginalMode();
159 Assert.assertNotNull(mmOrig);
160 System.err.println("[0] orig : "+mmOrig);
161 System.err.println("[0] current: "+mmSet0);
162 Assert.assertEquals(mmSet0, mmOrig);
163
164
165 monitorModes = MonitorModeUtil.filterByFlags(monitorModes, 0); // no interlace, double-scan etc
166 Assert.assertNotNull(monitorModes);
167 Assert.assertTrue(monitorModes.size()>0);
168 monitorModes = MonitorModeUtil.filterByRotation(monitorModes, 0);
169 Assert.assertNotNull(monitorModes);
170 Assert.assertTrue(monitorModes.size()>0);
171 monitorModes = MonitorModeUtil.filterByResolution(monitorModes, new Dimension(mm_width+1, mm_height+1));
172 Assert.assertNotNull(monitorModes);
173 Assert.assertTrue(monitorModes.size()>0);
174 monitorModes = MonitorModeUtil.filterByRate(monitorModes, mmOrig.getRefreshRate());
175 Assert.assertNotNull(monitorModes);
176 Assert.assertTrue(monitorModes.size()>0);
177
178 monitorModes = MonitorModeUtil.getHighestAvailableBpp(monitorModes);
179 Assert.assertNotNull(monitorModes);
180 Assert.assertTrue(monitorModes.size()>0);
181
182 // set mode
183 {
184 final MonitorMode mm = monitorModes.get(0);
185 System.err.println("[1] set current: "+mm);
186 final boolean smOk = monitor.setCurrentMode(mm);
187 final MonitorMode mmCachedCurrent = monitor.getCurrentMode();
188 final MonitorMode mmQueriedCurrent = monitor.queryCurrentMode();
189 final boolean mmCurrentEquals = mmQueriedCurrent.equals(mmCachedCurrent);
190 System.err.println("[1] changeOK : "+smOk);
191 System.err.println("[1] has current cached : "+mmCachedCurrent);
192 System.err.println("[1] has current queried : "+mmQueriedCurrent+", equal "+mmCurrentEquals);
193 window0WindowBounds = window0.getBounds();
194 window0SurfaceSize = new Dimension(window0.getSurfaceWidth(), window0.getSurfaceHeight());
195 System.err.println("Test.1: Window bounds : "+window0WindowBounds+" [wu] within "+screen.getViewportInWindowUnits()+" [wu]");
196 System.err.println("Test.1: Window size : "+window0SurfaceSize+" [pixels]");
197 System.err.println("Test.1: Screen viewport : "+screen.getViewport()+" [pixels]");
198
199 Assert.assertTrue(monitor.isModeChangedByUs());
200 Assert.assertEquals(mm, mmCachedCurrent);
201 Assert.assertNotSame(mmOrig, mmCachedCurrent);
202 Assert.assertEquals(mmCachedCurrent, mmQueriedCurrent);
203 Assert.assertTrue(smOk);
204 }
205
206 Thread.sleep(duration);
207
208 Assert.assertEquals(true,display.isNativeValid());
209 Assert.assertEquals(true,screen.isNativeValid());
210 Assert.assertEquals(true,window0.isNativeValid());
211 Assert.assertEquals(true,window0.isVisible());
212
213 // WARNING: See note in 'UITestCase.resetXRandRIfX11();'
214 final int xrandrErrorCode;
215 if( 0 == ( xrandrErrorCode = UITestCase.resetXRandRIfX11() ) ) {
216 System.err.println("XRandR Reset :"+monitor.queryCurrentMode());
217 } else {
218 System.err.println("XRandR Reset : Failed w/ errorCode "+xrandrErrorCode+", fall back to manual reset");
219 final boolean smOk = monitor.setCurrentMode(mmOrig);
220 System.err.println("[X] changeOK : "+smOk);
221 }
222 Thread.sleep(duration);
223 validateScreenModeReset0(mmOrig);
224
225 destroyWindow(window0);
226
227 Thread.sleep(waitTimeShort);
228 validateScreenModeReset(mmOrig);
229 }
230
231 void validateScreenModeReset0(final MonitorMode mmOrig) {
232 final Display display = NewtFactory.createDisplay(null); // local display
233 Assert.assertNotNull(display);
234 final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
235 Assert.assertNotNull(screen);
236 screen.addReference();
237 Assert.assertEquals(true,display.isNativeValid());
238 Assert.assertEquals(true,screen.isNativeValid());
239
240 final MonitorDevice monitor = screen.getMonitorDevices().get(0);
241 Assert.assertEquals(mmOrig, monitor.queryCurrentMode());
242
243 screen.removeReference();
244 }
245 void validateScreenModeReset(final MonitorMode mmOrig) {
246 final Display display = NewtFactory.createDisplay(null); // local display
247 Assert.assertNotNull(display);
248 final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
249 Assert.assertNotNull(screen);
250 Assert.assertEquals(false,display.isNativeValid());
251 Assert.assertEquals(false,screen.isNativeValid());
252 screen.addReference();
253 Assert.assertEquals(true,display.isNativeValid());
254 Assert.assertEquals(true,screen.isNativeValid());
255
256 final MonitorDevice monitor = screen.getMonitorDevices().get(0);
257 Assert.assertEquals(mmOrig, monitor.getCurrentMode());
258
259 screen.removeReference();
260 Assert.assertEquals(false,display.isNativeValid());
261 Assert.assertEquals(false,screen.isNativeValid());
262 }
263
264 public static void main(final String args[]) throws IOException {
265 manualTest = true;
266 for(int i=0; i<args.length; i++) {
267 if(args[i].equals("-time")) {
268 i++;
269 duration = MiscUtils.atol(args[i], duration);
270 } else if(args[i].equals("-mwidth")) {
271 i++;
272 mm_width = MiscUtils.atoi(args[i], mm_width);
273 } else if(args[i].equals("-mheight")) {
274 i++;
275 mm_height = MiscUtils.atoi(args[i], mm_height);
276 }
277 }
278 System.err.println("Desired mode w/ resolution "+mm_width+"x"+mm_height);
279 final String tstname = TestScreenMode00cNEWT.class.getName();
280 org.junit.runner.JUnitCore.main(tstname);
281 }
282}
Provides a pluggable mechanism for arbitrary window toolkits to adapt their components to the NativeW...
static final String TYPE_X11
X11 type, as retrieved with getNativeWindowType(boolean).
static synchronized void initSingleton()
Static one time initialization of this factory.
static String getNativeWindowType(final boolean useCustom)
abstract boolean isNativeValid()
Visual output device, i.e.
final boolean isModeChangedByUs()
Returns true</true> if the MonitorMode has been changed programmatic via this API only,...
final MonitorMode getCurrentMode()
Returns the cached current MonitorMode w/o native query.
abstract MonitorMode queryCurrentMode()
Returns the current MonitorMode resulting from a native query.
abstract boolean setCurrentMode(MonitorMode mode)
Set the current com.jogamp.newt.MonitorMode.
final List< MonitorMode > getSupportedModes()
Returns a list of immutable MonitorModes supported by this monitor.
final MonitorMode getOriginalMode()
Returns the immutable original com.jogamp.newt.MonitorMode, as used at NEWT initialization.
Immutable MonitorMode Class, consisting of it's read only components:
final boolean equals(final Object obj)
Tests equality of two MonitorMode objects by evaluating equality of it's components:
final float getRefreshRate()
Returns the vertical refresh rate.
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 RectangleImmutable getViewport()
See Coordinate System.
abstract RectangleImmutable getViewportInWindowUnits()
See Coordinate System.
abstract List< MonitorMode > getMonitorModes()
Return a list of all MonitorModes for all MonitorDevices.
abstract boolean isNativeValid()
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 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 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 void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
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
Convenient com.jogamp.newt.MonitorMode utility methods, filters etc.
static List< MonitorMode > filterByFlags(final List< MonitorMode > monitorModes, final int flags)
static List< MonitorMode > filterByResolution(final List< MonitorMode > monitorModes, final DimensionImmutable resolution)
static List< MonitorMode > getHighestAvailableBpp(final List< MonitorMode > monitorModes)
static List< MonitorMode > filterByRotation(final List< MonitorMode > monitorModes, final int rotation)
static List< MonitorMode > filterByRate(final List< MonitorMode > monitorModes, final float refreshRate)
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
Tests X11 XRandR MonitorMode reset via UITestCase#resetXRandRIfX11().
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
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)
static int resetXRandRIfX11()
Iterates through all outputs and sets the preferred mode and normal rotation using RandR 1....
Definition: UITestCase.java:80
int getSurfaceWidth()
Returns the width of the client area excluding insets (window decorations) in pixel units.
int getSurfaceHeight()
Returns the height of the client area excluding insets (window decorations) in pixel units.
Rectangle getBounds()
Returns a newly created Rectangle containing window origin, getX() & getY(), and size,...
Immutable Dimension Interface, consisting of it's read only components:
Specifying NEWT's Window functionality:
Definition: Window.java:115
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.