JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestBug664GLCanvasSetVisibleSwingAWT.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.awt;
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.GLAutoDrawable;
40import com.jogamp.opengl.GLCapabilities;
41import com.jogamp.opengl.GLEventListener;
42import com.jogamp.opengl.GLProfile;
43import com.jogamp.opengl.awt.GLCanvas;
44import javax.swing.JFrame;
45import javax.swing.JPanel;
46import javax.swing.SwingUtilities;
47import javax.swing.WindowConstants;
48
49import jogamp.nativewindow.jawt.JAWTUtil;
50
51import org.junit.AfterClass;
52import org.junit.Assert;
53import org.junit.BeforeClass;
54import org.junit.Test;
55import org.junit.FixMethodOrder;
56import org.junit.runners.MethodSorters;
57
58import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
59import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
60import com.jogamp.opengl.test.junit.util.UITestCase;
61import com.jogamp.opengl.util.Animator;
62
63@FixMethodOrder(MethodSorters.NAME_ASCENDING)
65 static long durationPerTest = 500;
66 static boolean shallUseOffscreenFBOLayer = false;
67 static boolean shallUseOffscreenPBufferLayer = false;
68 static GLProfile glp;
69 static int width, height;
70 static boolean waitForKey = false;
71
72 @BeforeClass
73 public static void initClass() {
76 Assert.assertNotNull(glp);
77 width = 640;
78 height = 480;
79 } else {
80 setTestSupported(false);
81 }
82 }
83
84 @AfterClass
85 public static void releaseClass() {
86 }
87
88 protected JPanel create(final JFrame[] top, final int width, final int height, final int num)
89 throws InterruptedException, InvocationTargetException
90 {
91 final JPanel[] jPanel = new JPanel[] { null };
92 SwingUtilities.invokeAndWait(new Runnable() {
93 public void run() {
94 jPanel[0] = new JPanel();
95 jPanel[0].setLayout(new BorderLayout());
96
97 final JFrame jFrame1 = new JFrame("JFrame #"+num);
98 // jFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
99 jFrame1.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); // equivalent to Frame, use windowClosing event!
100 jFrame1.getContentPane().add(jPanel[0]);
101 jFrame1.setSize(width, height);
102
103 top[0] = jFrame1;
104 } } );
105 return jPanel[0];
106 }
107
108 protected void add(final Container cont, final Component comp, final JFrame jFrame)
109 throws InterruptedException, InvocationTargetException
110 {
111 SwingUtilities.invokeAndWait(new Runnable() {
112 public void run() {
113 cont.add(comp, BorderLayout.CENTER);
114 jFrame.pack();
115 jFrame.validate();
116 } } );
117 }
118
119 protected void dispose(final GLCanvas glc)
120 throws InterruptedException, InvocationTargetException
121 {
122 SwingUtilities.invokeAndWait(new Runnable() {
123 public void run() {
124 glc.destroy();
125 } } );
126 }
127
128 protected void setFrameVisible(final JFrame jFrame, final boolean visible) throws InterruptedException, InvocationTargetException {
129 SwingUtilities.invokeAndWait(new Runnable() {
130 public void run() {
131 jFrame.setVisible(visible);
132 } } ) ;
133 }
134
135 protected void setComponentVisible(final Component comp, final boolean visible) throws InterruptedException, InvocationTargetException {
136 SwingUtilities.invokeAndWait(new Runnable() {
137 public void run() {
138 comp.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 private volatile int frameCount = 0;
150
151 protected void runTestGL(final boolean onscreen, final GLCapabilities caps)
152 throws AWTException, InterruptedException, InvocationTargetException
153 {
154
155 for(int i=0; i<1; i++) {
156 final Animator anim = new Animator();
157 final GLCanvas glc = new GLCanvas(caps);
158 Assert.assertNotNull(glc);
159 anim.add(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);
168 @Override
169 public void init(final GLAutoDrawable drawable) {}
170 @Override
171 public void dispose(final GLAutoDrawable drawable) {}
172 @Override
173 public void display(final GLAutoDrawable drawable) {
174 frameCount++;
175 }
176 @Override
177 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {}
178 });
179 final GearsES2 gears = new GearsES2(1);
180 gears.setVerbose(false);
181 glc.addGLEventListener(gears);
182
183 final JFrame[] top = new JFrame[] { null };
184 final Container glcCont = create(top, width, height, i);
185 add(glcCont, glc, top[0]);
186
187 System.err.println("XXXX Visible Part 1/3");
188 frameCount = 0;
189 setFrameVisible(top[0], true);
190 Assert.assertTrue("Component didn't become visible", AWTRobotUtil.waitForVisible(glc, true, null));
191 Assert.assertTrue("Component didn't become realized", AWTRobotUtil.waitForRealized(glc, true, null));
192
193 anim.setUpdateFPSFrames(60, System.err);
194 anim.start();
195 anim.resetFPSCounter();
196
197 while( anim.getTotalFPSDuration() < durationPerTest ) {
198 Thread.sleep(60);
199 }
200
201 System.err.println("XXXXX Invisible Part 2/3");
202 setComponentVisible(glc, false);
203 Assert.assertTrue("Component didn't become invisible", AWTRobotUtil.waitForVisible(glc, false, null));
204 final int frameCountT0 = frameCount;
205 anim.resetFPSCounter();
206
207 while( anim.getTotalFPSDuration() < durationPerTest ) {
208 Thread.sleep(60);
209 }
210
211 final int frameCountT1 = frameCount;
212 System.err.println("GLCanvas invisible frame count: Before "+frameCountT0+", after "+frameCountT1);
213 Assert.assertTrue("GLCanvas rendered more that 4 times while being invisible, before "+frameCountT0+", after "+frameCountT1,
214 4 >= frameCountT1 - frameCountT0);
215
216 System.err.println("XXXX Visible Part 3/3");
217 setComponentVisible(glc, true);
218 Assert.assertTrue("Component didn't become visible", AWTRobotUtil.waitForVisible(glc, true, null));
219 anim.resetFPSCounter();
220
221 while( anim.getTotalFPSDuration() < durationPerTest ) {
222 Thread.sleep(60);
223 }
224
225 System.err.println("GLCanvas isOffscreenLayerSurfaceEnabled: "+glc.isOffscreenLayerSurfaceEnabled()+": "+glc.getChosenGLCapabilities());
226
227 dispose(top[0]);
228 }
229 }
230
231 @Test
232 public void test01Onscreen()
233 throws AWTException, InterruptedException, InvocationTargetException
234 {
235 if( shallUseOffscreenFBOLayer || shallUseOffscreenPBufferLayer || JAWTUtil.isOffscreenLayerRequired() ) {
236 System.err.println("Offscreen test requested or platform requires it.");
237 return;
238 }
240 if(shallUseOffscreenPBufferLayer) {
241 caps.setPBuffer(true);
242 caps.setOnscreen(true); // simulate normal behavior ..
243 }
244 runTestGL(true, caps);
245 }
246
247 @Test
248 public void test02Offscreen()
249 throws AWTException, InterruptedException, InvocationTargetException
250 {
251 if( !JAWTUtil.isOffscreenLayerSupported() ) {
252 System.err.println("Platform doesn't support offscreen test.");
253 return;
254 }
256 if(shallUseOffscreenPBufferLayer) {
257 caps.setPBuffer(true);
258 caps.setOnscreen(true); // simulate normal behavior ..
259 }
260 runTestGL(false, caps);
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 i++;
267 try {
268 durationPerTest = Long.parseLong(args[i]);
269 } catch (final Exception ex) { ex.printStackTrace(); }
270 } else if(args[i].equals("-layeredFBO")) {
271 shallUseOffscreenFBOLayer = true;
272 } else if(args[i].equals("-layeredPBuffer")) {
273 shallUseOffscreenPBufferLayer = true;
274 } else if(args[i].equals("-wait")) {
275 waitForKey = true;
276 }
277 }
278 System.err.println("waitForKey "+waitForKey);
279
280 System.err.println("shallUseOffscreenFBOLayer "+shallUseOffscreenFBOLayer);
281 System.err.println("shallUseOffscreenPBufferLayer "+shallUseOffscreenPBufferLayer);
282 if(waitForKey) {
283 UITestCase.waitForKey("Start");
284 }
285 org.junit.runner.JUnitCore.main(TestBug664GLCanvasSetVisibleSwingAWT.class.getName());
286 }
287}
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 getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
Definition: GLProfile.java:739
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
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
JPanel create(final JFrame[] top, final int width, final int height, final int num)
void add(final Container cont, final Component comp, final JFrame jFrame)
static boolean waitForRealized(final java.awt.Component comp, final boolean realized, final Runnable waitAction)
static boolean waitForVisible(final java.awt.Component comp, final boolean visible, final Runnable waitAction)
final void resetFPSCounter()
Reset all performance counter (startTime, currentTime, frame number)
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
final void setUpdateFPSFrames(final int frames, final PrintStream out)
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
void setSize(int width, int height)
Requests a new width and height for this AWTGLAutoDrawable.