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  * @bug 8158072 7172749
  44  */
  45 public final class ClassCastExceptionForInvalidSurface {
  46 
  47     static GraphicsEnvironment ge
  48             = GraphicsEnvironment.getLocalGraphicsEnvironment();
  49 
  50     static GraphicsConfiguration gc
  51             = ge.getDefaultScreenDevice().getDefaultConfiguration();
  52 
  53     static volatile VolatileImage vi = gc.createCompatibleVolatileImage(10, 10);
  54 
  55     static volatile Throwable failed;
  56 
  57     static BlockingQueue<VolatileImage> list = new ArrayBlockingQueue<>(50);
  58 
  59     // Will run the test no more than 15 seconds
  60     static long endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(15);
  61 
  62     public static void main(final String[] args) throws InterruptedException {
  63 
  64         // Catch all uncaught exceptions and treat them as test failure
  65         Thread.setDefaultUncaughtExceptionHandler((t, e) -> failed = e);
  66 
  67         // Data for rendering
  68         BufferedImage bi = new BufferedImage(10, 10, TYPE_INT_ARGB);
  69         FontRenderContext frc = new FontRenderContext(null, false, false);
  70         Font font = new Font("Serif", Font.PLAIN, 12);
  71         GlyphVector gv = font.createGlyphVector(frc, new char[]{'a', '1'});
  72 
  73         Thread t1 = new Thread(() -> {
  74             while (!isComplete()) {
  75                 vi = gc.createCompatibleVolatileImage(10, 10);
  76                 if (!list.offer(vi)) {
  77                     vi.flush();
  78                 }
  79             }
  80             list.forEach(Image::flush);
  81         });
  82         Thread t2 = new Thread(() -> {
  83             while (!isComplete()) {
  84                 VolatileImage vi = list.poll();
  85                 if (vi != null) {
  86                     vi.flush();
  87                 }
  88             }
  89         });
  90 
  91         Thread t3 = new Thread(() -> {
  92             while (!isComplete()) {
  93                 vi.createGraphics().drawImage(bi, 1, 1, null);
  94             }
  95         });
  96         Thread t4 = new Thread(() -> {
  97             while (!isComplete()) {
  98                 vi.createGraphics().drawGlyphVector(gv, 0, 0);
  99                 vi.createGraphics().drawOval(0, 0, 10, 10);
 100                 vi.createGraphics().drawLine(0, 0, 10, 10);
 101                 vi.createGraphics().drawString("123", 1, 1);
 102                 vi.createGraphics().draw(new Rectangle(0, 0, 10, 10));
 103                 vi.createGraphics().fillOval(0, 0, 10, 10);
 104                 final Graphics2D graphics = vi.createGraphics();
 105                 graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 106                                           RenderingHints.VALUE_ANTIALIAS_ON);
 107                 graphics.fillPolygon(new int[] {0, 10, 10, 0},
 108                                      new int [] {0, 0, 10, 10}, 4);
 109             }
 110         });
 111         t1.start();
 112         t2.start();
 113         t3.start();
 114         t4.start();
 115         t1.join();
 116         t2.join();
 117         t3.join();
 118         t4.join();
 119 
 120         if (failed != null) {
 121             System.err.println("Test failed");
 122             failed.printStackTrace();
 123         }
 124     }
 125 
 126     private static boolean isComplete() {
 127         return endtime - System.nanoTime() < 0 || failed != null;
 128     }
 129 }