JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestNewtCanvasSWTGLn.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 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.swt;
30
31import com.jogamp.opengl.GLAutoDrawable;
32import com.jogamp.opengl.GLCapabilities;
33import com.jogamp.opengl.GLCapabilitiesImmutable;
34import com.jogamp.opengl.GLEventListener;
35import com.jogamp.opengl.GLProfile;
36
37import org.eclipse.swt.SWT;
38import org.eclipse.swt.layout.FillLayout;
39import org.eclipse.swt.widgets.Composite;
40import org.eclipse.swt.widgets.Display;
41import org.eclipse.swt.widgets.Shell;
42import org.junit.Assert;
43import org.junit.Assume;
44import org.junit.Before;
45import org.junit.BeforeClass;
46import org.junit.After;
47import org.junit.Test;
48import org.junit.FixMethodOrder;
49import org.junit.runners.MethodSorters;
50
51import com.jogamp.nativewindow.swt.SWTAccessor;
52import com.jogamp.newt.NewtFactory;
53import com.jogamp.newt.Screen;
54import com.jogamp.newt.opengl.GLWindow;
55import com.jogamp.newt.swt.NewtCanvasSWT;
56import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
57import com.jogamp.opengl.test.junit.jogl.demos.es2.MultisampleDemoES2;
58import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
59import com.jogamp.opengl.test.junit.util.MiscUtils;
60import com.jogamp.opengl.test.junit.util.NewtTestUtil;
61import com.jogamp.opengl.test.junit.util.SWTTestUtil;
62import com.jogamp.opengl.test.junit.util.TestUtil;
63import com.jogamp.opengl.test.junit.util.UITestCase;
64import com.jogamp.opengl.util.Animator;
65import com.jogamp.opengl.util.GLReadBufferUtil;
66import com.jogamp.opengl.util.texture.TextureIO;
67
68/**
69 * Tests that a basic SWT app can open without crashing under different GL profiles
70 * _and_ custom GLCapabilities.
71 * <p>
72 * Uses JOGL's NewtCanvasSWT, which allows to be a native container of a NEWT Window.<br/>
73 * This method allows utilizing custom GLCapability settings,
74 * independent from the already instantiated SWT visual.
75 * </p>
76 * <p>
77 * Note that {@link SWTAccessor#invoke(boolean, Runnable)} is still used to comply w/
78 * SWT running on Mac OSX, i.e. to enforce UI action on the main thread.
79 * </p>
80 */
81@FixMethodOrder(MethodSorters.NAME_ASCENDING)
82public class TestNewtCanvasSWTGLn extends UITestCase {
83
84 static int duration = 250;
85
86 static final int iwidth = 640;
87 static final int iheight = 480;
88
89 Display display = null;
90 Shell shell = null;
91 Composite composite = null;
92 com.jogamp.newt.Display swtNewtDisplay = null;
93
94 @BeforeClass
95 public static void startup() {
96 System.out.println( "GLProfile " + GLProfile.glAvailabilityToString() );
97 }
98
99 @Before
100 public void init() {
101 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
102 @Override
103 public void run() {
104 display = new Display();
105 Assert.assertNotNull( display );
106 }});
107 display.syncExec(new Runnable() {
108 @Override
109 public void run() {
110 shell = new Shell( display );
111 Assert.assertNotNull( shell );
112 shell.setLayout( new FillLayout() );
113 composite = new Composite( shell, SWT.NONE );
114 composite.setLayout( new FillLayout() );
115 Assert.assertNotNull( composite );
116 }});
117 swtNewtDisplay = NewtFactory.createDisplay(null, false); // no-reuse
118 }
119
120 @After
121 public void release() {
122 Assert.assertNotNull( display );
123 Assert.assertNotNull( shell );
124 Assert.assertNotNull( composite );
125 try {
126 display.syncExec(new Runnable() {
127 @Override
128 public void run() {
129 composite.dispose();
130 shell.dispose();
131 }});
132 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
133 @Override
134 public void run() {
135 display.dispose();
136 }});
137 }
138 catch( final Throwable throwable ) {
139 throwable.printStackTrace();
140 Assume.assumeNoException( throwable );
141 }
142 swtNewtDisplay = null;
143 display = null;
144 shell = null;
145 composite = null;
146 }
147
148 protected void runTestAGL( final GLCapabilitiesImmutable caps, final GLEventListener demo,
149 final boolean postAttach, final boolean useAnimator ) throws InterruptedException {
150 final GLReadBufferUtil screenshot = new GLReadBufferUtil(false, false);
151
152 final Screen screen = NewtFactory.createScreen(swtNewtDisplay, 0);
153 final GLWindow glWindow1 = GLWindow.create(screen, caps);
154 Assert.assertNotNull(glWindow1);
155 Assert.assertEquals(false, glWindow1.isVisible());
156 Assert.assertEquals(false, glWindow1.isNativeValid());
157 Assert.assertNull(glWindow1.getParent());
158 glWindow1.addGLEventListener(demo);
159 glWindow1.addGLEventListener(new GLEventListener() {
160 int displayCount = 0;
161 @Override
162 public void init(final GLAutoDrawable drawable) { }
163 @Override
164 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
165 @Override
166 public void display(final GLAutoDrawable drawable) {
167 if(displayCount < 3) {
168 snapshot(displayCount++, null, drawable.getGL(), screenshot, TextureIO.PNG, null);
169 }
170 }
171 @Override
172 public void dispose(final GLAutoDrawable drawable) { }
173 });
174
175 final NewtCanvasSWT canvas1 = NewtCanvasSWT.create( composite, 0, postAttach ? null : glWindow1 );
176 Assert.assertNotNull( canvas1 );
177
178 display.syncExec( new Runnable() {
179 @Override
180 public void run() {
181 shell.setText( getSimpleTestName(".") );
182 shell.setSize( 640, 480 );
183 shell.open();
184 }
185 });
186
187 if(postAttach) {
188 display.syncExec( new Runnable() {
189 @Override
190 public void run() {
191 canvas1.setNEWTChild(glWindow1);
192 } } );
193 }
194
195 // final SWTTestUtil.WaitAction2 awtRobotWaitAction = new SWTTestUtil.WaitAction2(glWindow1.getScreen().getDisplay().getEDTUtil(), display, true, TestUtil.TIME_SLICE);
196 // final SWTTestUtil.WaitAction2 generalWaitAction = new SWTTestUtil.WaitAction2(glWindow1.getScreen().getDisplay().getEDTUtil(), display, true, 10);
197 final SWTTestUtil.WaitAction awtRobotWaitAction = new SWTTestUtil.WaitAction(display, true, TestUtil.TIME_SLICE);
198 final SWTTestUtil.WaitAction generalWaitAction = new SWTTestUtil.WaitAction(display, true, 10);
199
200 Assert.assertTrue("GLWindow didn't become visible natively!", NewtTestUtil.waitForRealized(glWindow1, true, awtRobotWaitAction));
201
202 System.err.println("GLWindow LOS.0: "+glWindow1.getLocationOnScreen(null));
203 System.err.println("NewtCanvasSWT LOS.0: "+canvas1.getNativeWindow().getLocationOnScreen(null));
204
205 // canvas1.update();
206
207 Animator anim;
208 if(useAnimator) {
209 anim = new Animator(glWindow1);
210 anim.start();
211 } else {
212 anim = null;
213 }
214
215 final long lStartTime = System.currentTimeMillis();
216 final long lEndTime = lStartTime + duration;
217 try {
218 while( (System.currentTimeMillis() < lEndTime) && !canvas1.isDisposed() ) {
219 generalWaitAction.run();
220 }
221 } catch( final Throwable throwable ) {
222 throwable.printStackTrace();
223 Assume.assumeNoException( throwable );
224 }
225 if(null != anim) {
226 anim.stop();
227 }
228
229 display.syncExec( new Runnable() {
230 @Override
231 public void run() {
232 if( !canvas1.isDisposed() ) {
233 canvas1.dispose();
234 }
235 } } );
236 }
237
238 @Test
239 public void preAttach_WithAnimator() throws InterruptedException {
240 runTestAGL( new GLCapabilities(GLProfile.getGL2ES2()), new GearsES2(), false /* postAttach */, true /* animator */);
241 }
242
243 @Test
244 public void preAttach_NoAnimator() throws InterruptedException {
245 runTestAGL( new GLCapabilities(GLProfile.getGL2ES2()), new GearsES2(), false /* postAttach */, false /* animator */);
246 }
247
248 @Test
249 public void postAttach_WithAnimator() throws InterruptedException {
250 runTestAGL( new GLCapabilities(GLProfile.getGL2ES2()), new GearsES2(), true /* postAttach */, true /* animator */);
251 }
252
253 @Test
254 public void test_MultisampleAndAlpha() throws InterruptedException {
256 caps.setSampleBuffers(true);
257 caps.setNumSamples(2);
258 runTestAGL( caps, new MultisampleDemoES2(true), false /* postAttach */, false /* animator */);
259 }
260
261 public static void main(final String args[]) {
262 for(int i=0; i<args.length; i++) {
263 if(args[i].equals("-time")) {
264 duration = MiscUtils.atoi(args[++i], duration);
265 }
266 }
267 System.out.println("durationPerTest: "+duration);
268 org.junit.runner.JUnitCore.main(TestNewtCanvasSWTGLn.class.getName());
269 }
270}
static void invokeOnOSTKThread(final boolean blocking, final Runnable runnable)
Runs the specified action in an SWT compatible OS toolkit thread, which is:
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
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
Point getLocationOnScreen(final Point storage)
Returns the window's top-left client-area position in the screen.
Definition: GLWindow.java:643
final NativeWindow getParent()
Definition: GLWindow.java:282
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
SWT Canvas containing a NEWT Window using native parenting.
NativeWindow getNativeWindow()
Returns the associated NativeWindow of this NativeWindowHolder, which is identical to getNativeSurfac...
static NewtCanvasSWT create(final Composite parent, final int style, final Window child)
Creates an instance using NewtCanvasSWT(Composite, int, Window) on the SWT thread.
void dispose()
Destroys this resource:
Window setNEWTChild(final Window newChild)
Sets a new NEWT child, provoking reparenting.
Specifies a set of OpenGL capabilities.
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 String glAvailabilityToString(final AbstractGraphicsDevice device)
Definition: GLProfile.java:333
static GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
Tests that a basic SWT app can open without crashing under different GL profiles and custom GLCapabil...
void runTestAGL(final GLCapabilitiesImmutable caps, final GLEventListener demo, final boolean postAttach, final boolean useAnimator)
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
static boolean waitForRealized(final Screen screen, final boolean realized, final Runnable waitAction)
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
Utility to read out the current FB to TextureData, optionally writing the data back to a texture obje...
static final String PNG
Constant which can be used as a file suffix to indicate a PNG file, value {@value}.
Definition: TextureIO.java:171
Point getLocationOnScreen(Point point)
Returns the window's top-left client-area position in the screen.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Specifies an immutable set of OpenGL capabilities.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.