JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestSWTEclipseGLCanvas01GLn.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.GL2ES1;
32import com.jogamp.opengl.GLContext;
33import com.jogamp.opengl.GLDrawableFactory;
34import com.jogamp.opengl.GLProfile;
35
36import org.eclipse.swt.SWT;
37import org.eclipse.swt.events.PaintEvent;
38import org.eclipse.swt.events.PaintListener;
39import org.eclipse.swt.graphics.Rectangle;
40import org.eclipse.swt.layout.FillLayout;
41import org.eclipse.swt.opengl.GLCanvas;
42import org.eclipse.swt.opengl.GLData;
43import org.eclipse.swt.widgets.Composite;
44import org.eclipse.swt.widgets.Display;
45import org.eclipse.swt.widgets.Event;
46import org.eclipse.swt.widgets.Listener;
47import org.eclipse.swt.widgets.Shell;
48
49import org.junit.Assert;
50import org.junit.Assume;
51import org.junit.Before;
52import org.junit.BeforeClass;
53import org.junit.After;
54import org.junit.Test;
55import org.junit.FixMethodOrder;
56import org.junit.runners.MethodSorters;
57
58import com.jogamp.nativewindow.swt.SWTAccessor;
59import com.jogamp.opengl.test.junit.jogl.demos.es1.OneTriangle;
60import com.jogamp.opengl.test.junit.util.SWTTestUtil;
61import com.jogamp.opengl.test.junit.util.UITestCase;
62
63/**
64 * Tests that a basic SWT app can open without crashing under different GL profiles.
65 * <p>
66 * Uses the SWT GLCanvas <code>org.eclipse.swt.opengl.GLCanvas</code>.
67 * </p>
68 * @author Wade Walker, et.al.
69 */
70@FixMethodOrder(MethodSorters.NAME_ASCENDING)
72
73 static int duration = 250;
74
75 static final int iwidth = 640;
76 static final int iheight = 480;
77
78 Display display = null;
79 Shell shell = null;
80 Composite composite = null;
81 GLCanvas glcanvas = null;
82 volatile boolean triangleSet = false;
83
84
85 @BeforeClass
86 public static void startup() {
87 System.out.println( "GLProfile " + GLProfile.glAvailabilityToString() );
88 }
89
90 @Before
91 public void init() {
92 triangleSet = false;
93 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
94 public void run() {
95 display = new Display();
96 Assert.assertNotNull( display );
97 } } );
98 SWTAccessor.invokeOnSWTThread(display, true, new Runnable() {
99 public void run() {
100 shell = new Shell( display );
101 Assert.assertNotNull( shell );
102 shell.setLayout( new FillLayout() );
103 composite = new Composite( shell, SWT.NONE );
104 composite.setLayout( new FillLayout() );
105 Assert.assertNotNull( composite );
106 }});
107 }
108
109 @After
110 public void release() {
111 Assert.assertNotNull( display );
112 Assert.assertNotNull( shell );
113 Assert.assertNotNull( composite );
114 try {
115 SWTAccessor.invokeOnSWTThread(display, true, new Runnable() {
116 public void run() {
117 if( null != glcanvas ) {
118 glcanvas.dispose();
119 }
120 composite.dispose();
121 shell.dispose();
122 display.dispose();
123 }});
124 }
125 catch( final Throwable throwable ) {
126 throwable.printStackTrace();
127 Assume.assumeNoException( throwable );
128 }
129 display = null;
130 shell = null;
131 composite = null;
132 glcanvas = null;
133 }
134
135 protected void runTestAGL( final GLProfile glprofile ) throws InterruptedException {
136 final GLContext glcontext[] = { null };
137
138 SWTAccessor.invokeOnSWTThread(display, true, new Runnable() {
139 public void run() {
140 shell.setText( getClass().getName() );
141 shell.setSize( 640, 480 );
142 shell.open();
143
144 final GLData gldata = new GLData();
145 gldata.doubleBuffer = true;
146
147 // need SWT.NO_BACKGROUND to prevent SWT from clearing the window
148 // at the wrong times (we use glClear for this instead)
149 glcanvas = new GLCanvas( composite, SWT.NO_BACKGROUND, gldata );
150 Assert.assertNotNull( glcanvas );
151
152 glcanvas.setCurrent();
153 glcontext[0] = GLDrawableFactory.getFactory( glprofile ).createExternalGLContext();
154 Assert.assertNotNull( glcontext[0] );
155
156 // fix the viewport when the user resizes the window
157 glcanvas.addListener( SWT.Resize, new Listener() {
158 public void handleEvent( final Event event ) {
159 final Rectangle rectangle = glcanvas.getClientArea();
160 glcanvas.setCurrent();
161 glcontext[0].makeCurrent();
162 final GL2ES1 gl = glcontext[0].getGL().getGL2ES1();
163 OneTriangle.setup( gl, rectangle.width, rectangle.height );
164 triangleSet = true;
165 glcontext[0].release();
166 System.err.println("resize");
167 }
168 });
169
170 // draw the triangle when the OS tells us that any part of the window needs drawing
171 glcanvas.addPaintListener( new PaintListener() {
172 public void paintControl( final PaintEvent paintevent ) {
173 final Rectangle rectangle = glcanvas.getClientArea();
174 glcanvas.setCurrent();
175 glcontext[0].makeCurrent();
176 final GL2ES1 gl = glcontext[0].getGL().getGL2ES1();
177 if( !triangleSet ) {
178 OneTriangle.setup( gl, rectangle.width, rectangle.height );
179 triangleSet = true;
180 }
181 OneTriangle.render( gl, rectangle.width, rectangle.height );
182 glcanvas.swapBuffers();
183 glcontext[0].release();
184 System.err.println("paint");
185 }
186 });
187 } } );
188
189 final SWTTestUtil.WaitAction generalWaitAction = new SWTTestUtil.WaitAction(display, true, 16);
190
191 final long lStartTime = System.currentTimeMillis();
192 final long lEndTime = lStartTime + duration;
193 try {
194 while( (System.currentTimeMillis() < lEndTime) && !glcanvas.isDisposed() ) {
195 SWTAccessor.invokeOnSWTThread(display, true, new Runnable() {
196 public void run() {
197 if( !triangleSet ) {
198 glcanvas.setSize( 640, 480 );
199 }
200 // glcanvas.redraw();
201 } } );
202 generalWaitAction.run();
203 }
204 } catch( final Throwable throwable ) {
205 throwable.printStackTrace();
206 Assume.assumeNoException( throwable );
207 }
208 }
209
210 @Test
211 public void test() throws InterruptedException {
212 final GLProfile glprofile = GLProfile.getGL2ES1();
213 runTestAGL( glprofile );
214 }
215
216 static int atoi(final String a) {
217 int i=0;
218 try {
219 i = Integer.parseInt(a);
220 } catch (final Exception ex) { ex.printStackTrace(); }
221 return i;
222 }
223
224 public static void main(final String args[]) {
225 for(int i=0; i<args.length; i++) {
226 if(args[i].equals("-time")) {
227 duration = atoi(args[++i]);
228 }
229 }
230 System.out.println("durationPerTest: "+duration);
231 org.junit.runner.JUnitCore.main(TestSWTEclipseGLCanvas01GLn.class.getName());
232 }
233}
static void invokeOnOSTKThread(final boolean blocking, final Runnable runnable)
Runs the specified action in an SWT compatible OS toolkit thread, which is:
static void invokeOnSWTThread(final org.eclipse.swt.widgets.Display display, final boolean blocking, final Runnable runnable)
Runs the specified action on the SWT UI thread.
Abstraction for an OpenGL rendering context.
Definition: GLContext.java:74
abstract int makeCurrent()
Makes this GLContext current on the calling thread.
abstract void release()
Releases control of this GLContext from the current thread.
abstract GL getGL()
Returns the GL pipeline object for this GLContext.
abstract GLContext createExternalGLContext()
static GLDrawableFactory getFactory(final GLProfile glProfile)
Returns the sole GLDrawableFactory instance.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static String glAvailabilityToString(final AbstractGraphicsDevice device)
Definition: GLProfile.java:333
static GLProfile getGL2ES1(final AbstractGraphicsDevice device)
Returns the GL2ES1 profile implementation, hence compatible w/ GL2ES1.
Definition: GLProfile.java:883
A heavyweight AWT component which provides OpenGL rendering support.
Definition: GLCanvas.java:170
void swapBuffers()
Swaps the front and back buffers of this drawable.
Definition: GLCanvas.java:1201
A utility class to encapsulate drawing a single triangle for unit tests.
static void setup(final GL2ES1 gl, final int width, final int height)
static void render(final GL2ES1 gl, final int width, final int height)
Tests that a basic SWT app can open without crashing under different GL profiles.
GL2ES1 getGL2ES1()
Casts this object to the GL2ES1 interface.
void setSize(int width, int height)
Requests a new width and height for this AWTGLAutoDrawable.