JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGLAutoDrawableDelegateNEWT.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.acore;
30
31import java.io.IOException;
32
33import com.jogamp.opengl.GLCapabilities;
34import com.jogamp.opengl.GLCapabilitiesImmutable;
35import com.jogamp.opengl.GLContext;
36import com.jogamp.opengl.GLDrawable;
37import com.jogamp.opengl.GLDrawableFactory;
38import com.jogamp.opengl.GLEventListener;
39import com.jogamp.opengl.GLProfile;
40
41import org.junit.Assert;
42import org.junit.Test;
43import org.junit.FixMethodOrder;
44import org.junit.runners.MethodSorters;
45
46import com.jogamp.newt.NewtFactory;
47import com.jogamp.newt.Window;
48import com.jogamp.newt.event.WindowAdapter;
49import com.jogamp.newt.event.WindowEvent;
50import com.jogamp.newt.event.WindowUpdateEvent;
51import com.jogamp.opengl.GLAutoDrawableDelegate;
52import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
53import com.jogamp.opengl.test.junit.util.MiscUtils;
54import com.jogamp.opengl.test.junit.util.NewtTestUtil;
55import com.jogamp.opengl.test.junit.util.QuitAdapter;
56import com.jogamp.opengl.test.junit.util.UITestCase;
57import com.jogamp.opengl.util.Animator;
58import com.jogamp.opengl.util.AnimatorBase;
59
60/**
61 * Test using a NEWT {@link Window} for onscreen case.
62 * <p>
63 * Creates a {@link GLDrawable} using the
64 * {@link GLDrawableFactory#createGLDrawable(com.jogamp.nativewindow.NativeSurface) factory model}.
65 * The {@link GLContext} is derived {@link GLDrawable#createContext(GLContext) from the drawable}.
66 * </p>
67 * <p>
68 * Finally a {@link GLAutoDrawableDelegate} is created with the just created {@link GLDrawable} and {@link GLContext}.
69 * It is being used to run the {@link GLEventListener}.
70 * </p>
71 */
72@FixMethodOrder(MethodSorters.NAME_ASCENDING)
74 static long duration = 500; // ms
75
76 void doTest(final GLCapabilitiesImmutable reqGLCaps, final GLEventListener demo) throws InterruptedException {
78
79 //
80 // Create native windowing resources .. X11/Win/OSX
81 //
82 final Window window = NewtFactory.createWindow(reqGLCaps);
83 Assert.assertNotNull(window);
84 window.setSize(640, 400);
85 window.setVisible(true);
86 Assert.assertTrue(NewtTestUtil.waitForVisible(window, true, null));
87 Assert.assertTrue(NewtTestUtil.waitForRealized(window, true, null));
88 System.out.println("Window: "+window.getClass().getName());
89
90 final GLDrawable drawable = factory.createGLDrawable(window);
91 Assert.assertNotNull(drawable);
92 drawable.setRealized(true);
93
94 final GLAutoDrawableDelegate glad = new GLAutoDrawableDelegate(drawable, null, window, false, null) {
95 @Override
96 protected void destroyImplInLock() {
97 super.destroyImplInLock(); // destroys drawable/context
98 window.destroy(); // destroys the actual window, incl. the device
99 }
100 };
101
102 window.setWindowDestroyNotifyAction( new Runnable() {
103 @Override
104 public void run() {
106 } } );
107
108 window.addWindowListener(new WindowAdapter() {
109 @Override
110 public void windowRepaint(final WindowUpdateEvent e) {
111 glad.windowRepaintOp();
112 }
113
114 @Override
115 public void windowResized(final WindowEvent e) {
116 glad.windowResizedOp(window.getSurfaceWidth(), window.getSurfaceHeight());
117 }
118 });
119
120 glad.addGLEventListener(demo);
121
122 final QuitAdapter quitAdapter = new QuitAdapter();
123 //glWindow.addKeyListener(new TraceKeyAdapter(quitAdapter));
124 //glWindow.addWindowListener(new TraceWindowAdapter(quitAdapter));
125 window.addKeyListener(quitAdapter);
126 window.addWindowListener(quitAdapter);
127
128 final Animator animator = new Animator(0 /* w/o AWT */);
129 animator.setUpdateFPSFrames(60, System.err);
130 animator.add(glad);
131 animator.start();
132 Assert.assertTrue(animator.isStarted());
133 Assert.assertTrue(animator.isAnimating());
134
135 while(!quitAdapter.shouldQuit() && animator.isAnimating() && animator.getTotalFPSDuration()<duration) {
136 Thread.sleep(100);
137 }
138 System.out.println("Fin start ...");
139
140 animator.stop();
141 Assert.assertFalse(animator.isAnimating());
142 Assert.assertFalse(animator.isStarted());
143 glad.destroy();
144 System.out.println("Fin Drawable: "+drawable);
145 System.out.println("Fin Window: "+window);
146 }
147
148 @Test
149 public void testOnScreenDblBuf() throws InterruptedException {
150 final GLCapabilities reqGLCaps = new GLCapabilities( GLProfile.getGL2ES2() );
151 doTest(reqGLCaps, new GearsES2(1));
152 }
153
154 public static void main(final String args[]) throws IOException {
155 for(int i=0; i<args.length; i++) {
156 if(args[i].equals("-time")) {
157 i++;
158 duration = MiscUtils.atol(args[i], duration);
159 }
160 }
161 org.junit.runner.JUnitCore.main(TestGLAutoDrawableDelegateNEWT.class.getName());
162 }
163
164}
static Window createWindow(final CapabilitiesImmutable caps)
Create a top level Window entity on the default Display and default Screen.
NEWT Window events are provided for notification purposes ONLY.
Fully functional GLAutoDrawable implementation utilizing already created GLDrawable and GLContext ins...
final void windowResizedOp(final int newWidth, final int newHeight)
Handling resize events from the windowing system.
final void windowDestroyNotifyOp()
Implementation to handle destroy notifications from the windowing system.
final void windowRepaintOp()
Default implementation to handle repaint events from the windowing system.
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext....
Specifies a set of OpenGL capabilities.
abstract GLDrawable createGLDrawable(NativeSurface target)
Returns an unrealized GLDrawable according to it's chosen GLCapabilitiesImmutable,...
static GLDrawableFactory getFactory(final GLProfile glProfile)
Returns the sole GLDrawableFactory instance.
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
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
static boolean waitForVisible(final Window win, final boolean visible, final Runnable waitAction)
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)
synchronized boolean isStarted()
Indicates whether this animator has been started.
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
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.
Specifying NEWT's Window functionality:
Definition: Window.java:115
void addKeyListener(KeyListener l)
Appends the given com.jogamp.newt.event.KeyListener to the end of the list.
void addWindowListener(WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
void setWindowDestroyNotifyAction(Runnable r)
Set a custom action handling destruction issued by a toolkit triggered window destroy replacing the d...
void setSize(int width, int height)
Sets the size of the window's client area in window units, excluding decorations.
void setVisible(boolean visible)
Calls setVisible(true, visible), i.e.
void destroy()
Destroys this window incl.releasing all related resources.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Specifies an immutable set of OpenGL capabilities.
GLProfile getGLProfile()
Returns the GL profile you desire or used by the drawable.
An abstraction for an OpenGL rendering target.
Definition: GLDrawable.java:51
void setRealized(boolean realized)
Indicates to GLDrawable implementations whether the underlying surface has been created and can be dr...
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.