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