1 /* 2 * Copyright (c) 2007, 2008, 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 /* 25 * @test 26 * @bug 6648018 27 * @summary Tests that we don't run into infinite validation loop when copying 28 a VolatileImage to the screen 29 * @author Dmitri.Trembovetski@sun.com: area=Graphics 30 * @run main/othervm InfiniteValidationLoopTest 31 * @run main/othervm -Dsun.java2d.d3d=false InfiniteValidationLoopTest 32 */ 33 import java.awt.Color; 34 import java.awt.Frame; 35 import java.awt.Graphics; 36 import java.awt.GraphicsConfiguration; 37 import static java.awt.image.VolatileImage.*; 38 import java.awt.image.VolatileImage; 39 import java.util.concurrent.CountDownLatch; 40 41 public class InfiniteValidationLoopTest extends Frame { 42 private static volatile boolean failed = false; 43 private static final int LOOP_THRESHOLD = 50; 44 private static volatile CountDownLatch latch; 45 private VolatileImage vi; 46 47 public InfiniteValidationLoopTest() { 48 super("InfiniteValidationLoopTest"); 49 } 50 51 @Override 52 public void paint(Graphics g) { 53 try { 54 runTest(g); 55 } finally { 56 latch.countDown(); 57 } 58 } 59 60 private void runTest(Graphics g) { 61 int status = IMAGE_OK; 62 int count1 = 0; 63 do { 64 GraphicsConfiguration gc = getGraphicsConfiguration(); 65 int count2 = 0; 66 while (vi == null || (status = vi.validate(gc)) != IMAGE_OK) { 67 if (++count2 > LOOP_THRESHOLD) { 68 System.err.println("Infinite loop detected: count2="+count2); 69 failed = true; 70 return; 71 } 72 if (vi == null || status == IMAGE_INCOMPATIBLE) { 73 if (vi != null) { vi.flush(); vi = null; } 74 vi = gc.createCompatibleVolatileImage(100, 100); 75 continue; 76 } 77 if (status == IMAGE_RESTORED) { 78 Graphics gg = vi.getGraphics(); 79 gg.setColor(Color.green); 80 gg.fillRect(0, 0, vi.getWidth(), vi.getHeight()); 81 break; 82 } 83 } 84 g.drawImage(vi, getInsets().left, getInsets().top, null); 85 if (++count1 > LOOP_THRESHOLD) { 86 System.err.println("Infinite loop detected: count1="+count1); 87 failed = true; 88 return; 89 } 90 } while (vi.contentsLost()); 91 } 92 93 public static void main(String[] args) { 94 latch = new CountDownLatch(1); 95 InfiniteValidationLoopTest t1 = new InfiniteValidationLoopTest(); 96 t1.pack(); 97 t1.setSize(200, 200); 98 t1.setVisible(true); 99 try { latch.await(); } catch (InterruptedException ex) {} 100 t1.dispose(); 101 102 latch = new CountDownLatch(1); 103 t1 = new InfiniteValidationLoopTest(); 104 t1.pack(); 105 t1.setSize(50, 50); 106 t1.setVisible(true); 107 try { latch.await(); } catch (InterruptedException ex) {} 108 t1.dispose(); 109 110 if (failed) { 111 throw new 112 RuntimeException("Failed: infinite validattion loop detected"); 113 } 114 System.out.println("Test PASSED"); 115 } 116 }