Path: blob/master/test/jdk/sun/java2d/DirectX/RenderingToCachedGraphicsTest/RenderingToCachedGraphicsTest.java
41153 views
/*1* Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 6648018 665266227* @summary Verifies that rendering to a cached onscreen Graphics works28* @author [email protected]: area=Graphics29* @run main/othervm RenderingToCachedGraphicsTest30* @run main/othervm -Dsun.java2d.d3d=false RenderingToCachedGraphicsTest31*/32import java.awt.Canvas;33import java.awt.Color;34import java.awt.Frame;35import java.awt.Graphics;36import java.awt.GraphicsEnvironment;37import java.awt.Point;38import java.awt.Rectangle;39import java.awt.Robot;40import java.awt.Toolkit;41import java.awt.event.WindowAdapter;42import java.awt.event.WindowEvent;43import java.awt.image.BufferStrategy;44import java.awt.image.BufferedImage;45import static java.awt.image.VolatileImage.*;46import java.awt.image.VolatileImage;47import java.io.File;48import java.util.concurrent.CountDownLatch;49import javax.imageio.ImageIO;5051public class RenderingToCachedGraphicsTest extends Frame {52private static volatile boolean failed = false;53private static volatile CountDownLatch latch;54private Graphics cachedGraphics;55private Canvas renderCanvas;5657public RenderingToCachedGraphicsTest() {58super("Test starts in 2 seconds");59renderCanvas = new Canvas() {60@Override61public void paint(Graphics g) {62if (getWidth() < 100 || getHeight() < 100) {63repaint();64return;65}66// wait for a bit so that Vista's Window manager's animation67// effects on window's appearance are completed (6652662)68try { Thread.sleep(2000); } catch (InterruptedException ex) {}6970try {71runTest();72} catch (Throwable t) {73failed = true;74} finally {75latch.countDown();76}77}78@Override79public void update(Graphics g) {}80};8182add("Center", renderCanvas);83}8485private void runTest() {86// this will cause screen update manager to dump the accelerated surface87// for this canvas88renderCanvas.createBufferStrategy(2);89BufferStrategy bs = renderCanvas.getBufferStrategy();90do {91Graphics bsg = bs.getDrawGraphics();92bsg.setColor(Color.blue);93bsg.fillRect(0, 0,94renderCanvas.getWidth(), renderCanvas.getHeight());95} while (bs.contentsLost() || bs.contentsRestored());9697// grab the "unaccelerated" onscreen surface98cachedGraphics = renderCanvas.getGraphics();99cachedGraphics.setColor(Color.red);100cachedGraphics.fillRect(0, 0, getWidth(), getHeight());101102bs.dispose();103bs = null;104// now the update manager should be able to accelerate onscreen105// rendering to it again106107cachedGraphics.setColor(Color.green);108// this causes restoration of the new accelerated onscreen surface109// (it is created in "lost" state)110cachedGraphics.fillRect(0, 0,111renderCanvas.getWidth(),112renderCanvas.getHeight());113Toolkit.getDefaultToolkit().sync();114// and now we should be able to render to it115cachedGraphics.fillRect(0, 0,116renderCanvas.getWidth(),117renderCanvas.getHeight());118Toolkit.getDefaultToolkit().sync();119120Robot robot = null;121try {122robot = new Robot();123} catch (Exception e) {124e.printStackTrace();125failed = true;126return;127}128129Point p = renderCanvas.getLocationOnScreen();130Rectangle r = new Rectangle(p.x, p.y,131renderCanvas.getWidth(),132renderCanvas.getHeight());133BufferedImage bi = robot.createScreenCapture(r);134for (int y = 0; y < bi.getHeight(); y++) {135for (int x = 0; x < bi.getWidth(); x++) {136if (bi.getRGB(x, y) != Color.green.getRGB()) {137System.err.println("Colors mismatch!");138String name = "RenderingToCachedGraphicsTest.png";139try {140ImageIO.write(bi, "png", new File(name));141System.err.println("Dumped grabbed image to: "+name);142} catch (Exception e) {}143failed = true;144return;145}146}147}148}149150public static void main(String[] args) {151int depth = GraphicsEnvironment.getLocalGraphicsEnvironment().152getDefaultScreenDevice().getDefaultConfiguration().153getColorModel().getPixelSize();154if (depth < 16) {155System.out.println("Test PASSED (depth < 16bit)");156return;157}158159latch = new CountDownLatch(1);160RenderingToCachedGraphicsTest t1 = new RenderingToCachedGraphicsTest();161t1.pack();162t1.setSize(300, 300);163t1.setVisible(true);164165try { latch.await(); } catch (InterruptedException ex) {}166t1.dispose();167168if (failed) {169throw new170RuntimeException("Failed: rendering didn't show up");171}172System.out.println("Test PASSED");173}174}175176177