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