summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2010-12-03 21:47:01 +0100
committerSven Gothel <[email protected]>2010-12-03 21:47:01 +0100
commitfd0d3f15d41d32b3987f4b88862619ef1a31b999 (patch)
tree04191b2c57b18b88842682ece7833a7ec89c5990
parentc58fd6b65a4da45663330f405ab94f24adb4c91d (diff)
Fix NEWT GLWindow: Adding missing shared GLContext setter (with unit test)
-rw-r--r--src/junit/com/jogamp/test/junit/jogl/acore/TestSharedContextListNEWT.java133
-rw-r--r--src/junit/com/jogamp/test/junit/jogl/demos/gl2/gears/Gears.java68
-rw-r--r--src/newt/classes/com/jogamp/newt/opengl/GLWindow.java15
3 files changed, 199 insertions, 17 deletions
diff --git a/src/junit/com/jogamp/test/junit/jogl/acore/TestSharedContextListNEWT.java b/src/junit/com/jogamp/test/junit/jogl/acore/TestSharedContextListNEWT.java
new file mode 100644
index 000000000..cccd91b4c
--- /dev/null
+++ b/src/junit/com/jogamp/test/junit/jogl/acore/TestSharedContextListNEWT.java
@@ -0,0 +1,133 @@
+/**
+ * Copyright 2010 JogAmp Community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of JogAmp Community.
+ */
+
+package com.jogamp.test.junit.jogl.acore;
+
+import com.jogamp.newt.opengl.GLWindow;
+import javax.media.opengl.GLCapabilities;
+import javax.media.opengl.GLDrawableFactory;
+import javax.media.opengl.GLPbuffer;
+import javax.media.opengl.GLProfile;
+import com.jogamp.opengl.util.Animator;
+
+import com.jogamp.test.junit.util.UITestCase;
+import com.jogamp.test.junit.jogl.demos.gl2.gears.Gears;
+
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class TestSharedContextListNEWT extends UITestCase {
+ static GLProfile glp;
+ static GLCapabilities caps;
+ static int width, height;
+ GLPbuffer sharedDrawable;
+ Gears sharedGears;
+
+ @BeforeClass
+ public static void initClass() {
+ GLProfile.initSingleton(true);
+ glp = GLProfile.getDefault();
+ Assert.assertNotNull(glp);
+ caps = new GLCapabilities(glp);
+ Assert.assertNotNull(caps);
+ width = 512;
+ height = 512;
+ }
+
+ private void initShared() {
+ sharedDrawable = GLDrawableFactory.getFactory(glp).createGLPbuffer(null, caps, null, width, height, null);
+ Assert.assertNotNull(sharedDrawable);
+ sharedGears = new Gears();
+ Assert.assertNotNull(sharedGears);
+ sharedDrawable.addGLEventListener(sharedGears);
+ // init and render one frame, which will setup the Gears display lists
+ sharedDrawable.display();
+ }
+
+ private void releaseShared() {
+ Assert.assertNotNull(sharedDrawable);
+ sharedDrawable.destroy();
+ }
+
+ protected GLWindow runTestGL(Animator animator, int x, int y, boolean useShared) {
+ GLWindow glWindow = GLWindow.create(caps);
+ Assert.assertNotNull(glWindow);
+ glWindow.setTitle("Shared Gears NEWT Test: "+x+"/"+y+" shared "+useShared);
+ if(useShared) {
+ glWindow.setSharedContext(sharedDrawable.getContext());
+ }
+
+ glWindow.setSize(width, height);
+ glWindow.setPosition(x, y);
+
+ Gears gears = new Gears();
+ if(useShared) {
+ gears.setGears(sharedGears.getGear1(), sharedGears.getGear2(), sharedGears.getGear3());
+ }
+ glWindow.addGLEventListener(gears);
+
+ animator.add(glWindow);
+
+ glWindow.setVisible(true);
+
+ return glWindow;
+ }
+
+ @Test
+ public void test01() throws InterruptedException {
+ initShared();
+ Animator animator = new Animator();
+ GLWindow f1 = runTestGL(animator, 0, 0, true);
+ GLWindow f2 = runTestGL(animator, width, 0, true);
+ GLWindow f3 = runTestGL(animator, 0, height, false);
+ animator.start();
+ while(animator.isAnimating() && animator.getDuration()<duration) {
+ Thread.sleep(100);
+ }
+ animator.stop();
+ f1.destroy();
+ f2.destroy();
+ f3.destroy();
+ releaseShared();
+ }
+
+ static long duration = 500; // ms
+
+ public static void main(String args[]) {
+ for(int i=0; i<args.length; i++) {
+ if(args[i].equals("-time")) {
+ i++;
+ try {
+ duration = Integer.parseInt(args[i]);
+ } catch (Exception ex) { ex.printStackTrace(); }
+ }
+ }
+ org.junit.runner.JUnitCore.main(TestSharedContextListNEWT.class.getName());
+ }
+}
diff --git a/src/junit/com/jogamp/test/junit/jogl/demos/gl2/gears/Gears.java b/src/junit/com/jogamp/test/junit/jogl/demos/gl2/gears/Gears.java
index d9194c216..78846250f 100644
--- a/src/junit/com/jogamp/test/junit/jogl/demos/gl2/gears/Gears.java
+++ b/src/junit/com/jogamp/test/junit/jogl/demos/gl2/gears/Gears.java
@@ -16,7 +16,7 @@ import com.jogamp.newt.Window;
public class Gears implements GLEventListener {
private float view_rotx = 20.0f, view_roty = 30.0f, view_rotz = 0.0f;
- private int gear1, gear2, gear3;
+ private int gear1=0, gear2=0, gear3=0;
private float angle = 0.0f;
private int swapInterval;
@@ -31,6 +31,27 @@ public class Gears implements GLEventListener {
this.swapInterval = 1;
}
+ public void setGears(int g1, int g2, int g3) {
+ gear1 = g1;
+ gear2 = g2;
+ gear3 = g3;
+ }
+
+ /**
+ * @return display list gear1
+ */
+ public int getGear1() { return gear1; }
+
+ /**
+ * @return display list gear2
+ */
+ public int getGear2() { return gear2; }
+
+ /**
+ * @return display list gear3
+ */
+ public int getGear3() { return gear3; }
+
public void init(GLAutoDrawable drawable) {
System.err.println("Gears: Init");
// Use debug pipeline
@@ -50,23 +71,38 @@ public class Gears implements GLEventListener {
gl.glEnable(GL2.GL_DEPTH_TEST);
/* make the gears */
- gear1 = gl.glGenLists(1);
- gl.glNewList(gear1, GL2.GL_COMPILE);
- gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, red, 0);
- gear(gl, 1.0f, 4.0f, 1.0f, 20, 0.7f);
- gl.glEndList();
+ if(0>=gear1) {
+ gear1 = gl.glGenLists(1);
+ gl.glNewList(gear1, GL2.GL_COMPILE);
+ gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, red, 0);
+ gear(gl, 1.0f, 4.0f, 1.0f, 20, 0.7f);
+ gl.glEndList();
+ System.err.println("gear1 list created: "+gear1);
+ } else {
+ System.err.println("gear1 list reused: "+gear1);
+ }
- gear2 = gl.glGenLists(1);
- gl.glNewList(gear2, GL2.GL_COMPILE);
- gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, green, 0);
- gear(gl, 0.5f, 2.0f, 2.0f, 10, 0.7f);
- gl.glEndList();
+ if(0>=gear2) {
+ gear2 = gl.glGenLists(1);
+ gl.glNewList(gear2, GL2.GL_COMPILE);
+ gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, green, 0);
+ gear(gl, 0.5f, 2.0f, 2.0f, 10, 0.7f);
+ gl.glEndList();
+ System.err.println("gear2 list created: "+gear2);
+ } else {
+ System.err.println("gear2 list reused: "+gear2);
+ }
- gear3 = gl.glGenLists(1);
- gl.glNewList(gear3, GL2.GL_COMPILE);
- gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, blue, 0);
- gear(gl, 1.3f, 2.0f, 0.5f, 10, 0.7f);
- gl.glEndList();
+ if(0>=gear3) {
+ gear3 = gl.glGenLists(1);
+ gl.glNewList(gear3, GL2.GL_COMPILE);
+ gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, blue, 0);
+ gear(gl, 1.3f, 2.0f, 0.5f, 10, 0.7f);
+ gl.glEndList();
+ System.err.println("gear3 list created: "+gear3);
+ } else {
+ System.err.println("gear3 list reused: "+gear3);
+ }
gl.glEnable(GL2.GL_NORMALIZE);
diff --git a/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java b/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java
index 5531bf168..358a3d4ce 100644
--- a/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java
+++ b/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java
@@ -373,7 +373,7 @@ public class GLWindow implements GLAutoDrawable, Window, NEWTEventConsumer {
drawable = factory.createGLDrawable(nw);
}
drawable.setRealized(true);
- context = drawable.createContext(null);
+ context = drawable.createContext(sharedContext);
}
if(Window.DEBUG_WINDOW_EVENT || Window.DEBUG_IMPLEMENTATION) {
String msg = new String("GLWindow.setVisibleActionPost("+visible+", "+nativeWindowCreated+") "+Thread.currentThread()+", fin");
@@ -404,6 +404,7 @@ public class GLWindow implements GLAutoDrawable, Window, NEWTEventConsumer {
// OpenGL-related methods and state
//
+ private GLContext sharedContext = null;
private GLDrawableFactory factory;
private GLDrawable drawable;
private GLContext context;
@@ -419,6 +420,18 @@ public class GLWindow implements GLAutoDrawable, Window, NEWTEventConsumer {
return factory;
}
+ /**
+ * Specifies an {@link javax.media.opengl.GLContext OpenGL context} to share with.<br>
+ * At native creation, {@link #setVisible(boolean) setVisible(true)},
+ * a {@link javax.media.opengl.GLDrawable drawable} and {@link javax.media.opengl.GLContext context} is created besides the native Window itself,<br>
+ * hence you shall set the shared context before.
+ *
+ * @param sharedContext The OpenGL context shared by this GLWindow's one
+ */
+ public void setSharedContext(GLContext sharedContext) {
+ this.sharedContext = sharedContext;
+ }
+
public void setContext(GLContext newCtx) {
context = newCtx;
}