File: CycleDMImage.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (303 lines) | stat: -rw-r--r-- 11,858 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.awt.Color;
import java.awt.Component;
import java.awt.DisplayMode;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Transparency;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
 * @test
 * @bug 4836241 6364134 8232200 8252133
 * @key headful
 * @summary verify that images are restored correctly after display mode
 *          switches and that no other rendering or crash problems occur
 * @run main/timeout=500 CycleDMImage
 */
public class CycleDMImage extends Component implements Runnable, KeyListener {
    /**
     * This test cycles through all available display modes, waiting after
     * each call to setDisplayMode() to ensure that the new one is active
     * before proceeding on to the next one.  The Component is filled with
     * a green background color and then compatible images of all 3
     * Transparency types are copied to the screen.  The results of these
     * operations are checked (using Robot) and the test fails if any of the
     * rendering is wrong in any of the DisplayModes.  The purpose of this
     * test is to ensure that display mode switches do not cause problems
     * with image restoration (or other rendering operations).
     */
    boolean painted = false;
    boolean earlyExit = false;
    Image rImage = null, wImage = null, bImage = null;
    int imgSize = 10;
    static Robot robot = null;
    volatile static boolean done = false;
    static String errorMessage = null;

    public synchronized void paint(Graphics g) {
        if (!painted) {
            painted = true;
            (new Thread(this)).start();
        }
        if (rImage == null) {
            GraphicsConfiguration gc = getGraphicsConfiguration();
            rImage = gc.createCompatibleImage(imgSize, imgSize);
            wImage = gc.createCompatibleImage(imgSize, imgSize,
                                              Transparency.BITMASK);
            bImage = gc.createCompatibleImage(imgSize, imgSize,
                                              Transparency.TRANSLUCENT);
            Graphics imgGraphics = rImage.getGraphics();
            imgGraphics.setColor(Color.red);
            imgGraphics.fillRect(0, 0, imgSize, imgSize);
            imgGraphics = wImage.getGraphics();
            imgGraphics.setColor(Color.white);
            imgGraphics.fillRect(0, 0, imgSize, imgSize);
            imgGraphics = bImage.getGraphics();
            imgGraphics.setColor(Color.blue);
            imgGraphics.fillRect(0, 0, imgSize, imgSize);
        }
        g.setColor(Color.green);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(rImage, 0, 0, this);
        g.drawImage(wImage, imgSize, 0, this);
        g.drawImage(bImage, imgSize*2, 0, this);
        g.drawImage(rImage, 0, getHeight()-imgSize, null);
        g.drawImage(rImage, getWidth()-imgSize, getHeight()-imgSize, null);
        g.drawImage(rImage, getWidth()-imgSize, 0, null);
    }

    static void delay(long ms) {
        try {
            Thread.sleep(ms);
        } catch (Exception e) {}
    }

    public boolean checkResult(DisplayMode dm) {
        Rectangle bounds = getGraphicsConfiguration().getBounds();
        int pixels[] = new int[imgSize * 4];
        BufferedImage clientPixels =
            robot.createScreenCapture(new Rectangle(bounds.x, bounds.y,
                                                    imgSize*4, 1));
        clientPixels.getRGB(0, 0, imgSize * 4, 1, pixels, 0, getWidth());
        // Now check the results.  We expect: imgSize blocks of r/w/b/g
        int colors[] = {0xffff0000, 0xffffffff, 0xff0000ff, 0xff00ff00};
        for (int color = 0; color < 4; ++color) {
            for (int i = 0; i < imgSize; ++i) {
                int pixelIndex = imgSize * color + i;
                if (pixels[pixelIndex] != colors[color]) {
                    errorMessage = "\n    DisplayMode(" +
                        dm.getWidth() + " x " +
                        dm.getHeight() + " x " +
                        dm.getBitDepth() + "bpp x " +
                        dm.getRefreshRate() +
                        ")\n    Pixel " + i +
                        ": Expected " +
                        Integer.toHexString(colors[color]) +
                        ", got " +
                        Integer.toHexString(pixels[pixelIndex]) +
                        " at " + i;
                    return false;
                }
            }
        }
        return true;
    }

