aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2013-09-08 19:26:20 +0200
committerSven Gothel <[email protected]>2013-09-08 19:26:20 +0200
commitfb6440fb6e4fac31d03799d5cf804d02c78f2c38 (patch)
tree4461a7f1fae2229d8afc8c9d3667ceba69e9a9b3 /src
parent541701f5803c5005864efcb09a0c4aacecb470f9 (diff)
NewtCanvasAWT: Implement AWTPrintLifecycle and hence support for AWT printing.
Note: Same bug existing as w/ GLCanvas, i.e. 'GLDrawableUtil.swapGLContextAndAllGLEventListener(gladPre, gladNew)': If 'gladPre' is onscreen and using MSAA (on NV/GLX), the ctx cannot be made current in it's new 'gladNew' location. Same workaround applied, i.e. use onscreen drawable while printing.
Diffstat (limited to 'src')
-rw-r--r--src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java166
-rw-r--r--src/test/com/jogamp/opengl/test/junit/jogl/tile/TestTiledPrintingGearsNewtAWT.java241
2 files changed, 405 insertions, 2 deletions
diff --git a/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java b/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java
index e4b5a25c4..2127c4096 100644
--- a/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java
+++ b/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java
@@ -32,9 +32,12 @@ package com.jogamp.newt.awt;
import java.awt.AWTKeyStroke;
import java.awt.Canvas;
import java.awt.Component;
+import java.awt.EventQueue;
import java.awt.Graphics;
+import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.KeyboardFocusManager;
+import java.awt.RenderingHints;
import java.beans.Beans;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
@@ -46,6 +49,12 @@ import java.util.Set;
import javax.media.nativewindow.NativeWindow;
import javax.media.nativewindow.OffscreenLayerOption;
import javax.media.nativewindow.WindowClosingProtocol;
+import javax.media.opengl.GLAnimatorControl;
+import javax.media.opengl.GLAutoDrawable;
+import javax.media.opengl.GLCapabilities;
+import javax.media.opengl.GLDrawableFactory;
+import javax.media.opengl.GLProfile;
+import javax.media.opengl.awt.AWTPrintLifecycle;
import javax.swing.MenuSelectionManager;
import jogamp.nativewindow.awt.AWTMisc;
@@ -54,6 +63,7 @@ import jogamp.newt.WindowImpl;
import jogamp.newt.awt.NewtFactoryAWT;
import jogamp.newt.awt.event.AWTParentWindowAdapter;
import jogamp.newt.driver.DriverClearFocus;
+import jogamp.opengl.awt.AWTTilePainter;
import com.jogamp.common.os.Platform;
import com.jogamp.common.util.awt.AWTEDTExecutor;
@@ -69,6 +79,8 @@ import com.jogamp.newt.event.WindowListener;
import com.jogamp.newt.event.awt.AWTAdapter;
import com.jogamp.newt.event.awt.AWTKeyAdapter;
import com.jogamp.newt.event.awt.AWTMouseAdapter;
+import com.jogamp.opengl.util.GLDrawableUtil;
+import com.jogamp.opengl.util.TileRenderer;
/**
* AWT {@link java.awt.Canvas Canvas} containing a NEWT {@link Window} using native parenting.
@@ -80,7 +92,7 @@ import com.jogamp.newt.event.awt.AWTMouseAdapter;
* the underlying JAWT mechanism to composite the image, if supported.
*/
@SuppressWarnings("serial")
-public class NewtCanvasAWT extends java.awt.Canvas implements WindowClosingProtocol, OffscreenLayerOption {
+public class NewtCanvasAWT extends java.awt.Canvas implements WindowClosingProtocol, OffscreenLayerOption, AWTPrintLifecycle {
public static final boolean DEBUG = Debug.debug("Window");
private JAWTWindow jawtWindow = null;
@@ -419,7 +431,7 @@ public class NewtCanvasAWT extends java.awt.Canvas implements WindowClosingProto
@Override
public void paint(Graphics g) {
- if( validateComponent(true) ) {
+ if( validateComponent(true) && !printActive ) {
newtChild.windowRepaint(0, 0, getWidth(), getHeight());
}
}
@@ -441,7 +453,155 @@ public class NewtCanvasAWT extends java.awt.Canvas implements WindowClosingProto
}
}
}
+
+ private static final int PRINT_TILE_SIZE = 512;
+ private volatile boolean printActive = false;
+ private boolean printUseAA = false;
+ private GLAnimatorControl printAnimator = null;
+ private GLAutoDrawable printGLAD = null;
+ private AWTTilePainter printAWTTiles = null;
+
+ @Override
+ public void setupPrint(Graphics2D g2d, double scaleMatX, double scaleMatY) {
+ if( !validateComponent(true) ) {
+ if(DEBUG) {
+ System.err.println(getThreadName()+": Info: NewtCanvasAWT setupPrint - skipped GL render, drawable not valid yet");
+ }
+ return; // not yet available ..
+ }
+ if( !isVisible() ) {
+ if(DEBUG) {
+ System.err.println(getThreadName()+": Info: NewtCanvasAWT setupPrint - skipped GL render, drawable visible");
+ }
+ return; // not yet available ..
+ }
+ printActive = true;
+ final RenderingHints rHints = g2d.getRenderingHints();
+ {
+ final Object _useAA = rHints.get(RenderingHints.KEY_ANTIALIASING);
+ printUseAA = null != _useAA && ( _useAA == RenderingHints.VALUE_ANTIALIAS_DEFAULT || _useAA == RenderingHints.VALUE_ANTIALIAS_ON );
+ }
+ if( DEBUG ) {
+ System.err.println("AWT print.setup: canvasSize "+getWidth()+"x"+getWidth()+", scaleMat "+scaleMatX+" x "+scaleMatY+", useAA "+printUseAA+", printAnimator "+printAnimator);
+ AWTTilePainter.dumpHintsAndScale(g2d);
+ }
+ final int componentCount = isOpaque() ? 3 : 4;
+ final TileRenderer printRenderer = new TileRenderer();
+ printAWTTiles = new AWTTilePainter(printRenderer, componentCount, scaleMatX, scaleMatY, DEBUG);
+ AWTEDTExecutor.singleton.invoke(getTreeLock(), true /* allowOnNonEDT */, true /* wait */, setupPrintOnEDT);
+ }
+ private final Runnable setupPrintOnEDT = new Runnable() {
+ @Override
+ public void run() {
+ final GLAutoDrawable glad;
+ if( null != newtChild && newtChild instanceof GLAutoDrawable ) {
+ glad = (GLAutoDrawable)newtChild;
+ } else {
+ if( DEBUG ) {
+ System.err.println("AWT print.setup exit, newtChild not a GLAutoDrawable: "+newtChild);
+ }
+ printAWTTiles = null;
+ printActive = false;
+ return;
+ }
+ printAnimator = glad.getAnimator();
+ if( null != printAnimator ) {
+ printAnimator.remove(glad);
+ }
+ final GLCapabilities caps = (GLCapabilities)glad.getChosenGLCapabilities().cloneMutable();
+ final GLProfile glp = caps.getGLProfile();
+ if( caps.getSampleBuffers() ) {
+ // bug / issue w/ swapGLContextAndAllGLEventListener and onscreen MSAA w/ NV/GLX
+ printGLAD = glad;
+ } else {
+ caps.setDoubleBuffered(false);
+ caps.setOnscreen(false);
+ if( printUseAA && !caps.getSampleBuffers() ) {
+ if ( !glp.isGL2ES3() ) {
+ if( DEBUG ) {
+ System.err.println("Ignore MSAA due to gl-profile < GL2ES3");
+ }
+ printUseAA = false;
+ } else {
+ caps.setSampleBuffers(true);
+ caps.setNumSamples(8);
+ }
+ }
+ final GLDrawableFactory factory = GLDrawableFactory.getFactory(caps.getGLProfile());
+ printGLAD = factory.createOffscreenAutoDrawable(null, caps, null, PRINT_TILE_SIZE, PRINT_TILE_SIZE, null);
+ GLDrawableUtil.swapGLContextAndAllGLEventListener(glad, printGLAD);
+ }
+
+ printAWTTiles.renderer.setTileSize(printGLAD.getWidth(), printGLAD.getHeight(), 0);
+ printAWTTiles.renderer.attachToAutoDrawable(printGLAD);
+ if( DEBUG ) {
+ System.err.println("AWT print.setup "+printAWTTiles);
+ System.err.println("AWT print.setup AA "+printUseAA+", "+caps);
+ System.err.println("AWT print.setup "+printGLAD);
+ }
+ }
+ };
+
+ @Override
+ public void releasePrint() {
+ if( !printActive || null == printGLAD ) {
+ throw new IllegalStateException("setupPrint() not called");
+ }
+ // sendReshape = false; // clear reshape flag
+ AWTEDTExecutor.singleton.invoke(getTreeLock(), true /* allowOnNonEDT */, true /* wait */, releasePrintOnEDT);
+ newtChild.sendWindowEvent(WindowEvent.EVENT_WINDOW_RESIZED); // trigger a resize/relayout to listener
+ }
+ private final Runnable releasePrintOnEDT = new Runnable() {
+ @Override
+ public void run() {
+ if( DEBUG ) {
+ System.err.println("AWT print.release "+printAWTTiles);
+ }
+ final GLAutoDrawable glad = (GLAutoDrawable)newtChild;
+ printAWTTiles.dispose();
+ printAWTTiles= null;
+ if( printGLAD != glad ) {
+ GLDrawableUtil.swapGLContextAndAllGLEventListener(printGLAD, glad);
+ printGLAD.destroy();
+ }
+ printGLAD = null;
+ if( null != printAnimator ) {
+ printAnimator.add(glad);
+ printAnimator = null;
+ }
+ printActive = false;
+ }
+ };
+
+ @Override
+ public void print(Graphics graphics) {
+ if( !printActive || null == printGLAD ) {
+ throw new IllegalStateException("setupPrint() not called");
+ }
+ if(DEBUG && !EventQueue.isDispatchThread()) {
+ System.err.println(getThreadName()+": Warning: GLCanvas print - not called from AWT-EDT");
+ // we cannot dispatch print on AWT-EDT due to printing internal locking ..
+ }
+
+ final Graphics2D g2d = (Graphics2D)graphics;
+ printAWTTiles.setupGraphics2DAndClipBounds(g2d);
+ try {
+ final TileRenderer tileRenderer = printAWTTiles.renderer;
+ if( DEBUG ) {
+ System.err.println("AWT print.0: "+tileRenderer);
+ }
+ do {
+ tileRenderer.display();
+ } while ( !tileRenderer.eot() );
+ } finally {
+ printAWTTiles.resetGraphics2D();
+ }
+ if( DEBUG ) {
+ System.err.println("AWT print.X: "+printAWTTiles);
+ }
+ }
+
private final void requestFocusNEWTChild() {
if(null!=newtChild) {
newtChild.setFocusAction(null);
@@ -685,6 +845,8 @@ public class NewtCanvasAWT extends java.awt.Canvas implements WindowClosingProto
}
}
}
+
+ protected static String getThreadName() { return Thread.currentThread().getName(); }
static String newtWinHandleToHexString(Window w) {
return null != w ? toHexString(w.getWindowHandle()) : "nil";
diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/tile/TestTiledPrintingGearsNewtAWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/tile/TestTiledPrintingGearsNewtAWT.java
new file mode 100644
index 000000000..820b62743
--- /dev/null
+++ b/src/test/com/jogamp/opengl/test/junit/jogl/tile/TestTiledPrintingGearsNewtAWT.java
@@ -0,0 +1,241 @@
+/**
+ * Copyright 2013 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.tile;
+
+import java.awt.BorderLayout;
+import java.awt.Button;
+import java.awt.Dimension;
+import java.awt.Frame;
+import java.awt.Label;
+import java.awt.Panel;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.print.PageFormat;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.lang.reflect.InvocationTargetException;
+
+import javax.media.opengl.GLCapabilities;
+import javax.media.opengl.GLProfile;
+
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runners.MethodSorters;
+
+import com.jogamp.newt.awt.NewtCanvasAWT;
+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.newt.opengl.GLWindow;
+import com.jogamp.opengl.test.junit.jogl.demos.gl2.Gears;
+import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
+import com.jogamp.opengl.test.junit.util.QuitAdapter;
+import com.jogamp.opengl.util.Animator;
+
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class TestTiledPrintingGearsNewtAWT extends TiledPrintingAWTBase {
+
+ static boolean waitForKey = false;
+ /** only when run manually .. */
+ static boolean allow600dpi = false;
+ static GLProfile glp;
+ static int width, height;
+
+ @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);
+ }
+ // Runtime.getRuntime().traceInstructions(true);
+ // Runtime.getRuntime().traceMethodCalls(true);
+ }
+
+ @AfterClass
+ public static void releaseClass() {
+ }
+
+ protected void runTestGL(GLCapabilities caps) throws InterruptedException, InvocationTargetException {
+ final GLWindow glad = GLWindow.create(caps);
+ Assert.assertNotNull(glad);
+
+ final NewtCanvasAWT canvas = new NewtCanvasAWT(glad);
+ Assert.assertNotNull(canvas);
+ Dimension glc_sz = new Dimension(width, height);
+ canvas.setMinimumSize(glc_sz);
+ canvas.setPreferredSize(glc_sz);
+ canvas.setSize(glc_sz);
+
+ final Gears gears = new Gears();
+ glad.addGLEventListener(gears);
+
+ final Frame frame = new Frame("Newt/AWT Print");
+ Assert.assertNotNull(frame);
+
+ final ActionListener print72DPIAction = new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ doPrintManual(frame, 72, false);
+ } };
+ final ActionListener print300DPIAction = new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ doPrintManual(frame, 300, false);
+ } };
+ final ActionListener print600DPIAction = new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ doPrintManual(frame, 600, false);
+ } };
+ final Button print72DPIButton = new Button("72dpi");
+ print72DPIButton.addActionListener(print72DPIAction);
+ final Button print300DPIButton = new Button("300dpi");
+ print300DPIButton.addActionListener(print300DPIAction);
+ final Button print600DPIButton = new Button("600dpi");
+ print600DPIButton.addActionListener(print600DPIAction);
+
+ frame.setLayout(new BorderLayout());
+ Panel printPanel = new Panel();
+ printPanel.add(print72DPIButton);
+ printPanel.add(print300DPIButton);
+ printPanel.add(print600DPIButton);
+ Panel southPanel = new Panel();
+ southPanel.add(new Label("South"));
+ Panel eastPanel = new Panel();
+ eastPanel.add(new Label("East"));
+ Panel westPanel = new Panel();
+ westPanel.add(new Label("West"));
+ frame.add(printPanel, BorderLayout.NORTH);
+ frame.add(canvas, BorderLayout.CENTER);
+ frame.add(southPanel, BorderLayout.SOUTH);
+ frame.add(eastPanel, BorderLayout.EAST);
+ frame.add(westPanel, BorderLayout.WEST);
+ frame.setTitle("Tiles Newt/AWT Print Test");
+
+ Animator animator = new Animator(glad);
+ QuitAdapter quitAdapter = new QuitAdapter();
+
+ new AWTKeyAdapter(new TraceKeyAdapter(quitAdapter)).addTo(canvas);
+ new AWTWindowAdapter(new TraceWindowAdapter(quitAdapter)).addTo(frame);
+
+ javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ frame.pack();
+ frame.setVisible(true);
+ }});
+ Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, true));
+ Assert.assertEquals(true, AWTRobotUtil.waitForRealized(canvas, true));
+
+ animator.setUpdateFPSFrames(60, System.err);
+ animator.start();
+
+ boolean printDone = false;
+ while(!quitAdapter.shouldQuit() && animator.isAnimating() && ( 0 == duration || animator.getTotalFPSDuration()<duration )) {
+ Thread.sleep(200);
+ if( !printDone ) {
+ printDone = true;
+ doPrintAuto(frame, PageFormat.LANDSCAPE, null, 72, false);
+ waitUntilPrintJobsIdle();
+ doPrintAuto(frame, PageFormat.LANDSCAPE, null, 72, true);
+ waitUntilPrintJobsIdle();
+ // No AA needed for 300 dpi and greater :)
+ doPrintAuto(frame, PageFormat.LANDSCAPE, null, 300, false);
+ waitUntilPrintJobsIdle();
+ if( allow600dpi ) {
+ doPrintAuto(frame, PageFormat.LANDSCAPE, null, 600, false);
+ waitUntilPrintJobsIdle();
+ }
+ }
+ }
+ // try { Thread.sleep(4000); } catch (InterruptedException e) { } // time to finish print jobs .. FIXME ??
+
+ Assert.assertNotNull(frame);
+ Assert.assertNotNull(canvas);
+ Assert.assertNotNull(animator);
+
+ animator.stop();
+ Assert.assertEquals(false, animator.isAnimating());
+ javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ frame.setVisible(false);
+ }});
+ Assert.assertEquals(false, frame.isVisible());
+ javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ final Frame _frame = frame;
+ _frame.remove(canvas);
+ _frame.dispose();
+ }});
+ glad.destroy();
+ }
+
+ @Test
+ public void test01_Onscreen_aa0() throws InterruptedException, InvocationTargetException {
+ GLCapabilities caps = new GLCapabilities(glp);
+ runTestGL(caps);
+ }
+
+ @Test
+ public void test02_Onscreen_aa8() throws InterruptedException, InvocationTargetException {
+ GLCapabilities caps = new GLCapabilities(glp);
+ caps.setSampleBuffers(true);
+ caps.setNumSamples(8); // FIXME
+ runTestGL(caps);
+ }
+
+ static long duration = 500; // ms
+
+ public static void main(String args[]) {
+ allow600dpi = true;
+ for(int i=0; i<args.length; i++) {
+ if(args[i].equals("-time")) {
+ i++;
+ try {
+ duration = Integer.parseInt(args[i]);
+ } catch (Exception ex) { ex.printStackTrace(); }
+ } else if(args[i].equals("-wait")) {
+ waitForKey = true;
+ }
+ }
+ if(waitForKey) {
+ BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
+ System.err.println("Press enter to continue");
+ try {
+ System.err.println(stdin.readLine());
+ } catch (IOException e) { }
+ }
+ org.junit.runner.JUnitCore.main(TestTiledPrintingGearsNewtAWT.class.getName());
+ }
+}