1/* 2 * Copyright (c) 2005, 2012, 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 24import java.awt.AWTException; 25import java.awt.Color; 26import java.awt.Component; 27import java.awt.Dimension; 28import java.awt.Frame; 29import java.awt.Graphics; 30import java.awt.Point; 31import java.awt.Rectangle; 32import java.awt.Robot; 33import java.awt.Toolkit; 34import java.awt.image.BufferedImage; 35import java.awt.image.VolatileImage; 36 37/* 38 * 39 * 40 * Tests that the use of shared memory pixmaps isn't broken: 41 * create a VolatileImage, fill it with red color, copy it to the screen 42 * make sure the pixels on the screen are red. 43 * 44 * Note that we force the use of shared memory pixmaps in the shell script. 45 * 46 * @author Dmitri.Trembovetski 47 */ 48 49public class SharedMemoryPixmapsTest { 50 static final int IMAGE_SIZE = 100; 51 static boolean show = false; 52 final Frame testFrame; 53 /** Creates a new instance of SharedMemoryPixmapsTest */ 54 public SharedMemoryPixmapsTest() { 55 testFrame = new Frame("SharedMemoryPixmapsTest"); 56 testFrame.add(new TestComponent()); 57 testFrame.setUndecorated(true); 58 testFrame.setResizable(false); 59 testFrame.pack(); 60 testFrame.setLocationRelativeTo(null); 61 testFrame.setVisible(true); 62 testFrame.toFront(); 63 } 64 65 public static void main(String[] args) { 66 for (String s : args) { 67 if ("-show".equals(s)) { 68 show = true; 69 } else { 70 System.err.println("Usage: SharedMemoryPixmapsTest [-show]"); 71 } 72 } 73 new SharedMemoryPixmapsTest(); 74 } 75 76 private class TestComponent extends Component { 77 VolatileImage vi = null; 78 boolean tested = false; 79 80 void initVI() { 81 int res; 82 if (vi == null) { 83 res = VolatileImage.IMAGE_INCOMPATIBLE; 84 } else { 85 res = vi.validate(getGraphicsConfiguration()); 86 } 87 if (res == VolatileImage.IMAGE_INCOMPATIBLE) { 88 if (vi != null) vi.flush(); 89 vi = createVolatileImage(IMAGE_SIZE, IMAGE_SIZE); 90 vi.validate(getGraphicsConfiguration()); 91 res = VolatileImage.IMAGE_RESTORED; 92 } 93 if (res == VolatileImage.IMAGE_RESTORED) { 94 Graphics vig = vi.getGraphics(); 95 vig.setColor(Color.red); 96 vig.fillRect(0, 0, vi.getWidth(), vi.getHeight()); 97 vig.dispose(); 98 } 99 } 100 101 @Override 102 public synchronized void paint(Graphics g) { 103 do { 104 g.setColor(Color.green); 105 g.fillRect(0, 0, getWidth(), getHeight()); 106 107 initVI(); 108 g.drawImage(vi, 0, 0, null); 109 } while (vi.contentsLost()); 110 111 Toolkit.getDefaultToolkit().sync(); 112 if (!tested) { 113 if (testRendering()) { 114 System.err.println("Test Passed"); 115 } else { 116 System.err.println("Test Failed"); 117 } 118 tested = true; 119 } 120 if (!show) { 121 testFrame.setVisible(false); 122 testFrame.dispose(); 123 } 124 } 125 126 private boolean testRendering() throws RuntimeException { 127 try { 128 Thread.sleep(2000); 129 } catch (InterruptedException ex) {} 130 Robot r = null; 131 try { 132 r = new Robot(); 133 } catch (AWTException ex) { 134 ex.printStackTrace(); 135 throw new RuntimeException("Can't create Robot"); 136 } 137 Point p = getLocationOnScreen(); 138 BufferedImage b = 139 r.createScreenCapture(new Rectangle(p, getPreferredSize())); 140 for (int y = 0; y < b.getHeight(); y++) { 141 for (int x = 0; x < b.getWidth(); x++) { 142 if (b.getRGB(x, y) != Color.red.getRGB()) { 143 System.err.println("Incorrect pixel" + " at " 144 + x + "x" + y + " : " + 145 Integer.toHexString(b.getRGB(x, y))); 146 if (show) { 147 return false; 148 } 149 System.err.println("Test Failed"); 150 System.exit(1); 151 } 152 } 153 } 154 return true; 155 } 156 157 @Override 158 public Dimension getPreferredSize() { 159 return new Dimension(IMAGE_SIZE, IMAGE_SIZE); 160 } 161 } 162 163} 164