Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/DirectX/OnScreenRenderingResizeTest/OnScreenRenderingResizeTest.java
38918 views
/*1* Copyright (c) 2007, 2014, 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*/22/*23* @test24* @bug 6664068 666693125* @summary Tests that resizing a window to which a tight loop is rendering26* doesn't produce artifacts or crashes27* @author [email protected]: area=Graphics28* @run main/othervm OnScreenRenderingResizeTest29* @run main/othervm -Dsun.java2d.d3d=false OnScreenRenderingResizeTest30*/3132import java.awt.AWTException;33import java.awt.Color;34import java.awt.EventQueue;35import java.awt.Frame;36import java.awt.Graphics;37import java.awt.Graphics2D;38import java.awt.GraphicsConfiguration;39import java.awt.Insets;40import java.awt.Point;41import java.awt.Rectangle;42import java.awt.Robot;43import java.awt.event.WindowAdapter;44import java.awt.event.WindowEvent;45import java.awt.image.BufferedImage;46import java.awt.image.VolatileImage;47import java.io.File;48import java.io.IOException;49import javax.imageio.ImageIO;5051public class OnScreenRenderingResizeTest {5253private static volatile boolean done = false;54private static volatile boolean nocheck = false;5556private static final int FRAME_W = 256;57private static final int FRAME_H = 256;58private static final int IMAGE_W = 128;59private static final int IMAGE_H = 128;60private static long RUN_TIME = 1000*20;6162private static final Color renderColor = Color.green;63private static final Color bgColor = Color.white;6465public static void main(String[] args) {6667for (String arg : args) {68if ("-inf".equals(arg)) {69System.err.println("Test will run indefinitely");70RUN_TIME = Long.MAX_VALUE;71} else if ("-nocheck".equals(arg)) {72System.err.println("Test will not check rendering results");73nocheck = true;74} else {75System.err.println("Usage: OnScreenRenderingResizeTest [-inf][-nocheck]");76}77}7879BufferedImage output =80new BufferedImage(IMAGE_W, IMAGE_H, BufferedImage.TYPE_INT_RGB);81output.setAccelerationPriority(0.0f);82Graphics g = output.getGraphics();83g.setColor(renderColor);84g.fillRect(0, 0, output.getWidth(), output.getHeight());8586final Frame frame = new Frame("OnScreenRenderingResizeTest") {87public void paint(Graphics g) {}88public void update(Graphics g) {}89};90frame.setBackground(bgColor);91frame.setUndecorated(true);92frame.pack();9394GraphicsConfiguration gc = frame.getGraphicsConfiguration();95Rectangle gcBounds = gc.getBounds();96frame.setBounds(gcBounds.width / 4, gcBounds.height / 4, FRAME_W, FRAME_H);9798frame.addWindowListener(new WindowAdapter() {99public void windowClosing(WindowEvent e) {100done = true;101}102});103try {104EventQueue.invokeAndWait(new Runnable() {105public void run() {106frame.setVisible(true);107}108});109// wait for Vista's effects to complete110Thread.sleep(2000);111} catch (Exception ex) {112ex.printStackTrace();113}114115int maxW = gcBounds.width /2;116int maxH = gcBounds.height/2;117int minW = frame.getWidth();118int minH = frame.getHeight();119int incW = 10, incH = 10, cnt = 0;120Robot robot = null;121if (!nocheck && gc.getColorModel().getPixelSize() > 8) {122try {123robot = new Robot();124} catch (AWTException ex) {125System.err.println("Robot creation failed, continuing.");126}127} else {128System.err.println("No screen rendering checks.");129}130131VolatileImage vi = gc.createCompatibleVolatileImage(512, 512);132vi.validate(gc);133134long timeStarted = System.currentTimeMillis();135while (!done && (System.currentTimeMillis() - timeStarted) < RUN_TIME) {136137if (++cnt > 100) {138int w = frame.getWidth() + incW;139int h = frame.getHeight() + incH;140if (w < minW || w > maxW ) {141incW = -incW;142}143if (h < minH || h > maxH ) {144incH = -incH;145}146frame.setSize(w, h);147cnt = 0;148}149150// try to put the device into non-default state, for example,151// this operation below will set the transform152vi.validate(gc);153Graphics2D vig = (Graphics2D)vi.getGraphics();154vig.rotate(30.0f, vi.getWidth()/2, vi.getHeight()/2);155vig.drawImage(output, 0, 0,156vi.getWidth(), vi.getHeight(), null);157158Insets in = frame.getInsets();159frame.getGraphics().drawImage(output, in.left, in.top, null);160if (cnt == 90 && robot != null) {161robot.waitForIdle();162// area where we blitted to should be either white or green163Point p = frame.getLocationOnScreen();164p.translate(in.left+10, in.top+10);165BufferedImage bi =166robot.createScreenCapture(167new Rectangle(p.x, p.y, IMAGE_W/2, IMAGE_H/2));168int accepted1[] = { Color.white.getRGB(), Color.green.getRGB()};169checkBI(bi, accepted1);170171// the are where we didn't render should stay white172p = frame.getLocationOnScreen();173p.translate(in.left, in.top+IMAGE_H+5);174bi = robot.createScreenCapture(175new Rectangle(p.x, p.y,176frame.getWidth()-in.left-in.right,177frame.getHeight()-in.top-in.bottom-5-IMAGE_H));178int accepted2[] = { Color.white.getRGB() };179checkBI(bi, accepted2);180}181182Thread.yield();183}184frame.dispose();185System.out.println("Test Passed");186}187188private static void checkBI(BufferedImage bi, int accepted[]) {189for (int x = 0; x < bi.getWidth(); x++) {190for (int y = 0; y < bi.getHeight(); y++) {191int pix = bi.getRGB(x, y);192boolean found = false;193for (int acc : accepted) {194if (pix == acc) {195found = true;196break;197}198}199if (!found) {200try {201String name = "OnScreenRenderingResizeTest.png";202ImageIO.write(bi, "png", new File(name));203System.out.println("Screen shot file: " + name);204} catch (IOException ex) {}205206throw new207RuntimeException("Test failed at " + x + "-" + y +208" rgb=0x" + Integer.toHexString(pix));209}210}211}212}213}214215216