diff options
author | Sven Gothel <[email protected]> | 2013-05-08 03:49:55 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2013-05-08 03:49:55 +0200 |
commit | ff08ebae2f6ed8788d481f4a21fc7a07a75733ee (patch) | |
tree | 9afe3ea4c5fc4df2e5dd3b91c6de17fb4fc2800b | |
parent | da8717097df2afba3fc7e9ef648ce6bc4ebd4f9f (diff) |
GLJPanel: Use PixelBufferProvider AWTPixelBufferProviderInt; PixelBufferProvider: Add 'dispose()' to interface.
Transition reusing AWT specific PixelBufferProvider to allow a later user provided PixelBufferProvider.
6 files changed, 323 insertions, 28 deletions
diff --git a/make/scripts/tests.sh b/make/scripts/tests.sh index 8221f1da8..3fae45fdb 100755 --- a/make/scripts/tests.sh +++ b/make/scripts/tests.sh @@ -280,6 +280,7 @@ function testawtswt() { #testnoawt com.jogamp.opengl.test.junit.jogl.demos.es1.newt.TestOlympicES1NEWT $* #testnoawt com.jogamp.opengl.test.junit.jogl.demos.es1.newt.TestRedSquareES1NEWT $* #testawt com.jogamp.opengl.test.junit.jogl.demos.es2.awt.TestGearsES2AWT $* +testawt com.jogamp.opengl.test.junit.jogl.demos.es2.awt.TestGearsES2GLJPanelAWT $* #testawt com.jogamp.opengl.test.junit.jogl.demos.es2.newt.TestGearsES2NewtCanvasAWT $* #testnoawt com.jogamp.opengl.test.junit.jogl.demos.es2.newt.TestGearsES2NEWT $* #testawtswt com.jogamp.opengl.test.junit.jogl.demos.es2.newt.TestGearsES2NewtCanvasSWT $* @@ -385,7 +386,7 @@ function testawtswt() { #testnoawt com.jogamp.opengl.test.junit.newt.TestDisplayLifecycle02NEWT #testnoawt com.jogamp.opengl.test.junit.newt.TestScreenMode00NEWT $* #testnoawt com.jogamp.opengl.test.junit.newt.TestScreenMode00bNEWT -testnoawt com.jogamp.opengl.test.junit.newt.TestScreenMode01NEWT +#testnoawt com.jogamp.opengl.test.junit.newt.TestScreenMode01NEWT #testnoawt com.jogamp.opengl.test.junit.newt.TestScreenMode01aNEWT $* #testnoawt com.jogamp.opengl.test.junit.newt.TestScreenMode01bNEWT $* #testnoawt com.jogamp.opengl.test.junit.newt.TestScreenMode01cNEWT $* diff --git a/src/jogl/classes/com/jogamp/opengl/util/GLReadBufferUtil.java b/src/jogl/classes/com/jogamp/opengl/util/GLReadBufferUtil.java index 140c8691f..dc87c7ac9 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/GLReadBufferUtil.java +++ b/src/jogl/classes/com/jogamp/opengl/util/GLReadBufferUtil.java @@ -264,6 +264,7 @@ public class GLReadBufferUtil { readPixelBuffer = null; } readPixelSizeLast = 0; + pixelBufferProvider.dispose(); } } diff --git a/src/jogl/classes/com/jogamp/opengl/util/texture/TextureData.java b/src/jogl/classes/com/jogamp/opengl/util/texture/TextureData.java index 66fba98fe..2f0c86255 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/texture/TextureData.java +++ b/src/jogl/classes/com/jogamp/opengl/util/texture/TextureData.java @@ -105,6 +105,9 @@ public class TextureData { * </p> */ Buffer allocate(int width, int height, int minByteSize); + + /** Dispose resources. */ + void dispose(); } /** * Default {@link PixelBufferProvider} utilizing best match for {@link PixelAttributes} @@ -139,6 +142,11 @@ public class TextureData { public final Buffer allocate(int width, int height, int minByteSize) { return Buffers.newDirectByteBuffer(minByteSize); } + + @Override + public void dispose() { + // nop + } } protected int width; diff --git a/src/jogl/classes/com/jogamp/opengl/util/texture/awt/AWTTextureData.java b/src/jogl/classes/com/jogamp/opengl/util/texture/awt/AWTTextureData.java index d77bd835e..7a0f00edf 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/texture/awt/AWTTextureData.java +++ b/src/jogl/classes/com/jogamp/opengl/util/texture/awt/AWTTextureData.java @@ -104,11 +104,22 @@ public class AWTTextureData extends TextureData { return IntBuffer.wrap( readBackIntBuffer ); } + @Override + public void dispose() { + if(null != image) { + image.flush(); + image = null; + } + } + /** Returns the number source components being used as indicated at {@link #allocate(int, int, int)}. */ public int getComponentCount() { return componentCount; } /** Returns the underlying {@link BufferedImage} as allocated via {@link #allocate(int, int, int)}. */ public BufferedImage getImage() { return image; } + + /** Returns true if an underlying {@link BufferedImage} has been allocated via {@link #allocate(int, int, int)}. */ + public boolean hasImage() { return null != image; } } // Mechanism for lazily converting input BufferedImages with custom diff --git a/src/jogl/classes/javax/media/opengl/awt/GLJPanel.java b/src/jogl/classes/javax/media/opengl/awt/GLJPanel.java index 383c40dc3..2543c5ec4 100644 --- a/src/jogl/classes/javax/media/opengl/awt/GLJPanel.java +++ b/src/jogl/classes/javax/media/opengl/awt/GLJPanel.java @@ -84,10 +84,12 @@ import jogamp.opengl.GLDrawableHelper; import jogamp.opengl.GLDrawableImpl; import jogamp.opengl.awt.Java2D; import jogamp.opengl.util.glsl.GLSLTextureRaster; - import com.jogamp.nativewindow.awt.AWTWindowClosingProtocol; import com.jogamp.opengl.FBObject; +import com.jogamp.opengl.util.GLBuffers; import com.jogamp.opengl.util.GLPixelStorageModes; +import com.jogamp.opengl.util.texture.TextureData.PixelAttributes; +import com.jogamp.opengl.util.texture.awt.AWTTextureData.AWTPixelBufferProviderInt; /** A lightweight Swing component which provides OpenGL rendering support. Provided for compatibility with Swing user interfaces @@ -920,17 +922,15 @@ public class GLJPanel extends JPanel implements AWTGLAutoDrawable, WindowClosing // backends, both of which rely on reading back the OpenGL frame // buffer and drawing it with a BufferedImage class OffscreenBackend implements Backend { - // This image is exactly the correct size to render into the panel - protected BufferedImage offscreenImage; + protected AWTPixelBufferProviderInt pixelBufferProvider = new AWTPixelBufferProviderInt(); + private PixelAttributes pixelAttribs; + // One of these is used to store the read back pixels before storing // in the BufferedImage protected IntBuffer readBackInts; protected int readBackWidthInPixels; protected int readBackHeightInPixels; - private int glFormat; - private int glType; - // Implementation using software rendering private GLDrawableImpl offscreenDrawable; private FBObject fboFlipped; @@ -1049,10 +1049,7 @@ public class GLJPanel extends JPanel implements AWTGLAutoDrawable, WindowClosing @Override public void setOpaque(boolean opaque) { if (opaque != isOpaque()) { - if (offscreenImage != null) { - offscreenImage.flush(); - offscreenImage = null; - } + pixelBufferProvider.dispose(); } } @@ -1065,32 +1062,44 @@ public class GLJPanel extends JPanel implements AWTGLAutoDrawable, WindowClosing @Override public void postGL(Graphics g, boolean isDisplay) { if (isDisplay) { + final GL gl = offscreenContext.getGL(); + + final int componentCount; + final int alignment; + if( isOpaque() ) { + // w/o alpha + componentCount = 3; + alignment = 1; + } else { + // with alpha + componentCount = 4; + alignment = 4; + } + // Must now copy pixels from offscreen context into surface - if (offscreenImage == null) { + if ( !pixelBufferProvider.hasImage() ) { if (0 >= panelWidth || 0 >= panelHeight ) { return; } - final boolean withAlpha = !isOpaque(); - glFormat = GL.GL_BGRA; - glType = GL.GL_UNSIGNED_BYTE; // offscreenContext.getDefaultPixelDataType(); + pixelAttribs = pixelBufferProvider.getAttributes(gl, componentCount); - offscreenImage = new BufferedImage(panelWidth, panelHeight, withAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB); + final int[] tmp = { 0 }; + final int readPixelSize = GLBuffers.sizeof(gl, tmp, pixelAttribs.format, pixelAttribs.type, panelWidth, panelHeight, 1, true); + final IntBuffer intBuffer = (IntBuffer) pixelBufferProvider.allocate(panelWidth, panelHeight, readPixelSize); if(!flipVertical || null != glslTextureRaster) { - final int[] readBackIntBuffer = ((DataBufferInt) offscreenImage.getRaster().getDataBuffer()).getData(); - readBackInts = IntBuffer.wrap(readBackIntBuffer); + readBackInts = intBuffer; } else { readBackInts = IntBuffer.allocate(readBackWidthInPixels * readBackHeightInPixels); } if(DEBUG) { + final BufferedImage offscreenImage = pixelBufferProvider.getImage(); System.err.println(getThreadName()+": GLJPanel.OffscreenBackend.postGL.0: flippedVertical "+flipVertical+", glslTextureRaster "+(null!=glslTextureRaster)); System.err.println(getThreadName()+": GLJPanel.OffscreenBackend.postGL.0: panelSize "+panelWidth+"x"+panelHeight +", readBackSizeInPixels "+readBackWidthInPixels+"x"+readBackHeightInPixels); System.err.println(getThreadName()+": GLJPanel.OffscreenBackend.postGL.0: offscreenImage "+offscreenImage.getWidth()+"x"+offscreenImage.getHeight()); } } - final GL gl = offscreenContext.getGL(); - if( DEBUG_VIEWPORT ) { int[] vp = new int[] { 0, 0, 0, 0 }; gl.glGetIntegerv(GL.GL_VIEWPORT, vp, 0); @@ -1098,7 +1107,7 @@ public class GLJPanel extends JPanel implements AWTGLAutoDrawable, WindowClosing } // Save current modes - psm.setAlignment(gl, 1, 1); + psm.setAlignment(gl, alignment, alignment); if(gl.isGL2GL3()) { final GL2GL3 gl2gl3 = gl.getGL2GL3(); gl2gl3.glPixelStorei(GL2GL3.GL_PACK_ROW_LENGTH, readBackWidthInPixels); @@ -1117,11 +1126,11 @@ public class GLJPanel extends JPanel implements AWTGLAutoDrawable, WindowClosing gl.glBindTexture(GL.GL_TEXTURE_2D, fboTex.getName()); // gl.glClear(GL.GL_DEPTH_BUFFER_BIT); // fboFlipped runs w/o DEPTH! glslTextureRaster.display(gl.getGL2ES2()); - gl.glReadPixels(0, 0, readBackWidthInPixels, readBackHeightInPixels, glFormat, glType, readBackInts); + gl.glReadPixels(0, 0, readBackWidthInPixels, readBackHeightInPixels, pixelAttribs.format, pixelAttribs.type, readBackInts); fboFlipped.unbind(gl); } else { - gl.glReadPixels(0, 0, readBackWidthInPixels, readBackHeightInPixels, glFormat, glType, readBackInts); + gl.glReadPixels(0, 0, readBackWidthInPixels, readBackHeightInPixels, pixelAttribs.format, pixelAttribs.type, readBackInts); if ( flipVertical ) { // Copy temporary data into raster of BufferedImage for faster @@ -1129,6 +1138,7 @@ public class GLJPanel extends JPanel implements AWTGLAutoDrawable, WindowClosing // where !offscreenContext.offscreenImageNeedsVerticalFlip(), // but that's the software rendering path which is very slow // anyway + final BufferedImage offscreenImage = pixelBufferProvider.getImage(); final Object src = readBackInts.array(); final Object dest = ((DataBufferInt) offscreenImage.getRaster().getDataBuffer()).getData(); final int srcIncr = readBackWidthInPixels; @@ -1153,7 +1163,8 @@ public class GLJPanel extends JPanel implements AWTGLAutoDrawable, WindowClosing public void doPaintComponent(Graphics g) { helper.invokeGL(offscreenDrawable, offscreenContext, updaterDisplayAction, updaterInitAction); - if (offscreenImage != null) { + final BufferedImage offscreenImage = pixelBufferProvider.getImage(); + if ( null != offscreenImage ) { // Draw resulting image in one shot g.drawImage(offscreenImage, 0, 0, offscreenImage.getWidth(), @@ -1204,10 +1215,7 @@ public class GLJPanel extends JPanel implements AWTGLAutoDrawable, WindowClosing } } - if (offscreenImage != null) { - offscreenImage.flush(); - offscreenImage = null; - } + pixelBufferProvider.dispose(); return _drawable.isRealized(); } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/awt/TestGearsES2GLJPanelAWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/awt/TestGearsES2GLJPanelAWT.java new file mode 100644 index 000000000..eddf5126b --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/awt/TestGearsES2GLJPanelAWT.java @@ -0,0 +1,266 @@ +/** + * 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.demos.es2.awt; + +import java.awt.AWTException; +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.lang.reflect.InvocationTargetException; + +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLProfile; +import javax.media.opengl.awt.GLJPanel; +import javax.swing.JFrame; +import javax.swing.SwingUtilities; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.jogamp.newt.event.TraceKeyAdapter; +import com.jogamp.newt.event.TraceWindowAdapter; +import com.jogamp.newt.event.awt.AWTKeyAdapter; +import com.jogamp.newt.event.awt.AWTWindowAdapter; +import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2; +import com.jogamp.opengl.test.junit.util.MiscUtils; +import com.jogamp.opengl.test.junit.util.QuitAdapter; +import com.jogamp.opengl.test.junit.util.UITestCase; +import com.jogamp.opengl.util.FPSAnimator; + +public class TestGearsES2GLJPanelAWT extends UITestCase { + static GLProfile glp; + static int width, height; + static boolean shallUsePBuffer = false; + static boolean shallUseBitmap = false; + static boolean useMSAA = false; + static int swapInterval = 1; + static boolean useAnimator = true; + static boolean manualTest = false; + + @BeforeClass + public static void initClass() { + if(GLProfile.isAvailable(GLProfile.GL2)) { + glp = GLProfile.get(GLProfile.GL2); + Assert.assertNotNull(glp); + width = 640; + height = 480; + } else { + setTestSupported(false); + } + } + + @AfterClass + public static void releaseClass() { + } + + protected void runTestGL(GLCapabilities caps) + throws AWTException, InterruptedException, InvocationTargetException + { + final JFrame frame = new JFrame("Swing GLJPanel"); + Assert.assertNotNull(frame); + + final GLJPanel glJPanel = new GLJPanel(caps); + Assert.assertNotNull(glJPanel); + Dimension glc_sz = new Dimension(width, height); + glJPanel.setMinimumSize(glc_sz); + glJPanel.setPreferredSize(glc_sz); + glJPanel.setSize(glc_sz); + glJPanel.addGLEventListener(new GearsES2()); + final SnapshotGLEventListener snap = new SnapshotGLEventListener(); + glJPanel.addGLEventListener(snap); + + final FPSAnimator animator = useAnimator ? new FPSAnimator(glJPanel, 60) : null; + + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + frame.getContentPane().add(glJPanel, BorderLayout.CENTER); + frame.getContentPane().validate(); + frame.pack(); + frame.setVisible(true); + } } ) ; + + if( useAnimator ) { + animator.setUpdateFPSFrames(60, System.err); + animator.start(); + Assert.assertEquals(true, animator.isAnimating()); + } + + QuitAdapter quitAdapter = new QuitAdapter(); + + new AWTKeyAdapter(new TraceKeyAdapter(quitAdapter)).addTo(glJPanel); + new AWTWindowAdapter(new TraceWindowAdapter(quitAdapter)).addTo(frame); + + final long t0 = System.currentTimeMillis(); + long t1 = t0; + boolean triggerSnap = false; + while(!quitAdapter.shouldQuit() && t1 - t0 < duration) { + Thread.sleep(100); + t1 = System.currentTimeMillis(); + snap.getDisplayCount(); + if( !triggerSnap && snap.getDisplayCount() > 1 ) { + // Snapshot only after one frame has been rendered to suite FBO MSAA! + snap.setMakeSnapshot(); + triggerSnap = true; + } + } + + Assert.assertNotNull(frame); + Assert.assertNotNull(glJPanel); + Assert.assertNotNull(animator); + + if( useAnimator ) { + animator.stop(); + Assert.assertEquals(false, animator.isAnimating()); + } + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + frame.setVisible(false); + frame.getContentPane().remove(glJPanel); + frame.remove(glJPanel); + glJPanel.destroy(); + frame.dispose(); + } } ); + } + + @Test + public void test01_DefaultNorm() + throws AWTException, InterruptedException, InvocationTargetException + { + GLCapabilities caps = new GLCapabilities(GLProfile.getDefault()); + if(useMSAA) { + caps.setNumSamples(4); + caps.setSampleBuffers(true); + } + if(shallUsePBuffer) { + caps.setPBuffer(true); + } + if(shallUseBitmap) { + caps.setBitmap(true); + } + runTestGL(caps); + } + + @Test + public void test02_DefaultMsaa() + throws AWTException, InterruptedException, InvocationTargetException + { + if( manualTest ) { + return; + } + GLCapabilities caps = new GLCapabilities(GLProfile.getDefault()); + caps.setNumSamples(4); + caps.setSampleBuffers(true); + runTestGL(caps); + } + + @Test + public void test03_PbufferNorm() + throws AWTException, InterruptedException, InvocationTargetException + { + if( manualTest ) { + return; + } + GLCapabilities caps = new GLCapabilities(GLProfile.getDefault()); + caps.setPBuffer(true); + runTestGL(caps); + } + + @Test + public void test04_PbufferMsaa() + throws AWTException, InterruptedException, InvocationTargetException + { + if( manualTest ) { + return; + } + GLCapabilities caps = new GLCapabilities(GLProfile.getDefault()); + caps.setNumSamples(4); + caps.setSampleBuffers(true); + caps.setPBuffer(true); + runTestGL(caps); + } + + @Test + public void test05_BitmapNorm() + throws AWTException, InterruptedException, InvocationTargetException + { + if( manualTest ) { + return; + } + GLCapabilities caps = new GLCapabilities(GLProfile.getDefault()); + caps.setBitmap(true); + runTestGL(caps); + } + + @Test + public void test06_BitmapMsaa() + throws AWTException, InterruptedException, InvocationTargetException + { + if( manualTest ) { + return; + } + GLCapabilities caps = new GLCapabilities(GLProfile.getDefault()); + caps.setNumSamples(4); + caps.setSampleBuffers(true); + caps.setBitmap(true); + runTestGL(caps); + } + + 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++; + duration = MiscUtils.atol(args[i], duration); + } else if(args[i].equals("-vsync")) { + i++; + swapInterval = MiscUtils.atoi(args[i], swapInterval); + } else if(args[i].equals("-msaa")) { + useMSAA = true; + } else if(args[i].equals("-noanim")) { + useAnimator = false; + } else if(args[i].equals("-pbuffer")) { + shallUsePBuffer = true; + } else if(args[i].equals("-bitmap")) { + shallUseBitmap = true; + } else if(args[i].equals("-manual")) { + manualTest = true; + } + } + System.err.println("swapInterval "+swapInterval); + System.err.println("useMSAA "+useMSAA); + System.err.println("useAnimator "+useAnimator); + System.err.println("shallUsePBuffer "+shallUsePBuffer); + System.err.println("shallUseBitmap "+shallUseBitmap); + System.err.println("manualTest "+manualTest); + + org.junit.runner.JUnitCore.main(TestGearsES2GLJPanelAWT.class.getName()); + } +} |