JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestSWTJOGLGLCanvas01GLn.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;
42
43import org.junit.Assert;
44import org.junit.Assume;
45import org.junit.Before;
46import org.junit.BeforeClass;
47import org.junit.After;
48import org.junit.Test;
49import org.junit.FixMethodOrder;
50import org.junit.runners.MethodSorters;
51
52import com.jogamp.nativewindow.swt.SWTAccessor;
53import com.jogamp.opengl.swt.GLCanvas;
54import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
55import com.jogamp.opengl.test.junit.jogl.demos.es2.MultisampleDemoES2;
56import com.jogamp.opengl.test.junit.util.SWTTestUtil;
57import com.jogamp.opengl.test.junit.util.UITestCase;
58import com.jogamp.opengl.util.Animator;
59import com.jogamp.opengl.util.GLReadBufferUtil;
60import com.jogamp.opengl.util.texture.TextureIO;
61
62/**
63 * Tests that a basic SWT app can open without crashing under different GL profiles.
64 * <p>
65 * Uses JOGL's new SWT GLCanvas,
66 * which allows utilizing custom GLCapability settings,
67 * independent from the already instantiated SWT visual.
68 * </p>
69 * <p>
70 * Note that {@link SWTAccessor#invokeOnOSTKThread(boolean, Runnable)} is still used to comply w/
71 * SWT running on Mac OSX, i.e. to enforce UI action on the main thread.
72 * </p>
73 * @author Wade Walker, et al.
74 */
75@FixMethodOrder(MethodSorters.NAME_ASCENDING)
77
78 static int duration = 250;
79 static boolean doAnimation = true;
80
81 static final int iwidth = 640;
82 static final int iheight = 480;
83
84 Display display = null;
85 Shell shell = null;
86 Composite composite = null;
87
88 @BeforeClass
89 public static void startup() {
90 System.out.println( "GLProfile " + GLProfile.glAvailabilityToString() );
91 }
92
93 @Before
94 public void init() {
95 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
96 @Override
97 public void run() {
98 display = new Display();
99 Assert.assertNotNull( display );
100 SWTAccessor.printInfo(System.err, display);
101 }});
102 display.syncExec(new Runnable() {
103 @Override
104 public void run() {
105 shell = new Shell( display );
106 Assert.assertNotNull( shell );
107 shell.setLayout( new FillLayout() );
108 composite = new Composite( shell, SWT.NO_BACKGROUND );
109 composite.setLayout( new FillLayout() );
110 Assert.assertNotNull( composite );
111 }});
112 }
113
114 @After
115 public void release() {
116 Assert.assertNotNull( display );
117 Assert.assertNotNull( shell );
118 Assert.assertNotNull( composite );
119 try {
120 display.syncExec(new Runnable() {
121 @Override
122 public void run() {
123 composite.dispose();
124 shell.dispose();
125 }});
126 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
127 @Override
128 public void run() {
129 display.dispose();
130 }});
131 }
132 catch( final Throwable throwable ) {
133 throwable.printStackTrace();
134 Assume.assumeNoException( throwable );
135 }
136 display = null;
137 shell = null;
138 composite = null;
139 }
140
141 protected void runTestAGL( final GLCapabilitiesImmutable caps, final GLEventListener demo ) throws InterruptedException {
142 final GLReadBufferUtil screenshot = new GLReadBufferUtil(false, false);
143
144 final GLCanvas canvas = GLCanvas.create( composite, 0, caps, null);
145 Assert.assertNotNull( canvas );
146
147 canvas.addGLEventListener( demo );
149 int displayCount = 0;
150 @Override
151 public void init(final GLAutoDrawable drawable) { }
152 @Override
153 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
154 @Override
155 public void display(final GLAutoDrawable drawable) {
156 if(displayCount < 3) {
157 snapshot(displayCount++, null, drawable.getGL(), screenshot, TextureIO.PNG, null);
158 }
159 }
160 @Override
161 public void dispose(final GLAutoDrawable drawable) { }
162 });
163
164 display.syncExec(new Runnable() {
165 @Override
166 public void run() {
167 shell.setText( getSimpleTestName(".") );
168 shell.setSize( 640, 480 );
169 shell.open();
170 } } );
171
172 final Animator anim = new Animator();
173 if(doAnimation) {
174 anim.add(canvas);
175 anim.start();
176 }
177
178 final SWTTestUtil.WaitAction waitAction = new SWTTestUtil.WaitAction(display, true, 16);
179 final long lStartTime = System.currentTimeMillis();
180 final long lEndTime = lStartTime + duration;
181 try {
182 while( (System.currentTimeMillis() < lEndTime) && !canvas.isDisposed() ) {
183 waitAction.run();
184 }
185 } catch( final Throwable throwable ) {
186 throwable.printStackTrace();
187 Assume.assumeNoException( throwable );
188 }
189
190 anim.stop();
191
192 display.syncExec(new Runnable() {
193 @Override
194 public void run() {
195 canvas.dispose();
196 } } );
197 }
198
199 @Test
200 public void test() throws InterruptedException {
201 runTestAGL( new GLCapabilities(GLProfile.getGL2ES2()), new GearsES2() );
202 }
203
204 @Test
205 public void test_MultisampleAndAlpha() throws InterruptedException {
207 caps.setSampleBuffers(true);
208 caps.setNumSamples(2);
209 runTestAGL( caps, new MultisampleDemoES2(true) );
210 }
211
212 static int atoi(final String a) {
213 int i=0;
214 try {
215 i = Integer.parseInt(a);
216 } catch (final Exception ex) { ex.printStackTrace(); }
217 return i;
218 }
219
220 public static void main(final String args[]) {
221 for(int i=0; i<args.length; i++) {
222 if(args[i].equals("-time")) {
223 duration = atoi(args[++i]);
224 } else if(args[i].equals("-still")) {
225 doAnimation = false;
226 }
227 }
228 System.out.println("durationPerTest: "+duration);
229 org.junit.runner.JUnitCore.main(TestSWTJOGLGLCanvas01GLn.class.getName());
230 }
231}
static void invokeOnOSTKThread(final boolean blocking, final Runnable runnable)
Runs the specified action in an SWT compatible OS toolkit thread, which is:
static void printInfo(final PrintStream out, final Display d)
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
A heavyweight AWT component which provides OpenGL rendering support.
Definition: GLCanvas.java:170
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:1065
Tests that a basic SWT app can open without crashing under different GL profiles.
void runTestAGL(final GLCapabilitiesImmutable caps, final GLEventListener demo)
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
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
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
Specifies an immutable set of OpenGL capabilities.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.