diff options
Diffstat (limited to 'src/test')
18 files changed, 431 insertions, 468 deletions
diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURendererListenerBase01.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURendererListenerBase01.java index 0ecc8f67b..a837c56f8 100644 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURendererListenerBase01.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURendererListenerBase01.java @@ -27,6 +27,7 @@ */ package com.jogamp.opengl.test.junit.graph.demos; +import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; @@ -48,6 +49,7 @@ import com.jogamp.graph.curve.opengl.Renderer; import com.jogamp.newt.event.KeyEvent; import com.jogamp.newt.event.KeyListener; import com.jogamp.newt.opengl.GLWindow; +import com.jogamp.opengl.util.GLReadBufferUtil; /** * @@ -60,7 +62,7 @@ import com.jogamp.newt.opengl.GLWindow; * - s: screenshot */ public abstract class GPURendererListenerBase01 implements GLEventListener { - private Screenshot screenshot; + private GLReadBufferUtil screenshot; private Renderer renderer; private int renderModes; private boolean debug; @@ -88,7 +90,7 @@ public abstract class GPURendererListenerBase01 implements GLEventListener { this.renderModes = renderModes; this.debug = debug; this.trace = trace; - this.screenshot = new Screenshot(); + this.screenshot = new GLReadBufferUtil(false, false); } public final Renderer getRenderer() { return renderer; } @@ -191,8 +193,9 @@ public abstract class GPURendererListenerBase01 implements GLEventListener { PrintWriter pw = new PrintWriter(sw); pw.printf("-%03dx%03d-Z%04d-T%04d-%s", drawable.getWidth(), drawable.getHeight(), (int)Math.abs(zoom), texSize, objName); - String filename = dir + tech + sw +".tga"; - screenshot.surface2File(drawable, filename /*, exportAlpha */); + final String filename = dir + tech + sw +".tga"; + screenshot.readPixels(drawable.getGL(), drawable, false); + screenshot.write(new File(filename)); } int screenshot_num = 0; diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/ReadBufferUtil.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/ReadBufferUtil.java deleted file mode 100644 index dc1ea2da3..000000000 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/ReadBufferUtil.java +++ /dev/null @@ -1,108 +0,0 @@ -/** - * 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.opengl.test.junit.graph.demos; - -import com.jogamp.opengl.util.GLBuffers; -import java.nio.*; -import javax.media.opengl.*; - -import com.jogamp.opengl.util.texture.Texture; -import com.jogamp.opengl.util.texture.TextureData; - -public class ReadBufferUtil { - protected int readPixelSizeLast = 0; - protected Buffer readPixelBuffer = null; - protected TextureData readTextureData = null; - protected Texture readTexture = new Texture(GL.GL_TEXTURE_2D); - - public Buffer getPixelBuffer() { return readPixelBuffer; } - public void rewindPixelBuffer() { readPixelBuffer.rewind(); } - - public TextureData getTextureData() { return readTextureData; } - public Texture getTexture() { return readTexture; } - - public boolean isValid() { - return null!=readTexture && null!=readTextureData && null!=readPixelBuffer ; - } - - public void fetchOffscreenTexture(GLDrawable drawable, GL gl) { - int readPixelSize = drawable.getWidth() * drawable.getHeight() * 3 ; // RGB - boolean newData = false; - if(readPixelSize>readPixelSizeLast) { - readPixelBuffer = GLBuffers.newDirectGLBuffer(GL.GL_UNSIGNED_BYTE, readPixelSize); - readPixelSizeLast = readPixelSize ; - try { - readTextureData = new TextureData( - gl.getGLProfile(), - // gl.isGL2GL3()?gl.GL_RGBA:gl.GL_RGB, - GL.GL_RGB, - drawable.getWidth(), drawable.getHeight(), - 0, - GL.GL_RGB, - GL.GL_UNSIGNED_BYTE, - false, false, - false /* flip */, - readPixelBuffer, - null /* Flusher */); - newData = true; - } catch (Exception e) { - readTextureData = null; - readPixelBuffer = null; - readPixelSizeLast = 0; - throw new RuntimeException("can not fetch offscreen texture", e); - } - } - if(null!=readPixelBuffer) { - readPixelBuffer.clear(); - gl.glReadPixels(0, 0, drawable.getWidth(), drawable.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, readPixelBuffer); - readPixelBuffer.rewind(); - if(newData) { - readTexture.updateImage(gl, readTextureData); - } else { - readTexture.updateSubImage(gl, readTextureData, 0, - 0, 0, // src offset - 0, 0, // dst offset - drawable.getWidth(), drawable.getHeight()); - } - readPixelBuffer.rewind(); - } - } - - public void dispose(GL gl) { - readTexture.destroy(gl); - readTextureData = null; - if(null != readPixelBuffer) { - readPixelBuffer.clear(); - readPixelBuffer = null; - } - readPixelSizeLast = 0; - } - -} - diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/Screenshot.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/Screenshot.java deleted file mode 100644 index f4b6f6dd9..000000000 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/Screenshot.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.jogamp.opengl.test.junit.graph.demos; - -import java.io.File; -import java.io.IOException; - -import javax.media.opengl.GL; -import javax.media.opengl.GLAutoDrawable; - -import com.jogamp.opengl.util.texture.TextureIO; - -public class Screenshot { - - ReadBufferUtil readBufferUtil = new ReadBufferUtil(); - - public void dispose(GL gl) { - readBufferUtil.dispose(gl); - } - - public void surface2File(GLAutoDrawable drawable, String filename) { - GL gl = drawable.getGL(); - // FIXME glFinish() is an expensive paranoia sync, should not be necessary due to spec - gl.glFinish(); - readBufferUtil.fetchOffscreenTexture(drawable, gl); - gl.glFinish(); - try { - surface2File(filename); - } catch (IOException ex) { - throw new RuntimeException("can not write survace to file", ex); - } - } - - void surface2File(String filename) throws IOException { - File file = new File(filename); - TextureIO.write(readBufferUtil.getTextureData(), file); - System.err.println("Wrote: " + file.getAbsolutePath() + ", ..."); - readBufferUtil.rewindPixelBuffer(); - } - -} diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UIListenerBase01.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UIListenerBase01.java index df2dca4fb..b89f87be4 100644 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UIListenerBase01.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UIListenerBase01.java @@ -27,6 +27,7 @@ */ package com.jogamp.opengl.test.junit.graph.demos.ui; +import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; @@ -41,13 +42,12 @@ import javax.media.opengl.GLPipelineFactory; import javax.media.opengl.GLRunnable; import com.jogamp.graph.curve.opengl.RegionRenderer; -import com.jogamp.graph.curve.opengl.TextRenderer; import com.jogamp.newt.event.KeyEvent; import com.jogamp.newt.event.KeyListener; import com.jogamp.newt.event.MouseEvent; import com.jogamp.newt.event.MouseListener; import com.jogamp.newt.opengl.GLWindow; -import com.jogamp.opengl.test.junit.graph.demos.Screenshot; +import com.jogamp.opengl.util.GLReadBufferUtil; /** * @@ -60,7 +60,7 @@ import com.jogamp.opengl.test.junit.graph.demos.Screenshot; * - s: screenshot */ public abstract class UIListenerBase01 implements GLEventListener { - private Screenshot screenshot; + private GLReadBufferUtil screenshot; private RegionRenderer rRenderer; private boolean debug; private boolean trace; @@ -85,7 +85,7 @@ public abstract class UIListenerBase01 implements GLEventListener { this.rRenderer = rRenderer; this.debug = debug; this.trace = trace; - this.screenshot = new Screenshot(); + this.screenshot = new GLReadBufferUtil(false, false); } public final RegionRenderer getRegionRenderer() { return rRenderer; } @@ -179,8 +179,9 @@ public abstract class UIListenerBase01 implements GLEventListener { PrintWriter pw = new PrintWriter(sw); pw.printf("-%03dx%03d-Z%04d-T%04d-%s", drawable.getWidth(), drawable.getHeight(), (int)Math.abs(zoom), 0, objName); - String filename = dir + tech + sw +".tga"; - screenshot.surface2File(drawable, filename /*, exportAlpha */); + final String filename = dir + tech + sw +".tga"; + screenshot.readPixels(drawable.getGL(), drawable, false); + screenshot.write(new File(filename)); } int screenshot_num = 0; diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGPUMemSec01NEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGPUMemSec01NEWT.java new file mode 100644 index 000000000..a328a2bac --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGPUMemSec01NEWT.java @@ -0,0 +1,319 @@ +/** + * 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.opengl.test.junit.jogl.acore; + +import com.jogamp.common.nio.Buffers; +import com.jogamp.opengl.util.GLPixelStorageModes; +import com.jogamp.opengl.test.junit.util.NEWTGLContext; +import com.jogamp.opengl.test.junit.util.UITestCase; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import javax.media.opengl.GL; +import javax.media.opengl.GL2ES2; +import javax.media.opengl.GL2GL3; +import javax.media.opengl.GLDrawable; +import javax.media.opengl.GLException; +import javax.media.opengl.GLProfile; + +import org.junit.Assert; +import org.junit.Test; + +public class TestGPUMemSec01NEWT extends UITestCase { + static String hexString(int i) { + return "0x"+Integer.toHexString(i); + } + static String exceptionMsg(String pre, int format, int type, int components, int width, int height, int rl1, int rl4, int rl8) { + return pre + + ": fmt "+hexString(format)+", type "+hexString(type)+", comps "+components+ + ", "+width+"x"+height+ + ", rowlenA1 "+rl1+", rowlenA4 "+rl4+", rowlenA8 "+rl8; + } + + static NEWTGLContext.WindowContext createCurrentGLOffscreenWindow(int width, int height) throws GLException, InterruptedException { + final NEWTGLContext.WindowContext winctx = NEWTGLContext.createOffscreenWindow(GLProfile.getGL2ES2(), width, height, true); + final GL _gl = winctx.context.getGL(); + Assert.assertTrue(_gl.isGL2GL3()); + final GL2GL3 gl = _gl.getGL2GL3(); + + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + + // misc GL setup + gl.glClearColor(1, 1, 1, 1); + gl.glEnable(GL2ES2.GL_DEPTH_TEST); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + gl.glViewport(0, 0, width, height); + gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + + return winctx; + } + + static int readPixelsCheck(GL gl, int format, int type, int components, int width, int height) throws InterruptedException { + int expectedExceptions = 0; + + final int rowlenA1 = width * components; + + final int rowlenA4 = ( ( width * components + 3 ) / 4 ) * 4 ; + Assert.assertTrue(rowlenA4 % 4 == 0); + + final int rowlenA8 = ( ( width * components + 7 ) / 8 ) * 8 ; + Assert.assertTrue(rowlenA8 % 8 == 0); + + GLPixelStorageModes psm = new GLPixelStorageModes(); + psm.setPackAlignment(gl, 1); + + Exception ee = null; + + // ok size ! + try { + ByteBuffer bb = Buffers.newDirectByteBuffer(height*rowlenA1); + gl.glReadPixels(0, 0, width, height, format, type, bb); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + } catch(IndexOutOfBoundsException e) { + ee = e; + } + Assert.assertNull( + exceptionMsg("Unexpected IndexOutOfBoundsException (size ok, alignment 1)", + format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee); + ee = null; + + + // too small -10 ! + try { + ByteBuffer bb = Buffers.newDirectByteBuffer(height*rowlenA1-10); + gl.glReadPixels(0, 0, width, height, format, type, bb); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + } catch(IndexOutOfBoundsException e) { + ee = e; + System.err.println( + exceptionMsg("OK Expected IndexOutOfBoundsException (size-10 bytes)", + format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8)+ + ": "+ee.getMessage()); + expectedExceptions++; + } + Assert.assertNotNull( + exceptionMsg("Expected IndexOutOfBoundsException (size-10 bytes)", + format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee); + ee = null; + + // too small size/4 ! + try { + ByteBuffer bb = Buffers.newDirectByteBuffer(height*rowlenA1/4); + gl.glReadPixels(0, 0, width, height, format, type, bb); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + } catch(IndexOutOfBoundsException e) { + ee = e; + System.err.println( + exceptionMsg("OK Expected IndexOutOfBoundsException (size/4 bytes)", + format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8)+ + ": "+ee.getMessage()); + expectedExceptions++; + } + Assert.assertNotNull( + exceptionMsg("Expected IndexOutOfBoundsException (size/4 bytes)", + format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee); + ee = null; + + // + // Alignment test + // + psm.setPackAlignment(gl, 4); + + // ok size ! + try { + ByteBuffer bb = Buffers.newDirectByteBuffer(height*rowlenA4); + gl.glReadPixels(0, 0, width, height, format, type, bb); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + } catch(IndexOutOfBoundsException e) { + ee = e; + } + Assert.assertNull( + exceptionMsg("Unexpected IndexOutOfBoundsException (size ok, alignment 4)", + format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee); + ee = null; + + // too small if rowlenA1%4 > 0 + try { + ByteBuffer bb = Buffers.newDirectByteBuffer(height*rowlenA1); + gl.glReadPixels(0, 0, width, height, format, type, bb); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + } catch(IndexOutOfBoundsException e) { + ee = e; + if(rowlenA1%4>0) { + System.err.println( + exceptionMsg("OK Expected IndexOutOfBoundsException (alignment 4)", + format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8)+ + ": "+ee.getMessage()); + expectedExceptions++; + } + } + if(rowlenA1%4>0) { + Assert.assertNotNull( + exceptionMsg("Expected IndexOutOfBoundsException (alignment 4)", + format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee); + } else { + Assert.assertNull( + exceptionMsg("Unexpected IndexOutOfBoundsException (alignment 4)", + format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee); + } + ee = null; + + psm.setPackAlignment(gl, 8); + + // ok size ! + try { + ByteBuffer bb = Buffers.newDirectByteBuffer(height*rowlenA8); + gl.glReadPixels(0, 0, width, height, format, type, bb); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + } catch(IndexOutOfBoundsException e) { + ee = e; + } + Assert.assertNull( + exceptionMsg("Unexpected IndexOutOfBoundsException (size ok, alignment 8)", + format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee); + ee = null; + + // too small if rowlenA1%8 > 0 + try { + ByteBuffer bb = Buffers.newDirectByteBuffer(height*rowlenA1); + gl.glReadPixels(0, 0, width, height, format, type, bb); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + } catch(IndexOutOfBoundsException e) { + ee = e; + if(rowlenA1%8>0) { + System.err.println( + exceptionMsg("OK Expected IndexOutOfBoundsException (alignment 8)", + format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8)+ + ": "+ee.getMessage()); + expectedExceptions++; + } + } + if(rowlenA1%8>0) { + Assert.assertNotNull( + exceptionMsg("Expected IndexOutOfBoundsException (alignment 8)", + format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee); + } else { + Assert.assertNull( + exceptionMsg("Unexpected IndexOutOfBoundsException (alignment 8)", + format, type, components, width, height, rowlenA1, rowlenA4, rowlenA8), ee); + } + ee = null; + + psm.restore(gl); + + return expectedExceptions; + } + + @Test + public void testReadPixels_640x480xREDxUB() throws InterruptedException { + final int width = 640; + final int height= 480; + + // preset .. + final NEWTGLContext.WindowContext winctx = createCurrentGLOffscreenWindow(width, height); + final GLDrawable drawable = winctx.context.getGLDrawable(); + final GL2GL3 gl = winctx.context.getGL().getGL2GL3(); + + // 2 x too small - 0 x alignment + Assert.assertEquals(2, readPixelsCheck(gl, GL2GL3.GL_RED, GL.GL_UNSIGNED_BYTE, 1, width, height)); + + drawable.swapBuffers(); + Thread.sleep(50); + + NEWTGLContext.destroyWindow(winctx); + } + + @Test + public void testReadPixels_640x480xRGBxUB() throws InterruptedException { + final int width = 640; + final int height= 480; + + // preset .. + final NEWTGLContext.WindowContext winctx = createCurrentGLOffscreenWindow(width, height); + final GLDrawable drawable = winctx.context.getGLDrawable(); + final GL2GL3 gl = winctx.context.getGL().getGL2GL3(); + + // 2 x too small - 0 x alignment + Assert.assertEquals(2, readPixelsCheck(gl, GL2GL3.GL_RGB, GL.GL_UNSIGNED_BYTE, 3, width, height)); + + drawable.swapBuffers(); + Thread.sleep(50); + + NEWTGLContext.destroyWindow(winctx); + } + + @Test + public void testReadPixels_102x100xREDxUB() throws InterruptedException { + int wwidth = 640; + int wheight= 480; + int rwidth = 102; + int rheight= 100; + + // preset .. + final NEWTGLContext.WindowContext winctx = createCurrentGLOffscreenWindow(wwidth, wheight); + final GLDrawable drawable = winctx.context.getGLDrawable(); + final GL2GL3 gl = winctx.context.getGL().getGL2GL3(); + + // 2 x too small - 2 x alignment + Assert.assertEquals(4, readPixelsCheck(gl, GL2GL3.GL_RED, GL.GL_UNSIGNED_BYTE, 1, rwidth, rheight)); + + drawable.swapBuffers(); + Thread.sleep(50); + + NEWTGLContext.destroyWindow(winctx); + } + + @Test + public void testReadPixels_102x100xRGBxUB() throws InterruptedException { + final int wwidth = 640; + final int wheight= 480; + final int rwidth = 102; + final int rheight= 100; + + // preset .. + final NEWTGLContext.WindowContext winctx = createCurrentGLOffscreenWindow(wwidth, wheight); + final GLDrawable drawable = winctx.context.getGLDrawable(); + final GL2GL3 gl = winctx.context.getGL().getGL2GL3(); + + // 2 x too small - 2 x alignment + Assert.assertEquals(4, readPixelsCheck(gl, GL2GL3.GL_RGB, GL.GL_UNSIGNED_BYTE, 3, rwidth, rheight)); + + drawable.swapBuffers(); + Thread.sleep(50); + + NEWTGLContext.destroyWindow(winctx); + } + + public static void main(String args[]) throws IOException { + String tstname = TestGPUMemSec01NEWT.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } +} + diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl2/gears/TestGearsAWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl2/gears/TestGearsAWT.java index 8b050374a..d0f73898f 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl2/gears/TestGearsAWT.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl2/gears/TestGearsAWT.java @@ -104,7 +104,7 @@ public class TestGearsAWT extends UITestCase { @Test public void test01() throws InterruptedException { - GLCapabilities caps = new GLCapabilities(GLProfile.getDefault()); + GLCapabilities caps = new GLCapabilities(glp); runTestGL(caps); } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestFBOMRTNEWT01.java b/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestFBOMRTNEWT01.java index 6c826c221..f3af56fb3 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestFBOMRTNEWT01.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestFBOMRTNEWT01.java @@ -56,7 +56,7 @@ public class TestFBOMRTNEWT01 extends UITestCase { @Test public void test01() throws InterruptedException { // preset .. - final NEWTGLContext.WindowContext winctx = NEWTGLContext.createWindow(GLProfile.getGL2ES2(), 640, 480, true); + final NEWTGLContext.WindowContext winctx = NEWTGLContext.createOnscreenWindow(GLProfile.getGL2ES2(), 640, 480, true); final GLDrawable drawable = winctx.context.getGLDrawable(); final GL _gl = winctx.context.getGL(); Assert.assertTrue(_gl.isGL2GL3()); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestGLSLShaderState01NEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestGLSLShaderState01NEWT.java index c29faf31d..484734b28 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestGLSLShaderState01NEWT.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestGLSLShaderState01NEWT.java @@ -60,7 +60,7 @@ public class TestGLSLShaderState01NEWT extends UITestCase { @Test public void testShaderState01Validation() throws InterruptedException { // preset .. - final NEWTGLContext.WindowContext winctx = NEWTGLContext.createWindow(GLProfile.getGL2ES2(), 480, 480, true); + final NEWTGLContext.WindowContext winctx = NEWTGLContext.createOnscreenWindow(GLProfile.getGL2ES2(), 480, 480, true); final GLDrawable drawable = winctx.context.getGLDrawable(); final GL2ES2 gl = winctx.context.getGL().getGL2ES2(); System.err.println(winctx.context); @@ -173,18 +173,18 @@ public class TestGLSLShaderState01NEWT extends UITestCase { NEWTGLContext.destroyWindow(winctx); } - @Test + @Test(timeout=120000) public void testShaderState00PerformanceSingleKeepEnabled() throws InterruptedException { testShaderState00PerformanceSingle(false); } - @Test + @Test(timeout=120000) public void testShaderState00PerformanceSingleToggleEnable() throws InterruptedException { testShaderState00PerformanceSingle(true); } void testShaderState00PerformanceSingle(boolean toggleEnable) throws InterruptedException { // preset .. - final NEWTGLContext.WindowContext winctx = NEWTGLContext.createWindow(GLProfile.getGL2ES2(), 480, 480, false); + final NEWTGLContext.WindowContext winctx = NEWTGLContext.createOnscreenWindow(GLProfile.getGL2ES2(), 480, 480, false); final GLDrawable drawable = winctx.context.getGLDrawable(); final GL2ES2 gl = winctx.context.getGL().getGL2ES2(); System.err.println(winctx.context); @@ -240,17 +240,20 @@ public class TestGLSLShaderState01NEWT extends UITestCase { st.uniform(gl, pmvMatrixUniform); gl.glViewport(0, 0, drawable.getWidth(), drawable.getHeight()); - long t0 = System.currentTimeMillis(); - int frames; + gl.setSwapInterval(0); // validation .. GLSLMiscHelper.displayVCArrays(drawable, gl, st, toggleEnable, vertices0, colors0, toggleEnable, 1, 0); // warmup .. - for(frames=0; frames<GLSLMiscHelper.frames_warmup; frames++) { + for(int frames=0; frames<GLSLMiscHelper.frames_warmup; frames++) { GLSLMiscHelper.displayVCArraysNoChecks(drawable, gl, toggleEnable, vertices0, colors0, toggleEnable); - } + } + // measure .. + long t0 = System.currentTimeMillis(); + int frames; + for(frames=0; frames<GLSLMiscHelper.frames_perftest; frames++) { GLSLMiscHelper.displayVCArraysNoChecks(drawable, gl, toggleEnable, vertices0, colors0, toggleEnable); } @@ -267,10 +270,10 @@ public class TestGLSLShaderState01NEWT extends UITestCase { NEWTGLContext.destroyWindow(winctx); } - @Test + @Test(timeout=120000) public void testShaderState01PerformanceDouble() throws InterruptedException { // preset .. - final NEWTGLContext.WindowContext winctx = NEWTGLContext.createWindow(GLProfile.getGL2ES2(), 480, 480, false); + final NEWTGLContext.WindowContext winctx = NEWTGLContext.createOnscreenWindow(GLProfile.getGL2ES2(), 480, 480, false); final GLDrawable drawable = winctx.context.getGLDrawable(); final GL2ES2 gl = winctx.context.getGL().getGL2ES2(); System.err.println(winctx.context); @@ -336,18 +339,22 @@ public class TestGLSLShaderState01NEWT extends UITestCase { st.uniform(gl, pmvMatrixUniform); gl.glViewport(0, 0, drawable.getWidth(), drawable.getHeight()); + gl.setSwapInterval(0); + // validation .. GLSLMiscHelper.displayVCArrays(drawable, gl, st, true, vertices0, colors0, true, 1, 0); GLSLMiscHelper.displayVCArrays(drawable, gl, st, true, vertices1, colors1, true, 2, 0); - - long t0 = System.currentTimeMillis(); - int frames; + // warmup .. - for(frames=0; frames<GLSLMiscHelper.frames_warmup; frames+=2) { + for(int frames=0; frames<GLSLMiscHelper.frames_warmup; frames+=2) { GLSLMiscHelper.displayVCArraysNoChecks(drawable, gl, true, vertices0, colors0, true); GLSLMiscHelper.displayVCArraysNoChecks(drawable, gl, true, vertices1, colors1, true); - } + } + // measure .. + long t0 = System.currentTimeMillis(); + int frames; + for(frames=0; frames<GLSLMiscHelper.frames_perftest; frames+=2) { GLSLMiscHelper.displayVCArraysNoChecks(drawable, gl, true, vertices0, colors0, true); GLSLMiscHelper.displayVCArraysNoChecks(drawable, gl, true, vertices1, colors1, true); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestGLSLShaderState02NEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestGLSLShaderState02NEWT.java index 0d81db137..2ecc3b30d 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestGLSLShaderState02NEWT.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestGLSLShaderState02NEWT.java @@ -68,7 +68,7 @@ public class TestGLSLShaderState02NEWT extends UITestCase { private void testShaderState01Validation(boolean linkSP1) throws InterruptedException { // preset .. - final NEWTGLContext.WindowContext winctx = NEWTGLContext.createWindow(GLProfile.getGL2ES2(), 480, 480, true); + final NEWTGLContext.WindowContext winctx = NEWTGLContext.createOnscreenWindow(GLProfile.getGL2ES2(), 480, 480, true); final GLDrawable drawable = winctx.context.getGLDrawable(); final GL2ES2 gl = winctx.context.getGL().getGL2ES2(); System.err.println(winctx.context); @@ -229,10 +229,10 @@ public class TestGLSLShaderState02NEWT extends UITestCase { NEWTGLContext.destroyWindow(winctx); } - @Test + @Test(timeout=120000) public void testShaderState01PerformanceDouble() throws InterruptedException { // preset .. - final NEWTGLContext.WindowContext winctx = NEWTGLContext.createWindow(GLProfile.getGL2ES2(), 480, 480, false); + final NEWTGLContext.WindowContext winctx = NEWTGLContext.createOnscreenWindow(GLProfile.getGL2ES2(), 480, 480, false); final GLDrawable drawable = winctx.context.getGLDrawable(); final GL2ES2 gl = winctx.context.getGL().getGL2ES2(); System.err.println(winctx.context); @@ -308,6 +308,8 @@ public class TestGLSLShaderState02NEWT extends UITestCase { st.uniform(gl, pmvMatrixUniform); gl.glViewport(0, 0, drawable.getWidth(), drawable.getHeight()); + gl.setSwapInterval(0); + // validation .. st.attachShaderProgram(gl, sp0); GLSLMiscHelper.displayVCArrays(drawable, gl, st, true, vertices0, colors0, true, 1, 0); @@ -316,18 +318,20 @@ public class TestGLSLShaderState02NEWT extends UITestCase { GLSLMiscHelper.displayVCArrays(drawable, gl, st, true, vertices0, colors0, true, 1, 0); GLSLMiscHelper.displayVCArrays(drawable, gl, st, true, vertices1, colors1, true, 2, 0); - long t0 = System.currentTimeMillis(); - int frames; // warmup .. - for(frames=0; frames<GLSLMiscHelper.frames_warmup; frames+=2) { + for(int frames=0; frames<GLSLMiscHelper.frames_warmup; frames+=2) { // SP0 st.attachShaderProgram(gl, sp0); GLSLMiscHelper.displayVCArraysNoChecks(drawable, gl, true, vertices0, colors0, true); // SP1 st.attachShaderProgram(gl, sp1); GLSLMiscHelper.displayVCArraysNoChecks(drawable, gl, true, vertices1, colors1, true); - } + } + // measure .. + long t0 = System.currentTimeMillis(); + int frames; + for(frames=0; frames<GLSLMiscHelper.frames_perftest; frames+=4) { // SP0 st.attachShaderProgram(gl, sp0); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestRulerNEWT01.java b/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestRulerNEWT01.java index 365596711..cc0ec4601 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestRulerNEWT01.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/glsl/TestRulerNEWT01.java @@ -58,7 +58,7 @@ public class TestRulerNEWT01 extends UITestCase { @Test public void test01() throws InterruptedException { // preset .. - final NEWTGLContext.WindowContext winctx = NEWTGLContext.createWindow(GLProfile.getGL2ES2(), 640, 480, true); + final NEWTGLContext.WindowContext winctx = NEWTGLContext.createOnscreenWindow(GLProfile.getGL2ES2(), 640, 480, true); final GLDrawable drawable = winctx.context.getGLDrawable(); final GL2ES2 gl = winctx.context.getGL().getGL2ES2(); System.err.println(winctx.context); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2File.java b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2File.java index 95e7d6e53..b829c8deb 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2File.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2File.java @@ -37,7 +37,7 @@ import java.io.File; public class ReadBuffer2File extends ReadBufferBase { public ReadBuffer2File(GLDrawable externalRead) { - super(externalRead); + super(externalRead, false); } @Override @@ -52,10 +52,9 @@ public class ReadBuffer2File extends ReadBufferBase { } File file = File.createTempFile("shot" + shotNum + "-", ".ppm"); - TextureIO.write(readBufferUtil.getTextureData(), file); + readBufferUtil.write(file); System.out.println("Wrote: " + file.getAbsolutePath() + ", ..."); shotNum++; - readBufferUtil.rewindPixelBuffer(); } @Override diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2Screen.java b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2Screen.java index 23271dde2..8c315e97f 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2Screen.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2Screen.java @@ -47,7 +47,7 @@ public class ReadBuffer2Screen extends ReadBufferBase { boolean enableBufferVBO = true; // FIXME public ReadBuffer2Screen (GLDrawable externalRead) { - super(externalRead); + super(externalRead, true); } @Override diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBufferBase.java b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBufferBase.java index 53675bc31..e3ca25ae6 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBufferBase.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBufferBase.java @@ -30,16 +30,19 @@ package com.jogamp.opengl.test.junit.jogl.offscreen; import javax.media.opengl.*; +import com.jogamp.opengl.util.GLReadBufferUtil; + public class ReadBufferBase implements GLEventListener { public boolean glDebug = false ; public boolean glTrace = false ; protected GLDrawable externalRead; - ReadBufferUtil readBufferUtil = new ReadBufferUtil(); + GLReadBufferUtil readBufferUtil; - public ReadBufferBase (GLDrawable externalRead) { + public ReadBufferBase (GLDrawable externalRead, boolean write2Texture) { this.externalRead = externalRead ; + this.readBufferUtil = new GLReadBufferUtil(false, write2Texture); } public void init(GLAutoDrawable drawable) { @@ -84,7 +87,7 @@ public class ReadBufferBase implements GLEventListener { public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL(); - readBufferUtil.fetchOffscreenTexture(drawable, gl); + readBufferUtil.readPixels(gl, drawable, false); } } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBufferUtil.java b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBufferUtil.java deleted file mode 100644 index 5a2c73cf4..000000000 --- a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBufferUtil.java +++ /dev/null @@ -1,106 +0,0 @@ -/** - * 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.opengl.test.junit.jogl.offscreen; - -import com.jogamp.opengl.util.GLBuffers; -import java.nio.*; -import javax.media.opengl.*; - -import com.jogamp.opengl.util.texture.Texture; -import com.jogamp.opengl.util.texture.TextureData; - -public class ReadBufferUtil { - protected int readPixelSizeLast = 0; - protected Buffer readPixelBuffer = null; - protected TextureData readTextureData = null; - protected Texture readTexture = new Texture(GL.GL_TEXTURE_2D); - - public Buffer getPixelBuffer() { return readPixelBuffer; } - public void rewindPixelBuffer() { readPixelBuffer.rewind(); } - - public TextureData getTextureData() { return readTextureData; } - public Texture getTexture() { return readTexture; } - - public boolean isValid() { - return null!=readTexture && null!=readTextureData && null!=readPixelBuffer ; - } - - public void fetchOffscreenTexture(GLDrawable drawable, GL gl) { - int readPixelSize = drawable.getWidth() * drawable.getHeight() * 3 ; // RGB - boolean newData = false; - if(readPixelSize>readPixelSizeLast) { - readPixelBuffer = GLBuffers.newDirectGLBuffer(GL.GL_UNSIGNED_BYTE, readPixelSize); - readPixelSizeLast = readPixelSize ; - try { - readTextureData = new TextureData( - gl.getGLProfile(), - // gl.isGL2GL3()?gl.GL_RGBA:gl.GL_RGB, - gl.GL_RGB, - drawable.getWidth(), drawable.getHeight(), - 0, - gl.GL_RGB, - gl.GL_UNSIGNED_BYTE, - false, false, - false /* flip */, - readPixelBuffer, - null /* Flusher */); - newData = true; - } catch (Exception e) { - readTextureData = null; - readPixelBuffer = null; - readPixelSizeLast = 0; - throw new RuntimeException("can not fetch offscreen texture", e); - } - } - if(null!=readPixelBuffer) { - readPixelBuffer.clear(); - gl.glReadPixels(0, 0, drawable.getWidth(), drawable.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, readPixelBuffer); - readPixelBuffer.rewind(); - if(newData) { - readTexture.updateImage(gl, readTextureData); - } else { - readTexture.updateSubImage(gl, readTextureData, 0, - 0, 0, // src offset - 0, 0, // dst offset - drawable.getWidth(), drawable.getHeight()); - } - readPixelBuffer.rewind(); - } - } - - public void dispose(GL gl) { - readTexture.destroy(gl); - readTextureData = null; - readPixelBuffer.clear(); - readPixelBuffer = null; - readPixelSizeLast = 0; - } - -} - diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/Surface2File.java b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/Surface2File.java index 77fd40181..8ca1f3ef8 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/Surface2File.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/Surface2File.java @@ -30,6 +30,7 @@ package com.jogamp.opengl.test.junit.jogl.offscreen; import javax.media.opengl.*; +import com.jogamp.opengl.util.GLReadBufferUtil; import com.jogamp.opengl.util.texture.TextureIO; import java.io.File; @@ -39,7 +40,7 @@ import javax.media.nativewindow.*; public class Surface2File implements SurfaceUpdatedListener { - ReadBufferUtil readBufferUtil = new ReadBufferUtil(); + GLReadBufferUtil readBufferUtil = new GLReadBufferUtil(false, false); int shotNum = 0; public void dispose(GL gl) { @@ -54,7 +55,7 @@ public class Surface2File implements SurfaceUpdatedListener { GL gl = ctx.getGL(); // FIXME glFinish() is an expensive paranoia sync, should not be necessary due to spec gl.glFinish(); - readBufferUtil.fetchOffscreenTexture(drawable, gl); + readBufferUtil.readPixels(gl, drawable, false); gl.glFinish(); try { surface2File("shot"); @@ -71,9 +72,8 @@ public class Surface2File implements SurfaceUpdatedListener { } File file = File.createTempFile(basename + shotNum + "-", ".ppm"); - TextureIO.write(readBufferUtil.getTextureData(), file); + readBufferUtil.write(file); System.err.println("Wrote: " + file.getAbsolutePath() + ", ..."); shotNum++; - readBufferUtil.rewindPixelBuffer(); } } diff --git a/src/test/com/jogamp/opengl/test/junit/util/NEWTGLContext.java b/src/test/com/jogamp/opengl/test/junit/util/NEWTGLContext.java index b8df88901..3992c0876 100644 --- a/src/test/com/jogamp/opengl/test/junit/util/NEWTGLContext.java +++ b/src/test/com/jogamp/opengl/test/junit/util/NEWTGLContext.java @@ -31,8 +31,6 @@ import com.jogamp.newt.Display; import com.jogamp.newt.NewtFactory; import com.jogamp.newt.Screen; import com.jogamp.newt.Window; -import com.jogamp.opengl.util.GLArrayDataServer; -import com.jogamp.opengl.util.glsl.ShaderState; import javax.media.opengl.GL; import javax.media.opengl.GL2ES2; @@ -56,7 +54,44 @@ public class NEWTGLContext { } } - public static WindowContext createWindow(GLProfile glp, int width, int height, boolean debugGL) throws InterruptedException { + public static WindowContext createOffscreenWindow(GLProfile glp, int width, int height, boolean debugGL) throws InterruptedException { + GLCapabilities caps = new GLCapabilities(glp); + caps.setOnscreen(false); + + // + // Create native windowing resources .. X11/Win/OSX + // + Display display = NewtFactory.createDisplay(null); // local display + Assert.assertNotNull(display); + + Screen screen = NewtFactory.createScreen(display, 0); // screen 0 + Assert.assertNotNull(screen); + + Window window = NewtFactory.createWindow(screen, caps); + Assert.assertNotNull(window); + window.setSize(width, height); + window.setVisible(true); + AWTRobotUtil.waitForVisible(window, true); + AWTRobotUtil.waitForRealized(window, true); + + GLDrawableFactory factory = GLDrawableFactory.getFactory(glp); + GLDrawable drawable = factory.createGLDrawable(window); + Assert.assertNotNull(drawable); + + drawable.setRealized(true); + + GLContext context = drawable.createContext(null); + Assert.assertNotNull(context); + + context.enableGLDebugMessage(debugGL); + + int res = context.makeCurrent(); + Assert.assertTrue(GLContext.CONTEXT_CURRENT_NEW==res || GLContext.CONTEXT_CURRENT==res); + + return new WindowContext(window, context); + } + + public static WindowContext createOnscreenWindow(GLProfile glp, int width, int height, boolean debugGL) throws InterruptedException { GLCapabilities caps = new GLCapabilities(glp); // // Create native windowing resources .. X11/Win/OSX diff --git a/src/test/com/jogamp/opengl/test/junit/util/SingletonInstance.java b/src/test/com/jogamp/opengl/test/junit/util/SingletonInstance.java deleted file mode 100644 index f72a41e15..000000000 --- a/src/test/com/jogamp/opengl/test/junit/util/SingletonInstance.java +++ /dev/null @@ -1,164 +0,0 @@ -/** - * 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.opengl.test.junit.util; - -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.channels.FileLock; -import java.util.Date; - -public class SingletonInstance { - - static final boolean DEBUG = true; - static final String temp_file_path; - - static { - String s = null; - try { - File tmpFile = File.createTempFile("TEST", "tst"); - String absTmpFile = tmpFile.getCanonicalPath(); - tmpFile.delete(); - s = absTmpFile.substring(0, absTmpFile.lastIndexOf(File.separator)); - } catch (IOException ex) { - ex.printStackTrace(); - } - temp_file_path = s; - } - - public static String getCanonicalTempPath() { - return temp_file_path; - } - - public static String getCanonicalTempLockFilePath(String basename) { - return getCanonicalTempPath() + File.separator + basename; - } - - public SingletonInstance(String name, String lockFileBasename) { - this.name = name; - this.file = new File ( getCanonicalTempLockFilePath ( lockFileBasename ) ); - setupFileCleanup(); - } - - public SingletonInstance(String name, File lockFile) { - this.name = name; - this.file = lockFile ; - setupFileCleanup(); - } - - public String getName() { return name; } - - void setupFileCleanup() { - file.deleteOnExit(); - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - unlock(); - } - }); - } - - public synchronized void lock(long timeout_ms, long poll_ms) { - long i=0; - try { - do { - if(tryLock()) { - return; - } - if(DEBUG && 0==i) { - System.err.println("SLOCK "+System.currentTimeMillis()+" ??? "+name+" - Wait for lock " + file); - } - i++; - Thread.sleep(poll_ms); - } while ( i < timeout_ms / poll_ms ) ; - } catch ( InterruptedException ie ) { - throw new RuntimeException(ie); - } - throw new RuntimeException("SLOCK "+System.currentTimeMillis()+" EEE "+name+" - couldn't get lock within "+timeout_ms+"ms"); - } - - public synchronized boolean tryLock() { - try { - randomAccessFile = new RandomAccessFile(file, "rw"); - fileLock = randomAccessFile.getChannel().tryLock(); - - if (fileLock != null) { - locked = true; - if(DEBUG) { - System.err.println("SLOCK "+System.currentTimeMillis()+" +++ "+name+" - Locked " + file); - } - return true; - } - } catch (Exception e) { - System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+name+" - Unable to create and/or lock file: " + file); - e.printStackTrace(); - } - return false; - } - - public synchronized boolean unlock() { - try { - if(null != fileLock) { - if(locked) { - fileLock.release(); - if(DEBUG) { - System.err.println("SLOCK "+System.currentTimeMillis()+" --- "+name+" - Unlocked " + file); - } - } - fileLock = null; - } - if(null != randomAccessFile) { - randomAccessFile.close(); - randomAccessFile = null; - } - if(null != file) { - file.delete(); - file = null; - } - return true; - } catch (Exception e) { - System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+name+" - Unable to remove lock file: " + file); - e.printStackTrace(); - } finally { - fileLock = null; - randomAccessFile = null; - locked = false; - } - return false; - } - - public synchronized boolean isLocked() { - return locked; - } - - String name; - File file = null; - RandomAccessFile randomAccessFile = null; - FileLock fileLock = null; - boolean locked = false; -} diff --git a/src/test/com/jogamp/opengl/test/junit/util/UITestCase.java b/src/test/com/jogamp/opengl/test/junit/util/UITestCase.java index ff2e838f6..a25835596 100644 --- a/src/test/com/jogamp/opengl/test/junit/util/UITestCase.java +++ b/src/test/com/jogamp/opengl/test/junit/util/UITestCase.java @@ -28,6 +28,8 @@ package com.jogamp.opengl.test.junit.util; +import com.jogamp.common.util.locks.SingletonInstance; + import org.junit.Before; import org.junit.BeforeClass; import org.junit.After; @@ -40,13 +42,20 @@ public abstract class UITestCase { @Rule public TestName _unitTestName = new TestName(); public static final String SINGLE_INSTANCE_LOCK_FILE = "UITestCase.lock"; + public static final int SINGLE_INSTANCE_LOCK_PORT = 59999; + + public static final long SINGLE_INSTANCE_LOCK_TO = 3*60*1000; // wait up to 3 min + public static final long SINGLE_INSTANCE_LOCK_POLL = 1000; // poll every 1s static volatile SingletonInstance singletonInstance; private final synchronized void initSingletonInstance() { if( null == singletonInstance ) { - singletonInstance = new SingletonInstance(getClass().getName(), SINGLE_INSTANCE_LOCK_FILE); - singletonInstance.lock(3*60*1000, 1000); // wait up to 3 min, poll every 1s + // singletonInstance = SingletonInstance.createFileLock(SINGLE_INSTANCE_LOCK_POLL, SINGLE_INSTANCE_LOCK_FILE); + singletonInstance = SingletonInstance.createServerSocket(SINGLE_INSTANCE_LOCK_POLL, SINGLE_INSTANCE_LOCK_PORT); + if(!singletonInstance.tryLock(SINGLE_INSTANCE_LOCK_TO)) { + throw new RuntimeException("Fatal: Could lock single instance: "+singletonInstance.getName()); + } } } |