JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestLandscapeES2NewtCanvasAWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 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.newt;
30
31import java.awt.Frame;
32import java.io.IOException;
33import java.lang.reflect.InvocationTargetException;
34
35import com.jogamp.newt.Display;
36import com.jogamp.newt.NewtFactory;
37import com.jogamp.newt.Screen;
38import com.jogamp.newt.awt.NewtCanvasAWT;
39import com.jogamp.newt.event.WindowEvent;
40import com.jogamp.newt.event.WindowAdapter;
41import com.jogamp.newt.opengl.GLWindow;
42import com.jogamp.newt.opengl.util.NEWTDemoListener;
43import com.jogamp.opengl.test.junit.util.MiscUtils;
44import com.jogamp.opengl.test.junit.util.UITestCase;
45import com.jogamp.opengl.util.Animator;
46import com.jogamp.opengl.util.AnimatorBase;
47import com.jogamp.opengl.test.junit.jogl.demos.es2.LandscapeES2;
48import com.jogamp.opengl.test.junit.newt.parenting.NewtAWTReparentingKeyAdapter;
49import com.jogamp.opengl.test.junit.newt.parenting.NewtReparentingKeyAdapter;
50
51import com.jogamp.nativewindow.util.Dimension;
52import com.jogamp.nativewindow.util.DimensionImmutable;
53import com.jogamp.opengl.GLCapabilities;
54import com.jogamp.opengl.GLCapabilitiesImmutable;
55import com.jogamp.opengl.GLProfile;
56import javax.swing.SwingUtilities;
57
58import org.junit.Test;
59import org.junit.FixMethodOrder;
60import org.junit.runners.MethodSorters;
61
62/**
63 * <p>
64 * The demo code uses {@link NewtReparentingKeyAdapter} including {@link NEWTDemoListener} functionality.
65 * </p>
66 * <p>
67 * Manual invocation via main allows setting each tests's duration in milliseconds, e.g.{@code -duration 10000}, and many more, see {@link #main(String[])}
68 * </p>
69 */
70@FixMethodOrder(MethodSorters.NAME_ASCENDING)
72 static DimensionImmutable wsize = new Dimension(500, 290);
73
74 static long duration = 1000; // ms
75 static int swapInterval = 1;
76 static boolean shallUseOffscreenFBOLayer = false;
77 static boolean forceES2 = false;
78 static boolean forceGL3 = false;
79 static boolean mainRun = false;
80 static boolean exclusiveContext = false;
81 static boolean useAnimator = true;
82
83 // public enum ResizeBy { GLWindow, Component, Frame };
84 protected void runTestGL(final GLCapabilitiesImmutable caps) throws InterruptedException, InvocationTargetException {
85 System.err.println("requested: vsync "+swapInterval+", "+caps);
86 final Display dpy = NewtFactory.createDisplay(null);
87 final Screen screen = NewtFactory.createScreen(dpy, 0);
88 final GLWindow glWindow = GLWindow.create(screen, caps);
89
90 // Enforce landscape shader to be linked once,
91 // since on some platforms (Mesa/AMD) it takes a long time!
92 final LandscapeES2 demo = new LandscapeES2(swapInterval);
93 glWindow.addGLEventListener(demo);
94 glWindow.setVisible(true);
95 glWindow.display();
96
97 final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow);
98 if ( shallUseOffscreenFBOLayer ) {
99 newtCanvasAWT.setShallUseOffscreenLayer(true);
100 }
101
102 final Frame frame = new Frame("AWT Parent Frame");
103 {
104 final java.awt.Dimension d = new java.awt.Dimension(wsize.getWidth(), wsize.getHeight());
105 frame.setSize(d);
106 }
107 frame.add(newtCanvasAWT);
108 frame.setTitle("Gears NewtCanvasAWT Test (translucent "+!caps.isBackgroundOpaque()+"), swapInterval "+swapInterval+", size "+wsize);
109
110 final Animator animator = useAnimator ? new Animator(0 /* w/o AWT */) : null;
111 if( useAnimator ) {
112 animator.setExclusiveContext(exclusiveContext);
113 }
114
115 glWindow.addWindowListener(new WindowAdapter() {
116 @Override
117 public void windowResized(final WindowEvent e) {
118 System.err.println("window resized: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight());
119 }
120 @Override
121 public void windowMoved(final WindowEvent e) {
122 System.err.println("window moved: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight());
123 }
124 });
125
126 final NewtReparentingKeyAdapter newtDemoListener = new NewtAWTReparentingKeyAdapter(frame, newtCanvasAWT, glWindow);
127 newtDemoListener.quitAdapterEnable(true);
128 glWindow.addKeyListener(newtDemoListener);
129 glWindow.addMouseListener(newtDemoListener);
130 glWindow.addWindowListener(newtDemoListener);
131
132 if( useAnimator ) {
133 animator.add(glWindow);
134 animator.start();
135 }
136
137 SwingUtilities.invokeAndWait(new Runnable() {
138 @Override
139 public void run() {
140 frame.validate();
141 frame.setVisible(true);
142 }
143 });
144
145 if( useAnimator ) {
146 animator.setUpdateFPSFrames(60, System.err);
147 }
148
149 System.err.println("NW chosen: "+glWindow.getDelegatedWindow().getChosenCapabilities());
150 System.err.println("GL chosen: "+glWindow.getChosenCapabilities());
151 System.err.println("window pos/siz: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", "+glWindow.getInsets());
152
153 final long t0 = System.currentTimeMillis();
154 long t1 = t0;
155 while(!newtDemoListener.shouldQuit() && t1-t0<duration) {
156 Thread.sleep(100);
157 t1 = System.currentTimeMillis();
158 }
159
160 if( useAnimator ) {
161 animator.stop();
162 }
163 SwingUtilities.invokeAndWait(new Runnable() {
164 @Override
165 public void run() {
166 frame.dispose();
167 }
168 });
169 glWindow.destroy(); // removeNotify does not destroy GLWindow
170 }
171
172 @Test
173 public void test01GL2ES2() throws InterruptedException, InvocationTargetException {
174 final GLProfile glp;
175 if(forceGL3) {
176 glp = GLProfile.get(GLProfile.GL3);
177 } else if(forceES2) {
179 } else {
180 glp = GLProfile.getGL2ES2();
181 }
182 final GLCapabilities caps = new GLCapabilities( glp );
183 runTestGL(caps);
184 }
185
186 @Test
187 public void test02GL3() throws InterruptedException, InvocationTargetException {
188 if(mainRun) return;
189
191 System.err.println("GL3 n/a");
192 return;
193 }
194 final GLProfile glp = GLProfile.get(GLProfile.GL3);
195 final GLCapabilities caps = new GLCapabilities( glp );
196 runTestGL(caps);
197 }
198
199 public static void main(final String args[]) throws IOException {
200 mainRun = true;
201
202 for(int i=0; i<args.length; i++) {
203 if(args[i].equals("-time")) {
204 i++;
205 duration = MiscUtils.atol(args[i], duration);
206 } else if(args[i].equals("-vsync")) {
207 i++;
208 swapInterval = MiscUtils.atoi(args[i], swapInterval);
209 } else if(args[i].equals("-layeredFBO")) {
210 shallUseOffscreenFBOLayer = true;
211 } else if(args[i].equals("-exclctx")) {
212 exclusiveContext = true;
213 } else if(args[i].equals("-noanim")) {
214 useAnimator = false;
215 } else if(args[i].equals("-es2")) {
216 forceES2 = true;
217 } else if(args[i].equals("-gl3")) {
218 forceGL3 = true;
219 }
220 }
221
222 System.err.println("size "+wsize);
223 System.err.println("shallUseOffscreenFBOLayer "+shallUseOffscreenFBOLayer);
224 System.err.println("forceES2 "+forceES2);
225 System.err.println("forceGL3 "+forceGL3);
226 System.err.println("swapInterval "+swapInterval);
227 System.err.println("exclusiveContext "+exclusiveContext);
228 System.err.println("useAnimator "+useAnimator);
229
230 org.junit.runner.JUnitCore.main(TestLandscapeES2NewtCanvasAWT.class.getName());
231 }
232}
static Display createDisplay(final String name)
Create a Display entity.
static Screen createScreen(final Display display, final int index)
Create a Screen entity.
A screen may span multiple MonitorDevices representing their combined virtual size.
Definition: Screen.java:58
AWT Canvas containing a NEWT Window using native parenting.
void setShallUseOffscreenLayer(final boolean v)
Request an offscreen layer, if supported.
NEWT Window events are provided for notification purposes ONLY.
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 void addMouseListener(final MouseListener l)
Appends the given MouseListener to the end of the list.
Definition: GLWindow.java:927
final int getX()
Returns the current x position of this window, relative to it's parent.
Definition: GLWindow.java:436
final void addKeyListener(final KeyListener l)
Appends the given com.jogamp.newt.event.KeyListener to the end of the list.
Definition: GLWindow.java:902
final int getY()
Returns the current y position of the top-left corner of the client area relative to it's parent in w...
Definition: GLWindow.java:441
final int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:461
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
final void addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
Definition: GLWindow.java:882
final CapabilitiesImmutable getChosenCapabilities()
Gets an immutable set of chosen capabilities.
Definition: GLWindow.java:266
final InsetsImmutable getInsets()
Returns the insets defined as the width and height of the window decoration on the left,...
Definition: GLWindow.java:431
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
final Window getDelegatedWindow()
If the implementation uses delegation, return the delegated Window instance, otherwise return this in...
Definition: GLWindow.java:277
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.
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 final String GLES2
The embedded OpenGL profile ES 2.x, with x >= 0.
Definition: GLProfile.java:585
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
static GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
AWT specializing demo functionality of NewtReparentingKeyAdapter, includes NEWTDemoListener.
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
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
final synchronized Thread setExclusiveContext(final Thread t)
Dedicate all GLAutoDrawable's context to the given exclusive context thread.
final void setUpdateFPSFrames(final int frames, final PrintStream out)
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
Immutable Dimension Interface, consisting of it's read only components:
CapabilitiesImmutable getChosenCapabilities()
Gets an immutable set of chosen capabilities.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Specifies an immutable set of OpenGL capabilities.