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