JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestSharedContextVBOES2SWT3.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.acore;
30
31import java.util.List;
32
33import com.jogamp.opengl.GLCapabilities;
34import com.jogamp.opengl.GLContext;
35import com.jogamp.opengl.GLProfile;
36
37import com.jogamp.nativewindow.swt.SWTAccessor;
38import com.jogamp.opengl.util.Animator;
39import com.jogamp.opengl.swt.GLCanvas;
40import com.jogamp.opengl.test.junit.util.GLTestUtil;
41import com.jogamp.opengl.test.junit.util.MiscUtils;
42import com.jogamp.opengl.test.junit.util.SWTTestUtil;
43import com.jogamp.opengl.test.junit.util.UITestCase;
44import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
45
46import org.eclipse.swt.SWT;
47import org.eclipse.swt.layout.FillLayout;
48import org.eclipse.swt.widgets.Composite;
49import org.eclipse.swt.widgets.Display;
50import org.eclipse.swt.widgets.Shell;
51import org.junit.After;
52import org.junit.Assert;
53import org.junit.Assume;
54import org.junit.Before;
55import org.junit.BeforeClass;
56import org.junit.Test;
57import org.junit.FixMethodOrder;
58import org.junit.runners.MethodSorters;
59
60/**
61 * Sharing the VBO of 3 GearsES2 instances, each in their own SWT GLCanvas.
62 * <p>
63 * This is achieved by using the 1st GLCanvas as the <i>master</i>
64 * and using the build-in blocking mechanism to postpone creation
65 * of the 2nd and 3rd GLCanvas until the 1st GLCanvas's GLContext becomes created.
66 * </p>
67 * <p>
68 * Above method allows random creation of the 1st GLCanvas <b>in theory</b>, which triggers
69 * creation of the <i>dependent</i> other GLCanvas sharing it's GLContext.<br>
70 * However, since this test may perform on the <i>main thread</i> we have
71 * to initialize all in order, since otherwise the <i>test main thread</i>
72 * itself blocks SWT GLCanvas creation ..
73 * </p>
74 * <p>
75 * On MacOS 12+ and SWT 4.26 while not using AWT (-Djava.awt.headless=true, -XstartOnFirstThread),
76 * we recently get the following Exception from SWT (suppressed):
77 * <pre>
78java.lang.NullPointerException: Cannot invoke "org.eclipse.swt.internal.cocoa.NSGraphicsContext.saveGraphicsState()" because "context" is null
79 at org.eclipse.swt.widgets.Widget.drawRect(Widget.java:764)
80 at org.eclipse.swt.widgets.Canvas.drawRect(Canvas.java:170)
81 at org.eclipse.swt.widgets.Display.windowProc(Display.java:6287)
82 at org.eclipse.swt.internal.cocoa.OS.objc_msgSendSuper(Native Method)
83 at org.eclipse.swt.widgets.Display.applicationNextEventMatchingMask(Display.java:5565)
84 at org.eclipse.swt.widgets.Display.applicationProc(Display.java:5965)
85 at org.eclipse.swt.internal.cocoa.OS.objc_msgSend(Native Method)
86 at org.eclipse.swt.internal.cocoa.NSApplication.nextEventMatchingMask(NSApplication.java:92)
87 at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3983)
88 at com.jogamp.opengl.test.junit.util.SWTTestUtil$WaitAction$1.run(SWTTestUtil.java:52)
89 at org.eclipse.swt.widgets.Synchronizer.syncExec(Synchronizer.java:183)
90 at org.eclipse.swt.widgets.Display.syncExec(Display.java:5250)
91 at com.jogamp.opengl.test.junit.util.SWTTestUtil$WaitAction.run(SWTTestUtil.java:63)
92 at com.jogamp.opengl.test.junit.jogl.acore.TestSharedContextVBOES2SWT3.test02AsyncEachAnimator(TestSharedContextVBOES2SWT3.java:376)
93 * </pre>
94 * This is not observed if running using AWT (-Djava.awt.headless=false).
95 * </p>
96 */
97@FixMethodOrder(MethodSorters.NAME_ASCENDING)
99 static GLProfile glp;
100 static GLCapabilities caps;
101 static int width, height;
102
103 @BeforeClass
104 public static void initClass() {
107 Assert.assertNotNull(glp);
108 caps = new GLCapabilities(glp);
109 Assert.assertNotNull(caps);
110 width = 256;
111 height = 256;
112 } else {
113 setTestSupported(false);
114 }
115 }
116
117 Display display = null;
118 Shell shell1 = null;
119 Composite composite1 = null;
120 Shell shell2 = null;
121 Composite composite2 = null;
122 Shell shell3 = null;
123 Composite composite3 = null;
124
125 @Before
126 public void init() {
127 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
128 @Override
129 public void run() {
130 display = new Display();
131 Assert.assertNotNull( display );
132 }});
133 display.syncExec(new Runnable() {
134 @Override
135 public void run() {
136 shell1 = new Shell( display );
137 shell1.setLayout( new FillLayout() );
138 composite1 = new Composite( shell1, SWT.NO_BACKGROUND );
139 composite1.setLayout( new FillLayout() );
140
141 shell2 = new Shell( display );
142 shell2.setLayout( new FillLayout() );
143 composite2 = new Composite( shell2, SWT.NO_BACKGROUND );
144 composite2.setLayout( new FillLayout() );
145
146 shell3 = new Shell( display );
147 shell3.setLayout( new FillLayout() );
148 composite3 = new Composite( shell3, SWT.NO_BACKGROUND );
149 composite3.setLayout( new FillLayout() );
150 }});
151 }
152
153 @After
154 public void release() {
155 Assert.assertNotNull( display );
156 Assert.assertNotNull( shell1 );
157 Assert.assertNotNull( composite1 );
158 Assert.assertNotNull( shell2 );
159 Assert.assertNotNull( composite2 );
160 Assert.assertNotNull( shell3 );
161 Assert.assertNotNull( composite3 );
162 try {
163 display.syncExec(new Runnable() {
164 @Override
165 public void run() {
166 composite3.dispose();
167 shell3.dispose();
168 composite2.dispose();
169 shell2.dispose();
170 composite1.dispose();
171 shell1.dispose();
172 }});
173 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
174 @Override
175 public void run() {
176 display.dispose();
177 }});
178 }
179 catch( final Throwable throwable ) {
180 throwable.printStackTrace();
181 Assume.assumeNoException( throwable );
182 }
183 display = null;
184 shell1 = null;
185 composite1 = null;
186 shell2 = null;
187 composite2 = null;
188 shell3 = null;
189 composite3 = null;
190 }
191
192 protected GLCanvas createGLCanvas(final Shell shell, final Composite composite, final int x, final int y, final GearsES2 gears) throws InterruptedException {
193 final GLCanvas glCanvas = GLCanvas.create( composite, 0, caps, null);
194 Assert.assertNotNull( glCanvas );
195 glCanvas.addGLEventListener(gears);
196 display.syncExec(new Runnable() {
197 @Override
198 public void run() {
199 shell.setText("SWT GLCanvas Shared Gears Test");
200 shell.setSize( width, height);
201 shell.setLocation(x, y);
202 } } );
203 return glCanvas;
204 }
205
206 @Test
207 public void test01SyncedOneAnimator() throws InterruptedException {
208 final Animator animator = new Animator();
209 final GearsES2 g1 = new GearsES2(0);
210 // g1.setUseMappedBuffers(useMappedBuffers);
211 g1.setValidateBuffers(true);
212 final GLCanvas c1 = createGLCanvas(shell1, composite1, 0, 0, g1);
213 animator.add(c1);
214
215 final GearsES2 g2 = new GearsES2(0);
216 g2.setSharedGears(g1);
217 final GLCanvas c2 = createGLCanvas(shell2, composite2, 0+width, 0+0, g2);
219 animator.add(c2);
220
221 final GearsES2 g3 = new GearsES2(0);
222 g3.setSharedGears(g1);
223 final GLCanvas c3 = createGLCanvas(shell3, composite3, 0, height, g3);
225 animator.add(c3);
226
227 display.syncExec(new Runnable() {
228 @Override
229 public void run() {
230 shell1.open(); // master ..
231 shell2.open(); // shall wait until f1 is ready
232 shell3.open(); // shall wait until f1 is ready
233 } } );
234 final long t0 = System.currentTimeMillis();
235 animator.start(); // kicks off GLContext .. and hence gears of f2 + f3 completion
236
237 final SWTTestUtil.WaitAction waitAction = new SWTTestUtil.WaitAction(display, true, 200);
238 Assert.assertEquals(true, GLTestUtil.waitForRealized(c1, true, waitAction));
239 Assert.assertEquals(true, GLTestUtil.waitForContextCreated(c1, true, waitAction));
240 Assert.assertTrue("Gears1 not initialized", g1.waitForInit(true));
241
242 Assert.assertEquals(true, GLTestUtil.waitForRealized(c2, true, waitAction));
243 Assert.assertEquals(true, GLTestUtil.waitForContextCreated(c2, true, waitAction));
244 Assert.assertTrue("Gears2 not initialized", g2.waitForInit(true));
245
246 Assert.assertEquals(true, GLTestUtil.waitForRealized(c3, true, waitAction));
247 Assert.assertEquals(true, GLTestUtil.waitForContextCreated(c3, true, waitAction));
248 Assert.assertTrue("Gears3 not initialized", g3.waitForInit(true));
249
250 final GLContext ctx1 = c1.getContext();
251 final GLContext ctx2 = c2.getContext();
252 final GLContext ctx3 = c3.getContext();
253 {
254 final List<GLContext> ctx1Shares = ctx1.getCreatedShares();
255 final List<GLContext> ctx2Shares = ctx2.getCreatedShares();
256 final List<GLContext> ctx3Shares = ctx3.getCreatedShares();
257 MiscUtils.dumpSharedGLContext("XXX-C-3.1", ctx1);
258 MiscUtils.dumpSharedGLContext("XXX-C-3.2", ctx2);
259 MiscUtils.dumpSharedGLContext("XXX-C-3.3", ctx3);
260
261 Assert.assertTrue("Ctx1 is not shared", ctx1.isShared());
262 Assert.assertTrue("Ctx2 is not shared", ctx2.isShared());
263 Assert.assertTrue("Ctx3 is not shared", ctx3.isShared());
264 Assert.assertEquals("Ctx1 has unexpected number of created shares", 2, ctx1Shares.size());
265 Assert.assertEquals("Ctx2 has unexpected number of created shares", 2, ctx2Shares.size());
266 Assert.assertEquals("Ctx3 has unexpected number of created shares", 2, ctx3Shares.size());
267 }
268
269 Assert.assertTrue("Gears1 is shared", !g1.usesSharedGears());
270 Assert.assertTrue("Gears2 is not shared", g2.usesSharedGears());
271 Assert.assertTrue("Gears3 is not shared", g3.usesSharedGears());
272
273 while(animator.isAnimating() && System.currentTimeMillis()-t0<duration) {
274 waitAction.run();
275 }
276
277 // Stopped animator allows native windowing system 'repaint' event
278 // to trigger GLAD 'display'
279 animator.stop();
280 Assert.assertEquals(false, animator.isAnimating());
281
282 display.syncExec(new Runnable() {
283 @Override
284 public void run() {
285 c3.dispose();
286 c2.dispose();
287 c1.dispose();
288 } } );
289 }
290
291 @Test
292 public void test02AsyncEachAnimator() throws InterruptedException {
293 final Animator a1 = new Animator();
294 final GearsES2 g1 = new GearsES2(0);
295 g1.setSyncObjects(g1); // this is master, since rendered we must use it as sync
296 // g1.setUseMappedBuffers(useMappedBuffers);
297 g1.setValidateBuffers(true);
298 System.err.println("TestSharedContextVBOES2SWT3.test02AsyncEachAnimator: 0.0");
299 final GLCanvas c1 = createGLCanvas(shell1, composite1, 0, 0, g1);
300 a1.add(c1);
301 display.syncExec(new Runnable() {
302 @Override
303 public void run() {
304 shell1.open();
305 } } );
306 a1.start();
307
308 System.err.println("TestSharedContextVBOES2SWT3.test02AsyncEachAnimator: 1.0");
309 final SWTTestUtil.WaitAction waitAction = new SWTTestUtil.WaitAction(display, true, 200);
310 Assert.assertEquals(true, GLTestUtil.waitForRealized(c1, true, waitAction));
311 System.err.println("TestSharedContextVBOES2SWT3.test02AsyncEachAnimator: 1.1");
312 Assert.assertEquals(true, GLTestUtil.waitForContextCreated(c1, true, waitAction));
313 System.err.println("TestSharedContextVBOES2SWT3.test02AsyncEachAnimator: 1.2");
314 Assert.assertTrue("Gears1 not initialized", g1.waitForInit(true));
315
316 System.err.println("TestSharedContextVBOES2SWT3.test02AsyncEachAnimator: 2.0");
317 final Animator a2 = new Animator();
318 final GearsES2 g2 = new GearsES2(0);
319 g2.setSharedGears(g1);
320 final GLCanvas c2 = createGLCanvas(shell2, composite2, width, 0, g2);
322 a2.add(c2);
323 display.syncExec(new Runnable() {
324 @Override
325 public void run() {
326 shell2.open();
327 } } );
328 a2.start();
329
330 System.err.println("TestSharedContextVBOES2SWT3.test02AsyncEachAnimator: 3.0");
331 final Animator a3 = new Animator();
332 final GearsES2 g3 = new GearsES2(0);
333 g3.setSharedGears(g1);
334 final GLCanvas c3 = createGLCanvas(shell3, composite3, 0, height, g3);
336 a3.add(c3);
337 display.syncExec(new Runnable() {
338 @Override
339 public void run() {
340 shell3.open();
341 } } );
342 a3.start();
343
344 System.err.println("TestSharedContextVBOES2SWT3.test02AsyncEachAnimator: 4.0");
345 Assert.assertEquals(true, GLTestUtil.waitForRealized(c2, true, waitAction));
346 System.err.println("TestSharedContextVBOES2SWT3.test02AsyncEachAnimator: 4.1: Exception "+(null != waitAction.getException(true)));
347 Assert.assertEquals(true, GLTestUtil.waitForContextCreated(c2, true, waitAction));
348 System.err.println("TestSharedContextVBOES2SWT3.test02AsyncEachAnimator: 4.2: Exception "+(null != waitAction.getException(true)));
349 Assert.assertTrue("Gears2 not initialized", g2.waitForInit(true));
350
351 System.err.println("TestSharedContextVBOES2SWT3.test02AsyncEachAnimator: 5.0: Exception "+(null != waitAction.getException(true)));
352 Assert.assertEquals(true, GLTestUtil.waitForRealized(c3, true, waitAction));
353 System.err.println("TestSharedContextVBOES2SWT3.test02AsyncEachAnimator: 5.1: Exception "+(null != waitAction.getException(true)));
354 Assert.assertEquals(true, GLTestUtil.waitForContextCreated(c3, true, waitAction));
355 System.err.println("TestSharedContextVBOES2SWT3.test02AsyncEachAnimator: 5.2: Exception "+(null != waitAction.getException(true)));
356 Assert.assertTrue("Gears3 not initialized", g3.waitForInit(true));
357
358 System.err.println("TestSharedContextVBOES2SWT3.test02AsyncEachAnimator: 6.0: Exception "+(null != waitAction.getException(true)));
359 final long t0 = System.currentTimeMillis();
360
361 final GLContext ctx1 = c1.getContext();
362 final GLContext ctx2 = c2.getContext();
363 final GLContext ctx3 = c3.getContext();
364 {
365 final List<GLContext> ctx1Shares = ctx1.getCreatedShares();
366 final List<GLContext> ctx2Shares = ctx2.getCreatedShares();
367 final List<GLContext> ctx3Shares = ctx3.getCreatedShares();
368 MiscUtils.dumpSharedGLContext("XXX-C-3.1", ctx1);
369 MiscUtils.dumpSharedGLContext("XXX-C-3.2", ctx2);
370 MiscUtils.dumpSharedGLContext("XXX-C-3.2", ctx3);
371
372 Assert.assertTrue("Ctx1 is not shared", ctx1.isShared());
373 Assert.assertTrue("Ctx2 is not shared", ctx2.isShared());
374 Assert.assertTrue("Ctx3 is not shared", ctx3.isShared());
375 Assert.assertEquals("Ctx1 has unexpected number of created shares", 2, ctx1Shares.size());
376 Assert.assertEquals("Ctx2 has unexpected number of created shares", 2, ctx2Shares.size());
377 Assert.assertEquals("Ctx3 has unexpected number of created shares", 2, ctx3Shares.size());
378 }
379
380 Assert.assertTrue("Gears1 is shared", !g1.usesSharedGears());
381 Assert.assertTrue("Gears2 is not shared", g2.usesSharedGears());
382 Assert.assertTrue("Gears3 is not shared", g3.usesSharedGears());
383
384 while(System.currentTimeMillis()-t0<duration) {
385 waitAction.run();
386 }
387 System.err.println("TestSharedContextVBOES2SWT3.test02AsyncEachAnimator: 8.0: Exception "+(null != waitAction.getException(true)));
388 // Stopped animator allows native windowing system 'repaint' event
389 // to trigger GLAD 'display'
390 a1.stop();
391 Assert.assertEquals(false, a1.isAnimating());
392 a2.stop();
393 Assert.assertEquals(false, a2.isAnimating());
394 a3.stop();
395 Assert.assertEquals(false, a3.isAnimating());
396
397 display.syncExec(new Runnable() {
398 @Override
399 public void run() {
400 c3.dispose();
401 c2.dispose();
402 c1.dispose();
403 } } );
404 }
405
406 static long duration = 2000; // ms
407
408 public static void main(final String args[]) {
409 for(int i=0; i<args.length; i++) {
410 if(args[i].equals("-time")) {
411 i++;
412 try {
413 duration = Integer.parseInt(args[i]);
414 } catch (final Exception ex) { ex.printStackTrace(); }
415 }
416 }
417 /**
418 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
419 System.err.println("Press enter to continue");
420 System.err.println(stdin.readLine()); */
421 org.junit.runner.JUnitCore.main(TestSharedContextVBOES2SWT3.class.getName());
422 }
423}
static void invokeOnOSTKThread(final boolean blocking, final Runnable runnable)
Runs the specified action in an SWT compatible OS toolkit thread, which is:
Specifies a set of OpenGL capabilities.
Abstraction for an OpenGL rendering context.
Definition: GLContext.java:74
final boolean isShared()
Returns true if this GLContext is shared, otherwise false.
Definition: GLContext.java:261
final List< GLContext > getCreatedShares()
Returns a new list of created GLContext shared with this GLContext.
Definition: GLContext.java:277
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static boolean isAvailable(final AbstractGraphicsDevice device, final String profile)
Returns the availability of a profile on a device.
Definition: GLProfile.java:305
static final String GL2ES2
The intersection of the desktop GL3, GL2 and embedded ES2 profile.
Definition: GLProfile.java:594
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
A heavyweight AWT component which provides OpenGL rendering support.
Definition: GLCanvas.java:170
final void setSharedAutoDrawable(final GLAutoDrawable sharedAutoDrawable)
Specifies an GLAutoDrawable, which OpenGL context shall be shared by this GLAutoDrawable's GLContext.
Definition: GLCanvas.java:288
GLContext getContext()
Returns the context associated with this drawable.
Definition: GLCanvas.java:1166
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:1065
Sharing the VBO of 3 GearsES2 instances, each in their own SWT GLCanvas.
GLCanvas createGLCanvas(final Shell shell, final Composite composite, final int x, final int y, final GearsES2 gears)
boolean waitForInit(final boolean initialized)
Definition: GearsES2.java:187
static boolean waitForRealized(final GLAutoDrawable glad, final boolean realized, final Runnable waitAction)
Definition: GLTestUtil.java:91
static boolean waitForContextCreated(final GLAutoDrawable autoDrawable, final boolean created, final Runnable waitAction)
Definition: GLTestUtil.java:42
static void dumpSharedGLContext(final String prefix, final GLContext self)
Definition: MiscUtils.java:271
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