PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestSharedContextVBOES2AWT3b.java

https://github.com/s6rapala/jogl
Java | 329 lines | 242 code | 39 blank | 48 comment | 4 complexity | 3edfccf2ba8c6fc05d20f96969661776 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, Apache-2.0, GPL-3.0
  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. package com.jogamp.opengl.test.junit.jogl.acore;
  29. import java.awt.Frame;
  30. import java.lang.reflect.InvocationTargetException;
  31. import java.util.List;
  32. import javax.media.opengl.GLCapabilities;
  33. import javax.media.opengl.GLContext;
  34. import javax.media.opengl.GLProfile;
  35. import javax.media.opengl.awt.GLJPanel;
  36. import com.jogamp.opengl.util.Animator;
  37. import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
  38. import com.jogamp.opengl.test.junit.util.MiscUtils;
  39. import com.jogamp.opengl.test.junit.util.UITestCase;
  40. import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
  41. import org.junit.Assert;
  42. import org.junit.BeforeClass;
  43. import org.junit.Test;
  44. import org.junit.FixMethodOrder;
  45. import org.junit.runners.MethodSorters;
  46. /**
  47. * Sharing the VBO of 3 GearsES2 instances, each in their own AWT GLJPanel.
  48. * <p>
  49. * This is achieved by using the 1st GLJPanel as the <i>master</i>
  50. * and using the build-in blocking mechanism to postpone creation
  51. * of the 2nd and 3rd GLJPanel until the 1st GLJPanel 's GLContext becomes created.
  52. * </p>
  53. * <p>
  54. * Above method allows random creation of the 1st GLJPanel, which triggers
  55. * creation of the <i>dependent</i> other GLJPanel sharing it's GLContext.
  56. * </p>
  57. */
  58. @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  59. public class TestSharedContextVBOES2AWT3b extends UITestCase {
  60. static GLProfile glp;
  61. static GLCapabilities caps;
  62. static int width, height;
  63. @BeforeClass
  64. public static void initClass() {
  65. if(GLProfile.isAvailable(GLProfile.GL2ES2)) {
  66. glp = GLProfile.get(GLProfile.GL2ES2);
  67. Assert.assertNotNull(glp);
  68. caps = new GLCapabilities(glp);
  69. Assert.assertNotNull(caps);
  70. width = 256;
  71. height = 256;
  72. } else {
  73. setTestSupported(false);
  74. }
  75. }
  76. protected GLJPanel createGLJPanel(final Frame frame, int x, int y, GearsES2 gears) throws InterruptedException {
  77. final GLJPanel glCanvas = new GLJPanel(caps);
  78. Assert.assertNotNull(glCanvas);
  79. glCanvas.addGLEventListener(gears);
  80. frame.add(glCanvas);
  81. frame.setLocation(x, y);
  82. frame.setSize(width, height);
  83. frame.setTitle("AWT GLJPanel Shared Gears Test: "+x+"/"+y+" shared true");
  84. return glCanvas;
  85. }
  86. @Test
  87. public void test01SyncedOneAnimator() throws InterruptedException, InvocationTargetException {
  88. final Frame f1 = new Frame();
  89. final Animator animator = new Animator();
  90. final GearsES2 g1 = new GearsES2(0);
  91. final GLJPanel c1 = createGLJPanel(f1, 0, 0, g1);
  92. animator.add(c1);
  93. final Frame f2 = new Frame();
  94. final GearsES2 g2 = new GearsES2(0);
  95. g2.setSharedGears(g1);
  96. final GLJPanel c2 = createGLJPanel(f2, f1.getX()+width,
  97. f1.getY()+0, g2);
  98. c2.setSharedAutoDrawable(c1);
  99. animator.add(c2);
  100. final Frame f3 = new Frame();
  101. final GearsES2 g3 = new GearsES2(0);
  102. g3.setSharedGears(g1);
  103. final GLJPanel c3 = createGLJPanel(f3, f1.getX()+0,
  104. f1.getY()+height, g3);
  105. c3.setSharedAutoDrawable(c1);
  106. animator.add(c3);
  107. javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
  108. public void run() {
  109. f2.setVisible(true); // shall wait until f1 is ready
  110. f1.setVisible(true); // master ..
  111. f3.setVisible(true); // shall wait until f1 is ready
  112. } } );
  113. animator.start(); // kicks off GLContext .. and hence gears of f2 + f3 completion
  114. Thread.sleep(1000/60*10); // wait ~10 frames giving a chance to create (blocking until master share is valid)
  115. Assert.assertTrue(AWTRobotUtil.waitForRealized(c1, true));
  116. Assert.assertTrue(AWTRobotUtil.waitForVisible(c1, true));
  117. Assert.assertTrue(AWTRobotUtil.waitForContextCreated(c1, true));
  118. Assert.assertTrue("Gears1 not initialized", g1.waitForInit(true));
  119. Assert.assertTrue(AWTRobotUtil.waitForRealized(c2, true));
  120. Assert.assertTrue(AWTRobotUtil.waitForVisible(c2, true));
  121. Assert.assertTrue(AWTRobotUtil.waitForContextCreated(c2, true));
  122. Assert.assertTrue("Gears2 not initialized", g2.waitForInit(true));
  123. Assert.assertTrue(AWTRobotUtil.waitForRealized(c3, true));
  124. Assert.assertTrue(AWTRobotUtil.waitForVisible(c3, true));
  125. Assert.assertTrue(AWTRobotUtil.waitForContextCreated(c3, true));
  126. Assert.assertTrue("Gears3 not initialized", g3.waitForInit(true));
  127. final GLContext ctx1 = c1.getContext();
  128. final GLContext ctx2 = c2.getContext();
  129. final GLContext ctx3 = c3.getContext();
  130. {
  131. final List<GLContext> ctx1Shares = ctx1.getCreatedShares();
  132. final List<GLContext> ctx2Shares = ctx2.getCreatedShares();
  133. final List<GLContext> ctx3Shares = ctx3.getCreatedShares();
  134. System.err.println("XXX-C-3.1:");
  135. MiscUtils.dumpSharedGLContext(ctx1);
  136. System.err.println("XXX-C-3.2:");
  137. MiscUtils.dumpSharedGLContext(ctx2);
  138. System.err.println("XXX-C-3.3:");
  139. MiscUtils.dumpSharedGLContext(ctx3);
  140. Assert.assertTrue("Ctx1 is not shared", ctx1.isShared());
  141. Assert.assertTrue("Ctx2 is not shared", ctx2.isShared());
  142. Assert.assertTrue("Ctx3 is not shared", ctx3.isShared());
  143. Assert.assertEquals("Ctx1 has unexpected number of created shares", 2, ctx1Shares.size());
  144. Assert.assertEquals("Ctx2 has unexpected number of created shares", 2, ctx2Shares.size());
  145. Assert.assertEquals("Ctx3 has unexpected number of created shares", 2, ctx3Shares.size());
  146. }
  147. Assert.assertTrue("Gears1 is shared", !g1.usesSharedGears());
  148. Assert.assertTrue("Gears2 is not shared", g2.usesSharedGears());
  149. Assert.assertTrue("Gears3 is not shared", g3.usesSharedGears());
  150. try {
  151. Thread.sleep(duration);
  152. } catch(Exception e) {
  153. e.printStackTrace();
  154. }
  155. // Stopped animator allows native windowing system 'repaint' event
  156. // to trigger GLAD 'display'
  157. animator.stop();
  158. Assert.assertEquals(false, animator.isAnimating());
  159. javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
  160. public void run() {
  161. try {
  162. f3.dispose();
  163. f2.dispose();
  164. f1.dispose();
  165. } catch (Throwable t) {
  166. throw new RuntimeException(t);
  167. }
  168. }});
  169. Assert.assertTrue(AWTRobotUtil.waitForRealized(c1, false));
  170. Assert.assertTrue(AWTRobotUtil.waitForRealized(c2, false));
  171. Assert.assertTrue(AWTRobotUtil.waitForRealized(c3, false));
  172. }
  173. @Test
  174. public void test02AsyncEachAnimator() throws InterruptedException, InvocationTargetException {
  175. final Frame f1 = new Frame();
  176. final Animator a1 = new Animator();
  177. final GearsES2 g1 = new GearsES2(0);
  178. final GLJPanel c1 = createGLJPanel(f1, 0, 0, g1);
  179. a1.add(c1);
  180. a1.start();
  181. // f1.setVisible(true); // we do this post f2 .. to test pending creation!
  182. final Frame f2 = new Frame();
  183. final Animator a2 = new Animator();
  184. final GearsES2 g2 = new GearsES2(0);
  185. g2.setSharedGears(g1);
  186. final GLJPanel c2 = createGLJPanel(f2, f1.getX()+width, f1.getY()+0, g2);
  187. c2.setSharedAutoDrawable(c1);
  188. a2.add(c2);
  189. a2.start();
  190. javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
  191. public void run() {
  192. f2.setVisible(true);
  193. } } );
  194. Thread.sleep(200); // wait a while ..
  195. javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
  196. public void run() {
  197. f1.setVisible(true); // test pending creation of f2
  198. } } );
  199. final Frame f3 = new Frame();
  200. final Animator a3 = new Animator();
  201. final GearsES2 g3 = new GearsES2(0);
  202. g3.setSharedGears(g1);
  203. final GLJPanel c3 = createGLJPanel(f3, f1.getX()+0, f1.getY()+height, g3);
  204. c3.setSharedAutoDrawable(c1);
  205. a3.add(c3);
  206. a3.start();
  207. javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
  208. public void run() {
  209. f3.setVisible(true);
  210. } } );
  211. Thread.sleep(1000/60*10); // wait ~10 frames giving a chance to create (blocking until master share is valid)
  212. Assert.assertTrue(AWTRobotUtil.waitForRealized(c1, true));
  213. Assert.assertTrue(AWTRobotUtil.waitForVisible(c1, true));
  214. Assert.assertTrue(AWTRobotUtil.waitForContextCreated(c1, true));
  215. Assert.assertTrue("Gears1 not initialized", g1.waitForInit(true));
  216. Assert.assertTrue(AWTRobotUtil.waitForRealized(c2, true));
  217. Assert.assertTrue(AWTRobotUtil.waitForVisible(c2, true));
  218. Assert.assertTrue(AWTRobotUtil.waitForContextCreated(c2, true));
  219. Assert.assertTrue("Gears2 not initialized", g2.waitForInit(true));
  220. Assert.assertTrue(AWTRobotUtil.waitForRealized(c3, true));
  221. Assert.assertTrue(AWTRobotUtil.waitForVisible(c3, true));
  222. Assert.assertTrue(AWTRobotUtil.waitForContextCreated(c3, true));
  223. Assert.assertTrue("Gears3 not initialized", g3.waitForInit(true));
  224. final GLContext ctx1 = c1.getContext();
  225. final GLContext ctx2 = c2.getContext();
  226. final GLContext ctx3 = c3.getContext();
  227. {
  228. final List<GLContext> ctx1Shares = ctx1.getCreatedShares();
  229. final List<GLContext> ctx2Shares = ctx2.getCreatedShares();
  230. final List<GLContext> ctx3Shares = ctx3.getCreatedShares();
  231. System.err.println("XXX-C-3.1:");
  232. MiscUtils.dumpSharedGLContext(ctx1);
  233. System.err.println("XXX-C-3.2:");
  234. MiscUtils.dumpSharedGLContext(ctx2);
  235. System.err.println("XXX-C-3.3:");
  236. MiscUtils.dumpSharedGLContext(ctx3);
  237. Assert.assertTrue("Ctx1 is not shared", ctx1.isShared());
  238. Assert.assertTrue("Ctx2 is not shared", ctx2.isShared());
  239. Assert.assertTrue("Ctx3 is not shared", ctx3.isShared());
  240. Assert.assertEquals("Ctx1 has unexpected number of created shares", 2, ctx1Shares.size());
  241. Assert.assertEquals("Ctx2 has unexpected number of created shares", 2, ctx2Shares.size());
  242. Assert.assertEquals("Ctx3 has unexpected number of created shares", 2, ctx3Shares.size());
  243. }
  244. Assert.assertTrue("Gears1 is shared", !g1.usesSharedGears());
  245. Assert.assertTrue("Gears2 is not shared", g2.usesSharedGears());
  246. Assert.assertTrue("Gears3 is not shared", g3.usesSharedGears());
  247. try {
  248. Thread.sleep(duration);
  249. } catch(Exception e) {
  250. e.printStackTrace();
  251. }
  252. // Stopped animator allows native windowing system 'repaint' event
  253. // to trigger GLAD 'display'
  254. a1.stop();
  255. Assert.assertEquals(false, a1.isAnimating());
  256. a2.stop();
  257. Assert.assertEquals(false, a2.isAnimating());
  258. a3.stop();
  259. Assert.assertEquals(false, a3.isAnimating());
  260. javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
  261. public void run() {
  262. try {
  263. f3.dispose();
  264. f2.dispose();
  265. f1.dispose();
  266. } catch (Throwable t) {
  267. throw new RuntimeException(t);
  268. }
  269. }});
  270. Assert.assertTrue(AWTRobotUtil.waitForRealized(c1, false));
  271. Assert.assertTrue(AWTRobotUtil.waitForRealized(c2, false));
  272. Assert.assertTrue(AWTRobotUtil.waitForRealized(c3, false));
  273. }
  274. static long duration = 1000; // ms
  275. public static void main(String args[]) {
  276. for(int i=0; i<args.length; i++) {
  277. if(args[i].equals("-time")) {
  278. i++;
  279. try {
  280. duration = Integer.parseInt(args[i]);
  281. } catch (Exception ex) { ex.printStackTrace(); }
  282. }
  283. }
  284. /**
  285. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  286. System.err.println("Press enter to continue");
  287. System.err.println(stdin.readLine()); */
  288. org.junit.runner.JUnitCore.main(TestSharedContextVBOES2AWT3b.class.getName());
  289. }
  290. }