JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestDisplayLifecycle01NEWT.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;
30
31import org.junit.Assert;
32import org.junit.BeforeClass;
33import org.junit.Test;
34import org.junit.FixMethodOrder;
35import org.junit.runners.MethodSorters;
36
37import com.jogamp.opengl.*;
38
39import com.jogamp.newt.*;
40import com.jogamp.newt.event.*;
41import com.jogamp.newt.opengl.*;
42import com.jogamp.newt.util.EDTUtil;
43
44import java.io.IOException;
45
46import com.jogamp.opengl.test.junit.util.UITestCase;
47import com.jogamp.opengl.test.junit.util.MiscUtils;
48import com.jogamp.opengl.test.junit.util.NewtTestUtil;
49import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
50
51@FixMethodOrder(MethodSorters.NAME_ASCENDING)
53 static GLProfile glp;
54 static GLCapabilities caps;
55 static final int width = 500, height = 500, xy_pos=100;
56 static long durationPerTest = 100; // ms
57
58 @BeforeClass
59 public static void initClass() {
60 glp = GLProfile.getDefault();
61 caps = new GLCapabilities(glp);
62 }
63
64 static GLWindow createWindow(final Screen screen, final GLCapabilities caps, final int width, final int height)
65 throws InterruptedException
66 {
67 Assert.assertNotNull(caps);
68
69 //
70 // Create native windowing resources .. X11/Win/OSX
71 //
72 GLWindow glWindow;
73 if(null!=screen) {
74 final Window window = NewtFactory.createWindow(screen, caps);
75 Assert.assertNotNull(window);
76 glWindow = GLWindow.create(window);
77 } else {
78 glWindow = GLWindow.create(caps);
79 }
80 glWindow.setUpdateFPSFrames(1, null);
81
82 final GLEventListener demo = new GearsES2();
83 setDemoFields(demo, glWindow);
84 glWindow.addGLEventListener(demo);
86 glWindow.setSize(width, height);
87 return glWindow;
88 }
89
90 private void testDisplayCreate01(final Display display, final Screen screen) throws InterruptedException {
91 // start-state == end-state
92 Assert.assertEquals(0,Display.getActiveDisplayNumber());
93 Assert.assertEquals(0,display.getReferenceCount());
94 Assert.assertEquals(false,display.isNativeValid());
95 Assert.assertNotNull(display.getEDTUtil());
96 Assert.assertEquals(false,display.getEDTUtil().isRunning());
97 Assert.assertEquals(0,screen.getReferenceCount());
98 Assert.assertEquals(false,screen.isNativeValid());
99
100 // Create Window, pending lazy native creation
101 final GLWindow window = createWindow(screen, caps, width, height);
102 window.setPosition(xy_pos, xy_pos);
103 Assert.assertEquals(screen,window.getScreen());
104 Assert.assertEquals(0,Display.getActiveDisplayNumber());
105 Assert.assertEquals(0,display.getReferenceCount());
106 Assert.assertEquals(false,display.isNativeValid());
107 Assert.assertEquals(true,display.getEDTUtil().isRunning());
108 Assert.assertEquals(0,screen.getReferenceCount());
109 Assert.assertEquals(false,screen.isNativeValid());
110
111 Assert.assertNotNull(window.getScreen());
112 Assert.assertEquals(false,window.isNativeValid());
113 Assert.assertEquals(false,window.isVisible());
114 Assert.assertTrue(NewtTestUtil.hasPositionMax2xInsetsOr64Pix(window, xy_pos, xy_pos));
115
116 // lazy native creation sequence: Display, Screen and Window
117 Assert.assertEquals(0, window.getTotalFPSFrames());
118 window.setVisible(true);
119
120 Assert.assertEquals(screen,window.getScreen());
121 Assert.assertEquals(1,Display.getActiveDisplayNumber());
122 Assert.assertEquals(1,display.getReferenceCount());
123 Assert.assertEquals(true,display.isNativeValid());
124 Assert.assertEquals(true,display.getEDTUtil().isRunning());
125 Assert.assertEquals(1,screen.getReferenceCount());
126 Assert.assertEquals(true,screen.isNativeValid());
127 Assert.assertEquals(true,window.isNativeValid());
128 Assert.assertEquals(true,window.isVisible());
129 Assert.assertTrue(NewtTestUtil.hasPositionMax2xInsetsOr64Pix(window, xy_pos, xy_pos));
130 System.err.println("Frames for setVisible(true) 1: "+window.getTotalFPSFrames());
131 Assert.assertTrue(0 < window.getTotalFPSFrames());
132
133 while(window.getTotalFPSDuration()<1*durationPerTest) {
134 window.display();
135 Thread.sleep(100);
136 }
137 System.err.println("duration: "+window.getTotalFPSDuration());
138
139 // just make the Window invisible
140 window.setVisible(false);
141 Assert.assertEquals(true,window.isNativeValid());
142 Assert.assertEquals(false,window.isVisible());
143 // Invisible window might have been moved away (Windows 10), informal only:
144 NewtTestUtil.hasPositionMax2xInsetsOr64Pix(window, xy_pos, xy_pos);
145
146 // just make the Window visible again
147 window.resetFPSCounter();
148 Assert.assertEquals(0, window.getTotalFPSFrames());
149 window.setVisible(true);
150 Assert.assertEquals(true,window.isNativeValid());
151 Assert.assertEquals(true,window.isVisible());
152 Assert.assertTrue(NewtTestUtil.hasPositionMax2xInsetsOr64Pix(window, xy_pos, xy_pos));
153 System.err.println("Frames for setVisible(true) 1: "+window.getTotalFPSFrames());
154 Assert.assertTrue(0 < window.getTotalFPSFrames());
155
156 while(window.getTotalFPSDuration()<2*durationPerTest) {
157 window.display();
158 Thread.sleep(100);
159 }
160 System.err.println("duration: "+window.getTotalFPSDuration());
161
162 // destruction ..
163 window.destroy();
164 Assert.assertEquals(screen,window.getScreen());
165 Assert.assertEquals(0,Display.getActiveDisplayNumber());
166 Assert.assertEquals(0,display.getReferenceCount());
167 Assert.assertEquals(false,display.isNativeValid());
168 Assert.assertNotNull(display.getEDTUtil());
169 Assert.assertEquals(false,display.getEDTUtil().isRunning());
170 Assert.assertEquals(0,screen.getReferenceCount());
171 Assert.assertEquals(false,screen.isNativeValid());
172 Assert.assertEquals(false,window.isNativeValid());
173 Assert.assertEquals(false,window.isVisible());
174 window.resetFPSCounter();
175 Assert.assertEquals(0, window.getTotalFPSFrames());
176
177 // a display call shall not change a thing
178 window.display();
179 Assert.assertEquals(0, window.getTotalFPSFrames());
180 Assert.assertEquals(false,window.isNativeValid());
181 Assert.assertEquals(false,window.isVisible());
182
183 // recover Window
184 window.setVisible(true);
185
186 Assert.assertEquals(screen,window.getScreen());
187 Assert.assertEquals(1,Display.getActiveDisplayNumber());
188 Assert.assertEquals(1,display.getReferenceCount());
189 Assert.assertEquals(true,display.isNativeValid());
190 Assert.assertEquals(true,display.getEDTUtil().isRunning());
191 Assert.assertEquals(1,screen.getReferenceCount());
192 Assert.assertEquals(true,screen.isNativeValid());
193 Assert.assertEquals(true,window.isNativeValid());
194 Assert.assertEquals(true,window.isVisible());
195 System.err.println("Frames for setVisible(true) 2: "+window.getTotalFPSFrames());
196 Assert.assertTrue(0 < window.getTotalFPSFrames());
197
198 while(window.getTotalFPSDuration()<1*durationPerTest) {
199 window.display();
200 Thread.sleep(100);
201 }
202 System.err.println("duration: "+window.getTotalFPSDuration());
203
204 // destruction ..
205 window.destroy();
206 Display.dumpDisplayList("Post destroy(true)");
207
208 // end-state == start-state
209 Assert.assertEquals(0,Display.getActiveDisplayNumber());
210 Assert.assertEquals(0,display.getReferenceCount());
211 Assert.assertEquals(false,display.isNativeValid());
212 {
213 final EDTUtil edtUtil = display.getEDTUtil();
214 Assert.assertNotNull(edtUtil);
215 Assert.assertEquals(false,edtUtil.isRunning());
216 edtUtil.start();
217 Assert.assertEquals(true,edtUtil.isRunning());
218 edtUtil.invoke(true, null);
219 Assert.assertEquals(true,edtUtil.isRunning());
220 edtUtil.invokeStop(true, null);
221 edtUtil.waitUntilStopped();
222 Assert.assertEquals(false,edtUtil.isRunning());
223 }
224 Assert.assertEquals(0,screen.getReferenceCount());
225 Assert.assertEquals(false,screen.isNativeValid());
226
227 Assert.assertNotNull(window.getScreen());
228 Assert.assertEquals(false,window.isNativeValid());
229 Assert.assertEquals(false,window.isVisible());
230 }
231
232 @Test
233 public void testDisplayCreate01_AutoDestroyLifecycle() throws InterruptedException {
234 Assert.assertEquals(0,Display.getActiveDisplayNumber());
235
236 // Create Display/Screen, pending lazy native creation
237 final Display display = NewtFactory.createDisplay(null);
238 final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
239 System.err.println("Pass - 1");
240 testDisplayCreate01(display, screen);
241 System.err.println("Pass - 2");
242 testDisplayCreate01(display, screen);
243
244 Assert.assertEquals(0,Display.getActiveDisplayNumber());
245 }
246
247 public static void setDemoFields(final GLEventListener demo, final GLWindow glWindow) {
248 Assert.assertNotNull(demo);
249 Assert.assertNotNull(glWindow);
250 if(!MiscUtils.setFieldIfExists(demo, "window", glWindow)) {
251 MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
252 }
253 }
254
255 static int atoi(final String a) {
256 int i=0;
257 try {
258 i = Integer.parseInt(a);
259 } catch (final Exception ex) { ex.printStackTrace(); }
260 return i;
261 }
262
263 public static void main(final String args[]) throws IOException {
264 for(int i=0; i<args.length; i++) {
265 if(args[i].equals("-time")) {
266 durationPerTest = atoi(args[++i]);
267 }
268 }
269 System.err.println("durationPerTest: "+durationPerTest);
270 final String tstname = TestDisplayLifecycle01NEWT.class.getName();
271 org.junit.runner.JUnitCore.main(tstname);
272 }
273
274}
static int getActiveDisplayNumber()
Definition: Display.java:452
static void dumpDisplayList(final String prefix)
Definition: Display.java:357
static Display createDisplay(final String name)
Create a Display entity.
static Window createWindow(final CapabilitiesImmutable caps)
Create a top level Window entity on the default Display and default Screen.
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
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 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 addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
Definition: GLWindow.java:882
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 void setDemoFields(final GLEventListener demo, final GLWindow glWindow)
static boolean setFieldIfExists(final Object instance, final String fieldName, final Object value)
Definition: MiscUtils.java:193
Specifying NEWT's Window functionality:
Definition: Window.java:115
EDT stands for Event Dispatch Thread.
Definition: EDTUtil.java:53
boolean invokeStop(boolean wait, Runnable finalTask)
Append the final task to the EDT task queue, signals EDT to stop.
void start()
Starts the EDT after it's creation or after stopping.
boolean invoke(boolean wait, Runnable task)
Appends task to the EDT task queue if current thread is not EDT, otherwise execute task immediately.
boolean waitUntilStopped()
Wait until EDT task is stopped.
void setUpdateFPSFrames(int frames, PrintStream out)
void resetFPSCounter()
Reset all performance counter (startTime, currentTime, frame number)
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.