1 /* 2 * Copyright (c) 2007, 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 /* 25 * @test 26 * @key headful 27 * @bug 6648018 6652662 28 * @summary Verifies that rendering to a cached onscreen Graphics works 29 * @author Dmitri.Trembovetski@sun.com: area=Graphics 30 * @run main/othervm RenderingToCachedGraphicsTest 31 * @run main/othervm -Dsun.java2d.d3d=false RenderingToCachedGraphicsTest 32 */ 33 import java.awt.Canvas; 34 import java.awt.Color; 35 import java.awt.Frame; 36 import java.awt.Graphics; 37 import java.awt.GraphicsEnvironment; 38 import java.awt.Point; 39 import java.awt.Rectangle; 40 import java.awt.Robot; 41 import java.awt.Toolkit; 42 import java.awt.event.WindowAdapter; 43 import java.awt.event.WindowEvent; 44 import java.awt.image.BufferStrategy; 45 import java.awt.image.BufferedImage; 46 import static java.awt.image.VolatileImage.*; 47 import java.awt.image.VolatileImage; 48 import java.io.File; 49 import java.util.concurrent.CountDownLatch; 50 import javax.imageio.ImageIO; 51 52 public class RenderingToCachedGraphicsTest extends Frame { 53 private static volatile boolean failed = false; 54 private static volatile CountDownLatch latch; 55 private Graphics cachedGraphics; 56 private Canvas renderCanvas; 57 58 public RenderingToCachedGraphicsTest() { 59 super("Test starts in 2 seconds"); 60 renderCanvas = new Canvas() { 61 @Override 62 public void paint(Graphics g) { 63 if (getWidth() < 100 || getHeight() < 100) { 64 repaint(); 65 return; 66 } 67 // wait for a bit so that Vista's Window manager's animation 68 // effects on window's appearance are completed (6652662) 69 try { Thread.sleep(2000); } catch (InterruptedException ex) {} 70 71 try { 72 runTest(); 73 } catch (Throwable t) { 74 failed = true; 75 } finally { 76 latch.countDown(); 77 } 78 } 79 @Override 80 public void update(Graphics g) {} 81 }; 82 83 add("Center", renderCanvas); 84 } 85 86 private void runTest() { 87 // this will cause screen update manager to dump the accelerated surface 88 // for this canvas 89 renderCanvas.createBufferStrategy(2); 90 BufferStrategy bs = renderCanvas.getBufferStrategy(); 91 do { 92 Graphics bsg = bs.getDrawGraphics(); 93 bsg.setColor(Color.blue); 94 bsg.fillRect(0, 0, 95 renderCanvas.getWidth(), renderCanvas.getHeight()); 96 } while (bs.contentsLost() || bs.contentsRestored()); 97 98 // grab the "unaccelerated" onscreen surface 99 cachedGraphics = renderCanvas.getGraphics(); 100 cachedGraphics.setColor(Color.red); 101 cachedGraphics.fillRect(0, 0, getWidth(), getHeight()); 102 103 bs.dispose(); 104 bs = null; 105 // now the update manager should be able to accelerate onscreen 106 // rendering to it again 107 108 cachedGraphics.setColor(Color.green); 109 // this causes restoration of the new accelerated onscreen surface 110 // (it is created in "lost" state) 111 cachedGraphics.fillRect(0, 0, 112 renderCanvas.getWidth(), 113 renderCanvas.getHeight()); 114 Toolkit.getDefaultToolkit().sync(); 115 // and now we should be able to render to it 116 cachedGraphics.fillRect(0, 0, 117 renderCanvas.getWidth(), 118 renderCanvas.getHeight()); 119 Toolkit.getDefaultToolkit().sync(); 120 121 Robot robot = null; 122 try { 123 robot = new Robot(); 124 } catch (Exception e) { 125 e.printStackTrace(); 126 failed = true; 127 return; 128 } 129 130 Point p = renderCanvas.getLocationOnScreen(); 131 Rectangle r = new Rectangle(p.x, p.y, 132 renderCanvas.getWidth(), 133 renderCanvas.getHeight()); 134 BufferedImage bi = robot.createScreenCapture(r); 135 for (int y = 0; y < bi.getHeight(); y++) { 136 for (int x = 0; x < bi.getWidth(); x++) { 137 if (bi.getRGB(x, y) != Color.green.getRGB()) { 138 System.err.println("Colors mismatch!"); 139 String name = "RenderingToCachedGraphicsTest.png"; 140 try { 141 ImageIO.write(bi, "png", new File(name)); 142 System.err.println("Dumped grabbed image to: "+name); 143 } catch (Exception e) {} 144 failed = true; 145 return; 146 } 147 } 148 } 149 } 150 151 public static void main(String[] args) { 152 int depth = GraphicsEnvironment.getLocalGraphicsEnvironment(). 153 getDefaultScreenDevice().getDefaultConfiguration(). 154 getColorModel().getPixelSize(); 155 if (depth < 16) { 156 System.out.println("Test PASSED (depth < 16bit)"); 157 return; 158 } 159 160 latch = new CountDownLatch(1); 161 RenderingToCachedGraphicsTest t1 = new RenderingToCachedGraphicsTest(); 162 t1.pack(); 163 t1.setSize(300, 300); 164 t1.setVisible(true); 165 166 try { latch.await(); } catch (InterruptedException ex) {} 167 t1.dispose(); 168 169 if (failed) { 170 throw new 171 RuntimeException("Failed: rendering didn't show up"); 172 } 173 System.out.println("Test PASSED"); 174 } 175 }