11/*
2- * Copyright (c) 2007, 2016 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2007, 2022 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2525 * @key headful
2626 * @bug 6664068 6666931
2727 * @summary Tests that resizing a window to which a tight loop is rendering
28- * doesn't produce artifacts or crashes
29- * @author [email protected] : area=Graphics 28+ * doesn't produce artifacts or crashes
3029 * @run main/othervm OnScreenRenderingResizeTest
3130 * @run main/othervm -Dsun.java2d.d3d=false OnScreenRenderingResizeTest
3231 */
3332
3433import java .awt .AWTException ;
3534import java .awt .Color ;
36- import java .awt .EventQueue ;
3735import java .awt .Frame ;
3836import java .awt .Graphics ;
3937import java .awt .Graphics2D ;
4038import java .awt .GraphicsConfiguration ;
41- import java .awt .Insets ;
4239import java .awt .Point ;
4340import java .awt .Rectangle ;
4441import java .awt .Robot ;
45- import java .awt .event .WindowAdapter ;
46- import java .awt .event .WindowEvent ;
42+ import java .awt .EventQueue ;
4743import java .awt .image .BufferedImage ;
4844import java .awt .image .VolatileImage ;
4945import java .io .File ;
5248
5349public class OnScreenRenderingResizeTest {
5450
55- private static volatile boolean done = false ;
5651 private static volatile boolean nocheck = false ;
5752
58- private static final int FRAME_W = 256 ;
59- private static final int FRAME_H = 256 ;
60- private static final int IMAGE_W = 128 ;
61- private static final int IMAGE_H = 128 ;
53+ private static int FRAME_W ;
54+ private static int FRAME_H ;
55+ private static int IMAGE_W ;
56+ private static int IMAGE_H ;
57+ private static final int tolerance = 12 ;
6258 private static long RUN_TIME = 1000 *20 ;
6359
6460 private static final Color renderColor = Color .green ;
6561 private static final Color bgColor = Color .white ;
6662
67- public static void main (String [] args ) {
63+ private static Frame frame ;
64+
65+ private static void createAndShowGUI () {
66+ frame = new Frame ();
67+ frame .setBackground (bgColor );
68+ frame .setUndecorated (true );
69+ frame .setAlwaysOnTop (true );
70+ frame .pack ();
71+ frame .setVisible (true );
72+ }
73+
74+ public static void main (String [] args ) throws Exception {
6875
6976 for (String arg : args ) {
7077 if ("-inf" .equals (arg )) {
@@ -74,126 +81,135 @@ public static void main(String[] args) {
7481 System .err .println ("Test will not check rendering results" );
7582 nocheck = true ;
7683 } else {
77- System .err .println ("Usage: OnScreenRenderingResizeTest [-inf][-nocheck]" );
84+ System .err .println ("Usage: OnScreenRenderingResizeTest" +
85+ " [-inf][-nocheck]" );
7886 }
7987 }
8088
81- BufferedImage output =
82- new BufferedImage (IMAGE_W , IMAGE_H , BufferedImage .TYPE_INT_RGB );
83- output .setAccelerationPriority (0.0f );
84- Graphics g = output .getGraphics ();
85- g .setColor (renderColor );
86- g .fillRect (0 , 0 , output .getWidth (), output .getHeight ());
87-
88- final Frame frame = new Frame ("OnScreenRenderingResizeTest" ) {
89- public void paint (Graphics g ) {}
90- public void update (Graphics g ) {}
91- };
92- frame .setBackground (bgColor );
93- frame .setUndecorated (true );
94- frame .pack ();
95-
96- GraphicsConfiguration gc = frame .getGraphicsConfiguration ();
97- Rectangle gcBounds = gc .getBounds ();
98- frame .setBounds (gcBounds .width / 4 , gcBounds .height / 4 , FRAME_W , FRAME_H );
99-
100- frame .addWindowListener (new WindowAdapter () {
101- public void windowClosing (WindowEvent e ) {
102- done = true ;
103- }
104- });
10589 try {
10690 EventQueue .invokeAndWait (new Runnable () {
10791 public void run () {
108- frame . setVisible ( true );
92+ createAndShowGUI ( );
10993 }
11094 });
111- // wait for Vista's effects to complete
11295 Thread .sleep (2000 );
11396 } catch (Exception ex ) {
11497 ex .printStackTrace ();
11598 }
11699
117- int maxW = gcBounds .width /2 ;
118- int maxH = gcBounds .height /2 ;
119- int minW = frame .getWidth ();
120- int minH = frame .getHeight ();
121- int incW = 10 , incH = 10 , cnt = 0 ;
122- Robot robot = null ;
123- if (!nocheck && gc .getColorModel ().getPixelSize () > 8 ) {
124- try {
125- robot = new Robot ();
126- } catch (AWTException ex ) {
127- System .err .println ("Robot creation failed, continuing." );
128- }
129- } else {
130- System .err .println ("No screen rendering checks." );
131- }
132-
133- VolatileImage vi = gc .createCompatibleVolatileImage (512 , 512 );
134- vi .validate (gc );
135-
136- long timeStarted = System .currentTimeMillis ();
137- while (!done && (System .currentTimeMillis () - timeStarted ) < RUN_TIME ) {
138-
139- if (++cnt > 100 ) {
140- int w = frame .getWidth () + incW ;
141- int h = frame .getHeight () + incH ;
142- if (w < minW || w > maxW ) {
143- incW = -incW ;
144- }
145- if (h < minH || h > maxH ) {
146- incH = -incH ;
100+ try {
101+ GraphicsConfiguration gc = frame .getGraphicsConfiguration ();
102+ Rectangle gcBounds = gc .getBounds ();
103+ FRAME_W = (gcBounds .width / 4 );
104+ FRAME_H = (gcBounds .height / 4 );
105+ IMAGE_W = (gcBounds .width / 8 );
106+ IMAGE_H = (gcBounds .height / 8 );
107+ frame .setBounds (gcBounds .width / 4 , gcBounds .height / 4 ,
108+ FRAME_W , FRAME_H );
109+
110+ BufferedImage output =
111+ new BufferedImage (IMAGE_W , IMAGE_H ,
112+ BufferedImage .TYPE_INT_RGB );
113+ output .setAccelerationPriority (0.0f );
114+ Graphics g = output .getGraphics ();
115+ g .setColor (renderColor );
116+ g .fillRect (0 , 0 , IMAGE_W , IMAGE_H );
117+
118+ int maxW = gcBounds .width / 2 ;
119+ int maxH = gcBounds .height / 2 ;
120+ int minW = FRAME_W ;
121+ int minH = FRAME_H ;
122+ int incW = 10 , incH = 10 , cnt = 0 ;
123+ Robot robot = null ;
124+ if (!nocheck && gc .getColorModel ().getPixelSize () > 8 ) {
125+ try {
126+ robot = new Robot ();
127+ robot .setAutoDelay (100 );
128+ robot .mouseMove (0 ,0 );
129+ } catch (AWTException ex ) {
130+ System .err .println ("Robot creation failed, continuing." );
147131 }
148- frame . setSize ( w , h );
149- cnt = 0 ;
132+ } else {
133+ System . err . println ( "No screen rendering checks." ) ;
150134 }
151-
152- // try to put the device into non-default state, for example,
153- // this operation below will set the transform
135+ VolatileImage vi = gc .
136+ createCompatibleVolatileImage (IMAGE_W , IMAGE_H );
154137 vi .validate (gc );
155- Graphics2D vig = (Graphics2D )vi .getGraphics ();
156- vig .rotate (30.0f , vi .getWidth ()/2 , vi .getHeight ()/2 );
157- vig .drawImage (output , 0 , 0 ,
158- vi .getWidth (), vi .getHeight (), null );
159-
160- Insets in = frame .getInsets ();
161- frame .getGraphics ().drawImage (output , in .left , in .top , null );
162- if (cnt == 90 && robot != null ) {
163- robot .waitForIdle ();
164- // area where we blitted to should be either white or green
165- Point p = frame .getLocationOnScreen ();
166- p .translate (in .left +10 , in .top +10 );
167- BufferedImage bi =
168- robot .createScreenCapture (
169- new Rectangle (p .x , p .y , IMAGE_W /2 , IMAGE_H /2 ));
170- int accepted1 [] = { Color .white .getRGB (), Color .green .getRGB ()};
171- checkBI (bi , accepted1 );
172-
173- // the are where we didn't render should stay white
174- p = frame .getLocationOnScreen ();
175- p .translate (in .left , in .top +IMAGE_H +5 );
176- bi = robot .createScreenCapture (
177- new Rectangle (p .x , p .y ,
178- frame .getWidth ()-in .left -in .right ,
179- frame .getHeight ()-in .top -in .bottom -5 -IMAGE_H ));
180- int accepted2 [] = { Color .white .getRGB () };
181- checkBI (bi , accepted2 );
138+ long timeStarted = System .currentTimeMillis ();
139+ while ((System .currentTimeMillis () - timeStarted ) < RUN_TIME ) {
140+
141+ if (++cnt > 100 ) {
142+ int w = frame .getWidth () + incW ;
143+ int h = frame .getHeight () + incH ;
144+ if (w < minW || w > maxW ) {
145+ incW = -incW ;
146+ }
147+ if (h < minH || h > maxH ) {
148+ incH = -incH ;
149+ }
150+ frame .setSize (w , h );
151+ cnt = 0 ;
152+ }
153+ // try to put the device into non-default state, for example,
154+ // this operation below will set the transform
155+ vi .validate (gc );
156+ Graphics2D vig = (Graphics2D )vi .getGraphics ();
157+ vig .rotate (30.0f , IMAGE_W /2 , IMAGE_H /2 );
158+ vig .drawImage (output , 0 , 0 ,
159+ IMAGE_W , IMAGE_H , null );
160+
161+ frame .getGraphics ().
162+ drawImage (output , 0 , 0 , null );
163+ if (cnt == 90 && robot != null ) {
164+ robot .waitForIdle ();
165+ // area where we blit and should be either white or green
166+ Point p = frame .getLocationOnScreen ();
167+ p .translate (10 , 10 );
168+ BufferedImage bi =
169+ robot .createScreenCapture (
170+ new Rectangle (p .x , p .y ,
171+ (IMAGE_W / 2 ), (IMAGE_H / 2 )));
172+ int accepted1 [] = {Color .white .getRGB (),
173+ Color .green .getRGB ()};
174+ checkBI (bi , accepted1 );
175+
176+ // the area where we didn't render should stay white
177+ robot .waitForIdle ();
178+ p = frame .getLocationOnScreen ();
179+ p .translate (10 , IMAGE_H + 10 );
180+ bi = robot .createScreenCapture (
181+ new Rectangle (p .x , p .y ,
182+ frame .getWidth () - 20 ,
183+ frame .getHeight () - 20 - (IMAGE_H )));
184+ int accepted2 [] = { Color .white .getRGB () };
185+ checkBI (bi , accepted2 );
186+ }
187+ Thread .yield ();
182188 }
183-
184- Thread . yield ();
189+ } finally {
190+ frame . dispose ();
185191 }
186- frame .dispose ();
187192 System .out .println ("Test Passed" );
188193 }
189194
190195 private static void checkBI (BufferedImage bi , int accepted []) {
191196 for (int x = 0 ; x < bi .getWidth (); x ++) {
192197 for (int y = 0 ; y < bi .getHeight (); y ++) {
193- int pix = bi .getRGB (x , y );
198+ int actual = bi .getRGB (x , y );
199+ int alpha = (actual >> 24 ) & 0xFF ;
200+ int red = (actual >> 16 ) & 0xFF ;
201+ int green = (actual >> 8 ) & 0xFF ;
202+ int blue = (actual ) & 0xFF ;
194203 boolean found = false ;
195204 for (int acc : accepted ) {
196- if (pix == acc ) {
205+ int accAlpha = (acc >> 24 ) & 0xFF ;
206+ int accRed = (acc >> 16 ) & 0xFF ;
207+ int accGreen = (acc >> 8 ) & 0xFF ;
208+ int accBlue = (acc ) & 0xFF ;
209+ if (!(Math .abs (alpha - accAlpha ) > tolerance ||
210+ Math .abs (red - accRed ) > tolerance ||
211+ Math .abs (green - accGreen ) > tolerance ||
212+ Math .abs (blue - accBlue ) > tolerance )) {
197213 found = true ;
198214 break ;
199215 }
@@ -204,10 +220,10 @@ private static void checkBI(BufferedImage bi, int accepted[]) {
204220 ImageIO .write (bi , "png" , new File (name ));
205221 System .out .println ("Screen shot file: " + name );
206222 } catch (IOException ex ) {}
207-
208223 throw new
209224 RuntimeException ("Test failed at " + x + "-" + y +
210- " rgb=0x" + Integer .toHexString (pix ));
225+ " rgb=0x" + Integer .
226+ toHexString (actual ));
211227 }
212228 }
213229 }
0 commit comments