JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestScreenMode01dNEWT.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;
32import com.jogamp.opengl.GLCapabilities;
33import com.jogamp.opengl.GLProfile;
34
35import com.jogamp.opengl.util.Animator;
36
37import org.junit.AfterClass;
38import org.junit.Assert;
39import org.junit.BeforeClass;
40import org.junit.Test;
41import org.junit.FixMethodOrder;
42import org.junit.runners.MethodSorters;
43
44import com.jogamp.newt.Display;
45import com.jogamp.newt.MonitorDevice;
46import com.jogamp.newt.NewtFactory;
47import com.jogamp.newt.Screen;
48import com.jogamp.newt.Window;
49import com.jogamp.newt.MonitorMode;
50import com.jogamp.newt.opengl.GLWindow;
51import com.jogamp.newt.util.MonitorModeUtil;
52import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
53import com.jogamp.opengl.test.junit.util.NewtTestUtil;
54import com.jogamp.opengl.test.junit.util.UITestCase;
55
56import java.util.List;
57import com.jogamp.nativewindow.util.Dimension;
58import com.jogamp.nativewindow.util.RectangleImmutable;
59
60/**
61 * Demonstrates fullscreen without MonitorMode change
62 * and fullscreen before and after MonitorMode change.
63 * <p>
64 * Also tests MonitorMode reset, by destroying the last Screen (reference),
65 * i.e. the original MonitorMode should get reinstated!
66 * </p>
67 * <p>
68 * Also documents NV RANDR/GL bug, see {@link TestScreenMode01dNEWT#cleanupGL()}.</p>
69 */
70@FixMethodOrder(MethodSorters.NAME_ASCENDING)
71public class TestScreenMode01dNEWT extends UITestCase {
72 static GLProfile glp;
73 static int width, height;
74
75 static int waitTimeShort = 2000; // 2 sec
76 static int waitTimeLong = 8000; // 8 sec
77
78 @BeforeClass
79 public static void initClass() {
80 setResetXRandRIfX11AfterClass();
81 width = 640;
82 height = 480;
83 glp = GLProfile.getDefault();
84 }
85
86 @AfterClass
87 public static void releaseClass() throws InterruptedException {
88 Thread.sleep(waitTimeShort);
89 }
90
91 /**
92 * Following configurations results in a SIGSEGV:
93 * <pre>
94 * Ubuntu 11.04 (natty), NV GTX 460, driver [280.10* - 285.03]
95 * </pre>
96 *
97 * Situation:
98 * <pre>
99 * 1 - Create Screen, GLWindow (w/ context)
100 * 2 - ScreenMode change
101 * 3 - Destroy GLWindow (w/ context), Screen
102 * 4 - Create Screen, GLWindow (w/ context) (*)
103 * </pre>
104 *
105 * Step 4 causes the exception within 1st 'glXMakeContextCurrent(..)' call
106 * on the the created GL context.
107 *
108 * Remedy:
109 * <pre>
110 * A) Releasing all resources before step 4 .. works.
111 * B) Holding the native Display/Screen in NEWT also works (ie screen.addReference()).
112 * </pre>
113 *
114 * Hence there must be some correlations with the screen randr mode
115 * and some of the glcontext/gldrawables.
116 *
117 * <pre>
118 * Remedy A) is demonstrated here
119 * Remedy B) is shown in {@link TestScreenMode01bNEWT}
120 * </pre>
121 */
122 private void cleanupGL() throws InterruptedException {
123 System.err.println("*** cleanupGL.shutdown");
125 System.err.println("*** cleanupGL.initSingleton");
127 System.err.println("*** cleanupGL.DONE");
128 }
129
130 static GLWindow createWindow(final Screen screen, final GLCapabilities caps, final int width, final int height, final boolean onscreen, final boolean undecorated) {
131 Assert.assertNotNull(caps);
132 caps.setOnscreen(onscreen);
133
134 final GLWindow window = GLWindow.create(screen, caps);
135 window.setSize(width, height);
136 window.addGLEventListener(new GearsES2());
137 Assert.assertNotNull(window);
138 window.setVisible(true);
139 return window;
140 }
141
142 static void destroyWindow(final Window window) throws InterruptedException {
143 if(null!=window) {
144 window.destroy();
145 Assert.assertTrue(NewtTestUtil.waitForRealized(window, false, null));
146 }
147 }
148
149 @Test
150 public void test01FullscreenChange01() throws InterruptedException {
151 Thread.sleep(waitTimeShort);
152 final GLCapabilities caps = new GLCapabilities(glp);
153 Assert.assertNotNull(caps);
154 final Display display = NewtFactory.createDisplay(null); // local display
155 Assert.assertNotNull(display);
156 final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
157 Assert.assertNotNull(screen);
158
159 final GLWindow window = createWindow(screen, caps, width, height, true /* onscreen */, false /* undecorated */);
160 final Animator animator = new Animator(window);
161 animator.start();
162
163 final MonitorDevice monitor = window.getMainMonitor();
164
165 Assert.assertEquals(false, window.isFullscreen());
166 Assert.assertEquals(width, window.getWidth());
167 Assert.assertEquals(height, window.getHeight());
168
169 window.setFullscreen(true);
170 Assert.assertEquals(true, window.isFullscreen());
171 Assert.assertEquals(monitor.getViewport().getWidth(), window.getSurfaceWidth());
172 Assert.assertEquals(monitor.getViewport().getHeight(), window.getSurfaceHeight());
173
174 Thread.sleep(waitTimeShort);
175
176 window.setFullscreen(false);
177 Assert.assertEquals(false, window.isFullscreen());
178 Assert.assertEquals(width, window.getWidth());
179 Assert.assertEquals(height, window.getHeight());
180
181 Thread.sleep(waitTimeShort);
182
183 animator.stop();
184 Assert.assertEquals(false, animator.isAnimating());
185 Assert.assertEquals(false, animator.isStarted());
186
187 destroyWindow(window);
188
189 Assert.assertEquals(false,window.isVisible());
190 Assert.assertEquals(false,window.isRealized());
191 Assert.assertEquals(false,window.isNativeValid());
192 Assert.assertTrue(NewtTestUtil.waitForRealized(screen, false, null));
193 Assert.assertEquals(false,screen.isNativeValid());
194 Assert.assertEquals(false,display.isNativeValid());
195
196 cleanupGL();
197 }
198
199 @Test
200 public void test02ScreenModeChange01() throws InterruptedException {
201 Thread.sleep(waitTimeShort);
202
203 final GLCapabilities caps = new GLCapabilities(glp);
204 Assert.assertNotNull(caps);
205 final Display display = NewtFactory.createDisplay(null); // local display
206 Assert.assertNotNull(display);
207 final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
208 Assert.assertNotNull(screen);
209 final GLWindow window = createWindow(screen, caps, width, height, true /* onscreen */, false /* undecorated */);
210 Assert.assertNotNull(window);
211
212 final RectangleImmutable winRect = window.getBounds();
213 final MonitorDevice monitor = screen.getMainMonitor( winRect );
214
215 List<MonitorMode> monitorModes = monitor.getSupportedModes();
216 Assert.assertTrue(monitorModes.size()>0);
217 if(monitorModes.size()==1) {
218 // no support ..
219 System.err.println("Your platform has no MonitorMode change support, sorry");
220 destroyWindow(window);
221 return;
222 }
223
224 final Animator animator = new Animator(window);
225 animator.start();
226
227 MonitorMode mmCurrent = monitor.queryCurrentMode();
228 Assert.assertNotNull(mmCurrent);
229 final MonitorMode mmOrig = monitor.getOriginalMode();
230 Assert.assertNotNull(mmOrig);
231 System.err.println("[0] orig : "+mmOrig);
232 System.err.println("[0] current: "+mmCurrent);
233 Assert.assertEquals(mmCurrent, mmOrig);
234
235 monitorModes = MonitorModeUtil.filterByFlags(monitorModes, 0); // no interlace, double-scan etc
236 Assert.assertNotNull(monitorModes);
237 Assert.assertTrue(monitorModes.size()>0);
238 monitorModes = MonitorModeUtil.filterByRotation(monitorModes, 0);
239 Assert.assertNotNull(monitorModes);
240 Assert.assertTrue(monitorModes.size()>0);
241 monitorModes = MonitorModeUtil.filterByResolution(monitorModes, new Dimension(801, 601));
242 Assert.assertNotNull(monitorModes);
243 Assert.assertTrue(monitorModes.size()>0);
244 monitorModes = MonitorModeUtil.filterByRate(monitorModes, mmOrig.getRefreshRate());
245 Assert.assertNotNull(monitorModes);
246 Assert.assertTrue(monitorModes.size()>0);
247
248 monitorModes = MonitorModeUtil.getHighestAvailableBpp(monitorModes);
249 Assert.assertNotNull(monitorModes);
250 Assert.assertTrue(monitorModes.size()>0);
251
252 // set mode
253 {
254 final MonitorMode sm = monitorModes.get(0);
255 System.err.println("[0] set current: "+sm);
256 final boolean smOk = monitor.setCurrentMode(sm);
257 mmCurrent = monitor.getCurrentMode();
258 System.err.println("[0] has current: "+mmCurrent+", changeOK "+smOk);
259 Assert.assertTrue(monitor.isModeChangedByUs());
260 Assert.assertEquals(sm, mmCurrent);
261 Assert.assertNotSame(mmOrig, mmCurrent);
262 Assert.assertEquals(mmCurrent, monitor.queryCurrentMode());
263 Assert.assertTrue(smOk);
264 }
265
266 Thread.sleep(waitTimeLong);
267
268 Assert.assertEquals(true,display.isNativeValid());
269 Assert.assertEquals(true,screen.isNativeValid());
270 Assert.assertEquals(true,window.isNativeValid());
271 Assert.assertEquals(true,window.isVisible());
272
273 animator.stop();
274 Assert.assertEquals(false, animator.isAnimating());
275 Assert.assertEquals(false, animator.isStarted());
276
277 destroyWindow(window);
278
279 Assert.assertEquals(false,window.isVisible());
280 Assert.assertEquals(false,window.isRealized());
281 Assert.assertEquals(false,window.isNativeValid());
282 Assert.assertTrue(NewtTestUtil.waitForRealized(screen, false, null));
283 Assert.assertEquals(false,screen.isNativeValid());
284 Assert.assertEquals(false,display.isNativeValid());
285
286 validateScreenModeReset(mmOrig, winRect);
287 cleanupGL();
288 }
289
290 @Test
291 public void test03ScreenModeChangeWithFS01Post() throws InterruptedException {
292 Thread.sleep(waitTimeShort);
293 testScreenModeChangeWithFS01Impl(false) ;
294 }
295
296 @Test
297 public void test04ScreenModeChangeWithFS01Pre() throws InterruptedException {
298 Thread.sleep(waitTimeShort);
299 testScreenModeChangeWithFS01Impl(true) ;
300 }
301
302 protected void testScreenModeChangeWithFS01Impl(final boolean preFS) throws InterruptedException {
303 final GLCapabilities caps = new GLCapabilities(glp);
304 final Display display = NewtFactory.createDisplay(null); // local display
305 final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
306 final GLWindow window = createWindow(screen, caps, width, height, true /* onscreen */, false /* undecorated */);
307 final Animator animator = new Animator(window);
308 animator.start();
309
310 final RectangleImmutable winRect = window.getBounds();
311 final MonitorDevice monitor = screen.getMainMonitor(winRect);
312 MonitorMode mmCurrent = monitor.queryCurrentMode();
313 Assert.assertNotNull(mmCurrent);
314 final MonitorMode mmOrig = monitor.getOriginalMode();
315 Assert.assertNotNull(mmOrig);
316 System.err.println("[0] orig : "+mmOrig);
317 System.err.println("[0] current: "+mmCurrent);
318 Assert.assertEquals(mmCurrent, mmOrig);
319
320 List<MonitorMode> monitorModes = monitor.getSupportedModes();
321 if(monitorModes.size()==1) {
322 // no support ..
323 destroyWindow(window);
324 return;
325 }
326 Assert.assertTrue(monitorModes.size()>0);
327 monitorModes = MonitorModeUtil.filterByFlags(monitorModes, 0); // no interlace, double-scan etc
328 monitorModes = MonitorModeUtil.filterByRotation(monitorModes, 0);
329 monitorModes = MonitorModeUtil.filterByResolution(monitorModes, new Dimension(801, 601));
330 monitorModes = MonitorModeUtil.filterByRate(monitorModes, mmOrig.getRefreshRate());
331 monitorModes = MonitorModeUtil.getHighestAvailableBpp(monitorModes);
332
333 final MonitorMode monitorMode = monitorModes.get(0);
334 Assert.assertNotNull(monitorMode);
335
336 if(preFS) {
337 System.err.println("[1] set FS pre 0: "+window.isFullscreen());
338 window.setFullscreen(true);
339 System.err.println("[1] set FS pre 1: "+window.isFullscreen());
340 Assert.assertEquals(true, window.isFullscreen());
341 System.err.println("[1] set FS pre X: "+window.isFullscreen());
342 }
343 Thread.sleep(waitTimeShort);
344
345 // set mode
346 {
347 System.err.println("[2] set current: "+monitorMode);
348 final boolean smOk = monitor.setCurrentMode(monitorMode);
349 mmCurrent = monitor.getCurrentMode();
350 System.err.println("[2] has current: "+mmCurrent+", changeOK "+smOk);
351 Assert.assertTrue(monitor.isModeChangedByUs());
352 Assert.assertEquals(monitorMode, mmCurrent);
353 Assert.assertNotSame(mmOrig, mmCurrent);
354 Assert.assertEquals(mmCurrent, monitor.queryCurrentMode());
355 Assert.assertTrue(smOk);
356 }
357
358 if(!preFS) {
359 System.err.println("[3] set FS post 0: "+window.isFullscreen());
360 window.setFullscreen(true);
361 Assert.assertEquals(true, window.isFullscreen());
362 System.err.println("[3] set FS post X: "+window.isFullscreen());
363 }
364
365 Thread.sleep(waitTimeLong);
366
367 if(!preFS) {
368 System.err.println("[4] set !FS post 0: "+window.isFullscreen());
369 window.setFullscreen(false);
370 Assert.assertEquals(false, window.isFullscreen());
371 System.err.println("[4] set !FS post X: "+window.isFullscreen());
372 Thread.sleep(waitTimeShort);
373 }
374
375 Assert.assertEquals(true,display.isNativeValid());
376 Assert.assertEquals(true,screen.isNativeValid());
377 Assert.assertEquals(true,window.isNativeValid());
378 Assert.assertEquals(true,window.isVisible());
379
380 animator.stop();
381 Assert.assertEquals(false, animator.isAnimating());
382 Assert.assertEquals(false, animator.isStarted());
383
384 destroyWindow(window);
385
386 Assert.assertEquals(false,window.isVisible());
387 Assert.assertEquals(false,window.isRealized());
388 Assert.assertEquals(false,window.isNativeValid());
389 Assert.assertTrue(NewtTestUtil.waitForRealized(screen, false, null));
390 Assert.assertEquals(false,screen.isNativeValid());
391 Assert.assertEquals(false,display.isNativeValid());
392
393 validateScreenModeReset(mmOrig, winRect);
394 cleanupGL();
395 }
396
397 /**
398 *
399 * @param mmOrig
400 * @param rect in window units
401 */
402 void validateScreenModeReset(final MonitorMode mmOrig, final RectangleImmutable rect) {
403 final Display display = NewtFactory.createDisplay(null); // local display
404 Assert.assertNotNull(display);
405 final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
406 Assert.assertNotNull(screen);
407 Assert.assertEquals(false,display.isNativeValid());
408 Assert.assertEquals(false,screen.isNativeValid());
409 screen.addReference();
410 Assert.assertEquals(true,display.isNativeValid());
411 Assert.assertEquals(true,screen.isNativeValid());
412
413 final MonitorDevice monitor = screen.getMainMonitor(rect);
414 Assert.assertEquals(mmOrig, monitor.getCurrentMode());
415
416 screen.removeReference();
417 Assert.assertEquals(false,display.isNativeValid());
418 Assert.assertEquals(false,screen.isNativeValid());
419 }
420
421 public static void main(final String args[]) throws IOException {
422 final String tstname = TestScreenMode01dNEWT.class.getName();
423 org.junit.runner.JUnitCore.main(tstname);
424 }
425}
void setOnscreen(final boolean onscreen)
Sets whether the surface shall be on- or offscreen.
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.
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 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().
final MonitorDevice getMainMonitor(final RectangleImmutable r)
Returns the MonitorDevice with the highest viewport coverage of the given rectangle in window units,...
Definition: Screen.java:227
abstract boolean isNativeValid()
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 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 MonitorDevice getMainMonitor()
Returns the MonitorDevice with the highest viewport coverage of this window.
Definition: GLWindow.java:292
final boolean setFullscreen(final boolean fullscreen)
Enable or disable fullscreen mode for this window.
Definition: GLWindow.java:534
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 int getHeight()
Returns the height of the client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:451
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
final int getWidth()
Returns the width of the client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:446
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 void shutdown()
Manual shutdown method, may be called after your last JOGL use within the running JVM.
Definition: GLProfile.java:277
static GLProfile getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
Definition: GLProfile.java:739
static void initSingleton()
Static initialization of JOGL.
Definition: GLProfile.java:204
Demonstrates fullscreen without MonitorMode change and fullscreen before and after MonitorMode change...
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
synchronized boolean isStarted()
Indicates whether this animator has been started.
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 Rectangle interface, with its position on the top-left.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
boolean isRealized()
Returns true if this drawable is realized, otherwise false.