From 908ebd99d1eb57ce773a1fdd67c76886da86b9e6 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 13 Feb 2014 21:32:36 +0100 Subject: Bug 975 - GLJPanel's OffscreenDrawable shall not double swap (custom swap by GLEventListener using [AWT]GLReadBufferUtil) When utilizing [AWT]GLReadBufferUtil it is usually desired to read from the front-buffer instead the back-buffer. The latter may not be defined, e.g. when using MSAA. A GLEventListener utilizing [AWT]GLReadBufferUtil, must perform the drawable.swapBuffers() to be able to read from the front-buffer. Usually GLAutoDrawable.setAutoSwapBuffer(false) should be called here, to avoid a double swap - however GLJPanel does not support toggling auto-swap since it requires to control swap for it's own read-pixels. Remedy for GLJPanel: - GLJPanel issues helper.setAutoSwapBufferMode(false) - immutable - Enable GLJPanel.swapBuffer() if initializes This was previously disabled. - GLJPanel's OffscreenBackend listens to surfaceUpdated, to be notified whether postGL needs to swap buffer or the drawable.swapBuffer() was already called between preGL and postGL. See unit tests adding/removing a snapshot GLEventListener performing swapBuffers() and setting auto-swap accordingly. --- .../test/junit/jogl/acore/GLReadBuffer00Base.java | 97 ++++++++++ .../jogl/acore/TestGLReadBuffer01GLJPanelAWT.java | 214 +++++++++++++++++++++ .../jogl/acore/TestGLReadBuffer01GLWindowNEWT.java | 158 +++++++++++++++ .../junit/jogl/awt/TestGLJPanelResize02AWT.java | 204 -------------------- 4 files changed, 469 insertions(+), 204 deletions(-) create mode 100644 src/test/com/jogamp/opengl/test/junit/jogl/acore/GLReadBuffer00Base.java create mode 100644 src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGLReadBuffer01GLJPanelAWT.java create mode 100644 src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGLReadBuffer01GLWindowNEWT.java delete mode 100644 src/test/com/jogamp/opengl/test/junit/jogl/awt/TestGLJPanelResize02AWT.java (limited to 'src/test/com') diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/acore/GLReadBuffer00Base.java b/src/test/com/jogamp/opengl/test/junit/jogl/acore/GLReadBuffer00Base.java new file mode 100644 index 000000000..c5885d6b4 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/acore/GLReadBuffer00Base.java @@ -0,0 +1,97 @@ +/** + * Copyright 2014 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 java.lang.reflect.InvocationTargetException; + +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLCapabilitiesImmutable; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLProfile; + +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import com.jogamp.opengl.test.junit.util.UITestCase; + +/** + * Multiple GLJPanels in a JFrame + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public abstract class GLReadBuffer00Base extends UITestCase { + + @BeforeClass + public static void initClass() { + GLProfile.initSingleton(); + } + + protected abstract void test(final GLCapabilitiesImmutable caps, final boolean useSwingDoubleBuffer, final boolean skipGLOrientationVerticalFlip); + + private boolean defAutoSwapBufferMode = true; + protected void addSnapshotGLEL(final GLAutoDrawable glad, final GLEventListener snapshotGLEL) { + defAutoSwapBufferMode = glad.getAutoSwapBufferMode(); + glad.setAutoSwapBufferMode(false); + glad.addGLEventListener(snapshotGLEL); + } + protected void removeSnapshotGLEL(final GLAutoDrawable glad, final GLEventListener snapshotGLEL) { + glad.removeGLEventListener(snapshotGLEL); + glad.setAutoSwapBufferMode(defAutoSwapBufferMode); + } + + + @Test + public void test00_MSAA0_DefFlip() throws InterruptedException, InvocationTargetException { + test(new GLCapabilities(null), false /*useSwingDoubleBuffer*/, false /* skipGLOrientationVerticalFlip */); + } + + @Test + public void test01_MSAA0_UsrFlip() throws InterruptedException, InvocationTargetException { + test(new GLCapabilities(null), false /*useSwingDoubleBuffer*/, true /* skipGLOrientationVerticalFlip */); + } + + @Test + public void test10_MSAA4_DefFlip() throws InterruptedException, InvocationTargetException { + final GLCapabilities caps = new GLCapabilities(null); + caps.setNumSamples(4); + caps.setSampleBuffers(true); + test(caps, false /*useSwingDoubleBuffer*/, false /* skipGLOrientationVerticalFlip */); + } + + @Test + public void test11_MSAA4_UsrFlip() throws InterruptedException, InvocationTargetException { + final GLCapabilities caps = new GLCapabilities(null); + caps.setNumSamples(4); + caps.setSampleBuffers(true); + test(caps, false /*useSwingDoubleBuffer*/, true /* skipGLOrientationVerticalFlip */); + } + + static long duration = 500; // ms +} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGLReadBuffer01GLJPanelAWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGLReadBuffer01GLJPanelAWT.java new file mode 100644 index 000000000..4ad84aa73 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/acore/TestGLReadBuffer01GLJPanelAWT.java @@ -0,0 +1,214 @@ +/** + * Copyright 2014 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 java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; +import javax.media.opengl.GL; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLCapabilitiesImmutable; +import javax.media.opengl.GLDrawable; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLProfile; +import javax.media.opengl.awt.GLJPanel; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.SwingUtilities; + +import org.junit.Assume; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2; +import com.jogamp.opengl.test.junit.util.MiscUtils; +import com.jogamp.opengl.util.Animator; +import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil; +import com.jogamp.opengl.util.texture.TextureIO; + +/** + * Multiple GLJPanels in a JFrame + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestGLReadBuffer01GLJPanelAWT extends GLReadBuffer00Base { + + @BeforeClass + public static void initClass() { + GLProfile.initSingleton(); + } + + public void test(final GLCapabilitiesImmutable caps, final boolean useSwingDoubleBuffer, final boolean skipGLOrientationVerticalFlip) { + final AWTGLReadBufferUtil awtGLReadBufferUtil = new AWTGLReadBufferUtil(caps.getGLProfile(), false); + final JFrame frame = new JFrame(); + final Dimension d = new Dimension(320, 240); + final GLJPanel glad = createGLJPanel(skipGLOrientationVerticalFlip, useSwingDoubleBuffer, caps, d); + try { + javax.swing.SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + frame.setLocation(64, 64); + final JPanel panel = new JPanel(); + panel.setLayout(new BorderLayout()); + panel.setDoubleBuffered(useSwingDoubleBuffer); + frame.getContentPane().add(panel); + + final GearsES2 gears = new GearsES2(0); + gears.setFlipVerticalInGLOrientation(skipGLOrientationVerticalFlip); + gears.setVerbose(false); + glad.addGLEventListener(gears); + panel.add(glad); + frame.pack(); + frame.setVisible(true); + } } ); + } catch( Throwable throwable ) { + throwable.printStackTrace(); + Assume.assumeNoException( throwable ); + } + final GLEventListener snapshotGLEL = new SnapshotGLEL(awtGLReadBufferUtil, skipGLOrientationVerticalFlip); + final Dimension size0 = frame.getSize(); + final Dimension size1 = new Dimension(size0.width+100, size0.height+100); + final Dimension size2 = new Dimension(size0.width-100, size0.height-100); + try { + final Animator anim = new Animator(glad); + anim.start(); + try { Thread.sleep(2*duration); } catch (InterruptedException e) { } + anim.stop(); + addSnapshotGLEL(glad, snapshotGLEL); + glad.display(); + + try { Thread.sleep(duration); } catch (InterruptedException e) { } + javax.swing.SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + frame.setSize(size1); + frame.validate(); + } } ); + try { Thread.sleep(duration); } catch (InterruptedException e) { } + javax.swing.SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + frame.setSize(size2); + frame.validate(); + } } ); + try { Thread.sleep(duration); } catch (InterruptedException e) { } + javax.swing.SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + frame.setSize(size0); + frame.validate(); + } } ); + try { Thread.sleep(duration); } catch (InterruptedException e) { } + + removeSnapshotGLEL(glad, snapshotGLEL); + anim.start(); + try { Thread.sleep(2*duration); } catch (InterruptedException e) { } + anim.stop(); + } catch (Exception e1) { + e1.printStackTrace(); + } + try { + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + frame.dispose(); + } } ); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + + private GLJPanel createGLJPanel(final boolean skipGLOrientationVerticalFlip, final boolean useSwingDoubleBuffer, final GLCapabilitiesImmutable caps, final Dimension size) { + final GLJPanel canvas = new GLJPanel(caps); + canvas.setSize(size); + canvas.setPreferredSize(size); + canvas.setMinimumSize(size); + canvas.setDoubleBuffered(useSwingDoubleBuffer); + canvas.setSkipGLOrientationVerticalFlip(skipGLOrientationVerticalFlip); + return canvas; + } + + private class SnapshotGLEL implements GLEventListener { + final AWTGLReadBufferUtil glReadBufferUtil; + final boolean skipGLOrientationVerticalFlip; + int i; + + SnapshotGLEL(final AWTGLReadBufferUtil glReadBufferUtil, final boolean skipGLOrientationVerticalFlip) { + this.glReadBufferUtil = glReadBufferUtil; + this.skipGLOrientationVerticalFlip = skipGLOrientationVerticalFlip; + i = 0; + } + + @Override + public void init(GLAutoDrawable drawable) { } + @Override + public void dispose(GLAutoDrawable drawable) { } + @Override + public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { } + @Override + public void display(GLAutoDrawable drawable) { + snapshot(i++, drawable.getGL(), TextureIO.PNG, null); + } + public void snapshot(int sn, GL gl, String fileSuffix, String destPath) { + final GLDrawable drawable = gl.getContext().getGLReadDrawable(); + final String filenameAWT = getSnapshotFilename(sn, "awt", + drawable.getChosenGLCapabilities(), drawable.getWidth(), drawable.getHeight(), + glReadBufferUtil.hasAlpha(), fileSuffix, destPath); + // gl.glFinish(); // just make sure rendering finished .. + drawable.swapBuffers(); + final boolean awtOrientation = !( drawable.isGLOriented() && skipGLOrientationVerticalFlip ); + System.err.println(Thread.currentThread().getName()+": ** screenshot: awtOrient/v-flip "+awtOrientation+", "+filenameAWT); + + final BufferedImage image = glReadBufferUtil.readPixelsToBufferedImage(gl, awtOrientation); + final File fout = new File(filenameAWT); + try { + ImageIO.write(image, "png", fout); + } catch (IOException e) { + e.printStackTrace(); + } + /** + final String filenameJGL = getSnapshotFilename(sn, "jgl", + drawable.getChosenGLCapabilities(), drawable.getWidth(), drawable.getHeight(), + glReadBufferUtil.hasAlpha(), fileSuffix, destPath); + glReadBufferUtil.write(new File(filenameJGL)); + */ + } + }; + + static GLCapabilitiesImmutable caps = null; + + public static void main(String[] args) { + for(int i=0; i