430N/A/*
2362N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
430N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
430N/A *
430N/A * This code is free software; you can redistribute it and/or modify it
430N/A * under the terms of the GNU General Public License version 2 only, as
430N/A * published by the Free Software Foundation.
430N/A *
430N/A * This code is distributed in the hope that it will be useful, but WITHOUT
430N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
430N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
430N/A * version 2 for more details (a copy is included in the LICENSE file that
430N/A * accompanied this code).
430N/A *
430N/A * You should have received a copy of the GNU General Public License version
430N/A * 2 along with this work; if not, write to the Free Software Foundation,
430N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
430N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
430N/A */
430N/A
430N/A/*
430N/A * @test
430N/A * @bug 6648018 6652662
430N/A * @summary Verifies that rendering to a cached onscreen Graphics works
430N/A * @author Dmitri.Trembovetski@sun.com: area=Graphics
430N/A * @run main/othervm RenderingToCachedGraphicsTest
430N/A * @run main/othervm -Dsun.java2d.d3d=false RenderingToCachedGraphicsTest
430N/A */
430N/Aimport java.awt.Canvas;
430N/Aimport java.awt.Color;
430N/Aimport java.awt.Frame;
430N/Aimport java.awt.Graphics;
430N/Aimport java.awt.GraphicsEnvironment;
430N/Aimport java.awt.Point;
430N/Aimport java.awt.Rectangle;
430N/Aimport java.awt.Robot;
430N/Aimport java.awt.Toolkit;
430N/Aimport java.awt.event.WindowAdapter;
430N/Aimport java.awt.event.WindowEvent;
430N/Aimport java.awt.image.BufferStrategy;
430N/Aimport java.awt.image.BufferedImage;
430N/Aimport static java.awt.image.VolatileImage.*;
430N/Aimport java.awt.image.VolatileImage;
430N/Aimport java.io.File;
430N/Aimport java.util.concurrent.CountDownLatch;
430N/Aimport javax.imageio.ImageIO;
430N/A
430N/Apublic class RenderingToCachedGraphicsTest extends Frame {
430N/A private static volatile boolean failed = false;
430N/A private static volatile CountDownLatch latch;
430N/A private Graphics cachedGraphics;
430N/A private Canvas renderCanvas;
430N/A
430N/A public RenderingToCachedGraphicsTest() {
430N/A super("Test starts in 2 seconds");
430N/A renderCanvas = new Canvas() {
430N/A @Override
430N/A public void paint(Graphics g) {
430N/A if (getWidth() < 100 || getHeight() < 100) {
430N/A repaint();
430N/A return;
430N/A }
430N/A // wait for a bit so that Vista's Window manager's animation
430N/A // effects on window's appearance are completed (6652662)
430N/A try { Thread.sleep(2000); } catch (InterruptedException ex) {}
430N/A
430N/A try {
430N/A runTest();
430N/A } catch (Throwable t) {
430N/A failed = true;
430N/A } finally {
430N/A latch.countDown();
430N/A }
430N/A }
430N/A @Override
430N/A public void update(Graphics g) {}
430N/A };
430N/A
430N/A add("Center", renderCanvas);
430N/A }
430N/A
430N/A private void runTest() {
430N/A // this will cause screen update manager to dump the accelerated surface
430N/A // for this canvas
430N/A renderCanvas.createBufferStrategy(2);
430N/A BufferStrategy bs = renderCanvas.getBufferStrategy();
430N/A do {
430N/A Graphics bsg = bs.getDrawGraphics();
430N/A bsg.setColor(Color.blue);
430N/A bsg.fillRect(0, 0,
430N/A renderCanvas.getWidth(), renderCanvas.getHeight());
430N/A } while (bs.contentsLost() || bs.contentsRestored());
430N/A
430N/A // grab the "unaccelerated" onscreen surface
430N/A cachedGraphics = renderCanvas.getGraphics();
430N/A cachedGraphics.setColor(Color.red);
430N/A cachedGraphics.fillRect(0, 0, getWidth(), getHeight());
430N/A
430N/A bs.dispose();
430N/A bs = null;
430N/A // now the update manager should be able to accelerate onscreen
430N/A // rendering to it again
430N/A
430N/A cachedGraphics.setColor(Color.green);
430N/A // this causes restoration of the new accelerated onscreen surface
430N/A // (it is created in "lost" state)
430N/A cachedGraphics.fillRect(0, 0,
430N/A renderCanvas.getWidth(),
430N/A renderCanvas.getHeight());
430N/A Toolkit.getDefaultToolkit().sync();
430N/A // and now we should be able to render to it
430N/A cachedGraphics.fillRect(0, 0,
430N/A renderCanvas.getWidth(),
430N/A renderCanvas.getHeight());
430N/A Toolkit.getDefaultToolkit().sync();
430N/A
430N/A Robot robot = null;
430N/A try {
430N/A robot = new Robot();
430N/A } catch (Exception e) {
430N/A e.printStackTrace();
430N/A failed = true;
430N/A return;
430N/A }
430N/A
430N/A Point p = renderCanvas.getLocationOnScreen();
430N/A Rectangle r = new Rectangle(p.x, p.y,
430N/A renderCanvas.getWidth(),
430N/A renderCanvas.getHeight());
430N/A BufferedImage bi = robot.createScreenCapture(r);
430N/A for (int y = 0; y < bi.getHeight(); y++) {
430N/A for (int x = 0; x < bi.getWidth(); x++) {
430N/A if (bi.getRGB(x, y) != Color.green.getRGB()) {
430N/A System.err.println("Colors mismatch!");
430N/A String name = "RenderingToCachedGraphicsTest.png";
430N/A try {
430N/A ImageIO.write(bi, "png", new File(name));
430N/A System.err.println("Dumped grabbed image to: "+name);
430N/A } catch (Exception e) {}
430N/A failed = true;
430N/A return;
430N/A }
430N/A }
430N/A }
430N/A }
430N/A
430N/A public static void main(String[] args) {
430N/A int depth = GraphicsEnvironment.getLocalGraphicsEnvironment().
430N/A getDefaultScreenDevice().getDefaultConfiguration().
430N/A getColorModel().getPixelSize();
430N/A if (depth < 16) {
430N/A System.out.println("Test PASSED (depth < 16bit)");
430N/A return;
430N/A }
430N/A
430N/A latch = new CountDownLatch(1);
430N/A RenderingToCachedGraphicsTest t1 = new RenderingToCachedGraphicsTest();
430N/A t1.pack();
430N/A t1.setSize(300, 300);
430N/A t1.setVisible(true);
430N/A
430N/A try { latch.await(); } catch (InterruptedException ex) {}
430N/A t1.dispose();
430N/A
430N/A if (failed) {
430N/A throw new
430N/A RuntimeException("Failed: rendering didn't show up");
430N/A }
430N/A System.out.println("Test PASSED");
430N/A }
430N/A}