JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestSharedContextVBOES2NEWT4.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;
32import java.util.concurrent.atomic.AtomicBoolean;
33
34import com.jogamp.newt.opengl.GLWindow;
35
36import com.jogamp.nativewindow.util.InsetsImmutable;
37import com.jogamp.opengl.GLAnimatorControl;
38import com.jogamp.opengl.GLAutoDrawable;
39import com.jogamp.opengl.GLCapabilities;
40import com.jogamp.opengl.GLContext;
41import com.jogamp.opengl.GLProfile;
42
43import com.jogamp.opengl.util.Animator;
44import com.jogamp.opengl.test.junit.util.NewtTestUtil;
45import com.jogamp.opengl.test.junit.util.GLTestUtil;
46import com.jogamp.opengl.test.junit.util.MiscUtils;
47import com.jogamp.opengl.test.junit.util.UITestCase;
48import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
49
50import org.junit.Assert;
51import org.junit.BeforeClass;
52import org.junit.Test;
53import org.junit.FixMethodOrder;
54import org.junit.runners.MethodSorters;
55
56/**
57 * Expected error test sharing w/ different shared-master context,
58 * i.e. 3rd instance using GL buffers from 1st instance
59 * but the context from the 2nd instance.
60 * <p>
61 * This is achieved by using the 1st GLWindow as the <i>master</i>
62 * and synchronizing via GLSharedContextSetter to postpone creation
63 * of the 2nd and 3rd GLWindow until the 1st GLWindow's GLContext becomes created.
64 * </p>
65 */
66@FixMethodOrder(MethodSorters.NAME_ASCENDING)
68 static GLProfile glp;
69 static GLCapabilities caps;
70 static int width, height;
71
72 @BeforeClass
73 public static void initClass() {
76 Assert.assertNotNull(glp);
77 caps = new GLCapabilities(glp);
78 Assert.assertNotNull(caps);
79 width = 256;
80 height = 256;
81 } else {
82 setTestSupported(false);
83 }
84 }
85
86 protected GLWindow createGLWindow(final int x, final int y, final GearsES2 gears) throws InterruptedException {
87 final GLWindow glWindow = GLWindow.create(caps);
88 Assert.assertNotNull(glWindow);
89 glWindow.setPosition(x, y);
90 glWindow.setTitle("Shared Gears NEWT Test: "+x+"/"+y+" shared true");
91 glWindow.setSize(width, height);
92 glWindow.addGLEventListener(gears);
93
94 return glWindow;
95 }
96
97 @Test
98 public void test01() throws InterruptedException {
99 final Animator animator = new Animator(0 /* w/o AWT */);
100 animator.start();
101
102 final GearsES2 g1 = new GearsES2(0);
103 final GLWindow f1 = createGLWindow(0, 0, g1);
104 animator.add(f1);
105 final InsetsImmutable insets = f1.getInsets();
106
107 final GearsES2 g2 = new GearsES2(0);
108 g2.setSharedGears(g1);
109 final GLWindow f2 = createGLWindow(f1.getX()+width+insets.getTotalWidth(),
110 f1.getY()+0, g2);
111 f2.setSharedAutoDrawable(f1);
112 animator.add(f2);
113 f2.setVisible(true);
114
115 final GearsES2 g3 = new GearsES2(0);
116 g3.setSharedGears(g1); // GL objects from 1st instance
117 final GLWindow f3 = createGLWindow(f1.getX()+0,
118 f1.getY()+height+insets.getTotalHeight(), g3);
119 f3.setSharedAutoDrawable(f2); // GL context from 2nd instance: ERROR! (Mixed master)
120 animator.add(f3);
121 final AtomicBoolean gotAnimException = new AtomicBoolean(false);
122 final AtomicBoolean gotOtherException = new AtomicBoolean(false);
124 @Override
125 public void uncaughtException(final GLAnimatorControl _animator, final GLAutoDrawable _drawable, final Throwable _cause) {
126 if( _animator == animator && _drawable == f3 && _cause instanceof RuntimeException ) {
127 System.err.println("Caught expected exception: "+_cause.getMessage());
128 gotAnimException.set(true);
129 } else {
130 System.err.println("Caught unexpected exception: "+_cause.getMessage());
131 _cause.printStackTrace();
132 gotOtherException.set(true);
133 }
134 }
135 });
136 f3.setVisible(true);
137
138 Assert.assertTrue(NewtTestUtil.waitForRealized(f1, false, null));
139 Assert.assertTrue(NewtTestUtil.waitForVisible(f1, false, null));
140 Assert.assertTrue(GLTestUtil.waitForContextCreated(f1, false, null));
141
142 Assert.assertTrue(NewtTestUtil.waitForRealized(f2, true, null));
143 Assert.assertTrue(NewtTestUtil.waitForVisible(f2, true, null));
144 Assert.assertTrue(GLTestUtil.waitForContextCreated(f2, false, null));
145
146 Assert.assertTrue(NewtTestUtil.waitForRealized(f3, true, null));
147 Assert.assertTrue(NewtTestUtil.waitForVisible(f3, true, null));
148 Assert.assertTrue(GLTestUtil.waitForContextCreated(f3, false, null));
149
150 f1.setVisible(true); // kick off f1 GLContext .. and hence allow f2 + f3 creation
151
152 Assert.assertTrue(NewtTestUtil.waitForRealized(f1, true, null));
153 Assert.assertTrue(NewtTestUtil.waitForVisible(f1, true, null));
154 Assert.assertTrue(GLTestUtil.waitForContextCreated(f1, true, null));
155 Assert.assertTrue("Gears1 not initialized", g1.waitForInit(true));
156
157 Assert.assertTrue(NewtTestUtil.waitForRealized(f2, true, null));
158 Assert.assertTrue(NewtTestUtil.waitForVisible(f2, true, null));
159 Assert.assertTrue(GLTestUtil.waitForContextCreated(f2, true, null));
160 Assert.assertTrue("Gears2 not initialized", g2.waitForInit(true));
161
162 Assert.assertTrue(NewtTestUtil.waitForRealized(f3, true, null));
163 Assert.assertTrue(NewtTestUtil.waitForVisible(f3, true, null));
164 Assert.assertTrue(GLTestUtil.waitForContextCreated(f3, true, null));
165 Assert.assertTrue("Gears3 not initialized", g3.waitForInit(true));
166
167 Assert.assertFalse("Unexpected exception (animator) caught", gotAnimException.get());
168 Assert.assertFalse("Unexpected exception (other) caught", gotOtherException.get());
169
170 final GLContext ctx1 = f1.getContext();
171 final GLContext ctx2 = f2.getContext();
172 final GLContext ctx3 = f3.getContext();
173 {
174 final List<GLContext> ctx1Shares = ctx1.getCreatedShares();
175 final List<GLContext> ctx2Shares = ctx2.getCreatedShares();
176 final List<GLContext> ctx3Shares = ctx3.getCreatedShares();
177 MiscUtils.dumpSharedGLContext("XXX-C-3.1", ctx1);
178 MiscUtils.dumpSharedGLContext("XXX-C-3.2", ctx2);
179 MiscUtils.dumpSharedGLContext("XXX-C-3.3", ctx3);
180
181 Assert.assertTrue("Ctx1 is not shared", ctx1.isShared());
182 Assert.assertTrue("Ctx2 is not shared", ctx2.isShared());
183 Assert.assertTrue("Ctx3 is not shared", ctx3.isShared());
184 Assert.assertEquals("Ctx1 has unexpected number of created shares", 2, ctx1Shares.size());
185 Assert.assertEquals("Ctx2 has unexpected number of created shares", 2, ctx2Shares.size());
186 Assert.assertEquals("Ctx3 has unexpected number of created shares", 2, ctx3Shares.size());
187 Assert.assertEquals("Ctx1 Master Context is different", ctx1, ctx1.getSharedMaster());
188 Assert.assertEquals("Ctx2 Master Context is different", ctx1, ctx2.getSharedMaster());
189 Assert.assertEquals("Ctx3 Master Context is different", ctx2, ctx3.getSharedMaster()); // Mixed master!
190 }
191
192 Assert.assertTrue("Gears1 is shared", !g1.usesSharedGears());
193 Assert.assertTrue("Gears2 is not shared", g2.usesSharedGears());
194 Assert.assertTrue("Gears3 is not shared", g3.usesSharedGears());
195
196 try {
197 Thread.sleep(duration);
198 } catch(final Exception e) {
199 e.printStackTrace();
200 }
201 animator.stop();
202 Assert.assertEquals(false, animator.isAnimating());
203
204 System.err.println("XXX Destroy in clean order NOW");
205 f3.destroy();
206 f2.destroy();
207 f1.destroy();
208
209 Assert.assertTrue(NewtTestUtil.waitForVisible(f1, false, null));
210 Assert.assertTrue(NewtTestUtil.waitForRealized(f1, false, null));
211 Assert.assertTrue(NewtTestUtil.waitForVisible(f2, false, null));
212 Assert.assertTrue(NewtTestUtil.waitForRealized(f2, false, null));
213 Assert.assertTrue(NewtTestUtil.waitForVisible(f3, false, null));
214 Assert.assertTrue(NewtTestUtil.waitForRealized(f3, false, null));
215 }
216
217 static long duration = 1000; // ms
218
219 public static void main(final String args[]) {
220 for(int i=0; i<args.length; i++) {
221 if(args[i].equals("-time")) {
222 i++;
223 try {
224 duration = Integer.parseInt(args[i]);
225 } catch (final Exception ex) { ex.printStackTrace(); }
226 }
227 }
228 /**
229 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
230 System.err.println("Press enter to continue");
231 System.err.println(stdin.readLine()); */
232 org.junit.runner.JUnitCore.main(TestSharedContextVBOES2NEWT4.class.getName());
233 }
234}
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final void setPosition(final int x, final int y)
Sets the location of the window's client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:525
final void setTitle(final String title)
Definition: GLWindow.java:297
final int getX()
Returns the current x position of this window, relative to it's parent.
Definition: GLWindow.java:436
final int getY()
Returns the current y position of the top-left corner of the client area relative to it's parent in w...
Definition: GLWindow.java:441
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
Definition: GLWindow.java:625
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
final InsetsImmutable getInsets()
Returns the insets defined as the width and height of the window decoration on the left,...
Definition: GLWindow.java:431
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
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 GLContext getSharedMaster()
Returns the shared master GLContext of this GLContext if shared, otherwise return null.
Definition: GLContext.java:272
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.
Expected error test sharing w/ different shared-master context, i.e.
GLWindow createGLWindow(final int x, final int y, final GearsES2 gears)
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
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 void setUncaughtExceptionHandler(final UncaughtExceptionHandler handler)
Set the handler invoked when this animator abruptly stops due to an uncaught exception from one of it...
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
Immutable insets representing rectangular window decoration insets on all four edges in window units.
A registered UncaughtExceptionHandler instance is invoked when an animator abruptly stops due to an u...
An animator control interface, which implementation may drive a com.jogamp.opengl....
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.