From fd0d3f15d41d32b3987f4b88862619ef1a31b999 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 3 Dec 2010 21:47:01 +0100 Subject: Fix NEWT GLWindow: Adding missing shared GLContext setter (with unit test) --- .../jogl/acore/TestSharedContextListNEWT.java | 133 +++++++++++++++++++++ .../test/junit/jogl/demos/gl2/gears/Gears.java | 68 ++++++++--- .../classes/com/jogamp/newt/opengl/GLWindow.java | 15 ++- 3 files changed, 199 insertions(+), 17 deletions(-) create mode 100644 src/junit/com/jogamp/test/junit/jogl/acore/TestSharedContextListNEWT.java 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()=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.
+ * 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,
+ * 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; } -- cgit v1.2.3