changeset 5895:b6b8fc7d6c18 8.0-b119

RT-28195: Printing: printing of a canvas doesn't start Reviewed-by: flar, kcr
author prr
date Tue, 03 Dec 2013 15:32:09 -0800
parents 7453c535525d
children 1428f17dbbf5 89ee519f5c1a a4b45faa34a2
files modules/graphics/src/main/java/com/sun/javafx/sg/prism/NGCanvas.java modules/graphics/src/main/java/com/sun/prism/ResourceFactory.java modules/graphics/src/main/java/com/sun/prism/d3d/D3DResourceFactory.java modules/graphics/src/main/java/com/sun/prism/es2/ES2ResourceFactory.java modules/graphics/src/main/java/com/sun/prism/j2d/J2DRTTexture.java modules/graphics/src/main/java/com/sun/prism/j2d/J2DResourceFactory.java modules/graphics/src/main/java/com/sun/prism/null3d/DummyResourceFactory.java modules/graphics/src/main/java/com/sun/prism/sw/SWRTTexture.java modules/graphics/src/main/java/com/sun/prism/sw/SWResourceFactory.java modules/graphics/src/test/java/com/sun/javafx/sg/prism/TestGraphics.java
diffstat 10 files changed, 101 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/modules/graphics/src/main/java/com/sun/javafx/sg/prism/NGCanvas.java	Tue Dec 03 18:18:31 2013 -0500
+++ b/modules/graphics/src/main/java/com/sun/javafx/sg/prism/NGCanvas.java	Tue Dec 03 15:32:09 2013 -0800
@@ -28,6 +28,8 @@
 import javafx.geometry.VPos;
 import javafx.scene.text.Font;
 import java.nio.IntBuffer;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.FutureTask;
 import java.util.LinkedList;
 import com.sun.javafx.font.PGFont;
 import com.sun.javafx.geom.Arc2D;
@@ -43,10 +45,13 @@
 import com.sun.javafx.geom.transform.BaseTransform;
 import com.sun.javafx.geom.transform.NoninvertibleTransformException;
 import com.sun.javafx.text.PrismTextLayout;
+import com.sun.javafx.tk.RenderJob;
+import com.sun.javafx.tk.Toolkit;
 import com.sun.prism.BasicStroke;
 import com.sun.prism.CompositeMode;
 import com.sun.prism.Graphics;
 import com.sun.prism.Image;
+import com.sun.prism.PrinterGraphics;
 import com.sun.prism.RTTexture;
 import com.sun.prism.ResourceFactory;
 import com.sun.prism.Texture;
