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