JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGearsES2SimpleNEWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2015 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.io.IOException;
32import java.net.URLConnection;
33
34import com.jogamp.common.util.IOUtil;
35import com.jogamp.newt.Display;
36import com.jogamp.newt.Display.PointerIcon;
37import com.jogamp.newt.NewtFactory;
38import com.jogamp.newt.Screen;
39import com.jogamp.newt.Window;
40import com.jogamp.newt.event.WindowEvent;
41import com.jogamp.newt.event.WindowAdapter;
42import com.jogamp.newt.opengl.GLWindow;
43import com.jogamp.newt.opengl.util.NEWTDemoListener;
44import com.jogamp.opengl.test.junit.util.UITestCase;
45import com.jogamp.opengl.test.junit.util.MiscUtils;
46import com.jogamp.opengl.test.junit.util.QuitAdapter;
47import com.jogamp.opengl.util.Animator;
48import com.jogamp.opengl.util.PNGPixelRect;
49import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
50import com.jogamp.opengl.test.junit.newt.parenting.NewtReparentingKeyAdapter;
51import com.jogamp.nativewindow.ScalableSurface;
52import com.jogamp.nativewindow.util.Dimension;
53import com.jogamp.nativewindow.util.DimensionImmutable;
54import com.jogamp.opengl.GLCapabilities;
55import com.jogamp.opengl.GLCapabilitiesImmutable;
56import com.jogamp.opengl.GLProfile;
57
58import jogamp.newt.driver.PNGIcon;
59
60import org.junit.Test;
61import org.junit.FixMethodOrder;
62import org.junit.runners.MethodSorters;
63
64/**
65 * <p>
66 * The demo code uses {@link NEWTDemoListener} functionality.
67 * </p>
68 * <p>
69 * Manual invocation via main allows setting each tests's duration in milliseconds, e.g.{@code -duration 10000} and using a translucent window {@code -translucent}.
70 * </p>
71 */
72@FixMethodOrder(MethodSorters.NAME_ASCENDING)
73public class TestGearsES2SimpleNEWT extends UITestCase {
74 static final DimensionImmutable wsize = new Dimension(800, 600);
75 static final float[] reqSurfacePixelScale = new float[] { ScalableSurface.AUTOMAX_PIXELSCALE, ScalableSurface.AUTOMAX_PIXELSCALE };
76
77 static final int swapInterval = 1;
78
79 static long duration = 500; // ms
80 static boolean opaque = true;
81
82 private void setTitle(final Window win, final GLCapabilitiesImmutable caps) {
83 final float[] sDPI = win.getPixelsPerMM(new float[2]);
84 sDPI[0] *= 25.4f;
85 sDPI[1] *= 25.4f;
86 win.setTitle("GLWindow: swapI "+swapInterval+", win: "+win.getBounds()+", pix: "+win.getSurfaceWidth()+"x"+win.getSurfaceHeight()+", sDPI "+sDPI[0]+" x "+sDPI[1]);
87 }
88 protected void runTestGL(final GLCapabilitiesImmutable caps, final boolean undecorated) throws InterruptedException {
89 System.err.println("requested: vsync "+swapInterval+", "+caps);
90 final Display dpy = NewtFactory.createDisplay(null);
91 final Screen screen = NewtFactory.createScreen(dpy, 0);
92 final GLWindow glWindow = GLWindow.create(screen, caps);
93 glWindow.setSurfaceScale(reqSurfacePixelScale);
94 final float[] valReqSurfacePixelScale = glWindow.getRequestedSurfaceScale(new float[2]);
95 glWindow.setSize(wsize.getWidth(), wsize.getHeight());
96 glWindow.setUndecorated(undecorated);
97
98 final GearsES2 demo = new GearsES2(swapInterval);
99 demo.setValidateBuffers(true);
100 glWindow.addGLEventListener(demo);
101
103 glWindow.addGLEventListener(snap);
104
105 final Animator animator = new Animator(0 /* w/o AWT */);
106
107 final QuitAdapter quitAdapter = new QuitAdapter();
108 glWindow.addKeyListener(quitAdapter);
109 glWindow.addWindowListener(quitAdapter);
110
111 glWindow.addWindowListener(new WindowAdapter() {
112 @Override
113 public void windowResized(final WindowEvent e) {
114 System.err.println("window resized: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight());
115 setTitle(glWindow, caps);
116 }
117 @Override
118 public void windowMoved(final WindowEvent e) {
119 System.err.println("window moved: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight());
120 setTitle(glWindow, caps);
121 }
122 });
123
124 final PointerIcon[] pointerIcons = { null, null, null, null, null };
125 {
126 final Display disp = glWindow.getScreen().getDisplay();
127 disp.createNative();
128 int idx = 0;
129 {
130 PointerIcon _pointerIcon = null;
131 final IOUtil.ClassResources res = new IOUtil.ClassResources(new String[] { "jogamp/newt/assets/cross-grey-alpha-16x16.png" }, glWindow.getClass().getClassLoader(), null);
132 try {
133 _pointerIcon = disp.createPointerIcon(res, 8, 8);
134 System.err.printf("Create PointerIcon #%02d: %s%n", idx, _pointerIcon.toString());
135 } catch (final Exception e) {
136 e.printStackTrace();
137 }
138 pointerIcons[idx] = _pointerIcon;
139 }
140 idx++;
141 {
142 PointerIcon _pointerIcon = null;
143 final IOUtil.ClassResources res = new IOUtil.ClassResources(new String[] { "jogamp/newt/assets/pointer-grey-alpha-16x24.png" }, glWindow.getClass().getClassLoader(), null);
144 try {
145 _pointerIcon = disp.createPointerIcon(res, 0, 0);
146 System.err.printf("Create PointerIcon #%02d: %s%n", idx, _pointerIcon.toString());
147 } catch (final Exception e) {
148 e.printStackTrace();
149 }
150 pointerIcons[idx] = _pointerIcon;
151 }
152 idx++;
153 {
154 PointerIcon _pointerIcon = null;
155 final IOUtil.ClassResources res = new IOUtil.ClassResources(new String[] { "arrow-red-alpha-64x64.png" }, glWindow.getClass().getClassLoader(), null);
156 try {
157 _pointerIcon = disp.createPointerIcon(res, 0, 0);
158 System.err.printf("Create PointerIcon #%02d: %s%n", idx, _pointerIcon.toString());
159 } catch (final Exception e) {
160 e.printStackTrace();
161 }
162 pointerIcons[idx] = _pointerIcon;
163 }
164 idx++;
165 {
166 PointerIcon _pointerIcon = null;
167 final IOUtil.ClassResources res = new IOUtil.ClassResources(new String[] { "arrow-blue-alpha-64x64.png" }, glWindow.getClass().getClassLoader(), null);
168 try {
169 _pointerIcon = disp.createPointerIcon(res, 0, 0);
170 System.err.printf("Create PointerIcon #%02d: %s%n", idx, _pointerIcon.toString());
171 } catch (final Exception e) {
172 e.printStackTrace();
173 }
174 pointerIcons[idx] = _pointerIcon;
175 }
176 idx++;
177 if( PNGIcon.isAvailable() ) {
178 PointerIcon _pointerIcon = null;
179 final IOUtil.ClassResources res = new IOUtil.ClassResources(new String[] { "jogamp-pointer-64x64.png" }, glWindow.getClass().getClassLoader(), null);
180 try {
181 final URLConnection urlConn = res.resolve(0);
182 final PNGPixelRect image = PNGPixelRect.read(urlConn.getInputStream(), null, false /* directBuffer */, 0 /* destMinStrideInBytes */, false /* destIsGLOriented */);
183 System.err.printf("Create PointerIcon #%02d: %s%n", idx, image.toString());
184 _pointerIcon = disp.createPointerIcon(image, 32, 0);
185 System.err.printf("Create PointerIcon #%02d: %s%n", idx, _pointerIcon.toString());
186 } catch (final Exception e) {
187 e.printStackTrace();
188 }
189 pointerIcons[idx] = _pointerIcon;
190 }
191 idx++;
192 }
193
194 final NEWTDemoListener newtDemoListener = new NEWTDemoListener(glWindow, pointerIcons);
195 glWindow.addKeyListener(newtDemoListener);
196 glWindow.addMouseListener(newtDemoListener);
197
198 animator.add(glWindow);
199 animator.start();
200
201 glWindow.setVisible(true);
202 animator.setUpdateFPSFrames(60, System.err);
203
204 System.err.println("NW chosen: "+glWindow.getDelegatedWindow().getChosenCapabilities());
205 System.err.println("GL chosen: "+glWindow.getChosenCapabilities());
206 System.err.println("window pos/siz: "+glWindow.getX()+"/"+glWindow.getY()+"[wu] "+glWindow.getWidth()+"x"+glWindow.getHeight()+"[wu] "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+"[px], "+glWindow.getInsets());
207
208 final float[] hasSurfacePixelScale1 = glWindow.getCurrentSurfaceScale(new float[2]);
209 System.err.println("HiDPI PixelScale: "+reqSurfacePixelScale[0]+"x"+reqSurfacePixelScale[1]+" (req) -> "+
210 valReqSurfacePixelScale[0]+"x"+valReqSurfacePixelScale[1]+" (val) -> "+
211 hasSurfacePixelScale1[0]+"x"+hasSurfacePixelScale1[1]+" (has)");
212 setTitle(glWindow, caps);
213
214 snap.setMakeSnapshot();
215
216 final long t0 = System.currentTimeMillis();
217 long t1 = t0;
218 while(!quitAdapter.shouldQuit() && t1-t0<duration) {
219 Thread.sleep(100);
220 t1 = System.currentTimeMillis();
221 }
222
223 animator.stop();
224
225 glWindow.destroy();
226 }
227
228 @Test
229 public void test01_GL2ES2() throws InterruptedException {
230 final GLProfile glp = GLProfile.getGL2ES2();
231 final GLCapabilities caps = new GLCapabilities( glp );
232 caps.setBackgroundOpaque(opaque);
233 runTestGL(caps, false);
234 }
235
236 public static void main(final String args[]) throws IOException {
237 duration = 1000000; // ~16 min by default per main launch
238 for(int i=0; i<args.length; i++) {
239 if(args[i].equals("-time")) {
240 i++;
241 duration = MiscUtils.atol(args[i], duration);
242 } else if(args[i].equals("-translucent")) {
243 opaque = false;
244 }
245 }
246 org.junit.runner.JUnitCore.main(TestGearsES2SimpleNEWT.class.getName());
247 }
248}
void setBackgroundOpaque(final boolean opaque)
Sets whether the surface shall be opaque or translucent.
abstract void createNative()
Manual trigger the native creation, if it is not done yet.
abstract PointerIcon createPointerIcon(final IOUtil.ClassResources pngResource, final int hotX, final int hotY)
Returns the newly created PointerIcon or null if not implemented on platform.
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
abstract Display getDisplay()
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 float[] getRequestedSurfaceScale(final float[] result)
Returns the requested pixel scale of the associated NativeSurface.
Definition: GLWindow.java:500
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 float[] getCurrentSurfaceScale(final float[] result)
Returns the current pixel scale of the associated NativeSurface.
Definition: GLWindow.java:505
final int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:461
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
Definition: GLWindow.java:625
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 int getHeight()
Returns the height of the client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:451
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 setUndecorated(final boolean value)
Definition: GLWindow.java:337
final boolean setSurfaceScale(final float[] pixelScale)
Request a pixel scale in x- and y-direction for the associated NativeSurface, where size_in_pixel_uni...
Definition: GLWindow.java:495
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
final int getWidth()
Returns the width of the client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:446
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 GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
void runTestGL(final GLCapabilitiesImmutable caps, final boolean undecorated)
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 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
static PNGPixelRect read(final InputStream in, final PixelFormat ddestFmt, final boolean destDirectBuffer, final int destMinStrideInBytes, final boolean destIsGLOriented)
Reads a PNG image from the specified InputStream.
int getSurfaceWidth()
Returns the width of the client area excluding insets (window decorations) in pixel units.
int getSurfaceHeight()
Returns the height of the client area excluding insets (window decorations) in pixel units.
Rectangle getBounds()
Returns a newly created Rectangle containing window origin, getX() & getY(), and size,...
Adding mutable surface pixel scale property to implementing class, usually to a NativeSurface impleme...
static final float AUTOMAX_PIXELSCALE
Setting surface-pixel-scale of {@value}, results in maximum platform dependent pixel-scale,...
Immutable Dimension Interface, consisting of it's read only components:
Native PointerIcon handle.
Definition: Display.java:92
Specifying NEWT's Window functionality:
Definition: Window.java:115
void setTitle(String title)
CapabilitiesImmutable getChosenCapabilities()
Gets an immutable set of chosen capabilities.
float[] getPixelsPerMM(final float[] ppmmStore)
Returns the pixels per millimeter of this window's NativeSurface according to the main monitor's curr...
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Specifies an immutable set of OpenGL capabilities.