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