JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestAddRemove01GLCanvasSwingAWT.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.opengl.test.junit.jogl.demos.es2.GearsES2;
57import com.jogamp.opengl.test.junit.util.MiscUtils;
58import com.jogamp.opengl.test.junit.util.UITestCase;
59
60@FixMethodOrder(MethodSorters.NAME_ASCENDING)
62 static long durationPerTest = 100;
63 static int addRemoveCount = 15;
64 static int pauseEach = 0;
65 static int pauseDuration = 500;
66 static boolean noOnscreenTest = false;
67 static boolean noOffscreenTest = false;
68 static boolean offscreenPBufferOnly = false;
69 static boolean offscreenFBOOnly = false;
70 static GLProfile glpGL2, glpGL2ES2;
71 static int width, height;
72 static boolean waitForKey = false;
73 static boolean waitForKeyPost = false;
74
75 @BeforeClass
76 public static void initClass() {
77 width = 640;
78 height = 480;
80 glpGL2ES2 = GLProfile.get(GLProfile.GL2ES2);
81 Assert.assertNotNull(glpGL2ES2);
82 }
84 glpGL2 = GLProfile.get(GLProfile.GL2);
85 Assert.assertNotNull(glpGL2);
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 if(waitForKey) {
153 UITestCase.waitForKey("Start");
154 }
155 for(int i=0; i<addRemoveOpCount; i++) {
156 int ti = 0;
157 System.err.println("Loop."+(ti++)+" "+(i+1)+"/"+addRemoveOpCount);
158 final GLCanvas glc = new GLCanvas(caps);
159 Assert.assertNotNull(glc);
160 if( !onscreen ) {
162 }
163 final Dimension glc_sz = new Dimension(width, height);
164 glc.setMinimumSize(glc_sz);
165 glc.setPreferredSize(glc_sz);
166 glc.setSize(glc_sz);
167 final GearsES2 gears = new GearsES2(1);
168 gears.setVerbose(false);
169 glc.addGLEventListener(gears);
170
171 final JFrame[] top = new JFrame[] { null };
172 final Container glcCont = create(top, width, height, i);
173 add(glcCont, glc);
174
175 setVisible(top[0], true);
176
177 final long t0 = System.currentTimeMillis();
178 do {
179 glc.display();
180 Thread.sleep(10);
181 } while ( ( System.currentTimeMillis() - t0 ) < durationPerTest ) ;
182
183 System.err.println("Loop."+(ti++)+" "+(i+1)+"/"+addRemoveOpCount+": GLCanvas isOffscreenLayerSurfaceEnabled: "+glc.isOffscreenLayerSurfaceEnabled()+": "+glc.getChosenGLCapabilities());
184
185 dispose(top[0]);
186
187 if( 0 < pauseEach && 0 == i % pauseEach ) {
188 System.err.println("******* P A U S E - Start ********");
189 // OSXUtil.WaitUntilFinish();
190 Thread.sleep(pauseDuration);
191 System.err.println("******* P A U S E - End ********");
192 }
193 }
194 if(waitForKeyPost) {
195 UITestCase.waitForKey("End");
196 }
197 }
198
199 @Test
200 public void test01Onscreen()
201 throws AWTException, InterruptedException, InvocationTargetException
202 {
203 if( noOnscreenTest || JAWTUtil.isOffscreenLayerRequired() ) {
204 System.err.println("No onscreen test requested or platform doesn't support onscreen rendering.");
205 return;
206 }
207 final GLCapabilities caps = new GLCapabilities(glpGL2ES2);
208 runTestGL(true, caps, addRemoveCount);
209 }
210
211 @Test
212 public void test02OffscreenFBO()
213 throws AWTException, InterruptedException, InvocationTargetException
214 {
215 if( noOffscreenTest || !JAWTUtil.isOffscreenLayerSupported() ) {
216 System.err.println("No offscreen test requested or platform doesn't support offscreen rendering.");
217 return;
218 }
219 if( offscreenPBufferOnly ) {
220 System.err.println("Only PBuffer test is requested.");
221 return;
222 }
223 final GLCapabilities caps = new GLCapabilities(glpGL2ES2);
224 if(offscreenPBufferOnly) {
225 caps.setPBuffer(true);
226 caps.setOnscreen(true); // simulate normal behavior ..
227 }
228 runTestGL(false, caps, addRemoveCount);
229 }
230
231 @Test
233 throws AWTException, InterruptedException, InvocationTargetException
234 {
235 if( noOffscreenTest || !JAWTUtil.isOffscreenLayerSupported() ) {
236 System.err.println("No offscreen test requested or platform doesn't support offscreen rendering.");
237 return;
238 }
239 if( offscreenFBOOnly ) {
240 System.err.println("Only FBO test is requested.");
241 return;
242 }
243 final GLCapabilities caps = new GLCapabilities(glpGL2);
244 caps.setPBuffer(true);
245 caps.setOnscreen(true); // simulate normal behavior ..
246 runTestGL(false, caps, addRemoveCount);
247 }
248
249 public static void main(final String args[]) throws IOException {
250 for(int i=0; i<args.length; i++) {
251 if(args[i].equals("-time")) {
252 i++;
253 try {
254 durationPerTest = Long.parseLong(args[i]);
255 } catch (final Exception ex) { ex.printStackTrace(); }
256 } else if(args[i].equals("-loops")) {
257 i++;
258 addRemoveCount = MiscUtils.atoi(args[i], addRemoveCount);
259 } else if(args[i].equals("-pauseEach")) {
260 i++;
261 pauseEach = MiscUtils.atoi(args[i], pauseEach);
262 } else if(args[i].equals("-pauseDuration")) {
263 i++;
264 pauseDuration = MiscUtils.atoi(args[i], pauseDuration);
265 } else if(args[i].equals("-noOnscreen")) {
266 noOnscreenTest = true;
267 } else if(args[i].equals("-noOffscreen")) {
268 noOffscreenTest = true;
269 } else if(args[i].equals("-layeredFBO")) {
270 offscreenFBOOnly = true;
271 } else if(args[i].equals("-layeredPBuffer")) {
272 offscreenPBufferOnly = true;
273 } else if(args[i].equals("-wait")) {
274 waitForKey = true;
275 } else if(args[i].equals("-waitPost")) {
276 waitForKeyPost = true;
277 }
278 }
279 System.err.println("waitForKey "+waitForKey);
280 System.err.println("waitForKeyPost "+waitForKeyPost);
281
282 System.err.println("addRemoveCount "+addRemoveCount);
283 System.err.println("pauseEach "+pauseEach);
284 System.err.println("pauseDuration "+pauseDuration);
285
286 System.err.println("noOnscreenTest "+noOnscreenTest);
287 System.err.println("noOffscreenTest "+noOffscreenTest);
288 System.err.println("offscreenPBufferOnly "+offscreenPBufferOnly);
289 System.err.println("offscreenFBOOnly "+offscreenFBOOnly);
290
291 org.junit.runner.JUnitCore.main(TestAddRemove01GLCanvasSwingAWT.class.getName());
292 }
293}
void setOnscreen(final boolean onscreen)
Sets whether the surface shall be on- or offscreen.
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.
static final String GL2
The desktop OpenGL profile 1.x up to 3.0.
Definition: GLProfile.java:579
A heavyweight AWT component which provides OpenGL rendering support.
Definition: GLCanvas.java:170
void setShallUseOffscreenLayer(final boolean v)
Request an offscreen layer, if supported.
Definition: GLCanvas.java:304
final boolean isOffscreenLayerSurfaceEnabled()
Returns true if this instance uses an offscreen layer, otherwise false.
Definition: GLCanvas.java:314
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:1065
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
Definition: GLCanvas.java:1225
void runTestGL(final boolean onscreen, final GLCapabilities caps, final int addRemoveOpCount)
JPanel create(final JFrame[] top, final int width, final int height, final int num)
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
void setSize(int width, int height)
Requests a new width and height for this AWTGLAutoDrawable.