Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/GraphicsDevice/DisplayModes/CycleDMImage.java
41154 views
1
/*
2
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.awt.Color;
25
import java.awt.Component;
26
import java.awt.DisplayMode;
27
import java.awt.Frame;
28
import java.awt.Graphics;
29
import java.awt.GraphicsConfiguration;
30
import java.awt.GraphicsDevice;
31
import java.awt.GraphicsEnvironment;
32
import java.awt.Image;
33
import java.awt.Rectangle;
34
import java.awt.Robot;
35
import java.awt.Transparency;
36
import java.awt.Window;
37
import java.awt.event.KeyEvent;
38
import java.awt.event.KeyListener;
39
import java.awt.image.BufferedImage;
40
import java.util.ArrayList;
41
42
/**
43
* @test
44
* @bug 4836241 6364134 8232200 8252133
45
* @key headful
46
* @summary verify that images are restored correctly after display mode
47
* switches and that no other rendering or crash problems occur
48
* @run main/timeout=500 CycleDMImage
49
*/
50
public class CycleDMImage extends Component implements Runnable, KeyListener {
51
/**
52
* This test cycles through all available display modes, waiting after
53
* each call to setDisplayMode() to ensure that the new one is active
54
* before proceeding on to the next one. The Component is filled with
55
* a green background color and then compatible images of all 3
56
* Transparency types are copied to the screen. The results of these
57
* operations are checked (using Robot) and the test fails if any of the
58
* rendering is wrong in any of the DisplayModes. The purpose of this
59
* test is to ensure that display mode switches do not cause problems
60
* with image restoration (or other rendering operations).
61
*/
62
boolean painted = false;
63
boolean earlyExit = false;
64
Image rImage = null, wImage = null, bImage = null;
65
int imgSize = 10;
66
static Robot robot = null;
67
volatile static boolean done = false;
68
static String errorMessage = null;
69
70
public synchronized void paint(Graphics g) {
71
if (!painted) {
72
painted = true;
73
(new Thread(this)).start();
74
}
75
if (rImage == null) {
76
GraphicsConfiguration gc = getGraphicsConfiguration();
77
rImage = gc.createCompatibleImage(imgSize, imgSize);
78
wImage = gc.createCompatibleImage(imgSize, imgSize,
79
Transparency.BITMASK);
80
bImage = gc.createCompatibleImage(imgSize, imgSize,
81
Transparency.TRANSLUCENT);
82
Graphics imgGraphics = rImage.getGraphics();
83
imgGraphics.setColor(Color.red);
84
imgGraphics.fillRect(0, 0, imgSize, imgSize);
85
imgGraphics = wImage.getGraphics();
86
imgGraphics.setColor(Color.white);
87
imgGraphics.fillRect(0, 0, imgSize, imgSize);
88
imgGraphics = bImage.getGraphics();
89
imgGraphics.setColor(Color.blue);
90
imgGraphics.fillRect(0, 0, imgSize, imgSize);
91
}
92
g.setColor(Color.green);
93
g.fillRect(0, 0, getWidth(), getHeight());
94
g.drawImage(rImage, 0, 0, this);
95
g.drawImage(wImage, imgSize, 0, this);
96
g.drawImage(bImage, imgSize*2, 0, this);
97
g.drawImage(rImage, 0, getHeight()-imgSize, null);
98
g.drawImage(rImage, getWidth()-imgSize, getHeight()-imgSize, null);
99
g.drawImage(rImage, getWidth()-imgSize, 0, null);
100
}
101
102
static void delay(long ms) {
103
try {
104
Thread.sleep(ms);
105
} catch (Exception e) {}
106
}
107
108
public boolean checkResult(DisplayMode dm) {
109
Rectangle bounds = getGraphicsConfiguration().getBounds();
110
int pixels[] = new int[imgSize * 4];
111
BufferedImage clientPixels =
112
robot.createScreenCapture(new Rectangle(bounds.x, bounds.y,
113
imgSize*4, 1));
114
clientPixels.getRGB(0, 0, imgSize * 4, 1, pixels, 0, getWidth());
115
// Now check the results. We expect: imgSize blocks of r/w/b/g
116
int colors[] = {0xffff0000, 0xffffffff, 0xff0000ff, 0xff00ff00};
117
for (int color = 0; color < 4; ++color) {
118
for (int i = 0; i < imgSize; ++i) {
119
int pixelIndex = imgSize * color + i;
120
if (pixels[pixelIndex] != colors[color]) {
121
errorMessage = "\n DisplayMode(" +
122
dm.getWidth() + " x " +
123
dm.getHeight() + " x " +
124
dm.getBitDepth() + "bpp x " +
125
dm.getRefreshRate() +
126
")\n Pixel " + i +
127
": Expected " +
128
Integer.toHexString(colors[color]) +
129
", got " +
130
Integer.toHexString(pixels[pixelIndex]) +
131
" at " + i;
132
return false;
133
}
134
}
135
}
136
return true;
137
}
138
139
boolean displayModesEqual(DisplayMode dm1, DisplayMode dm2) {
140
if (dm1.equals(dm2)) {
141
return true;
142
}
143
// not enough - check whether the modes are equal except for
144
// refreshRate, if either mode has REFRESH_RATE_UNKNOWN
145
// value for this parameter
146
if (dm1.getWidth() != dm2.getWidth() ||
147
dm1.getHeight() != dm2.getHeight() ||
148
dm1.getBitDepth() != dm2.getBitDepth())
149
{
150
// core parameters must match
151
return false;
152
}
153
// Now we know that w1 == w2, h1 == h2, and d1 == d2; must be the
154
// case that the refresh rates do not match.
155
// Is either one REFRESH_RATE_UNKNOWN?
156
if (dm1.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN ||
157
dm2.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN)
158
{
159
return true;
160
}
161
return false;
162
}
163
164
public void run() {
165
GraphicsDevice gd = getGraphicsConfiguration().getDevice();
166
gd.setFullScreenWindow((Window) getParent());
167
// First, delay a bit just to let the fullscreen window
168
// settle down before switching display modes
169
robot.waitForIdle();
170
delay(1000);
171
172
if (!gd.isDisplayChangeSupported()) {
173
System.err.println("Display change is not supported,"+
174
" the test is considered passed.");
175
finished();
176
return;
177
}
178
179
// We are really only interested in unique w/h/d resolutions
180
// and it would be nice to skip the myriad of refresh rate
181
// varations, so let us construct a subset that contains
182
// only those DisplayModes with unique w/h/d values
183
// Also, due to a current bug (4837228), we should skip the
184
// 24-bit depths since attempting to create bitmask-transparent
185
// ddraw images can cause the test to crash (we should change this
186
// test to include that depth when the bug is fixed).
187
ArrayList<DisplayMode> dmSubset = new ArrayList<>();
188
for (final DisplayMode dm : gd.getDisplayModes()) {
189
boolean skip = false;
190
for (final DisplayMode dmUnique : dmSubset) {
191
int bitDepth = dm.getBitDepth();
192
int width = dm.getWidth();
193
int height = dm.getHeight();
194
if (bitDepth == 24 || width <= 800 || height <= 600 ||
195
(dmUnique.getWidth() == width &&
196
dmUnique.getHeight() == height &&
197
dmUnique.getBitDepth() == bitDepth)) {
198
skip = true;
199
break;
200
}
201
}
202
if (!skip) {
203
dmSubset.add(dm);
204
}
205
}
206
207
// Now, cycle through the display modes one-by-one. For
208
// each new display mode, delay until we detect that the
209
// new mode == the current mode. Then delay an additional
210
// second (to allow any repaints to occur)
211
212
for (DisplayMode newDM : dmSubset) {
213
gd.setDisplayMode(newDM);
214
while (!displayModesEqual(newDM, gd.getDisplayMode())) {
215
delay(100);
216
}
217
// Delay another few seconds after the new display mode is active
218
delay(4000);
219
220
// Check the rendering results
221
if (!checkResult(newDM)) {
222
finished();
223
return;
224
}
225
226
// Escape out if requested by the user
227
if (earlyExit) {
228
System.out.println("Exiting test early, by request");
229
System.exit(0);
230
}
231
}
232
233
// Done with test; if we got here, we passed
234
System.out.println("Passed");
235
finished();
236
}
237
238
public static void finished() {
239
synchronized (CycleDMImage.class) {
240
done = true;
241
CycleDMImage.class.notify();
242
}
243
}
244
245
/**
246
* KeyListener methods; these provide a way for a user to escape out of
247
* a potentially lengthy test.
248
*/
249
250
public void keyTyped(KeyEvent e) {
251
}
252
253
public void keyPressed(KeyEvent e) {
254
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
255
earlyExit = true;
256
}
257
}
258
259
public void keyReleased(KeyEvent e) {
260
}
261
262
public static void main(String args[]) throws Exception {
263
robot = new Robot();
264
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
265
for (final GraphicsDevice gd: ge.getScreenDevices()) {
266
if (!gd.isFullScreenSupported()) {
267
System.err.println("FullScreen mode is not supported,"+
268
" the test is considered passed.");
269
continue;
270
}
271
done = false;
272
DisplayMode currentDM = gd.getDisplayMode();
273
Frame frame = new Frame(gd.getDefaultConfiguration());
274
try {
275
frame.setSize(400, 400);
276
frame.setUndecorated(true);
277
CycleDMImage comp = new CycleDMImage();
278
frame.addKeyListener(comp);
279
frame.add(comp);
280
frame.setVisible(true);
281
// Sleep awaiting frame disposal
282
synchronized (CycleDMImage.class) {
283
while (!done) {
284
try {
285
CycleDMImage.class.wait(100);
286
} catch (InterruptedException e) {
287
}
288
}
289
}
290
} finally {
291
gd.setDisplayMode(currentDM);
292
gd.setFullScreenWindow(null);
293
frame.dispose();
294
}
295
if (errorMessage != null) {
296
throw new RuntimeException(errorMessage);
297
}
298
// delay a bit just to let the fullscreen window disposing complete
299
// before switching to next display
300
delay(10000);
301
}
302
}
303
}
304
305