Path: blob/master/test/jdk/sun/java2d/DirectX/OnScreenRenderingResizeTest/OnScreenRenderingResizeTest.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*/22/*23* @test24* @key headful25* @bug 6664068 666693126* @summary Tests that resizing a window to which a tight loop is rendering27* doesn't produce artifacts or crashes28* @author [email protected]: area=Graphics29* @run main/othervm OnScreenRenderingResizeTest30* @run main/othervm -Dsun.java2d.d3d=false OnScreenRenderingResizeTest31*/3233import java.awt.AWTException;34import java.awt.Color;35import java.awt.EventQueue;36import java.awt.Frame;37import java.awt.Graphics;38import java.awt.Graphics2D;39import java.awt.GraphicsConfiguration;40import java.awt.Insets;41import java.awt.Point;42import java.awt.Rectangle;43import java.awt.Robot;44import java.awt.event.WindowAdapter;45import java.awt.event.WindowEvent;46import java.awt.image.BufferedImage;47import java.awt.image.VolatileImage;48import java.io.File;49import java.io.IOException;50import javax.imageio.ImageIO;5152public class OnScreenRenderingResizeTest {5354private static volatile boolean done = false;55private static volatile boolean nocheck = false;5657private static final int FRAME_W = 256;58private static final int FRAME_H = 256;59private static final int IMAGE_W = 128;60private static final int IMAGE_H = 128;61private static long RUN_TIME = 1000*20;6263private static final Color renderColor = Color.green;64private static final Color bgColor = Color.white;6566public static void main(String[] args) {6768for (String arg : args) {69if ("-inf".equals(arg)) {70System.err.println("Test will run indefinitely");71RUN_TIME = Long.MAX_VALUE;72} else if ("-nocheck".equals(arg)) {73System.err.println("Test will not check rendering results");74nocheck = true;75} else {76System.err.println("Usage: OnScreenRenderingResizeTest [-inf][-nocheck]");77}78}7980BufferedImage output =81new BufferedImage(IMAGE_W, IMAGE_H, BufferedImage.TYPE_INT_RGB);82output.setAccelerationPriority(0.0f);83Graphics g = output.getGraphics();84g.setColor(renderColor);85g.fillRect(0, 0, output.getWidth(), output.getHeight());8687final Frame frame = new Frame("OnScreenRenderingResizeTest") {88public void paint(Graphics g) {}89public void update(Graphics g) {}90};91frame.setBackground(bgColor);92frame.setUndecorated(true);93frame.pack();9495GraphicsConfiguration gc = frame.getGraphicsConfiguration();96Rectangle gcBounds = gc.getBounds();97frame.setBounds(gcBounds.width / 4, gcBounds.height / 4, FRAME_W, FRAME_H);9899frame.addWindowListener(new WindowAdapter() {100public void windowClosing(WindowEvent e) {101done = true;102}103});104try {105EventQueue.invokeAndWait(new Runnable() {106public void run() {107frame.setVisible(true);108}109});110// wait for Vista's effects to complete111Thread.sleep(2000);112} catch (Exception ex) {113ex.printStackTrace();114}115116int maxW = gcBounds.width /2;117int maxH = gcBounds.height/2;118int minW = frame.getWidth();119int minH = frame.getHeight();120int incW = 10, incH = 10, cnt = 0;121Robot robot = null;122if (!nocheck && gc.getColorModel().getPixelSize() > 8) {123try {124robot = new Robot();125} catch (AWTException ex) {126System.err.println("Robot creation failed, continuing.");127}128} else {129System.err.println("No screen rendering checks.");130}131132VolatileImage vi = gc.createCompatibleVolatileImage(512, 512);133vi.validate(gc);134135long timeStarted = System.currentTimeMillis();136while (!done && (System.currentTimeMillis() - timeStarted) < RUN_TIME) {137138if (++cnt > 100) {139int w = frame.getWidth() + incW;140int h = frame.getHeight() + incH;141if (w < minW || w > maxW ) {142incW = -incW;143}144if (h < minH || h > maxH ) {145incH = -incH;146}147frame.setSize(w, h);148cnt = 0;149}150151// try to put the device into non-default state, for example,152// this operation below will set the transform153vi.validate(gc);154Graphics2D vig = (Graphics2D)vi.getGraphics();155vig.rotate(30.0f, vi.getWidth()/2, vi.getHeight()/2);156vig.drawImage(output, 0, 0,157vi.getWidth(), vi.getHeight(), null);158159Insets in = frame.getInsets();160frame.getGraphics().drawImage(output, in.left, in.top, null);161if (cnt == 90 && robot != null) {162robot.waitForIdle();163// area where we blitted to should be either white or green164Point p = frame.getLocationOnScreen();165p.translate(in.left+10, in.top+10);166BufferedImage bi =167robot.createScreenCapture(168new Rectangle(p.x, p.y, IMAGE_W/2, IMAGE_H/2));169int accepted1[] = { Color.white.getRGB(), Color.green.getRGB()};170checkBI(bi, accepted1);171172// the are where we didn't render should stay white173p = frame.getLocationOnScreen();174p.translate(in.left, in.top+IMAGE_H+5);175bi = robot.createScreenCapture(176new Rectangle(p.x, p.y,177frame.getWidth()-in.left-in.right,178frame.getHeight()-in.top-in.bottom-5-IMAGE_H));179int accepted2[] = { Color.white.getRGB() };180checkBI(bi, accepted2);181}182183Thread.yield();184}185frame.dispose();186System.out.println("Test Passed");187}188189private static void checkBI(BufferedImage bi, int accepted[]) {190for (int x = 0; x < bi.getWidth(); x++) {191for (int y = 0; y < bi.getHeight(); y++) {192int pix = bi.getRGB(x, y);193boolean found = false;194for (int acc : accepted) {195if (pix == acc) {196found = true;197break;198}199}200if (!found) {201try {202String name = "OnScreenRenderingResizeTest.png";203ImageIO.write(bi, "png", new File(name));204System.out.println("Screen shot file: " + name);205} catch (IOException ex) {}206207throw new208RuntimeException("Test failed at " + x + "-" + y +209" rgb=0x" + Integer.toHexString(pix));210}211}212}213}214}215216217