JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGearsES2GLJPanelsAWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 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.demos.es2.awt;
30
31import java.awt.AWTException;
32import java.awt.BorderLayout;
33import java.awt.Component;
34import java.awt.Container;
35import java.awt.event.ComponentAdapter;
36import java.awt.event.ComponentEvent;
37import java.lang.reflect.InvocationTargetException;
38import java.nio.FloatBuffer;
39
40import com.jogamp.opengl.GLAnimatorControl;
41import com.jogamp.opengl.GLCapabilities;
42import com.jogamp.opengl.GLCapabilitiesImmutable;
43import com.jogamp.opengl.GLEventListener;
44import com.jogamp.opengl.GLProfile;
45import com.jogamp.opengl.awt.GLJPanel;
46import javax.swing.JComponent;
47import javax.swing.JFrame;
48import javax.swing.JPanel;
49import javax.swing.JTextField;
50import javax.swing.SwingUtilities;
51
52import org.junit.AfterClass;
53import org.junit.Assert;
54import org.junit.BeforeClass;
55import org.junit.Test;
56import org.junit.FixMethodOrder;
57import org.junit.runners.MethodSorters;
58
59import com.jogamp.common.nio.Buffers;
60import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
61import com.jogamp.opengl.test.junit.jogl.demos.gl2.Gears;
62import com.jogamp.opengl.test.junit.util.MiscUtils;
63import com.jogamp.opengl.test.junit.util.QuitAdapter;
64import com.jogamp.opengl.test.junit.util.UITestCase;
65import com.jogamp.opengl.util.FPSAnimator;
66import com.jogamp.opengl.util.awt.AWTGLPixelBuffer.SingleAWTGLPixelBufferProvider;
67
68@FixMethodOrder(MethodSorters.NAME_ASCENDING)
70 static int demoCount = 4;
71 static boolean jOpaque = false; // flicker-less w/o opaque, opaque leads to overdraw w/ mixed clipRects -> flicker - due to JComponent _paintImmediately(..) (?)
72 static boolean glOpaque = true; // can be either ..
73 static float glAlpha = 0.3f;
74 static boolean jZOrder = false;
75 static GLProfile glp;
76 static boolean shallUsePBuffer = false;
77 static boolean shallUseBitmap = false;
78 static boolean useMSAA = false;
79 static int swapInterval = 0;
80 static boolean useAnimator = true;
81 static boolean manualTest = false;
82 static boolean initSingleBuffer = false;
83
84 /**
85 * Even though GLJPanel uses a SingleAWTGLPixelBufferProvider per default,
86 * we like to initialize it's size to a common maximum to ensure
87 * only one {@link AWTGLPixelBuffer} gets allocated.
88 */
89 static SingleAWTGLPixelBufferProvider singleAWTGLPixelBufferProvider;
90
91 @BeforeClass
92 public static void initClass() {
95 Assert.assertNotNull(glp);
96 } else {
97 setTestSupported(false);
98 }
99
100 if( initSingleBuffer ) {
101 singleAWTGLPixelBufferProvider = new SingleAWTGLPixelBufferProvider( glp.isGL2ES3() /* allowRowStride */);
102 singleAWTGLPixelBufferProvider.initSingleton(null, 4, true, 600, 600, 1);
103 } else {
104 singleAWTGLPixelBufferProvider = null;
105 }
106 }
107
108 @AfterClass
109 public static void releaseClass() {
110 }
111
112 final static boolean useInterPanel = true;
113
114 /** Adds new JPanel to frame's content pane at index 0 */
115 private JComponent addPanel(final GLCapabilitiesImmutable caps, final GLAnimatorControl anim, final JFrame frame, final boolean opaque, final int x, final int y, final int w, final int h, final FloatBuffer color, final float[] clearColor)
116 throws InterruptedException, InvocationTargetException
117 {
118 final GLJPanel canvas = new GLJPanel(caps);
119 if( initSingleBuffer ) {
120 canvas.setPixelBufferProvider( singleAWTGLPixelBufferProvider );
121 }
122 canvas.setOpaque(opaque);
123 if ( !useInterPanel ) {
124 canvas.setBounds(x, y, w, h);
125 }
126 final GLEventListener demo;
127 if( caps.isBitmap() ) {
128 demo = new Gears(swapInterval);
129 } else {
130 final GearsES2 gdemo = new GearsES2(swapInterval);
131 gdemo.setIgnoreFocus(true);
132 gdemo.setGearsColors(color, color, color);
133 gdemo.setClearColor(clearColor);
134 demo = gdemo;
135 }
136 canvas.addGLEventListener(demo);
137 if( null != anim ) {
138 anim.add(canvas);
139 }
140
141 final JPanel panel;
142 final JTextField text;
143 if ( useInterPanel ) {
144 panel = new JPanel(new BorderLayout());
145 panel.setBounds(x, y, w, h);
146 panel.setOpaque(opaque);
147 text = new JTextField(x+"/"+y+" "+w+"x"+h);
148 text.setOpaque(true);
149 } else {
150 panel = null;
151 text = null;
152 }
153
154 SwingUtilities.invokeAndWait(new Runnable() {
155 public void run() {
156 if ( useInterPanel ) {
157 panel.add(text, BorderLayout.NORTH);
158 panel.add(canvas, BorderLayout.CENTER);
159 frame.getContentPane().add(panel, 0);
160 } else {
161 frame.getContentPane().add(canvas, 0);
162 }
163 } } ) ;
164 return useInterPanel ? panel : canvas;
165 }
166
167 public static final FloatBuffer red = Buffers.newDirectFloatBuffer( new float[] { 1.0f, 0.0f, 0.0f, 1.0f } );
168 public static final FloatBuffer green = Buffers.newDirectFloatBuffer( new float[] { 0.0f, 1.0f, 0.0f, 1.0f } );
169 public static final FloatBuffer blue = Buffers.newDirectFloatBuffer( new float[] { 0.0f, 0.0f, 1.0f, 1.0f } );
170 public static final FloatBuffer yellow = Buffers.newDirectFloatBuffer( new float[] { 1.0f, 1.0f, 0.0f, 1.0f } );
171 public static final FloatBuffer grey = Buffers.newDirectFloatBuffer( new float[] { 0.5f, 0.5f, 0.5f, 1.0f } );
172 public static final float grayf = 0.3f;
173 public static final float[] redish = new float[] { grayf, 0.0f, 0.0f, glAlpha };
174 public static final float[] greenish = new float[] { 0.0f, grayf, 0.0f, glAlpha };
175 public static final float[] blueish = new float[] { 0.0f, 0.0f, grayf, glAlpha };
176 public static final float[] yellowish = new float[] { grayf, grayf, 0.0f, glAlpha };
177 public static final float[] greyish = new float[] { grayf, grayf, grayf, glAlpha };
178
179 protected void relayout(final Container cont, final float oW, final float oH) {
180 final int count = cont.getComponentCount();
181 final int nW = cont.getWidth();
182 final int nH = cont.getHeight();
183 for(int i = 0 ; i < count; i++ ) {
184 final Component comp = cont.getComponent(i);
185 final float fx = comp.getX() / oW;
186 final float fy = comp.getY() / oH;
187 final float fw = comp.getWidth() / oW;
188 final float fh = comp.getHeight() / oH;
189 comp.setBounds( (int)(fx * nW), (int)(fy * nH), (int)(fw * nW), (int)(fh * nH) );
190 }
191 }
192
193 protected void runTestGL(final GLCapabilities caps)
194 throws AWTException, InterruptedException, InvocationTargetException
195 {
196 if( !glOpaque ) {
197 caps.setAlphaBits(caps.getRedBits());
198 }
199
200 final JFrame frame = new JFrame("Swing GLJPanel");
201 Assert.assertNotNull(frame);
202
203 final FPSAnimator animator = useAnimator ? new FPSAnimator(60) : null;
204
205 SwingUtilities.invokeAndWait(new Runnable() {
206 public void run() {
207 frame.getContentPane().setLayout(null);
208 } } );
209
210 final float[] oldSize = new float[] { 600f, 600f };
211
212 frame.addComponentListener(new ComponentAdapter() {
213 @Override
214 public void componentResized(final ComponentEvent e) {
215 final int count = frame.getComponentCount();
216 for(int i = 0 ; i < count; i++ ) {
217 relayout(frame.getContentPane(), oldSize[0], oldSize[1]);
218 }
219 frame.getContentPane().invalidate();
220 frame.getContentPane().validate();
221 // frame.pack();
222 oldSize[0] = frame.getContentPane().getWidth();
223 oldSize[1] = frame.getContentPane().getHeight();
224 }
225 } ) ;
226
227 if( demoCount > 0 ) {
228 addPanel(caps, animator, frame, jOpaque, 50, 50, 300, 300, red, redish); // A
229 }
230 if( demoCount > 1 ) {
231 addPanel(caps, animator, frame, jOpaque, 0, 250, 300, 300, blue, blueish); // C
232 }
233 if( demoCount > 2 ) {
234 addPanel(caps, animator, frame, jOpaque, 300, 0, 150, 150, green, greenish); // B
235 }
236 if( demoCount > 3 ) {
237 addPanel(caps, animator, frame, jOpaque, 300, 300, 100, 100, yellow, yellowish); // D
238 }
239 if( jZOrder ) {
240 final Container cont = frame.getContentPane();
241 final int count = cont.getComponentCount();
242 for(int i = 0 ; i < count; i++ ) {
243 cont.setComponentZOrder(cont.getComponent(i), count - 1 - i);
244 }
245 }
246
247 SwingUtilities.invokeAndWait(new Runnable() {
248 public void run() {
249 frame.setSize((int)oldSize[0], (int)oldSize[1]);
250 frame.getContentPane().validate();
251 // frame.pack();
252 frame.setVisible(true);
253 } } ) ;
254
255 if( useAnimator ) {
256 animator.setUpdateFPSFrames(60, System.err);
257 animator.start();
258 Assert.assertEquals(true, animator.isAnimating());
259 }
260
261 final QuitAdapter quitAdapter = new QuitAdapter();
262
263 final long t0 = System.currentTimeMillis();
264 long t1 = t0;
265 while(!quitAdapter.shouldQuit() && t1 - t0 < duration) {
266 Thread.sleep(100);
267 t1 = System.currentTimeMillis();
268 }
269
270 Assert.assertNotNull(frame);
271 Assert.assertNotNull(animator);
272
273 if( useAnimator ) {
274 animator.stop();
275 Assert.assertEquals(false, animator.isAnimating());
276 }
277 SwingUtilities.invokeAndWait(new Runnable() {
278 public void run() {
279 frame.setVisible(false);
280 // frame.getContentPane().removeAll();
281 // frame.removeAll();
282 frame.dispose();
283 } } );
284 }
285
286 @Test
287 public void test01_DefaultNorm()
288 throws AWTException, InterruptedException, InvocationTargetException
289 {
291 if(useMSAA) {
292 caps.setNumSamples(4);
293 caps.setSampleBuffers(true);
294 }
295 if(shallUsePBuffer) {
296 caps.setPBuffer(true);
297 }
298 if(shallUseBitmap) {
299 caps.setBitmap(true);
300 }
301 runTestGL(caps);
302 }
303
304 @Test
305 public void test02_DefaultMsaa()
306 throws AWTException, InterruptedException, InvocationTargetException
307 {
308 if( manualTest ) {
309 return;
310 }
312 caps.setNumSamples(4);
313 caps.setSampleBuffers(true);
314 runTestGL(caps);
315 }
316
317 @Test
318 public void test03_PbufferNorm()
319 throws AWTException, InterruptedException, InvocationTargetException
320 {
321 if( manualTest ) {
322 return;
323 }
325 caps.setPBuffer(true);
326 runTestGL(caps);
327 }
328
329 @Test
330 public void test04_PbufferMsaa()
331 throws AWTException, InterruptedException, InvocationTargetException
332 {
333 if( manualTest ) {
334 return;
335 }
337 caps.setNumSamples(4);
338 caps.setSampleBuffers(true);
339 caps.setPBuffer(true);
340 runTestGL(caps);
341 }
342
343 @Test
344 public void test05_BitmapNorm()
345 throws AWTException, InterruptedException, InvocationTargetException
346 {
347 if( manualTest ) {
348 return;
349 }
351 caps.setBitmap(true);
352 runTestGL(caps);
353 }
354
355 @Test
356 public void test06_BitmapMsaa()
357 throws AWTException, InterruptedException, InvocationTargetException
358 {
359 if( manualTest ) {
360 return;
361 }
363 caps.setNumSamples(4);
364 caps.setSampleBuffers(true);
365 caps.setBitmap(true);
366 runTestGL(caps);
367 }
368
369 static long duration = 500; // ms
370
371 public static void main(final String args[]) {
372 for(int i=0; i<args.length; i++) {
373 if(args[i].equals("-time")) {
374 i++;
375 duration = MiscUtils.atol(args[i], duration);
376 } else if(args[i].equals("-vsync")) {
377 i++;
378 swapInterval = MiscUtils.atoi(args[i], swapInterval);
379 } else if(args[i].equals("-msaa")) {
380 useMSAA = true;
381 } else if(args[i].equals("-jOpaque")) {
382 i++;
383 jOpaque = MiscUtils.atob(args[i], jOpaque);
384 } else if(args[i].equals("-glOpaque")) {
385 i++;
386 glOpaque = MiscUtils.atob(args[i], glOpaque);
387 } else if(args[i].equals("-alpha")) {
388 i++;
389 glAlpha = MiscUtils.atof(args[i], glAlpha);
390 } else if(args[i].equals("-initSingleBuffer")) {
391 i++;
392 initSingleBuffer = MiscUtils.atob(args[i], initSingleBuffer);
393 } else if(args[i].equals("-jZOrder")) {
394 jZOrder = true;
395 } else if(args[i].equals("-noanim")) {
396 useAnimator = false;
397 } else if(args[i].equals("-pbuffer")) {
398 shallUsePBuffer = true;
399 } else if(args[i].equals("-bitmap")) {
400 shallUseBitmap = true;
401 } else if(args[i].equals("-manual")) {
402 manualTest = true;
403 } else if(args[i].equals("-demos")) {
404 i++;
405 demoCount = MiscUtils.atoi(args[i], demoCount);
406 }
407 }
408 System.err.println("swapInterval "+swapInterval);
409 System.err.println("opaque gl "+glOpaque+", java/gljpanel "+jOpaque);
410 System.err.println("alpha "+glAlpha);
411 System.err.println("jZOrder "+jZOrder);
412 System.err.println("demos "+demoCount);
413 System.err.println("useMSAA "+useMSAA);
414 System.err.println("useAnimator "+useAnimator);
415 System.err.println("shallUsePBuffer "+shallUsePBuffer);
416 System.err.println("shallUseBitmap "+shallUseBitmap);
417 System.err.println("manualTest "+manualTest);
418 System.err.println("useSingleBuffer "+initSingleBuffer);
419
420 org.junit.runner.JUnitCore.main(TestGearsES2GLJPanelsAWT.class.getName());
421 }
422}
void setBitmap(final boolean enable)
Requesting offscreen bitmap mode.
Specifies a set of OpenGL capabilities.
void setPBuffer(final boolean enable)
Requesting offscreen pbuffer mode.
void setNumSamples(final int numSamples)
If sample buffers are enabled, indicates the number of buffers to be allocated.
void setSampleBuffers(final boolean enable)
Defaults to false.
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 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.
static final String GL2
The desktop OpenGL profile 1.x up to 3.0.
Definition: GLProfile.java:579
final boolean isGL2ES3()
Indicates whether this profile is capable of GL2ES3.
A lightweight Swing component which provides OpenGL rendering support.
Definition: GLJPanel.java:189
void setPixelBufferProvider(final AWTGLPixelBufferProvider custom)
Definition: GLJPanel.java:441
void setOpaque(final boolean opaque)
Definition: GLJPanel.java:981
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLJPanel.java:989
void relayout(final Container cont, final float oW, final float oH)
static float atof(final String str, final float def)
Definition: MiscUtils.java:75
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
static boolean atob(final String str, final boolean def)
Definition: MiscUtils.java:48
final void setUpdateFPSFrames(final int frames, final PrintStream out)
An Animator subclass which attempts to achieve a target frames-per-second rate to avoid using all CPU...
final synchronized boolean start()
Starts this animator, if not running.
final synchronized boolean stop()
Stops this FPSAnimator.
AWTGLPixelBuffer initSingleton(final GLProfile glp, final int componentCount, final boolean pack, final int width, final int height, final int depth)
Initializes the single AWTGLPixelBuffer w/ a given size, if not yet allocated.
An animator control interface, which implementation may drive a com.jogamp.opengl....
Specifies an immutable set of OpenGL capabilities.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.