JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGLAutoDrawableNewtCanvasAWTOnOffscrnCapsAWT.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.Component;
32import java.awt.Dimension;
33import java.awt.Frame;
34import java.io.IOException;
35
36import com.jogamp.nativewindow.AbstractGraphicsDevice;
37import com.jogamp.nativewindow.CapabilitiesImmutable;
38import com.jogamp.opengl.GLAutoDrawable;
39import com.jogamp.opengl.GLCapabilities;
40import com.jogamp.opengl.GLCapabilitiesImmutable;
41import com.jogamp.opengl.GLContext;
42import com.jogamp.opengl.GLDrawable;
43import com.jogamp.opengl.GLDrawableFactory;
44import com.jogamp.opengl.GLEventListener;
45import com.jogamp.opengl.GLProfile;
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.newt.awt.NewtCanvasAWT;
57import com.jogamp.newt.opengl.GLWindow;
58import com.jogamp.opengl.JoglVersion;
59import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
60import com.jogamp.opengl.test.junit.util.NewtTestUtil;
61import com.jogamp.opengl.test.junit.util.GLTestUtil;
62import com.jogamp.opengl.test.junit.util.UITestCase;
63
64/**
65 * Tests using a NEWT {@link GLWindow} {@link GLAutoDrawable auto drawable} for on- and offscreen cases.
66 * <p>
67 * The NEWT {@link GLAutoDrawable} is being used to run the {@link GLEventListener}.
68 * </p>
69 */
70@FixMethodOrder(MethodSorters.NAME_ASCENDING)
72 static final int widthStep = 800/4;
73 static final int heightStep = 600/4;
74 volatile int szStep = 2;
75
76 static GLCapabilities getCaps(final String profile) {
77 if( !GLProfile.isAvailable(profile) ) {
78 System.err.println("Profile "+profile+" n/a");
79 return null;
80 }
81 return new GLCapabilities(GLProfile.get(profile));
82 }
83
84 static void setComponentSize(final Frame frame, final Component comp, final int width, final int height) {
85 try {
86 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
87 public void run() {
88 final Dimension new_sz = new Dimension(width, height);
89 comp.setMinimumSize(new_sz);
90 comp.setPreferredSize(new_sz);
91 comp.setSize(new_sz);
92 frame.pack();
93 frame.validate();
94 } } );
95 } catch( final Throwable throwable ) {
96 throwable.printStackTrace();
97 Assume.assumeNoException( throwable );
98 }
99 }
100
101 static interface MyGLEventListener extends GLEventListener {
102 void setMakeSnapshot();
103 }
104
105 void doTest(final boolean offscreenLayer, final GLCapabilitiesImmutable reqGLCaps, final GLEventListener demo) throws InterruptedException {
106 if(!offscreenLayer && JAWTUtil.isOffscreenLayerRequired()) {
107 System.err.println("onscreen layer n/a");
108 return;
109 }
110 if(offscreenLayer && !JAWTUtil.isOffscreenLayerSupported()) {
111 System.err.println("offscreen layer n/a");
112 return;
113 }
114 System.out.println("Requested GL Caps: "+reqGLCaps);
115 final GLDrawableFactory factory = GLDrawableFactory.getFactory(reqGLCaps.getGLProfile());
116 final AbstractGraphicsDevice device = factory.getDefaultDevice();
117 final GLCapabilitiesImmutable expGLCaps = offscreenLayer ?
118 GLGraphicsConfigurationUtil.fixOffscreenGLCapabilities(reqGLCaps, factory, device) :
119 GLGraphicsConfigurationUtil.fixGLCapabilities(reqGLCaps, factory, device);
120 System.out.println("Expected GL Caps: "+expGLCaps);
121
122 final GLWindow glad = GLWindow.create(reqGLCaps);
123 Assert.assertNotNull(glad);
124
125
126 final NewtCanvasAWT nca = new NewtCanvasAWT(glad);
127 Assert.assertNotNull(nca);
128 final Dimension size0 = new Dimension(widthStep*szStep, heightStep*szStep);
129 nca.setShallUseOffscreenLayer(offscreenLayer); // trigger offscreen layer - if supported
130 nca.setPreferredSize(size0);
131 nca.setMinimumSize(size0);
132 nca.setSize(size0);
133
134 final Frame frame = new Frame(getSimpleTestName("."));
135 Assert.assertNotNull(frame);
136 frame.add(nca);
137
138 try {
139 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
140 public void run() {
141 frame.pack();
142 frame.setVisible(true);
143 }});
144 } catch( final Throwable throwable ) {
145 throwable.printStackTrace();
146 Assume.assumeNoException( throwable );
147 }
148
149 Assert.assertTrue(NewtTestUtil.waitForVisible(glad, true, null));
150 Assert.assertTrue(NewtTestUtil.waitForRealized(glad, true, null));
151 System.out.println("Window: "+glad.getClass().getName());
152
153 // Check caps of NativeWindow config w/o GL
154 final CapabilitiesImmutable chosenCaps = glad.getChosenGLCapabilities();
155 System.out.println("Window Caps Pre_GL: "+chosenCaps);
156 Assert.assertNotNull(chosenCaps);
157 Assert.assertTrue(chosenCaps.getGreenBits()>5);
158 Assert.assertTrue(chosenCaps.getBlueBits()>5);
159 Assert.assertTrue(chosenCaps.getRedBits()>5);
160
161 glad.display(); // force native context creation
162
163 //
164 // Create native OpenGL resources .. XGL/WGL/CGL ..
165 // equivalent to GLAutoDrawable methods: setVisible(true)
166 //
167 {
168 final GLDrawable actualDrawable = glad.getDelegatedDrawable();
169 Assert.assertNotNull(actualDrawable);
170 System.out.println("Drawable Pre-GL(0): "+actualDrawable.getClass().getName()+", "+actualDrawable.getNativeSurface().getClass().getName());
171 }
172
173 System.out.println("Window Caps PostGL : "+glad.getChosenGLCapabilities());
174 System.out.println("Drawable Post-GL(1): "+glad.getClass().getName()+", "+glad.getNativeSurface().getClass().getName());
175
176 // Check caps of GLDrawable after realization
177 final GLCapabilitiesImmutable chosenGLCaps = glad.getChosenGLCapabilities();
178 System.out.println("Chosen GL Caps(1): "+chosenGLCaps);
179 Assert.assertNotNull(chosenGLCaps);
180 Assert.assertTrue(chosenGLCaps.getGreenBits()>5);
181 Assert.assertTrue(chosenGLCaps.getBlueBits()>5);
182 Assert.assertTrue(chosenGLCaps.getRedBits()>5);
183 Assert.assertTrue(chosenGLCaps.getDepthBits()>4);
184 Assert.assertEquals(expGLCaps.isOnscreen(), chosenGLCaps.isOnscreen());
185 Assert.assertEquals(expGLCaps.isFBO(), chosenGLCaps.isFBO());
186 Assert.assertEquals(expGLCaps.isPBuffer(), chosenGLCaps.isPBuffer());
187 Assert.assertEquals(expGLCaps.isBitmap(), chosenGLCaps.isBitmap());
188 /** Single/Double buffer cannot be checked since result may vary ..
189 if(chosenGLCaps.isOnscreen() || chosenGLCaps.isFBO()) {
190 // dbl buffer may be disabled w/ offscreen pbuffer and bitmap
191 Assert.assertEquals(expGLCaps.getDoubleBuffered(), chosenGLCaps.getDoubleBuffered());
192 } */
193
194 {
195 final GLContext context = glad.getContext();
196 System.out.println("Chosen GL CTX (2): "+context.getGLVersion());
197 Assert.assertNotNull(context);
198 Assert.assertTrue(context.isCreated());
199 }
200
201 System.out.println("Chosen GL Caps(2): "+glad.getChosenGLCapabilities());
202 System.out.println("Drawable Post-GL(2): "+glad.getClass().getName()+", "+glad.getNativeSurface().getClass().getName());
203
204 glad.addGLEventListener(demo);
205
206 final SnapshotGLEventListener snapshotGLEventListener = new SnapshotGLEventListener();
207 glad.addGLEventListener(snapshotGLEventListener);
208
209 glad.display(); // initial resize/display
210
211 // 1 - szStep = 2
212 final int[] expSurfaceSize = glad.getNativeSurface().convertToPixelUnits(new int[] { widthStep*szStep, heightStep*szStep });
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 snapshotGLEventListener.setMakeSnapshot();
216 glad.display();
217
218 // 2, 3 (resize + display)
219 szStep = 1;
220 setComponentSize(frame, nca, widthStep*szStep, heightStep*szStep);
221 expSurfaceSize[0] = widthStep*szStep;
222 expSurfaceSize[1] = heightStep*szStep;
223 glad.getNativeSurface().convertToPixelUnits(expSurfaceSize);
224 Assert.assertTrue("Surface Size not reached: Expected "+expSurfaceSize[0]+"x"+expSurfaceSize[1]+", Is "+glad.getSurfaceWidth()+"x"+glad.getSurfaceHeight(),
225 GLTestUtil.waitForSize(glad, expSurfaceSize[0], expSurfaceSize[1], null));
226 glad.display();
227 snapshotGLEventListener.setMakeSnapshot();
228 glad.display();
229
230 // 4, 5 (resize + display)
231 szStep = 4;
232 setComponentSize(frame, nca, widthStep*szStep, heightStep*szStep);
233 expSurfaceSize[0] = widthStep*szStep;
234 expSurfaceSize[1] = heightStep*szStep;
235 glad.getNativeSurface().convertToPixelUnits(expSurfaceSize);
236 Assert.assertTrue("Surface Size not reached: Expected "+expSurfaceSize[0]+"x"+expSurfaceSize[1]+", Is "+glad.getSurfaceWidth()+"x"+glad.getSurfaceHeight(),
237 GLTestUtil.waitForSize(glad, expSurfaceSize[0], expSurfaceSize[1], null));
238 glad.display();
239 snapshotGLEventListener.setMakeSnapshot();
240 glad.display();
241
242 Thread.sleep(50);
243
244 try {
245 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
246 public void run() {
247 frame.setVisible(false);
248 frame.remove(nca);
249 frame.dispose();
250 }});
251 } catch( final Throwable throwable ) {
252 throwable.printStackTrace();
253 Assume.assumeNoException( throwable );
254 }
255 glad.destroy();
256 System.out.println("Fin: "+nca);
257 System.out.println("Fin: "+glad);
258 }
259
260 @Test
261 public void testGL2OnScreenDblBuf() throws InterruptedException {
262 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
263 if(null == reqGLCaps) return;
264 doTest(false, reqGLCaps, new GearsES2(1));
265 }
266
267 @Test
268 public void testGL2OnScreenDblBufStencil() throws InterruptedException {
269 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
270 if(null == reqGLCaps) return;
271 reqGLCaps.setStencilBits(1);
272 doTest(false, reqGLCaps, new GearsES2(1));
273 }
274
275 @Test
276 public void testGL2OnScreenDblBufMSAA() throws InterruptedException {
277 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
278 if(null == reqGLCaps) return;
279 reqGLCaps.setSampleBuffers(true);
280 reqGLCaps.setNumSamples(4);
281 doTest(false, reqGLCaps, new GearsES2(1));
282 }
283
284 @Test
285 public void testGL2OnScreenDblBufStencilMSAA() throws InterruptedException {
286 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
287 if(null == reqGLCaps) return;
288 reqGLCaps.setStencilBits(1);
289 reqGLCaps.setSampleBuffers(true);
290 reqGLCaps.setNumSamples(4);
291 doTest(false, reqGLCaps, new GearsES2(1));
292 }
293
294 @Test
295 public void testGL2OffScreenLayerAutoDblBuf() throws InterruptedException {
296 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
297 if(null == reqGLCaps) return;
298 doTest(true, reqGLCaps, new GearsES2(1));
299 }
300
301 @Test
302 public void testGL2OffScreenFBODblBufStencil() throws InterruptedException {
303 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
304 if(null == reqGLCaps) return;
305 reqGLCaps.setFBO(true);
306 reqGLCaps.setOnscreen(true); // get native NEWT Window, not OffscreenWindow
307 reqGLCaps.setStencilBits(1);
308 doTest(true, reqGLCaps, new GearsES2(1));
309 }
310
311 @Test
312 public void testGL2OffScreenFBODblBufMSAA() throws InterruptedException {
313 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
314 if(null == reqGLCaps) return;
315 reqGLCaps.setFBO(true);
316 reqGLCaps.setOnscreen(true); // get native NEWT Window, not OffscreenWindow
317 reqGLCaps.setSampleBuffers(true);
318 reqGLCaps.setNumSamples(4);
319 doTest(true, reqGLCaps, new GearsES2(1));
320 }
321
322 @Test
323 public void testGL2OffScreenFBODblBufStencilMSAA() throws InterruptedException {
324 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
325 if(null == reqGLCaps) return;
326 reqGLCaps.setFBO(true);
327 reqGLCaps.setOnscreen(true); // get native NEWT Window, not OffscreenWindow
328 reqGLCaps.setStencilBits(1);
329 reqGLCaps.setSampleBuffers(true);
330 reqGLCaps.setNumSamples(4);
331 doTest(true, reqGLCaps, new GearsES2(1));
332 }
333
334 @Test
335 public void testGL2OffScreenPbuffer() throws InterruptedException {
336 final GLCapabilities reqGLCaps = getCaps(GLProfile.GL2);
337 if(null == reqGLCaps) return;
338 reqGLCaps.setPBuffer(true);
339 reqGLCaps.setOnscreen(true); // get native NEWT Window, not OffscreenWindow
340 doTest(true, reqGLCaps, new GearsES2(1));
341 }
342
343 public static void main(final String args[]) throws IOException {
344 org.junit.runner.JUnitCore.main(TestGLAutoDrawableNewtCanvasAWTOnOffscrnCapsAWT.class.getName());
345 }
346
347}
void setOnscreen(final boolean onscreen)
Sets whether the surface shall be on- or offscreen.
AWT Canvas containing a NEWT Window using native parenting.
void setShallUseOffscreenLayer(final boolean v)
Request an offscreen layer, if supported.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:466
final int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:461
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
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.
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
abstract AbstractGraphicsDevice getDefaultDevice()
Retrieve the default device connection, unit ID and unique ID name.
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
static boolean waitForSize(final GLDrawable drawable, final int width, final int height, final Runnable waitAction)
Definition: GLTestUtil.java:72
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
static boolean waitForVisible(final Window win, final boolean visible, final Runnable waitAction)
A interface describing a graphics device in a toolkit-independent manner.
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.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
GLContext getContext()
Returns the context associated with this drawable.
GLDrawable getDelegatedDrawable()
If the implementation uses delegation, return the delegated GLDrawable instance, otherwise return thi...
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
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
NativeSurface getNativeSurface()
Returns the associated NativeSurface of this NativeSurfaceHolder.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.