package com.luciad.view.opengl.binding.jogl2; import org.junit.Assert; import org.junit.Test; import javax.media.opengl.DefaultGLCapabilitiesChooser; import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLDrawableFactory; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.swing.JFrame; import java.awt.BorderLayout; /** * Tests whether GL context sharing with a pbuffer can prevent a GLCanvas's * texture objects from being lost when the canvas is moved to a new parent. * * @author tomn */ public class GLCanvasContextSharingFullscreenTest { @Test public void testContextSharing() throws InterruptedException { // Create a canvas and add a listener that performs the tests. GLCanvas canvas = new GLCanvas(); canvas.setSize( 500, 500 ); canvas.addGLEventListener( new MyEventListener() ); // Show the canvas in windowed mode JFrame frame = new JFrame(); frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); frame.getContentPane().setLayout( new BorderLayout() ); frame.getContentPane().add( canvas, BorderLayout.CENTER ); frame.pack(); frame.setVisible( true ); canvas.display(); Thread.sleep( 2000 ); // Switch the canvas to "fullscreen" mode JFrame fullscreen = new JFrame(); fullscreen.setUndecorated( true ); fullscreen.setExtendedState( JFrame.MAXIMIZED_BOTH ); frame.getContentPane().remove( canvas ); frame.setVisible( false ); fullscreen.getContentPane().setLayout( new BorderLayout() ); fullscreen.getContentPane().add( canvas, BorderLayout.CENTER ); fullscreen.setVisible( true ); canvas.display(); Thread.sleep( 2000 ); } private static class MyEventListener implements GLEventListener { private int fTextureObject = -1; private GLAutoDrawable fSharing; @Override public void init( GLAutoDrawable aGLAutoDrawable ) { System.out.println( "Canvas init" ); if ( fSharing != null ) { fSharing.display(); return; } // When canvas is inited, share its context with a pbuffer System.out.println( "Creating sharing context" ); GLProfile profile = aGLAutoDrawable.getGLProfile(); fSharing = GLDrawableFactory.getFactory( profile ).createGLPbuffer( null, new GLCapabilities( profile ), new DefaultGLCapabilitiesChooser(), 16, 16, aGLAutoDrawable.getContext() ); // Let pbuffer check if it actually shares textures with the canvas fSharing.addGLEventListener( new GLEventListener() { @Override public void init( GLAutoDrawable aGLAutoDrawable ) { } @Override public void dispose( GLAutoDrawable aGLAutoDrawable ) { } @Override public void display( GLAutoDrawable aGLAutoDrawable ) { System.out.println( "Pbuffer display" ); if ( fTextureObject != -1 ) { GL2 gl = aGLAutoDrawable.getGL().getGL2(); Assert.assertTrue( "Texture object lost in pbuffer", gl.glIsTexture( fTextureObject ) ); } } @Override public void reshape( GLAutoDrawable aGLAutoDrawable, int i, int i1, int i2, int i3 ) { } } ); fSharing.display(); } @Override public void dispose( GLAutoDrawable aGLAutoDrawable ) { System.out.println( "Canvas destroy" ); fSharing.display(); } @Override public void display( GLAutoDrawable aGLAutoDrawable ) { System.out.println( "Canvas display" ); GL2 gl = aGLAutoDrawable.getGL().getGL2(); // Initialize a texture object if necessary if ( fTextureObject == -1 ) { int[] t = new int[ 1 ]; gl.glGenTextures( 1, t, 0 ); fTextureObject = t[ 0 ]; gl.glClearColor( 0f, 1f, 0f, 1f ); gl.glClear( GL2.GL_COLOR_BUFFER_BIT ); gl.glBindTexture( GL2.GL_TEXTURE_2D, fTextureObject ); gl.glCopyTexImage2D( GL2.GL_TEXTURE_2D, 0, GL2.GL_RGB8, 0, 0, 256, 256, 0 ); } // Check if the texture object is valid. This fails after going full screen. Assert.assertTrue( "Texture object lost in canvas", gl.glIsTexture( fTextureObject ) ); gl.glClearColor( 1f, 0f, 0f, 1f ); gl.glClear( GL2.GL_COLOR_BUFFER_BIT ); // Use the texture for visual verification gl.glBindTexture( GL2.GL_TEXTURE_2D, fTextureObject ); gl.glTexParameteri( GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR ); gl.glTexParameteri( GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR ); gl.glEnable( GL2.GL_TEXTURE_2D ); gl.glBegin( GL2.GL_QUADS ); gl.glTexCoord2f( 0, 0 ); gl.glVertex2f( -1, -1 ); gl.glTexCoord2f( 1, 0 ); gl.glVertex2f( 0, -1 ); gl.glTexCoord2f( 1, 1 ); gl.glVertex2f( 0, 0 ); gl.glTexCoord2f( 0, 1 ); gl.glVertex2f( -1, 0 ); gl.glEnd(); fSharing.display(); } @Override public void reshape( GLAutoDrawable aGLAutoDrawable, int i, int i1, int i2, int i3 ) { System.out.println( "Canvas reshape" ); } } }