author | yan |
Mon, 06 Oct 2008 16:45:00 +0400 | |
changeset 1966 | 12a51fb0db0d |
parent 889 | 6549643c008c |
child 4256 | 24d614d4764a |
child 4204 | d8eff41661bf |
permissions | -rw-r--r-- |
2 | 1 |
/* |
888
c7009cf0001f
6728492: typo in copyrights in some files touched by the d3d pipeline port
tdv
parents:
887
diff
changeset
|
2 |
* Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.awt; |
|
27 |
||
28 |
import java.awt.AWTPermission; |
|
29 |
import java.awt.DisplayMode; |
|
30 |
import java.awt.GraphicsEnvironment; |
|
31 |
import java.awt.GraphicsDevice; |
|
32 |
import java.awt.GraphicsConfiguration; |
|
33 |
import java.awt.Rectangle; |
|
34 |
import java.awt.Window; |
|
35 |
import java.util.ArrayList; |
|
36 |
import java.util.HashSet; |
|
37 |
import java.util.HashMap; |
|
38 |
||
39 |
import sun.java2d.opengl.GLXGraphicsConfig; |
|
40 |
import sun.java2d.loops.SurfaceType; |
|
41 |
||
42 |
/** |
|
43 |
* This is an implementation of a GraphicsDevice object for a single |
|
44 |
* X11 screen. |
|
45 |
* |
|
46 |
* @see GraphicsEnvironment |
|
47 |
* @see GraphicsConfiguration |
|
48 |
*/ |
|
49 |
public class X11GraphicsDevice |
|
50 |
extends GraphicsDevice |
|
51 |
implements DisplayChangedListener |
|
52 |
{ |
|
53 |
int screen; |
|
54 |
HashMap x11ProxyKeyMap = new HashMap(); |
|
55 |
||
56 |
private static AWTPermission fullScreenExclusivePermission; |
|
57 |
private static Boolean xrandrExtSupported; |
|
58 |
private final Object configLock = new Object(); |
|
59 |
private SunDisplayChanger topLevels = new SunDisplayChanger(); |
|
60 |
private DisplayMode origDisplayMode; |
|
61 |
private boolean shutdownHookRegistered; |
|
62 |
||
63 |
public X11GraphicsDevice(int screennum) { |
|
64 |
this.screen = screennum; |
|
65 |
} |
|
66 |
||
67 |
/* |
|
68 |
* Initialize JNI field and method IDs for fields that may be |
|
69 |
* accessed from C. |
|
70 |
*/ |
|
71 |
private static native void initIDs(); |
|
72 |
||
73 |
static { |
|
74 |
if (!GraphicsEnvironment.isHeadless()) { |
|
75 |
initIDs(); |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
/** |
|
80 |
* Returns the X11 screen of the device. |
|
81 |
*/ |
|
82 |
public int getScreen() { |
|
83 |
return screen; |
|
84 |
} |
|
85 |
||
86 |
public Object getProxyKeyFor(SurfaceType st) { |
|
87 |
synchronized (x11ProxyKeyMap) { |
|
88 |
Object o = x11ProxyKeyMap.get(st); |
|
89 |
if (o == null) { |
|
90 |
o = new Object(); |
|
91 |
x11ProxyKeyMap.put(st, o); |
|
92 |
} |
|
93 |
return o; |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Returns the X11 Display of this device. |
|
99 |
* This method is also in MDrawingSurfaceInfo but need it here |
|
100 |
* to be able to allow a GraphicsConfigTemplate to get the Display. |
|
101 |
*/ |
|
102 |
public native long getDisplay(); |
|
103 |
||
104 |
/** |
|
105 |
* Returns the type of the graphics device. |
|
106 |
* @see #TYPE_RASTER_SCREEN |
|
107 |
* @see #TYPE_PRINTER |
|
108 |
* @see #TYPE_IMAGE_BUFFER |
|
109 |
*/ |
|
110 |
public int getType() { |
|
111 |
return TYPE_RASTER_SCREEN; |
|
112 |
} |
|
113 |
||
114 |
/** |
|
115 |
* Returns the identification string associated with this graphics |
|
116 |
* device. |
|
117 |
*/ |
|
118 |
public String getIDstring() { |
|
119 |
return ":0."+screen; |
|
120 |
} |
|
121 |
||
122 |
||
123 |
GraphicsConfiguration[] configs; |
|
124 |
GraphicsConfiguration defaultConfig; |
|
125 |
HashSet doubleBufferVisuals; |
|
126 |
||
127 |
/** |
|
128 |
* Returns all of the graphics |
|
129 |
* configurations associated with this graphics device. |
|
130 |
*/ |
|
131 |
public GraphicsConfiguration[] getConfigurations() { |
|
132 |
if (configs == null) { |
|
133 |
synchronized (configLock) { |
|
134 |
makeConfigurations(); |
|
135 |
} |
|
136 |
} |
|
137 |
return configs; |
|
138 |
} |
|
139 |
||
140 |
private void makeConfigurations() { |
|
141 |
if (configs == null) { |
|
142 |
int i = 1; // Index 0 is always the default config |
|
143 |
int num = getNumConfigs(screen); |
|
144 |
GraphicsConfiguration[] ret = new GraphicsConfiguration[num]; |
|
145 |
if (defaultConfig == null) { |
|
146 |
ret [0] = getDefaultConfiguration(); |
|
147 |
} |
|
148 |
else { |
|
149 |
ret [0] = defaultConfig; |
|
150 |
} |
|
151 |
||
152 |
boolean glxSupported = X11GraphicsEnvironment.isGLXAvailable(); |
|
153 |
boolean dbeSupported = isDBESupported(); |
|
154 |
if (dbeSupported && doubleBufferVisuals == null) { |
|
155 |
doubleBufferVisuals = new HashSet(); |
|
156 |
getDoubleBufferVisuals(screen); |
|
157 |
} |
|
158 |
for ( ; i < num; i++) { |
|
159 |
int visNum = getConfigVisualId(i, screen); |
|
160 |
int depth = getConfigDepth (i, screen); |
|
161 |
if (glxSupported) { |
|
162 |
ret[i] = GLXGraphicsConfig.getConfig(this, visNum); |
|
163 |
} |
|
164 |
if (ret[i] == null) { |
|
165 |
boolean doubleBuffer = |
|
166 |
(dbeSupported && |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
167 |
doubleBufferVisuals.contains(Integer.valueOf(visNum))); |
2 | 168 |
ret[i] = X11GraphicsConfig.getConfig(this, visNum, depth, |
169 |
getConfigColormap(i, screen), |
|
170 |
doubleBuffer); |
|
171 |
} |
|
172 |
} |
|
173 |
configs = ret; |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
/* |
|
178 |
* Returns the number of X11 visuals representable as an |
|
179 |
* X11GraphicsConfig object. |
|
180 |
*/ |
|
181 |
public native int getNumConfigs(int screen); |
|
182 |
||
183 |
/* |
|
184 |
* Returns the visualid for the given index of graphics configurations. |
|
185 |
*/ |
|
186 |
public native int getConfigVisualId (int index, int screen); |
|
187 |
/* |
|
188 |
* Returns the depth for the given index of graphics configurations. |
|
189 |
*/ |
|
190 |
public native int getConfigDepth (int index, int screen); |
|
191 |
||
192 |
/* |
|
193 |
* Returns the colormap for the given index of graphics configurations. |
|
194 |
*/ |
|
195 |
public native int getConfigColormap (int index, int screen); |
|
196 |
||
197 |
||
198 |
// Whether or not double-buffering extension is supported |
|
199 |
public static native boolean isDBESupported(); |
|
200 |
// Callback for adding a new double buffer visual into our set |
|
201 |
private void addDoubleBufferVisual(int visNum) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
202 |
doubleBufferVisuals.add(Integer.valueOf(visNum)); |
2 | 203 |
} |
204 |
// Enumerates all visuals that support double buffering |
|
205 |
private native void getDoubleBufferVisuals(int screen); |
|
206 |
||
207 |
/** |
|
208 |
* Returns the default graphics configuration |
|
209 |
* associated with this graphics device. |
|
210 |
*/ |
|
211 |
public GraphicsConfiguration getDefaultConfiguration() { |
|
212 |
if (defaultConfig == null) { |
|
213 |
synchronized (configLock) { |
|
214 |
makeDefaultConfiguration(); |
|
215 |
} |
|
216 |
} |
|
217 |
return defaultConfig; |
|
218 |
} |
|
219 |
||
220 |
private void makeDefaultConfiguration() { |
|
221 |
if (defaultConfig == null) { |
|
222 |
int visNum = getConfigVisualId(0, screen); |
|
223 |
if (X11GraphicsEnvironment.isGLXAvailable()) { |
|
224 |
defaultConfig = GLXGraphicsConfig.getConfig(this, visNum); |
|
225 |
if (X11GraphicsEnvironment.isGLXVerbose()) { |
|
226 |
if (defaultConfig != null) { |
|
227 |
System.out.print("OpenGL pipeline enabled"); |
|
228 |
} else { |
|
229 |
System.out.print("Could not enable OpenGL pipeline"); |
|
230 |
} |
|
231 |
System.out.println(" for default config on screen " + |
|
232 |
screen); |
|
233 |
} |
|
234 |
} |
|
235 |
if (defaultConfig == null) { |
|
236 |
int depth = getConfigDepth(0, screen); |
|
237 |
boolean doubleBuffer = false; |
|
238 |
if (isDBESupported() && doubleBufferVisuals == null) { |
|
239 |
doubleBufferVisuals = new HashSet(); |
|
240 |
getDoubleBufferVisuals(screen); |
|
241 |
doubleBuffer = |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
242 |
doubleBufferVisuals.contains(Integer.valueOf(visNum)); |
2 | 243 |
} |
244 |
defaultConfig = X11GraphicsConfig.getConfig(this, visNum, |
|
245 |
depth, getConfigColormap(0, screen), |
|
246 |
doubleBuffer); |
|
247 |
} |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
private static native void enterFullScreenExclusive(long window); |
|
252 |
private static native void exitFullScreenExclusive(long window); |
|
253 |
private static native boolean initXrandrExtension(); |
|
254 |
private static native DisplayMode getCurrentDisplayMode(int screen); |
|
255 |
private static native void enumDisplayModes(int screen, |
|
256 |
ArrayList<DisplayMode> modes); |
|
257 |
private static native void configDisplayMode(int screen, |
|
258 |
int width, int height, |
|
259 |
int displayMode); |
|
260 |
private static native void resetNativeData(int screen); |
|
261 |
||
262 |
/** |
|
263 |
* Returns true only if: |
|
264 |
* - the Xrandr extension is present |
|
265 |
* - the necessary Xrandr functions were loaded successfully |
|
266 |
* - XINERAMA is not enabled |
|
267 |
*/ |
|
268 |
private static synchronized boolean isXrandrExtensionSupported() { |
|
269 |
if (xrandrExtSupported == null) { |
|
270 |
xrandrExtSupported = |
|
271 |
Boolean.valueOf(initXrandrExtension()); |
|
272 |
} |
|
273 |
return xrandrExtSupported.booleanValue(); |
|
274 |
} |
|
275 |
||
276 |
@Override |
|
277 |
public boolean isFullScreenSupported() { |
|
278 |
// REMIND: for now we will only allow fullscreen exclusive mode |
|
279 |
// on the primary screen; we could change this behavior slightly |
|
280 |
// in the future by allowing only one screen to be in fullscreen |
|
281 |
// exclusive mode at any given time... |
|
282 |
boolean fsAvailable = (screen == 0) && isXrandrExtensionSupported(); |
|
283 |
if (fsAvailable) { |
|
284 |
SecurityManager security = System.getSecurityManager(); |
|
285 |
if (security != null) { |
|
286 |
if (fullScreenExclusivePermission == null) { |
|
287 |
fullScreenExclusivePermission = |
|
288 |
new AWTPermission("fullScreenExclusive"); |
|
289 |
} |
|
290 |
try { |
|
291 |
security.checkPermission(fullScreenExclusivePermission); |
|
292 |
} catch (SecurityException e) { |
|
293 |
return false; |
|
294 |
} |
|
295 |
} |
|
296 |
} |
|
297 |
return fsAvailable; |
|
298 |
} |
|
299 |
||
300 |
@Override |
|
301 |
public boolean isDisplayChangeSupported() { |
|
302 |
return (isFullScreenSupported() && (getFullScreenWindow() != null)); |
|
303 |
} |
|
304 |
||
305 |
private static void enterFullScreenExclusive(Window w) { |
|
306 |
X11ComponentPeer peer = (X11ComponentPeer)w.getPeer(); |
|
307 |
if (peer != null) { |
|
308 |
enterFullScreenExclusive(peer.getContentWindow()); |
|
309 |
} |
|
310 |
} |
|
311 |
||
312 |
private static void exitFullScreenExclusive(Window w) { |
|
313 |
X11ComponentPeer peer = (X11ComponentPeer)w.getPeer(); |
|
314 |
if (peer != null) { |
|
315 |
exitFullScreenExclusive(peer.getContentWindow()); |
|
316 |
} |
|
317 |
} |
|
318 |
||
319 |
@Override |
|
320 |
public synchronized void setFullScreenWindow(Window w) { |
|
321 |
Window old = getFullScreenWindow(); |
|
322 |
if (w == old) { |
|
323 |
return; |
|
324 |
} |
|
325 |
||
326 |
boolean fsSupported = isFullScreenSupported(); |
|
327 |
if (fsSupported && old != null) { |
|
328 |
// enter windowed mode (and restore original display mode) |
|
329 |
exitFullScreenExclusive(old); |
|
330 |
setDisplayMode(origDisplayMode); |
|
331 |
} |
|
332 |
||
333 |
super.setFullScreenWindow(w); |
|
334 |
||
335 |
if (fsSupported && w != null) { |
|
336 |
// save original display mode |
|
337 |
if (origDisplayMode == null) { |
|
338 |
origDisplayMode = getDisplayMode(); |
|
339 |
} |
|
340 |
||
341 |
// enter fullscreen mode |
|
342 |
enterFullScreenExclusive(w); |
|
343 |
} |
|
344 |
} |
|
345 |
||
346 |
private DisplayMode getDefaultDisplayMode() { |
|
347 |
GraphicsConfiguration gc = getDefaultConfiguration(); |
|
348 |
Rectangle r = gc.getBounds(); |
|
349 |
return new DisplayMode(r.width, r.height, |
|
350 |
DisplayMode.BIT_DEPTH_MULTI, |
|
351 |
DisplayMode.REFRESH_RATE_UNKNOWN); |
|
352 |
} |
|
353 |
||
354 |
@Override |
|
355 |
public synchronized DisplayMode getDisplayMode() { |
|
356 |
if (isFullScreenSupported()) { |
|
357 |
return getCurrentDisplayMode(screen); |
|
358 |
} else { |
|
359 |
if (origDisplayMode == null) { |
|
360 |
origDisplayMode = getDefaultDisplayMode(); |
|
361 |
} |
|
362 |
return origDisplayMode; |
|
363 |
} |
|
364 |
} |
|
365 |
||
366 |
@Override |
|
367 |
public synchronized DisplayMode[] getDisplayModes() { |
|
368 |
if (!isFullScreenSupported()) { |
|
369 |
return super.getDisplayModes(); |
|
370 |
} |
|
371 |
ArrayList<DisplayMode> modes = new ArrayList<DisplayMode>(); |
|
372 |
enumDisplayModes(screen, modes); |
|
373 |
DisplayMode[] retArray = new DisplayMode[modes.size()]; |
|
374 |
return modes.toArray(retArray); |
|
375 |
} |
|
376 |
||
377 |
@Override |
|
378 |
public synchronized void setDisplayMode(DisplayMode dm) { |
|
379 |
if (!isDisplayChangeSupported()) { |
|
380 |
super.setDisplayMode(dm); |
|
381 |
return; |
|
382 |
} |
|
383 |
Window w = getFullScreenWindow(); |
|
384 |
if (w == null) { |
|
385 |
throw new IllegalStateException("Must be in fullscreen mode " + |
|
386 |
"in order to set display mode"); |
|
387 |
} |
|
887 | 388 |
if (getDisplayMode().equals(dm)) { |
389 |
return; |
|
390 |
} |
|
2 | 391 |
if (dm == null || |
392 |
(dm = getMatchingDisplayMode(dm)) == null) |
|
393 |
{ |
|
394 |
throw new IllegalArgumentException("Invalid display mode"); |
|
395 |
} |
|
396 |
||
397 |
if (!shutdownHookRegistered) { |
|
398 |
// register a shutdown hook so that we return to the |
|
399 |
// original DisplayMode when the VM exits (if the application |
|
400 |
// is already in the original DisplayMode at that time, this |
|
401 |
// hook will have no effect) |
|
402 |
shutdownHookRegistered = true; |
|
403 |
Runnable r = new Runnable() { |
|
404 |
public void run() { |
|
405 |
Window old = getFullScreenWindow(); |
|
406 |
if (old != null) { |
|
407 |
exitFullScreenExclusive(old); |
|
408 |
setDisplayMode(origDisplayMode); |
|
409 |
} |
|
410 |
} |
|
411 |
}; |
|
412 |
Thread t = new Thread(r,"Display-Change-Shutdown-Thread-"+screen); |
|
413 |
Runtime.getRuntime().addShutdownHook(t); |
|
414 |
} |
|
415 |
||
416 |
// switch to the new DisplayMode |
|
417 |
configDisplayMode(screen, |
|
418 |
dm.getWidth(), dm.getHeight(), |
|
419 |
dm.getRefreshRate()); |
|
420 |
||
421 |
// update bounds of the fullscreen window |
|
422 |
w.setBounds(0, 0, dm.getWidth(), dm.getHeight()); |
|
423 |
||
424 |
// configDisplayMode() is synchronous, so the display change will be |
|
425 |
// complete by the time we get here (and it is therefore safe to call |
|
426 |
// displayChanged() now) |
|
427 |
((X11GraphicsEnvironment) |
|
428 |
GraphicsEnvironment.getLocalGraphicsEnvironment()).displayChanged(); |
|
429 |
} |
|
430 |
||
431 |
private synchronized DisplayMode getMatchingDisplayMode(DisplayMode dm) { |
|
432 |
if (!isDisplayChangeSupported()) { |
|
433 |
return null; |
|
434 |
} |
|
435 |
DisplayMode[] modes = getDisplayModes(); |
|
436 |
for (DisplayMode mode : modes) { |
|
437 |
if (dm.equals(mode) || |
|
438 |
(dm.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN && |
|
439 |
dm.getWidth() == mode.getWidth() && |
|
440 |
dm.getHeight() == mode.getHeight() && |
|
441 |
dm.getBitDepth() == mode.getBitDepth())) |
|
442 |
{ |
|
443 |
return mode; |
|
444 |
} |
|
445 |
} |
|
446 |
return null; |
|
447 |
} |
|
448 |
||
449 |
/** |
|
450 |
* From the DisplayChangedListener interface; called from |
|
451 |
* X11GraphicsEnvironment when the display mode has been changed. |
|
452 |
*/ |
|
453 |
public synchronized void displayChanged() { |
|
454 |
// reset the list of configs (and default config) |
|
455 |
defaultConfig = null; |
|
456 |
configs = null; |
|
457 |
doubleBufferVisuals = null; |
|
458 |
||
459 |
// reset the native data structures associated with this device (they |
|
460 |
// will be reinitialized when the GraphicsConfigs are configured) |
|
461 |
resetNativeData(screen); |
|
462 |
||
463 |
// pass on to all top-level windows on this screen |
|
464 |
topLevels.notifyListeners(); |
|
465 |
} |
|
466 |
||
467 |
/** |
|
468 |
* From the DisplayChangedListener interface; devices do not need |
|
469 |
* to react to this event. |
|
470 |
*/ |
|
471 |
public void paletteChanged() { |
|
472 |
} |
|
473 |
||
474 |
/** |
|
475 |
* Add a DisplayChangeListener to be notified when the display settings |
|
476 |
* are changed. Typically, only top-level containers need to be added |
|
477 |
* to X11GraphicsDevice. |
|
478 |
*/ |
|
479 |
public void addDisplayChangedListener(DisplayChangedListener client) { |
|
480 |
topLevels.add(client); |
|
481 |
} |
|
482 |
||
483 |
/** |
|
484 |
* Remove a DisplayChangeListener from this X11GraphicsDevice. |
|
485 |
*/ |
|
486 |
public void removeDisplayChangedListener(DisplayChangedListener client) { |
|
487 |
topLevels.remove(client); |
|
488 |
} |
|
489 |
||
490 |
public String toString() { |
|
491 |
return ("X11GraphicsDevice[screen="+screen+"]"); |
|
492 |
} |
|
493 |
} |