0N/A/*
0N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
873N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A/*
0N/A * @test
5037N/A * @bug 6664068 6666931
6131N/A * @summary Tests that resizing a window to which a tight loop is rendering
0N/A * doesn't produce artifacts or crashes
0N/A * @author Dmitri.Trembovetski@sun.com: area=Graphics
0N/A * @run main/othervm OnScreenRenderingResizeTest
0N/A * @run main/othervm -Dsun.java2d.d3d=false OnScreenRenderingResizeTest
5422N/A */
0N/A
0N/Aimport java.awt.AWTException;
0N/Aimport java.awt.Color;
3012N/Aimport java.awt.EventQueue;
5040N/Aimport java.awt.Frame;
3381N/Aimport java.awt.Graphics;
3349N/Aimport java.awt.Graphics2D;
3349N/Aimport java.awt.GraphicsConfiguration;
3012N/Aimport java.awt.Insets;
1083N/Aimport java.awt.Point;
1083N/Aimport java.awt.Rectangle;
1083N/Aimport java.awt.Robot;
1083N/Aimport java.awt.event.WindowAdapter;
4156N/Aimport java.awt.event.WindowEvent;
1083N/Aimport java.awt.image.BufferedImage;
1083N/Aimport java.awt.image.VolatileImage;
1083N/Aimport java.io.File;
1083N/Aimport java.io.IOException;
3090N/Aimport javax.imageio.ImageIO;
3090N/A
3214N/Apublic class OnScreenRenderingResizeTest {
5481N/A
1083N/A private static volatile boolean done = false;
1083N/A private static volatile boolean nocheck = false;
1083N/A
1083N/A private static final int FRAME_W = 256;
233N/A private static final int FRAME_H = 256;
712N/A private static final int IMAGE_W = 128;
1177N/A private static final int IMAGE_H = 128;
564N/A private static long RUN_TIME = 1000*20;
0N/A
0N/A private static final Color renderColor = Color.green;
0N/A private static final Color bgColor = Color.white;
0N/A
712N/A public static void main(String[] args) {
0N/A
0N/A for (String arg : args) {
0N/A if ("-inf".equals(arg)) {
0N/A System.err.println("Test will run indefinitely");
0N/A RUN_TIME = Long.MAX_VALUE;
233N/A } else if ("-nocheck".equals(arg)) {
233N/A System.err.println("Test will not check rendering results");
0N/A nocheck = true;
0N/A } else {
0N/A System.err.println("Usage: OnScreenRenderingResizeTest [-inf][-nocheck]");
0N/A }
0N/A }
0N/A
0N/A BufferedImage output =
0N/A new BufferedImage(IMAGE_W, IMAGE_H, BufferedImage.TYPE_INT_RGB);
567N/A output.setAccelerationPriority(0.0f);
567N/A Graphics g = output.getGraphics();
567N/A g.setColor(renderColor);
567N/A g.fillRect(0, 0, output.getWidth(), output.getHeight());
2046N/A
2046N/A final Frame frame = new Frame("OnScreenRenderingResizeTest") {
1210N/A public void paint(Graphics g) {}
2033N/A public void update(Graphics g) {}
712N/A };
5614N/A frame.setBackground(bgColor);
2046N/A frame.pack();
712N/A frame.setSize(FRAME_W, FRAME_H);
675N/A frame.addWindowListener(new WindowAdapter() {
5058N/A public void windowClosing(WindowEvent e) {
712N/A done = true;
675N/A }
567N/A });
0N/A try {
5058N/A EventQueue.invokeAndWait(new Runnable() {
712N/A public void run() {
422N/A frame.setVisible(true);
0N/A }
422N/A });
0N/A // wait for Vista's effects to complete
422N/A Thread.sleep(2000);
0N/A } catch (Exception ex) {
0N/A ex.printStackTrace();
0N/A }
0N/A
902N/A GraphicsConfiguration gc = frame.getGraphicsConfiguration();
902N/A int maxW = gc.getBounds().width /2;
902N/A int maxH = gc.getBounds().height/2;
902N/A int minW = frame.getWidth();
0N/A int minH = frame.getHeight();
0N/A int incW = 10, incH = 10, cnt = 0;
0N/A Robot robot = null;
0N/A if (!nocheck && gc.getColorModel().getPixelSize() > 8) {
233N/A try {
233N/A robot = new Robot();
233N/A } catch (AWTException ex) {
233N/A System.err.println("Robot creation failed, continuing.");
730N/A }
730N/A } else {
730N/A System.err.println("No screen rendering checks.");
0N/A }
729N/A
729N/A VolatileImage vi = gc.createCompatibleVolatileImage(512, 512);
729N/A vi.validate(gc);
0N/A
0N/A long timeStarted = System.currentTimeMillis();
0N/A while (!done && (System.currentTimeMillis() - timeStarted) < RUN_TIME) {
110N/A
110N/A if (++cnt > 100) {
121N/A int w = frame.getWidth() + incW;
699N/A int h = frame.getHeight() + incH;
0N/A if (w < minW || w > maxW ) {
0N/A incW = -incW;
1008N/A }
1008N/A if (h < minH || h > maxH ) {
1008N/A incH = -incH;
1008N/A }
3214N/A frame.setSize(w, h);
3381N/A cnt = 0;
2086N/A }
3646N/A
3646N/A // try to put the device into non-default state, for example,
3646N/A // this operation below will set the transform
5947N/A vi.validate(gc);
2086N/A Graphics2D vig = (Graphics2D)vi.getGraphics();
3646N/A vig.rotate(30.0f, vi.getWidth()/2, vi.getHeight()/2);
1177N/A vig.drawImage(output, 0, 0,
3980N/A vi.getWidth(), vi.getHeight(), null);
3980N/A
5058N/A Insets in = frame.getInsets();
3980N/A frame.getGraphics().drawImage(output, in.left, in.top, null);
3980N/A if (cnt == 90 && robot != null) {
3349N/A // area where we blitted to should be either white or green
3349N/A Point p = frame.getLocationOnScreen();
3349N/A p.translate(in.left+10, in.top+10);
3349N/A BufferedImage bi =
4686N/A robot.createScreenCapture(
3349N/A new Rectangle(p.x, p.y, IMAGE_W/2, IMAGE_H/2));
3349N/A int accepted1[] = { Color.white.getRGB(), Color.green.getRGB()};
3381N/A checkBI(bi, accepted1);
3381N/A
3349N/A // the are where we didn't render should stay white
5713N/A p = frame.getLocationOnScreen();
5713N/A p.translate(in.left, in.top+IMAGE_H+5);
5713N/A bi = robot.createScreenCapture(
5713N/A new Rectangle(p.x, p.y,
5902N/A frame.getWidth()-in.left-in.right,
5902N/A frame.getHeight()-in.top-in.bottom-5-IMAGE_H));
5902N/A int accepted2[] = { Color.white.getRGB() };
5902N/A checkBI(bi, accepted1);
5902N/A }
5902N/A
5902N/A Thread.yield();
5902N/A }
5902N/A frame.dispose();
5713N/A System.out.println("Test Passed");
3381N/A }
0N/A
0N/A private static void checkBI(BufferedImage bi, int accepted[]) {
0N/A for (int x = 0; x < bi.getWidth(); x++) {
0N/A for (int y = 0; y < bi.getHeight(); y++) {
3381N/A int pix = bi.getRGB(x, y);
5058N/A boolean found = false;
5058N/A for (int acc : accepted) {
5058N/A if (pix == acc) {
3381N/A found = true;
0N/A break;
5058N/A }
5896N/A }
1177N/A if (!found) {
1177N/A try {
0N/A String name = "OnScreenRenderingResizeTest.png";
0N/A ImageIO.write(bi, "png", new File(name));
0N/A System.out.println("Screen shot file: " + name);
3853N/A } catch (IOException ex) {}
1177N/A
1177N/A throw new
0N/A RuntimeException("Test failed at " + x + "-" + y +
3853N/A " rgb=0x" + Integer.toHexString(pix));
3853N/A }
3853N/A }
3853N/A }
3853N/A }
0N/A}
3853N/A