@@ -196,7 +201,7 @@
                 if (oldtex != null) {
                     if (init_type == InitType.PRESERVE_UPPER_LEFT) {
                         g.setCompositeMode(CompositeMode.SRC);
-                        if (oldtex.createGraphics() == null) {
+                        if (oldtex.isSurfaceLost()) {
                             if (savedPixelData != null) {
                                 savedPixelData.restore(g, cw, ch);
                             }
@@ -242,12 +247,12 @@
             return false;
         }
 
-        private void save(Graphics g, int tw, int th) {
+        private void save(int tw, int th) {
             if (tex.isVolatile()) {
                 if (savedPixelData == null) {
-                    savedPixelData = new PixelData(g, tw, th);
+                    savedPixelData = new PixelData(tw, th);
                 }
-                savedPixelData.save(g, tex);
+                savedPixelData.save(tex);
             }
         }
     }
@@ -259,13 +264,13 @@
         private boolean validPixels = false;
         private int cw, ch;
 
-        private PixelData(Graphics g, int cw, int ch) {
+        private PixelData(int cw, int ch) {
             this.cw = cw;
             this.ch = ch;
             pixels = IntBuffer.allocate(cw*ch);
         }
 
-        private void save(Graphics g, RTTexture tex) {
+        private void save(RTTexture tex) {
             int tw = tex.getContentWidth();
             int th = tex.getContentHeight();
             if (cw < tw || ch < th) {
@@ -288,7 +293,6 @@
                                           Texture.WrapMode.CLAMP_TO_EDGE);
                 g.drawTexture(tempTex, 0, 0, tw, th);
                 tempTex.dispose();
-                validPixels = false;
             }
         }
     }
@@ -513,8 +517,61 @@
                             TEMP_COORDS[2], TEMP_COORDS[3]);
     }
 
+    private static void runOnRenderThread(final Runnable r) {
+        // We really need a standard mechanism to detect the render thread !
+        if (Thread.currentThread().getName().startsWith("QuantumRenderer")) {
+            r.run();
+        } else {
+            FutureTask<Void> f = new FutureTask<Void>(r, null);
+            Toolkit.getToolkit().addRenderJob(new RenderJob(f));
+            try {
+                // block until job is complete
+                f.get();
+            } catch (ExecutionException ex) {
+                throw new AssertionError(ex);
+            } catch (InterruptedException ex) {
+                // ignore; recovery is impossible
+            }
+        }
+    }
+
+    private boolean printedCanvas(Graphics g) {
+       final RTTexture localTex = cv.tex;
+       if (!(g instanceof PrinterGraphics) || localTex  == null) {
+          return false;
+        }
+        ResourceFactory factory = g.getResourceFactory();
+        boolean isCompatTex = factory.isCompatibleTexture(localTex);
+        if (isCompatTex) {
+            return false;
+        }
+
+        final int tw = localTex.getContentWidth();
+        final int th = localTex.getContentHeight();
+        final RTTexture tmpTex =
+              factory.createRTTexture(tw, th, WrapMode.CLAMP_TO_ZERO);
+        final Graphics texg = tmpTex.createGraphics();
+        texg.setCompositeMode(CompositeMode.SRC);
+        if (cv.savedPixelData == null) {
+            final PixelData pd = new PixelData(cw, ch);
+            runOnRenderThread(new Runnable() {
+                public void run() {
+                  pd.save(localTex);
+                  pd.restore(texg, tw, th);
+                }
+            });
+        } else {
+            cv.savedPixelData.restore(texg, tw, th);
+        }
+        g.drawTexture(tmpTex, 0, 0, tw, th);
+        tmpTex.unlock();
+        tmpTex.dispose();
+        return true;        
+    }
+
     @Override
     protected void renderContent(Graphics g) {
+        if (printedCanvas(g)) return;
         initCanvas(g);
         if (cv.tex != null) {
             if (thebuf != null) {
@@ -528,7 +585,7 @@
                           0, 0, dw, dh,
                           0, 0, tw, th);
             // Must save the pixels every frame if RTT is volatile.
-            cv.save(g, tw, th);
+            cv.save(tw, th);
         }
         this.temp.g = this.clip.g = this.cv.g = null;
     }
--- a/modules/graphics/src/main/java/com/sun/prism/ResourceFactory.java	Tue Dec 03 18:18:31 2013 -0500
+++ b/modules/graphics/src/main/java/com/sun/prism/ResourceFactory.java	Tue Dec 03 15:32:09 2013 -0800
@@ -156,6 +156,14 @@
     public Texture createFloatTexture(int width, int height);
     public RTTexture createRTTexture(int width, int height, Texture.WrapMode wrapMode);
     public RTTexture createRTTexture(int width, int height, Texture.WrapMode wrapMode, boolean antiAliasing);
+    
+    /**
+     * A Texture may have been obtained from a different resource factory.
+     * @param tex the texture to check.
+     * @return whether this texture is compatible.
+     */
+    public boolean isCompatibleTexture(Texture tex);
+
     public Presentable createPresentable(PresentableState pState);
     public VertexBuffer createVertexBuffer(int maxQuads);
 
--- a/modules/graphics/src/main/java/com/sun/prism/d3d/D3DResourceFactory.java	Tue Dec 03 18:18:31 2013 -0500
+++ b/modules/graphics/src/main/java/com/sun/prism/d3d/D3DResourceFactory.java	Tue Dec 03 15:32:09 2013 -0800
@@ -123,6 +123,11 @@
     }
 
     @Override
+    public boolean isCompatibleTexture(Texture tex) {
+        return tex instanceof D3DTexture;
+    }
+  
+    @Override
     public D3DTexture createTexture(PixelFormat format, Usage usagehint,
                                     WrapMode wrapMode, int w, int h)
     {
--- a/modules/graphics/src/main/java/com/sun/prism/es2/ES2ResourceFactory.java	Tue Dec 03 18:18:31 2013 -0500
+++ b/modules/graphics/src/main/java/com/sun/prism/es2/ES2ResourceFactory.java	Tue Dec 03 15:32:09 2013 -0800
@@ -73,6 +73,11 @@
         return new ES2SwapChain(context, pState);
     }
 
