JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestAddRemove02GLWindowNewtCanvasAWT.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.jogl.acore;
30
31import java.awt.AWTException;
32import java.awt.BorderLayout;
33import java.awt.Component;
34import java.awt.Container;
35import java.awt.Dimension;
36import java.io.IOException;
37import java.lang.reflect.InvocationTargetException;
38
39import com.jogamp.opengl.GLCapabilities;
40import com.jogamp.opengl.GLProfile;
41import com.jogamp.opengl.awt.GLCanvas;
42import javax.swing.JFrame;
43import javax.swing.JPanel;
44import javax.swing.SwingUtilities;
45import javax.swing.WindowConstants;
46
47import jogamp.nativewindow.jawt.JAWTUtil;
48
49import org.junit.AfterClass;
50import org.junit.Assert;
51import org.junit.BeforeClass;
52import org.junit.Test;
53import org.junit.FixMethodOrder;
54import org.junit.runners.MethodSorters;
55
56import com.jogamp.newt.awt.NewtCanvasAWT;
57import com.jogamp.newt.opengl.GLWindow;
58import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
59import com.jogamp.opengl.test.junit.util.MiscUtils;
60import com.jogamp.opengl.test.junit.util.UITestCase;
61
62@FixMethodOrder(MethodSorters.NAME_ASCENDING)
64 static long durationPerTest = 50;
65 static int addRemoveCount = 15;
66 static int pauseEach = 0;
67 static int pauseDuration = 500;
68 static boolean noOnscreenTest = false;
69 static boolean noOffscreenTest = false;
70 static boolean offscreenPBufferOnly = false;
71 static boolean offscreenFBOOnly = false;
72 static GLProfile glp;
73 static int width, height;
74 static boolean waitForKey = false;
75 static boolean waitForKeyPost = false;
76
77 @BeforeClass
78 public static void initClass() {
81 Assert.assertNotNull(glp);
82 width = 640;
83 height = 480;
84 } else {
85 setTestSupported(false);
86 }
87 }
88
89 @AfterClass
90 public static void releaseClass() {
91 }
92
93 protected JPanel create(final JFrame[] top, final int width, final int height, final int num)
94 throws InterruptedException, InvocationTargetException
95 {
96 final JPanel[] jPanel = new JPanel[] { null };
97 SwingUtilities.invokeAndWait(new Runnable() {
98 public void run() {
99 jPanel[0] = new JPanel();
100 jPanel[0].setLayout(new BorderLayout());
101
102 final JFrame jFrame1 = new JFrame("JFrame #"+num);
103 // jFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
104 jFrame1.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); // equivalent to Frame, use windowClosing event!
105 jFrame1.getContentPane().add(jPanel[0]);
106 jFrame1.setSize(width, height);
107
108 top[0] = jFrame1;
109 } } );
110 return jPanel[0];
111 }
112
113 protected void add(final Container cont, final Component comp)
114 throws InterruptedException, InvocationTargetException
115 {
116 SwingUtilities.invokeAndWait(new Runnable() {
117 public void run() {
118 cont.add(comp, BorderLayout.CENTER);
119 } } );
120 }
121
122 protected void dispose(final GLCanvas glc)
123 throws InterruptedException, InvocationTargetException
124 {
125 SwingUtilities.invokeAndWait(new Runnable() {
126 public void run() {
127 glc.destroy();
128 } } );
129 }
130
131 protected void setVisible(final JFrame jFrame, final boolean visible) throws InterruptedException, InvocationTargetException {
132 SwingUtilities.invokeAndWait(new Runnable() {
133 public void run() {
134 if( visible ) {
135 jFrame.pack();
136 jFrame.validate();
137 }
138 jFrame.setVisible(visible);
139 } } ) ;
140 }
141
142 protected void dispose(final JFrame jFrame) throws InterruptedException, InvocationTargetException {
143 SwingUtilities.invokeAndWait(new Runnable() {
144 public void run() {
145 jFrame.dispose();
146 } } ) ;
147 }
148
149 protected void runTestGL(final boolean onscreen, final GLCapabilities caps, final int addRemoveOpCount)
150 throws AWTException, InterruptedException, InvocationTargetException
151 {
152
153 for(int i=0; i<addRemoveOpCount; i++) {
154 System.err.println("Loop # "+i+" / "+addRemoveCount);
155 final GLWindow glw = GLWindow.create(caps);
156 Assert.assertNotNull(glw);
157 final NewtCanvasAWT glc = new NewtCanvasAWT(glw);
158 Assert.assertNotNull(glc);
159 if( !onscreen ) {
161 }
162 final Dimension glc_sz = new Dimension(width, height);
163 glc.setMinimumSize(glc_sz);
164 glc.setPreferredSize(glc_sz);
165 glc.setSize(glc_sz);
166 final GearsES2 gears = new GearsES2(1);
167 gears.setVerbose(false);
168 glw.addGLEventListener(gears);
169
170 final JFrame[] top = new JFrame[] { null };
171 final Container glcCont = create(top, width, height, i);
172 add(glcCont, glc);
173
174 setVisible(top[0], true);
175
176 final long t0 = System.currentTimeMillis();
177 do {
178 glw.display();
179 Thread.sleep(10);
180 } while ( ( System.currentTimeMillis() - t0 ) < durationPerTest ) ;
181
182 System.err.println("GLCanvas isOffscreenLayerSurfaceEnabled: "+glc.isOffscreenLayerSurfaceEnabled()+": "+glw.getChosenGLCapabilities());
183
184 dispose(top[0]);
185 glw.destroy();
186
187 if( 0 < pauseEach && 0 == i % pauseEach ) {
188 System.err.println("******* P A U S E ********");
189 Thread.sleep(pauseDuration);
190 }
191 }
192 if(waitForKeyPost) {
193 UITestCase.waitForKey("End");
194 }
195 }
196
197 @Test
198 public void test01Onscreen()
199 throws AWTException, InterruptedException, InvocationTargetException
200 {
201 if( noOnscreenTest || JAWTUtil.isOffscreenLayerRequired() ) {
202 System.err.println("No onscreen test requested or platform doesn't support onscreen rendering.");
203 return;
204 }
205 final GLCapabilities caps = new GLCapabilities(glp);
206 runTestGL(true, caps, addRemoveCount);
207 }
208
209 @Test
210 public void test02OffscreenFBO()
211 throws AWTException, InterruptedException, InvocationTargetException
212 {
213 if( noOffscreenTest || !JAWTUtil.isOffscreenLayerSupported() ) {
214 System.err.println("No offscreen test requested or platform doesn't support offscreen rendering.");
215 return;
216 }
217 if( offscreenPBufferOnly ) {
218 System.err.println("Only PBuffer test is requested.");
219 return;
220 }
221 final GLCapabilities caps = new GLCapabilities(glp);
222 runTestGL(false, caps, addRemoveCount);
223 }
224
225 @Test
227 throws AWTException, InterruptedException, InvocationTargetException
228 {
229 if( noOffscreenTest || !JAWTUtil.isOffscreenLayerSupported() ) {
230 System.err.println("No offscreen test requested or platform doesn't support offscreen rendering.");
231 return;
232 }
233 if( offscreenFBOOnly ) {
234 System.err.println("Only FBO test is requested.");
235 return;
236 }
237 final GLCapabilities caps = new GLCapabilities(glp);
238 caps.setPBuffer(true);
239 caps.setOnscreen(true); // simulate normal behavior ..
240 runTestGL(false, caps, addRemoveCount);
241 }
242
243 public static void main(final String args[]) throws IOException {
244 for(int i=0; i<args.length; i++) {
245 if(args[i].equals("-time")) {
246 i++;
247 try {
248 durationPerTest = Long.parseLong(args[i]);
249 } catch (final Exception ex) { ex.printStackTrace(); }
250 } else if(args[i].equals("-loops")) {
251 i++;
252 addRemoveCount = MiscUtils.atoi(args[i], addRemoveCount);
253 } else if(args[i].equals("-pauseEach")) {
254 i++;
255 pauseEach = MiscUtils.atoi(args[i], pauseEach);
256 } else if(args[i].equals("-pauseDuration")) {
257 i++;
258 pauseDuration = MiscUtils.atoi(args[i], pauseDuration);
259 } else if(args[i].equals("-noOnscreen")) {
260 noOnscreenTest = true;
261 } else if(args[i].equals("-noOffscreen")) {
262 noOffscreenTest = true;
263 } else if(args[i].equals("-layeredFBO")) {
264 offscreenFBOOnly = true;
265 } else if(args[i].equals("-layeredPBuffer")) {
266 offscreenPBufferOnly = true;
267 } else if(args[i].equals("-wait")) {
268 waitForKey = true;
269 } else if(args[i].equals("-waitPost")) {
270 waitForKeyPost = true;
271 }
272 }
273 System.err.println("waitForKey "+waitForKey);
274 System.err.println("waitForKeyPost "+waitForKeyPost);
275
276 System.err.println("addRemoveCount "+addRemoveCount);
277 System.err.println("pauseEach "+pauseEach);
278 System.err.println("pauseDuration "+pauseDuration);
279
280 System.err.println("noOnscreenTest "+noOnscreenTest);
281 System.err.println("noOffscreenTest "+noOffscreenTest);
282 System.err.println("offscreenPBufferOnly "+offscreenPBufferOnly);
283 System.err.println("offscreenFBOOnly "+offscreenFBOOnly);
284 if(waitForKey) {
285 UITestCase.waitForKey("Start");
286 }
287 org.junit.runner.JUnitCore.main(TestAddRemove02GLWindowNewtCanvasAWT.class.getName());
288 }
289}
void setOnscreen(final boolean onscreen)
Sets whether the surface shall be on- or offscreen.
AWT Canvas containing a NEWT Window using native parenting.
final boolean isOffscreenLayerSurfaceEnabled()
Returns true if this instance uses an offscreen layer, otherwise false.
void setShallUseOffscreenLayer(final boolean v)
Request an offscreen layer, if supported.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
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.
void setPBuffer(final boolean enable)
Requesting offscreen pbuffer mode.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static boolean isAvailable(final AbstractGraphicsDevice device, final String profile)
Returns the availability of a profile on a device.
Definition: GLProfile.java:305
static final String GL2ES2
The intersection of the desktop GL3, GL2 and embedded ES2 profile.
Definition: GLProfile.java:594
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
A heavyweight AWT component which provides OpenGL rendering support.
Definition: GLCanvas.java:170
JPanel create(final JFrame[] top, final int width, final int height, final int num)
void runTestGL(final boolean onscreen, final GLCapabilities caps, final int addRemoveOpCount)
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...