Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/DirectX/RenderingToCachedGraphicsTest/RenderingToCachedGraphicsTest.java
38918 views
/*1* Copyright (c) 2007, 2008, 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* @bug 6648018 665266226* @summary Verifies that rendering to a cached onscreen Graphics works27* @author [email protected]: area=Graphics28* @run main/othervm RenderingToCachedGraphicsTest29* @run main/othervm -Dsun.java2d.d3d=false RenderingToCachedGraphicsTest30*/31import java.awt.Canvas;32import java.awt.Color;33import java.awt.Frame;34import java.awt.Graphics;35import java.awt.GraphicsEnvironment;36import java.awt.Point;37import java.awt.Rectangle;38import java.awt.Robot;39import java.awt.Toolkit;40import java.awt.event.WindowAdapter;41import java.awt.event.WindowEvent;42import java.awt.image.BufferStrategy;43import java.awt.image.BufferedImage;44import static java.awt.image.VolatileImage.*;45import java.awt.image.VolatileImage;46import java.io.File;47import java.util.concurrent.CountDownLatch;48import javax.imageio.ImageIO;4950public class RenderingToCachedGraphicsTest extends Frame {51private static volatile boolean failed = false;52private static volatile CountDownLatch latch;53private Graphics cachedGraphics;54private Canvas renderCanvas;5556public RenderingToCachedGraphicsTest() {57super("Test starts in 2 seconds");58renderCanvas = new Canvas() {59@Override60public void paint(Graphics g) {61if (getWidth() < 100 || getHeight() < 100) {62repaint();63return;64}65// wait for a bit so that Vista's Window manager's animation66// effects on window's appearance are completed (6652662)67try { Thread.sleep(2000); } catch (InterruptedException ex) {}6869try {70runTest();71} catch (Throwable t) {72failed = true;73} finally {74latch.countDown();75}76}77@Override78public void update(Graphics g) {}79};8081add("Center", renderCanvas);82}8384private void runTest() {85// this will cause screen update manager to dump the accelerated surface86// for this canvas87renderCanvas.createBufferStrategy(2);88BufferStrategy bs = renderCanvas.getBufferStrategy();89do {90Graphics bsg = bs.getDrawGraphics();91bsg.setColor(Color.blue);92bsg.fillRect(0, 0,93renderCanvas.getWidth(), renderCanvas.getHeight());94} while (bs.contentsLost() || bs.contentsRestored());9596// grab the "unaccelerated" onscreen surface97cachedGraphics = renderCanvas.getGraphics();98cachedGraphics.setColor(Color.red);99cachedGraphics.fillRect(0, 0, getWidth(), getHeight());100101bs.dispose();102bs = null;103// now the update manager should be able to accelerate onscreen104// rendering to it again105106cachedGraphics.setColor(Color.green);107// this causes restoration of the new accelerated onscreen surface108// (it is created in "lost" state)109cachedGraphics.fillRect(0, 0,110renderCanvas.getWidth(),111renderCanvas.getHeight());112Toolkit.getDefaultToolkit().sync();113// and now we should be able to render to it114cachedGraphics.fillRect(0, 0,115renderCanvas.getWidth(),116renderCanvas.getHeight());117Toolkit.getDefaultToolkit().sync();118119Robot robot = null;120try {121robot = new Robot();122} catch (Exception e) {123e.printStackTrace();124failed = true;125return;126}127128Point p = renderCanvas.getLocationOnScreen();129Rectangle r = new Rectangle(p.x, p.y,130renderCanvas.getWidth(),131renderCanvas.getHeight());132BufferedImage bi = robot.createScreenCapture(r);133for (int y = 0; y < bi.getHeight(); y++) {134for (int x = 0; x < bi.getWidth(); x++) {135if (bi.getRGB(x, y) != Color.green.getRGB()) {136System.err.println("Colors mismatch!");137String name = "RenderingToCachedGraphicsTest.png";138try {139ImageIO.write(bi, "png", new File(name));140System.err.println("Dumped grabbed image to: "+name);141} catch (Exception e) {}142failed = true;143return;144}145}146}147}148149public static void main(String[] args) {150int depth = GraphicsEnvironment.getLocalGraphicsEnvironment().151getDefaultScreenDevice().getDefaultConfiguration().152getColorModel().getPixelSize();153if (depth < 16) {154System.out.println("Test PASSED (depth < 16bit)");155return;156}157158latch = new CountDownLatch(1);159RenderingToCachedGraphicsTest t1 = new RenderingToCachedGraphicsTest();160t1.pack();161t1.setSize(300, 300);162t1.setVisible(true);163164try { latch.await(); } catch (InterruptedException ex) {}165t1.dispose();166167if (failed) {168throw new169RuntimeException("Failed: rendering didn't show up");170}171System.out.println("Test PASSED");172}173}174175176