Path: blob/master/test/jdk/sun/java2d/marlin/CrashPaintTest.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Color;24import java.awt.Graphics2D;25import java.awt.Paint;26import java.awt.PaintContext;27import java.awt.Rectangle;28import java.awt.RenderingHints;29import java.awt.TexturePaint;30import java.awt.geom.AffineTransform;31import java.awt.geom.Ellipse2D;32import java.awt.geom.Rectangle2D;33import java.awt.image.BufferedImage;34import java.awt.image.ColorModel;35import java.awt.image.Raster;36import java.io.File;37import java.io.IOException;38import java.util.Locale;39import java.util.logging.Handler;40import java.util.logging.LogRecord;41import java.util.logging.Logger;42import javax.imageio.ImageIO;4344/**45* @test46* @bug 814888647* @summary Verifies that Marlin supports reentrant operations (ThreadLocal)48* like in custom Paint or custom Composite49* @run main CrashPaintTest50*/51public class CrashPaintTest {5253static final boolean SAVE_IMAGE = false;5455public static void main(String argv[]) {56Locale.setDefault(Locale.US);5758// initialize j.u.l Looger:59final Logger log = Logger.getLogger("sun.java2d.marlin");60log.addHandler(new Handler() {61@Override62public void publish(LogRecord record) {63Throwable th = record.getThrown();64// detect any Throwable:65if (th != null) {66System.out.println("Test failed:\n" + record.getMessage());67th.printStackTrace(System.out);6869throw new RuntimeException("Test failed: ", th);70}71}7273@Override74public void flush() {75}7677@Override78public void close() throws SecurityException {79}80});8182// enable Marlin logging & internal checks:83System.setProperty("sun.java2d.renderer.log", "true");84System.setProperty("sun.java2d.renderer.useLogger", "true");85System.setProperty("sun.java2d.renderer.doChecks", "true");8687// Force using thread-local storage:88System.setProperty("sun.java2d.renderer.useThreadLocal", "true");89// Force smaller pixelsize to force using array caches:90System.setProperty("sun.java2d.renderer.pixelsize", "256");9192final int width = 300;93final int height = 300;9495final BufferedImage image = new BufferedImage(width, height,96BufferedImage.TYPE_INT_ARGB);9798final Graphics2D g2d = (Graphics2D) image.getGraphics();99try {100g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,101RenderingHints.VALUE_ANTIALIAS_ON);102103g2d.setBackground(Color.WHITE);104g2d.clearRect(0, 0, width, height);105106final Ellipse2D.Double ellipse107= new Ellipse2D.Double(0, 0, width, height);108109final Paint paint = new CustomPaint(100);110111for (int i = 0; i < 20; i++) {112final long start = System.nanoTime();113g2d.setPaint(paint);114g2d.fill(ellipse);115116g2d.setColor(Color.GREEN);117g2d.draw(ellipse);118119final long time = System.nanoTime() - start;120System.out.println("paint: duration= " + (1e-6 * time) + " ms.");121}122123if (SAVE_IMAGE) {124try {125final File file = new File("CrashPaintTest.png");126System.out.println("Writing file: "127+ file.getAbsolutePath());128ImageIO.write(image, "PNG", file);129} catch (IOException ex) {130System.out.println("Writing file failure:");131ex.printStackTrace();132}133}134135// Check image on few pixels:136final Raster raster = image.getData();137138// 170, 175 = blue139checkPixel(raster, 170, 175, Color.BLUE.getRGB());140// 50, 50 = blue141checkPixel(raster, 50, 50, Color.BLUE.getRGB());142143// 190, 110 = pink144checkPixel(raster, 190, 110, Color.PINK.getRGB());145// 280, 210 = pink146checkPixel(raster, 280, 210, Color.PINK.getRGB());147148} finally {149g2d.dispose();150}151}152153private static void checkPixel(final Raster raster,154final int x, final int y,155final int expected) {156157final int[] rgb = (int[]) raster.getDataElements(x, y, null);158159if (rgb[0] != expected) {160throw new IllegalStateException("bad pixel at (" + x + ", " + y161+ ") = " + rgb[0] + " expected: " + expected);162}163}164165private static class CustomPaint extends TexturePaint {166private int size;167168CustomPaint(final int size) {169super(new BufferedImage(size, size,170BufferedImage.TYPE_INT_ARGB),171new Rectangle2D.Double(0, 0, size, size)172);173this.size = size;174}175176@Override177public PaintContext createContext(ColorModel cm,178Rectangle deviceBounds,179Rectangle2D userBounds,180AffineTransform at,181RenderingHints hints) {182183// Fill bufferedImage using184final Graphics2D g2d = (Graphics2D) getImage().getGraphics();185try {186g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,187RenderingHints.VALUE_ANTIALIAS_ON);188g2d.setBackground(Color.PINK);189g2d.clearRect(0, 0, size, size);190191g2d.setColor(Color.BLUE);192g2d.drawRect(0, 0, size, size);193194g2d.fillOval(size / 10, size / 10,195size * 8 / 10, size * 8 / 10);196197} finally {198g2d.dispose();199}200201return super.createContext(cm, deviceBounds, userBounds, at, hints);202}203}204}205206207