JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestPerf001GLJPanelInit01AWT.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 */
28package com.jogamp.opengl.test.junit.jogl.perf;
29
30import java.awt.Component;
31import java.awt.Dimension;
32import java.awt.GridLayout;
33import java.lang.reflect.InvocationTargetException;
34import java.util.ArrayList;
35import java.util.List;
36import java.util.concurrent.atomic.AtomicInteger;
37
38import com.jogamp.opengl.GLAnimatorControl;
39import com.jogamp.opengl.GLAutoDrawable;
40import com.jogamp.opengl.GLCapabilities;
41import com.jogamp.opengl.GLCapabilitiesImmutable;
42import com.jogamp.opengl.GLEventListener;
43import com.jogamp.opengl.GLProfile;
44import com.jogamp.opengl.awt.GLCanvas;
45import com.jogamp.opengl.awt.GLJPanel;
46import javax.swing.JFrame;
47import javax.swing.JPanel;
48import javax.swing.SwingUtilities;
49
50import org.junit.Assume;
51import org.junit.BeforeClass;
52import org.junit.FixMethodOrder;
53import org.junit.Test;
54import org.junit.runners.MethodSorters;
55
56import com.jogamp.common.os.Platform;
57import com.jogamp.newt.awt.NewtCanvasAWT;
58import com.jogamp.newt.opengl.GLWindow;
59import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
60import com.jogamp.opengl.test.junit.util.MiscUtils;
61import com.jogamp.opengl.test.junit.util.UITestCase;
62import com.jogamp.opengl.util.Animator;
63
64/**
65 * Tests multiple [GLJPanels, GLCanvas or NewtCanvasAWT] in a JFrame's Grid
66 */
67@FixMethodOrder(MethodSorters.NAME_ASCENDING)
69 final long INIT_TIMEOUT = 10L*1000L; // 10s
70
71 @BeforeClass
72 public static void initClass() {
74 }
75
76 static enum CanvasType { GLCanvas_T, GLJPanel_T, NewtCanvasAWT_T };
77
78 static class GLADComp {
79 GLADComp(final GLAutoDrawable glad, final Component comp) {
80 this.glad = glad;
81 this.comp = comp;
82 }
83 final GLAutoDrawable glad;
84 final Component comp;
85 }
86 public void test(final GLCapabilitiesImmutable caps, final boolean useGears, final int width, final int height, final int rows,
87 final int columns, final CanvasType canvasType, final boolean useAnim) {
88 final GLAnimatorControl animator = useAnim ? new Animator() : null;
89
90 final JFrame frame;
91 final JPanel panel;
92 final List<NewtCanvasAWT> newtCanvasAWTList = new ArrayList<NewtCanvasAWT>();
93
94 panel = new JPanel();
95 frame = new JFrame(getSimpleTestName("."));
96
97 panel.setLayout(new GridLayout(rows, columns));
98 // panel.setBounds(0, 0, width, height);
99 final int panelCount = rows*columns;
100 final Dimension eSize = new Dimension(width/columns, height/rows);
101 final long[] t = new long[10];
102 if( wait ) {
103 UITestCase.waitForKey("Pre-Init");
104 }
105 System.err.println("INIT START");
106 initCount.set(0);
107 try {
108 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
109 public void run() {
110 t[0] = Platform.currentTimeMillis();
111 for(int i=0; i<panelCount; i++) {
112 final GLADComp gladComp;
113 switch(canvasType) {
114 case GLCanvas_T:
115 gladComp = createGLCanvas(caps, useGears, animator, eSize);
116 break;
117 case GLJPanel_T:
118 gladComp = createGLJPanel(caps, useGears, animator, eSize);
119 break;
120 case NewtCanvasAWT_T:
121 gladComp = createNewtCanvasAWT(caps, useGears, animator, eSize);
122 newtCanvasAWTList.add((NewtCanvasAWT)gladComp.comp);
123 break;
124 default: throw new InternalError("XXX");
125 }
126 gladComp.glad.addGLEventListener(new GLEventListener() {
127 @Override
128 public void init(final GLAutoDrawable drawable) {
129 initCount.getAndIncrement();
130 }
131 @Override
132 public void dispose(final GLAutoDrawable drawable) {}
133 @Override
134 public void display(final GLAutoDrawable drawable) {}
135 @Override
136 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {}
137 });
138 panel.add(gladComp.comp);
139 }
140 t[1] = Platform.currentTimeMillis();
141 frame.getContentPane().add(panel);
142
143 // frame.validate();
144 frame.pack();
145 frame.setVisible(true);
146 t[2] = Platform.currentTimeMillis();
147 } } );
148 } catch( final Throwable throwable ) {
149 throwable.printStackTrace();
150 Assume.assumeNoException( throwable );
151 }
152 final long t0 = System.currentTimeMillis();
153 long t1 = t0;
154 while( panelCount > initCount.get() && INIT_TIMEOUT > t1 - t0 ) {
155 try {
156 Thread.sleep(100);
157 System.err.println("Sleep initialized: "+initCount.get()+"/"+panelCount);
158 } catch (final InterruptedException e1) {
159 e1.printStackTrace();
160 }
161 t1 = System.currentTimeMillis();
162 }
163 t[3] = Platform.currentTimeMillis();
164 final double panelCountF = initCount.get();
165 System.err.printf("P: %d %s:%n\tctor\t%6d/t %6.2f/1%n\tvisible\t%6d/t %6.2f/1%n\tsum-i\t%6d/t %6.2f/1%n",
166 initCount.get(),
167 canvasType,
168 t[1]-t[0], (t[1]-t[0])/panelCountF,
169 t[3]-t[1], (t[3]-t[1])/panelCountF,
170 t[3]-t[0], (t[3]-t[0])/panelCountF);
171 System.err.println("INIT END: "+initCount.get()+"/"+panelCount);
172 if( wait ) {
173 UITestCase.waitForKey("Post-Init");
174 }
175 if( null != animator ) {
176 animator.start();
177 }
178 try {
179 Thread.sleep(duration);
180 } catch (final InterruptedException e1) {
181 e1.printStackTrace();
182 }
183 if( null != animator ) {
184 animator.stop();
185 }
186 t[4] = Platform.currentTimeMillis();
187 try {
188 SwingUtilities.invokeAndWait(new Runnable() {
189 public void run() {
190 while( !newtCanvasAWTList.isEmpty() ) {
191 newtCanvasAWTList.remove(0).destroy(); // removeNotify does not destroy GLWindow
192 }
193 frame.dispose();
194 } } );
195 } catch (final Exception e1) {
196 e1.printStackTrace();
197 }
198 final long ti_net = (t[4]-t[0])-duration;
199 System.err.printf("T: duration %d %d%n\ttotal-d\t%6d/t %6.2f/1%n\ttotal-i\t%6d/t %6.2f/1%n",
200 duration, t[4]-t[3],
201 t[4]-t[0], (t[4]-t[0])/panelCountF,
202 ti_net, ti_net/panelCountF);
203 System.err.println("Total: "+(t[4]-t[0]));
204 }
205
206 private GLADComp createNewtCanvasAWT(final GLCapabilitiesImmutable caps, final boolean useGears, final GLAnimatorControl anim, final Dimension size) {
207 final GLWindow window = GLWindow.create(caps);
208 final NewtCanvasAWT canvas = new NewtCanvasAWT(window);
209 canvas.setSize(size);
210 canvas.setPreferredSize(size);
211 if( useGears ) {
212 final GearsES2 g = new GearsES2(0);
213 g.setVerbose(false);
214 window.addGLEventListener(g);
215 }
216 if( null != anim ) {
217 anim.add(window);
218 }
219 return new GLADComp(window, canvas);
220 }
221 private GLADComp createGLCanvas(final GLCapabilitiesImmutable caps, final boolean useGears, final GLAnimatorControl anim, final Dimension size) {
222 final GLCanvas canvas = new GLCanvas(caps);
223 canvas.setSize(size);
224 canvas.setPreferredSize(size);
225 if( useGears ) {
226 canvas.addGLEventListener(new GearsES2(0));
227 }
228 if( null != anim ) {
229 anim.add(canvas);
230 }
231 return new GLADComp(canvas, canvas);
232 }
233 private GLADComp createGLJPanel(final GLCapabilitiesImmutable caps, final boolean useGears, final GLAnimatorControl anim, final Dimension size) {
234 final GLJPanel canvas = new GLJPanel(caps);
235 canvas.setSize(size);
236 canvas.setPreferredSize(size);
237 if( useGears ) {
238 canvas.addGLEventListener(new GearsES2(0));
239 }
240 if( null != anim ) {
241 anim.add(canvas);
242 }
243 return new GLADComp(canvas, canvas);
244 }
245
246 // @Test
247 public void test01NopGLJPanel() throws InterruptedException, InvocationTargetException {
248 test(new GLCapabilities(null), false /*useGears*/, width, height, rows, cols, CanvasType.GLJPanel_T, false /*useAnim*/);
249 }
250
251 // @Test
252 public void test02NopGLJPanelBMP() throws InterruptedException, InvocationTargetException {
253 final GLCapabilities caps = new GLCapabilities(null);
254 caps.setBitmap(true);
255 test(caps, false /*useGears*/, width, height, rows, cols, CanvasType.GLJPanel_T, false /*useAnim*/);
256 }
257
258 // @Test
259 public void test03NopGLCanvas() throws InterruptedException, InvocationTargetException {
260 test(new GLCapabilities(null), false /*useGears*/, width, height, rows, cols, CanvasType.GLCanvas_T, false /*useAnim*/);
261 }
262
263 // @Test
264 public void test11GearsGLJPanel() throws InterruptedException, InvocationTargetException {
265 test(new GLCapabilities(null), true /*useGears*/, width, height, rows, cols, CanvasType.GLJPanel_T, true /*useAnim*/);
266 }
267
268 // @Test
269 public void test13GearsGLCanvas() throws InterruptedException, InvocationTargetException {
270 test(new GLCapabilities(null), true /*useGears*/, width, height, rows, cols, CanvasType.GLCanvas_T, true /*useAnim*/);
271 }
272
273 @Test
274 public void test14GearsNewtCanvasAWT() throws InterruptedException, InvocationTargetException {
275 test(new GLCapabilities(null), true /*useGears*/, width, height, rows, cols, CanvasType.NewtCanvasAWT_T, true /*useAnim*/);
276 }
277
278 static long duration = 0; // ms
279 static boolean wait = false;
280 static int width = 800, height = 600, rows = 5, cols = 5;
281
282 AtomicInteger initCount = new AtomicInteger(0);
283
284 public static void main(final String[] args) {
285 CanvasType canvasType = CanvasType.GLJPanel_T;
286 boolean useGears = false, manual=false;
287 boolean waitMain = false;
288
289 for(int i=0; i<args.length; i++) {
290 if(args[i].equals("-time")) {
291 i++;
292 duration = MiscUtils.atol(args[i], duration);
293 } else if(args[i].equals("-width")) {
294 width = MiscUtils.atoi(args[++i], width);
295 } else if(args[i].equals("-height")) {
296 height = MiscUtils.atoi(args[++i], height);
297 } else if(args[i].equals("-rows")) {
298 rows = MiscUtils.atoi(args[++i], rows);
299 } else if(args[i].equals("-cols")) {
300 cols = MiscUtils.atoi(args[++i], cols);
301 } else if(args[i].equals("-type")) {
302 i++;
303 canvasType = CanvasType.valueOf(args[i]);
304 manual = true;
305 } else if(args[i].equals("-gears")) {
306 useGears = true;
307 } else if(args[i].equals("-wait")) {
308 wait = true;
309 manual = true;
310 } else if(args[i].equals("-waitMain")) {
311 waitMain = true;
312 manual = true;
313 } else if(args[i].equals("-manual")) {
314 manual = true;
315 }
316 }
317 if( waitMain ) {
318 UITestCase.waitForKey("Main-Start");
319 }
320 if( manual ) {
323 demo.test(null, useGears, width, height, rows, cols, canvasType, useGears /*useAnim*/);
324 } else {
325 org.junit.runner.JUnitCore.main(TestPerf001GLJPanelInit01AWT.class.getName());
326 }
327 }
328
329}
void setBitmap(final boolean enable)
Requesting offscreen bitmap mode.
AWT Canvas containing a NEWT Window using native parenting.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
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 void initSingleton()
Static initialization of JOGL.
Definition: GLProfile.java:204
Tests multiple [GLJPanels, GLCanvas or NewtCanvasAWT] in a JFrame's Grid.
void test(final GLCapabilitiesImmutable caps, final boolean useGears, final int width, final int height, final int rows, final int columns, final CanvasType canvasType, final boolean useAnim)
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
An animator control interface, which implementation may drive a com.jogamp.opengl....
boolean start()
Starts this animator, if not running.
boolean stop()
Stops this animator.
void add(GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Specifies an immutable set of OpenGL capabilities.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.