--- old/src/java.desktop/share/classes/sun/java2d/marlin/ByteArrayCache.java 2016-02-04 23:02:21.274174412 +0100 +++ new/src/java.desktop/share/classes/sun/java2d/marlin/ByteArrayCache.java 2016-02-04 23:02:21.150174407 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -126,7 +126,7 @@ } if (doChecks) { - check(array, 0, array.length, value); + check(array, fromIndex, toIndex, value); } } @@ -135,9 +135,10 @@ { if (doChecks) { // check zero on full array: - for (int i = fromIndex; i < toIndex; i++) { + for (int i = 0; i < array.length; i++) { if (array[i] != value) { - logException("Invalid array value at " + i + "\n" + logException("Invalid value at: " + i + " = " + array[i] + + " from: " + fromIndex + " to: " + toIndex + "\n" + Arrays.toString(array), new Throwable()); // ensure array is correctly filled: --- old/src/java.desktop/share/classes/sun/java2d/marlin/FloatArrayCache.java 2016-02-04 23:02:21.582174423 +0100 +++ new/src/java.desktop/share/classes/sun/java2d/marlin/FloatArrayCache.java 2016-02-04 23:02:21.454174418 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -127,7 +127,7 @@ } if (doChecks) { - check(array, 0, array.length, value); + check(array, fromIndex, toIndex, value); } } @@ -136,9 +136,10 @@ { if (doChecks) { // check zero on full array: - for (int i = fromIndex; i < toIndex; i++) { + for (int i = 0; i < array.length; i++) { if (array[i] != value) { - logException("Invalid array value at " + i + "\n" + logException("Invalid value at: " + i + " = " + array[i] + + " from: " + fromIndex + " to: " + toIndex + "\n" + Arrays.toString(array), new Throwable()); // ensure array is correctly filled: --- old/src/java.desktop/share/classes/sun/java2d/marlin/IntArrayCache.java 2016-02-04 23:02:21.894174435 +0100 +++ new/src/java.desktop/share/classes/sun/java2d/marlin/IntArrayCache.java 2016-02-04 23:02:21.766174430 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -126,7 +126,7 @@ } if (doChecks) { - check(array, 0, array.length, value); + check(array, fromIndex, toIndex, value); } } @@ -135,9 +135,10 @@ { if (doChecks) { // check zero on full array: - for (int i = fromIndex; i < toIndex; i++) { + for (int i = 0; i < array.length; i++) { if (array[i] != value) { - logException("Invalid array value at " + i + "\n" + logException("Invalid value at: " + i + " = " + array[i] + + " from: " + fromIndex + " to: " + toIndex + "\n" + Arrays.toString(array), new Throwable()); // ensure array is correctly filled: --- old/src/java.desktop/share/classes/sun/java2d/marlin/MarlinCache.java 2016-02-04 23:02:22.198174446 +0100 +++ new/src/java.desktop/share/classes/sun/java2d/marlin/MarlinCache.java 2016-02-04 23:02:22.066174441 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -590,8 +590,8 @@ alphaRow[to + 1] = 0; } if (doChecks) { - IntArrayCache.check(blkFlags, 0, blkFlags.length, 0); - IntArrayCache.check(alphaRow, 0, alphaRow.length, 0); + IntArrayCache.check(blkFlags, blkW, blkE, 0); + IntArrayCache.check(alphaRow, from, px1 - bboxX0, 0); } if (doMonitors) { --- old/src/java.desktop/share/classes/sun/java2d/marlin/MarlinRenderingEngine.java 2016-02-04 23:02:22.518174458 +0100 +++ new/src/java.desktop/share/classes/sun/java2d/marlin/MarlinRenderingEngine.java 2016-02-04 23:02:22.394174453 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -894,17 +894,16 @@ // Per-thread RendererContext private static final ThreadLocal rdrCtxThreadLocal; - // RendererContext queue when ThreadLocal is disabled - private static final ConcurrentLinkedQueue rdrCtxQueue; + // RendererContext queue when + // ThreadLocal is disabled or for child contexts (reentrance) + private static final ConcurrentLinkedQueue rdrCtxQueue + = new ConcurrentLinkedQueue(); // Static initializer to use TL or CLQ mode static { - // CLQ mode by default: useThreadLocal = MarlinProperties.isUseThreadLocal(); rdrCtxThreadLocal = (useThreadLocal) ? new ThreadLocal() : null; - rdrCtxQueue = (!useThreadLocal) ? new ConcurrentLinkedQueue() - : null; // Soft reference by default: String refType = AccessController.doPrivileged( @@ -1026,20 +1025,28 @@ @SuppressWarnings({"unchecked"}) static RendererContext getRendererContext() { RendererContext rdrCtx = null; - final Object ref = (useThreadLocal) ? rdrCtxThreadLocal.get() - : rdrCtxQueue.poll(); - if (ref != null) { - // resolve reference: - rdrCtx = (REF_TYPE == REF_HARD) ? ((RendererContext) ref) - : ((Reference) ref).get(); - } - // create a new RendererContext if none is available - if (rdrCtx == null) { - rdrCtx = RendererContext.createContext(); - if (useThreadLocal) { + if (useThreadLocal) { + final Object ref = rdrCtxThreadLocal.get(); + if (ref != null) { + rdrCtx = (REF_TYPE == REF_HARD) ? ((RendererContext) ref) + : ((Reference) ref).get(); + } + if (rdrCtx == null) { + // create a new RendererContext (TL) if none is available + rdrCtx = RendererContext.createContext(false); // update thread local reference: rdrCtxThreadLocal.set(rdrCtx.reference); } + // Check reentrance: + if (rdrCtx.usedTL) { + // get or create another RendererContext: + rdrCtx = getOrCreateContextFromQueue(); + } else { + // TL mode: set used flag: + rdrCtx.usedTL = true; + } + } else { + rdrCtx = getOrCreateContextFromQueue(); } if (doMonitors) { RendererContext.stats.mon_pre_getAATileGenerator.start(); @@ -1047,6 +1054,21 @@ return rdrCtx; } + @SuppressWarnings({"unchecked"}) + private static RendererContext getOrCreateContextFromQueue() { + RendererContext rdrCtx = null; + final Object ref = rdrCtxQueue.poll(); + if (ref != null) { + rdrCtx = (REF_TYPE == REF_HARD) ? ((RendererContext) ref) + : ((Reference) ref).get(); + } + if (rdrCtx == null) { + // create a new RendererContext (QUEUE) if none is available + rdrCtx = RendererContext.createContext(true); + } + return rdrCtx; + } + /** * Reset and return the given RendererContext instance for reuse * @param rdrCtx RendererContext instance @@ -1057,8 +1079,11 @@ if (doMonitors) { RendererContext.stats.mon_pre_getAATileGenerator.stop(); } - if (!useThreadLocal) { + if (!useThreadLocal || rdrCtx.storageQueue) { rdrCtxQueue.offer(rdrCtx.reference); + } else { + // TL mode: unset used flag: + rdrCtx.usedTL = false; } } } --- old/src/java.desktop/share/classes/sun/java2d/marlin/RendererContext.java 2016-02-04 23:02:22.838174470 +0100 +++ new/src/java.desktop/share/classes/sun/java2d/marlin/RendererContext.java 2016-02-04 23:02:22.710174465 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -50,11 +50,14 @@ /** * Create a new renderer context * + * @param storageQueue true = queue storage; false = thread-Local storage * @return new RendererContext instance */ - static RendererContext createContext() { + static RendererContext createContext(final boolean storageQueue) { final RendererContext newCtx = new RendererContext("ctx" - + Integer.toString(contextCount.getAndIncrement())); + + Integer.toString(contextCount.getAndIncrement()), + storageQueue); + if (RendererContext.stats != null) { RendererContext.stats.allContexts.add(newCtx); } @@ -63,6 +66,13 @@ // context name (debugging purposes) final String name; + // true = queue storage; false = thread-Local storage + final boolean storageQueue; + /* + * Used flag indicating this thread-Local context is already used + * (used to detect reentrance) + */ + boolean usedTL = false; /* * Reference to this instance (hard, soft or weak). * @see MarlinRenderingEngine#REF_TYPE @@ -101,14 +111,16 @@ /** * Constructor * - * @param name + * @param name context name (debugging) + * @param storageQueue true = queue storage; false = thread-Local storage */ - RendererContext(final String name) { + RendererContext(final String name, final boolean storageQueue) { if (logCreateContext) { MarlinUtils.logInfo("new RendererContext = " + name); } this.name = name; + this.storageQueue = storageQueue; // NormalizingPathIterator instances: nPCPathIterator = new NormalizingPathIterator.NearestPixelCenter(float6); --- old/src/java.desktop/share/classes/sun/java2d/marlin/Version.java 2016-02-04 23:02:23.126174481 +0100 +++ new/src/java.desktop/share/classes/sun/java2d/marlin/Version.java 2016-02-04 23:02:23.006174476 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,7 +27,7 @@ public final class Version { - private static final String version = "marlin-0.7.3-Unsafe-OpenJDK"; + private static final String version = "marlin-0.7.3.2-Unsafe-OpenJDK"; public static String getVersion() { return version; --- old/src/java.desktop/share/classes/sun/java2d/pipe/AAShapePipe.java 2016-02-04 23:02:23.418174492 +0100 +++ new/src/java.desktop/share/classes/sun/java2d/pipe/AAShapePipe.java 2016-02-04 23:02:23.290174487 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -143,10 +143,18 @@ } public void renderTiles(SunGraphics2D sg, Shape s, - AATileGenerator aatg, int abox[], TileState ts) + final AATileGenerator aatg, + final int[] abox, final TileState ts) { Object context = null; try { + // defensive copy of int[] abox as local variables: + final int bx = abox[0]; + final int by = abox[1]; + final int bw = abox[2]; + final int bh = abox[3]; + + // reentrance: outpipe may also use AAShapePipe: context = outpipe.startSequence(sg, s, ts.computeDevBox(abox), abox); @@ -158,16 +166,15 @@ final byte[] alpha = ts.getAlphaTile(tw * th); byte[] atile; - for (int y = abox[1]; y < abox[3]; y += th) { - int h = Math.min(th, abox[3] - y); + for (int y = by; y < bh; y += th) { + final int h = Math.min(th, bh - y); + + for (int x = bx; x < bw; x += tw) { + final int w = Math.min(tw, bw - x); - for (int x = abox[0]; x < abox[2]; x += tw) { - int w = Math.min(tw, abox[2] - x); + final int a = aatg.getTypicalAlpha(); - int a = aatg.getTypicalAlpha(); - if (a == 0x00 || - outpipe.needTile(context, x, y, w, h) == false) - { + if (a == 0x00 || !outpipe.needTile(context, x, y, w, h)) { aatg.nextTile(); outpipe.skipTile(context, x, y); continue; @@ -180,8 +187,8 @@ aatg.getAlpha(alpha, 0, tw); } - outpipe.renderPathTile(context, atile, 0, tw, - x, y, w, h); + // TODO: check reentrance issue ? on byte[] alpha ? + outpipe.renderPathTile(context, atile, 0, tw, x, y, w, h); } } } finally { --- /dev/null 2016-02-04 21:09:39.087173009 +0100 +++ new/test/sun/java2d/marlin/CrashPaintTest.java 2016-02-04 23:02:23.606174499 +0100 @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.PaintContext; +import java.awt.Rectangle; +import java.awt.RenderingHints; +import java.awt.TexturePaint; +import java.awt.geom.AffineTransform; +import java.awt.geom.Ellipse2D; +import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.io.File; +import java.io.IOException; +import java.util.Locale; +import java.util.logging.Handler; +import java.util.logging.LogRecord; +import java.util.logging.Logger; +import javax.imageio.ImageIO; + +/** + * @test + * @bug 8148886 + * @summary Verifies that Marlin supports reentrant operations (ThreadLocal) + * like in custom Paint or custom Composite + * @run main CrashPaintTest + */ +public class CrashPaintTest { + + static final boolean SAVE_IMAGE = false; + + public static void main(String argv[]) { + Locale.setDefault(Locale.US); + + // initialize j.u.l Looger: + final Logger log = Logger.getLogger("sun.java2d.marlin"); + log.addHandler(new Handler() { + @Override + public void publish(LogRecord record) { + Throwable th = record.getThrown(); + // detect potential Throwable thrown by XxxArrayCache.check(): + if (th != null && th.getClass() == Throwable.class) { + StackTraceElement[] stackElements = th.getStackTrace(); + + for (int i = 0; i < stackElements.length; i++) { + StackTraceElement e = stackElements[i]; + + if (e.getClassName().startsWith("sun.java2d.marlin") + && e.getClassName().contains("ArrayCache") + && "check".equals(e.getMethodName())) { + System.out.println("Test failed:\n" + + record.getMessage()); + th.printStackTrace(System.out); + + throw new RuntimeException("Test failed: ", th); + } + } + } + } + + @Override + public void flush() { + } + + @Override + public void close() throws SecurityException { + } + }); + + // enable Marlin logging & internal checks: + System.setProperty("sun.java2d.renderer.log", "true"); + System.setProperty("sun.java2d.renderer.useLogger", "true"); + System.setProperty("sun.java2d.renderer.doChecks", "true"); + + // Force using thread-local storage: + System.setProperty("sun.java2d.renderer.useThreadLocal", "true"); + // Force smaller pixelsize to force using array caches: + System.setProperty("sun.java2d.renderer.pixelsize", "256"); + + final int width = 300; + final int height = 300; + + final BufferedImage image = new BufferedImage(width, height, + BufferedImage.TYPE_INT_ARGB); + + final Graphics2D g2d = (Graphics2D) image.getGraphics(); + try { + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + + g2d.setBackground(Color.WHITE); + g2d.clearRect(0, 0, width, height); + + final Ellipse2D.Double ellipse + = new Ellipse2D.Double(0, 0, width, height); + + final long start = System.nanoTime(); + + g2d.setPaint(new CustomPaint(100)); + g2d.fill(ellipse); + + g2d.setColor(Color.GREEN); + g2d.setStroke(new BasicStroke(1f)); + g2d.draw(ellipse); + + final long time = System.nanoTime() - start; + + System.out.println("paint: duration= " + (1e-6 * time) + " ms."); + + if (SAVE_IMAGE) { + try { + final File file = new File("CrashPaintTest.png"); + System.out.println("Writing file: " + + file.getAbsolutePath()); + ImageIO.write(image, "PNG", file); + } catch (IOException ex) { + System.out.println("Writing file failure:"); + ex.printStackTrace(); + } + } + } finally { + g2d.dispose(); + } + } + + private static class CustomPaint extends TexturePaint { + private int size; + + CustomPaint(final int size) { + super(new BufferedImage(size, size, + BufferedImage.TYPE_INT_ARGB), + new Rectangle2D.Double(0, 0, size, size) + ); + this.size = size; + } + + @Override + public PaintContext createContext(ColorModel cm, + Rectangle deviceBounds, + Rectangle2D userBounds, + AffineTransform at, + RenderingHints hints) { + + // Fill bufferedImage using + final Graphics2D g2d = (Graphics2D) getImage().getGraphics(); + try { + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setBackground(Color.PINK); + g2d.clearRect(0, 0, size, size); + + g2d.setColor(Color.BLUE); + g2d.drawRect(0, 0, size, size); + + g2d.fillOval(size / 10, size / 10, + size * 8 / 10, size * 8 / 10); + + } finally { + g2d.dispose(); + } + + return super.createContext(cm, deviceBounds, userBounds, at, hints); + } + } +}