JOGL: src/test/com/jogamp/opengl/test/junit/jogl/acore/TestOffscreenLayer01GLCanvasAWT.java Source File

JOGL v2.6.0-rc-20250712 JOGL, High-Performance Graphics Binding for Java™ (public API).

TestOffscreenLayer01GLCanvasAWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2011 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.BorderLayout;
32import java.awt.Button;
33import java.awt.Container;
34import java.awt.Dimension;
35import java.awt.Frame;
36import java.io.IOException;
37import java.lang.reflect.InvocationTargetException;
38
39import com.jogamp.opengl.GLAnimatorControl;
40import com.jogamp.opengl.GLCapabilities;
41import com.jogamp.opengl.GLEventListener;
42import com.jogamp.opengl.GLProfile;
43import com.jogamp.opengl.awt.GLCanvas;
44
45import jogamp.nativewindow.jawt.JAWTUtil;
46
47import org.junit.Assert;
48import org.junit.BeforeClass;
49import org.junit.Test;
50import org.junit.FixMethodOrder;
51import org.junit.runners.MethodSorters;
52
53import com.jogamp.common.os.Platform;
54import com.jogamp.newt.Window;
55import com.jogamp.newt.opengl.GLWindow;
56import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
57import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
58import com.jogamp.opengl.test.junit.util.MiscUtils;
59import com.jogamp.opengl.test.junit.util.UITestCase;
60import com.jogamp.opengl.util.Animator;
61
62@FixMethodOrder(MethodSorters.NAME_ASCENDING)
63public class TestOffscreenLayer01GLCanvasAWT extends UITestCase {
64 static boolean singleBuffer = false;
65 static boolean useMSAA = false;
66 static boolean addComp = true;
67 static int swapInterval = 1;
68 static boolean shallUseOffscreenPBufferLayer = false;
69 static boolean noAnimation = false;
70 static Dimension frameSize0;
71 static Dimension frameSize1;
72 static Dimension preferredGLSize;
73 static long durationPerTest = 1000;
74 static boolean waitForKey = false;
75
76 @BeforeClass
77 public static void initClass() {
78 frameSize0 = new Dimension(500,300);
79 frameSize1 = new Dimension(800,600);
80 preferredGLSize = new Dimension(400,200);
81 }
82
83 private void setupFrameAndShow(final Frame f, final java.awt.Component comp) throws InterruptedException, InvocationTargetException {
84
85 final Container c = new Container();
86 c.setLayout(new BorderLayout());
87 c.add(new Button("north"), BorderLayout.NORTH);
88 c.add(new Button("south"), BorderLayout.SOUTH);
89 c.add(new Button("east"), BorderLayout.EAST);
90 c.add(new Button("west"), BorderLayout.WEST);
91 c.add(comp, BorderLayout.CENTER);
92
93 f.setLayout(new BorderLayout());
94 f.add(new Button("NORTH"), BorderLayout.NORTH);
95 f.add(new Button("SOUTH"), BorderLayout.SOUTH);
96 f.add(new Button("EAST"), BorderLayout.EAST);
97 f.add(new Button("WEST"), BorderLayout.WEST);
98 f.add(c, BorderLayout.CENTER);
99
100 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
101 public void run() {
102 f.pack();
103 f.validate();
104 f.setVisible(true);
105 }});
106 }
107
108 private void end(final GLAnimatorControl actrl, final Frame f, final Window w) throws InterruptedException, InvocationTargetException {
109 actrl.stop();
110 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
111 public void run() {
112 f.dispose();
113 } } );
114 if(null != w) {
115 w.destroy();
116 }
117 }
118
119 @Test
120 public void testInfo00() throws InterruptedException, InvocationTargetException {
121 System.err.println("Java Version: "+Platform.getJavaVersionNumber());
122 System.err.println("OS Version: "+Platform.getOSVersionNumber());
123 System.err.println("JAWTUtil.isOffscreenLayerRequired(): "+JAWTUtil.isOffscreenLayerRequired());
124 System.err.println("JAWTUtil.isOffscreenLayerSupported(): "+JAWTUtil.isOffscreenLayerSupported());
125 }
126
127 @Test
128 public void test01_GLDefault() throws InterruptedException, InvocationTargetException {
129 testOffscreenLayerGLCanvas_Impl(null);
130 }
131
132 @Test
133 public void test01_GL3() throws InterruptedException, InvocationTargetException {
134 if( !GLProfile.isAvailable(GLProfile.GL3) ) {
135 System.err.println("GL3 n/a");
136 return;
137 }
138 testOffscreenLayerGLCanvas_Impl(GLProfile.get(GLProfile.GL3));
139 }
140
141 private void testOffscreenLayerGLCanvas_Impl(final GLProfile glp) throws InterruptedException, InvocationTargetException {
142 if(!JAWTUtil.isOffscreenLayerSupported()) {
143 System.err.println("offscreen layer n/a");
144 return;
145 }
146 final Frame frame1 = new Frame("AWT Parent Frame");
147
148 final GLCapabilities caps = new GLCapabilities(glp);
149 if(singleBuffer) {
150 caps.setDoubleBuffered(false);
151 }
152 if(useMSAA) {
153 caps.setNumSamples(4);
154 caps.setSampleBuffers(true);
155 }
156 if(shallUseOffscreenPBufferLayer) {
157 caps.setPBuffer(true);
158 caps.setOnscreen(true); // simulate normal behavior ..
159 }
160 final GLCanvas glc = new GLCanvas(caps);
161 glc.setShallUseOffscreenLayer(true); // trigger offscreen layer - if supported
162 glc.setPreferredSize(preferredGLSize);
163 glc.setMinimumSize(preferredGLSize);
164 glc.setSize(preferredGLSize);
165
166 final GearsES2 demo1 = new GearsES2(swapInterval);
167 if(noAnimation) {
168 demo1.setDoRotation(false);
169 }
170 glc.addGLEventListener(demo1);
171
172 frame1.setSize(frameSize0);
173 setupFrameAndShow(frame1, glc);
174 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glc, true, null));
175 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(glc, true, null));
176 Assert.assertEquals(true, glc.isOffscreenLayerSurfaceEnabled());
177
178 final GLAnimatorControl animator1 = new Animator(glc);
179 if(!noAnimation) {
180 animator1.start();
181 }
182 animator1.setUpdateFPSFrames(60, System.err);
183
184 Thread.sleep(durationPerTest/2);
185 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
186 public void run() {
187 frame1.setSize(frameSize1);
188 frame1.pack();
189 frame1.validate();
190 }});
191
192 Thread.sleep(durationPerTest/2);
193
194 end(animator1, frame1, null);
195 if( waitForKey ) {
196 UITestCase.waitForKey("Continue");
197 }
198 }
199
200 public static void setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug) {
201 Assert.assertNotNull(demo);
202 Assert.assertNotNull(glWindow);
203 final Window window = glWindow.getDelegatedWindow();
204 if(debug) {
205 MiscUtils.setFieldIfExists(demo, "glDebug", true);
206 MiscUtils.setFieldIfExists(demo, "glTrace", true);
207 }
208 if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
209 MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
210 }
211 }
212
213 static int atoi(final String a) {
214 int i=0;
215 try {
216 i = Integer.parseInt(a);
217 } catch (final Exception ex) { ex.printStackTrace(); }
218 return i;
219 }
220
221 public static void main(final String args[]) throws IOException {
222 for(int i=0; i<args.length; i++) {
223 if(args[i].equals("-time")) {
224 durationPerTest = atoi(args[++i]);
225 } else if(args[i].equals("-vsync")) {
226 i++;
227 swapInterval = MiscUtils.atoi(args[i], swapInterval);
228 } else if(args[i].equals("-layeredPBuffer")) {
229 shallUseOffscreenPBufferLayer = true;
230 } else if(args[i].equals("-msaa")) {
231 useMSAA = true;
232 } else if(args[i].equals("-single")) {
233 singleBuffer = true;
234 } else if(args[i].equals("-still")) {
235 noAnimation = true;
236 } else if(args[i].equals("-wait")) {
237 waitForKey = true;
238 }
239 }
240 if(waitForKey) {
241 UITestCase.waitForKey("Start");
242 }
243 final String tstname = TestOffscreenLayer01GLCanvasAWT.class.getName();
244 /*
245 org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] {
246 tstname,
247 "filtertrace=true",
248 "haltOnError=false",
249 "haltOnFailure=false",
250 "showoutput=true",
251 "outputtoformatters=true",
252 "logfailedtests=true",
253 "logtestlistenerevents=true",
254 "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter",
255 "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } ); */
256 org.junit.runner.JUnitCore.main(tstname);
257 }
258
259}
com.jogamp.nativewindow.Capabilities.setOnscreen
void setOnscreen(final boolean onscreen)
Sets whether the surface shall be on- or offscreen.
Definition: Capabilities.java:321
com.jogamp.nativewindow.util.Dimension
Definition: Dimension.java:32
com.jogamp.newt.opengl.GLWindow
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
com.jogamp.newt.opengl.GLWindow.getDelegatedWindow
final Window getDelegatedWindow()
If the implementation uses delegation, return the delegated Window instance, otherwise return this in...
Definition: GLWindow.java:277
com.jogamp.opengl.GLCapabilities
Specifies a set of OpenGL capabilities.
Definition: GLCapabilities.java:58
com.jogamp.opengl.GLCapabilities.setPBuffer
void setPBuffer(final boolean enable)
Requesting offscreen pbuffer mode.
Definition: GLCapabilities.java:269
com.jogamp.opengl.GLCapabilities.setDoubleBuffered
void setDoubleBuffered(final boolean enable)
Enables or disables double buffering.
Definition: GLCapabilities.java:306
com.jogamp.opengl.GLCapabilities.setNumSamples
void setNumSamples(final int numSamples)
If sample buffers are enabled, indicates the number of buffers to be allocated.
Definition: GLCapabilities.java:440
com.jogamp.opengl.GLCapabilities.setSampleBuffers
void setSampleBuffers(final boolean enable)
Defaults to false.
Definition: GLCapabilities.java:423
com.jogamp.opengl.GLProfile
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
com.jogamp.opengl.GLProfile.isAvailable
static boolean isAvailable(final AbstractGraphicsDevice device, final String profile)
Returns the availability of a profile on a device.
Definition: GLProfile.java:305
com.jogamp.opengl.GLProfile.GL3
static final String GL3
The desktop OpenGL core profile 3.x, with x >= 1.
Definition: GLProfile.java:576
com.jogamp.opengl.GLProfile.get
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
Definition: GLProfile.java:1000
com.jogamp.opengl.test.junit.jogl.acore.TestOffscreenLayer01GLCanvasAWT
Definition: TestOffscreenLayer01GLCanvasAWT.java:63
com.jogamp.opengl.test.junit.jogl.acore.TestOffscreenLayer01GLCanvasAWT.test01_GL3
void test01_GL3()
Definition: TestOffscreenLayer01GLCanvasAWT.java:133
com.jogamp.opengl.test.junit.jogl.acore.TestOffscreenLayer01GLCanvasAWT.test01_GLDefault
void test01_GLDefault()
Definition: TestOffscreenLayer01GLCanvasAWT.java:128
com.jogamp.opengl.test.junit.jogl.acore.TestOffscreenLayer01GLCanvasAWT.initClass
static void initClass()
Definition: TestOffscreenLayer01GLCanvasAWT.java:77
com.jogamp.opengl.test.junit.jogl.acore.TestOffscreenLayer01GLCanvasAWT.testInfo00
void testInfo00()
Definition: TestOffscreenLayer01GLCanvasAWT.java:120
com.jogamp.opengl.test.junit.jogl.acore.TestOffscreenLayer01GLCanvasAWT.main
static void main(final String args[])
Definition: TestOffscreenLayer01GLCanvasAWT.java:221
com.jogamp.opengl.test.junit.jogl.acore.TestOffscreenLayer01GLCanvasAWT.setDemoFields
static void setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug)
Definition: TestOffscreenLayer01GLCanvasAWT.java:200
com.jogamp.opengl.test.junit.util.MiscUtils
Definition: MiscUtils.java:47
com.jogamp.opengl.test.junit.util.MiscUtils.setFieldIfExists
static boolean setFieldIfExists(final Object instance, final String fieldName, final Object value)
Definition: MiscUtils.java:193
com.jogamp.opengl.test.junit.util.MiscUtils.atoi
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
com.jogamp.opengl.test.junit.util.UITestCase
Definition: UITestCase.java:62
com.jogamp.newt.Window
Specifying NEWT's Window functionality:
Definition: Window.java:115
com.jogamp.opengl.GLAnimatorControl
An animator control interface, which implementation may drive a com.jogamp.opengl....
Definition: GLAnimatorControl.java:35
com.jogamp.opengl.GLEventListener
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
Definition: GLEventListener.java:47