1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.awt.Font; 25 import java.awt.Graphics2D; 26 import java.awt.GraphicsConfiguration; 27 import java.awt.GraphicsEnvironment; 28 import java.awt.Image; 29 import java.awt.Rectangle; 30 import java.awt.RenderingHints; 31 import java.awt.font.FontRenderContext; 32 import java.awt.font.GlyphVector; 33 import java.awt.image.BufferedImage; 34 import java.awt.image.VolatileImage; 35 import java.util.concurrent.ArrayBlockingQueue; 36 import java.util.concurrent.BlockingQueue; 37 import java.util.concurrent.TimeUnit; 38 39 import static java.awt.image.BufferedImage.TYPE_INT_ARGB; 40 41 /** 42 * @test 43 * @key headful 44 * @bug 8158072 7172749 45 */ 46 public final class ClassCastExceptionForInvalidSurface { 47 48 static GraphicsEnvironment ge 49 = GraphicsEnvironment.getLocalGraphicsEnvironment(); 50 51 static GraphicsConfiguration gc 52 = ge.getDefaultScreenDevice().getDefaultConfiguration(); 53 54 static volatile VolatileImage vi = gc.createCompatibleVolatileImage(10, 10); 55 56 static volatile Throwable failed; 57 58 static BlockingQueue<VolatileImage> list = new ArrayBlockingQueue<>(50); 59 60 // Will run the test no more than 15 seconds 61 static long endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(15); 62 63 public static void main(final String[] args) throws InterruptedException { 64 65 // Catch all uncaught exceptions and treat them as test failure 66 Thread.setDefaultUncaughtExceptionHandler((t, e) -> failed = e); 67 68 // Data for rendering 69 BufferedImage bi = new BufferedImage(10, 10, TYPE_INT_ARGB); 70 FontRenderContext frc = new FontRenderContext(null, false, false); 71 Font font = new Font("Serif", Font.PLAIN, 12); 72 GlyphVector gv = font.createGlyphVector(frc, new char[]{'a', '1'}); 73 74 Thread t1 = new Thread(() -> { 75 while (!isComplete()) { 76 vi = gc.createCompatibleVolatileImage(10, 10); 77 if (!list.offer(vi)) { 78 vi.flush(); 79 } 80 } 81 list.forEach(Image::flush); 82 }); 83 Thread t2 = new Thread(() -> { 84 while (!isComplete()) { 85 VolatileImage vi = list.poll(); 86 if (vi != null) { 87 vi.flush(); 88 } 89 } 90 }); 91 92 Thread t3 = new Thread(() -> { 93 while (!isComplete()) { 94 vi.createGraphics().drawImage(bi, 1, 1, null); 95 } 96 }); 97 Thread t4 = new Thread(() -> { 98 while (!isComplete()) { 99 vi.createGraphics().drawGlyphVector(gv, 0, 0); 100 vi.createGraphics().drawOval(0, 0, 10, 10); 101 vi.createGraphics().drawLine(0, 0, 10, 10); 102 vi.createGraphics().drawString("123", 1, 1); 103 vi.createGraphics().draw(new Rectangle(0, 0, 10, 10)); 104 vi.createGraphics().fillOval(0, 0, 10, 10); 105 final Graphics2D graphics = vi.createGraphics(); 106 graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 107 RenderingHints.VALUE_ANTIALIAS_ON); 108 graphics.fillPolygon(new int[] {0, 10, 10, 0}, 109 new int [] {0, 0, 10, 10}, 4); 110 } 111 }); 112 t1.start(); 113 t2.start(); 114 t3.start(); 115 t4.start(); 116 t1.join(); 117 t2.join(); 118 t3.join(); 119 t4.join(); 120 121 if (failed != null) { 122 System.err.println("Test failed"); 123 failed.printStackTrace(); 124 } 125 } 126 127 private static boolean isComplete() { 128 return endtime - System.nanoTime() < 0 || failed != null; 129 } 130 }