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