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