JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGLCanvasAWTActionDeadlock00AWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2012 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 com.jogamp.opengl.GLAutoDrawable;
32import com.jogamp.opengl.GLEventListener;
33import com.jogamp.opengl.awt.GLCanvas;
34import com.jogamp.opengl.util.Animator;
35import com.jogamp.opengl.util.AnimatorBase;
36import com.jogamp.opengl.util.FPSAnimator;
37
38import com.jogamp.opengl.test.junit.util.UITestCase;
39import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
40
41import com.jogamp.opengl.test.junit.util.MiscUtils;
42
43import java.awt.BorderLayout;
44import java.awt.Frame;
45import java.awt.Insets;
46
47import org.junit.Assert;
48import org.junit.Assume;
49import org.junit.Test;
50import org.junit.FixMethodOrder;
51import org.junit.runners.MethodSorters;
52
53
54@FixMethodOrder(MethodSorters.NAME_ASCENDING)
56 static long durationPerTest = 1000; // ms
57 static final int width = 512;
58 static final int height = 512;
59
60 GLEventListener gle1 = null;
61 GLEventListener gle2 = null;
62
63 @Test
64 public void test01Animator() throws InterruptedException {
65 testImpl(new Animator(), 0, false);
66 }
67
68 @Test
69 public void test02FPSAnimator() throws InterruptedException {
70 testImpl(new FPSAnimator(30), 0, false);
71 }
72
73 @Test
74 public void test02FPSAnimator_RestartOnAWTEDT() throws InterruptedException {
75 testImpl(new FPSAnimator(30), 100, false);
76 }
77
78 /** May crash due to invalid thread usage, i.e. non AWT-EDT
79 @Test
80 public void test02FPSAnimator_RestartOnCurrentThread() throws InterruptedException {
81 testImpl(new FPSAnimator(30), 100, true);
82 } */
83
84 void testImpl(final AnimatorBase animator, final int restartPeriod, final boolean restartOnCurrentThread) throws InterruptedException {
85 final Frame frame1 = new Frame("Frame 1");
86 gle1 = new GLEventListener() {
87 @Override
88 public void init(final GLAutoDrawable drawable) {
89 }
90
91 @Override
92 public void dispose(final GLAutoDrawable drawable) {
93 }
94
95 @Override
96 public void display(final GLAutoDrawable drawable) {
97 frame1.setTitle("f "+frameCount+", fps "+animator.getLastFPS());
98 frameCount++;
99 }
100
101 @Override
102 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
103 }
104 };
105 gle2 = new GearsES2();
106
107 Assert.assertNotNull(frame1);
108 {
109 final Insets insets = frame1.getInsets();
110 final int w = width + insets.left + insets.right;
111 final int h = height + insets.top + insets.bottom;
112 frame1.setSize(w, h);
113 }
114 frame1.setLocation(0, 0);
115 frame1.setTitle("Generic Title");
116
117 GLCanvas glCanvas = createGLCanvas();
118 glCanvas.addGLEventListener(gle1);
119 glCanvas.addGLEventListener(gle2);
120
121 animator.setUpdateFPSFrames(60, System.err);
122 animator.add(glCanvas);
123 animator.start();
124
125 attachGLCanvas(frame1, glCanvas, false);
126
127 try {
128 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
129 public void run() {
130 frame1.setVisible(true);
131 }});
132 } catch (final Throwable t) {
133 t.printStackTrace();
134 Assume.assumeNoException(t);
135 }
136
137 final long sleep = 0 < restartPeriod ? restartPeriod : 100;
138 long togo = durationPerTest;
139 while( 0 < togo ) {
140 if(0 < restartPeriod) {
141 glCanvas = restart(frame1, glCanvas, restartOnCurrentThread);
142 }
143
144 Thread.sleep(sleep);
145
146 togo -= sleep;
147 }
148
149 dispose(frame1, glCanvas);
150 animator.stop();
151
152 gle1 = null;
153 gle2 = null;
154 }
155
156 void dispose(final Frame frame, final GLCanvas glCanvas) {
157 try {
158 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
159 public void run() {
160 glCanvas.destroy();
161 frame.dispose();
162 }});
163 } catch (final Throwable t) {
164 t.printStackTrace();
165 Assume.assumeNoException(t);
166 }
167 }
168
169 GLCanvas restart(final Frame frame, GLCanvas glCanvas, final boolean restartOnCurrentThread) throws InterruptedException {
170 glCanvas.disposeGLEventListener(gle1, true);
171 glCanvas.disposeGLEventListener(gle2, true);
172 detachGLCanvas(frame, glCanvas, restartOnCurrentThread);
173
174 glCanvas = createGLCanvas();
175
176 attachGLCanvas(frame, glCanvas, restartOnCurrentThread);
177 glCanvas.addGLEventListener(gle1);
178 glCanvas.addGLEventListener(gle2);
179
180 return glCanvas;
181 }
182
183 void attachGLCanvas(final Frame frame, final GLCanvas glCanvas, final boolean restartOnCurrentThread) {
184 System.err.println("*** attachGLCanvas.0 on-current-thread "+restartOnCurrentThread+", currentThread "+Thread.currentThread().getName());
185 if( restartOnCurrentThread ) {
186 frame.setLayout(new BorderLayout());
187 frame.add(glCanvas, BorderLayout.CENTER);
188 frame.validate();
189 } else {
190 try {
191 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
192 public void run() {
193 frame.setLayout(new BorderLayout());
194 frame.add(glCanvas, BorderLayout.CENTER);
195 frame.validate();
196 }});
197 } catch (final Throwable t) {
198 t.printStackTrace();
199 Assume.assumeNoException(t);
200 }
201 }
202 System.err.println("*** attachGLCanvas.X");
203 }
204
205 void detachGLCanvas(final Frame frame, final GLCanvas glCanvas, final boolean restartOnCurrentThread) {
206 System.err.println("*** detachGLCanvas.0 on-current-thread "+restartOnCurrentThread+", currentThread "+Thread.currentThread().getName());
207 if( restartOnCurrentThread ) {
208 frame.remove(glCanvas);
209 frame.validate();
210 } else {
211 try {
212 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
213 public void run() {
214 frame.remove(glCanvas);
215 frame.validate();
216 }});
217 } catch (final Throwable t) {
218 t.printStackTrace();
219 Assume.assumeNoException(t);
220 }
221 }
222 System.err.println("*** detachGLCanvas.X");
223 }
224
225 int frameCount = 0;
226
227 GLCanvas createGLCanvas() {
228 System.err.println("*** createGLCanvas.0");
229 final GLCanvas glCanvas = new GLCanvas();
230 glCanvas.setBounds(0, 0, width, height);
231 Assert.assertNotNull(glCanvas);
232 System.err.println("*** createGLCanvas.X");
233 return glCanvas;
234 }
235
236 public static void main(final String args[]) {
237 for(int i=0; i<args.length; i++) {
238 if(args[i].equals("-time")) {
239 durationPerTest = MiscUtils.atoi(args[++i], (int)durationPerTest);
240 }
241 }
242 org.junit.runner.JUnitCore.main(TestGLCanvasAWTActionDeadlock00AWT.class.getName());
243 }
244}
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
Base implementation of GLAnimatorControl
An Animator subclass which attempts to achieve a target frames-per-second rate to avoid using all CPU...
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.