887
|
1 |
/*
|
|
2 |
* Copyright 2007-2008 Sun Microsystems, Inc. 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. 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.java2d.d3d;
|
|
27 |
|
|
28 |
import java.awt.Dialog;
|
|
29 |
import java.awt.DisplayMode;
|
|
30 |
import java.awt.Frame;
|
|
31 |
import java.awt.GraphicsConfiguration;
|
|
32 |
import java.awt.Rectangle;
|
|
33 |
import java.awt.Toolkit;
|
|
34 |
import java.awt.Window;
|
|
35 |
import java.awt.event.WindowAdapter;
|
|
36 |
import java.awt.event.WindowEvent;
|
|
37 |
import java.awt.event.WindowListener;
|
|
38 |
import java.awt.peer.WindowPeer;
|
|
39 |
import java.util.ArrayList;
|
|
40 |
import sun.awt.Win32GraphicsDevice;
|
|
41 |
import sun.awt.windows.WWindowPeer;
|
|
42 |
import sun.java2d.pipe.hw.ContextCapabilities;
|
|
43 |
import sun.java2d.windows.WindowsFlags;
|
|
44 |
import static sun.java2d.pipe.BufferedOpCodes.*;
|
|
45 |
import static sun.java2d.d3d.D3DContext.D3DContextCaps.*;
|
|
46 |
import sun.java2d.d3d.D3DContext.D3DContextCaps;
|
|
47 |
|
|
48 |
/**
|
|
49 |
* This class implements D3D-specific functionality, such as fullscreen
|
|
50 |
* exclusive mode and display changes. It is kept separate from
|
|
51 |
* Win32GraphicsDevice to help avoid overburdening the parent class.
|
|
52 |
*/
|
|
53 |
public class D3DGraphicsDevice extends Win32GraphicsDevice {
|
|
54 |
private D3DContext context;
|
|
55 |
|
|
56 |
private static boolean d3dAvailable;
|
|
57 |
|
|
58 |
private ContextCapabilities d3dCaps;
|
|
59 |
|
|
60 |
private static native boolean initD3D();
|
|
61 |
|
|
62 |
static {
|
|
63 |
// loading the library doesn't help because we need the
|
|
64 |
// toolkit thread running, so we have to call getDefaultToolkit()
|
|
65 |
Toolkit.getDefaultToolkit();
|
|
66 |
d3dAvailable = initD3D();
|
|
67 |
if (d3dAvailable) {
|
|
68 |
// we don't use pixel formats for the d3d pipeline
|
|
69 |
pfDisabled = true;
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Used to construct a Direct3D-enabled GraphicsDevice.
|
|
75 |
*
|
|
76 |
* @return a D3DGraphicsDevice if it could be created
|
|
77 |
* successfully, null otherwise.
|
|
78 |
*/
|
|
79 |
public static D3DGraphicsDevice createDevice(int screen) {
|
|
80 |
if (!d3dAvailable) {
|
|
81 |
return null;
|
|
82 |
}
|
|
83 |
|
|
84 |
ContextCapabilities d3dCaps = getDeviceCaps(screen);
|
|
85 |
// could not initialize the device successfully
|
|
86 |
if ((d3dCaps.getCaps() & CAPS_DEVICE_OK) == 0) {
|
|
87 |
if (WindowsFlags.isD3DVerbose()) {
|
|
88 |
System.out.println("Could not enable Direct3D pipeline on " +
|
|
89 |
"screen " + screen);
|
|
90 |
}
|
|
91 |
return null;
|
|
92 |
}
|
|
93 |
if (WindowsFlags.isD3DVerbose()) {
|
|
94 |
System.out.println("Direct3D pipeline enabled on screen " + screen);
|
|
95 |
}
|
|
96 |
|
|
97 |
D3DGraphicsDevice gd = new D3DGraphicsDevice(screen, d3dCaps);
|
|
98 |
return gd;
|
|
99 |
}
|
|
100 |
|
|
101 |
private static native int getDeviceCapsNative(int screen);
|
|
102 |
private static native String getDeviceIdNative(int screen);
|
|
103 |
private static ContextCapabilities getDeviceCaps(final int screen) {
|
|
104 |
ContextCapabilities d3dCaps = null;
|
|
105 |
D3DRenderQueue rq = D3DRenderQueue.getInstance();
|
|
106 |
rq.lock();
|
|
107 |
try {
|
|
108 |
class Result {
|
|
109 |
int caps;
|
|
110 |
String id;
|
|
111 |
};
|
|
112 |
final Result res = new Result();
|
|
113 |
rq.flushAndInvokeNow(new Runnable() {
|
|
114 |
public void run() {
|
|
115 |
res.caps = getDeviceCapsNative(screen);
|
|
116 |
res.id = getDeviceIdNative(screen);
|
|
117 |
}
|
|
118 |
});
|
|
119 |
d3dCaps = new D3DContextCaps(res.caps, res.id);
|
|
120 |
} finally {
|
|
121 |
rq.unlock();
|
|
122 |
}
|
|
123 |
|
|
124 |
return d3dCaps != null ? d3dCaps : new D3DContextCaps(CAPS_EMPTY, null);
|
|
125 |
}
|
|
126 |
|
|
127 |
public final boolean isCapPresent(int cap) {
|
|
128 |
return ((d3dCaps.getCaps() & cap) != 0);
|
|
129 |
}
|
|
130 |
|
|
131 |
private D3DGraphicsDevice(int screennum, ContextCapabilities d3dCaps) {
|
|
132 |
super(screennum);
|
|
133 |
descString = "D3DGraphicsDevice[screen="+screennum;
|
|
134 |
this.d3dCaps = d3dCaps;
|
|
135 |
context = new D3DContext(D3DRenderQueue.getInstance(), this);
|
|
136 |
}
|
|
137 |
|
|
138 |
public boolean isD3DEnabledOnDevice() {
|
|
139 |
return isValid() && isCapPresent(CAPS_DEVICE_OK);
|
|
140 |
}
|
|
141 |
|
|
142 |
/**
|
|
143 |
* Returns true if d3d pipeline has been successfully initialized.
|
|
144 |
* @return true if d3d pipeline is initialized, false otherwise
|
|
145 |
*/
|
|
146 |
public static boolean isD3DAvailable() {
|
|
147 |
return d3dAvailable;
|
|
148 |
}
|
|
149 |
|
|
150 |
/**
|
|
151 |
* Return the owning Frame for a given Window. Used in setFSWindow below
|
|
152 |
* to set the properties of the owning Frame when a Window goes
|
|
153 |
* into fullscreen mode.
|
|
154 |
*/
|
|
155 |
private Frame getToplevelOwner(Window w) {
|
|
156 |
Window owner = w;
|
|
157 |
while (owner != null) {
|
|
158 |
owner = owner.getOwner();
|
|
159 |
if (owner instanceof Frame) {
|
|
160 |
return (Frame) owner;
|
|
161 |
}
|
|
162 |
}
|
|
163 |
// could get here if passed Window is an owner-less Dialog
|
|
164 |
return null;
|
|
165 |
}
|
|
166 |
|
|
167 |
private boolean fsStatus;
|
|
168 |
private Rectangle ownerOrigBounds = null;
|
|
169 |
private boolean ownerWasVisible;
|
|
170 |
private Window realFSWindow;
|
|
171 |
private WindowListener fsWindowListener;
|
|
172 |
private boolean fsWindowWasAlwaysOnTop;
|
|
173 |
private static native boolean enterFullScreenExclusiveNative(int screen,
|
|
174 |
long hwnd);
|
|
175 |
|
|
176 |
@Override
|
|
177 |
protected void enterFullScreenExclusive(final int screen, WindowPeer wp)
|
|
178 |
{
|
|
179 |
final WWindowPeer wpeer = (WWindowPeer)realFSWindow.getPeer();
|
|
180 |
|
|
181 |
D3DRenderQueue rq = D3DRenderQueue.getInstance();
|
|
182 |
rq.lock();
|
|
183 |
try {
|
|
184 |
rq.flushAndInvokeNow(new Runnable() {
|
|
185 |
public void run() {
|
|
186 |
long hwnd = wpeer.getHWnd();
|
|
187 |
if (hwnd == 0l) {
|
|
188 |
// window is disposed
|
|
189 |
fsStatus = false;
|
|
190 |
return;
|
|
191 |
}
|
|
192 |
fsStatus = enterFullScreenExclusiveNative(screen, hwnd);
|
|
193 |
}
|
|
194 |
});
|
|
195 |
} finally {
|
|
196 |
rq.unlock();
|
|
197 |
}
|
|
198 |
if (!fsStatus) {
|
|
199 |
super.enterFullScreenExclusive(screen, wp);
|
|
200 |
}
|
|
201 |
}
|
|
202 |
|
|
203 |
private static native boolean exitFullScreenExclusiveNative(int screen);
|
|
204 |
@Override
|
|
205 |
protected void exitFullScreenExclusive(final int screen, WindowPeer w) {
|
|
206 |
if (fsStatus) {
|
|
207 |
D3DRenderQueue rq = D3DRenderQueue.getInstance();
|
|
208 |
rq.lock();
|
|
209 |
try {
|
|
210 |
rq.flushAndInvokeNow(new Runnable() {
|
|
211 |
public void run() {
|
|
212 |
exitFullScreenExclusiveNative(screen);
|
|
213 |
}
|
|
214 |
});
|
|
215 |
} finally {
|
|
216 |
rq.unlock();
|
|
217 |
}
|
|
218 |
} else {
|
|
219 |
super.exitFullScreenExclusive(screen, w);
|
|
220 |
}
|
|
221 |
}
|
|
222 |
|
|
223 |
/**
|
|
224 |
* WindowAdapter class for the full-screen frame, responsible for
|
|
225 |
* restoring the devices. This is important to do because unless the device
|
|
226 |
* is restored it will not go back into the FS mode once alt+tabbed out.
|
|
227 |
* This is a problem for windows for which we do not do any d3d-related
|
|
228 |
* operations (like when we disabled on-screen rendering).
|
|
229 |
*
|
|
230 |
* REMIND: we create an instance per each full-screen device while a single
|
|
231 |
* instance would suffice (but requires more management).
|
|
232 |
*/
|
|
233 |
private static class D3DFSWindowAdapter extends WindowAdapter {
|
|
234 |
@Override
|
|
235 |
public void windowDeactivated(WindowEvent e) {
|
|
236 |
D3DRenderQueue.getInstance().restoreDevices();
|
|
237 |
}
|
|
238 |
@Override
|
|
239 |
public void windowActivated(WindowEvent e) {
|
|
240 |
D3DRenderQueue.getInstance().restoreDevices();
|
|
241 |
}
|
|
242 |
}
|
|
243 |
|
|
244 |
@Override
|
|
245 |
protected void addFSWindowListener(Window w) {
|
|
246 |
// if the window is not a toplevel (has an owner) we have to use the
|
|
247 |
// real toplevel to enter the full-screen mode with (4933099).
|
|
248 |
if (!(w instanceof Frame) && !(w instanceof Dialog) &&
|
|
249 |
(realFSWindow = getToplevelOwner(w)) != null)
|
|
250 |
{
|
|
251 |
ownerOrigBounds = realFSWindow.getBounds();
|
|
252 |
WWindowPeer fp = (WWindowPeer)realFSWindow.getPeer();
|
|
253 |
|
|
254 |
ownerWasVisible = realFSWindow.isVisible();
|
|
255 |
Rectangle r = w.getBounds();
|
|
256 |
// we use operations on peer instead of component because calling
|
|
257 |
// them on component will take the tree lock
|
|
258 |
fp.reshape(r.x, r.y, r.width, r.height);
|
|
259 |
fp.setVisible(true);
|
|
260 |
} else {
|
|
261 |
realFSWindow = w;
|
|
262 |
}
|
|
263 |
|
|
264 |
fsWindowWasAlwaysOnTop = realFSWindow.isAlwaysOnTop();
|
|
265 |
((WWindowPeer)realFSWindow.getPeer()).setAlwaysOnTop(true);
|
|
266 |
|
|
267 |
fsWindowListener = new D3DFSWindowAdapter();
|
|
268 |
realFSWindow.addWindowListener(fsWindowListener);
|
|
269 |
}
|
|
270 |
|
|
271 |
@Override
|
|
272 |
protected void removeFSWindowListener(Window w) {
|
|
273 |
realFSWindow.removeWindowListener(fsWindowListener);
|
|
274 |
fsWindowListener = null;
|
|
275 |
|
|
276 |
/**
|
|
277 |
* Bug 4933099: There is some funny-business to deal with when this
|
|
278 |
* method is called with a Window instead of a Frame. See 4836744
|
|
279 |
* for more information on this. One side-effect of our workaround
|
|
280 |
* for the problem is that the owning Frame of a Window may end
|
|
281 |
* up getting resized during the fullscreen process. When we
|
|
282 |
* return from fullscreen mode, we should resize the Frame to
|
|
283 |
* its original size (just like the Window is being resized
|
|
284 |
* to its original size in GraphicsDevice).
|
|
285 |
*/
|
|
286 |
WWindowPeer wpeer = (WWindowPeer)realFSWindow.getPeer();
|
|
287 |
if (wpeer != null) {
|
|
288 |
if (ownerOrigBounds != null) {
|
|
289 |
// if the window went into fs mode before it was realized it
|
|
290 |
// could have (0,0) dimensions
|
|
291 |
if (ownerOrigBounds.width == 0) ownerOrigBounds.width = 1;
|
|
292 |
if (ownerOrigBounds.height == 0) ownerOrigBounds.height = 1;
|
|
293 |
wpeer.reshape(ownerOrigBounds.x, ownerOrigBounds.y,
|
|
294 |
ownerOrigBounds.width, ownerOrigBounds.height);
|
|
295 |
if (!ownerWasVisible) {
|
|
296 |
wpeer.setVisible(false);
|
|
297 |
}
|
|
298 |
ownerOrigBounds = null;
|
|
299 |
}
|
|
300 |
if (!fsWindowWasAlwaysOnTop) {
|
|
301 |
wpeer.setAlwaysOnTop(false);
|
|
302 |
}
|
|
303 |
}
|
|
304 |
|
|
305 |
realFSWindow = null;
|
|
306 |
}
|
|
307 |
|
|
308 |
private static native DisplayMode getCurrentDisplayModeNative(int screen);
|
|
309 |
@Override
|
|
310 |
protected DisplayMode getCurrentDisplayMode(final int screen) {
|
|
311 |
D3DRenderQueue rq = D3DRenderQueue.getInstance();
|
|
312 |
rq.lock();
|
|
313 |
try {
|
|
314 |
class Result {
|
|
315 |
DisplayMode dm = null;
|
|
316 |
};
|
|
317 |
final Result res = new Result();
|
|
318 |
rq.flushAndInvokeNow(new Runnable() {
|
|
319 |
public void run() {
|
|
320 |
res.dm = getCurrentDisplayModeNative(screen);
|
|
321 |
}
|
|
322 |
});
|
|
323 |
if (res.dm == null) {
|
|
324 |
return super.getCurrentDisplayMode(screen);
|
|
325 |
}
|
|
326 |
return res.dm;
|
|
327 |
} finally {
|
|
328 |
rq.unlock();
|
|
329 |
}
|
|
330 |
}
|
|
331 |
private static native void configDisplayModeNative(int screen, long hwnd,
|
|
332 |
int width, int height,
|
|
333 |
int bitDepth,
|
|
334 |
int refreshRate);
|
|
335 |
@Override
|
|
336 |
protected void configDisplayMode(final int screen, final WindowPeer w,
|
|
337 |
final int width, final int height,
|
|
338 |
final int bitDepth, final int refreshRate)
|
|
339 |
{
|
|
340 |
// we entered fs mode via gdi
|
|
341 |
if (!fsStatus) {
|
|
342 |
super.configDisplayMode(screen, w, width, height, bitDepth,
|
|
343 |
refreshRate);
|
|
344 |
return;
|
|
345 |
}
|
|
346 |
|
|
347 |
final WWindowPeer wpeer = (WWindowPeer)realFSWindow.getPeer();
|
|
348 |
|
|
349 |
// REMIND: we do this before we switch the display mode, so
|
|
350 |
// the dimensions may be exceeding the dimensions of the screen,
|
|
351 |
// is this a problem?
|
|
352 |
|
|
353 |
// update the bounds of the owner frame
|
|
354 |
if (getFullScreenWindow() != realFSWindow) {
|
|
355 |
Rectangle screenBounds = getDefaultConfiguration().getBounds();
|
|
356 |
wpeer.reshape(screenBounds.x, screenBounds.y, width, height);
|
|
357 |
}
|
|
358 |
|
|
359 |
D3DRenderQueue rq = D3DRenderQueue.getInstance();
|
|
360 |
rq.lock();
|
|
361 |
try {
|
|
362 |
rq.flushAndInvokeNow(new Runnable() {
|
|
363 |
public void run() {
|
|
364 |
long hwnd = wpeer.getHWnd();
|
|
365 |
if (hwnd == 0l) {
|
|
366 |
// window is disposed
|
|
367 |
return;
|
|
368 |
}
|
|
369 |
// REMIND: do we really need a window here?
|
|
370 |
// we should probably just use the current one
|
|
371 |
configDisplayModeNative(screen, hwnd, width, height,
|
|
372 |
bitDepth, refreshRate);
|
|
373 |
}
|
|
374 |
});
|
|
375 |
} finally {
|
|
376 |
rq.unlock();
|
|
377 |
}
|
|
378 |
}
|
|
379 |
|
|
380 |
private static native void enumDisplayModesNative(int screen,
|
|
381 |
ArrayList modes);
|
|
382 |
@Override
|
|
383 |
protected void enumDisplayModes(final int screen, final ArrayList modes) {
|
|
384 |
D3DRenderQueue rq = D3DRenderQueue.getInstance();
|
|
385 |
rq.lock();
|
|
386 |
try {
|
|
387 |
rq.flushAndInvokeNow(new Runnable() {
|
|
388 |
public void run() {
|
|
389 |
enumDisplayModesNative(screen, modes);
|
|
390 |
}
|
|
391 |
});
|
|
392 |
if (modes.size() == 0) {
|
|
393 |
modes.add(getCurrentDisplayModeNative(screen));
|
|
394 |
}
|
|
395 |
} finally {
|
|
396 |
rq.unlock();
|
|
397 |
}
|
|
398 |
}
|
|
399 |
|
|
400 |
private static native long getAvailableAcceleratedMemoryNative(int screen);
|
|
401 |
@Override
|
|
402 |
public int getAvailableAcceleratedMemory() {
|
|
403 |
D3DRenderQueue rq = D3DRenderQueue.getInstance();
|
|
404 |
rq.lock();
|
|
405 |
try {
|
|
406 |
class Result {
|
|
407 |
long mem = 0L;
|
|
408 |
};
|
|
409 |
final Result res = new Result();
|
|
410 |
rq.flushAndInvokeNow(new Runnable() {
|
|
411 |
public void run() {
|
|
412 |
res.mem = getAvailableAcceleratedMemoryNative(getScreen());
|
|
413 |
}
|
|
414 |
});
|
|
415 |
return (int)res.mem;
|
|
416 |
} finally {
|
|
417 |
rq.unlock();
|
|
418 |
}
|
|
419 |
}
|
|
420 |
|
|
421 |
@Override
|
|
422 |
public GraphicsConfiguration[] getConfigurations() {
|
|
423 |
if (configs == null) {
|
|
424 |
if (isD3DEnabledOnDevice()) {
|
|
425 |
defaultConfig = getDefaultConfiguration();
|
|
426 |
if (defaultConfig != null) {
|
|
427 |
configs = new GraphicsConfiguration[1];
|
|
428 |
configs[0] = defaultConfig;
|
|
429 |
return configs;
|
|
430 |
}
|
|
431 |
}
|
|
432 |
}
|
|
433 |
return super.getConfigurations();
|
|
434 |
}
|
|
435 |
|
|
436 |
@Override
|
|
437 |
public GraphicsConfiguration getDefaultConfiguration() {
|
|
438 |
if (defaultConfig == null) {
|
|
439 |
if (isD3DEnabledOnDevice()) {
|
|
440 |
defaultConfig = new D3DGraphicsConfig(this);
|
|
441 |
} else {
|
|
442 |
defaultConfig = super.getDefaultConfiguration();
|
|
443 |
}
|
|
444 |
}
|
|
445 |
return defaultConfig;
|
|
446 |
}
|
|
447 |
|
|
448 |
private static native boolean isD3DAvailableOnDeviceNative(int screen);
|
|
449 |
// REMIND: this method is not used now, we use caps instead
|
|
450 |
public static boolean isD3DAvailableOnDevice(final int screen) {
|
|
451 |
if (!d3dAvailable) {
|
|
452 |
return false;
|
|
453 |
}
|
|
454 |
|
|
455 |
// REMIND: should we cache the result per device somehow,
|
|
456 |
// and then reset and retry it on display change?
|
|
457 |
D3DRenderQueue rq = D3DRenderQueue.getInstance();
|
|
458 |
rq.lock();
|
|
459 |
try {
|
|
460 |
class Result {
|
|
461 |
boolean avail = false;
|
|
462 |
};
|
|
463 |
final Result res = new Result();
|
|
464 |
rq.flushAndInvokeNow(new Runnable() {
|
|
465 |
public void run() {
|
|
466 |
res.avail = isD3DAvailableOnDeviceNative(screen);
|
|
467 |
}
|
|
468 |
});
|
|
469 |
return res.avail;
|
|
470 |
} finally {
|
|
471 |
rq.unlock();
|
|
472 |
}
|
|
473 |
}
|
|
474 |
|
|
475 |
D3DContext getContext() {
|
|
476 |
return context;
|
|
477 |
}
|
|
478 |
|
|
479 |
ContextCapabilities getContextCapabilities() {
|
|
480 |
return d3dCaps;
|
|
481 |
}
|
|
482 |
|
|
483 |
@Override
|
|
484 |
public void displayChanged() {
|
|
485 |
super.displayChanged();
|
|
486 |
// REMIND: make sure this works when the device is lost and we don't
|
|
487 |
// disable d3d too eagerly
|
|
488 |
if (d3dAvailable) {
|
|
489 |
d3dCaps = getDeviceCaps(getScreen());
|
|
490 |
}
|
|
491 |
}
|
|
492 |
|
|
493 |
@Override
|
|
494 |
protected void invalidate(int defaultScreen) {
|
|
495 |
super.invalidate(defaultScreen);
|
|
496 |
// REMIND: this is a bit excessive, isD3DEnabledOnDevice will return
|
|
497 |
// false anyway because the device is invalid
|
|
498 |
d3dCaps = new D3DContextCaps(CAPS_EMPTY, null);
|
|
499 |
}
|
|
500 |
}
|