1 /*
   2  * Copyright (c) 2012, 2019, 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.awt;
  27 
  28 import java.awt.AWTPermission;
  29 import java.awt.DisplayMode;
  30 import java.awt.GraphicsConfiguration;
  31 import java.awt.GraphicsDevice;
  32 import java.awt.Insets;
  33 import java.awt.Rectangle;
  34 import java.awt.Window;
  35 import java.awt.geom.Rectangle2D;
  36 import java.util.Objects;
  37 
  38 import sun.java2d.SunGraphicsEnvironment;
  39 import sun.java2d.opengl.CGLGraphicsConfig;
  40 
  41 public final class CGraphicsDevice extends GraphicsDevice
  42         implements DisplayChangedListener {
  43 
  44     /**
  45      * CoreGraphics display ID. This identifier can become non-valid at any time
  46      * therefore methods, which is using this id should be ready to it.
  47      */
  48     private volatile int displayID;
  49     private volatile double xResolution;
  50     private volatile double yResolution;
  51     private volatile Rectangle bounds;
  52     private volatile int scale;
  53 
  54     private final GraphicsConfiguration config;
  55 
  56     private static AWTPermission fullScreenExclusivePermission;
  57 
  58     // Save/restore DisplayMode for the Full Screen mode
  59     private DisplayMode originalMode;
  60 
  61     public CGraphicsDevice(final int displayID) {
  62         this.displayID = displayID;
  63         config = CGLGraphicsConfig.getConfig(this, displayID, 0);
  64         // initializes default device state, might be redundant step since we
  65         // call "displayChanged()" later anyway, but we do not want to leave the
  66         // device in an inconsistent state after construction
  67         displayChanged();
  68     }
  69 
  70     /**
  71      * Return a list of all configurations.
  72      */
  73     @Override
  74     public GraphicsConfiguration[] getConfigurations() {
  75         return new GraphicsConfiguration[]{config};
  76     }
  77 
  78     /**
  79      * Return the default configuration.
  80      */
  81     @Override
  82     public GraphicsConfiguration getDefaultConfiguration() {
  83         return config;
  84     }
  85 
  86     /**
  87      * Return a human-readable screen description.
  88      */
  89     @Override
  90     public String getIDstring() {
  91         return "Display " + displayID;
  92     }
  93 
  94     /**
  95      * Returns the type of the graphics device.
  96      * @see #TYPE_RASTER_SCREEN
  97      * @see #TYPE_PRINTER
  98      * @see #TYPE_IMAGE_BUFFER
  99      */
 100     @Override
 101     public int getType() {
 102         return TYPE_RASTER_SCREEN;
 103     }
 104 
 105     public double getXResolution() {
 106         return xResolution;
 107     }
 108 
 109     public double getYResolution() {
 110         return yResolution;
 111     }
 112 
 113     Rectangle getBounds() {
 114         return bounds.getBounds();
 115     }
 116 
 117     public Insets getScreenInsets() {
 118         // the insets are queried synchronously and are not cached
 119         // since there are no Quartz or Cocoa means to receive notifications
 120         // on insets changes (e.g. when the Dock is resized):
 121         // the existing CGDisplayReconfigurationCallBack is not notified
 122         // as well as the NSApplicationDidChangeScreenParametersNotification
 123         // is fired on the Dock location changes only
 124         return nativeGetScreenInsets(displayID);
 125     }
 126 
 127     public int getScaleFactor() {
 128         return scale;
 129     }
 130 
 131     public void invalidate(final int defaultDisplayID) {
 132         displayID = defaultDisplayID;
 133     }
 134 
 135     @Override
 136     public void displayChanged() {
 137         xResolution = nativeGetXResolution(displayID);
 138         yResolution = nativeGetYResolution(displayID);
 139         bounds = nativeGetBounds(displayID).getBounds(); //does integer rounding
 140         initScaleFactor();
 141         //TODO configs/fullscreenWindow/modes?
 142     }
 143 
 144     @Override
 145     public void paletteChanged() {
 146         // devices do not need to react to this event.
 147     }
 148 
 149     /**
 150      * Enters full-screen mode, or returns to windowed mode.
 151      */
 152     @Override
 153     public synchronized void setFullScreenWindow(Window w) {
 154         Window old = getFullScreenWindow();
 155         if (w == old) {
 156             return;
 157         }
 158 
 159         boolean fsSupported = isFullScreenSupported();
 160 
 161         if (fsSupported && old != null) {
 162             // enter windowed mode and restore original display mode
 163             exitFullScreenExclusive(old);
 164             if (originalMode != null) {
 165                 setDisplayMode(originalMode);
 166                 originalMode = null;
 167             }
 168         }
 169 
 170         super.setFullScreenWindow(w);
 171 
 172         if (fsSupported && w != null) {
 173             if (isDisplayChangeSupported()) {
 174                 originalMode = getDisplayMode();
 175             }
 176             // enter fullscreen mode
 177             enterFullScreenExclusive(w);
 178         }
 179     }
 180 
 181     /**
 182      * Returns true if this GraphicsDevice supports
 183      * full-screen exclusive mode and false otherwise.
 184      */
 185     @Override
 186     public boolean isFullScreenSupported() {
 187         return isFSExclusiveModeAllowed();
 188     }
 189 
 190     private static boolean isFSExclusiveModeAllowed() {
 191         SecurityManager security = System.getSecurityManager();
 192         if (security != null) {
 193             if (fullScreenExclusivePermission == null) {
 194                 fullScreenExclusivePermission =
 195                     new AWTPermission("fullScreenExclusive");
 196             }
 197             try {
 198                 security.checkPermission(fullScreenExclusivePermission);
 199             } catch (SecurityException e) {
 200                 return false;
 201             }
 202         }
 203         return true;
 204     }
 205 
 206     private static void enterFullScreenExclusive(Window w) {
 207         FullScreenCapable peer = AWTAccessor.getComponentAccessor().getPeer(w);
 208         if (peer != null) {
 209             peer.enterFullScreenMode();
 210         }
 211     }
 212 
 213     private static void exitFullScreenExclusive(Window w) {
 214         FullScreenCapable peer = AWTAccessor.getComponentAccessor().getPeer(w);
 215         if (peer != null) {
 216             peer.exitFullScreenMode();
 217         }
 218     }
 219 
 220     @Override
 221     public boolean isDisplayChangeSupported() {
 222         return true;
 223     }
 224 
 225     @Override
 226     public void setDisplayMode(final DisplayMode dm) {
 227         if (dm == null) {
 228             throw new IllegalArgumentException("Invalid display mode");
 229         }
 230         if (!Objects.equals(dm, getDisplayMode())) {
 231             nativeSetDisplayMode(displayID, dm.getWidth(), dm.getHeight(),
 232                     dm.getBitDepth(), dm.getRefreshRate());
 233             if (isFullScreenSupported() && getFullScreenWindow() != null) {
 234                 getFullScreenWindow().setSize(dm.getWidth(), dm.getHeight());
 235             }
 236         }
 237     }
 238 
 239     @Override
 240     public DisplayMode getDisplayMode() {
 241         return nativeGetDisplayMode(displayID);
 242     }
 243 
 244     @Override
 245     public DisplayMode[] getDisplayModes() {
 246         return nativeGetDisplayModes(displayID);
 247     }
 248 
 249     private void initScaleFactor() {
 250         if (SunGraphicsEnvironment.isUIScaleEnabled()) {
 251             double debugScale = SunGraphicsEnvironment.getDebugScale();
 252             scale = (int) (debugScale >= 1
 253                     ? Math.round(debugScale)
 254                     : nativeGetScaleFactor(displayID));
 255         } else {
 256             scale = 1;
 257         }
 258     }
 259 
 260     private static native double nativeGetScaleFactor(int displayID);
 261 
 262     private static native void nativeSetDisplayMode(int displayID, int w, int h, int bpp, int refrate);
 263 
 264     private static native DisplayMode nativeGetDisplayMode(int displayID);
 265 
 266     private static native DisplayMode[] nativeGetDisplayModes(int displayID);
 267 
 268     private static native double nativeGetXResolution(int displayID);
 269 
 270     private static native double nativeGetYResolution(int displayID);
 271 
 272     private static native Insets nativeGetScreenInsets(int displayID);
 273 
 274     private static native Rectangle2D nativeGetBounds(int displayID);
 275 }