JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestSharedContextVBOES2NEWT1.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 com.jogamp.newt.opengl.GLWindow;
32
33import com.jogamp.nativewindow.util.InsetsImmutable;
34import com.jogamp.opengl.GLAutoDrawable;
35import com.jogamp.opengl.GLCapabilities;
36import com.jogamp.opengl.GLContext;
37import com.jogamp.opengl.GLDrawableFactory;
38import com.jogamp.opengl.GLProfile;
39
40import com.jogamp.opengl.util.Animator;
41import com.jogamp.opengl.test.junit.util.NewtTestUtil;
42import com.jogamp.opengl.test.junit.util.GLTestUtil;
43import com.jogamp.opengl.test.junit.util.MiscUtils;
44import com.jogamp.opengl.test.junit.util.UITestCase;
45import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
46
47import org.junit.Assert;
48import org.junit.BeforeClass;
49import org.junit.Test;
50import org.junit.FixMethodOrder;
51import org.junit.runners.MethodSorters;
52
53/**
54 * Sharing the VBO of 3 GearsES2 instances, each in their own GLWindow.
55 * <p>
56 * This is achieved by creating a <i>master</i> GLContext to an offscreen invisible GLAutoDrawable,
57 * which is then shared by the 3 GLContext of the three GLWindow instances.
58 * </p>
59 * <p>
60 * The original VBO is created by attaching a GearsES2 instance to
61 * the <i>master</i> GLAutoDrawable and initializing it.
62 * </p>
63 * <p>
64 * Above method allows random creation of all GLWindow instances.
65 * </p>
66 * <p>
67 * One tests uses only one animator, where the GLWindow, GLDrawable and GLContext
68 * creation of all 3 GLWindows is sequential.
69 * </p>
70 * <p>
71 * Another tests uses 3 animator, one for each GLWindow,
72 * where the GLWindow, GLDrawable and GLContext creation
73 * of all 3 GLWindows is <i>random</i>.
74 * This fact benefits from the <i>master</i> GLContext/GLAutoDrawable,
75 * since it is guaranteed it exist and is realized at the time of the <i>shared</i>
76 * GLWindow creation.
77 * </p>
78 */
79@FixMethodOrder(MethodSorters.NAME_ASCENDING)
81 static GLProfile glp;
82 static GLCapabilities caps;
83 static int width, height;
84 GLAutoDrawable sharedDrawable;
85 GearsES2 sharedGears;
86
87 @BeforeClass
88 public static void initClass() {
91 Assert.assertNotNull(glp);
92 caps = new GLCapabilities(glp);
93 Assert.assertNotNull(caps);
94 width = 256;
95 height = 256;
96 } else {
97 setTestSupported(false);
98 }
99 }
100
101 private void initShared(final boolean onscreen) throws InterruptedException {
102 if(onscreen) {
103 final GLWindow glWindow = GLWindow.create(caps);
104 Assert.assertNotNull(glWindow);
105 glWindow.setSize(width, height);
106 glWindow.setVisible(true);
107 sharedDrawable = glWindow;
108 } else {
109 sharedDrawable = GLDrawableFactory.getFactory(glp).createDummyAutoDrawable(null, true /* createNewDevice */, caps, null);
110 }
111 Assert.assertNotNull(sharedDrawable);
112 final GLAutoDrawable obj = sharedDrawable;
113 Assert.assertTrue(GLTestUtil.waitForRealized(obj, true, null));
114
115 sharedGears = new GearsES2();
116 Assert.assertNotNull(sharedGears);
117 sharedDrawable.addGLEventListener(sharedGears);
118 // init and render one frame, which will setup the Gears display lists
119 sharedDrawable.display();
120 final GLContext ctxM = sharedDrawable.getContext();
121 Assert.assertTrue("Master ctx not created", GLTestUtil.waitForContextCreated(sharedDrawable, true, null));
122 Assert.assertTrue("Master Ctx is shared before shared creation", !ctxM.isShared());
123 Assert.assertTrue("Master Gears not initialized", sharedGears.waitForInit(true));
124 System.err.println("Master Gears Init done: "+sharedGears);
125 Assert.assertTrue("Master Gears is shared", !sharedGears.usesSharedGears());
126 }
127
128 private void releaseShared() {
129 Assert.assertNotNull(sharedDrawable);
130 sharedDrawable.destroy();
131 sharedDrawable = null;
132 }
133
134 protected GLWindow runTestGL(final Animator animator, final int x, final int y, final boolean useShared, final boolean vsync) throws InterruptedException {
135 final GLWindow glWindow = GLWindow.create(caps);
136 Assert.assertNotNull(glWindow);
137 glWindow.setPosition(x, y);
138 glWindow.setTitle("Shared Gears NEWT Test: "+x+"/"+y+" shared "+useShared);
139 if(useShared) {
140 glWindow.setSharedAutoDrawable(sharedDrawable);
141 }
142
143 glWindow.setSize(width, height);
144
145 final GearsES2 gears = new GearsES2(vsync ? 1 : 0);
146 if(useShared) {
147 gears.setSharedGears(sharedGears);
148 }
149 glWindow.addGLEventListener(gears);
150
151 animator.add(glWindow);
152 animator.start();
153 glWindow.setVisible(true);
154 Assert.assertTrue(NewtTestUtil.waitForRealized(glWindow, true, null));
155 Assert.assertTrue(NewtTestUtil.waitForVisible(glWindow, true, null));
156 Assert.assertTrue(GLTestUtil.waitForContextCreated(glWindow, true, null));
157
158 final GLContext sharedMasterContext = sharedDrawable.getContext();
159 MiscUtils.dumpSharedGLContext("Master Context", sharedMasterContext);
160 MiscUtils.dumpSharedGLContext("New Context", glWindow.getContext());
161 if( useShared ) {
162 Assert.assertEquals("Master Context not shared as expected", true, sharedMasterContext.isShared());
163 Assert.assertEquals("Master Context is different", sharedMasterContext, glWindow.getContext().getSharedMaster());
164 } else {
165 Assert.assertEquals("Master Context is not null", null, glWindow.getContext().getSharedMaster());
166 }
167 Assert.assertEquals("New Context not shared as expected", useShared, glWindow.getContext().isShared());
168
169 Assert.assertTrue("Gears not initialized", gears.waitForInit(true));
170 System.err.println("Slave Gears Init done: "+gears);
171 Assert.assertEquals("Gears is not shared as expected", useShared, gears.usesSharedGears());
172
173 return glWindow;
174 }
175
176 @Test
177 public void test01CommonAnimatorSharedOnscreen() throws InterruptedException {
178 initShared(true);
179 final Animator animator = new Animator(0 /* w/o AWT */);
180 final GLWindow f1 = runTestGL(animator, 0, 0, true, false);
181 final InsetsImmutable insets = f1.getInsets();
182 final GLWindow f2 = runTestGL(animator, f1.getX()+width+insets.getTotalWidth(),
183 f1.getY()+0, true, false);
184 final GLWindow f3 = runTestGL(animator, f1.getX()+0,
185 f1.getY()+height+insets.getTotalHeight(), true, false);
186 try {
187 Thread.sleep(duration);
188 } catch(final Exception e) {
189 e.printStackTrace();
190 }
191 animator.stop();
192
193 f1.destroy();
194 f2.destroy();
195 f3.destroy();
196 Assert.assertTrue(NewtTestUtil.waitForVisible(f1, false, null));
197 Assert.assertTrue(NewtTestUtil.waitForRealized(f1, false, null));
198 Assert.assertTrue(GLTestUtil.waitForContextCreated(f1, false, null));
199 Assert.assertTrue(NewtTestUtil.waitForVisible(f2, false, null));
200 Assert.assertTrue(NewtTestUtil.waitForRealized(f2, false, null));
201 Assert.assertTrue(GLTestUtil.waitForContextCreated(f2, false, null));
202 Assert.assertTrue(NewtTestUtil.waitForVisible(f3, false, null));
203 Assert.assertTrue(NewtTestUtil.waitForRealized(f3, false, null));
204 Assert.assertTrue(GLTestUtil.waitForContextCreated(f3, false, null));
205
206 releaseShared();
207 }
208
209 @Test
210 public void test02EachWithAnimatorSharedOnscreen() throws InterruptedException {
211 initShared(true);
212 final Animator animator1 = new Animator(0 /* w/o AWT */);
213 final Animator animator2 = new Animator(0 /* w/o AWT */);
214 final Animator animator3 = new Animator(0 /* w/o AWT */);
215 final GLWindow f1 = runTestGL(animator1, 0, 0, true, false);
216 final InsetsImmutable insets = f1.getInsets();
217 final GLWindow f2 = runTestGL(animator2, f1.getX()+width+insets.getTotalWidth(),
218 f1.getY()+0, true, false);
219 final GLWindow f3 = runTestGL(animator3, f1.getX()+0,
220 f1.getY()+height+insets.getTotalHeight(), true, false);
221
222 try {
223 Thread.sleep(duration);
224 } catch(final Exception e) {
225 e.printStackTrace();
226 }
227 animator1.stop();
228 animator2.stop();
229 animator3.stop();
230
231 f1.destroy();
232 f2.destroy();
233 f3.destroy();
234 Assert.assertTrue(NewtTestUtil.waitForVisible(f1, false, null));
235 Assert.assertTrue(NewtTestUtil.waitForRealized(f1, false, null));
236 Assert.assertTrue(GLTestUtil.waitForContextCreated(f1, false, null));
237 Assert.assertTrue(NewtTestUtil.waitForVisible(f2, false, null));
238 Assert.assertTrue(NewtTestUtil.waitForRealized(f2, false, null));
239 Assert.assertTrue(GLTestUtil.waitForContextCreated(f2, false, null));
240 Assert.assertTrue(NewtTestUtil.waitForVisible(f3, false, null));
241 Assert.assertTrue(NewtTestUtil.waitForRealized(f3, false, null));
242 Assert.assertTrue(GLTestUtil.waitForContextCreated(f3, false, null));
243
244 releaseShared();
245 }
246
247 @Test
248 public void test11CommonAnimatorSharedOffscreen() throws InterruptedException {
249 initShared(false);
250 final Animator animator = new Animator(0 /* w/o AWT */);
251 final GLWindow f1 = runTestGL(animator, 0, 0, true, false);
252 final InsetsImmutable insets = f1.getInsets();
253 final GLWindow f2 = runTestGL(animator, f1.getX()+width+insets.getTotalWidth(),
254 f1.getY()+0, true, false);
255 final GLWindow f3 = runTestGL(animator, f1.getX()+0,
256 f1.getY()+height+insets.getTotalHeight(), true, false);
257 try {
258 Thread.sleep(duration);
259 } catch(final Exception e) {
260 e.printStackTrace();
261 }
262 animator.stop();
263
264 f1.destroy();
265 f2.destroy();
266 f3.destroy();
267 Assert.assertTrue(NewtTestUtil.waitForVisible(f1, false, null));
268 Assert.assertTrue(NewtTestUtil.waitForRealized(f1, false, null));
269 Assert.assertTrue(GLTestUtil.waitForContextCreated(f1, false, null));
270 Assert.assertTrue(NewtTestUtil.waitForVisible(f2, false, null));
271 Assert.assertTrue(NewtTestUtil.waitForRealized(f2, false, null));
272 Assert.assertTrue(GLTestUtil.waitForContextCreated(f2, false, null));
273 Assert.assertTrue(NewtTestUtil.waitForVisible(f3, false, null));
274 Assert.assertTrue(NewtTestUtil.waitForRealized(f3, false, null));
275 Assert.assertTrue(GLTestUtil.waitForContextCreated(f3, false, null));
276
277 releaseShared();
278 }
279
280 @Test
281 public void test12EachWithAnimatorSharedOffscreen() throws InterruptedException {
282 initShared(false);
283 final Animator animator1 = new Animator(0 /* w/o AWT */);
284 final Animator animator2 = new Animator(0 /* w/o AWT */);
285 final Animator animator3 = new Animator(0 /* w/o AWT */);
286 final GLWindow f1 = runTestGL(animator1, 0, 0, true, false);
287 final InsetsImmutable insets = f1.getInsets();
288 final GLWindow f2 = runTestGL(animator2, f1.getX()+width+insets.getTotalWidth(),
289 f1.getY()+0, true, false);
290 final GLWindow f3 = runTestGL(animator3, f1.getX()+0,
291 f1.getY()+height+insets.getTotalHeight(), true, false);
292
293 try {
294 Thread.sleep(duration);
295 } catch(final Exception e) {
296 e.printStackTrace();
297 }
298 animator1.stop();
299 animator2.stop();
300 animator3.stop();
301
302 f1.destroy();
303 f2.destroy();
304 f3.destroy();
305 Assert.assertTrue(NewtTestUtil.waitForVisible(f1, false, null));
306 Assert.assertTrue(NewtTestUtil.waitForRealized(f1, false, null));
307 Assert.assertTrue(GLTestUtil.waitForContextCreated(f1, false, null));
308 Assert.assertTrue(NewtTestUtil.waitForVisible(f2, false, null));
309 Assert.assertTrue(NewtTestUtil.waitForRealized(f2, false, null));
310 Assert.assertTrue(GLTestUtil.waitForContextCreated(f2, false, null));
311 Assert.assertTrue(NewtTestUtil.waitForVisible(f3, false, null));
312 Assert.assertTrue(NewtTestUtil.waitForRealized(f3, false, null));
313 Assert.assertTrue(GLTestUtil.waitForContextCreated(f3, false, null));
314
315 releaseShared();
316 }
317
318 static long duration = 1000; // ms
319
320 public static void main(final String args[]) {
321 for(int i=0; i<args.length; i++) {
322 if(args[i].equals("-time")) {
323 i++;
324 try {
325 duration = Integer.parseInt(args[i]);
326 } catch (final Exception ex) { ex.printStackTrace(); }
327 }
328 }
329 /**
330 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
331 System.err.println("Press enter to continue");
332 System.err.println(stdin.readLine()); */
333 org.junit.runner.JUnitCore.main(TestSharedContextVBOES2NEWT1.class.getName());
334 }
335}
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
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
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
static GLDrawableFactory getFactory(final GLProfile glProfile)
Returns the sole GLDrawableFactory instance.
abstract GLAutoDrawable createDummyAutoDrawable(AbstractGraphicsDevice deviceReq, boolean createNewDevice, GLCapabilitiesImmutable capsRequested, GLCapabilitiesChooser chooser)
Creates a realized dummy GLAutoDrawable incl it's dummy, invisible NativeSurface as created with crea...
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.
Sharing the VBO of 3 GearsES2 instances, each in their own GLWindow.
GLWindow runTestGL(final Animator animator, final int x, final int y, final boolean useShared, final boolean vsync)
boolean waitForInit(final boolean initialized)
Definition: GearsES2.java:187
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 synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
Immutable insets representing rectangular window decoration insets on all four edges in window units.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
GLContext getContext()
Returns the context associated with this drawable.