JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestScreenMode01aNEWT.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.util.Dimension;
59import com.jogamp.nativewindow.util.DimensionImmutable;
60import com.jogamp.nativewindow.util.Rectangle;
61
62/**
63 * <p>
64 * Tests MonitorMode reset, by destroying the last Screen (reference),
65 * i.e. the original MonitorMode should get reinstated!
66 * </p>
67 * <p>
68 * Documents remedy B) for NV RANDR/GL bug
69 * </p>
70 *
71 * @see TestScreenMode01dNEWT#cleanupGL()
72 */
73@FixMethodOrder(MethodSorters.NAME_ASCENDING)
74public class TestScreenMode01aNEWT extends UITestCase {
75 static GLProfile glp;
76 static int width, height;
77
78 static long waitTimeShort = 2000;
79 static long duration = 4000;
80
81 static int mm_width = 800;
82 static int mm_height = 600;
83
84 @BeforeClass
85 public static void initClass() {
86 setResetXRandRIfX11AfterClass();
87 width = 100;
88 height = 100;
89 glp = GLProfile.getDefault();
90 }
91
92 @AfterClass
93 public static void releaseClass() throws InterruptedException {
94 Thread.sleep(waitTimeShort);
95 }
96
97 static Window createWindow(final Screen screen, final GLCapabilities caps, final String name, final int x, final int y, final int width, final int height) {
98 Assert.assertNotNull(caps);
99
100 final GLWindow window = GLWindow.create(screen, caps);
101 // Window window = NewtFactory.createWindow(screen, caps);
102 window.setTitle(name);
103 window.setPosition(x, y);
104 window.setSize(width, height);
105 window.addGLEventListener(new GearsES2());
106 Assert.assertNotNull(window);
107 window.setVisible(true);
108 return window;
109 }
110
111 static void destroyWindow(final Window window) throws InterruptedException {
112 if(null!=window) {
113 window.destroy();
114 Assert.assertTrue(NewtTestUtil.waitForRealized(window, false, null));
115 }
116 }
117
118 @Test
119 public void testScreenModeChange01() throws InterruptedException {
120 Thread.sleep(waitTimeShort);
121
122 final GLCapabilities caps = new GLCapabilities(glp);
123 Assert.assertNotNull(caps);
124 final Display display = NewtFactory.createDisplay(null); // local display
125 Assert.assertNotNull(display);
126 final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
127 Assert.assertNotNull(screen);
128 final Window window0 = createWindow(screen, caps, "win0", 0, 0, width, height);
129 Assert.assertNotNull(window0);
130
131 final MonitorDevice monitor = screen.getMonitorDevices().get(0);
132
133 Rectangle window0WindowBounds = window0.getBounds();
134 DimensionImmutable window0SurfaceSize = new Dimension(window0.getSurfaceWidth(), window0.getSurfaceHeight());
135 System.err.println("Test.0: Window bounds : "+window0WindowBounds+" [wu] within "+screen.getViewportInWindowUnits()+" [wu]");
136 System.err.println("Test.0: Window size : "+window0SurfaceSize+" [pixels]");
137 System.err.println("Test.0: Screen viewport : "+screen.getViewport()+" [pixels], "+screen.getViewportInWindowUnits()+" [wu]");
138 System.err.println("Test.0: Monitor viewport : "+monitor.getViewport()+" [pixels], "+monitor.getViewportInWindowUnits()+" [wu]");
139
140 final List<MonitorMode> allMonitorModes = screen.getMonitorModes();
141 Assert.assertTrue(allMonitorModes.size()>0);
142 if(allMonitorModes.size()==1) {
143 // no support ..
144 System.err.println("Your platform has no MonitorMode change support (all), sorry");
145 destroyWindow(window0);
146 return;
147 }
148
149 List<MonitorMode> monitorModes = monitor.getSupportedModes();
150 Assert.assertTrue(monitorModes.size()>0);
151 if(monitorModes.size()==1) {
152 // no support ..
153 System.err.println("Your platform has no MonitorMode change support (monitor), sorry");
154 destroyWindow(window0);
155 return;
156 }
157 Assert.assertTrue(allMonitorModes.containsAll(monitorModes));
158
159 final MonitorMode mmSet0 = monitor.queryCurrentMode();
160 Assert.assertNotNull(mmSet0);
161 final MonitorMode mmOrig = monitor.getOriginalMode();
162 Assert.assertNotNull(mmOrig);
163 System.err.println("[0] orig : "+mmOrig);
164 System.err.println("[0] current: "+mmSet0);
165 Assert.assertEquals(mmSet0, mmOrig);
166
167
168 monitorModes = MonitorModeUtil.filterByFlags(monitorModes, 0); // no interlace, double-scan etc
169 Assert.assertNotNull(monitorModes);
170 Assert.assertTrue(monitorModes.size()>0);
171 monitorModes = MonitorModeUtil.filterByRotation(monitorModes, 0);
172 Assert.assertNotNull(monitorModes);
173 Assert.assertTrue(monitorModes.size()>0);
174 monitorModes = MonitorModeUtil.filterByResolution(monitorModes, new Dimension(mm_width+1, mm_height+1));
175 Assert.assertNotNull(monitorModes);
176 Assert.assertTrue(monitorModes.size()>0);
177 monitorModes = MonitorModeUtil.filterByRate(monitorModes, mmOrig.getRefreshRate());
178 Assert.assertNotNull(monitorModes);
179 Assert.assertTrue(monitorModes.size()>0);
180
181 monitorModes = MonitorModeUtil.getHighestAvailableBpp(monitorModes);
182 Assert.assertNotNull(monitorModes);
183 Assert.assertTrue(monitorModes.size()>0);
184
185 // set mode
186 {
187 final MonitorMode mm = monitorModes.get(0);
188 System.err.println("[1] set current: "+mm);
189 final boolean smOk = monitor.setCurrentMode(mm);
190 final MonitorMode mmCachedCurrent = monitor.getCurrentMode();
191 final MonitorMode mmQueriedCurrent = monitor.queryCurrentMode();
192 final boolean mmCurrentEquals = mmQueriedCurrent.equals(mmCachedCurrent);
193 System.err.println("[1] changeOK : "+smOk);
194 System.err.println("[1] has current cached : "+mmCachedCurrent);
195 System.err.println("[1] has current queried : "+mmQueriedCurrent+", equal "+mmCurrentEquals);
196 window0WindowBounds = window0.getBounds();
197 window0SurfaceSize = new Dimension(window0.getSurfaceWidth(), window0.getSurfaceHeight());
198 System.err.println("Test.1: Window bounds : "+window0WindowBounds+" [wu] within "+screen.getViewportInWindowUnits()+" [wu]");
199 System.err.println("Test.1: Window size : "+window0SurfaceSize+" [pixels]");
200 System.err.println("Test.1: Screen viewport : "+screen.getViewport()+" [pixels], "+screen.getViewportInWindowUnits()+" [wu]");
201 System.err.println("Test.1: Monitor viewport : "+monitor.getViewport()+" [pixels], "+monitor.getViewportInWindowUnits()+" [wu]");
202
203 Assert.assertTrue(monitor.isModeChangedByUs());
204 Assert.assertEquals(mm, mmCachedCurrent);
205 Assert.assertNotSame(mmOrig, mmCachedCurrent);
206 Assert.assertEquals(mmCachedCurrent, monitor.queryCurrentMode());
207 Assert.assertTrue(smOk);
208 }
209
210 Thread.sleep(duration);
211
212 Assert.assertEquals(true,display.isNativeValid());
213 Assert.assertEquals(true,screen.isNativeValid());
214 Assert.assertEquals(true,window0.isNativeValid());
215 Assert.assertEquals(true,window0.isVisible());
216
217 // Auto reset by destruction!
218 destroyWindow(window0);
219
220 Assert.assertEquals(false,window0.isVisible());
221 Assert.assertEquals(false,window0.isNativeValid());
222 Assert.assertTrue(NewtTestUtil.waitForRealized(screen, false, null));
223 Assert.assertEquals(false,screen.isNativeValid());
224 Assert.assertEquals(false,display.isNativeValid());
225
226 Thread.sleep(waitTimeShort);
227
228 validateScreenModeReset(mmOrig, 0);
229 }
230
231 void validateScreenModeReset(final MonitorMode mmOrig, final int mmIdx) {
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 Assert.assertEquals(false,display.isNativeValid());
237 Assert.assertEquals(false,screen.isNativeValid());
238 screen.addReference();
239 Assert.assertEquals(true,display.isNativeValid());
240 Assert.assertEquals(true,screen.isNativeValid());
241
242 final MonitorDevice monitor = screen.getMonitorDevices().get(0);
243 Assert.assertEquals(mmOrig, monitor.getCurrentMode());
244
245 screen.removeReference();
246 Assert.assertEquals(false,display.isNativeValid());
247 Assert.assertEquals(false,screen.isNativeValid());
248 }
249
250 public static void main(final String args[]) throws IOException {
251 for(int i=0; i<args.length; i++) {
252 if(args[i].equals("-time")) {
253 i++;
254 duration = MiscUtils.atol(args[i], duration);
255 } else if(args[i].equals("-mwidth")) {
256 i++;
257 mm_width = MiscUtils.atoi(args[i], mm_width);
258 } else if(args[i].equals("-mheight")) {
259 i++;
260 mm_height = MiscUtils.atoi(args[i], mm_height);
261 }
262 }
263 System.err.println("Desired mode w/ resolution "+mm_width+"x"+mm_height);
264 final String tstname = TestScreenMode01aNEWT.class.getName();
265 org.junit.runner.JUnitCore.main(tstname);
266 }
267}
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.
final RectangleImmutable getViewportInWindowUnits()
Returns the current rectangular portion of the rotated virtual Screen size in window units represente...
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.
final RectangleImmutable getViewport()
Returns the current rectangular portion of the rotated virtual Screen size in pixel units represented...
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
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)
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.