JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGLAutoDrawableGLCanvasOnOffscrnCapsAWT.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.acore;
30
31import java.awt.Dimension;
32import java.awt.Frame;
33import java.io.IOException;
34
35import com.jogamp.junit.util.JunitTracer;
36import com.jogamp.nativewindow.CapabilitiesImmutable;
37import com.jogamp.opengl.GLAutoDrawable;
38import com.jogamp.opengl.GLCapabilities;
39import com.jogamp.opengl.GLCapabilitiesImmutable;
40import com.jogamp.opengl.GLContext;
41import com.jogamp.opengl.GLDrawable;
42import com.jogamp.opengl.GLDrawableFactory;
43import com.jogamp.opengl.GLEventListener;
44import com.jogamp.opengl.GLProfile;
45import com.jogamp.opengl.awt.GLCanvas;
46
47import jogamp.nativewindow.jawt.JAWTUtil;
48import jogamp.opengl.GLGraphicsConfigurationUtil;
49
50import org.junit.Assert;
51import org.junit.Assume;
52import org.junit.Test;
53import org.junit.FixMethodOrder;
54import org.junit.runners.MethodSorters;
55
56import com.jogamp.opengl.JoglVersion;
57import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
58import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
59import com.jogamp.opengl.test.junit.util.GLTestUtil;
60import com.jogamp.opengl.test.junit.util.UITestCase;
61
62/**
63 * Tests using an AWT {@link GLCanvas} {@link GLAutoDrawable auto drawable} for on- and offscreen cases.
64 */
65@FixMethodOrder(MethodSorters.NAME_ASCENDING)
67 static final int widthStep = 800/4;
68 static final int heightStep = 600/4;
69 static boolean waitForKey = false;
70 volatile int szStep = 2;
71
72 static GLCapabilities getCaps(final String profile) {
73 if( !GLProfile.isAvailable(profile) ) {
74 System.err.println("Profile "+profile+" n/a");
75 return null;
76 }
77 return new GLCapabilities(GLProfile.get(profile));
78 }
79
80 static void setGLCanvasSize(final Frame frame, final GLCanvas glc, final int width, final int height) {
81 try {
82 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
83 public void run() {
84 final Dimension new_sz = new Dimension(width, height);
85 glc.setMinimumSize(new_sz);
86 glc.setPreferredSize(new_sz);
87 glc.setSize(new_sz);
88 frame.pack();
89 frame.validate();
90 } } );
91 } catch( final Throwable throwable ) {
92 throwable.printStackTrace();
93 Assume.assumeNoException( throwable );
94 }
95 }
96
97 static interface MyGLEventListener extends GLEventListener {
98 void setMakeSnapshot();
99 }
100
101 void doTest(final GLCapabilitiesImmutable reqGLCaps, final GLEventListener demo) throws InterruptedException {
102 if(reqGLCaps.isOnscreen() && JAWTUtil.isOffscreenLayerRequired()) {
103 System.err.println("onscreen layer n/a");
104 return;
105 }
106 if(!reqGLCaps.isOnscreen() && !JAWTUtil.isOffscreenLayerSupported()) {
107 System.err.println("offscreen layer n/a");
108 return;
109 }
110 System.out.println("Requested GL Caps: "+reqGLCaps);
111 final GLDrawableFactory factory = GLDrawableFactory.getFactory(reqGLCaps.getGLProfile());
112 final GLCapabilitiesImmutable expGLCaps = GLGraphicsConfigurationUtil.fixGLCapabilities(reqGLCaps, factory, null);
113 System.out.println("Expected GL Caps: "+expGLCaps);
114 //
115 // Create native windowing resources .. X11/Win/OSX
116 //
117 final GLCanvas glad = new GLCanvas(reqGLCaps); // will implicit trigger offscreen layer - if !onscreen && supported
118 Assert.assertNotNull(glad);
119 final Dimension glc_sz = new Dimension(widthStep*szStep, heightStep*szStep);
120 glad.setMinimumSize(glc_sz);
121 glad.setPreferredSize(glc_sz);
122 glad.setSize(glc_sz);
123 final Frame frame = new Frame(getSimpleTestName("."));
124 Assert.assertNotNull(frame);
125 frame.add(glad);
126
127 try {
128 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
129 public void run() {
130 frame.pack();
131 frame.setVisible(true);
132 }});
133 } catch( final Throwable throwable ) {
134 throwable.printStackTrace();
135 Assume.assumeNoException( throwable );
136 }
137
138 Assert.assertTrue(AWTRobotUtil.waitForVisible(glad, true, null));
139 Assert.assertTrue(GLTestUtil.waitForRealized(glad, true, null));
140 System.out.println("Window: "+glad.getClass().getName());
141
142 // Check caps of NativeWindow config w/o GL
143 final CapabilitiesImmutable chosenCaps = glad.getChosenGLCapabilities();
144 System.out.println("Window Caps Pre_GL: "+chosenCaps);
145 Assert.assertNotNull(chosenCaps);
146 Assert.assertTrue(chosenCaps.getGreenBits()>5);
147 Assert.assertTrue(chosenCaps.getBlueBits()>5);
148 Assert.assertTrue(chosenCaps.getRedBits()>5);
149
150 glad.display(); // force native context creation
151
152 //
153 // Create native OpenGL resources .. XGL/WGL/CGL ..
154 // equivalent to GLAutoDrawable methods: setVisible(true)
155 //
156 {
157 final GLDrawable actualDrawable = glad.getDelegatedDrawable();
158 Assert.assertNotNull(actualDrawable);
159 System.out.println("Drawable Pre-GL(0): "+actualDrawable.getClass().getName()+", "+actualDrawable.getNativeSurface().getClass().getName());
160 }
161
162 System.out.println("Window Caps PostGL : "+glad.getChosenGLCapabilities());
163 System.out.println("Drawable Post-GL(1): "+glad.getClass().getName()+", "+glad.getNativeSurface().getClass().getName());
164
165 // Check caps of GLDrawable after realization
166 final GLCapabilitiesImmutable chosenGLCaps = glad.getChosenGLCapabilities();
167 System.out.println("Chosen GL Caps(1): "+chosenGLCaps);
168 Assert.assertNotNull(chosenGLCaps);
169 Assert.assertTrue(chosenGLCaps.getGreenBits()>5);
170 Assert.assertTrue(chosenGLCaps.getBlueBits()>5);
171 Assert.assertTrue(chosenGLCaps.getRedBits()>5);
172 Assert.assertTrue(chosenGLCaps.getDepthBits()>4);
173 Assert.assertEquals(expGLCaps.isOnscreen(), chosenGLCaps.isOnscreen());
174 Assert.assertEquals(expGLCaps.isFBO(), chosenGLCaps.isFBO());
175 Assert.assertEquals(expGLCaps.isPBuffer(), chosenGLCaps.isPBuffer());
176 Assert.assertEquals(expGLCaps.isBitmap(), chosenGLCaps.isBitmap());
177 /** Single/Double buffer cannot be checked since result may vary ..
178 if(chosenGLCaps.isOnscreen() || chosenGLCaps.isFBO()) {
179 // dbl buffer may be disabled w/ offscreen pbuffer and bitmap
180 Assert.assertEquals(expGLCaps.getDoubleBuffered(), chosenGLCaps.getDoubleBuffered());
181 } */
182
183 {
184 final GLContext context = glad.getContext();
185 System.out.println("Chosen GL CTX (2): "+context.getGLVersion());
186 Assert.assertNotNull(context);
187 Assert.assertTrue(context.isCreated());
188 }
189
190 System.out.println("Chosen GL Caps(2): "+glad.getChosenGLCapabilities());
191 System.out.println("Drawable Post-GL(2): "+glad.getClass().getName()+", "+glad.getNativeSurface().getClass().getName());
192
193 glad.addGLEventListener(demo);
194
195 final SnapshotGLEventListener snapshotGLEventListener = new SnapshotGLEventListener();
196 glad.addGLEventListener(snapshotGLEventListener);
197
198 glad.display(); // initial resize/display
199
200 // 1 - szStep = 2
201 final int[] expSurfaceSize = glad.getNativeSurface().convertToPixelUnits(new int[] { widthStep*szStep, heightStep*szStep });
202 Assert.assertTrue("Surface Size not reached: Expected "+expSurfaceSize[0]+"x"+expSurfaceSize[1]+", Is "+glad.getSurfaceWidth()+"x"+glad.getSurfaceHeight(),
203 GLTestUtil.waitForSize(glad, expSurfaceSize[0], expSurfaceSize[1], null));
204 snapshotGLEventListener.setMakeSnapshot();
205 glad.display();
206
207 // 2, 3 (resize + display)
208 szStep = 1;
209 setGLCanvasSize(frame, glad, widthStep*szStep, heightStep*szStep);
210 expSurfaceSize[0] = widthStep*szStep;
211 expSurfaceSize[1] = heightStep*szStep;
212 glad.getNativeSurface().convertToPixelUnits(expSurfaceSize);
213 Assert.assertTrue("Surface Size not reached: Expected "+expSurfaceSize[0]+"x"+expSurfaceSize[1]+", Is "+glad.getSurfaceWidth()+"x"+glad.getSurfaceHeight(),
214 GLTestUtil.waitForSize(glad, expSurfaceSize[0], expSurfaceSize[1], null));
215 glad.display();
216 snapshotGLEventListener.setMakeSnapshot();
217 glad.display();
218
219 // 4, 5 (resize + display)
220 szStep = 4;
221 setGLCanvasSize(frame, glad, widthStep*szStep, heightStep*szStep);
222 expSurfaceSize[0] = widthStep*szStep;
223 expSurfaceSize[1] = heightStep*szStep;
224 glad.getNativeSurface().convertToPixelUnits(expSurfaceSize);
225 Assert.assertTrue("Surface Size not reached: Expected "+expSurfaceSize[0]+"x"+expSurfaceSize[1]+", Is "+glad.getSurfaceWidth()+"x"+glad.getSurfaceHeight(),
226 GLTestUtil.waitForSize(glad, expSurfaceSize[0], expSurfaceSize[1], null));
227 glad.display();
228 snapshotGLEventListener.setMakeSnapshot();
229 glad.display();
230
231 Thread.sleep(50);
232
233 try {
234 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
235 public void run() {
236 frame.setVisible(false);
237 frame.remove(glad);
238 frame.dispose();
239 }});
240 } catch( final Throwable throwable ) {
241 throwable.printStackTrace();
242 Assume.assumeNoException( throwable );
243 }
244 System.out.println("Fin: "+glad);
245 }
246
247 @Test
248 public void testGL2OnScreenDblBuf() throws InterruptedException {
249 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
250 if(null == reqGLCaps) return;
251 doTest(reqGLCaps, new GearsES2(1));
252 }
253
254 @Test
255 public void testGL2OnScreenDblBufStencil() throws InterruptedException {
256 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
257 if(null == reqGLCaps) return;
258 reqGLCaps.setStencilBits(1);
259 doTest(reqGLCaps, new GearsES2(1));
260 }
261
262 @Test
263 public void testGL2OnScreenDblBufMSAA() throws InterruptedException {
264 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
265 if(null == reqGLCaps) return;
266 reqGLCaps.setSampleBuffers(true);
267 reqGLCaps.setNumSamples(4);
268 doTest(reqGLCaps, new GearsES2(1));
269 }
270
271 @Test
272 public void testGL2OnScreenDblBufStencilMSAA() throws InterruptedException {
273 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
274 if(null == reqGLCaps) return;
275 reqGLCaps.setStencilBits(1);
276 reqGLCaps.setSampleBuffers(true);
277 reqGLCaps.setNumSamples(4);
278 doTest(reqGLCaps, new GearsES2(1));
279 }
280
281 @Test
282 public void testGL2OffScreenAutoDblBuf() throws InterruptedException {
283 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
284 if(null == reqGLCaps) return;
285 reqGLCaps.setOnscreen(false);
286 doTest(reqGLCaps, new GearsES2(1));
287 }
288
289 @Test
290 public void testGL2OffScreenFBODblBufStencil() throws InterruptedException {
291 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
292 if(null == reqGLCaps) return;
293 reqGLCaps.setOnscreen(false);
294 reqGLCaps.setFBO(true);
295 reqGLCaps.setStencilBits(1);
296 doTest(reqGLCaps, new GearsES2(1));
297 }
298
299 @Test
300 public void testGL2OffScreenFBODblBufMSAA() throws InterruptedException {
301 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
302 if(null == reqGLCaps) return;
303 reqGLCaps.setOnscreen(false);
304 reqGLCaps.setFBO(true);
305 reqGLCaps.setSampleBuffers(true);
306 reqGLCaps.setNumSamples(4);
307 doTest(reqGLCaps, new GearsES2(1));
308 }
309
310 @Test
311 public void testGL2OffScreenFBODblBufStencilMSAA() throws InterruptedException {
312 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
313 if(null == reqGLCaps) return;
314 reqGLCaps.setOnscreen(false);
315 reqGLCaps.setFBO(true);
316 reqGLCaps.setStencilBits(1);
317 reqGLCaps.setSampleBuffers(true);
318 reqGLCaps.setNumSamples(4);
319 doTest(reqGLCaps, new GearsES2(1));
320 }
321
322 @Test
323 public void testGL2OffScreenPbuffer() throws InterruptedException {
324 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
325 if(null == reqGLCaps) return;
326 reqGLCaps.setOnscreen(false);
327 reqGLCaps.setPBuffer(true);
328 doTest(reqGLCaps, new GearsES2(1));
329 }
330
331 public static void main(final String args[]) throws IOException {
332 for(int i=0; i<args.length; i++) {
333 if(args[i].equals("-wait")) {
334 waitForKey = true;
335 }
336 }
337 if(waitForKey) {
338 JunitTracer.waitForKey("Start");
339 }
340 org.junit.runner.JUnitCore.main(TestGLAutoDrawableGLCanvasOnOffscrnCapsAWT.class.getName());
341 }
342
343}
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.
void setStencilBits(final int stencilBits)
Sets the number of bits requested for the stencil buffer.
void setFBO(final boolean enable)
Requesting offscreen FBO 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.
Abstraction for an OpenGL rendering context.
Definition: GLContext.java:74
final boolean isCreated()
Indicates whether the underlying native OpenGL context has been created.
Definition: GLContext.java:604
final String getGLVersion()
Returns a valid OpenGL version string, ie
Definition: GLContext.java:769
static GLDrawableFactory getFactory(final GLProfile glProfile)
Returns the sole GLDrawableFactory instance.
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 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
GLContext getContext()
Returns the context associated with this drawable.
Definition: GLCanvas.java:1166
final GLDrawable getDelegatedDrawable()
If the implementation uses delegation, return the delegated GLDrawable instance, otherwise return thi...
Definition: GLCanvas.java:1161
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Definition: GLCanvas.java:1248
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLCanvas.java:1243
NativeSurface getNativeSurface()
Returns the associated NativeSurface of this NativeSurfaceHolder.Returns the underlying NativeSurface...
Definition: GLCanvas.java:1259
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
static boolean waitForVisible(final java.awt.Component comp, final boolean visible, final Runnable waitAction)
static boolean waitForRealized(final GLAutoDrawable glad, final boolean realized, final Runnable waitAction)
Definition: GLTestUtil.java:91
static boolean waitForSize(final GLDrawable drawable, final int width, final int height, final Runnable waitAction)
Definition: GLTestUtil.java:72
Specifies an immutable set of capabilities that a window's rendering context must support,...
int getBlueBits()
Returns the number of bits for the color buffer's blue component.
boolean isBitmap()
Returns whether bitmap offscreen mode is requested, available or chosen.
int getRedBits()
Returns the number of bits for the color buffer's red component.
int getGreenBits()
Returns the number of bits for the color buffer's green component.
boolean isOnscreen()
Returns whether an on- or offscreen surface is requested, available or chosen.
int[] convertToPixelUnits(final int[] windowUnitsAndResult)
Converts the given window units into pixel units in place.
Specifies an immutable set of OpenGL capabilities.
int getDepthBits()
Returns the number of depth buffer bits.
boolean isPBuffer()
Returns whether pbuffer offscreen mode is requested, available or chosen.
GLProfile getGLProfile()
Returns the GL profile you desire or used by the drawable.
boolean isFBO()
Returns whether FBO offscreen mode is requested, available or chosen.
An abstraction for an OpenGL rendering target.
Definition: GLDrawable.java:51
NativeSurface getNativeSurface()
Returns the associated NativeSurface of this NativeSurfaceHolder.
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.