    boolean displayModesEqual(DisplayMode dm1, DisplayMode dm2) {
        if (dm1.equals(dm2)) {
            return true;
        }
        // not enough - check whether the modes are equal except for
        // refreshRate, if either mode has REFRESH_RATE_UNKNOWN
        // value for this parameter
        if (dm1.getWidth() != dm2.getWidth() ||
            dm1.getHeight() != dm2.getHeight() ||
            dm1.getBitDepth() != dm2.getBitDepth())
        {
            // core parameters must match
            return false;
        }
        // Now we know that w1 == w2, h1 == h2, and d1 == d2; must be the
        // case that the refresh rates do not match.
        // Is either one REFRESH_RATE_UNKNOWN?
        if (dm1.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN ||
            dm2.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN)
        {
            return true;
        }
        return false;
    }

    public void run() {
        GraphicsDevice gd = getGraphicsConfiguration().getDevice();
        gd.setFullScreenWindow((Window) getParent());
        // First, delay a bit just to let the fullscreen window
        // settle down before switching display modes
        robot.waitForIdle();
        delay(1000);

        if (!gd.isDisplayChangeSupported()) {
            System.err.println("Display change is not supported,"+
                               " the test is considered passed.");
            finished();
            return;
        }

        // We are really only interested in unique w/h/d resolutions
        // and it would be nice to skip the myriad of refresh rate
        // varations, so let us construct a subset that contains
        // only those DisplayModes with unique w/h/d values
        // Also, due to a current bug (4837228), we should skip the
        // 24-bit depths since attempting to create bitmask-transparent
        // ddraw images can cause the test to crash (we should change this
        // test to include that depth when the bug is fixed).
        ArrayList<DisplayMode> dmSubset = new ArrayList<>();
        for (final DisplayMode dm : gd.getDisplayModes()) {
            boolean skip = false;
            for (final DisplayMode dmUnique : dmSubset) {
                int bitDepth = dm.getBitDepth();
                int width = dm.getWidth();
                int height = dm.getHeight();
                if (bitDepth == 24 || width <= 800 || height <= 600 ||
                        (dmUnique.getWidth() == width &&
                         dmUnique.getHeight() == height &&
                         dmUnique.getBitDepth() == bitDepth)) {
                    skip = true;
                    break;
                }
            }
            if (!skip) {
                dmSubset.add(dm);
            }
        }

        // Now, cycle through the display modes one-by-one.  For
        // each new display mode, delay until we detect that the
        // new mode == the current mode.  Then delay an additional
        // second (to allow any repaints to occur)

        for (DisplayMode newDM : dmSubset) {
            gd.setDisplayMode(newDM);
            while (!displayModesEqual(newDM, gd.getDisplayMode())) {
                delay(100);
            }
            // Delay another few seconds after the new display mode is active
            delay(4000);

            // Check the rendering results
            if (!checkResult(newDM)) {
                finished();
                return;
            }

            // Escape out if requested by the user
            if (earlyExit) {
                System.out.println("Exiting test early, by request");
                System.exit(0);
            }
        }

        // Done with test; if we got here, we passed
        System.out.println("Passed");
        finished();
    }

    public static void finished() {
        synchronized (CycleDMImage.class) {
            done = true;
            CycleDMImage.class.notify();
        }
    }

    /**
     * KeyListener methods; these provide a way for a user to escape out of
     * a potentially lengthy test.
     */

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            earlyExit = true;
        }
    }

    public void keyReleased(KeyEvent e) {
    }

    public static void main(String args[]) throws Exception {
        robot = new Robot();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        for (final GraphicsDevice gd: ge.getScreenDevices()) {
            if (!gd.isFullScreenSupported()) {
                System.err.println("FullScreen mode is not supported,"+
                                           " the test is considered passed.");
                continue;
            }
            done = false;
            DisplayMode currentDM = gd.getDisplayMode();
            Frame frame = new Frame(gd.getDefaultConfiguration());
            try {
                frame.setSize(400, 400);
                frame.setUndecorated(true);
                CycleDMImage comp = new CycleDMImage();
                frame.addKeyListener(comp);
                frame.add(comp);
                frame.setVisible(true);
                // Sleep awaiting frame disposal
                synchronized (CycleDMImage.class) {
                    while (!done) {
                        try {
                            CycleDMImage.class.wait(100);
                        } catch (InterruptedException e) {
                        }
                    }
                }
            } finally {
                gd.setDisplayMode(currentDM);
                gd.setFullScreenWindow(null);
                frame.dispose();
            }
            if (errorMessage != null) {
                throw new RuntimeException(errorMessage);
            }
            // delay a bit just to let the fullscreen window disposing complete
            // before switching to next display
            delay(10000);
        }
    }
}