JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestSharedContextVBOES2NEWT0.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.newt.opengl.GLWindow;
34
35import com.jogamp.nativewindow.util.InsetsImmutable;
36import com.jogamp.opengl.GLAutoDrawable;
37import com.jogamp.opengl.GLCapabilities;
38import com.jogamp.opengl.GLContext;
39import com.jogamp.opengl.GLProfile;
40
41import com.jogamp.opengl.util.Animator;
42import com.jogamp.opengl.test.junit.util.NewtTestUtil;
43import com.jogamp.opengl.test.junit.util.GLTestUtil;
44import com.jogamp.opengl.test.junit.util.MiscUtils;
45import com.jogamp.opengl.test.junit.util.UITestCase;
46import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
47
48import org.junit.Assert;
49import org.junit.BeforeClass;
50import org.junit.Test;
51import org.junit.FixMethodOrder;
52import org.junit.runners.MethodSorters;
53
54/**
55 * Sharing the VBO of 3 GearsES2 instances, each in their own GLWindow.
56 * <p>
57 * This is achieved by relying on the sequential creation
58 * of the 3 GLWindows with their GLDrawable and GLContext.
59 * </p>
60 */
61@FixMethodOrder(MethodSorters.NAME_ASCENDING)
63 static GLProfile glp;
64 static GLCapabilities caps;
65 static int width, height;
66
67 @BeforeClass
68 public static void initClass() {
71 Assert.assertNotNull(glp);
72 caps = new GLCapabilities(glp);
73 Assert.assertNotNull(caps);
74 width = 256;
75 height = 256;
76 } else {
77 setTestSupported(false);
78 }
79 }
80
81 protected GLWindow runTestGL(final Animator animator, final int x, final int y, final GearsES2 gears, final GLAutoDrawable sharedDrawable) throws InterruptedException {
82 final boolean useShared = null != sharedDrawable;
83 final GLWindow glWindow = GLWindow.create(caps);
84 Assert.assertNotNull(glWindow);
85 glWindow.setPosition(x, y);
86 glWindow.setTitle("Shared Gears NEWT Test: "+x+"/"+y+" shared "+useShared);
87 if(useShared) {
88 glWindow.setSharedAutoDrawable(sharedDrawable);
89 }
90 glWindow.setSize(width, height);
91 glWindow.addGLEventListener(gears);
92
93 animator.add(glWindow);
94 glWindow.setVisible(true);
95 Assert.assertTrue(NewtTestUtil.waitForRealized(glWindow, true, null));
96 Assert.assertTrue(NewtTestUtil.waitForVisible(glWindow, true, null));
97 glWindow.display();
98 Assert.assertTrue(GLTestUtil.waitForContextCreated(glWindow, true, null));
99 Assert.assertTrue("Gears not initialized", gears.waitForInit(true));
100
101 return glWindow;
102 }
103
104 @Test
105 public void test01CommonAnimatorSharedCopyBuffer() throws InterruptedException {
106 testCommonAnimatorSharedImpl(false);
107 }
108 @Test
109 public void test02CommonAnimatorMapBuffer() throws InterruptedException {
110 testCommonAnimatorSharedImpl(true);
111 }
112 private void testCommonAnimatorSharedImpl(final boolean useMappedBuffers) throws InterruptedException {
113 final Animator animator = new Animator(0 /* w/o AWT */);
114
115 //
116 // 1st
117 //
118 final GearsES2 g1 = new GearsES2(0);
119 g1.setUseMappedBuffers(useMappedBuffers);
120 g1.setValidateBuffers(true);
121 final GLWindow f1 = runTestGL(animator, 0, 0, g1, null);
122 final GLContext ctx1 = f1.getContext();
123 Assert.assertTrue("Ctx is shared before shared creation", !ctx1.isShared());
124 final InsetsImmutable insets = f1.getInsets();
125
126 MiscUtils.dumpSharedGLContext("XXX-C-1.1", ctx1);
127
128 //
129 // 2nd
130 //
131 final GearsES2 g2 = new GearsES2(0);
132 g2.setSharedGears(g1);
133 final GLWindow f2 = runTestGL(animator, f1.getX()+width+insets.getTotalWidth(),
134 f1.getY()+0, g2, f1);
135 final GLContext ctx2 = f2.getContext();
136 Assert.assertTrue("Ctx1 is not shared", ctx1.isShared());
137 Assert.assertTrue("Ctx2 is not shared", ctx2.isShared());
138
139 {
140 final List<GLContext> ctx1Shares = ctx1.getCreatedShares();
141 final List<GLContext> ctx2Shares = ctx2.getCreatedShares();
142 MiscUtils.dumpSharedGLContext("XXX-C-2.1", ctx1);
143 MiscUtils.dumpSharedGLContext("XXX-C-2.2", ctx2);
144
145 Assert.assertEquals("Ctx1 has unexpected number of created shares", 1, ctx1Shares.size());
146 Assert.assertEquals("Ctx2 has unexpected number of created shares", 1, ctx2Shares.size());
147 }
148
149 //
150 // 3rd
151 //
152 final GearsES2 g3 = new GearsES2(0);
153 g3.setSharedGears(g1);
154 final GLWindow f3 = runTestGL(animator, f1.getX()+0,
155 f1.getY()+height+insets.getTotalHeight(), g3, f1);
156
157 final GLContext ctx3 = f3.getContext();
158 Assert.assertTrue("Ctx1 is not shared", ctx1.isShared());
159 Assert.assertTrue("Ctx2 is not shared", ctx2.isShared());
160 Assert.assertTrue("Ctx3 is not shared", ctx3.isShared());
161
162 {
163 final List<GLContext> ctx1Shares = ctx1.getCreatedShares();
164 final List<GLContext> ctx2Shares = ctx2.getCreatedShares();
165 final List<GLContext> ctx3Shares = ctx3.getCreatedShares();
166 MiscUtils.dumpSharedGLContext("XXX-C-3.1", ctx1);
167 MiscUtils.dumpSharedGLContext("XXX-C-3.2", ctx2);
168 MiscUtils.dumpSharedGLContext("XXX-C-3.3", ctx3);
169
170 Assert.assertEquals("Ctx1 has unexpected number of created shares", 2, ctx1Shares.size());
171 Assert.assertEquals("Ctx2 has unexpected number of created shares", 2, ctx2Shares.size());
172 Assert.assertEquals("Ctx3 has unexpected number of created shares", 2, ctx3Shares.size());
173 }
174
175 Assert.assertTrue("Gears1 is shared", !g1.usesSharedGears());
176 Assert.assertTrue("Gears2 is not shared", g2.usesSharedGears());
177 Assert.assertTrue("Gears3 is not shared", g3.usesSharedGears());
178
179 animator.start();
180
181 try {
182 Thread.sleep(duration);
183 } catch(final Exception e) {
184 e.printStackTrace();
185 }
186
187 f3.destroy();
188 Assert.assertTrue(NewtTestUtil.waitForVisible(f3, false, null));
189 Assert.assertTrue(NewtTestUtil.waitForRealized(f3, false, null));
190 Assert.assertTrue(GLTestUtil.waitForContextCreated(f3, false, null));
191 {
192 final List<GLContext> ctx1Shares = ctx1.getCreatedShares();
193 final List<GLContext> ctx2Shares = ctx2.getCreatedShares();
194 final List<GLContext> ctx3Shares = ctx3.getCreatedShares();
195 MiscUtils.dumpSharedGLContext("XXX-D-0.1", ctx1);
196 MiscUtils.dumpSharedGLContext("XXX-D-0.2", ctx2);
197 MiscUtils.dumpSharedGLContext("XXX-D-0.3", ctx3);
198
199 Assert.assertTrue("Ctx1 is shared", ctx1.isShared());
200 Assert.assertTrue("Ctx2 is shared", ctx2.isShared());
201 Assert.assertTrue("Ctx3 is shared", ctx3.isShared());
202 Assert.assertEquals("Ctx1 has unexpected number of created shares", 1, ctx1Shares.size());
203 Assert.assertEquals("Ctx2 has unexpected number of created shares", 1, ctx2Shares.size());
204 Assert.assertEquals("Ctx3 has unexpected number of created shares", 2, ctx3Shares.size());
205 }
206 try { Thread.sleep(durationPostDestroy); } catch(final Exception e) { e.printStackTrace(); }
207
208 f2.destroy();
209 Assert.assertTrue(NewtTestUtil.waitForVisible(f2, false, null));
210 Assert.assertTrue(NewtTestUtil.waitForRealized(f2, false, null));
211 Assert.assertTrue(GLTestUtil.waitForContextCreated(f2, false, null));
212 {
213 final List<GLContext> ctx1Shares = ctx1.getCreatedShares();
214 final List<GLContext> ctx2Shares = ctx2.getCreatedShares();
215 final List<GLContext> ctx3Shares = ctx3.getCreatedShares();
216 MiscUtils.dumpSharedGLContext("XXX-D-1.1", ctx1);
217 MiscUtils.dumpSharedGLContext("XXX-D-1.2", ctx2);
218 MiscUtils.dumpSharedGLContext("XXX-D-1.3", ctx3);
219
220 Assert.assertTrue("Ctx1 is not shared", ctx1.isShared());
221 Assert.assertTrue("Ctx2 is not shared", ctx2.isShared());
222 Assert.assertTrue("Ctx3 is not shared", ctx3.isShared());
223 Assert.assertEquals("Ctx1 has unexpected number of created shares", 0, ctx1Shares.size());
224 Assert.assertEquals("Ctx2 has unexpected number of created shares", 1, ctx2Shares.size());
225 Assert.assertEquals("Ctx3 has unexpected number of created shares", 1, ctx3Shares.size());
226 }
227 try { Thread.sleep(durationPostDestroy); } catch(final Exception e) { e.printStackTrace(); }
228
229 f1.destroy();
230 Assert.assertTrue(NewtTestUtil.waitForVisible(f1, false, null));
231 Assert.assertTrue(NewtTestUtil.waitForRealized(f1, false, null));
232 Assert.assertTrue(GLTestUtil.waitForContextCreated(f1, false, null));
233 {
234 final List<GLContext> ctx1Shares = ctx1.getCreatedShares();
235 final List<GLContext> ctx2Shares = ctx2.getCreatedShares();
236 final List<GLContext> ctx3Shares = ctx3.getCreatedShares();
237 MiscUtils.dumpSharedGLContext("XXX-D-2.1", ctx1);
238 MiscUtils.dumpSharedGLContext("XXX-D-2.2", ctx2);
239 MiscUtils.dumpSharedGLContext("XXX-D-2.3", ctx3);
240
241 Assert.assertTrue("Ctx1 is not shared", !ctx1.isShared());
242 Assert.assertTrue("Ctx2 is not shared", !ctx2.isShared());
243 Assert.assertTrue("Ctx3 is not shared", !ctx3.isShared());
244 Assert.assertEquals("Ctx1 has unexpected number of created shares", 0, ctx1Shares.size());
245 Assert.assertEquals("Ctx2 has unexpected number of created shares", 0, ctx2Shares.size());
246 Assert.assertEquals("Ctx3 has unexpected number of created shares", 0, ctx3Shares.size());
247 }
248 try { Thread.sleep(durationPostDestroy); } catch(final Exception e) { e.printStackTrace(); }
249
250 animator.stop();
251 Assert.assertEquals(false, animator.isAnimating());
252 }
253
254 static long duration = 1000; // ms
255 static long durationPostDestroy = 1000; // ms - ~60 frames post destroy
256
257 public static void main(final String args[]) {
258 for(int i=0; i<args.length; i++) {
259 if(args[i].equals("-time")) {
260 i++;
261 try {
262 duration = Integer.parseInt(args[i]);
263 } catch (final Exception ex) { ex.printStackTrace(); }
264 }
265 }
266 /**
267 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
268 System.err.println("Press enter to continue");
269 System.err.println(stdin.readLine()); */
270 org.junit.runner.JUnitCore.main(TestSharedContextVBOES2NEWT0.class.getName());
271 }
272}
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 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.
Sharing the VBO of 3 GearsES2 instances, each in their own GLWindow.
GLWindow runTestGL(final Animator animator, final int x, final int y, final GearsES2 gears, final GLAutoDrawable sharedDrawable)
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 start()
Starts this animator, if not running.
Definition: Animator.java:344
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.
GLContext getContext()
Returns the context associated with this drawable.