+    @Override
+    public boolean isCompatibleTexture(Texture tex) {
+        return tex instanceof ES2Texture;
+    }
+
     public Texture createTexture(PixelFormat formatHint,
                                  Usage usageHint,
                                  WrapMode wrapMode,
--- a/modules/graphics/src/main/java/com/sun/prism/j2d/J2DRTTexture.java	Tue Dec 03 18:18:31 2013 -0500
+++ b/modules/graphics/src/main/java/com/sun/prism/j2d/J2DRTTexture.java	Tue Dec 03 15:32:09 2013 -0800
@@ -91,6 +91,7 @@
                 ((ByteBuffer)pixels).put((byte)a);
             }
         }
+        pixels.rewind();
         return true;
     }
 
--- a/modules/graphics/src/main/java/com/sun/prism/j2d/J2DResourceFactory.java	Tue Dec 03 18:18:31 2013 -0500
+++ b/modules/graphics/src/main/java/com/sun/prism/j2d/J2DResourceFactory.java	Tue Dec 03 15:32:09 2013 -0800
@@ -131,6 +131,11 @@
         return tex;
     }
 
+    @Override
+    public boolean isCompatibleTexture(Texture tex) {
+        return tex instanceof J2DTexture;
+    }
+    
     public int getMaximumTextureSize() {
         return Integer.MAX_VALUE;
     }
--- a/modules/graphics/src/main/java/com/sun/prism/null3d/DummyResourceFactory.java	Tue Dec 03 18:18:31 2013 -0500
+++ b/modules/graphics/src/main/java/com/sun/prism/null3d/DummyResourceFactory.java	Tue Dec 03 15:32:09 2013 -0800
@@ -78,6 +78,11 @@
     }
 
     @Override
+    public boolean isCompatibleTexture(Texture tex) {
+        return tex instanceof DummyTexture;
+    }
+  
+    @Override
     public RTTexture createRTTexture(int width, int height, WrapMode wrapMode) {
         return createRTTexture(width, height, wrapMode, false);
     }
--- a/modules/graphics/src/main/java/com/sun/prism/sw/SWRTTexture.java	Tue Dec 03 18:18:31 2013 -0500
+++ b/modules/graphics/src/main/java/com/sun/prism/sw/SWRTTexture.java	Tue Dec 03 15:32:09 2013 -0800
@@ -104,6 +104,7 @@
         } else {
             return false;
         }
+        pixels.rewind();
         return true;
     }
 
--- a/modules/graphics/src/main/java/com/sun/prism/sw/SWResourceFactory.java	Tue Dec 03 18:18:31 2013 -0500
+++ b/modules/graphics/src/main/java/com/sun/prism/sw/SWResourceFactory.java	Tue Dec 03 15:32:09 2013 -0800
@@ -113,6 +113,11 @@
         return h;
     }
 
+    @Override
+    public boolean isCompatibleTexture(Texture tex) {
+        return tex instanceof SWTexture;
+    }
+
     @Override public RTTexture createRTTexture(int width, int height, WrapMode wrapMode, boolean antiAliasing) {
         return createRTTexture(width, height, wrapMode);
     }
--- a/modules/graphics/src/test/java/com/sun/javafx/sg/prism/TestGraphics.java	Tue Dec 03 18:18:31 2013 -0500
+++ b/modules/graphics/src/test/java/com/sun/javafx/sg/prism/TestGraphics.java	Tue Dec 03 15:32:09 2013 -0800
@@ -179,6 +179,7 @@
         @Override public Texture createTexture(Image image, Texture.Usage usageHint, Texture.WrapMode wrapMode) { return null; }
         @Override public Texture createTexture(PixelFormat formatHint, Texture.Usage usageHint, Texture.WrapMode wrapMode, int w, int h) { return null; }
         @Override public Texture createTexture(MediaFrame frame) { return null; }
+        @Override public boolean isCompatibleTexture(Texture tex) { return true; }
         @Override public Texture getCachedTexture(Image image, WrapMode wrapMode) { return null; }
         @Override public boolean isFormatSupported(PixelFormat format) { return false; }
         @Override public int getMaximumTextureSize() { return 0; }